1 /*
2  * Copyright (C) 2021 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.provisioninglisteners;
18 
19 import static android.content.Intent.ACTION_USER_REMOVED;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.managedprovisioning.common.ManagedProfileChecker;
27 import com.android.managedprovisioning.common.UserProvisioningStateHelperProvider;
28 import com.android.managedprovisioning.finalization.UserProvisioningStateHelper;
29 
30 /**
31  * This receiver is invoked after a managed user is removed.
32  */
33 public class ManagedUserRemovalListener extends BroadcastReceiver {
34     private final ManagedProfileChecker mManagedProfileChecker;
35     private final UserProvisioningStateHelperProvider mUserProvisioningStateHelperProvider;
36     private final ManagedUserRemovalUtils mManagedUserRemovalUtils;
37 
ManagedUserRemovalListener()38     public ManagedUserRemovalListener() {
39         this(
40                 ManagedProfileChecker.DEFAULT,
41                 UserProvisioningStateHelperProvider.DEFAULT,
42                 new ManagedUserRemovalUtils());
43     }
44 
45     @VisibleForTesting
ManagedUserRemovalListener( ManagedProfileChecker managedProfileChecker, UserProvisioningStateHelperProvider userProvisioningStateHelperProvider, ManagedUserRemovalUtils managedUserRemovalUtils)46     public ManagedUserRemovalListener(
47             ManagedProfileChecker managedProfileChecker,
48             UserProvisioningStateHelperProvider userProvisioningStateHelperProvider,
49             ManagedUserRemovalUtils managedUserRemovalUtils) {
50         mManagedProfileChecker = managedProfileChecker;
51         mUserProvisioningStateHelperProvider = userProvisioningStateHelperProvider;
52         mManagedUserRemovalUtils = managedUserRemovalUtils;
53     }
54 
55     @Override
onReceive(Context context, Intent intent)56     public void onReceive(Context context, Intent intent) {
57         if (!ACTION_USER_REMOVED.equals(intent.getAction())) {
58             return;
59         }
60         if (mManagedProfileChecker.hasManagedProfile(context)) {
61             return;
62         }
63         UserProvisioningStateHelper userProvisioningStateHelper =
64                 mUserProvisioningStateHelperProvider.createUserProvisioningStateHelper(context);
65         mManagedUserRemovalUtils
66                 .resetPrimaryUserProvisioningStateIfNecessary(context, userProvisioningStateHelper);
67     }
68 }
69