Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 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; |
Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
| 27 | |
| 28 | import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 29 | import com.android.managedprovisioning.task.DisableInstallShortcutListenersTask; |
Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 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 |
| 39 | public void onReceive(Context context, Intent intent) { |
Nicolas Prevot | 7d56b90 | 2014-11-03 15:26:06 +0000 | [diff] [blame] | 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. |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 53 | new DeleteNonRequiredAppsTask(context, dpm.getDeviceOwner(), |
| 54 | R.array.required_apps_managed_device, |
| 55 | R.array.vendor_required_apps_managed_device, |
| 56 | false /* not creating new profile */, |
| 57 | UserHandle.USER_OWNER, |
Julia Reynolds | ccd6016 | 2015-02-17 11:53:48 -0500 | [diff] [blame] | 58 | false /* delete non-required system apps */, |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 59 | new DeleteNonRequiredAppsTask.Callback() { |
| 60 | |
| 61 | @Override |
| 62 | public void onSuccess() {} |
| 63 | |
| 64 | @Override |
| 65 | public void onError() { |
| 66 | ProvisionLogger.loge("Error while checking if there are new system " |
| 67 | + "apps that need to be deleted"); |
| 68 | } |
| 69 | }).run(); |
Nicolas Prevot | 7d56b90 | 2014-11-03 15:26:06 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Check for managed profiles. |
| 73 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 74 | List<UserInfo> profiles = um.getProfiles(UserHandle.USER_OWNER); |
| 75 | if (profiles.size() <= 1) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Removes cross profile intent filters from the parent to all the managed profiles. |
| 80 | pm.clearCrossProfileIntentFilters(UserHandle.USER_OWNER); |
| 81 | |
| 82 | // For each managed profile reset cross profile intent filters and delete new apps. |
| 83 | for (UserInfo userInfo : profiles) { |
| 84 | if (!userInfo.isManagedProfile()) { |
| 85 | continue; |
Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 86 | } |
Nicolas Prevot | 7d56b90 | 2014-11-03 15:26:06 +0000 | [diff] [blame] | 87 | pm.clearCrossProfileIntentFilters(userInfo.id); |
| 88 | CrossProfileIntentFiltersHelper.setFilters( |
| 89 | pm, UserHandle.USER_OWNER, userInfo.id); |
| 90 | |
| 91 | ComponentName profileOwner = dpm.getProfileOwnerAsUser(userInfo.id); |
| 92 | if (profileOwner == null) { |
| 93 | // Shouldn't happen. |
| 94 | ProvisionLogger.loge("No profile owner on managed profile " + userInfo.id); |
| 95 | continue; |
| 96 | } |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 97 | |
Benjamin Franz | 7d3db0d | 2015-03-04 16:55:40 +0000 | [diff] [blame] | 98 | // always set the DISALLOW_WALLPAPER user restriction |
| 99 | um.setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, userInfo.getUserHandle()); |
| 100 | |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 101 | final DeleteNonRequiredAppsTask deleteNonRequiredAppsTask; |
| 102 | final DisableInstallShortcutListenersTask disableInstallShortcutListenersTask; |
| 103 | |
| 104 | disableInstallShortcutListenersTask = new DisableInstallShortcutListenersTask(context, |
| 105 | userInfo.id); |
| 106 | |
| 107 | deleteNonRequiredAppsTask = new DeleteNonRequiredAppsTask(context, |
| 108 | profileOwner.getPackageName(), |
Nicolas Prevot | 7d56b90 | 2014-11-03 15:26:06 +0000 | [diff] [blame] | 109 | R.array.required_apps_managed_profile, |
| 110 | R.array.vendor_required_apps_managed_profile, |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 111 | false /* not creating new profile */, |
| 112 | userInfo.id, |
Julia Reynolds | ccd6016 | 2015-02-17 11:53:48 -0500 | [diff] [blame] | 113 | false /* delete non-required system apps */, |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 114 | new DeleteNonRequiredAppsTask.Callback() { |
| 115 | |
| 116 | @Override |
| 117 | public void onSuccess() { |
| 118 | disableInstallShortcutListenersTask.run(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onError() { |
| 123 | ProvisionLogger.loge("Error while checking if there are new system " |
| 124 | + "apps that need to be deleted"); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | deleteNonRequiredAppsTask.run(); |
Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 131 | } |