blob: 257fa7508125ff8d94b2d685c7ebfc164e0bf19d [file] [log] [blame]
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -07001/*
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
17package com.android.systemui.statusbar.car;
18
Aarthi Balachandera7096002018-05-21 18:12:25 -070019import static android.content.DialogInterface.BUTTON_NEGATIVE;
Aarthi Balachander0e0e9212018-04-19 19:25:33 -070020import static android.content.DialogInterface.BUTTON_POSITIVE;
21
22import android.app.AlertDialog;
23import android.app.AlertDialog.Builder;
24import android.app.Dialog;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070025import android.content.Context;
Aarthi Balachander0e0e9212018-04-19 19:25:33 -070026import android.content.DialogInterface;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070027import android.content.pm.UserInfo;
28import android.content.res.Resources;
jovanake4ce3cc2018-04-19 12:17:12 -070029import android.graphics.Bitmap;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070030import android.os.AsyncTask;
jovanake4ce3cc2018-04-19 12:17:12 -070031import android.os.UserHandle;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070032import android.util.AttributeSet;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.view.ViewGroup;
36import android.widget.ImageView;
37import android.widget.TextView;
38
Aarthi Balachander608b6e32018-04-11 18:41:52 -070039import androidx.car.widget.PagedListView;
Aarthi Balachandere3110e42018-04-30 18:16:26 -070040import androidx.core.graphics.drawable.RoundedBitmapDrawable;
41import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
Aarthi Balachandera7096002018-05-21 18:12:25 -070042import androidx.recyclerview.widget.RecyclerView;
43
jovanake4ce3cc2018-04-19 12:17:12 -070044import com.android.internal.util.UserIcons;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070045import com.android.settingslib.users.UserManagerHelper;
46import com.android.systemui.R;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070047
Aarthi Balachander0e0e9212018-04-19 19:25:33 -070048import com.android.systemui.statusbar.phone.SystemUIDialog;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070049import java.util.ArrayList;
50import java.util.List;
51
52/**
53 * Displays a GridLayout with icons for the users in the system to allow switching between users.
54 * One of the uses of this is for the lock screen in auto.
55 */
Aarthi Balachander608b6e32018-04-11 18:41:52 -070056public class UserGridRecyclerView extends PagedListView implements
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070057 UserManagerHelper.OnUsersUpdateListener {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070058 private UserSelectionListener mUserSelectionListener;
59 private UserAdapter mAdapter;
60 private UserManagerHelper mUserManagerHelper;
61 private Context mContext;
62
63 public UserGridRecyclerView(Context context, AttributeSet attrs) {
64 super(context, attrs);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070065 mContext = context;
66 mUserManagerHelper = new UserManagerHelper(mContext);
67 }
68
69 /**
70 * Register listener for any update to the users
71 */
72 @Override
73 public void onFinishInflate() {
Aarthi Balachander608b6e32018-04-11 18:41:52 -070074 super.onFinishInflate();
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070075 mUserManagerHelper.registerOnUsersUpdateListener(this);
76 }
77
78 /**
79 * Unregisters listener checking for any change to the users
80 */
81 @Override
82 public void onDetachedFromWindow() {
Aarthi Balachander608b6e32018-04-11 18:41:52 -070083 super.onDetachedFromWindow();
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070084 mUserManagerHelper.unregisterOnUsersUpdateListener();
85 }
86
87 /**
88 * Initializes the adapter that populates the grid layout
89 *
90 * @return the adapter
91 */
92 public void buildAdapter() {
93 List<UserRecord> userRecords = createUserRecords(mUserManagerHelper
94 .getAllUsers());
95 mAdapter = new UserAdapter(mContext, userRecords);
96 super.setAdapter(mAdapter);
97 }
98
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070099 private List<UserRecord> createUserRecords(List<UserInfo> userInfoList) {
100 List<UserRecord> userRecords = new ArrayList<>();
101 for (UserInfo userInfo : userInfoList) {
jovanak80b48592018-04-11 17:09:53 -0700102 if (userInfo.isGuest()) {
103 // Don't display guests in the switcher.
104 continue;
105 }
jovanak0535abc2018-04-10 15:14:50 -0700106 boolean isForeground = mUserManagerHelper.getForegroundUserId() == userInfo.id;
jovanak80b48592018-04-11 17:09:53 -0700107 UserRecord record = new UserRecord(userInfo, false /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700108 false /* isAddUser */, isForeground);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700109 userRecords.add(record);
110 }
111
jovanak0535abc2018-04-10 15:14:50 -0700112 // Add guest user record if the foreground user is not a guest
jovanak82029ae2018-04-02 16:40:15 -0700113 if (!mUserManagerHelper.foregroundUserIsGuestUser()) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700114 userRecords.add(addGuestUserRecord());
115 }
116
jovanak0535abc2018-04-10 15:14:50 -0700117 // Add add user record if the foreground user can add users
jovanak82029ae2018-04-02 16:40:15 -0700118 if (mUserManagerHelper.foregroundUserCanAddUsers()) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700119 userRecords.add(addUserRecord());
120 }
121
122 return userRecords;
123 }
124
125 /**
126 * Create guest user record
127 */
128 private UserRecord addGuestUserRecord() {
129 UserInfo userInfo = new UserInfo();
130 userInfo.name = mContext.getString(R.string.car_guest);
jovanak80b48592018-04-11 17:09:53 -0700131 return new UserRecord(userInfo, true /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700132 false /* isAddUser */, false /* isForeground */);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700133 }
134
135 /**
136 * Create add user record
137 */
138 private UserRecord addUserRecord() {
139 UserInfo userInfo = new UserInfo();
140 userInfo.name = mContext.getString(R.string.car_add_user);
jovanak80b48592018-04-11 17:09:53 -0700141 return new UserRecord(userInfo, false /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700142 true /* isAddUser */, false /* isForeground */);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700143 }
144
145 public void setUserSelectionListener(UserSelectionListener userSelectionListener) {
146 mUserSelectionListener = userSelectionListener;
147 }
148
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700149 @Override
150 public void onUsersUpdate() {
151 mAdapter.clearUsers();
152 mAdapter.updateUsers(createUserRecords(mUserManagerHelper.getAllUsers()));
153 mAdapter.notifyDataSetChanged();
154 }
155
156 /**
157 * Adapter to populate the grid layout with the available user profiles
158 */
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700159 public final class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserAdapterViewHolder>
160 implements Dialog.OnClickListener {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700161
162 private final Context mContext;
163 private List<UserRecord> mUsers;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700164 private final Resources mRes;
165 private final String mGuestName;
166 private final String mNewUserName;
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700167 private AlertDialog mDialog;
168 // View that holds the add user button. Used to enable/disable the view
169 private View mAddUserView;
Aarthi Balachandera7096002018-05-21 18:12:25 -0700170 // User record for the add user. Need to call notifyUserSelected only if the user
171 // confirms adding a user
172 private UserRecord mAddUserRecord;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700173
174 public UserAdapter(Context context, List<UserRecord> users) {
175 mRes = context.getResources();
176 mContext = context;
177 updateUsers(users);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700178 mGuestName = mRes.getString(R.string.car_guest);
179 mNewUserName = mRes.getString(R.string.car_new_user);
180 }
181
182 public void clearUsers() {
183 mUsers.clear();
184 }
185
186 public void updateUsers(List<UserRecord> users) {
187 mUsers = users;
188 }
189
190 @Override
191 public UserAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
192 View view = LayoutInflater.from(mContext)
193 .inflate(R.layout.car_fullscreen_user_pod, parent, false);
194 view.setAlpha(1f);
195 view.bringToFront();
196 return new UserAdapterViewHolder(view);
197 }
198
199 @Override
200 public void onBindViewHolder(UserAdapterViewHolder holder, int position) {
201 UserRecord userRecord = mUsers.get(position);
Aarthi Balachandere3110e42018-04-30 18:16:26 -0700202 RoundedBitmapDrawable circleIcon = RoundedBitmapDrawableFactory.create(mRes,
203 getUserRecordIcon(userRecord));
204 circleIcon.setCircular(true);
205 holder.mUserAvatarImageView.setImageDrawable(circleIcon);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700206 holder.mUserNameTextView.setText(userRecord.mInfo.name);
Aarthi Balachandera7096002018-05-21 18:12:25 -0700207
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700208 holder.mView.setOnClickListener(v -> {
209 if (userRecord == null) {
210 return;
211 }
212
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700213
jovanak80b48592018-04-11 17:09:53 -0700214 // If the user selects Guest, start the guest session.
215 if (userRecord.mIsStartGuestSession) {
Aarthi Balachandera7096002018-05-21 18:12:25 -0700216 notifyUserSelected(userRecord);
jovanak6639c4d2018-04-25 14:45:18 -0700217 mUserManagerHelper.startNewGuestSession(mGuestName);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700218 return;
219 }
220
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700221 // If the user wants to add a user, show dialog to confirm adding a user
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700222 if (userRecord.mIsAddUser) {
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700223 // Disable button so it cannot be clicked multiple times
224 mAddUserView = holder.mView;
225 mAddUserView.setEnabled(false);
226
227 String message = mRes.getString(R.string.user_add_user_message_setup)
228 .concat(System.getProperty("line.separator"))
229 .concat(System.getProperty("line.separator"))
230 .concat(mRes.getString(R.string.user_add_user_message_update));
231
Aarthi Balachandera7096002018-05-21 18:12:25 -0700232 mAddUserRecord = userRecord;
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700233 mDialog = new Builder(mContext, R.style.Theme_Car_Dark_Dialog_Alert)
234 .setTitle(R.string.user_add_user_title)
235 .setMessage(message)
236 .setNegativeButton(android.R.string.cancel, this)
237 .setPositiveButton(android.R.string.ok, this)
238 .create();
239 // Sets window flags for the SysUI dialog
240 SystemUIDialog.applyFlags(mDialog);
241 mDialog.show();
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700242 return;
243 }
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700244 // If the user doesn't want to be a guest or add a user, switch to the user selected
Aarthi Balachandera7096002018-05-21 18:12:25 -0700245 notifyUserSelected(userRecord);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700246 mUserManagerHelper.switchToUser(userRecord.mInfo);
247 });
248
249 }
250
Aarthi Balachandera7096002018-05-21 18:12:25 -0700251 private void notifyUserSelected(UserRecord userRecord) {
252 // Notify the listener which user was selected
253 if (mUserSelectionListener != null) {
254 mUserSelectionListener.onUserSelected(userRecord);
255 }
256 }
257
jovanake4ce3cc2018-04-19 12:17:12 -0700258 private Bitmap getUserRecordIcon(UserRecord userRecord) {
259 if (userRecord.mIsStartGuestSession) {
jovanak6639c4d2018-04-25 14:45:18 -0700260 return mUserManagerHelper.getGuestDefaultIcon();
jovanake4ce3cc2018-04-19 12:17:12 -0700261 }
262
263 if (userRecord.mIsAddUser) {
264 return UserIcons.convertToBitmap(mContext
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700265 .getDrawable(R.drawable.car_add_circle_round));
jovanake4ce3cc2018-04-19 12:17:12 -0700266 }
267
268 return mUserManagerHelper.getUserIcon(userRecord.mInfo);
269 }
270
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700271 @Override
272 public void onClick(DialogInterface dialog, int which) {
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700273 if (which == BUTTON_POSITIVE) {
Aarthi Balachandera7096002018-05-21 18:12:25 -0700274 notifyUserSelected(mAddUserRecord);
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700275 new AddNewUserTask().execute(mNewUserName);
Aarthi Balachandera7096002018-05-21 18:12:25 -0700276 } else if (which == BUTTON_NEGATIVE) {
277 // Enable the add button only if cancel
278 if (mAddUserView != null) {
279 mAddUserView.setEnabled(true);
280 }
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700281 }
282 }
283
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700284 private class AddNewUserTask extends AsyncTask<String, Void, UserInfo> {
285
286 @Override
287 protected UserInfo doInBackground(String... userNames) {
288 return mUserManagerHelper.createNewUser(userNames[0]);
289 }
290
291 @Override
292 protected void onPreExecute() {
293 }
294
295 @Override
296 protected void onPostExecute(UserInfo user) {
297 if (user != null) {
298 mUserManagerHelper.switchToUser(user);
299 }
300 }
301 }
302
303 @Override
304 public int getItemCount() {
305 return mUsers.size();
306 }
307
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700308 public class UserAdapterViewHolder extends RecyclerView.ViewHolder {
309
310 public ImageView mUserAvatarImageView;
311 public TextView mUserNameTextView;
312 public View mView;
313
314 public UserAdapterViewHolder(View view) {
315 super(view);
316 mView = view;
317 mUserAvatarImageView = (ImageView) view.findViewById(R.id.user_avatar);
318 mUserNameTextView = (TextView) view.findViewById(R.id.user_name);
319 }
320 }
321 }
322
323 /**
324 * Object wrapper class for the userInfo. Use it to distinguish if a profile is a
jovanak0535abc2018-04-10 15:14:50 -0700325 * guest profile, add user profile, or the foreground user.
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700326 */
327 public static final class UserRecord {
328
329 public final UserInfo mInfo;
jovanak80b48592018-04-11 17:09:53 -0700330 public final boolean mIsStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700331 public final boolean mIsAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700332 public final boolean mIsForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700333
jovanak80b48592018-04-11 17:09:53 -0700334 public UserRecord(UserInfo userInfo, boolean isStartGuestSession, boolean isAddUser,
jovanak0535abc2018-04-10 15:14:50 -0700335 boolean isForeground) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700336 mInfo = userInfo;
jovanak80b48592018-04-11 17:09:53 -0700337 mIsStartGuestSession = isStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700338 mIsAddUser = isAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700339 mIsForeground = isForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700340 }
341 }
342
343 /**
344 * Listener used to notify when a user has been selected
345 */
346 interface UserSelectionListener {
347
348 void onUserSelected(UserRecord record);
349 }
350}