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.users;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.graphics.drawable.Drawable;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.car.settings.common.ButtonPreference;
27 import com.android.car.settings.common.ConfirmationDialogFragment;
28 import com.android.car.settings.common.FragmentController;
29 
30 /** Business Logic for preference which promotes a regular user to an admin user. */
31 public class MakeAdminPreferenceController extends
32         UserDetailsBasePreferenceController<ButtonPreference> {
33 
34     @VisibleForTesting
35     final ConfirmationDialogFragment.ConfirmListener mConfirmListener =
36             arguments -> {
37                 UserInfo userToMakeAdmin = (UserInfo) arguments.get(
38                         UsersDialogProvider.KEY_USER_TO_MAKE_ADMIN);
39                 getCarUserManagerHelper().grantAdminPermissions(userToMakeAdmin);
40                 getFragmentController().goBack();
41             };
42 
MakeAdminPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public MakeAdminPreferenceController(Context context, String preferenceKey,
44             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
45         super(context, preferenceKey, fragmentController, uxRestrictions);
46     }
47 
48     @Override
getPreferenceType()49     protected Class<ButtonPreference> getPreferenceType() {
50         return ButtonPreference.class;
51     }
52 
53 
54     /** Ensure that the listener is reset if the dialog was open during a configuration change. */
55     @Override
onCreateInternal()56     protected void onCreateInternal() {
57         super.onCreateInternal();
58         ConfirmationDialogFragment dialog =
59                 (ConfirmationDialogFragment) getFragmentController().findDialogByTag(
60                         ConfirmationDialogFragment.TAG);
61 
62         ConfirmationDialogFragment.resetListeners(
63                 dialog,
64                 mConfirmListener,
65                 /* rejectListener= */ null,
66                 /* neutralListener= */ null);
67     }
68 
69     @Override
updateState(ButtonPreference preference)70     protected void updateState(ButtonPreference preference) {
71         preference.setOnButtonClickListener(pref -> {
72 
73             ConfirmationDialogFragment dialogFragment =
74                     UsersDialogProvider.getConfirmGrantAdminDialogFragment(getContext(),
75                             mConfirmListener, /* rejectListener= */ null, getUserInfo());
76 
77             getFragmentController().showDialog(dialogFragment, ConfirmationDialogFragment.TAG);
78         });
79 
80         Drawable icon = new UserIconProvider().getRoundedUserIcon(getUserInfo(), getContext());
81         preference.setIcon(icon);
82     }
83 }
84