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 com.android.internal.util.Preconditions.checkNotNull;
20 
21 import android.accounts.Account;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.os.UserHandle;
25 
26 /**
27  * A helper class for the provisioning operation in primary profile after PO provisioning is done,
28  * including removing the migrated account from primary profile, and sending
29  * ACTION_MANAGED_PROFILE_PROVISIONED broadcast to the DPC in the primary profile.
30  */
31 class PrimaryProfileFinalizationHelper {
32 
33     private final Account mMigratedAccount;
34     private final String mMdmPackageName;
35     private final UserHandle mManagedUserHandle;
36 
PrimaryProfileFinalizationHelper(Account migratedAccount, UserHandle managedUserHandle, String mdmPackageName)37     PrimaryProfileFinalizationHelper(Account migratedAccount, UserHandle managedUserHandle,
38             String mdmPackageName) {
39         mMigratedAccount = migratedAccount;
40         mMdmPackageName = checkNotNull(mdmPackageName);
41         mManagedUserHandle = checkNotNull(managedUserHandle);
42     }
43 
finalizeProvisioningInPrimaryProfile(Context context, DpcReceivedSuccessReceiver.Callback callback)44     void finalizeProvisioningInPrimaryProfile(Context context,
45             DpcReceivedSuccessReceiver.Callback callback) {
46         DevicePolicyManager devicePolicyManager = (DevicePolicyManager)
47                 context.getSystemService(Context.DEVICE_POLICY_SERVICE);
48         devicePolicyManager.finalizeWorkProfileProvisioning(
49                 mManagedUserHandle, mMigratedAccount);
50 
51         if (callback != null) {
52             callback.cleanup();
53         }
54     }
55 }
56