blob: 692dbecdbc52a5139d2ce1e49db24beb68521332 [file] [log] [blame]
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +01001/*
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 */
16package com.android.managedprovisioning;
17
18import android.app.admin.DevicePolicyManager;
19import android.content.pm.PackageManager;
20import android.content.pm.UserInfo;
21import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +010025import android.os.UserHandle;
26import android.os.UserManager;
27
28import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask;
Sander Alewijnsed5e4c422014-11-25 17:56:16 +000029import com.android.managedprovisioning.task.DisableInstallShortcutListenersTask;
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +010030
31import 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 */
37public class PreBootListener extends BroadcastReceiver {
38 @Override
39 public void onReceive(Context context, Intent intent) {
Nicolas Prevot7d56b902014-11-03 15:26:06 +000040 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 Alewijnsed5e4c422014-11-25 17:56:16 +000053 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 Reynoldsccd60162015-02-17 11:53:48 -050058 false /* delete non-required system apps */,
Sander Alewijnsed5e4c422014-11-25 17:56:16 +000059 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 Prevot7d56b902014-11-03 15:26:06 +000070 }
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 Prevot3b76f0d2014-09-03 15:33:42 +010086 }
Nicolas Prevot7d56b902014-11-03 15:26:06 +000087 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 Alewijnsed5e4c422014-11-25 17:56:16 +000097
Benjamin Franz7d3db0d2015-03-04 16:55:40 +000098 // always set the DISALLOW_WALLPAPER user restriction
99 um.setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, userInfo.getUserHandle());
100
Sander Alewijnsed5e4c422014-11-25 17:56:16 +0000101 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 Prevot7d56b902014-11-03 15:26:06 +0000109 R.array.required_apps_managed_profile,
110 R.array.vendor_required_apps_managed_profile,
Sander Alewijnsed5e4c422014-11-25 17:56:16 +0000111 false /* not creating new profile */,
112 userInfo.id,
Julia Reynoldsccd60162015-02-17 11:53:48 -0500113 false /* delete non-required system apps */,
Sander Alewijnsed5e4c422014-11-25 17:56:16 +0000114 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 Prevot3b76f0d2014-09-03 15:33:42 +0100129 }
130 }
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +0100131}