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 | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 94 | boolean isForeground = mUserManagerHelper.getForegroundUserId() == userInfo.id; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 95 | UserRecord record = new UserRecord(userInfo, false /* isGuest */, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 96 | false /* isAddUser */, isForeground); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 97 | userRecords.add(record); |
| 98 | } |
| 99 | |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 100 | // Add guest user record if the foreground user is not a guest |
jovanak | 82029ae | 2018-04-02 16:40:15 -0700 | [diff] [blame] | 101 | if (!mUserManagerHelper.foregroundUserIsGuestUser()) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 102 | userRecords.add(addGuestUserRecord()); |
| 103 | } |
| 104 | |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 105 | // Add add user record if the foreground user can add users |
jovanak | 82029ae | 2018-04-02 16:40:15 -0700 | [diff] [blame] | 106 | if (mUserManagerHelper.foregroundUserCanAddUsers()) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 107 | userRecords.add(addUserRecord()); |
| 108 | } |
| 109 | |
| 110 | return userRecords; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Create guest user record |
| 115 | */ |
| 116 | private UserRecord addGuestUserRecord() { |
| 117 | UserInfo userInfo = new UserInfo(); |
| 118 | userInfo.name = mContext.getString(R.string.car_guest); |
| 119 | return new UserRecord(userInfo, true /* isGuest */, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 120 | false /* isAddUser */, false /* isForeground */); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Create add user record |
| 125 | */ |
| 126 | private UserRecord addUserRecord() { |
| 127 | UserInfo userInfo = new UserInfo(); |
| 128 | userInfo.name = mContext.getString(R.string.car_add_user); |
| 129 | return new UserRecord(userInfo, false /* isGuest */, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 130 | true /* isAddUser */, false /* isForeground */); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | public void setUserSelectionListener(UserSelectionListener userSelectionListener) { |
| 134 | mUserSelectionListener = userSelectionListener; |
| 135 | } |
| 136 | |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public void onUsersUpdate() { |
| 139 | mAdapter.clearUsers(); |
| 140 | mAdapter.updateUsers(createUserRecords(mUserManagerHelper.getAllUsers())); |
| 141 | mAdapter.notifyDataSetChanged(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Adapter to populate the grid layout with the available user profiles |
| 146 | */ |
| 147 | public final class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserAdapterViewHolder> { |
| 148 | |
| 149 | private final Context mContext; |
| 150 | private List<UserRecord> mUsers; |
| 151 | private final int mPodImageAvatarWidth; |
| 152 | private final int mPodImageAvatarHeight; |
| 153 | private final Resources mRes; |
| 154 | private final String mGuestName; |
| 155 | private final String mNewUserName; |
| 156 | |
| 157 | public UserAdapter(Context context, List<UserRecord> users) { |
| 158 | mRes = context.getResources(); |
| 159 | mContext = context; |
| 160 | updateUsers(users); |
| 161 | mPodImageAvatarWidth = mRes.getDimensionPixelSize( |
| 162 | R.dimen.car_fullscreen_user_pod_image_avatar_width); |
| 163 | mPodImageAvatarHeight = mRes.getDimensionPixelSize( |
| 164 | R.dimen.car_fullscreen_user_pod_image_avatar_height); |
| 165 | mGuestName = mRes.getString(R.string.car_guest); |
| 166 | mNewUserName = mRes.getString(R.string.car_new_user); |
| 167 | } |
| 168 | |
| 169 | public void clearUsers() { |
| 170 | mUsers.clear(); |
| 171 | } |
| 172 | |
| 173 | public void updateUsers(List<UserRecord> users) { |
| 174 | mUsers = users; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public UserAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 179 | View view = LayoutInflater.from(mContext) |
| 180 | .inflate(R.layout.car_fullscreen_user_pod, parent, false); |
| 181 | view.setAlpha(1f); |
| 182 | view.bringToFront(); |
| 183 | return new UserAdapterViewHolder(view); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onBindViewHolder(UserAdapterViewHolder holder, int position) { |
| 188 | UserRecord userRecord = mUsers.get(position); |
| 189 | holder.mUserAvatarImageView.setImageBitmap(getDefaultUserIcon(userRecord)); |
| 190 | holder.mUserNameTextView.setText(userRecord.mInfo.name); |
| 191 | holder.mView.setOnClickListener(v -> { |
| 192 | if (userRecord == null) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Notify the listener which user was selected |
| 197 | if (mUserSelectionListener != null) { |
| 198 | mUserSelectionListener.onUserSelected(userRecord); |
| 199 | } |
| 200 | |
| 201 | // If the user selects Guest, switch to Guest profile |
| 202 | if (userRecord.mIsGuest) { |
| 203 | mUserManagerHelper.switchToGuest(mGuestName); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // If the user wants to add a user, start task to add new user |
| 208 | if (userRecord.mIsAddUser) { |
| 209 | new AddNewUserTask().execute(mNewUserName); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | // If the user doesn't want to be a guest or add a user, switch to the user selected |
| 214 | mUserManagerHelper.switchToUser(userRecord.mInfo); |
| 215 | }); |
| 216 | |
| 217 | } |
| 218 | |
| 219 | private class AddNewUserTask extends AsyncTask<String, Void, UserInfo> { |
| 220 | |
| 221 | @Override |
| 222 | protected UserInfo doInBackground(String... userNames) { |
| 223 | return mUserManagerHelper.createNewUser(userNames[0]); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected void onPreExecute() { |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | protected void onPostExecute(UserInfo user) { |
| 232 | if (user != null) { |
| 233 | mUserManagerHelper.switchToUser(user); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public int getItemCount() { |
| 240 | return mUsers.size(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns the default user icon. This icon is a circle with a letter in it. The letter is |
| 245 | * the first character in the username. |
| 246 | * |
| 247 | * @param record the profile of the user for which the icon should be created |
| 248 | */ |
| 249 | private Bitmap getDefaultUserIcon(UserRecord record) { |
| 250 | CharSequence displayText; |
| 251 | boolean isAddUserText = false; |
| 252 | if (record.mIsAddUser) { |
| 253 | displayText = "+"; |
| 254 | isAddUserText = true; |
| 255 | } else { |
| 256 | displayText = record.mInfo.name.subSequence(0, 1); |
| 257 | } |
| 258 | Bitmap out = Bitmap.createBitmap(mPodImageAvatarWidth, mPodImageAvatarHeight, |
| 259 | Bitmap.Config.ARGB_8888); |
| 260 | Canvas canvas = new Canvas(out); |
| 261 | |
| 262 | // Draw the circle background. |
| 263 | GradientDrawable shape = new GradientDrawable(); |
| 264 | shape.setShape(GradientDrawable.RADIAL_GRADIENT); |
| 265 | shape.setGradientRadius(1.0f); |
| 266 | shape.setColor(mContext.getColor(R.color.car_user_switcher_no_user_image_bgcolor)); |
| 267 | shape.setBounds(0, 0, mPodImageAvatarWidth, mPodImageAvatarHeight); |
| 268 | shape.draw(canvas); |
| 269 | |
| 270 | // Draw the letter in the center. |
| 271 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 272 | paint.setColor(mContext.getColor(R.color.car_user_switcher_no_user_image_fgcolor)); |
| 273 | paint.setTextAlign(Align.CENTER); |
| 274 | if (isAddUserText) { |
| 275 | paint.setTextSize(mRes.getDimensionPixelSize( |
| 276 | R.dimen.car_touch_target_size)); |
| 277 | } else { |
| 278 | paint.setTextSize(mRes.getDimensionPixelSize( |
| 279 | R.dimen.car_fullscreen_user_pod_icon_text_size)); |
| 280 | } |
| 281 | |
| 282 | Paint.FontMetricsInt metrics = paint.getFontMetricsInt(); |
| 283 | // The Y coordinate is measured by taking half the height of the pod, but that would |
| 284 | // draw the character putting the bottom of the font in the middle of the pod. To |
| 285 | // correct this, half the difference between the top and bottom distance metrics of the |
| 286 | // font gives the offset of the font. Bottom is a positive value, top is negative, so |
| 287 | // the different is actually a sum. The "half" operation is then factored out. |
| 288 | canvas.drawText(displayText.toString(), mPodImageAvatarWidth / 2, |
| 289 | (mPodImageAvatarHeight - (metrics.bottom + metrics.top)) / 2, paint); |
| 290 | |
| 291 | return out; |
| 292 | } |
| 293 | |
| 294 | public class UserAdapterViewHolder extends RecyclerView.ViewHolder { |
| 295 | |
| 296 | public ImageView mUserAvatarImageView; |
| 297 | public TextView mUserNameTextView; |
| 298 | public View mView; |
| 299 | |
| 300 | public UserAdapterViewHolder(View view) { |
| 301 | super(view); |
| 302 | mView = view; |
| 303 | mUserAvatarImageView = (ImageView) view.findViewById(R.id.user_avatar); |
| 304 | mUserNameTextView = (TextView) view.findViewById(R.id.user_name); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * 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^] | 311 | * guest profile, add user profile, or the foreground user. |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 312 | */ |
| 313 | public static final class UserRecord { |
| 314 | |
| 315 | public final UserInfo mInfo; |
| 316 | public final boolean mIsGuest; |
| 317 | public final boolean mIsAddUser; |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 318 | public final boolean mIsForeground; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 319 | |
| 320 | public UserRecord(UserInfo userInfo, boolean isGuest, boolean isAddUser, |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 321 | boolean isForeground) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 322 | mInfo = userInfo; |
| 323 | mIsGuest = isGuest; |
| 324 | mIsAddUser = isAddUser; |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame^] | 325 | mIsForeground = isForeground; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Listener used to notify when a user has been selected |
| 331 | */ |
| 332 | interface UserSelectionListener { |
| 333 | |
| 334 | void onUserSelected(UserRecord record); |
| 335 | } |
| 336 | } |