• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.userlib.CarUserManagerHelper;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.car.settings.R;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Constructs the preferences to be displayed in {@link UsersListFragment} and
32  * {@link ChooseNewAdminFragment}.
33  */
34 public class UsersPreferenceProvider {
35 
36     /**
37      * Interface for registering clicks on users.
38      */
39     public interface UserClickListener {
40         /**
41          * Invoked when user is clicked.
42          *
43          * @param userInfo User for which the click is registered.
44          */
onUserClicked(UserInfo userInfo)45         void onUserClicked(UserInfo userInfo);
46     }
47 
48     private final Context mContext;
49     private final CarUserManagerHelper mCarUserManagerHelper;
50     private final UserClickListener mUserPreferenceClickListener;
51     private boolean mIncludeCurrentUser;
52     private boolean mIncludeGuest;
53 
UsersPreferenceProvider(Context context, CarUserManagerHelper carUserManagerHelper, UserClickListener listener)54     public UsersPreferenceProvider(Context context, CarUserManagerHelper carUserManagerHelper,
55             UserClickListener listener) {
56         mContext = context;
57         mCarUserManagerHelper = carUserManagerHelper;
58         mUserPreferenceClickListener = listener;
59         mIncludeCurrentUser = true;
60         mIncludeGuest = true;
61     }
62 
63     /**
64      * Sets whether createUserList should include an entry for the current user. Default is
65      * {@code true}.
66      */
setIncludeCurrentUser(boolean includeCurrentUser)67     public void setIncludeCurrentUser(boolean includeCurrentUser) {
68         mIncludeCurrentUser = includeCurrentUser;
69     }
70 
71     /**
72      * Sets whether createUserList should include an entry for the guest profile. Default is
73      * {@code true}.
74      */
setIncludeGuest(boolean includeGuest)75     public void setIncludeGuest(boolean includeGuest) {
76         mIncludeGuest = includeGuest;
77     }
78 
79     /**
80      * Creates the list of users (as preferences). The first user will be the current user (if
81      * requested) and the last user will be the guest user (if requested). Otherwise, the list is
82      * populated with all of the remaining switchable users.
83      */
createUserList()84     public List<Preference> createUserList() {
85         List<Preference> users = new ArrayList<>();
86         UserInfo currUserInfo = mCarUserManagerHelper.getCurrentProcessUserInfo();
87 
88         // Show current user
89         if (mIncludeCurrentUser) {
90             users.add(createUserPreference(currUserInfo));
91         }
92 
93         // Display other users on the system
94         List<UserInfo> infos = mCarUserManagerHelper.getAllSwitchableUsers();
95         for (UserInfo userInfo : infos) {
96             if (!userInfo.isGuest()) { // Do not show guest users.
97                 users.add(createUserPreference(userInfo));
98             }
99         }
100 
101         // Display guest session option.
102         if (mIncludeGuest) {
103             users.add(createGuestUserPreference());
104         }
105 
106         return users;
107     }
108 
createUserPreference(UserInfo userInfo)109     private Preference createUserPreference(UserInfo userInfo) {
110         Preference preference = new Preference(mContext);
111         preference.setIcon(
112                 new UserIconProvider(mCarUserManagerHelper).getUserIcon(userInfo, mContext));
113         preference.setTitle(
114                 UserUtils.getUserDisplayName(mContext, mCarUserManagerHelper, userInfo));
115 
116         if (!userInfo.isInitialized()) {
117             preference.setSummary(R.string.user_summary_not_set_up);
118         }
119         if (userInfo.isAdmin()) {
120             preference.setSummary(
121                     isCurrentUser(userInfo) ? R.string.signed_in_admin_user : R.string.user_admin);
122         }
123 
124         preference.setOnPreferenceClickListener(pref -> {
125             if (mUserPreferenceClickListener == null) {
126                 return false;
127             }
128             mUserPreferenceClickListener.onUserClicked(userInfo);
129             return true;
130         });
131 
132         return preference;
133     }
134 
createGuestUserPreference()135     private Preference createGuestUserPreference() {
136         Preference preference = new Preference(mContext);
137         preference.setIcon(
138                 new UserIconProvider(mCarUserManagerHelper).getDefaultGuestIcon(mContext));
139         preference.setTitle(R.string.user_guest);
140         return preference;
141     }
142 
isCurrentUser(UserInfo userInfo)143     private boolean isCurrentUser(UserInfo userInfo) {
144         return mCarUserManagerHelper.isCurrentProcessUser(userInfo);
145     }
146 }
147