1 /*
2  * Copyright 2016, 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.provisioning;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.managedprovisioning.R;
24 import com.android.managedprovisioning.finalization.FinalizationController;
25 import com.android.managedprovisioning.model.ProvisioningParams;
26 import com.android.managedprovisioning.task.AbstractProvisioningTask;
27 import com.android.managedprovisioning.task.AddWifiNetworkTask;
28 import com.android.managedprovisioning.task.CopyAccountToUserTask;
29 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask;
30 import com.android.managedprovisioning.task.DeviceOwnerInitializeProvisioningTask;
31 import com.android.managedprovisioning.task.DisallowAddUserTask;
32 import com.android.managedprovisioning.task.DownloadPackageTask;
33 import com.android.managedprovisioning.task.InstallPackageTask;
34 import com.android.managedprovisioning.task.SetDevicePolicyTask;
35 import com.android.managedprovisioning.task.VerifyPackageTask;
36 
37 /**
38  * Controller for Device Owner provisioning.
39  */
40 public class DeviceOwnerProvisioningController extends AbstractProvisioningController {
41 
DeviceOwnerProvisioningController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback)42     public DeviceOwnerProvisioningController(
43             Context context,
44             ProvisioningParams params,
45             int userId,
46             ProvisioningControllerCallback callback) {
47         this(context, params, userId, callback, new FinalizationController(context));
48     }
49 
50     @VisibleForTesting
DeviceOwnerProvisioningController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback, FinalizationController finalizationController)51     DeviceOwnerProvisioningController(
52             Context context,
53             ProvisioningParams params,
54             int userId,
55             ProvisioningControllerCallback callback,
56             FinalizationController finalizationController) {
57         super(context, params, userId, callback, finalizationController);
58     }
59 
setUpTasks()60     protected void setUpTasks() {
61         addTasks(new DeviceOwnerInitializeProvisioningTask(mContext, mParams, this));
62 
63         if (mParams.wifiInfo != null) {
64             addTasks(new AddWifiNetworkTask(mContext, mParams, this));
65         }
66 
67         if (mParams.deviceAdminDownloadInfo != null) {
68             DownloadPackageTask downloadTask = new DownloadPackageTask(mContext, mParams, this);
69             addTasks(downloadTask,
70                     new VerifyPackageTask(downloadTask, mContext, mParams, this),
71                     new InstallPackageTask(downloadTask, mContext, mParams, this));
72         }
73 
74         addTasks(
75                 new DeleteNonRequiredAppsTask(true /* new profile */, mContext, mParams, this),
76                 new SetDevicePolicyTask(mContext, mParams, this),
77                 new DisallowAddUserTask(mContext, mParams, this)
78         );
79 
80         if (mParams.accountToMigrate != null) {
81             addTasks(new CopyAccountToUserTask(UserHandle.USER_SYSTEM, mContext, mParams, this));
82         }
83     }
84 
getErrorTitle()85     @Override protected int getErrorTitle() {
86         return R.string.cant_set_up_device;
87     }
88 
89     @Override
getErrorMsgId(AbstractProvisioningTask task, int errorCode)90     protected int getErrorMsgId(AbstractProvisioningTask task, int errorCode) {
91         if (task instanceof AddWifiNetworkTask) {
92             return R.string.device_owner_error_wifi;
93         } else if (task instanceof DownloadPackageTask) {
94             switch (errorCode) {
95                 case DownloadPackageTask.ERROR_DOWNLOAD_FAILED:
96                     return R.string.device_owner_error_download_failed;
97                 case DownloadPackageTask.ERROR_OTHER:
98                     return R.string.cant_set_up_device;
99             }
100         } else if (task instanceof VerifyPackageTask) {
101             switch (errorCode) {
102                 case VerifyPackageTask.ERROR_HASH_MISMATCH:
103                     return R.string.device_owner_error_hash_mismatch;
104                 case VerifyPackageTask.ERROR_DEVICE_ADMIN_MISSING:
105                     return R.string.device_owner_error_package_invalid;
106             }
107         } else if (task instanceof InstallPackageTask) {
108             switch (errorCode) {
109                 case InstallPackageTask.ERROR_PACKAGE_INVALID:
110                     return R.string.device_owner_error_package_invalid;
111                 case InstallPackageTask.ERROR_INSTALLATION_FAILED:
112                     return R.string.device_owner_error_installation_failed;
113             }
114         }
115 
116         return R.string.cant_set_up_device;
117     }
118 
119     @Override
getRequireFactoryReset(AbstractProvisioningTask task, int errorCode)120     protected boolean getRequireFactoryReset(AbstractProvisioningTask task, int errorCode) {
121         return !((task instanceof AddWifiNetworkTask)
122                 || (task instanceof DeviceOwnerInitializeProvisioningTask));
123     }
124 
125     @Override
performCleanup()126     protected void performCleanup() {
127         // Do nothing, because a factory reset will be triggered.
128     }
129 }
130