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 
21 import com.android.managedprovisioning.R;
22 import com.android.managedprovisioning.model.ProvisioningParams;
23 import com.android.managedprovisioning.task.AbstractProvisioningTask;
24 import com.android.managedprovisioning.task.CreateAndProvisionManagedProfileTask;
25 
26 /**
27  * Controller for Profile Owner provisioning.
28  */
29 public class ProfileOwnerProvisioningController extends AbstractProvisioningController {
30     /**
31      * Instantiates a new {@link ProfileOwnerProvisioningController} instance and creates the
32      * relevant tasks.
33      *
34      * @return the newly created instance
35      */
createInstance( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback)36     static ProfileOwnerProvisioningController createInstance(
37             Context context,
38             ProvisioningParams params,
39             int userId,
40             ProvisioningControllerCallback callback) {
41         ProfileOwnerProvisioningController controller =
42                 new ProfileOwnerProvisioningController(context, params, userId, callback);
43         controller.setUpTasks();
44         return controller;
45     }
46 
ProfileOwnerProvisioningController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback)47     private ProfileOwnerProvisioningController(
48             Context context,
49             ProvisioningParams params,
50             int userId,
51             ProvisioningControllerCallback callback) {
52         super(context, params, userId, callback);
53     }
54 
setUpTasks()55     protected void setUpTasks() {
56         addTasks(new CreateAndProvisionManagedProfileTask(mContext, mParams, this));
57     }
58 
59     @Override
onSuccess(AbstractProvisioningTask task)60     public synchronized void onSuccess(AbstractProvisioningTask task) {
61         if (task instanceof CreateAndProvisionManagedProfileTask) {
62             // If the task was creating a managed profile, store the profile id
63             mUserId = ((CreateAndProvisionManagedProfileTask) task).getProfileUserId();
64         }
65         super.onSuccess(task);
66     }
67 
getErrorTitle()68     @Override protected int getErrorTitle() {
69         return R.string.cant_set_up_profile;
70     }
71 
72     @Override
getErrorMsgId(AbstractProvisioningTask task, int errorCode)73     protected String getErrorMsgId(AbstractProvisioningTask task, int errorCode) {
74         return mContext.getString(R.string.managed_provisioning_error_text);
75     }
76 
77     @Override
getRequireFactoryReset(AbstractProvisioningTask task, int errorCode)78     protected boolean getRequireFactoryReset(AbstractProvisioningTask task, int errorCode) {
79         if (mParams.isOrganizationOwnedProvisioning) {
80             // Do factory reset if any error occurs during PO provisioning in the admin
81             // integrated flow.
82             return true;
83         } else {
84             return false;
85         }
86     }
87 }
88