blob: 376d9160c6bf447950cf7dac0012ba78fcd2a324 [file] [log] [blame]
Alexandra Gherghina25138512014-07-21 22:42:21 +01001/*
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
17package com.android.settings;
18
Fan Zhangc7162cd2018-06-18 15:21:41 -070019import static android.content.pm.PackageManager.GET_ACTIVITIES;
20import static android.content.pm.PackageManager.GET_META_DATA;
21import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
22import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
23
Fan Zhangc3fd2892019-01-29 16:00:19 -080024import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
25
Alexandra Gherghina25138512014-07-21 22:42:21 +010026import android.content.BroadcastReceiver;
27import android.content.ComponentName;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
Doris Lingf2d76802018-04-02 14:51:36 -070032import android.content.pm.ShortcutInfo;
33import android.content.pm.ShortcutManager;
Xiaohui Chen762104e2015-09-01 10:26:53 -070034import android.content.pm.UserInfo;
Alexandra Gherghina25138512014-07-21 22:42:21 +010035import android.os.UserHandle;
36import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.util.Log;
Alexandra Gherghina25138512014-07-21 22:42:21 +010038
Fan Zhang23f8d592018-08-28 15:11:40 -070039import androidx.annotation.VisibleForTesting;
Arc Wang54f619d2021-09-28 22:26:48 +080040import androidx.window.embedding.SplitController;
Fan Zhang23f8d592018-08-28 15:11:40 -070041
Fan Zhangf1f0f8b2018-06-22 10:40:07 -070042import com.android.settings.Settings.CreateShortcutActivity;
Arc Wang54f619d2021-09-28 22:26:48 +080043import com.android.settings.homepage.SettingsHomepageActivity;
Jason Chiu3af73362021-11-11 14:24:37 +080044import com.android.settings.search.SearchStateReceiver;
Tsung-Mao Fangebcabd92021-04-06 18:27:06 +080045import com.android.settingslib.utils.ThreadUtils;
Fan Zhangc7162cd2018-06-18 15:21:41 -070046
Doris Lingf2d76802018-04-02 14:51:36 -070047import java.util.ArrayList;
Alexandra Gherghina25138512014-07-21 22:42:21 +010048import java.util.List;
49
Alexandra Gherghina25138512014-07-21 22:42:21 +010050/**
Kenny Guy4761da32017-05-16 18:54:21 +010051 * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
Gustav Sennton66313bb2016-05-11 12:31:40 +010052 * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
Doris Lingf2d76802018-04-02 14:51:36 -070053 * adds cross-profile intent filters for the appropriate Settings activities), disables the
Arc Wang54f619d2021-09-28 22:26:48 +080054 * webview setting for non-admin users, updates the intent flags for any existing shortcuts and
55 * enables DeepLinkHomepageActivity for large screen devices.
Alexandra Gherghina25138512014-07-21 22:42:21 +010056 */
Gustav Sennton66313bb2016-05-11 12:31:40 +010057public class SettingsInitialize extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010058 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010059 private static final String PRIMARY_PROFILE_SETTING =
60 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
Gustav Sennton4d3334c2016-12-13 19:16:48 +000061 private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
Alexandra Gherghina25138512014-07-21 22:42:21 +010062
63 @Override
64 public void onReceive(Context context, Intent broadcast) {
65 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Xiaohui Chen762104e2015-09-01 10:26:53 -070066 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
Fan Zhangc3fd2892019-01-29 16:00:19 -080067 final PackageManager pm = context.getPackageManager();
Gustav Sennton66313bb2016-05-11 12:31:40 +010068 managedProfileSetup(context, pm, broadcast, userInfo);
69 webviewSettingSetup(context, pm, userInfo);
Tsung-Mao Fangebcabd92021-04-06 18:27:06 +080070 ThreadUtils.postOnBackgroundThread(() -> refreshExistingShortcuts(context));
Jason Chiu3af73362021-11-11 14:24:37 +080071 enableTwoPaneDeepLinkActivityIfNecessary(pm, context);
Gustav Sennton66313bb2016-05-11 12:31:40 +010072 }
73
74 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
75 UserInfo userInfo) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070076 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010077 return;
78 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010079 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
80 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010081 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070082 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010083
Alexandra Gherghina25138512014-07-21 22:42:21 +010084 // 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 Gherghina25138512014-07-21 22:42:21 +010089 // Resolves activities for the managed profile (which we're running as)
90 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010091 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010092 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 Chen762104e2015-09-01 10:26:53 -0700100 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
Fan Zhangc3fd2892019-01-29 16:00:19 -0800101 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +0100102 }
103 }
104 }
105
106 // Disable launcher icon
Alexandra Gherghina25138512014-07-21 22:42:21 +0100107 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
108 pm.setComponentEnabledSetting(settingsComponentName,
109 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Kenny Guy4761da32017-05-16 18:54:21 +0100110 // Disable shortcut picker.
Fan Zhangf1f0f8b2018-06-22 10:40:07 -0700111 ComponentName shortcutComponentName = new ComponentName(
112 context, CreateShortcutActivity.class);
Kenny Guy4761da32017-05-16 18:54:21 +0100113 pm.setComponentEnabledSetting(shortcutComponentName,
114 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700115 }
Gustav Sennton66313bb2016-05-11 12:31:40 +0100116
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 Zhangc3fd2892019-01-29 16:00:19 -0800123 new ComponentName(SETTINGS_PACKAGE_NAME,
124 SETTINGS_PACKAGE_NAME + WEBVIEW_IMPLEMENTATION_ACTIVITY);
Gustav Sennton66313bb2016-05-11 12:31:40 +0100125 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 Lingf2d76802018-04-02 14:51:36 -0700131
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 Ling79848ab2018-11-15 16:29:53 -0800139 if (info.isImmutable()) {
140 continue;
141 }
Doris Lingf2d76802018-04-02 14:51:36 -0700142 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 Wang54f619d2021-09-28 22:26:48 +0800151
Jason Chiu3af73362021-11-11 14:24:37 +0800152 private void enableTwoPaneDeepLinkActivityIfNecessary(PackageManager pm, Context context) {
Arc Wang54f619d2021-09-28 22:26:48 +0800153 final ComponentName deepLinkHome = new ComponentName(Utils.SETTINGS_PACKAGE_NAME,
154 SettingsHomepageActivity.ALIAS_DEEP_LINK);
Jason Chiu3af73362021-11-11 14:24:37 +0800155 final ComponentName searchStateReceiver = new ComponentName(context,
156 SearchStateReceiver.class);
Arc Wang54f619d2021-09-28 22:26:48 +0800157 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 Chiu3af73362021-11-11 14:24:37 +0800161 pm.setComponentEnabledSetting(searchStateReceiver, enableState,
162 PackageManager.DONT_KILL_APP);
Arc Wang54f619d2021-09-28 22:26:48 +0800163 }
Alexandra Gherghina25138512014-07-21 22:42:21 +0100164}