blob: 198abe0bf35755be5bf20f448b829cce37fdb7a0 [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;
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010025import android.util.Log;
Alexandra Gherghina25138512014-07-21 22:42:21 +010026import android.os.UserHandle;
27import android.os.UserManager;
28
29import java.util.List;
30
31import static android.content.pm.PackageManager.GET_ACTIVITIES;
32import static android.content.pm.PackageManager.GET_META_DATA;
33import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
34
35/**
36 * Listens to {@link Intent.ACTION_BOOT_COMPLETED} and {@link Intent.ACTION_PRE_BOOT_COMPLETED}
37 * performs setup steps for a managed profile (disables the launcher icon of the Settings app and
38 * adds cross-profile intent filters for the appropriate Settings activities).
39 */
40public class ManagedProfileSetup extends BroadcastReceiver {
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010041 private static final String TAG = "Settings";
Alexandra Gherghina25138512014-07-21 22:42:21 +010042 private static final String PRIMARY_PROFILE_SETTING =
43 "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
44
45 @Override
46 public void onReceive(Context context, Intent broadcast) {
47 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
48 if (!Utils.isManagedProfile(um)) {
49 return;
50 }
Alexandra Gherghinaf2e39f22014-08-18 13:16:17 +010051 Log.i(TAG, "Received broadcast: " + broadcast.getAction()
52 + ". Setting up intent forwarding for managed profile.");
Alexandra Gherghina5667a682014-07-29 14:55:56 +010053 final PackageManager pm = context.getPackageManager();
54 // Clear any previous intent forwarding we set up
55 pm.clearCrossProfileIntentFilters(UserHandle.myUserId());
56
Alexandra Gherghina25138512014-07-21 22:42:21 +010057 // Set up intent forwarding for implicit intents
58 Intent intent = new Intent();
59 intent.addCategory(Intent.CATEGORY_DEFAULT);
60 intent.setPackage(context.getPackageName());
61
Alexandra Gherghina25138512014-07-21 22:42:21 +010062 // Resolves activities for the managed profile (which we're running as)
63 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
64 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER);
65 final int count = resolvedIntents.size();
66 for (int i = 0; i < count; i++) {
67 ResolveInfo info = resolvedIntents.get(i);
68 if (info.filter != null && info.activityInfo != null
69 && info.activityInfo.metaData != null) {
70 boolean shouldForward = info.activityInfo.metaData.getBoolean(
71 PRIMARY_PROFILE_SETTING);
72 if (shouldForward) {
73 pm.addCrossProfileIntentFilter(info.filter, UserHandle.myUserId(),
74 UserHandle.USER_OWNER, PackageManager.SKIP_CURRENT_PROFILE);
75 }
76 }
77 }
78
79 // Disable launcher icon
80 // Note: This needs to happen after forwarding intents, otherwise the main Settings
81 // intent gets lost
82 ComponentName settingsComponentName = new ComponentName(context, Settings.class);
83 pm.setComponentEnabledSetting(settingsComponentName,
84 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Amith Yamasani2d578ce2014-07-25 09:37:33 -070085 }
Alexandra Gherghina25138512014-07-21 22:42:21 +010086}