blob: ac012b06a650bcd496cf6fb30bfb2346d16217ff [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;
Xiaohui Chen762104e2015-09-01 10:26:53 -070025import android.content.pm.UserInfo;
Alexandra Gherghina25138512014-07-21 22:42:21 +010026import android.os.UserHandle;
27import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040028import android.util.Log;
Alexandra Gherghina25138512014-07-21 22:42:21 +010029
30import java.util.List;
31
32import static android.content.pm.PackageManager.GET_ACTIVITIES;
33import static android.content.pm.PackageManager.GET_META_DATA;
34import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
Tony Mak82d90962016-05-04 13:51:41 +010035import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
Alexandra Gherghina25138512014-07-21 22:42:21 +010036
37/**
38 * Listens to {@link Intent.ACTION_BOOT_COMPLETED} and {@link Intent.ACTION_PRE_BOOT_COMPLETED}
39 * performs setup steps for a managed profile (disables the launcher icon of the Settings app and
40 * adds cross-profile intent filters for the appropriate Settings activities).
41 */
42public class ManagedProfileSetup extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010043 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010044 private static final String PRIMARY_PROFILE_SETTING =
45 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
46
47 @Override
48 public void onReceive(Context context, Intent broadcast) {
49 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Xiaohui Chen762104e2015-09-01 10:26:53 -070050 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
51 if (userInfo == null || !userInfo.isManagedProfile()) {
Alexandra Gherghina25138512014-07-21 22:42:21 +010052 return;
53 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010054 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
55 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010056 final PackageManager pm = context.getPackageManager();
57 // Clear any previous intent forwarding we set up
Xiaohui Chen762104e2015-09-01 10:26:53 -070058 pm.clearCrossProfileIntentFilters(userInfo.id);
Alexandra Gherghina5667a682014-07-29 14:55:56 +010059
Alexandra Gherghina25138512014-07-21 22:42:21 +010060 // Set up intent forwarding for implicit intents
61 Intent intent = new Intent();
62 intent.addCategory(Intent.CATEGORY_DEFAULT);
63 intent.setPackage(context.getPackageName());
64
Alexandra Gherghina25138512014-07-21 22:42:21 +010065 // Resolves activities for the managed profile (which we're running as)
66 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
Tony Mak82d90962016-05-04 13:51:41 +010067 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
Alexandra Gherghina25138512014-07-21 22:42:21 +010068 final int count = resolvedIntents.size();
69 for (int i = 0; i < count; i++) {
70 ResolveInfo info = resolvedIntents.get(i);
71 if (info.filter != null && info.activityInfo != null
72 && info.activityInfo.metaData != null) {
73 boolean shouldForward = info.activityInfo.metaData.getBoolean(
74 PRIMARY_PROFILE_SETTING);
75 if (shouldForward) {
Xiaohui Chen762104e2015-09-01 10:26:53 -070076 pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
77 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
Alexandra Gherghina25138512014-07-21 22:42:21 +010078 }
79 }
80 }
81
82 // Disable launcher icon
83 // Note: This needs to happen after forwarding intents, otherwise the main Settings
84 // intent gets lost
85 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
86 pm.setComponentEnabledSetting(settingsComponentName,
87 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -070088 }
Alexandra Gherghina25138512014-07-21 22:42:21 +010089}