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.finalization; 18 19 import static android.app.admin.DevicePolicyManager.ACTION_MANAGED_PROFILE_PROVISIONED; 20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE; 21 22 import static com.android.internal.util.Preconditions.checkNotNull; 23 24 import android.accounts.Account; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.UserHandle; 28 29 import com.android.managedprovisioning.common.Utils; 30 31 /** 32 * A helper class for the provisioning operation in primary profile after PO provisioning is done, 33 * including removing the migrated account from primary profile, and sending 34 * ACTION_MANAGED_PROFILE_PROVISIONED broadcast to the DPC in the primary profile. 35 */ 36 class PrimaryProfileFinalizationHelper { 37 38 private final Account mMigratedAccount; 39 private final String mMdmPackageName; 40 private final boolean mKeepAccountMigrated; 41 private final Utils mUtils; 42 private final UserHandle mManagedUserHandle; 43 private final boolean mIsAdminIntegratedFlow; 44 PrimaryProfileFinalizationHelper(Account migratedAccount, boolean keepAccountMigrated, UserHandle managedUserHandle, String mdmPackageName, Utils utils, boolean isAdminIntegratedFlow)45 PrimaryProfileFinalizationHelper(Account migratedAccount, boolean keepAccountMigrated, 46 UserHandle managedUserHandle, String mdmPackageName, Utils utils, 47 boolean isAdminIntegratedFlow) { 48 mMigratedAccount = migratedAccount; 49 mKeepAccountMigrated = keepAccountMigrated; 50 mMdmPackageName = checkNotNull(mdmPackageName); 51 mManagedUserHandle = checkNotNull(managedUserHandle); 52 mUtils = checkNotNull(utils); 53 mIsAdminIntegratedFlow = isAdminIntegratedFlow; 54 } 55 finalizeProvisioningInPrimaryProfile(Context context, DpcReceivedSuccessReceiver.Callback callback)56 void finalizeProvisioningInPrimaryProfile(Context context, 57 DpcReceivedSuccessReceiver.Callback callback) { 58 final Intent primaryProfileSuccessIntent = new Intent(ACTION_MANAGED_PROFILE_PROVISIONED); 59 primaryProfileSuccessIntent.setPackage(mMdmPackageName); 60 primaryProfileSuccessIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | 61 Intent.FLAG_RECEIVER_FOREGROUND); 62 primaryProfileSuccessIntent.putExtra(Intent.EXTRA_USER, mManagedUserHandle); 63 64 // Now cleanup the primary profile if necessary 65 if (mMigratedAccount != null) { 66 primaryProfileSuccessIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, 67 mMigratedAccount); 68 finishAccountMigration(context, primaryProfileSuccessIntent, callback); 69 // Note that we currently do not check if account migration worked 70 } else { 71 handleFinalization(context, callback, primaryProfileSuccessIntent); 72 } 73 } 74 handleFinalization(Context context, DpcReceivedSuccessReceiver.Callback callback, Intent primaryProfileSuccessIntent)75 private void handleFinalization(Context context, DpcReceivedSuccessReceiver.Callback callback, 76 Intent primaryProfileSuccessIntent) { 77 context.sendBroadcast(primaryProfileSuccessIntent); 78 if (callback != null) { 79 callback.cleanup(); 80 } 81 } 82 finishAccountMigration(final Context context, final Intent primaryProfileSuccessIntent, DpcReceivedSuccessReceiver.Callback callback)83 private void finishAccountMigration(final Context context, 84 final Intent primaryProfileSuccessIntent, 85 DpcReceivedSuccessReceiver.Callback callback) { 86 if (!mKeepAccountMigrated) { 87 mUtils.removeAccountAsync(context, mMigratedAccount, () -> { 88 handleFinalization(context, callback, primaryProfileSuccessIntent); 89 }); 90 } else { 91 handleFinalization(context, callback, primaryProfileSuccessIntent); 92 } 93 } 94 } 95