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.content.pm.UserInfo; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.util.Log; 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 import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; 36 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, 40 * adds cross-profile intent filters for the appropriate Settings activities), and disables the 41 * webview setting for non-admin users. 42 */ 43 public class SettingsInitialize extends BroadcastReceiver { 44 private static final String TAG = "Settings"; 45 private static final String PRIMARY_PROFILE_SETTING = 46 "com.android.settings.PRIMARY_PROFILE_CONTROLLED"; 47 48 @Override 49 public void onReceive(Context context, Intent broadcast) { 50 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); 51 UserInfo userInfo = um.getUserInfo(UserHandle.myUserId()); 52 final PackageManager pm = context.getPackageManager(); 53 managedProfileSetup(context, pm, broadcast, userInfo); 54 webviewSettingSetup(context, pm, userInfo); 55 } 56 57 private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast, 58 UserInfo userInfo) { 59 if (userInfo == null || !userInfo.isManagedProfile()) { 60 return; 61 } 62 Log.i(TAG, "Received broadcast: " + broadcast.getAction() 63 + ". Setting up intent forwarding for managed profile."); 64 // Clear any previous intent forwarding we set up 65 pm.clearCrossProfileIntentFilters(userInfo.id); 66 67 // Set up intent forwarding for implicit intents 68 Intent intent = new Intent(); 69 intent.addCategory(Intent.CATEGORY_DEFAULT); 70 intent.setPackage(context.getPackageName()); 71 72 // Resolves activities for the managed profile (which we're running as) 73 List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent, 74 GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS); 75 final int count = resolvedIntents.size(); 76 for (int i = 0; i < count; i++) { 77 ResolveInfo info = resolvedIntents.get(i); 78 if (info.filter != null && info.activityInfo != null 79 && info.activityInfo.metaData != null) { 80 boolean shouldForward = info.activityInfo.metaData.getBoolean( 81 PRIMARY_PROFILE_SETTING); 82 if (shouldForward) { 83 pm.addCrossProfileIntentFilter(info.filter, userInfo.id, 84 userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE); 85 } 86 } 87 } 88 89 // Disable launcher icon 90 // Note: This needs to happen after forwarding intents, otherwise the main Settings 91 // intent gets lost 92 ComponentName settingsComponentName = new ComponentName(context, Settings.class); 93 pm.setComponentEnabledSetting(settingsComponentName, 94 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 95 } 96 97 // Disable WebView Setting if the current user is not an admin 98 private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) { 99 if (userInfo == null) { 100 return; 101 } 102 ComponentName settingsComponentName = 103 new ComponentName(context, WebViewImplementation.class); 104 pm.setComponentEnabledSetting(settingsComponentName, 105 userInfo.isAdmin() ? 106 PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 107 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 108 PackageManager.DONT_KILL_APP); 109 } 110 } 111