blob: 0bc892ca5a5d1c4fce012fb8b9e6644621d4a1a4 [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
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Doris Lingf2d76802018-04-02 14:51:36 -070025import android.content.pm.ShortcutInfo;
26import android.content.pm.ShortcutManager;
Xiaohui Chen762104e2015-09-01 10:26:53 -070027import android.content.pm.UserInfo;
Alexandra Gherghina25138512014-07-21 22:42:21 +010028import android.os.UserHandle;
29import android.os.UserManager;
Doris Lingf2d76802018-04-02 14:51:36 -070030import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.util.Log;
Alexandra Gherghina25138512014-07-21 22:42:21 +010032
Doris Lingf2d76802018-04-02 14:51:36 -070033import java.util.ArrayList;
Alexandra Gherghina25138512014-07-21 22:42:21 +010034import java.util.List;
35
36import static android.content.pm.PackageManager.GET_ACTIVITIES;
37import static android.content.pm.PackageManager.GET_META_DATA;
38import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
Tony Mak82d90962016-05-04 13:51:41 +010039import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
Alexandra Gherghina25138512014-07-21 22:42:21 +010040
Fan Zhang409eab12018-01-29 11:23:52 -080041import com.android.settings.shortcut.CreateShortcut;
42
Alexandra Gherghina25138512014-07-21 22:42:21 +010043/**
Kenny Guy4761da32017-05-16 18:54:21 +010044 * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
Gustav Sennton66313bb2016-05-11 12:31:40 +010045 * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
Doris Lingf2d76802018-04-02 14:51:36 -070046 * adds cross-profile intent filters for the appropriate Settings activities), disables the
47 * webview setting for non-admin users, and updates the intent flags for any existing shortcuts.
Alexandra Gherghina25138512014-07-21 22:42:21 +010048 */
Gustav Sennton66313bb2016-05-11 12:31:40 +010049public class SettingsInitialize extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010050 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010051 private static final String PRIMARY_PROFILE_SETTING =
52 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
Gustav Sennton4d3334c2016-12-13 19:16:48 +000053 private static final String SETTINGS_PACKAGE = "com.android.settings";
54 private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
Alexandra Gherghina25138512014-07-21 22:42:21 +010055
56 @Override
57 public void onReceive(Context context, Intent broadcast) {
58 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Xiaohui Chen762104e2015-09-01 10:26:53 -070059 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
Gustav Sennton66313bb2016-05-11 12:31:40 +010060 final PackageManager pm = context.getPackageManager();
61 managedProfileSetup(context, pm, broadcast, userInfo);
62 webviewSettingSetup(context, pm, userInfo);
Doris Lingf2d76802018-04-02 14:51:36 -070063 refreshExistingShortcuts(context);
Gustav Sennton66313bb2016-05-11 12:31:40 +010064 }
65
66 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
67 UserInfo userInfo) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070068 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010069 return;
70 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010071 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
72 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010073 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070074 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010075
Alexandra Gherghina25138512014-07-21 22:42:21 +010076 // Set up intent forwarding for implicit intents
77 Intent intent = new Intent();
78 intent.addCategory(Intent.CATEGORY_DEFAULT);
79 intent.setPackage(context.getPackageName());
80
Alexandra Gherghina25138512014-07-21 22:42:21 +010081 // Resolves activities for the managed profile (which we're running as)
82 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010083 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010084 final int count = resolvedIntents.size();
85 for (int i = 0; i < count; i++) {
86 ResolveInfo info = resolvedIntents.get(i);
87 if (info.filter != null && info.activityInfo != null
88 && info.activityInfo.metaData != null) {
89 boolean shouldForward = info.activityInfo.metaData.getBoolean(
90 PRIMARY_PROFILE_SETTING);
91 if (shouldForward) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070092 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
93 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +010094 }
95 }
96 }
97
98 // Disable launcher icon
Alexandra Gherghina25138512014-07-21 22:42:21 +010099 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
100 pm.setComponentEnabledSetting(settingsComponentName,
101 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Kenny Guy4761da32017-05-16 18:54:21 +0100102 // Disable shortcut picker.
103 ComponentName shortcutComponentName = new ComponentName(context, CreateShortcut.class);
104 pm.setComponentEnabledSetting(shortcutComponentName,
105 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700106 }
Gustav Sennton66313bb2016-05-11 12:31:40 +0100107
108 // Disable WebView Setting if the current user is not an admin
109 private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) {
110 if (userInfo == null) {
111 return;
112 }
113 ComponentName settingsComponentName =
Gustav Sennton4d3334c2016-12-13 19:16:48 +0000114 new ComponentName(SETTINGS_PACKAGE, SETTINGS_PACKAGE + WEBVIEW_IMPLEMENTATION_ACTIVITY);
Gustav Sennton66313bb2016-05-11 12:31:40 +0100115 pm.setComponentEnabledSetting(settingsComponentName,
116 userInfo.isAdmin() ?
117 PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
118 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
119 PackageManager.DONT_KILL_APP);
120 }
Doris Lingf2d76802018-04-02 14:51:36 -0700121
122 // Refresh settings shortcuts to have correct intent flags
123 @VisibleForTesting
124 void refreshExistingShortcuts(Context context) {
125 final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
126 final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
127 final List<ShortcutInfo> updates = new ArrayList<>();
128 for (ShortcutInfo info : pinnedShortcuts) {
129 final Intent shortcutIntent = info.getIntent();
130 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
131 final ShortcutInfo updatedInfo = new ShortcutInfo.Builder(context, info.getId())
132 .setIntent(shortcutIntent)
133 .build();
134 updates.add(updatedInfo);
135 }
136 shortcutManager.updateShortcuts(updates);
137 }
138
Alexandra Gherghina25138512014-07-21 22:42:21 +0100139}