Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 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.systemui.statusbar.car; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.UserInfo; |
| 22 | import android.content.res.Resources; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.Paint.Align; |
| 27 | import android.graphics.drawable.GradientDrawable; |
| 28 | import android.os.AsyncTask; |
| 29 | import android.support.annotation.Nullable; |
| 30 | import android.support.v7.widget.RecyclerView; |
| 31 | import android.support.v7.widget.RecyclerView.ViewHolder; |
| 32 | import android.util.AttributeSet; |
| 33 | import android.view.LayoutInflater; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.widget.ImageView; |
| 37 | import android.widget.TextView; |
| 38 | |
| 39 | import com.android.settingslib.users.UserManagerHelper; |
| 40 | import com.android.systemui.R; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import 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 | */ |
| 49 | public class UserGridRecyclerView extends RecyclerView implements |
| 50 | UserManagerHelper.OnUsersUpdateListener { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 51 | 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 Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 91 | private List<UserRecord> createUserRecords(List<UserInfo> userInfoList) { |
| 92 | List<UserRecord> userRecords = new ArrayList<>(); |
| 93 | for (UserInfo userInfo : userInfoList) { |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 94 | if (userInfo.isGuest()) { |
| 95 | // Don't display guests in the switcher. |
| 96 | continue; |
| 97 | } |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 98 | boolean isForeground = mUserManagerHelper.getForegroundUserId() == userInfo.id; |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 99 | UserRecord record = new UserRecord(userInfo, false /* isStartGuestSession */, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 100 | false /* isAddUser */, isForeground); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 101 | userRecords.add(record); |
| 102 | } |
| 103 | |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 104 | // Add guest user record if the foreground user is not a guest |
jovanak | 82029ae | 2018-04-02 16:40:15 -0700 | [diff] [blame] | 105 | if (!mUserManagerHelper.foregroundUserIsGuestUser()) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 106 | userRecords.add(addGuestUserRecord()); |
| 107 | } |
| 108 | |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 109 | // Add add user record if the foreground user can add users |
jovanak | 82029ae | 2018-04-02 16:40:15 -0700 | [diff] [blame] | 110 | if (mUserManagerHelper.foregroundUserCanAddUsers()) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 111 | 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); |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 123 | return new UserRecord(userInfo, true /* isStartGuestSession */, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 124 | false /* isAddUser */, false /* isForeground */); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 125 | } |
| 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); |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 133 | return new UserRecord(userInfo, false /* isStartGuestSession */, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 134 | true /* isAddUser */, false /* isForeground */); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void setUserSelectionListener(UserSelectionListener userSelectionListener) { |
| 138 | mUserSelectionListener = userSelectionListener; |
| 139 | } |
| 140 | |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 141 | @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 | |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 205 | // If the user selects Guest, start the guest session. |
| 206 | if (userRecord.mIsStartGuestSession) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 207 | 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); |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 270 | shape.setColor(mContext.getColor(R.color.car_grey_50)); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 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); |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 276 | paint.setColor(mContext.getColor(R.color.car_grey_900)); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 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 |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 315 | * guest profile, add user profile, or the foreground user. |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 316 | */ |
| 317 | public static final class UserRecord { |
| 318 | |
| 319 | public final UserInfo mInfo; |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 320 | public final boolean mIsStartGuestSession; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 321 | public final boolean mIsAddUser; |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 322 | public final boolean mIsForeground; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 323 | |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 324 | public UserRecord(UserInfo userInfo, boolean isStartGuestSession, boolean isAddUser, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 325 | boolean isForeground) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 326 | mInfo = userInfo; |
jovanak | 80b4859 | 2018-04-11 17:09:53 -0700 | [diff] [blame] | 327 | mIsStartGuestSession = isStartGuestSession; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 328 | mIsAddUser = isAddUser; |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 329 | mIsForeground = isForeground; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 330 | } |
| 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 | } |