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