1 /* 2 * Copyright 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 package com.android.managedprovisioning; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.pm.PackageManager; 20 import android.content.pm.UserInfo; 21 import android.content.BroadcastReceiver; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 28 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; 29 import com.android.managedprovisioning.task.DisableInstallShortcutListenersTask; 30 31 import java.util.List; 32 33 /** 34 * After a system update, this class resets the cross-profile intent filters and checks 35 * if apps that have been added to the system image need to be deleted. 36 */ 37 public class PreBootListener extends BroadcastReceiver { 38 @Override onReceive(Context context, Intent intent)39 public void onReceive(Context context, Intent intent) { 40 if (context.getUserId() != UserHandle.USER_OWNER) { 41 return; 42 } 43 44 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService( 45 Context.DEVICE_POLICY_SERVICE); 46 PackageManager pm = context.getPackageManager(); 47 48 // Check for device owner. 49 if (dpm.getDeviceOwner() != null && DeleteNonRequiredAppsTask 50 .shouldDeleteNonRequiredApps(context, UserHandle.USER_OWNER)) { 51 52 // Delete new apps. 53 new DeleteNonRequiredAppsTask(context, dpm.getDeviceOwner(), 54 DeleteNonRequiredAppsTask.DEVICE_OWNER, 55 false /* not creating new profile */, 56 UserHandle.USER_OWNER, 57 false /* delete non-required system apps */, 58 new DeleteNonRequiredAppsTask.Callback() { 59 60 @Override 61 public void onSuccess() {} 62 63 @Override 64 public void onError() { 65 ProvisionLogger.loge("Error while checking if there are new system " 66 + "apps that need to be deleted"); 67 } 68 }).run(); 69 } 70 71 // Check for managed profiles. 72 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); 73 List<UserInfo> profiles = um.getProfiles(UserHandle.USER_OWNER); 74 if (profiles.size() <= 1) { 75 return; 76 } 77 78 // Removes cross profile intent filters from the parent to all the managed profiles. 79 pm.clearCrossProfileIntentFilters(UserHandle.USER_OWNER); 80 81 // For each managed profile reset cross profile intent filters and delete new apps. 82 for (UserInfo userInfo : profiles) { 83 if (!userInfo.isManagedProfile()) { 84 continue; 85 } 86 pm.clearCrossProfileIntentFilters(userInfo.id); 87 CrossProfileIntentFiltersHelper.setFilters( 88 pm, UserHandle.USER_OWNER, userInfo.id); 89 90 ComponentName profileOwner = dpm.getProfileOwnerAsUser(userInfo.id); 91 if (profileOwner == null) { 92 // Shouldn't happen. 93 ProvisionLogger.loge("No profile owner on managed profile " + userInfo.id); 94 continue; 95 } 96 97 // always set the DISALLOW_WALLPAPER user restriction 98 um.setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, userInfo.getUserHandle()); 99 100 final DeleteNonRequiredAppsTask deleteNonRequiredAppsTask; 101 final DisableInstallShortcutListenersTask disableInstallShortcutListenersTask; 102 103 disableInstallShortcutListenersTask = new DisableInstallShortcutListenersTask(context, 104 userInfo.id); 105 106 deleteNonRequiredAppsTask = new DeleteNonRequiredAppsTask(context, 107 profileOwner.getPackageName(), 108 DeleteNonRequiredAppsTask.PROFILE_OWNER, 109 false /* not creating new profile */, 110 userInfo.id, 111 false /* delete non-required system apps */, 112 new DeleteNonRequiredAppsTask.Callback() { 113 114 @Override 115 public void onSuccess() { 116 disableInstallShortcutListenersTask.run(); 117 } 118 119 @Override 120 public void onError() { 121 ProvisionLogger.loge("Error while checking if there are new system " 122 + "apps that need to be deleted"); 123 } 124 }); 125 126 deleteNonRequiredAppsTask.run(); 127 } 128 } 129 } 130