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 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.GET_ACTIVITIES; |
| 20 | import static android.content.pm.PackageManager.GET_META_DATA; |
| 21 | import static android.content.pm.PackageManager.GET_RESOLVED_FILTER; |
| 22 | import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; |
| 23 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
Doris Ling | f2d7680 | 2018-04-02 14:51:36 -0700 | [diff] [blame] | 30 | import android.content.pm.ShortcutInfo; |
| 31 | import android.content.pm.ShortcutManager; |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 32 | import android.content.pm.UserInfo; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 33 | import android.os.UserHandle; |
| 34 | import android.os.UserManager; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 35 | import android.util.Log; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 36 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 37 | import androidx.annotation.VisibleForTesting; |
| 38 | |
Fan Zhang | f1f0f8b | 2018-06-22 10:40:07 -0700 | [diff] [blame] | 39 | import com.android.settings.Settings.CreateShortcutActivity; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 40 | |
Doris Ling | f2d7680 | 2018-04-02 14:51:36 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 44 | /** |
Kenny Guy | 4761da3 | 2017-05-16 18:54:21 +0100 | [diff] [blame] | 45 | * 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] | 46 | * performs setup steps for a managed profile (disables the launcher icon of the Settings app, |
Doris Ling | f2d7680 | 2018-04-02 14:51:36 -0700 | [diff] [blame] | 47 | * adds cross-profile intent filters for the appropriate Settings activities), disables the |
| 48 | * webview setting for non-admin users, and updates the intent flags for any existing shortcuts. |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 49 | */ |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 50 | public class SettingsInitialize extends BroadcastReceiver { |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 51 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 52 | private static final String PRIMARY_PROFILE_SETTING = |
| 53 | "com.android.settings.PRIMARY_PROFILE_CONTROLLED"; |
Gustav Sennton | 4d3334c | 2016-12-13 19:16:48 +0000 | [diff] [blame] | 54 | private static final String SETTINGS_PACKAGE = "com.android.settings"; |
| 55 | private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation"; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 56 | |
| 57 | @Override |
| 58 | public void onReceive(Context context, Intent broadcast) { |
| 59 | final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 60 | UserInfo userInfo = um.getUserInfo(UserHandle.myUserId()); |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 61 | final PackageManager pm = context.getPackageManager(); |
| 62 | managedProfileSetup(context, pm, broadcast, userInfo); |
| 63 | webviewSettingSetup(context, pm, userInfo); |
Doris Ling | f2d7680 | 2018-04-02 14:51:36 -0700 | [diff] [blame] | 64 | refreshExistingShortcuts(context); |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast, |
| 68 | UserInfo userInfo) { |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 69 | if (userInfo == null || !userInfo.isManagedProfile()) { |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 70 | return; |
| 71 | } |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 72 | Log.i(TAG, "Received broadcast: " + broadcast.getAction() |
| 73 | + ". Setting up intent forwarding for managed profile."); |
Alexandra Gherghina | 5667a68 | 2014-07-29 14:55:56 +0100 | [diff] [blame] | 74 | // Clear any previous intent forwarding we set up |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 75 | pm.clearCrossProfileIntentFilters(userInfo.id); |
Alexandra Gherghina | 5667a68 | 2014-07-29 14:55:56 +0100 | [diff] [blame] | 76 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 77 | // Set up intent forwarding for implicit intents |
| 78 | Intent intent = new Intent(); |
| 79 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 80 | intent.setPackage(context.getPackageName()); |
| 81 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 82 | // Resolves activities for the managed profile (which we're running as) |
| 83 | List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent, |
Tony Mak | 82d9096 | 2016-05-04 13:51:41 +0100 | [diff] [blame] | 84 | GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS); |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 85 | final int count = resolvedIntents.size(); |
| 86 | for (int i = 0; i < count; i++) { |
| 87 | ResolveInfo info = resolvedIntents.get(i); |
| 88 | if (info.filter != null && info.activityInfo != null |
| 89 | && info.activityInfo.metaData != null) { |
| 90 | boolean shouldForward = info.activityInfo.metaData.getBoolean( |
| 91 | PRIMARY_PROFILE_SETTING); |
| 92 | if (shouldForward) { |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 93 | pm.addCrossProfileIntentFilter(info.filter, userInfo.id, |
| 94 | userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE); |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Disable launcher icon |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 100 | ComponentName settingsComponentName = new ComponentName(context, Settings.class); |
| 101 | pm.setComponentEnabledSetting(settingsComponentName, |
| 102 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
Kenny Guy | 4761da3 | 2017-05-16 18:54:21 +0100 | [diff] [blame] | 103 | // Disable shortcut picker. |
Fan Zhang | f1f0f8b | 2018-06-22 10:40:07 -0700 | [diff] [blame] | 104 | ComponentName shortcutComponentName = new ComponentName( |
| 105 | context, CreateShortcutActivity.class); |
Kenny Guy | 4761da3 | 2017-05-16 18:54:21 +0100 | [diff] [blame] | 106 | pm.setComponentEnabledSetting(shortcutComponentName, |
| 107 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
Amith Yamasani | 2d578ce | 2014-07-25 09:37:33 -0700 | [diff] [blame] | 108 | } |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 109 | |
| 110 | // Disable WebView Setting if the current user is not an admin |
| 111 | private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) { |
| 112 | if (userInfo == null) { |
| 113 | return; |
| 114 | } |
| 115 | ComponentName settingsComponentName = |
Gustav Sennton | 4d3334c | 2016-12-13 19:16:48 +0000 | [diff] [blame] | 116 | new ComponentName(SETTINGS_PACKAGE, SETTINGS_PACKAGE + WEBVIEW_IMPLEMENTATION_ACTIVITY); |
Gustav Sennton | 66313bb | 2016-05-11 12:31:40 +0100 | [diff] [blame] | 117 | pm.setComponentEnabledSetting(settingsComponentName, |
| 118 | userInfo.isAdmin() ? |
| 119 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED : |
| 120 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 121 | PackageManager.DONT_KILL_APP); |
| 122 | } |
Doris Ling | f2d7680 | 2018-04-02 14:51:36 -0700 | [diff] [blame] | 123 | |
| 124 | // Refresh settings shortcuts to have correct intent flags |
| 125 | @VisibleForTesting |
| 126 | void refreshExistingShortcuts(Context context) { |
| 127 | final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); |
| 128 | final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts(); |
| 129 | final List<ShortcutInfo> updates = new ArrayList<>(); |
| 130 | for (ShortcutInfo info : pinnedShortcuts) { |
Doris Ling | 79848ab | 2018-11-15 16:29:53 -0800 | [diff] [blame^] | 131 | if (info.isImmutable()) { |
| 132 | continue; |
| 133 | } |
Doris Ling | f2d7680 | 2018-04-02 14:51:36 -0700 | [diff] [blame] | 134 | final Intent shortcutIntent = info.getIntent(); |
| 135 | shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 136 | final ShortcutInfo updatedInfo = new ShortcutInfo.Builder(context, info.getId()) |
| 137 | .setIntent(shortcutIntent) |
| 138 | .build(); |
| 139 | updates.add(updatedInfo); |
| 140 | } |
| 141 | shortcutManager.updateShortcuts(updates); |
| 142 | } |
| 143 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 144 | } |