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