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.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 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 UserClickListener mUserPreferenceClickListener;
50     private boolean mIncludeCurrentUser;
51     private boolean mIncludeGuest;
52 
UsersPreferenceProvider(Context context, UserClickListener listener)53     public UsersPreferenceProvider(Context context, UserClickListener listener) {
54         mContext = context;
55         mUserPreferenceClickListener = listener;
56         mIncludeCurrentUser = true;
57         mIncludeGuest = true;
58     }
59 
60     /**
61      * Sets whether createUserList should include an entry for the current user. Default is
62      * {@code true}.
63      */
setIncludeCurrentUser(boolean includeCurrentUser)64     public void setIncludeCurrentUser(boolean includeCurrentUser) {
65         mIncludeCurrentUser = includeCurrentUser;
66     }
67 
68     /**
69      * Sets whether createUserList 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 users (as preferences). The first user will be the current user (if
78      * requested) and the last user will be the guest user (if requested). Otherwise, the list is
79      * populated with all of the remaining switchable users.
80      */
createUserList()81     public List<Preference> createUserList() {
82         List<Preference> users = new ArrayList<>();
83         UserInfo currUserInfo = UserHelper.getInstance(mContext).getCurrentProcessUserInfo();
84 
85         // Show current user at the top of the list.
86         if (mIncludeCurrentUser) {
87             users.add(createUserPreference(currUserInfo));
88         }
89 
90         // Display all users on the system, except: Guests and current user who's displayed already.
91         List<UserInfo> infos = UserHelper.getInstance(mContext).getAllLivingUsers(
92                 userInfo -> !userInfo.isGuest() && userInfo.id != currUserInfo.id);
93         for (UserInfo userInfo : infos) {
94             users.add(createUserPreference(userInfo));
95         }
96 
97         // Display guest session option.
98         if (mIncludeGuest) {
99             users.add(createGuestUserPreference());
100         }
101 
102         return users;
103     }
104 
createUserPreference(UserInfo userInfo)105     private Preference createUserPreference(UserInfo userInfo) {
106         CarUiPreference preference = new CarUiPreference(mContext);
107         preference.setIcon(
108                 new UserIconProvider().getRoundedUserIcon(userInfo, mContext));
109         preference.setTitle(UserUtils.getUserDisplayName(mContext, userInfo));
110 
111         if (!userInfo.isInitialized()) {
112             preference.setSummary(R.string.user_summary_not_set_up);
113         }
114         if (userInfo.isAdmin()) {
115             preference.setSummary(
116                     isCurrentUser(userInfo) ? R.string.signed_in_admin_user : R.string.user_admin);
117         }
118 
119         preference.setOnPreferenceClickListener(pref -> {
120             if (mUserPreferenceClickListener == null) {
121                 return false;
122             }
123             mUserPreferenceClickListener.onUserClicked(userInfo);
124             return true;
125         });
126 
127         return preference;
128     }
129 
createGuestUserPreference()130     private Preference createGuestUserPreference() {
131         CarUiPreference preference = new CarUiPreference(mContext);
132         preference.setIcon(
133                 new UserIconProvider().getRoundedGuestDefaultIcon(mContext.getResources()));
134         preference.setTitle(R.string.user_guest);
135         return preference;
136     }
137 
isCurrentUser(UserInfo userInfo)138     private boolean isCurrentUser(UserInfo userInfo) {
139         return UserHelper.getInstance(mContext).isCurrentProcessUser(userInfo);
140     }
141 }
142