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