Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 25 | import android.content.pm.UserInfo; |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 26 | import android.util.Log; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 27 | import android.os.UserHandle; |
| 28 | import android.os.UserManager; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static android.content.pm.PackageManager.GET_ACTIVITIES; |
| 33 | import static android.content.pm.PackageManager.GET_META_DATA; |
| 34 | import static android.content.pm.PackageManager.GET_RESOLVED_FILTER; |
| 35 | |
| 36 | /** |
| 37 | * Listens to {@link Intent.ACTION_BOOT_COMPLETED} and {@link Intent.ACTION_PRE_BOOT_COMPLETED} |
| 38 | * performs setup steps for a managed profile (disables the launcher icon of the Settings app and |
| 39 | * adds cross-profile intent filters for the appropriate Settings activities). |
| 40 | */ |
| 41 | public class ManagedProfileSetup extends BroadcastReceiver { |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 42 | private static final String TAG = "Settings"; |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 43 | private static final String PRIMARY_PROFILE_SETTING = |
| 44 | "com.android.settings.PRIMARY_PROFILE_CONTROLLED"; |
| 45 | |
| 46 | @Override |
| 47 | public void onReceive(Context context, Intent broadcast) { |
| 48 | final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 49 | UserInfo userInfo = um.getUserInfo(UserHandle.myUserId()); |
| 50 | if (userInfo == null || !userInfo.isManagedProfile()) { |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 51 | return; |
| 52 | } |
Alexandra Gherghina | f2e39f2 | 2014-08-18 13:16:17 +0100 | [diff] [blame] | 53 | Log.i(TAG, "Received broadcast: " + broadcast.getAction() |
| 54 | + ". Setting up intent forwarding for managed profile."); |
Alexandra Gherghina | 5667a68 | 2014-07-29 14:55:56 +0100 | [diff] [blame] | 55 | final PackageManager pm = context.getPackageManager(); |
| 56 | // Clear any previous intent forwarding we set up |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 57 | pm.clearCrossProfileIntentFilters(userInfo.id); |
Alexandra Gherghina | 5667a68 | 2014-07-29 14:55:56 +0100 | [diff] [blame] | 58 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 59 | // Set up intent forwarding for implicit intents |
| 60 | Intent intent = new Intent(); |
| 61 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 62 | intent.setPackage(context.getPackageName()); |
| 63 | |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 64 | // Resolves activities for the managed profile (which we're running as) |
| 65 | List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent, |
| 66 | GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER); |
| 67 | final int count = resolvedIntents.size(); |
| 68 | for (int i = 0; i < count; i++) { |
| 69 | ResolveInfo info = resolvedIntents.get(i); |
| 70 | if (info.filter != null && info.activityInfo != null |
| 71 | && info.activityInfo.metaData != null) { |
| 72 | boolean shouldForward = info.activityInfo.metaData.getBoolean( |
| 73 | PRIMARY_PROFILE_SETTING); |
| 74 | if (shouldForward) { |
Xiaohui Chen | 762104e | 2015-09-01 10:26:53 -0700 | [diff] [blame] | 75 | pm.addCrossProfileIntentFilter(info.filter, userInfo.id, |
| 76 | userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE); |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Disable launcher icon |
| 82 | // Note: This needs to happen after forwarding intents, otherwise the main Settings |
| 83 | // intent gets lost |
| 84 | ComponentName settingsComponentName = new ComponentName(context, Settings.class); |
| 85 | pm.setComponentEnabledSetting(settingsComponentName, |
| 86 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
Amith Yamasani | 2d578ce | 2014-07-25 09:37:33 -0700 | [diff] [blame] | 87 | } |
Alexandra Gherghina | 2513851 | 2014-07-21 22:42:21 +0100 | [diff] [blame] | 88 | } |