Home | History | Annotate | Download | only in task
      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 
     17 package com.android.managedprovisioning.task;
     18 
     19 import android.app.AppGlobals;
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.pm.IPackageManager;
     24 import android.content.pm.PackageManager;
     25 import android.os.RemoteException;
     26 import android.os.UserHandle;
     27 
     28 import com.android.managedprovisioning.ProvisionLogger;
     29 import com.android.managedprovisioning.Utils;
     30 
     31 /**
     32  * This tasks sets a given component as the owner of the device. If provided it also sets a given
     33  * component as the device initializer, which can perform additional setup steps at the end of
     34  * provisioning before setting the device as provisioned.
     35  */
     36 public class SetDevicePolicyTask {
     37     public static final int ERROR_PACKAGE_NOT_INSTALLED = 0;
     38     public static final int ERROR_NO_RECEIVER = 1;
     39     public static final int ERROR_OTHER = 2;
     40 
     41     private final Callback mCallback;
     42     private final Context mContext;
     43     private String mAdminPackage;
     44     private ComponentName mAdminComponent;
     45     private final String mOwnerName;
     46     private ComponentName mInitializerComponent;
     47     private String mInitializerPackageName;
     48 
     49     private PackageManager mPackageManager;
     50     private DevicePolicyManager mDevicePolicyManager;
     51 
     52     public SetDevicePolicyTask(Context context, String ownerName,
     53             ComponentName initializerComponent, Callback callback) {
     54         mCallback = callback;
     55         mContext = context;
     56         mOwnerName = ownerName;
     57         mInitializerComponent = initializerComponent;
     58         if (mInitializerComponent != null) {
     59             mInitializerPackageName = initializerComponent.getPackageName();
     60         }
     61 
     62         mPackageManager = mContext.getPackageManager();
     63         mDevicePolicyManager = (DevicePolicyManager) mContext.
     64                 getSystemService(Context.DEVICE_POLICY_SERVICE);
     65     }
     66 
     67     public void run(ComponentName adminComponent) {
     68         try {
     69             mAdminComponent = adminComponent;
     70             mAdminPackage = mAdminComponent.getPackageName();
     71 
     72             enableDevicePolicyApp(mAdminPackage);
     73             setActiveAdmin(mAdminComponent);
     74             setDeviceOwner(mAdminPackage, mOwnerName);
     75 
     76             if (mInitializerComponent != null) {
     77                 // For secondary users, set device owner package as profile owner as well, in order
     78                 // to give it DO/PO privileges. This only applies if device initializer is present.
     79                 if (!Utils.isCurrentUserOwner() && !Utils.isManagedProfile(mContext)) {
     80                     int userId = UserHandle.myUserId();
     81                     if (!mDevicePolicyManager.setProfileOwner(mAdminComponent, mAdminPackage,
     82                             userId)) {
     83                         ProvisionLogger.loge("Fail to set profile owner for user " + userId);
     84                         mCallback.onError(ERROR_OTHER);
     85                         return;
     86                     }
     87                 }
     88                 enableDevicePolicyApp(mInitializerPackageName);
     89                 setActiveAdmin(mInitializerComponent);
     90                 if (!setDeviceInitializer(mInitializerComponent)) {
     91                     // error reported in setDeviceInitializer
     92                     return;
     93                 }
     94 
     95             }
     96         } catch (Exception e) {
     97             ProvisionLogger.loge("Failure setting device owner or initializer", e);
     98             mCallback.onError(ERROR_OTHER);
     99             return;
    100         }
    101 
    102         mCallback.onSuccess();
    103     }
    104 
    105     private void enableDevicePolicyApp(String packageName) {
    106         int enabledSetting = mPackageManager.getApplicationEnabledSetting(packageName);
    107         if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
    108             mPackageManager.setApplicationEnabledSetting(packageName,
    109                     PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
    110                     // Device policy app may have launched ManagedProvisioning, play nice and don't
    111                     // kill it as a side-effect of this call.
    112                     PackageManager.DONT_KILL_APP);
    113         }
    114     }
    115 
    116     public void setActiveAdmin(ComponentName component) {
    117         ProvisionLogger.logd("Setting " + component + " as active admin.");
    118         mDevicePolicyManager.setActiveAdmin(component, true);
    119     }
    120 
    121     public void setDeviceOwner(String packageName, String owner) {
    122         ProvisionLogger.logd("Setting " + packageName + " as device owner " + owner + ".");
    123         if (!mDevicePolicyManager.isDeviceOwner(packageName)) {
    124             mDevicePolicyManager.setDeviceOwner(packageName, owner);
    125         }
    126     }
    127 
    128     public boolean setDeviceInitializer(ComponentName component) {
    129         ProvisionLogger.logd("Setting " + component + " as device initializer.");
    130         if (!mDevicePolicyManager.isDeviceInitializerApp(component.getPackageName())) {
    131             mDevicePolicyManager.setDeviceInitializer(null, component);
    132         }
    133         IPackageManager pm = AppGlobals.getPackageManager();
    134         try {
    135             pm.setBlockUninstallForUser(component.getPackageName(), true,
    136                     UserHandle.getCallingUserId());
    137         } catch (RemoteException e) {
    138             ProvisionLogger.loge("Failed to block uninstall of device initializer app", e);
    139             mCallback.onError(ERROR_OTHER);
    140             return false;
    141         }
    142         return true;
    143     }
    144 
    145     public abstract static class Callback {
    146         public abstract void onSuccess();
    147         public abstract void onError(int errorCode);
    148     }
    149 }
    150