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 static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 20 21 import android.content.Context; 22 import android.os.UserManager; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 import com.android.managedprovisioning.R; 26 import com.android.managedprovisioning.common.ProvisionLogger; 27 import com.android.managedprovisioning.finalization.FinalizationController; 28 import com.android.managedprovisioning.model.ProvisioningParams; 29 import com.android.managedprovisioning.task.AbstractProvisioningTask; 30 import com.android.managedprovisioning.task.CopyAccountToUserTask; 31 import com.android.managedprovisioning.task.CreateManagedProfileTask; 32 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; 33 import com.android.managedprovisioning.task.DisableInstallShortcutListenersTask; 34 import com.android.managedprovisioning.task.InstallExistingPackageTask; 35 import com.android.managedprovisioning.task.ManagedProfileSettingsTask; 36 import com.android.managedprovisioning.task.SetDevicePolicyTask; 37 import com.android.managedprovisioning.task.StartManagedProfileTask; 38 39 /** 40 * Controller for Profile Owner provisioning. 41 */ 42 // TODO: Consider splitting this controller into one for managed profile and one for user owner 43 public class ProfileOwnerProvisioningController extends AbstractProvisioningController { 44 private final int mParentUserId; 45 ProfileOwnerProvisioningController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback)46 public ProfileOwnerProvisioningController( 47 Context context, 48 ProvisioningParams params, 49 int userId, 50 ProvisioningControllerCallback callback) { 51 this(context, params, userId, callback, new FinalizationController(context)); 52 } 53 54 @VisibleForTesting ProfileOwnerProvisioningController( Context context, ProvisioningParams params, int userId, ProvisioningControllerCallback callback, FinalizationController finalizationController)55 ProfileOwnerProvisioningController( 56 Context context, 57 ProvisioningParams params, 58 int userId, 59 ProvisioningControllerCallback callback, 60 FinalizationController finalizationController) { 61 super(context, params, userId, callback, finalizationController); 62 mParentUserId = userId; 63 } 64 setUpTasks()65 protected void setUpTasks() { 66 if (ACTION_PROVISION_MANAGED_PROFILE.equals(mParams.provisioningAction)) { 67 setUpTasksManagedProfile(); 68 } else { 69 setUpTasksManagedUser(); 70 } 71 } 72 setUpTasksManagedProfile()73 private void setUpTasksManagedProfile() { 74 addTasks( 75 new CreateManagedProfileTask(mContext, mParams, this), 76 new InstallExistingPackageTask(mParams.inferDeviceAdminPackageName(), mContext, 77 mParams, this), 78 new SetDevicePolicyTask(mContext, mParams, this), 79 new ManagedProfileSettingsTask(mContext, mParams, this), 80 new DisableInstallShortcutListenersTask(mContext, mParams, this), 81 new StartManagedProfileTask(mContext, mParams, this), 82 new CopyAccountToUserTask(mParentUserId, mContext, mParams, this)); 83 } 84 setUpTasksManagedUser()85 private void setUpTasksManagedUser() { 86 addTasks( 87 new DeleteNonRequiredAppsTask(true /* new profile */, mContext, mParams, this), 88 new InstallExistingPackageTask(mParams.inferDeviceAdminPackageName(), mContext, 89 mParams, this), 90 new SetDevicePolicyTask(mContext, mParams, this)); 91 } 92 93 @Override onSuccess(AbstractProvisioningTask task)94 public synchronized void onSuccess(AbstractProvisioningTask task) { 95 if (task instanceof CreateManagedProfileTask) { 96 // If the task was creating a managed profile, store the profile id 97 mUserId = ((CreateManagedProfileTask) task).getProfileUserId(); 98 } 99 super.onSuccess(task); 100 } 101 102 @Override performCleanup()103 protected void performCleanup() { 104 if (ACTION_PROVISION_MANAGED_PROFILE.equals(mParams.provisioningAction) 105 && mCurrentTaskIndex != 0) { 106 ProvisionLogger.logd("Removing managed profile"); 107 UserManager um = mContext.getSystemService(UserManager.class); 108 um.removeUserEvenWhenDisallowed(mUserId); 109 } 110 } 111 getErrorTitle()112 @Override protected int getErrorTitle() { 113 return R.string.cant_set_up_profile; 114 } 115 116 @Override getErrorMsgId(AbstractProvisioningTask task, int errorCode)117 protected int getErrorMsgId(AbstractProvisioningTask task, int errorCode) { 118 return R.string.managed_provisioning_error_text; 119 } 120 121 @Override getRequireFactoryReset(AbstractProvisioningTask task, int errorCode)122 protected boolean getRequireFactoryReset(AbstractProvisioningTask task, int errorCode) { 123 return false; 124 } 125 } 126