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 23 import androidx.annotation.VisibleForTesting; 24 25 import com.android.car.settings.R; 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 UsersBasePreferenceController { 35 36 private final ConfirmationDialogFragment.ConfirmListener mConfirmListener = arguments -> { 37 UserInfo userToMakeAdmin = (UserInfo) arguments.get( 38 UsersDialogProvider.KEY_USER_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 getPreferenceProvider().setIncludeCurrentUser(false); 49 getPreferenceProvider().setIncludeGuest(false); 50 } 51 52 /** Setter for the user info of the admin we're deleting. */ setAdminInfo(UserInfo adminInfo)53 public void setAdminInfo(UserInfo adminInfo) { 54 mAdminInfo = adminInfo; 55 } 56 57 @Override checkInitialized()58 protected void checkInitialized() { 59 if (mAdminInfo == null) { 60 throw new IllegalStateException("Admin info should be set by this point"); 61 } 62 } 63 64 @Override onCreateInternal()65 protected void onCreateInternal() { 66 super.onCreateInternal(); 67 ConfirmationDialogFragment dialogFragment = 68 (ConfirmationDialogFragment) getFragmentController().findDialogByTag( 69 ConfirmationDialogFragment.TAG); 70 71 ConfirmationDialogFragment.resetListeners( 72 dialogFragment, 73 mConfirmListener, 74 /* rejectListener= */ null, 75 /* neutralListener= */ null); 76 } 77 78 @Override userClicked(UserInfo userToMakeAdmin)79 protected void userClicked(UserInfo userToMakeAdmin) { 80 ConfirmationDialogFragment dialogFragment = 81 UsersDialogProvider.getConfirmGrantAdminDialogFragment(getContext(), 82 mConfirmListener, /* rejectListener= */ null, userToMakeAdmin); 83 getFragmentController().showDialog(dialogFragment, ConfirmationDialogFragment.TAG); 84 } 85 86 @VisibleForTesting assignNewAdminAndRemoveOldAdmin(UserInfo userToMakeAdmin)87 void assignNewAdminAndRemoveOldAdmin(UserInfo userToMakeAdmin) { 88 getCarUserManagerHelper().grantAdminPermissions(userToMakeAdmin); 89 removeOldAdmin(); 90 } 91 removeOldAdmin()92 private void removeOldAdmin() { 93 Context context = getContext(); 94 if (!UserHelper.getInstance(context).removeUser(context, mAdminInfo)) { 95 // If failed, need to show error dialog for users. 96 getFragmentController().showDialog( 97 ErrorDialog.newInstance(R.string.delete_user_error_title), /* tag= */ null); 98 } 99 } 100 } 101