blob: 0dc847a91fc0a111afa3eedbd946264456b25916 [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
19import android.app.ActivityManager;
20import android.content.Context;
21import android.content.pm.UserInfo;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Paint;
26import android.graphics.Paint.Align;
27import android.graphics.drawable.GradientDrawable;
28import android.os.AsyncTask;
29import android.support.annotation.Nullable;
30import android.support.v7.widget.RecyclerView;
31import android.support.v7.widget.RecyclerView.ViewHolder;
32import android.util.AttributeSet;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.view.ViewGroup;
36import android.widget.ImageView;
37import android.widget.TextView;
38
39import com.android.settingslib.users.UserManagerHelper;
40import com.android.systemui.R;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070041
42import java.util.ArrayList;
43import java.util.List;
44
45/**
46 * Displays a GridLayout with icons for the users in the system to allow switching between users.
47 * One of the uses of this is for the lock screen in auto.
48 */
49public class UserGridRecyclerView extends RecyclerView implements
50 UserManagerHelper.OnUsersUpdateListener {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070051 private UserSelectionListener mUserSelectionListener;
52 private UserAdapter mAdapter;
53 private UserManagerHelper mUserManagerHelper;
54 private Context mContext;
55
56 public UserGridRecyclerView(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 super.setHasFixedSize(true);
59 mContext = context;
60 mUserManagerHelper = new UserManagerHelper(mContext);
61 }
62
63 /**
64 * Register listener for any update to the users
65 */
66 @Override
67 public void onFinishInflate() {
68 mUserManagerHelper.registerOnUsersUpdateListener(this);
69 }
70
71 /**
72 * Unregisters listener checking for any change to the users
73 */
74 @Override
75 public void onDetachedFromWindow() {
76 mUserManagerHelper.unregisterOnUsersUpdateListener();
77 }
78
79 /**
80 * Initializes the adapter that populates the grid layout
81 *
82 * @return the adapter
83 */
84 public void buildAdapter() {
85 List<UserRecord> userRecords = createUserRecords(mUserManagerHelper
86 .getAllUsers());
87 mAdapter = new UserAdapter(mContext, userRecords);
88 super.setAdapter(mAdapter);
89 }
90
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070091 private List<UserRecord> createUserRecords(List<UserInfo> userInfoList) {
92 List<UserRecord> userRecords = new ArrayList<>();
93 for (UserInfo userInfo : userInfoList) {
jovanak80b48592018-04-11 17:09:53 -070094 if (userInfo.isGuest()) {
95 // Don't display guests in the switcher.
96 continue;
97 }
jovanak0535abc2018-04-10 15:14:50 -070098 boolean isForeground = mUserManagerHelper.getForegroundUserId() == userInfo.id;
jovanak80b48592018-04-11 17:09:53 -070099 UserRecord record = new UserRecord(userInfo, false /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700100 false /* isAddUser */, isForeground);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700101 userRecords.add(record);
102 }
103
jovanak0535abc2018-04-10 15:14:50 -0700104 // Add guest user record if the foreground user is not a guest
jovanak82029ae2018-04-02 16:40:15 -0700105 if (!mUserManagerHelper.foregroundUserIsGuestUser()) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700106 userRecords.add(addGuestUserRecord());
107 }
108
jovanak0535abc2018-04-10 15:14:50 -0700109 // Add add user record if the foreground user can add users
jovanak82029ae2018-04-02 16:40:15 -0700110 if (mUserManagerHelper.foregroundUserCanAddUsers()) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700111 userRecords.add(addUserRecord());
112 }
113
114 return userRecords;
115 }
116
117 /**
118 * Create guest user record
119 */
120 private UserRecord addGuestUserRecord() {
121 UserInfo userInfo = new UserInfo();
122 userInfo.name = mContext.getString(R.string.car_guest);
jovanak80b48592018-04-11 17:09:53 -0700123 return new UserRecord(userInfo, true /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700124 false /* isAddUser */, false /* isForeground */);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700125 }
126
127 /**
128 * Create add user record
129 */
130 private UserRecord addUserRecord() {
131 UserInfo userInfo = new UserInfo();
132 userInfo.name = mContext.getString(R.string.car_add_user);
jovanak80b48592018-04-11 17:09:53 -0700133 return new UserRecord(userInfo, false /* isStartGuestSession */,
jovanak0535abc2018-04-10 15:14:50 -0700134 true /* isAddUser */, false /* isForeground */);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700135 }
136
137 public void setUserSelectionListener(UserSelectionListener userSelectionListener) {
138 mUserSelectionListener = userSelectionListener;
139 }
140
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700141 @Override
142 public void onUsersUpdate() {
143 mAdapter.clearUsers();
144 mAdapter.updateUsers(createUserRecords(mUserManagerHelper.getAllUsers()));
145 mAdapter.notifyDataSetChanged();
146 }
147
148 /**
149 * Adapter to populate the grid layout with the available user profiles
150 */
151 public final class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserAdapterViewHolder> {
152
153 private final Context mContext;
154 private List<UserRecord> mUsers;
155 private final int mPodImageAvatarWidth;
156 private final int mPodImageAvatarHeight;
157 private final Resources mRes;
158 private final String mGuestName;
159 private final String mNewUserName;
160
161 public UserAdapter(Context context, List<UserRecord> users) {
162 mRes = context.getResources();
163 mContext = context;
164 updateUsers(users);
165 mPodImageAvatarWidth = mRes.getDimensionPixelSize(
166 R.dimen.car_fullscreen_user_pod_image_avatar_width);
167 mPodImageAvatarHeight = mRes.getDimensionPixelSize(
168 R.dimen.car_fullscreen_user_pod_image_avatar_height);
169 mGuestName = mRes.getString(R.string.car_guest);
170 mNewUserName = mRes.getString(R.string.car_new_user);
171 }
172
173 public void clearUsers() {
174 mUsers.clear();
175 }
176
177 public void updateUsers(List<UserRecord> users) {
178 mUsers = users;
179 }
180
181 @Override
182 public UserAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
183 View view = LayoutInflater.from(mContext)
184 .inflate(R.layout.car_fullscreen_user_pod, parent, false);
185 view.setAlpha(1f);
186 view.bringToFront();
187 return new UserAdapterViewHolder(view);
188 }
189
190 @Override
191 public void onBindViewHolder(UserAdapterViewHolder holder, int position) {
192 UserRecord userRecord = mUsers.get(position);
193 holder.mUserAvatarImageView.setImageBitmap(getDefaultUserIcon(userRecord));
194 holder.mUserNameTextView.setText(userRecord.mInfo.name);
195 holder.mView.setOnClickListener(v -> {
196 if (userRecord == null) {
197 return;
198 }
199
200 // Notify the listener which user was selected
201 if (mUserSelectionListener != null) {
202 mUserSelectionListener.onUserSelected(userRecord);
203 }
204
jovanak80b48592018-04-11 17:09:53 -0700205 // If the user selects Guest, start the guest session.
206 if (userRecord.mIsStartGuestSession) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700207 mUserManagerHelper.switchToGuest(mGuestName);
208 return;
209 }
210
211 // If the user wants to add a user, start task to add new user
212 if (userRecord.mIsAddUser) {
213 new AddNewUserTask().execute(mNewUserName);
214 return;
215 }
216
217 // If the user doesn't want to be a guest or add a user, switch to the user selected
218 mUserManagerHelper.switchToUser(userRecord.mInfo);
219 });
220
221 }
222
223 private class AddNewUserTask extends AsyncTask<String, Void, UserInfo> {
224
225 @Override
226 protected UserInfo doInBackground(String... userNames) {
227 return mUserManagerHelper.createNewUser(userNames[0]);
228 }
229
230 @Override
231 protected void onPreExecute() {
232 }
233
234 @Override
235 protected void onPostExecute(UserInfo user) {
236 if (user != null) {
237 mUserManagerHelper.switchToUser(user);
238 }
239 }
240 }
241
242 @Override
243 public int getItemCount() {
244 return mUsers.size();
245 }
246
247 /**
248 * Returns the default user icon. This icon is a circle with a letter in it. The letter is
249 * the first character in the username.
250 *
251 * @param record the profile of the user for which the icon should be created
252 */
253 private Bitmap getDefaultUserIcon(UserRecord record) {
254 CharSequence displayText;
255 boolean isAddUserText = false;
256 if (record.mIsAddUser) {
257 displayText = "+";
258 isAddUserText = true;
259 } else {
260 displayText = record.mInfo.name.subSequence(0, 1);
261 }
262 Bitmap out = Bitmap.createBitmap(mPodImageAvatarWidth, mPodImageAvatarHeight,
263 Bitmap.Config.ARGB_8888);
264 Canvas canvas = new Canvas(out);
265
266 // Draw the circle background.
267 GradientDrawable shape = new GradientDrawable();
268 shape.setShape(GradientDrawable.RADIAL_GRADIENT);
269 shape.setGradientRadius(1.0f);
270 shape.setColor(mContext.getColor(R.color.car_user_switcher_no_user_image_bgcolor));
271 shape.setBounds(0, 0, mPodImageAvatarWidth, mPodImageAvatarHeight);
272 shape.draw(canvas);
273
274 // Draw the letter in the center.
275 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
276 paint.setColor(mContext.getColor(R.color.car_user_switcher_no_user_image_fgcolor));
277 paint.setTextAlign(Align.CENTER);
278 if (isAddUserText) {
279 paint.setTextSize(mRes.getDimensionPixelSize(
280 R.dimen.car_touch_target_size));
281 } else {
282 paint.setTextSize(mRes.getDimensionPixelSize(
283 R.dimen.car_fullscreen_user_pod_icon_text_size));
284 }
285
286 Paint.FontMetricsInt metrics = paint.getFontMetricsInt();
287 // The Y coordinate is measured by taking half the height of the pod, but that would
288 // draw the character putting the bottom of the font in the middle of the pod. To
289 // correct this, half the difference between the top and bottom distance metrics of the
290 // font gives the offset of the font. Bottom is a positive value, top is negative, so
291 // the different is actually a sum. The "half" operation is then factored out.
292 canvas.drawText(displayText.toString(), mPodImageAvatarWidth / 2,
293 (mPodImageAvatarHeight - (metrics.bottom + metrics.top)) / 2, paint);
294
295 return out;
296 }
297
298 public class UserAdapterViewHolder extends RecyclerView.ViewHolder {
299
300 public ImageView mUserAvatarImageView;
301 public TextView mUserNameTextView;
302 public View mView;
303
304 public UserAdapterViewHolder(View view) {
305 super(view);
306 mView = view;
307 mUserAvatarImageView = (ImageView) view.findViewById(R.id.user_avatar);
308 mUserNameTextView = (TextView) view.findViewById(R.id.user_name);
309 }
310 }
311 }
312
313 /**
314 * Object wrapper class for the userInfo. Use it to distinguish if a profile is a
jovanak0535abc2018-04-10 15:14:50 -0700315 * guest profile, add user profile, or the foreground user.
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700316 */
317 public static final class UserRecord {
318
319 public final UserInfo mInfo;
jovanak80b48592018-04-11 17:09:53 -0700320 public final boolean mIsStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700321 public final boolean mIsAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700322 public final boolean mIsForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700323
jovanak80b48592018-04-11 17:09:53 -0700324 public UserRecord(UserInfo userInfo, boolean isStartGuestSession, boolean isAddUser,
jovanak0535abc2018-04-10 15:14:50 -0700325 boolean isForeground) {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700326 mInfo = userInfo;
jovanak80b48592018-04-11 17:09:53 -0700327 mIsStartGuestSession = isStartGuestSession;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700328 mIsAddUser = isAddUser;
jovanak0535abc2018-04-10 15:14:50 -0700329 mIsForeground = isForeground;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -0700330 }
331 }
332
333 /**
334 * Listener used to notify when a user has been selected
335 */
336 interface UserSelectionListener {
337
338 void onUserSelected(UserRecord record);
339 }
340}