blob: 151ef9a6ddfdc3d88fa263fdeb761f5c419bbe99 [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;
25import android.content.pm.UserInfo;
26import android.os.UserHandle;
27import android.os.UserManager;
28
29import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask;
30
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.
53 deleteNonRequiredApps(context, dpm.getDeviceOwner(), UserHandle.USER_OWNER,
54 R.array.required_apps_managed_device,
55 R.array.vendor_required_apps_managed_device,
56 false /* Do not disable INSTALL_SHORTCUT listeners */);
57 }
58
59 // Check for managed profiles.
60 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
61 List<UserInfo> profiles = um.getProfiles(UserHandle.USER_OWNER);
62 if (profiles.size() <= 1) {
63 return;
64 }
65
66 // Removes cross profile intent filters from the parent to all the managed profiles.
67 pm.clearCrossProfileIntentFilters(UserHandle.USER_OWNER);
68
69 // For each managed profile reset cross profile intent filters and delete new apps.
70 for (UserInfo userInfo : profiles) {
71 if (!userInfo.isManagedProfile()) {
72 continue;
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +010073 }
Nicolas Prevot7d56b902014-11-03 15:26:06 +000074 pm.clearCrossProfileIntentFilters(userInfo.id);
75 CrossProfileIntentFiltersHelper.setFilters(
76 pm, UserHandle.USER_OWNER, userInfo.id);
77
78 ComponentName profileOwner = dpm.getProfileOwnerAsUser(userInfo.id);
79 if (profileOwner == null) {
80 // Shouldn't happen.
81 ProvisionLogger.loge("No profile owner on managed profile " + userInfo.id);
82 continue;
83 }
84 deleteNonRequiredApps(context, profileOwner.getPackageName(), userInfo.id,
85 R.array.required_apps_managed_profile,
86 R.array.vendor_required_apps_managed_profile,
87 true /* Disable INSTALL_SHORTCUT listeners */);
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +010088 }
89 }
90
Nicolas Prevot7d56b902014-11-03 15:26:06 +000091 private void deleteNonRequiredApps(Context context, String mdmPackageName, int userId,
92 int requiredAppsList, int vendorRequiredAppsList,
93 boolean disableInstallShortcutListeners) {
94 new DeleteNonRequiredAppsTask(context, mdmPackageName, userId,
95 requiredAppsList, vendorRequiredAppsList,
96 false /*we are not creating a new profile*/,
97 disableInstallShortcutListeners,
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +010098 new DeleteNonRequiredAppsTask.Callback() {
99
100 @Override
101 public void onSuccess() {
102 }
103
104 @Override
105 public void onError() {
106 ProvisionLogger.loge("Error while checking if there are new system "
107 + "apps that need to be deleted");
108 }
109 }).run();
110 }
111}