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