blob: b376c007bbe4380ba8f23d39a3ea439216cac10f [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 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
jovanak80b48592018-04-11 17:09:53 -0700213 if (userRecord.mIsStartGuestSession) {
Aarthi Balachandera7096002018-05-21 18:12:25 -0700214 notifyUserSelected(userRecord);
jovanak78cacc42018-08-06 18:38:03 -0700215 mCarUserManagerHelper.startNewGuestSession(mGuestName);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700216 return;
217 }
218
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700219 // If the user wants to add a user, show dialog to confirm adding a user
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700220 if (userRecord.mIsAddUser) {
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700221 // Disable button so it cannot be clicked multiple times
222 mAddUserView = holder.mView;
223 mAddUserView.setEnabled(false);
224
225 String message = mRes.getString(R.string.user_add_user_message_setup)
226 .concat(System.getProperty("line.separator"))
227 .concat(System.getProperty("line.separator"))
228 .concat(mRes.getString(R.string.user_add_user_message_update));
229
Aarthi Balachandera7096002018-05-21 18:12:25 -0700230 mAddUserRecord = userRecord;
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700231 mDialog = new Builder(mContext, R.style.Theme_Car_Dark_Dialog_Alert)
232 .setTitle(R.string.user_add_user_title)
233 .setMessage(message)
234 .setNegativeButton(android.R.string.cancel, this)
235 .setPositiveButton(android.R.string.ok, this)
Aarthi Balachanderc6d13662018-08-13 14:49:41 -0700236 .setOnCancelListener(this)
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700237 .create();
238 // Sets window flags for the SysUI dialog
239 SystemUIDialog.applyFlags(mDialog);
240 mDialog.show();
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700241 return;
242 }
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700243 // 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 -0700244 notifyUserSelected(userRecord);
jovanak78cacc42018-08-06 18:38:03 -0700245 mCarUserManagerHelper.switchToUser(userRecord.mInfo);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700246 });
247
248 }
249
Aarthi Balachandera7096002018-05-21 18:12:25 -0700250 private void notifyUserSelected(UserRecord userRecord) {
251 // Notify the listener which user was selected
252 if (mUserSelectionListener != null) {
253 mUserSelectionListener.onUserSelected(userRecord);
254 }
255 }
256
jovanake4ce3cc2018-04-19 12:17:12 -0700257 private Bitmap getUserRecordIcon(UserRecord userRecord) {
258 if (userRecord.mIsStartGuestSession) {
jovanak78cacc42018-08-06 18:38:03 -0700259 return mCarUserManagerHelper.getGuestDefaultIcon();
jovanake4ce3cc2018-04-19 12:17:12 -0700260 }
261
262 if (userRecord.mIsAddUser) {
263 return UserIcons.convertToBitmap(mContext
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700264 .getDrawable(R.drawable.car_add_circle_round));
jovanake4ce3cc2018-04-19 12:17:12 -0700265 }
266
jovanak78cacc42018-08-06 18:38:03 -0700267 return mCarUserManagerHelper.getUserIcon(userRecord.mInfo);
jovanake4ce3cc2018-04-19 12:17:12 -0700268 }
269
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700270 @Override
271 public void onClick(DialogInterface dialog, int which) {
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700272 if (which == BUTTON_POSITIVE) {
Aarthi Balachandera7096002018-05-21 18:12:25 -0700273 notifyUserSelected(mAddUserRecord);
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700274 new AddNewUserTask().execute(mNewUserName);
Aarthi Balachandera7096002018-05-21 18:12:25 -0700275 } else if (which == BUTTON_NEGATIVE) {
276 // Enable the add button only if cancel
277 if (mAddUserView != null) {
278 mAddUserView.setEnabled(true);
279 }
Aarthi Balachander0e0e9212018-04-19 19:25:33 -0700280 }
281 }
282
Aarthi Balachanderc6d13662018-08-13 14:49:41 -0700283 @Override
284 public void onCancel(DialogInterface dialog) {
285 // Enable the add button again if user cancels dialog by clicking outside the dialog
286 if (mAddUserView != null) {
287 mAddUserView.setEnabled(true);
288 }
289 }
290
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700291 private class AddNewUserTask extends AsyncTask<String, Void, UserInfo> {
292
293 @Override
294 protected UserInfo doInBackground(String... userNames) {
jovanak78cacc42018-08-06 18:38:03 -0700295 return mCarUserManagerHelper.createNewNonAdminUser(userNames[0]);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700296 }
297
298 @Override
299 protected void onPreExecute() {
300 }
301
302 @Override
303 protected void onPostExecute(UserInfo user) {
304 if (user != null) {
jovanak78cacc42018-08-06 18:38:03 -0700305 mCarUserManagerHelper.switchToUser(user);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700306 }
307 }
308 }
309
310 @Override
311 public int getItemCount() {
312 return mUsers.size();
313 }
314
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700315 public class UserAdapterViewHolder extends RecyclerView.ViewHolder {
316
317 public ImageView mUserAvatarImageView;
318 public TextView mUserNameTextView;
319 public View mView;
320
321 public UserAdapterViewHolder(View view) {
322 super(view);
323 mView = view;
324 mUserAvatarImageView = (ImageView) view.findViewById(R.id.user_avatar);
325 mUserNameTextView = (TextView) view.findViewById(R.id.user_name);
326 }
327 }
328 }
329
330 /**
331 * Object wrapper class for the userInfo. Use it to distinguish if a profile is a
jovanak0535abc2018-04-10 15:14:50 -0700332 * guest profile, add user profile, or the foreground user.
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700333 */
334 public static final class UserRecord {
335
336 public final UserInfo mInfo;
jovanak80b48592018-04-11 17:09:53 -0700337 public final boolean mIsStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700338 public final boolean mIsAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700339 public final boolean mIsForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700340
jovanak80b48592018-04-11 17:09:53 -0700341 public UserRecord(UserInfo userInfo, boolean isStartGuestSession, boolean isAddUser,
jovanak0535abc2018-04-10 15:14:50 -0700342 boolean isForeground) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700343 mInfo = userInfo;
jovanak80b48592018-04-11 17:09:53 -0700344 mIsStartGuestSession = isStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700345 mIsAddUser = isAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700346 mIsForeground = isForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700347 }
348 }
349
350 /**
351 * Listener used to notify when a user has been selected
352 */
353 interface UserSelectionListener {
354
355 void onUserSelected(UserRecord record);
356 }
357}