blob: 93053381811a1b8bc7e4ecc6f09447be30fca641 [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;
40
Fan Zhangf1f0f8b2018-06-22 10:40:07 -070041import com.android.settings.Settings.CreateShortcutActivity;
Fan Zhangc7162cd2018-06-18 15:21:41 -070042
Doris Lingf2d76802018-04-02 14:51:36 -070043import java.util.ArrayList;
Alexandra Gherghina25138512014-07-21 22:42:21 +010044import java.util.List;
45
Alexandra Gherghina25138512014-07-21 22:42:21 +010046/**
Kenny Guy4761da32017-05-16 18:54:21 +010047 * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
Gustav Sennton66313bb2016-05-11 12:31:40 +010048 * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
Doris Lingf2d76802018-04-02 14:51:36 -070049 * 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 Gherghina25138512014-07-21 22:42:21 +010051 */
Gustav Sennton66313bb2016-05-11 12:31:40 +010052public class SettingsInitialize extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010053 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010054 private static final String PRIMARY_PROFILE_SETTING =
55 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
Gustav Sennton4d3334c2016-12-13 19:16:48 +000056 private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
Alexandra Gherghina25138512014-07-21 22:42:21 +010057
58 @Override
59 public void onReceive(Context context, Intent broadcast) {
60 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Xiaohui Chen762104e2015-09-01 10:26:53 -070061 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
Fan Zhangc3fd2892019-01-29 16:00:19 -080062 final PackageManager pm = context.getPackageManager();
Gustav Sennton66313bb2016-05-11 12:31:40 +010063 managedProfileSetup(context, pm, broadcast, userInfo);
64 webviewSettingSetup(context, pm, userInfo);
Doris Lingf2d76802018-04-02 14:51:36 -070065 refreshExistingShortcuts(context);
yubin.ying2df949f2022-04-14 13:24:08 +080066 ShutdownJobService.startShutdownJob(context);
Gustav Sennton66313bb2016-05-11 12:31:40 +010067 }
68
69 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
70 UserInfo userInfo) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070071 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010072 return;
73 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010074 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
75 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010076 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070077 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010078
Alexandra Gherghina25138512014-07-21 22:42:21 +010079 // Set up intent forwarding for implicit intents
80 Intent intent = new Intent();
81 intent.addCategory(Intent.CATEGORY_DEFAULT);
82 intent.setPackage(context.getPackageName());
83
Alexandra Gherghina25138512014-07-21 22:42:21 +010084 // Resolves activities for the managed profile (which we're running as)
85 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010086 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010087 final int count = resolvedIntents.size();
88 for (int i = 0; i < count; i++) {
89 ResolveInfo info = resolvedIntents.get(i);
90 if (info.filter != null && info.activityInfo != null
91 && info.activityInfo.metaData != null) {
92 boolean shouldForward = info.activityInfo.metaData.getBoolean(
93 PRIMARY_PROFILE_SETTING);
94 if (shouldForward) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070095 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
Fan Zhangc3fd2892019-01-29 16:00:19 -080096 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +010097 }
98 }
99 }
100
101 // Disable launcher icon
Alexandra Gherghina25138512014-07-21 22:42:21 +0100102 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
103 pm.setComponentEnabledSetting(settingsComponentName,
104 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Kenny Guy4761da32017-05-16 18:54:21 +0100105 // Disable shortcut picker.
Fan Zhangf1f0f8b2018-06-22 10:40:07 -0700106 ComponentName shortcutComponentName = new ComponentName(
107 context, CreateShortcutActivity.class);
Kenny Guy4761da32017-05-16 18:54:21 +0100108 pm.setComponentEnabledSetting(shortcutComponentName,
109 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700110 }
Gustav Sennton66313bb2016-05-11 12:31:40 +0100111
112 // Disable WebView Setting if the current user is not an admin
113 private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) {
114 if (userInfo == null) {
115 return;
116 }
117 ComponentName settingsComponentName =
Fan Zhangc3fd2892019-01-29 16:00:19 -0800118 new ComponentName(SETTINGS_PACKAGE_NAME,
119 SETTINGS_PACKAGE_NAME + WEBVIEW_IMPLEMENTATION_ACTIVITY);
Gustav Sennton66313bb2016-05-11 12:31:40 +0100120 pm.setComponentEnabledSetting(settingsComponentName,
121 userInfo.isAdmin() ?
122 PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
123 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
124 PackageManager.DONT_KILL_APP);
125 }
Doris Lingf2d76802018-04-02 14:51:36 -0700126
127 // Refresh settings shortcuts to have correct intent flags
128 @VisibleForTesting
129 void refreshExistingShortcuts(Context context) {
130 final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
131 final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
132 final List<ShortcutInfo> updates = new ArrayList<>();
133 for (ShortcutInfo info : pinnedShortcuts) {
Doris Ling79848ab2018-11-15 16:29:53 -0800134 if (info.isImmutable()) {
135 continue;
136 }
Doris Lingf2d76802018-04-02 14:51:36 -0700137 final Intent shortcutIntent = info.getIntent();
138 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
139 final ShortcutInfo updatedInfo = new ShortcutInfo.Builder(context, info.getId())
140 .setIntent(shortcutIntent)
141 .build();
142 updates.add(updatedInfo);
143 }
144 shortcutManager.updateShortcuts(updates);
145 }
146
Alexandra Gherghina25138512014-07-21 22:42:21 +0100147}