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