1 /*
2  * Copyright 2019, 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 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.managedprovisioning.R;
23 import com.android.managedprovisioning.finalization.FinalizationController;
24 import com.android.managedprovisioning.model.ProvisioningParams;
25 import com.android.managedprovisioning.task.AbstractProvisioningTask;
26 import com.android.managedprovisioning.task.AddWifiNetworkTask;
27 import com.android.managedprovisioning.task.ConnectMobileNetworkTask;
28 import com.android.managedprovisioning.task.DownloadPackageTask;
29 import com.android.managedprovisioning.task.InstallPackageTask;
30 import com.android.managedprovisioning.task.VerifyPackageTask;
31 
32 /**
33  * Controller for preparing admin integrated flow. This includes connecting to wi-fi and
34  * mobile network, downloading, verifying and installing the admin app.
35  */
36 public class AdminIntegratedFlowPrepareController extends AbstractProvisioningController {
37 
AdminIntegratedFlowPrepareController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback)38     public AdminIntegratedFlowPrepareController(
39             Context context,
40             ProvisioningParams params,
41             int userId,
42             ProvisioningControllerCallback callback) {
43         this(context, params, userId, callback, new FinalizationController(context));
44     }
45 
46     @VisibleForTesting
AdminIntegratedFlowPrepareController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback, FinalizationController finalizationController)47     AdminIntegratedFlowPrepareController(
48             Context context,
49             ProvisioningParams params,
50             int userId,
51             ProvisioningControllerCallback callback,
52             FinalizationController finalizationController) {
53         super(context, params, userId, callback, finalizationController);
54     }
55 
56     @Override
setUpTasks()57     protected void setUpTasks() {
58         if (mParams.wifiInfo != null) {
59             addTasks(new AddWifiNetworkTask(mContext, mParams, this));
60         } else if (mParams.useMobileData) {
61             addTasks(new ConnectMobileNetworkTask(mContext, mParams, this));
62         }
63 
64         if (mParams.deviceAdminDownloadInfo != null) {
65             DownloadPackageTask downloadTask = new DownloadPackageTask(mContext, mParams, this);
66             addTasks(downloadTask,
67                     new VerifyPackageTask(downloadTask, mContext, mParams, this),
68                     new InstallPackageTask(downloadTask, mContext, mParams, this));
69         }
70     }
71 
72     @Override
getErrorTitle()73     protected int getErrorTitle() {
74         return R.string.cant_set_up_device;
75     }
76 
77     @Override
getErrorMsgId(AbstractProvisioningTask task, int errorCode)78     protected int getErrorMsgId(AbstractProvisioningTask task, int errorCode) {
79         if (task instanceof AddWifiNetworkTask) {
80             return R.string.error_wifi;
81         } else if (task instanceof DownloadPackageTask) {
82             switch (errorCode) {
83                 case DownloadPackageTask.ERROR_DOWNLOAD_FAILED:
84                     return R.string.error_download_failed;
85                 case DownloadPackageTask.ERROR_OTHER:
86                     return R.string.cant_set_up_device;
87             }
88         } else if (task instanceof VerifyPackageTask) {
89             switch (errorCode) {
90                 case VerifyPackageTask.ERROR_HASH_MISMATCH:
91                     return R.string.error_hash_mismatch;
92                 case VerifyPackageTask.ERROR_DEVICE_ADMIN_MISSING:
93                     return R.string.error_package_invalid;
94             }
95         } else if (task instanceof InstallPackageTask) {
96             switch (errorCode) {
97                 case InstallPackageTask.ERROR_PACKAGE_INVALID:
98                     return R.string.error_package_invalid;
99                 case InstallPackageTask.ERROR_INSTALLATION_FAILED:
100                     return R.string.error_installation_failed;
101             }
102         }
103 
104         return R.string.cant_set_up_device;
105     }
106 
107     @Override
getRequireFactoryReset(AbstractProvisioningTask task, int errorCode)108     protected boolean getRequireFactoryReset(AbstractProvisioningTask task, int errorCode) {
109         return !(task instanceof AddWifiNetworkTask);
110     }
111 
112     @Override
performCleanup()113     protected void performCleanup() {
114         // Do nothing, because a factory reset will be triggered.
115     }
116 }
117