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.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ActivityInfo; 24 import android.content.pm.PackageInfo; 25 import android.content.pm.PackageManager; 26 import android.content.pm.PackageManager.NameNotFoundException; 27 import android.text.TextUtils; 28 29 import com.android.managedprovisioning.ProvisionLogger; 30 31 public class SetDevicePolicyTask { 32 public static final int ERROR_PACKAGE_NOT_INSTALLED = 0; 33 public static final int ERROR_NO_RECEIVER = 1; 34 public static final int ERROR_OTHER = 2; 35 36 private final Callback mCallback; 37 private final Context mContext; 38 private final String mPackageName; 39 private final String mOwner; 40 41 private String mAdminReceiver; 42 private PackageManager mPackageManager; 43 private DevicePolicyManager mDevicePolicyManager; 44 SetDevicePolicyTask(Context context, String packageName, String owner, Callback callback)45 public SetDevicePolicyTask(Context context, String packageName, String owner, 46 Callback callback) { 47 mCallback = callback; 48 mContext = context; 49 mPackageName = packageName; 50 mOwner = owner; 51 mPackageManager = mContext.getPackageManager(); 52 mDevicePolicyManager = (DevicePolicyManager) mContext. 53 getSystemService(Context.DEVICE_POLICY_SERVICE); 54 } 55 run()56 public void run() { 57 // Check whether package is installed and find the admin receiver. 58 if (isPackageInstalled()) { 59 enableDevicePolicyApp(); 60 setActiveAdmin(); 61 setDeviceOwner(); 62 mCallback.onSuccess(); 63 } 64 } 65 isPackageInstalled()66 private boolean isPackageInstalled() { 67 try { 68 PackageInfo pi = mPackageManager.getPackageInfo(mPackageName, 69 PackageManager.GET_RECEIVERS); 70 for (ActivityInfo ai : pi.receivers) { 71 if (!TextUtils.isEmpty(ai.permission) && 72 ai.permission.equals(android.Manifest.permission.BIND_DEVICE_ADMIN)) { 73 mAdminReceiver = ai.name; 74 return true; 75 } 76 } 77 mCallback.onError(ERROR_NO_RECEIVER); 78 return false; 79 } catch (NameNotFoundException e) { 80 mCallback.onError(ERROR_PACKAGE_NOT_INSTALLED); 81 return false; 82 } 83 } 84 enableDevicePolicyApp()85 private void enableDevicePolicyApp() { 86 int enabledSetting = mPackageManager 87 .getApplicationEnabledSetting(mPackageName); 88 if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { 89 mPackageManager.setApplicationEnabledSetting(mPackageName, 90 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0); 91 } 92 } 93 setActiveAdmin()94 public void setActiveAdmin() { 95 ProvisionLogger.logd("Setting " + mPackageName + " as active admin."); 96 ComponentName component = new ComponentName(mPackageName, mAdminReceiver); 97 mDevicePolicyManager.setActiveAdmin(component, true); 98 } 99 setDeviceOwner()100 public void setDeviceOwner() { 101 ProvisionLogger.logd("Setting " + mPackageName + " as device owner " + mOwner + "."); 102 if (!mDevicePolicyManager.isDeviceOwner(mPackageName)) { 103 mDevicePolicyManager.setDeviceOwner(mPackageName, mOwner); 104 } 105 } 106 107 public abstract static class Callback { onSuccess()108 public abstract void onSuccess(); onError(int errorCode)109 public abstract void onError(int errorCode); 110 } 111 } 112