blob: 515703358e54d1edcb41de45ae8434a41317278f [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);
Gustav Sennton66313bb2016-05-11 12:31:40 +010066 }
67
68 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
69 UserInfo userInfo) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070070 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010071 return;
72 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010073 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
74 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010075 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070076 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010077
Alexandra Gherghina25138512014-07-21 22:42:21 +010078 // 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 Gherghina25138512014-07-21 22:42:21 +010083 // Resolves activities for the managed profile (which we're running as)
84 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010085 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010086 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 Chen762104e2015-09-01 10:26:53 -070094 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
Fan Zhangc3fd2892019-01-29 16:00:19 -080095 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +010096 }
97 }
98 }
99
100 // Disable launcher icon
Alexandra Gherghina25138512014-07-21 22:42:21 +0100101 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
102 pm.setComponentEnabledSetting(settingsComponentName,
103 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Kenny Guy4761da32017-05-16 18:54:21 +0100104 // Disable shortcut picker.
Fan Zhangf1f0f8b2018-06-22 10:40:07 -0700105 ComponentName shortcutComponentName = new ComponentName(
106 context, CreateShortcutActivity.class);
Kenny Guy4761da32017-05-16 18:54:21 +0100107 pm.setComponentEnabledSetting(shortcutComponentName,
108 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700109 }
Gustav Sennton66313bb2016-05-11 12:31:40 +0100110
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 Zhangc3fd2892019-01-29 16:00:19 -0800117 new ComponentName(SETTINGS_PACKAGE_NAME,
118 SETTINGS_PACKAGE_NAME + WEBVIEW_IMPLEMENTATION_ACTIVITY);
Gustav Sennton66313bb2016-05-11 12:31:40 +0100119 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 Lingf2d76802018-04-02 14:51:36 -0700125
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 Ling79848ab2018-11-15 16:29:53 -0800133 if (info.isImmutable()) {
134 continue;
135 }
Doris Lingf2d76802018-04-02 14:51:36 -0700136 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 Gherghina25138512014-07-21 22:42:21 +0100146}