blob: da6401c2d62a407cdb67f9aefe897cec5b6ca665 [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;
jovanak78cacc42018-08-06 18:38:03 -070025import android.car.user.CarUserManagerHelper;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070026import android.content.Context;
Aarthi Balachander0e0e9212018-04-19 19:25:33 -070027import android.content.DialogInterface;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070028import android.content.pm.UserInfo;
29import android.content.res.Resources;
jovanake4ce3cc2018-04-19 12:17:12 -070030import android.graphics.Bitmap;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070031import android.os.AsyncTask;
jovanake4ce3cc2018-04-19 12:17:12 -070032import android.os.UserHandle;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070033import android.util.AttributeSet;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.view.ViewGroup;
37import android.widget.ImageView;
38import android.widget.TextView;
39
Aarthi Balachander608b6e32018-04-11 18:41:52 -070040import androidx.car.widget.PagedListView;
Aarthi Balachandere3110e42018-04-30 18:16:26 -070041import androidx.core.graphics.drawable.RoundedBitmapDrawable;
42import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
Aarthi Balachandera7096002018-05-21 18:12:25 -070043import androidx.recyclerview.widget.RecyclerView;
44
jovanake4ce3cc2018-04-19 12:17:12 -070045import com.android.internal.util.UserIcons;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070046import 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
jovanak78cacc42018-08-06 18:38:03 -070057 CarUserManagerHelper.OnUsersUpdateListener {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070058 private UserSelectionListener mUserSelectionListener;
59 private UserAdapter mAdapter;
jovanak78cacc42018-08-06 18:38:03 -070060 private CarUserManagerHelper mCarUserManagerHelper;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070061 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;
jovanak78cacc42018-08-06 18:38:03 -070066 mCarUserManagerHelper = new CarUserManagerHelper(mContext);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070067 }
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();
jovanak78cacc42018-08-06 18:38:03 -070075 mCarUserManagerHelper.registerOnUsersUpdateListener(this);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070076 }
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();
jovanak78cacc42018-08-06 18:38:03 -070084 mCarUserManagerHelper.unregisterOnUsersUpdateListener(this);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070085 }
86
87 /**
88 * Initializes the adapter that populates the grid layout
89 *
90 * @return the adapter
91 */
92 public void buildAdapter() {
jovanak78cacc42018-08-06 18:38:03 -070093 List<UserRecord> userRecords = createUserRecords(mCarUserManagerHelper
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070094 .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 }
jovanak78cacc42018-08-06 18:38:03 -0700106
107 boolean isForeground =
108 mCarUserManagerHelper.getCurrentForegroundUserId() == userInfo.id;
jovanak80b48592018-04-11 17:09:53 -0700109 UserRecord record = new UserRecord(userInfo, false /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700110 false /* isAddUser */, isForeground);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700111 userRecords.add(record);
112 }
113
jovanak2d26ae32018-07-31 10:44:41 -0700114 // Add button for starting guest session.
115 userRecords.add(createStartGuestUserRecord());
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700116
jovanak0535abc2018-04-10 15:14:50 -0700117 // Add add user record if the foreground user can add users
jovanak78cacc42018-08-06 18:38:03 -0700118 if (mCarUserManagerHelper.canForegroundUserAddUsers()) {
jovanak2d26ae32018-07-31 10:44:41 -0700119 userRecords.add(createAddUserRecord());
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700120 }
121
122 return userRecords;
123 }
124
125 /**
126 * Create guest user record
127 */
jovanak2d26ae32018-07-31 10:44:41 -0700128 private UserRecord createStartGuestUserRecord() {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700129 UserInfo userInfo = new UserInfo();
jovanak2d26ae32018-07-31 10:44:41 -0700130 userInfo.name = mContext.getString(R.string.start_guest_session);
131 return new UserRecord(userInfo, true /* isStartGuestSession */, false /* isAddUser */,
132 false /* isForeground */);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700133 }
134
135 /**
136 * Create add user record
137 */
jovanak2d26ae32018-07-31 10:44:41 -0700138 private UserRecord createAddUserRecord() {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700139 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();
jovanak78cacc42018-08-06 18:38:03 -0700152 mAdapter.updateUsers(createUserRecords(mCarUserManagerHelper.getAllUsers()));
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700153 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>
Aarthi Balachanderc6d13662018-08-13 14:49:41 -0700160 implements Dialog.OnClickListener, Dialog.OnCancelListener {
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 // View that holds the add user button. Used to enable/disable the view
168 private View mAddUserView;
Aarthi Balachandera7096002018-05-21 18:12:25 -0700169 // User record for the add user. Need to call notifyUserSelected only if the user
170 // confirms adding a user
171 private UserRecord mAddUserRecord;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700172
173 public UserAdapter(Context context, List<UserRecord> users) {
174 mRes = context.getResources();
175 mContext = context;
176 updateUsers(users);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700177 mGuestName = mRes.getString(R.string.car_guest);
178 mNewUserName = mRes.getString(R.string.car_new_user);
179 }
180
181 public void clearUsers() {
182 mUsers.clear();
183 }
184
185 public void updateUsers(List<UserRecord> users) {
186 mUsers = users;
187 }
188
189 @Override
190 public UserAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
191 View view = LayoutInflater.from(mContext)
192 .inflate(R.layout.car_fullscreen_user_pod, parent, false);
193 view.setAlpha(1f);
194 view.bringToFront();
195 return new UserAdapterViewHolder(view);
196 }
197
198 @Override
199 public void onBindViewHolder(UserAdapterViewHolder holder, int position) {
200 UserRecord userRecord = mUsers.get(position);
Aarthi Balachandere3110e42018-04-30 18:16:26 -0700201 RoundedBitmapDrawable circleIcon = RoundedBitmapDrawableFactory.create(mRes,
202 getUserRecordIcon(userRecord));
203 circleIcon.setCircular(true);
204 holder.mUserAvatarImageView.setImageDrawable(circleIcon);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700205 holder.mUserNameTextView.setText(userRecord.mInfo.name);
Aarthi Balachandera7096002018-05-21 18:12:25 -0700206
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700207 holder.mView.setOnClickListener(v -> {
208 if (userRecord == null) {
209 return;
210 }
211
jovanak80b48592018-04-11 17:09:53 -0700212 if (userRecord.mIsStartGuestSession) {
Aarthi Balachandera7096002018-05-21 18:12:25 -0700213 notifyUserSelected(userRecord);
jovanak78cacc42018-08-06 18:38:03 -0700214 mCarUserManagerHelper.startNewGuestSession(mGuestName);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700215 return;
216 }
217
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700218 // If the user wants to add a user, show dialog to confirm adding a user
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700219 if (userRecord.mIsAddUser) {
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700220 // Disable button so it cannot be clicked multiple times
221 mAddUserView = holder.mView;
222 mAddUserView.setEnabled(false);
Aarthi Balachandera7096002018-05-21 18:12:25 -0700223 mAddUserRecord = userRecord;
jovanaka17e1682018-08-09 15:02:13 -0700224
225 handleAddUserClicked();
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700226 return;
227 }
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700228 // 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 -0700229 notifyUserSelected(userRecord);
jovanak78cacc42018-08-06 18:38:03 -0700230 mCarUserManagerHelper.switchToUser(userRecord.mInfo);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700231 });
232
233 }
234
jovanaka17e1682018-08-09 15:02:13 -0700235 private void handleAddUserClicked() {
236 if (mCarUserManagerHelper.isUserLimitReached()) {
237 mAddUserView.setEnabled(true);
238 showMaxUserLimitReachedDialog();
239 } else {
240 showConfirmAddUserDialog();
241 }
242 }
243
244 private void showMaxUserLimitReachedDialog() {
245 AlertDialog maxUsersDialog = new Builder(mContext, R.style.Theme_Car_Dark_Dialog_Alert)
246 .setTitle(R.string.user_limit_reached_title)
247 .setMessage(getResources().getQuantityString(
248 R.plurals.user_limit_reached_message,
249 mCarUserManagerHelper.getMaxSupportedRealUsers(),
250 mCarUserManagerHelper.getMaxSupportedRealUsers()))
251 .setPositiveButton(android.R.string.ok, null)
252 .create();
253 // Sets window flags for the SysUI dialog
254 SystemUIDialog.applyFlags(maxUsersDialog);
255 maxUsersDialog.show();
256 }
257
258 private void showConfirmAddUserDialog() {
259 String message = mRes.getString(R.string.user_add_user_message_setup)
260 .concat(System.getProperty("line.separator"))
261 .concat(System.getProperty("line.separator"))
262 .concat(mRes.getString(R.string.user_add_user_message_update));
263
264 AlertDialog addUserDialog = new Builder(mContext, R.style.Theme_Car_Dark_Dialog_Alert)
265 .setTitle(R.string.user_add_user_title)
266 .setMessage(message)
267 .setNegativeButton(android.R.string.cancel, this)
268 .setPositiveButton(android.R.string.ok, this)
269 .setOnCancelListener(this)
270 .create();
271 // Sets window flags for the SysUI dialog
272 SystemUIDialog.applyFlags(addUserDialog);
273 addUserDialog.show();
274 }
275
Aarthi Balachandera7096002018-05-21 18:12:25 -0700276 private void notifyUserSelected(UserRecord userRecord) {
277 // Notify the listener which user was selected
278 if (mUserSelectionListener != null) {
279 mUserSelectionListener.onUserSelected(userRecord);
280 }
281 }
282
jovanake4ce3cc2018-04-19 12:17:12 -0700283 private Bitmap getUserRecordIcon(UserRecord userRecord) {
284 if (userRecord.mIsStartGuestSession) {
jovanak78cacc42018-08-06 18:38:03 -0700285 return mCarUserManagerHelper.getGuestDefaultIcon();
jovanake4ce3cc2018-04-19 12:17:12 -0700286 }
287
288 if (userRecord.mIsAddUser) {
289 return UserIcons.convertToBitmap(mContext
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700290 .getDrawable(R.drawable.car_add_circle_round));
jovanake4ce3cc2018-04-19 12:17:12 -0700291 }
292
jovanak78cacc42018-08-06 18:38:03 -0700293 return mCarUserManagerHelper.getUserIcon(userRecord.mInfo);
jovanake4ce3cc2018-04-19 12:17:12 -0700294 }
295
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700296 @Override
297 public void onClick(DialogInterface dialog, int which) {
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700298 if (which == BUTTON_POSITIVE) {
Aarthi Balachandera7096002018-05-21 18:12:25 -0700299 notifyUserSelected(mAddUserRecord);
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700300 new AddNewUserTask().execute(mNewUserName);
Aarthi Balachandera7096002018-05-21 18:12:25 -0700301 } else if (which == BUTTON_NEGATIVE) {
302 // Enable the add button only if cancel
303 if (mAddUserView != null) {
304 mAddUserView.setEnabled(true);
305 }
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700306 }
307 }
308
Aarthi Balachanderc6d13662018-08-13 14:49:41 -0700309 @Override
310 public void onCancel(DialogInterface dialog) {
311 // Enable the add button again if user cancels dialog by clicking outside the dialog
312 if (mAddUserView != null) {
313 mAddUserView.setEnabled(true);
314 }
315 }
316
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700317 private class AddNewUserTask extends AsyncTask<String, Void, UserInfo> {
318
319 @Override
320 protected UserInfo doInBackground(String... userNames) {
jovanak78cacc42018-08-06 18:38:03 -0700321 return mCarUserManagerHelper.createNewNonAdminUser(userNames[0]);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700322 }
323
324 @Override
325 protected void onPreExecute() {
326 }
327
328 @Override
329 protected void onPostExecute(UserInfo user) {
330 if (user != null) {
jovanak78cacc42018-08-06 18:38:03 -0700331 mCarUserManagerHelper.switchToUser(user);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700332 }
333 }
334 }
335
336 @Override
337 public int getItemCount() {
338 return mUsers.size();
339 }
340
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700341 public class UserAdapterViewHolder extends RecyclerView.ViewHolder {
342
343 public ImageView mUserAvatarImageView;
344 public TextView mUserNameTextView;
345 public View mView;
346
347 public UserAdapterViewHolder(View view) {
348 super(view);
349 mView = view;
350 mUserAvatarImageView = (ImageView) view.findViewById(R.id.user_avatar);
351 mUserNameTextView = (TextView) view.findViewById(R.id.user_name);
352 }
353 }
354 }
355
356 /**
357 * Object wrapper class for the userInfo. Use it to distinguish if a profile is a
jovanak0535abc2018-04-10 15:14:50 -0700358 * guest profile, add user profile, or the foreground user.
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700359 */
360 public static final class UserRecord {
361
362 public final UserInfo mInfo;
jovanak80b48592018-04-11 17:09:53 -0700363 public final boolean mIsStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700364 public final boolean mIsAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700365 public final boolean mIsForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700366
jovanak80b48592018-04-11 17:09:53 -0700367 public UserRecord(UserInfo userInfo, boolean isStartGuestSession, boolean isAddUser,
jovanak0535abc2018-04-10 15:14:50 -0700368 boolean isForeground) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700369 mInfo = userInfo;
jovanak80b48592018-04-11 17:09:53 -0700370 mIsStartGuestSession = isStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700371 mIsAddUser = isAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700372 mIsForeground = isForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700373 }
374 }
375
376 /**
377 * Listener used to notify when a user has been selected
378 */
379 interface UserSelectionListener {
380
381 void onUserSelected(UserRecord record);
382 }
383}