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 static com.android.internal.util.Preconditions.checkNotNull;
     20 
     21 import android.app.admin.DevicePolicyManager;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.pm.PackageManager;
     25 
     26 import com.android.internal.annotations.VisibleForTesting;
     27 import com.android.managedprovisioning.common.ProvisionLogger;
     28 import com.android.managedprovisioning.R;
     29 import com.android.managedprovisioning.common.Utils;
     30 import com.android.managedprovisioning.model.ProvisioningParams;
     31 
     32 /**
     33  * This tasks sets a given component as the device or profile owner. It also enables the management
     34  * app if it's not currently enabled and sets the component as active admin.
     35  */
     36 public class SetDevicePolicyTask extends AbstractProvisioningTask {
     37 
     38     private final PackageManager mPackageManager;
     39     private final DevicePolicyManager mDevicePolicyManager;
     40     private final Utils mUtils;
     41 
     42     public SetDevicePolicyTask(
     43             Context context,
     44             ProvisioningParams params,
     45             Callback callback) {
     46         this(new Utils(), context, params, callback);
     47     }
     48 
     49     @VisibleForTesting
     50     SetDevicePolicyTask(Utils utils,
     51                         Context context,
     52                         ProvisioningParams params,
     53                         Callback callback) {
     54         super(context, params, callback);
     55 
     56         mUtils = checkNotNull(utils);
     57         mPackageManager = mContext.getPackageManager();
     58         mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(
     59                 Context.DEVICE_POLICY_SERVICE);
     60     }
     61 
     62     @Override
     63     public int getStatusMsgId() {
     64         return R.string.progress_set_owner;
     65     }
     66 
     67     @Override
     68     public void run(int userId) {
     69         boolean success;
     70         try {
     71             ComponentName adminComponent = mUtils.findDeviceAdmin(
     72                     mProvisioningParams.deviceAdminPackageName,
     73                     mProvisioningParams.deviceAdminComponentName, mContext);
     74             String adminPackage = adminComponent.getPackageName();
     75 
     76             enableDevicePolicyApp(adminPackage);
     77             setActiveAdmin(adminComponent, userId);
     78             if (mUtils.isProfileOwnerAction(mProvisioningParams.provisioningAction)) {
     79                 success = setProfileOwner(adminComponent, userId);
     80             } else {
     81                 success = setDeviceOwner(adminComponent,
     82                         mContext.getResources().getString(R.string.default_owned_device_username),
     83                         userId);
     84             }
     85         } catch (Exception e) {
     86             ProvisionLogger.loge("Failure setting device or profile owner", e);
     87             error(0);
     88             return;
     89         }
     90 
     91         if (success) {
     92             success();
     93         } else {
     94             ProvisionLogger.loge("Error when setting device or profile owner.");
     95             error(0);
     96         }
     97     }
     98 
     99     private void enableDevicePolicyApp(String packageName) {
    100         int enabledSetting = mPackageManager.getApplicationEnabledSetting(packageName);
    101         if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
    102                 && enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
    103             mPackageManager.setApplicationEnabledSetting(packageName,
    104                     PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
    105                     // Device policy app may have launched ManagedProvisioning, play nice and don't
    106                     // kill it as a side-effect of this call.
    107                     PackageManager.DONT_KILL_APP);
    108         }
    109     }
    110 
    111     private void setActiveAdmin(ComponentName component, int userId) {
    112         ProvisionLogger.logd("Setting " + component + " as active admin.");
    113         mDevicePolicyManager.setActiveAdmin(component, true, userId);
    114     }
    115 
    116     private boolean setDeviceOwner(ComponentName component, String owner, int userId) {
    117         ProvisionLogger.logd("Setting " + component + " as device owner of user " + userId);
    118         if (!component.equals(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser())) {
    119             return mDevicePolicyManager.setDeviceOwner(component, owner, userId);
    120         }
    121         return true;
    122     }
    123 
    124     private boolean setProfileOwner(ComponentName component, int userId) {
    125         ProvisionLogger.logd("Setting " + component + " as profile owner of user " + userId);
    126         if (!component.equals(mDevicePolicyManager.getProfileOwnerAsUser(userId))) {
    127             return mDevicePolicyManager.setProfileOwner(component, component.getPackageName(),
    128                     userId);
    129         }
    130         return true;
    131     }
    132 }
    133