1 /*
2  * Copyright (C) 2018 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.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.car.internal.user.UserHelper;
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  * Business logic for when the last admin is about to be removed from the device and a new
32  * administrator needs to be chosen.
33  */
34 public class ChooseNewAdminPreferenceController extends ProfilesBasePreferenceController {
35 
36     private final ConfirmationDialogFragment.ConfirmListener mConfirmListener = arguments -> {
37         UserInfo userToMakeAdmin = (UserInfo) arguments.get(
38                 ProfilesDialogProvider.KEY_PROFILE_TO_MAKE_ADMIN);
39         assignNewAdminAndRemoveOldAdmin(userToMakeAdmin);
40         getFragmentController().goBack();
41     };
42 
43     private UserInfo mAdminInfo;
44 
ChooseNewAdminPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)45     public ChooseNewAdminPreferenceController(Context context, String preferenceKey,
46             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
47         super(context, preferenceKey, fragmentController, uxRestrictions);
48     }
49 
50     /** Setter for the profile info of the admin we're deleting. */
setAdminInfo(UserInfo adminInfo)51     public void setAdminInfo(UserInfo adminInfo) {
52         mAdminInfo = adminInfo;
53     }
54 
55     @Override
checkInitialized()56     protected void checkInitialized() {
57         if (mAdminInfo == null) {
58             throw new IllegalStateException("Admin info should be set by this point");
59         }
60     }
61 
62     @Override
onCreateInternal()63     protected void onCreateInternal() {
64         super.onCreateInternal();
65         ConfirmationDialogFragment dialogFragment =
66                 (ConfirmationDialogFragment) getFragmentController().findDialogByTag(
67                         ConfirmationDialogFragment.TAG);
68 
69         ConfirmationDialogFragment.resetListeners(
70                 dialogFragment,
71                 mConfirmListener,
72                 /* rejectListener= */ null,
73                 /* neutralListener= */ null);
74     }
75 
76     @Override
profileClicked(UserInfo profileToMakeAdmin)77     protected void profileClicked(UserInfo profileToMakeAdmin) {
78         ConfirmationDialogFragment dialogFragment =
79                 ProfilesDialogProvider.getConfirmGrantAdminDialogFragment(getContext(),
80                         mConfirmListener, /* rejectListener= */ null, profileToMakeAdmin);
81         getFragmentController().showDialog(dialogFragment, ConfirmationDialogFragment.TAG);
82     }
83 
84     @VisibleForTesting
assignNewAdminAndRemoveOldAdmin(UserInfo profileToMakeAdmin)85     void assignNewAdminAndRemoveOldAdmin(UserInfo profileToMakeAdmin) {
86         UserHelper.grantAdminPermissions(getContext(), profileToMakeAdmin.getUserHandle());
87         removeOldAdmin();
88     }
89 
removeOldAdmin()90     private void removeOldAdmin() {
91         Context context = getContext();
92         ProfileHelper profileHelper = ProfileHelper.getInstance(context);
93         int removeUserResult = profileHelper.removeProfile(context, mAdminInfo);
94         if (removeUserResult != ProfileHelper.REMOVE_PROFILE_RESULT_SUCCESS) {
95             // If failed, need to show error dialog for users.
96             getFragmentController().showDialog(
97                     ErrorDialog.newInstance(
98                             profileHelper.getErrorMessageForProfileResult(removeUserResult)),
99                     /* tag= */ null);
100         }
101     }
102 }
103