SetDevicePolicyTask.java revision bff80c5ff285b85b1c551a2a4ebd05e945cd6c0d
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 17package com.android.managedprovisioning.task; 18 19import android.app.AppGlobals; 20import android.app.admin.DevicePolicyManager; 21import android.content.ComponentName; 22import android.content.Context; 23import android.content.pm.IPackageManager; 24import android.content.pm.PackageManager; 25import android.os.RemoteException; 26import android.os.UserHandle; 27 28import com.android.internal.annotations.VisibleForTesting; 29import com.android.managedprovisioning.ProvisionLogger; 30import com.android.managedprovisioning.Utils; 31 32/** 33 * This tasks sets a given component as the owner of the device. 34 */ 35public class SetDevicePolicyTask { 36 37 private final Callback mCallback; 38 private final Context mContext; 39 private String mAdminPackage; 40 private ComponentName mAdminComponent; 41 private final String mOwnerName; 42 private final int mUserId; 43 44 private PackageManager mPackageManager; 45 private DevicePolicyManager mDevicePolicyManager; 46 47 public SetDevicePolicyTask(Context context, String ownerName, Callback callback) { 48 this(context, ownerName, callback, UserHandle.myUserId()); 49 } 50 51 @VisibleForTesting 52 /* package */ SetDevicePolicyTask(Context context, String ownerName, Callback callback, 53 int userId) { 54 mCallback = callback; 55 mContext = context; 56 mOwnerName = ownerName; 57 mUserId = userId; 58 59 mPackageManager = mContext.getPackageManager(); 60 mDevicePolicyManager = (DevicePolicyManager) mContext. 61 getSystemService(Context.DEVICE_POLICY_SERVICE); 62 } 63 64 public void run(ComponentName adminComponent) { 65 boolean success = true; 66 try { 67 mAdminComponent = adminComponent; 68 mAdminPackage = mAdminComponent.getPackageName(); 69 70 enableDevicePolicyApp(mAdminPackage); 71 setActiveAdmin(mAdminComponent); 72 success = setDeviceOwner(mAdminComponent, mOwnerName) 73 && setProfileOwner(mAdminComponent); 74 } catch (Exception e) { 75 ProvisionLogger.loge("Failure setting device or profile owner", e); 76 mCallback.onError(); 77 return; 78 } 79 if (success) { 80 mCallback.onSuccess(); 81 } else { 82 ProvisionLogger.loge("Error when setting device or profile owner."); 83 mCallback.onError(); 84 } 85 } 86 87 private void enableDevicePolicyApp(String packageName) { 88 int enabledSetting = mPackageManager.getApplicationEnabledSetting(packageName); 89 if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { 90 mPackageManager.setApplicationEnabledSetting(packageName, 91 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 92 // Device policy app may have launched ManagedProvisioning, play nice and don't 93 // kill it as a side-effect of this call. 94 PackageManager.DONT_KILL_APP); 95 } 96 } 97 98 private void setActiveAdmin(ComponentName component) { 99 ProvisionLogger.logd("Setting " + component + " as active admin."); 100 mDevicePolicyManager.setActiveAdmin(component, true, mUserId); 101 } 102 103 private boolean setDeviceOwner(ComponentName component, String owner) { 104 ProvisionLogger.logd("Setting " + component + " as device owner " + owner + "."); 105 if (!mDevicePolicyManager.isDeviceOwner(component)) { 106 return mDevicePolicyManager.setDeviceOwner(component, owner, mUserId); 107 } 108 return true; 109 } 110 111 private boolean setProfileOwner(ComponentName admin) { 112 ProvisionLogger.logd("Setting " + admin + " as profile owner "); 113 if (!mDevicePolicyManager.isProfileOwnerApp(admin.getPackageName())) { 114 return mDevicePolicyManager.setProfileOwner(admin, admin.getPackageName(), mUserId); 115 } 116 return true; 117 } 118 119 public abstract static class Callback { 120 public abstract void onSuccess(); 121 public abstract void onError(); 122 } 123} 124