• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.settings.profiles;
18 
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.car.settings.common.ConfirmationDialogFragment;
27 import com.android.car.settings.common.ErrorDialog;
28 import com.android.car.settings.common.FragmentController;
29 
30 /**
31  * Consolidates profile removal logic into one handler so we can have consistent logic across
32  * various parts of the Settings app.
33  */
34 public class RemoveProfileHandler {
35     @VisibleForTesting
36     static final String REMOVE_PROFILE_DIALOG_TAG = "RemoveProfileDialogFragment";
37 
38     private final Context mContext;
39     private final ProfileHelper mProfileHelper;
40     private final UserManager mUserManager;
41     private final FragmentController mFragmentController;
42 
43     private UserInfo mUserInfo;
44 
45     @VisibleForTesting
46     ConfirmationDialogFragment.ConfirmListener mRemoveConfirmListener;
47 
RemoveProfileHandler(Context context, ProfileHelper profileHelper, UserManager userManager, FragmentController fragmentController)48     public RemoveProfileHandler(Context context, ProfileHelper profileHelper,
49             UserManager userManager, FragmentController fragmentController) {
50         mContext = context;
51         mProfileHelper = profileHelper;
52         mUserManager = userManager;
53         mFragmentController = fragmentController;
54     }
55 
56     /**
57      * Sets the profile info to be handled for removal
58      * @param userInfo UserInfo of the profile to remove
59      */
setUserInfo(UserInfo userInfo)60     public void setUserInfo(UserInfo userInfo) {
61         mUserInfo = userInfo;
62 
63         mRemoveConfirmListener = arguments -> {
64             String profileType = arguments.getString(ProfilesDialogProvider.KEY_PROFILE_TYPE);
65             if (profileType.equals(ProfilesDialogProvider.LAST_ADMIN)) {
66                 mFragmentController.launchFragment(
67                         ChooseNewAdminFragment.newInstance(userInfo));
68             } else {
69                 int removeProfileResult = mProfileHelper.removeProfile(mContext, mUserInfo);
70                 if (removeProfileResult == ProfileHelper.REMOVE_PROFILE_RESULT_SUCCESS) {
71                     mFragmentController.goBack();
72                 } else {
73                     // If failed, need to show error dialog for users.
74                     mFragmentController.showDialog(
75                             ErrorDialog.newInstance(mProfileHelper.getErrorMessageForProfileResult(
76                                     removeProfileResult)), null);
77                 }
78             }
79         };
80     }
81 
82     /**
83      * Resets listeners as they can get unregistered with certain configuration changes.
84      */
resetListeners()85     public void resetListeners() {
86         ConfirmationDialogFragment removeProfileDialog =
87                 (ConfirmationDialogFragment) mFragmentController.findDialogByTag(
88                         REMOVE_PROFILE_DIALOG_TAG);
89 
90         ConfirmationDialogFragment.resetListeners(
91                 removeProfileDialog,
92                 mRemoveConfirmListener,
93                 /* rejectListener= */ null,
94                 /* neutralListener= */ null);
95     }
96 
97     /**
98      * Checks to see if the current active profile can delete the requested profile.
99      * @param userInfo UserInfo of the profile to delete
100      * @return True if the profile can be deleted by the current active profile. False otherwise.
101      */
canRemoveProfile(UserInfo userInfo)102     public boolean canRemoveProfile(UserInfo userInfo) {
103         return !mUserManager.hasUserRestriction(UserManager.DISALLOW_REMOVE_USER)
104                 && userInfo.id != UserHandle.USER_SYSTEM
105                 && !mUserManager.isDemoUser();
106     }
107 
108     /**
109      * Show the remove profile confirmation dialog. This will handle edge cases such as removing
110      * the last profile or removing the last admin profile.
111      */
showConfirmRemoveProfileDialog()112     public void showConfirmRemoveProfileDialog() {
113         boolean isLastProfile = mProfileHelper.getAllPersistentProfiles().size() == 1;
114         boolean isLastAdmin = mUserInfo.isAdmin()
115                 && mProfileHelper.getAllAdminProfiles().size() == 1;
116 
117         ConfirmationDialogFragment dialogFragment;
118 
119         if (isLastProfile) {
120             dialogFragment = ProfilesDialogProvider.getConfirmRemoveLastProfileDialogFragment(
121                     mContext, mRemoveConfirmListener, /* rejectListener= */ null);
122         } else if (isLastAdmin) {
123             dialogFragment = ProfilesDialogProvider.getConfirmRemoveLastAdminDialogFragment(
124                     mContext, mRemoveConfirmListener, /* rejectListener= */ null);
125         } else {
126             dialogFragment = ProfilesDialogProvider.getConfirmRemoveProfileDialogFragment(mContext,
127                     mRemoveConfirmListener, /* rejectListener= */ null);
128         }
129         mFragmentController.showDialog(dialogFragment, REMOVE_PROFILE_DIALOG_TAG);
130     }
131 
132 }
133