Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 25 | import android.content.pm.UserInfo; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 26 | import android.os.UserHandle; |
| 27 | import android.os.UserManager; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 28 | import android.util.Log; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static android.content.pm.PackageManager.GET_ACTIVITIES; |
| 33 | import static android.content.pm.PackageManager.GET_META_DATA; |
| 34 | import static android.content.pm.PackageManager.GET_RESOLVED_FILTER; |
Tony Mak | 82d9096 | 2016-05-04 13:51:41 +0100 | [diff] [blame] | 35 | import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 36 | |
Fan Zhang | 409eab1 | 2018-01-29 11:23:52 -0800 | [diff] [blame] | 37 | import com.android.settings.shortcut.CreateShortcut; |
| 38 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 39 | /** |
Kenny Guy | 4761da3 | 2017-05-16 18:54:21 +0100 | [diff] [blame] | 40 | * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED} |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 41 | * performs setup steps for a managed profile (disables the launcher icon of the Settings app, |
| 42 | * adds cross-profile intent filters for the appropriate Settings activities), and disables the |
| 43 | * webview setting for non-admin users. |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 44 | */ |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 45 | public class SettingsInitialize extends BroadcastReceiver { |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 46 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 47 | private static final String PRIMARY_PROFILE_SETTING = |
| 48 | "com.android.settings.PRIMARY_PROFILE_CONTROLLED"; |
Gustav Sennton | 4d3334c | 2016-12-13 19:16:48 +0000 | [diff] [blame] | 49 | private static final String SETTINGS_PACKAGE = "com.android.settings"; |
| 50 | private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation"; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | public void onReceive(Context context, Intent broadcast) { |
| 54 | final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 55 | UserInfo userInfo = um.getUserInfo(UserHandle.myUserId()); |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 56 | final PackageManager pm = context.getPackageManager(); |
| 57 | managedProfileSetup(context, pm, broadcast, userInfo); |
| 58 | webviewSettingSetup(context, pm, userInfo); |
| 59 | } |
| 60 | |
| 61 | private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast, |
| 62 | UserInfo userInfo) { |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 63 | if (userInfo == null || !userInfo.isManagedProfile()) { |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 64 | return; |
| 65 | } |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 66 | Log.i(TAG, "Received broadcast: " + broadcast.getAction() |
| 67 | + ". Setting up intent forwarding for managed profile."); |
Alexandra Gherghina | 5667a68 | 2014-07-29 14:55:56 +0100 | [diff] [blame] | 68 | // Clear any previous intent forwarding we set up |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 69 | pm.clearCrossProfileIntentFilters(userInfo.id); |
Alexandra Gherghina | 5667a68 | 2014-07-29 14:55:56 +0100 | [diff] [blame] | 70 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 71 | // Set up intent forwarding for implicit intents |
| 72 | Intent intent = new Intent(); |
| 73 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 74 | intent.setPackage(context.getPackageName()); |
| 75 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 76 | // Resolves activities for the managed profile (which we're running as) |
| 77 | List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent, |
Tony Mak | 82d9096 | 2016-05-04 13:51:41 +0100 | [diff] [blame] | 78 | GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS); |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 79 | final int count = resolvedIntents.size(); |
| 80 | for (int i = 0; i < count; i++) { |
| 81 | ResolveInfo info = resolvedIntents.get(i); |
| 82 | if (info.filter != null && info.activityInfo != null |
| 83 | && info.activityInfo.metaData != null) { |
| 84 | boolean shouldForward = info.activityInfo.metaData.getBoolean( |
| 85 | PRIMARY_PROFILE_SETTING); |
| 86 | if (shouldForward) { |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 87 | pm.addCrossProfileIntentFilter(info.filter, userInfo.id, |
| 88 | userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE); |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Disable launcher icon |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 94 | ComponentName settingsComponentName = new ComponentName(context, Settings.class); |
| 95 | pm.setComponentEnabledSetting(settingsComponentName, |
| 96 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
Kenny Guy | 4761da3 | 2017-05-16 18:54:21 +0100 | [diff] [blame] | 97 | // Disable shortcut picker. |
| 98 | ComponentName shortcutComponentName = new ComponentName(context, CreateShortcut.class); |
| 99 | pm.setComponentEnabledSetting(shortcutComponentName, |
| 100 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
Amith Yamasani | 2d578ce | 2014-07-25 09:37:33 -0700 | [diff] [blame] | 101 | } |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 102 | |
| 103 | // Disable WebView Setting if the current user is not an admin |
| 104 | private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) { |
| 105 | if (userInfo == null) { |
| 106 | return; |
| 107 | } |
| 108 | ComponentName settingsComponentName = |
Gustav Sennton | 4d3334c | 2016-12-13 19:16:48 +0000 | [diff] [blame] | 109 | new ComponentName(SETTINGS_PACKAGE, SETTINGS_PACKAGE + WEBVIEW_IMPLEMENTATION_ACTIVITY); |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 110 | pm.setComponentEnabledSetting(settingsComponentName, |
| 111 | userInfo.isAdmin() ? |
| 112 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED : |
| 113 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 114 | PackageManager.DONT_KILL_APP); |
| 115 | } |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 116 | } |