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.content.Context; 20 import android.content.pm.UserInfo; 21 22 import androidx.preference.Preference; 23 24 import com.android.car.settings.R; 25 import com.android.car.ui.preference.CarUiPreference; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Constructs the preferences to be displayed in {@link ProfilesListFragment} and 32 * {@link ChooseNewAdminFragment}. 33 */ 34 public class ProfilesPreferenceProvider { 35 36 /** 37 * Interface for registering clicks on profiles. 38 */ 39 public interface ProfileClickListener { 40 /** 41 * Invoked when profile is clicked. 42 * 43 * @param userInfo User for which the click is registered. 44 */ onProfileClicked(UserInfo userInfo)45 void onProfileClicked(UserInfo userInfo); 46 } 47 48 private final Context mContext; 49 private final ProfileClickListener mProfilePreferenceClickListener; 50 private boolean mIncludeCurrentProfile; 51 private boolean mIncludeGuest; 52 ProfilesPreferenceProvider(Context context, ProfileClickListener listener)53 public ProfilesPreferenceProvider(Context context, ProfileClickListener listener) { 54 mContext = context; 55 mProfilePreferenceClickListener = listener; 56 mIncludeCurrentProfile = false; 57 mIncludeGuest = false; 58 } 59 60 /** 61 * Sets whether createProfileList should include an entry for the current profile. Default is 62 * {@code true}. 63 */ setIncludeCurrentProfile(boolean includeCurrentProfile)64 public void setIncludeCurrentProfile(boolean includeCurrentProfile) { 65 mIncludeCurrentProfile = includeCurrentProfile; 66 } 67 68 /** 69 * Sets whether createProfileList should include an entry for the guest profile. Default is 70 * {@code true}. 71 */ setIncludeGuest(boolean includeGuest)72 public void setIncludeGuest(boolean includeGuest) { 73 mIncludeGuest = includeGuest; 74 } 75 76 /** 77 * Creates the list of profiles (as preferences). The first profile will be the current profile 78 * (if requested) and the last profile will be the guest profile (if requested). Otherwise, the 79 * list is populated with all of the remaining switchable profiles. 80 */ createProfileList()81 public List<Preference> createProfileList() { 82 List<Preference> profiles = new ArrayList<>(); 83 UserInfo currUserInfo = ProfileHelper.getInstance(mContext).getCurrentProcessUserInfo(); 84 85 // Show current profile at the top of the list. 86 if (mIncludeCurrentProfile) { 87 profiles.add(createProfilePreference(currUserInfo)); 88 } 89 90 // Display all profiles on the system, except: Guests and current profile who's displayed 91 // already. 92 List<UserInfo> infos = ProfileHelper.getInstance(mContext).getAllLivingProfiles( 93 userInfo -> !userInfo.isGuest() && userInfo.id != currUserInfo.id); 94 for (UserInfo userInfo : infos) { 95 profiles.add(createProfilePreference(userInfo)); 96 } 97 98 // Display guest session option. 99 if (mIncludeGuest) { 100 profiles.add(createGuestProfilePreference()); 101 } 102 103 return profiles; 104 } 105 createProfilePreference(UserInfo userInfo)106 private Preference createProfilePreference(UserInfo userInfo) { 107 CarUiPreference preference = new CarUiPreference(mContext); 108 preference.setIcon( 109 new ProfileIconProvider().getRoundedProfileIcon(userInfo, mContext)); 110 preference.setTitle(ProfileUtils.getProfileDisplayName(mContext, userInfo)); 111 112 if (!userInfo.isInitialized()) { 113 preference.setSummary(R.string.user_summary_not_set_up); 114 } 115 if (userInfo.isAdmin()) { 116 preference.setSummary(isCurrentProfile(userInfo) 117 ? R.string.signed_in_admin_user : R.string.user_admin); 118 } 119 120 preference.setOnPreferenceClickListener(pref -> { 121 if (mProfilePreferenceClickListener == null) { 122 return false; 123 } 124 mProfilePreferenceClickListener.onProfileClicked(userInfo); 125 return true; 126 }); 127 128 return preference; 129 } 130 createGuestProfilePreference()131 private Preference createGuestProfilePreference() { 132 CarUiPreference preference = new CarUiPreference(mContext); 133 preference.setIcon( 134 new ProfileIconProvider().getRoundedGuestDefaultIcon(mContext.getResources())); 135 preference.setTitle(R.string.user_guest); 136 return preference; 137 } 138 isCurrentProfile(UserInfo userInfo)139 private boolean isCurrentProfile(UserInfo userInfo) { 140 return ProfileHelper.getInstance(mContext).isCurrentProcessUser(userInfo); 141 } 142 } 143