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; |
| 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static android.content.pm.PackageManager.GET_ACTIVITIES; |
| 31 | import static android.content.pm.PackageManager.GET_META_DATA; |
| 32 | import static android.content.pm.PackageManager.GET_RESOLVED_FILTER; |
| 33 | |
| 34 | /** |
| 35 | * Listens to {@link Intent.ACTION_BOOT_COMPLETED} and {@link Intent.ACTION_PRE_BOOT_COMPLETED} |
| 36 | * performs setup steps for a managed profile (disables the launcher icon of the Settings app and |
| 37 | * adds cross-profile intent filters for the appropriate Settings activities). |
| 38 | */ |
| 39 | public class ManagedProfileSetup extends BroadcastReceiver { |
| 40 | private static final String PRIMARY_PROFILE_SETTING = |
| 41 | "com.android.settings.PRIMARY_PROFILE_CONTROLLED"; |
| 42 | |
| 43 | @Override |
| 44 | public void onReceive(Context context, Intent broadcast) { |
| 45 | final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 46 | if (!Utils.isManagedProfile(um)) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Set up intent forwarding for implicit intents |
| 51 | Intent intent = new Intent(); |
| 52 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 53 | intent.setPackage(context.getPackageName()); |
| 54 | |
| 55 | final PackageManager pm = context.getPackageManager(); |
| 56 | // Resolves activities for the managed profile (which we're running as) |
| 57 | List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent, |
| 58 | GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER); |
| 59 | final int count = resolvedIntents.size(); |
| 60 | for (int i = 0; i < count; i++) { |
| 61 | ResolveInfo info = resolvedIntents.get(i); |
| 62 | if (info.filter != null && info.activityInfo != null |
| 63 | && info.activityInfo.metaData != null) { |
| 64 | boolean shouldForward = info.activityInfo.metaData.getBoolean( |
| 65 | PRIMARY_PROFILE_SETTING); |
| 66 | if (shouldForward) { |
| 67 | pm.addCrossProfileIntentFilter(info.filter, UserHandle.myUserId(), |
| 68 | UserHandle.USER_OWNER, PackageManager.SKIP_CURRENT_PROFILE); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Disable launcher icon |
| 74 | // Note: This needs to happen after forwarding intents, otherwise the main Settings |
| 75 | // intent gets lost |
| 76 | ComponentName settingsComponentName = new ComponentName(context, Settings.class); |
| 77 | pm.setComponentEnabledSetting(settingsComponentName, |
| 78 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
| 79 | } |
| 80 | } |