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.car.userlib.CarUserManagerHelper; 21 import android.content.Context; 22 import android.content.pm.UserInfo; 23 24 import androidx.annotation.CallSuper; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceGroup; 27 28 import com.android.car.settings.common.FragmentController; 29 import com.android.car.settings.common.PreferenceController; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.Objects; 34 35 /** Shared business logic between {@link UsersListFragment} and {@link ChooseNewAdminFragment}. */ 36 public abstract class UsersBasePreferenceController extends PreferenceController<PreferenceGroup> { 37 38 /** Update screen when users list is updated. */ 39 private final CarUserManagerHelper.OnUsersUpdateListener mOnUsersUpdateListener = 40 this::refreshUi; 41 42 private UsersPreferenceProvider mPreferenceProvider; 43 private CarUserManagerHelper mCarUserManagerHelper; 44 private List<Preference> mUsersToDisplay = new ArrayList<>(); 45 UsersBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46 public UsersBasePreferenceController(Context context, String preferenceKey, 47 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 48 super(context, preferenceKey, fragmentController, uxRestrictions); 49 mCarUserManagerHelper = new CarUserManagerHelper(context); 50 UsersPreferenceProvider.UserClickListener userClickListener = this::userClicked; 51 mPreferenceProvider = new UsersPreferenceProvider(context, mCarUserManagerHelper, 52 userClickListener); 53 } 54 55 @Override getPreferenceType()56 protected Class<PreferenceGroup> getPreferenceType() { 57 return PreferenceGroup.class; 58 } 59 60 /** 61 * Ensure that helper is set by the time onCreate is called. Register a listener to refresh 62 * screen on updates. 63 */ 64 @Override 65 @CallSuper onCreateInternal()66 protected void onCreateInternal() { 67 mCarUserManagerHelper.registerOnUsersUpdateListener(mOnUsersUpdateListener); 68 } 69 70 /** Unregister listener to refresh screen on updates. */ 71 @Override 72 @CallSuper onDestroyInternal()73 protected void onDestroyInternal() { 74 mCarUserManagerHelper.unregisterOnUsersUpdateListener(mOnUsersUpdateListener); 75 } 76 77 @Override updateState(PreferenceGroup preferenceGroup)78 protected void updateState(PreferenceGroup preferenceGroup) { 79 List<Preference> newUsers = mPreferenceProvider.createUserList(); 80 if (userListsAreDifferent(mUsersToDisplay, newUsers)) { 81 mUsersToDisplay = newUsers; 82 preferenceGroup.removeAll(); 83 84 for (Preference preference : mUsersToDisplay) { 85 preferenceGroup.addPreference(preference); 86 } 87 } 88 } 89 90 /** Gets the car user manager helper. */ getCarUserManagerHelper()91 protected CarUserManagerHelper getCarUserManagerHelper() { 92 return mCarUserManagerHelper; 93 } 94 95 /** Handles the user click on a preference for a certain user. */ userClicked(UserInfo userInfo)96 protected abstract void userClicked(UserInfo userInfo); 97 98 99 /** Gets the preference provider to set additional arguments if necessary. */ getPreferenceProvider()100 protected UsersPreferenceProvider getPreferenceProvider() { 101 return mPreferenceProvider; 102 } 103 userListsAreDifferent(List<Preference> currentList, List<Preference> newList)104 private boolean userListsAreDifferent(List<Preference> currentList, 105 List<Preference> newList) { 106 if (currentList.size() != newList.size()) { 107 return true; 108 } 109 110 for (int i = 0; i < currentList.size(); i++) { 111 // Cannot use "compareTo" on preference, since it uses the order attribute to compare. 112 if (preferencesAreDifferent(currentList.get(i), newList.get(i))) { 113 return true; 114 } 115 } 116 117 return false; 118 } 119 preferencesAreDifferent(Preference lhs, Preference rhs)120 private boolean preferencesAreDifferent(Preference lhs, Preference rhs) { 121 return !Objects.equals(lhs.getTitle(), rhs.getTitle()) 122 || !Objects.equals(lhs.getSummary(), rhs.getSummary()); 123 } 124 } 125