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