blob: a66207e8127f8b9efcc864742353c7af4947510c [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
Alexandra Gherghina25138512014-07-21 22:42:21 +010024import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Doris Lingf2d76802018-04-02 14:51:36 -070030import android.content.pm.ShortcutInfo;
31import android.content.pm.ShortcutManager;
Xiaohui Chen762104e2015-09-01 10:26:53 -070032import android.content.pm.UserInfo;
Alexandra Gherghina25138512014-07-21 22:42:21 +010033import android.os.UserHandle;
34import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.util.Log;
Alexandra Gherghina25138512014-07-21 22:42:21 +010036
Fan Zhangf1f0f8b2018-06-22 10:40:07 -070037import com.android.settings.Settings.CreateShortcutActivity;
Fan Zhangc7162cd2018-06-18 15:21:41 -070038
Doris Lingf2d76802018-04-02 14:51:36 -070039import java.util.ArrayList;
Alexandra Gherghina25138512014-07-21 22:42:21 +010040import java.util.List;
41
Fan Zhangc7162cd2018-06-18 15:21:41 -070042import androidx.annotation.VisibleForTesting;
Fan Zhang409eab12018-01-29 11:23:52 -080043
Alexandra Gherghina25138512014-07-21 22:42:21 +010044/**
Kenny Guy4761da32017-05-16 18:54:21 +010045 * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
Gustav Sennton66313bb2016-05-11 12:31:40 +010046 * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
Doris Lingf2d76802018-04-02 14:51:36 -070047 * adds cross-profile intent filters for the appropriate Settings activities), disables the
48 * webview setting for non-admin users, and updates the intent flags for any existing shortcuts.
Alexandra Gherghina25138512014-07-21 22:42:21 +010049 */
Gustav Sennton66313bb2016-05-11 12:31:40 +010050public class SettingsInitialize extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010051 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010052 private static final String PRIMARY_PROFILE_SETTING =
53 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
Gustav Sennton4d3334c2016-12-13 19:16:48 +000054 private static final String SETTINGS_PACKAGE = "com.android.settings";
55 private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
Alexandra Gherghina25138512014-07-21 22:42:21 +010056
57 @Override
58 public void onReceive(Context context, Intent broadcast) {
59 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Xiaohui Chen762104e2015-09-01 10:26:53 -070060 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
Gustav Sennton66313bb2016-05-11 12:31:40 +010061 final PackageManager pm = context.getPackageManager();
62 managedProfileSetup(context, pm, broadcast, userInfo);
63 webviewSettingSetup(context, pm, userInfo);
Doris Lingf2d76802018-04-02 14:51:36 -070064 refreshExistingShortcuts(context);
Gustav Sennton66313bb2016-05-11 12:31:40 +010065 }
66
67 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
68 UserInfo userInfo) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070069 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010070 return;
71 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010072 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
73 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010074 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070075 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010076
Alexandra Gherghina25138512014-07-21 22:42:21 +010077 // Set up intent forwarding for implicit intents
78 Intent intent = new Intent();
79 intent.addCategory(Intent.CATEGORY_DEFAULT);
80 intent.setPackage(context.getPackageName());
81
Alexandra Gherghina25138512014-07-21 22:42:21 +010082 // Resolves activities for the managed profile (which we're running as)
83 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010084 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010085 final int count = resolvedIntents.size();
86 for (int i = 0; i < count; i++) {
87 ResolveInfo info = resolvedIntents.get(i);
88 if (info.filter != null && info.activityInfo != null
89 && info.activityInfo.metaData != null) {
90 boolean shouldForward = info.activityInfo.metaData.getBoolean(
91 PRIMARY_PROFILE_SETTING);
92 if (shouldForward) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070093 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
94 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +010095 }
96 }
97 }
98
99 // Disable launcher icon
Alexandra Gherghina25138512014-07-21 22:42:21 +0100100 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
101 pm.setComponentEnabledSetting(settingsComponentName,
102 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Kenny Guy4761da32017-05-16 18:54:21 +0100103 // Disable shortcut picker.
Fan Zhangf1f0f8b2018-06-22 10:40:07 -0700104 ComponentName shortcutComponentName = new ComponentName(
105 context, CreateShortcutActivity.class);
Kenny Guy4761da32017-05-16 18:54:21 +0100106 pm.setComponentEnabledSetting(shortcutComponentName,
107 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700108 }
Gustav Sennton66313bb2016-05-11 12:31:40 +0100109
110 // Disable WebView Setting if the current user is not an admin
111 private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) {
112 if (userInfo == null) {
113 return;
114 }
115 ComponentName settingsComponentName =
Gustav Sennton4d3334c2016-12-13 19:16:48 +0000116 new ComponentName(SETTINGS_PACKAGE, SETTINGS_PACKAGE + WEBVIEW_IMPLEMENTATION_ACTIVITY);
Gustav Sennton66313bb2016-05-11 12:31:40 +0100117 pm.setComponentEnabledSetting(settingsComponentName,
118 userInfo.isAdmin() ?
119 PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
120 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
121 PackageManager.DONT_KILL_APP);
122 }
Doris Lingf2d76802018-04-02 14:51:36 -0700123
124 // Refresh settings shortcuts to have correct intent flags
125 @VisibleForTesting
126 void refreshExistingShortcuts(Context context) {
127 final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
128 final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
129 final List<ShortcutInfo> updates = new ArrayList<>();
130 for (ShortcutInfo info : pinnedShortcuts) {
131 final Intent shortcutIntent = info.getIntent();
132 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
133 final ShortcutInfo updatedInfo = new ShortcutInfo.Builder(context, info.getId())
134 .setIntent(shortcutIntent)
135 .build();
136 updates.add(updatedInfo);
137 }
138 shortcutManager.updateShortcuts(updates);
139 }
140
Alexandra Gherghina25138512014-07-21 22:42:21 +0100141}