1 /*
2  * Copyright (C) 2020 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 package com.android.managedprovisioning;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.managedprovisioning.common.CrossProfileAppsPregrantControllerProvider;
24 import com.android.managedprovisioning.common.EncryptionControllerProvider;
25 import com.android.managedprovisioning.common.ManagedProfileChecker;
26 import com.android.managedprovisioning.common.UserProvisioningStateHelperProvider;
27 import com.android.managedprovisioning.finalization.UserProvisioningStateHelper;
28 import com.android.managedprovisioning.provisioninglisteners.ManagedUserRemovalListener;
29 import com.android.managedprovisioning.provisioninglisteners.ManagedUserRemovalUtils;
30 
31 /**
32  * Boot listener for triggering reminders at boot time.
33  */
34 public class BootReminder extends BroadcastReceiver {
35     private final ManagedProfileChecker mManagedProfileChecker;
36     private final UserProvisioningStateHelperProvider mUserProvisioningStateHelperProvider;
37     private final EncryptionControllerProvider mEncryptionControllerProvider;
38     private final CrossProfileAppsPregrantControllerProvider
39             mCrossProfileAppsPregrantControllerProvider;
40     private final ManagedUserRemovalUtils mManagedUserRemovalUtils;
41 
BootReminder()42     public BootReminder() {
43         this(
44                 ManagedProfileChecker.DEFAULT,
45                 UserProvisioningStateHelperProvider.DEFAULT,
46                 EncryptionControllerProvider.DEFAULT,
47                 CrossProfileAppsPregrantControllerProvider.DEFAULT,
48                 new ManagedUserRemovalUtils());
49     }
50 
51     @VisibleForTesting
BootReminder( ManagedProfileChecker managedProfileChecker, UserProvisioningStateHelperProvider userProvisioningStateHelperProvider, EncryptionControllerProvider encryptionControllerProvider, CrossProfileAppsPregrantControllerProvider crossProfileAppsPregrantControllerProvider, ManagedUserRemovalUtils managedUserRemovalUtils)52     public BootReminder(
53             ManagedProfileChecker managedProfileChecker,
54             UserProvisioningStateHelperProvider userProvisioningStateHelperProvider,
55             EncryptionControllerProvider encryptionControllerProvider,
56             CrossProfileAppsPregrantControllerProvider crossProfileAppsPregrantControllerProvider,
57             ManagedUserRemovalUtils managedUserRemovalUtils) {
58         mManagedProfileChecker = managedProfileChecker;
59         mUserProvisioningStateHelperProvider = userProvisioningStateHelperProvider;
60         mEncryptionControllerProvider = encryptionControllerProvider;
61         mCrossProfileAppsPregrantControllerProvider = crossProfileAppsPregrantControllerProvider;
62         mManagedUserRemovalUtils = managedUserRemovalUtils;
63     }
64 
65     @Override
onReceive(Context context, Intent intent)66     public void onReceive(Context context, Intent intent) {
67         mCrossProfileAppsPregrantControllerProvider
68                 .createCrossProfileAppsPregrantController(context)
69                         .checkCrossProfileAppsPermissions();
70 
71         // For encryption flows during setup wizard, this acts as a backup to
72         // PostEncryptionActivity in case the PackageManager has not yet written the package state
73         // to disk when the reboot is triggered.
74         mEncryptionControllerProvider.createEncryptionController(context).resumeProvisioning();
75 
76         resetPrimaryUserProvisioningStateIfNecessary(context);
77     }
78 
79     /**
80      * Resets the primary user provisioning state if a work profile was removed, but the state
81      * hasn't been updated by {@link ManagedUserRemovalListener}.
82      *
83      * <p>This can happen if the device gets rebooted after removing the work profile, but before
84      * {@link ManagedUserRemovalListener} receives the {@link Intent#ACTION_USER_REMOVED}
85      * broadcast.
86      */
resetPrimaryUserProvisioningStateIfNecessary(Context context)87     private void resetPrimaryUserProvisioningStateIfNecessary(Context context) {
88         if (mManagedProfileChecker.hasManagedProfile(context)) {
89             return;
90         }
91         UserProvisioningStateHelper userProvisioningStateHelper =
92                 mUserProvisioningStateHelperProvider.createUserProvisioningStateHelper(context);
93         mManagedUserRemovalUtils
94                 .resetPrimaryUserProvisioningStateIfNecessary(context, userProvisioningStateHelper);
95     }
96 }
97 
98