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 ConfirmationDialogFragment dialog = 58 (ConfirmationDialogFragment) getFragmentController().findDialogByTag( 59 ConfirmationDialogFragment.TAG); 60 61 ConfirmationDialogFragment.resetListeners(dialog, mConfirmListener, /* rejectListener= */ 62 null); 63 } 64 65 @Override updateState(ButtonPreference preference)66 protected void updateState(ButtonPreference preference) { 67 preference.setOnButtonClickListener(pref -> { 68 69 ConfirmationDialogFragment dialogFragment = 70 UsersDialogProvider.getConfirmGrantAdminDialogFragment(getContext(), 71 mConfirmListener, /* rejectListener= */ null, getUserInfo()); 72 73 getFragmentController().showDialog(dialogFragment, ConfirmationDialogFragment.TAG); 74 }); 75 76 Drawable icon = new UserIconProvider(getCarUserManagerHelper()).getUserIcon(getUserInfo(), 77 getContext()); 78 preference.setIcon(icon); 79 } 80 } 81