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 mPackageManager.setApplicationEnabledSetting(packageName, 103 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 104 // Device policy app may have launched ManagedProvisioning, play nice and don't 105 // kill it as a side-effect of this call. 106 PackageManager.DONT_KILL_APP); 107 } 108 } 109 110 private void setActiveAdmin(ComponentName component, int userId) { 111 ProvisionLogger.logd("Setting " + component + " as active admin."); 112 mDevicePolicyManager.setActiveAdmin(component, true, userId); 113 } 114 115 private boolean setDeviceOwner(ComponentName component, String owner, int userId) { 116 ProvisionLogger.logd("Setting " + component + " as device owner of user " + userId); 117 if (!component.equals(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser())) { 118 return mDevicePolicyManager.setDeviceOwner(component, owner, userId); 119 } 120 return true; 121 } 122 123 private boolean setProfileOwner(ComponentName component, int userId) { 124 ProvisionLogger.logd("Setting " + component + " as profile owner of user " + userId); 125 if (!component.equals(mDevicePolicyManager.getProfileOwnerAsUser(userId))) { 126 return mDevicePolicyManager.setProfileOwner(component, component.getPackageName(), 127 userId); 128 } 129 return true; 130 } 131 } 132