blob: 500ce779ef55bfd894513b2c7f5dfb4c6f5e53aa [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;
Tsung-Mao Fangebcabd92021-04-06 18:27:06 +080044import com.android.settingslib.utils.ThreadUtils;
Fan Zhangc7162cd2018-06-18 15:21:41 -070045
Doris Lingf2d76802018-04-02 14:51:36 -070046import java.util.ArrayList;
Alexandra Gherghina25138512014-07-21 22:42:21 +010047import java.util.List;
48
Alexandra Gherghina25138512014-07-21 22:42:21 +010049/**
Kenny Guy4761da32017-05-16 18:54:21 +010050 * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
Gustav Sennton66313bb2016-05-11 12:31:40 +010051 * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
Doris Lingf2d76802018-04-02 14:51:36 -070052 * adds cross-profile intent filters for the appropriate Settings activities), disables the
Arc Wang54f619d2021-09-28 22:26:48 +080053 * webview setting for non-admin users, updates the intent flags for any existing shortcuts and
54 * enables DeepLinkHomepageActivity for large screen devices.
Alexandra Gherghina25138512014-07-21 22:42:21 +010055 */
Gustav Sennton66313bb2016-05-11 12:31:40 +010056public class SettingsInitialize extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010057 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010058 private static final String PRIMARY_PROFILE_SETTING =
59 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
Gustav Sennton4d3334c2016-12-13 19:16:48 +000060 private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
Alexandra Gherghina25138512014-07-21 22:42:21 +010061
62 @Override
63 public void onReceive(Context context, Intent broadcast) {
64 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Xiaohui Chen762104e2015-09-01 10:26:53 -070065 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
Fan Zhangc3fd2892019-01-29 16:00:19 -080066 final PackageManager pm = context.getPackageManager();
Gustav Sennton66313bb2016-05-11 12:31:40 +010067 managedProfileSetup(context, pm, broadcast, userInfo);
68 webviewSettingSetup(context, pm, userInfo);
Tsung-Mao Fangebcabd92021-04-06 18:27:06 +080069 ThreadUtils.postOnBackgroundThread(() -> refreshExistingShortcuts(context));
Arc Wang54f619d2021-09-28 22:26:48 +080070 enableTwoPaneDeepLinkActivityIfNecessary(pm, broadcast);
Gustav Sennton66313bb2016-05-11 12:31:40 +010071 }
72
73 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
74 UserInfo userInfo) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070075 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010076 return;
77 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010078 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
79 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010080 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070081 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010082
Alexandra Gherghina25138512014-07-21 22:42:21 +010083 // Set up intent forwarding for implicit intents
84 Intent intent = new Intent();
85 intent.addCategory(Intent.CATEGORY_DEFAULT);
86 intent.setPackage(context.getPackageName());
87
Alexandra Gherghina25138512014-07-21 22:42:21 +010088 // Resolves activities for the managed profile (which we're running as)
89 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010090 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010091 final int count = resolvedIntents.size();
92 for (int i = 0; i < count; i++) {
93 ResolveInfo info = resolvedIntents.get(i);
94 if (info.filter != null && info.activityInfo != null
95 && info.activityInfo.metaData != null) {
96 boolean shouldForward = info.activityInfo.metaData.getBoolean(
97 PRIMARY_PROFILE_SETTING);
98 if (shouldForward) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070099 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
Fan Zhangc3fd2892019-01-29 16:00:19 -0800100 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +0100101 }
102 }
103 }
104
105 // Disable launcher icon
Alexandra Gherghina25138512014-07-21 22:42:21 +0100106 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
107 pm.setComponentEnabledSetting(settingsComponentName,
108 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Kenny Guy4761da32017-05-16 18:54:21 +0100109 // Disable shortcut picker.
Fan Zhangf1f0f8b2018-06-22 10:40:07 -0700110 ComponentName shortcutComponentName = new ComponentName(
111 context, CreateShortcutActivity.class);
Kenny Guy4761da32017-05-16 18:54:21 +0100112 pm.setComponentEnabledSetting(shortcutComponentName,
113 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700114 }
Gustav Sennton66313bb2016-05-11 12:31:40 +0100115
116 // Disable WebView Setting if the current user is not an admin
117 private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) {
118 if (userInfo == null) {
119 return;
120 }
121 ComponentName settingsComponentName =
Fan Zhangc3fd2892019-01-29 16:00:19 -0800122 new ComponentName(SETTINGS_PACKAGE_NAME,
123 SETTINGS_PACKAGE_NAME + WEBVIEW_IMPLEMENTATION_ACTIVITY);
Gustav Sennton66313bb2016-05-11 12:31:40 +0100124 pm.setComponentEnabledSetting(settingsComponentName,
125 userInfo.isAdmin() ?
126 PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
127 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
128 PackageManager.DONT_KILL_APP);
129 }
Doris Lingf2d76802018-04-02 14:51:36 -0700130
131 // Refresh settings shortcuts to have correct intent flags
132 @VisibleForTesting
133 void refreshExistingShortcuts(Context context) {
134 final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
135 final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
136 final List<ShortcutInfo> updates = new ArrayList<>();
137 for (ShortcutInfo info : pinnedShortcuts) {
Doris Ling79848ab2018-11-15 16:29:53 -0800138 if (info.isImmutable()) {
139 continue;
140 }
Doris Lingf2d76802018-04-02 14:51:36 -0700141 final Intent shortcutIntent = info.getIntent();
142 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
143 final ShortcutInfo updatedInfo = new ShortcutInfo.Builder(context, info.getId())
144 .setIntent(shortcutIntent)
145 .build();
146 updates.add(updatedInfo);
147 }
148 shortcutManager.updateShortcuts(updates);
149 }
Arc Wang54f619d2021-09-28 22:26:48 +0800150
151 private void enableTwoPaneDeepLinkActivityIfNecessary(PackageManager pm, Intent intent) {
Arc Wang54f619d2021-09-28 22:26:48 +0800152 final ComponentName deepLinkHome = new ComponentName(Utils.SETTINGS_PACKAGE_NAME,
153 SettingsHomepageActivity.ALIAS_DEEP_LINK);
154 final int enableState = SplitController.getInstance().isSplitSupported()
155 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
156 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
157 pm.setComponentEnabledSetting(deepLinkHome, enableState, PackageManager.DONT_KILL_APP);
158 }
Alexandra Gherghina25138512014-07-21 22:42:21 +0100159}