Home | History | Annotate | Download | only in ota
      1 /*
      2  * Copyright 2016, 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.managedprovisioning.ota;
     18 
     19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
     20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
     21 import static com.android.internal.util.Preconditions.checkNotNull;
     22 
     23 import android.app.admin.DevicePolicyManager;
     24 import android.content.ComponentName;
     25 import android.content.Context;
     26 import android.content.pm.PackageManager;
     27 import android.content.pm.UserInfo;
     28 import android.os.UserHandle;
     29 import android.os.UserManager;
     30 
     31 import com.android.internal.annotations.VisibleForTesting;
     32 import com.android.managedprovisioning.common.ProvisionLogger;
     33 import com.android.managedprovisioning.model.ProvisioningParams;
     34 import com.android.managedprovisioning.task.CrossProfileIntentFiltersSetter;
     35 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask;
     36 import com.android.managedprovisioning.task.DisableInstallShortcutListenersTask;
     37 import com.android.managedprovisioning.task.DisallowAddUserTask;
     38 import com.android.managedprovisioning.task.InstallExistingPackageTask;
     39 
     40 /**
     41  * After a system update, this class resets the cross-profile intent filters and performs any
     42  * tasks necessary to bring the system up to date.
     43  */
     44 public class OtaController {
     45 
     46     private static final String TELECOM_PACKAGE = "com.android.server.telecom";
     47 
     48     private final Context mContext;
     49     private final TaskExecutor mTaskExecutor;
     50     private final CrossProfileIntentFiltersSetter mCrossProfileIntentFiltersSetter;
     51 
     52     private final UserManager mUserManager;
     53     private final DevicePolicyManager mDevicePolicyManager;
     54 
     55     public OtaController(Context context) {
     56         this(context, new TaskExecutor(), new CrossProfileIntentFiltersSetter(context));
     57     }
     58 
     59     @VisibleForTesting
     60     OtaController(Context context, TaskExecutor taskExecutor,
     61             CrossProfileIntentFiltersSetter crossProfileIntentFiltersSetter) {
     62         mContext = checkNotNull(context);
     63         mTaskExecutor = checkNotNull(taskExecutor);
     64         mCrossProfileIntentFiltersSetter = checkNotNull(crossProfileIntentFiltersSetter);
     65 
     66         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     67         mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(
     68                 Context.DEVICE_POLICY_SERVICE);
     69     }
     70 
     71     public void run() {
     72         if (mContext.getUserId() != UserHandle.USER_SYSTEM) {
     73             return;
     74         }
     75 
     76         // Check for device owner.
     77         final int deviceOwnerUserId = mDevicePolicyManager.getDeviceOwnerUserId();
     78         if (deviceOwnerUserId != UserHandle.USER_NULL) {
     79             addDeviceOwnerTasks(deviceOwnerUserId, mContext);
     80         }
     81 
     82         for (UserInfo userInfo : mUserManager.getUsers()) {
     83             if (userInfo.isManagedProfile()) {
     84                 addManagedProfileTasks(userInfo.id, mContext);
     85             } else {
     86                 // if this user has managed profiles, reset the cross-profile intent filters between
     87                 // this user and its managed profiles.
     88                 mCrossProfileIntentFiltersSetter.resetFilters(userInfo.id);
     89             }
     90         }
     91     }
     92 
     93     void addDeviceOwnerTasks(final int userId, Context context) {
     94         ComponentName deviceOwner = mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser();
     95         if (deviceOwner == null) {
     96             // Shouldn't happen
     97             ProvisionLogger.loge("No device owner found.");
     98             return;
     99         }
    100 
    101         // Build a set of fake params to be able to run the tasks
    102         ProvisioningParams fakeParams = new ProvisioningParams.Builder()
    103                 .setDeviceAdminComponentName(deviceOwner)
    104                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
    105                 .build();
    106 
    107         mTaskExecutor.execute(userId,
    108                 new DeleteNonRequiredAppsTask(false, context, fakeParams, mTaskExecutor));
    109         mTaskExecutor.execute(userId,
    110                 new DisallowAddUserTask(context, fakeParams, mTaskExecutor));
    111     }
    112 
    113     void addManagedProfileTasks(final int userId, Context context) {
    114         mUserManager.setUserRestriction(UserManager.DISALLOW_WALLPAPER, true,
    115                 UserHandle.of(userId));
    116         // Enabling telecom package as it supports managed profiles from N.
    117         mTaskExecutor.execute(userId,
    118                 new InstallExistingPackageTask(TELECOM_PACKAGE, context, null, mTaskExecutor));
    119 
    120         ComponentName profileOwner = mDevicePolicyManager.getProfileOwnerAsUser(userId);
    121         if (profileOwner == null) {
    122             // Shouldn't happen.
    123             ProvisionLogger.loge("No profile owner on managed profile " + userId);
    124             return;
    125         }
    126 
    127         // Build a set of fake params to be able to run the tasks
    128         ProvisioningParams fakeParams = new ProvisioningParams.Builder()
    129                 .setDeviceAdminComponentName(profileOwner)
    130                 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
    131                 .build();
    132         mTaskExecutor.execute(userId,
    133                 new DisableInstallShortcutListenersTask(context, fakeParams, mTaskExecutor));
    134         mTaskExecutor.execute(userId,
    135                 new DeleteNonRequiredAppsTask(false, context, fakeParams, mTaskExecutor));
    136     }
    137 }
    138