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; |
| 41 | import com.android.systemui.qs.car.CarQSFragment; |
| 42 | import com.android.systemui.statusbar.phone.StatusBar; |
| 43 | |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.List; |
| 46 | |
| 47 | /** |
| 48 | * Displays a GridLayout with icons for the users in the system to allow switching between users. |
| 49 | * One of the uses of this is for the lock screen in auto. |
| 50 | */ |
| 51 | public class UserGridRecyclerView extends RecyclerView implements |
| 52 | UserManagerHelper.OnUsersUpdateListener { |
| 53 | |
| 54 | private StatusBar mStatusBar; |
| 55 | private UserSelectionListener mUserSelectionListener; |
| 56 | private UserAdapter mAdapter; |
| 57 | private UserManagerHelper mUserManagerHelper; |
| 58 | private Context mContext; |
| 59 | |
| 60 | public UserGridRecyclerView(Context context, AttributeSet attrs) { |
| 61 | super(context, attrs); |
| 62 | super.setHasFixedSize(true); |
| 63 | mContext = context; |
| 64 | mUserManagerHelper = new UserManagerHelper(mContext); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Register listener for any update to the users |
| 69 | */ |
| 70 | @Override |
| 71 | public void onFinishInflate() { |
| 72 | mUserManagerHelper.registerOnUsersUpdateListener(this); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Unregisters listener checking for any change to the users |
| 77 | */ |
| 78 | @Override |
| 79 | public void onDetachedFromWindow() { |
| 80 | mUserManagerHelper.unregisterOnUsersUpdateListener(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Initializes the adapter that populates the grid layout |
| 85 | * |
| 86 | * @return the adapter |
| 87 | */ |
| 88 | public void buildAdapter() { |
| 89 | List<UserRecord> userRecords = createUserRecords(mUserManagerHelper |
| 90 | .getAllUsers()); |
| 91 | mAdapter = new UserAdapter(mContext, userRecords); |
| 92 | super.setAdapter(mAdapter); |
| 93 | } |
| 94 | |
| 95 | public void setStatusBar(@Nullable StatusBar statusBar) { |
| 96 | mStatusBar = statusBar; |
| 97 | } |
| 98 | |
| 99 | private List<UserRecord> createUserRecords(List<UserInfo> userInfoList) { |
| 100 | List<UserRecord> userRecords = new ArrayList<>(); |
| 101 | for (UserInfo userInfo : userInfoList) { |
| 102 | boolean isCurrent = false; |
| 103 | if (ActivityManager.getCurrentUser() == userInfo.id) { |
| 104 | isCurrent = true; |
| 105 | } |
| 106 | UserRecord record = new UserRecord(userInfo, false /* isGuest */, |
| 107 | false /* isAddUser */, isCurrent); |
| 108 | userRecords.add(record); |
| 109 | } |
| 110 | |
| 111 | // Add guest user record if the current user is not a guest |
jovanak | 82029ae | 2018-04-02 16:40:15 -0700 | [diff] [blame] | 112 | if (!mUserManagerHelper.foregroundUserIsGuestUser()) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 113 | userRecords.add(addGuestUserRecord()); |
| 114 | } |
| 115 | |
| 116 | // Add add user record if the current user can add users |
jovanak | 82029ae | 2018-04-02 16:40:15 -0700 | [diff] [blame] | 117 | if (mUserManagerHelper.foregroundUserCanAddUsers()) { |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 118 | userRecords.add(addUserRecord()); |
| 119 | } |
| 120 | |
| 121 | return userRecords; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Create guest user record |
| 126 | */ |
| 127 | private UserRecord addGuestUserRecord() { |
| 128 | UserInfo userInfo = new UserInfo(); |
| 129 | userInfo.name = mContext.getString(R.string.car_guest); |
| 130 | return new UserRecord(userInfo, true /* isGuest */, |
| 131 | false /* isAddUser */, false /* isCurrent */); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Create add user record |
| 136 | */ |
| 137 | private UserRecord addUserRecord() { |
| 138 | UserInfo userInfo = new UserInfo(); |
| 139 | userInfo.name = mContext.getString(R.string.car_add_user); |
| 140 | return new UserRecord(userInfo, false /* isGuest */, |
| 141 | true /* isAddUser */, false /* isCurrent */); |
| 142 | } |
| 143 | |
| 144 | public void onUserSwitched(int newUserId) { |
| 145 | // Bring up security view after user switch is completed. |
| 146 | post(this::showOfflineAuthUi); |
| 147 | } |
| 148 | |
| 149 | public void setUserSelectionListener(UserSelectionListener userSelectionListener) { |
| 150 | mUserSelectionListener = userSelectionListener; |
| 151 | } |
| 152 | |
| 153 | void showOfflineAuthUi() { |
| 154 | // TODO: Show keyguard UI in-place. |
| 155 | if (mStatusBar != null) { |
| 156 | mStatusBar.executeRunnableDismissingKeyguard(null/* runnable */, null /* cancelAction */, |
| 157 | true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onUsersUpdate() { |
| 163 | mAdapter.clearUsers(); |
| 164 | mAdapter.updateUsers(createUserRecords(mUserManagerHelper.getAllUsers())); |
| 165 | mAdapter.notifyDataSetChanged(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Adapter to populate the grid layout with the available user profiles |
| 170 | */ |
| 171 | public final class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserAdapterViewHolder> { |
| 172 | |
| 173 | private final Context mContext; |
| 174 | private List<UserRecord> mUsers; |
| 175 | private final int mPodImageAvatarWidth; |
| 176 | private final int mPodImageAvatarHeight; |
| 177 | private final Resources mRes; |
| 178 | private final String mGuestName; |
| 179 | private final String mNewUserName; |
| 180 | |
| 181 | public UserAdapter(Context context, List<UserRecord> users) { |
| 182 | mRes = context.getResources(); |
| 183 | mContext = context; |
| 184 | updateUsers(users); |
| 185 | mPodImageAvatarWidth = mRes.getDimensionPixelSize( |
| 186 | R.dimen.car_fullscreen_user_pod_image_avatar_width); |
| 187 | mPodImageAvatarHeight = mRes.getDimensionPixelSize( |
| 188 | R.dimen.car_fullscreen_user_pod_image_avatar_height); |
| 189 | mGuestName = mRes.getString(R.string.car_guest); |
| 190 | mNewUserName = mRes.getString(R.string.car_new_user); |
| 191 | } |
| 192 | |
| 193 | public void clearUsers() { |
| 194 | mUsers.clear(); |
| 195 | } |
| 196 | |
| 197 | public void updateUsers(List<UserRecord> users) { |
| 198 | mUsers = users; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public UserAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 203 | View view = LayoutInflater.from(mContext) |
| 204 | .inflate(R.layout.car_fullscreen_user_pod, parent, false); |
| 205 | view.setAlpha(1f); |
| 206 | view.bringToFront(); |
| 207 | return new UserAdapterViewHolder(view); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void onBindViewHolder(UserAdapterViewHolder holder, int position) { |
| 212 | UserRecord userRecord = mUsers.get(position); |
| 213 | holder.mUserAvatarImageView.setImageBitmap(getDefaultUserIcon(userRecord)); |
| 214 | holder.mUserNameTextView.setText(userRecord.mInfo.name); |
| 215 | holder.mView.setOnClickListener(v -> { |
| 216 | if (userRecord == null) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | // Notify the listener which user was selected |
| 221 | if (mUserSelectionListener != null) { |
| 222 | mUserSelectionListener.onUserSelected(userRecord); |
| 223 | } |
| 224 | |
| 225 | // If the user selects Guest, switch to Guest profile |
| 226 | if (userRecord.mIsGuest) { |
| 227 | mUserManagerHelper.switchToGuest(mGuestName); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | // If the user wants to add a user, start task to add new user |
| 232 | if (userRecord.mIsAddUser) { |
| 233 | new AddNewUserTask().execute(mNewUserName); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // If the user doesn't want to be a guest or add a user, switch to the user selected |
| 238 | mUserManagerHelper.switchToUser(userRecord.mInfo); |
| 239 | }); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | private class AddNewUserTask extends AsyncTask<String, Void, UserInfo> { |
| 244 | |
| 245 | @Override |
| 246 | protected UserInfo doInBackground(String... userNames) { |
| 247 | return mUserManagerHelper.createNewUser(userNames[0]); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | protected void onPreExecute() { |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | protected void onPostExecute(UserInfo user) { |
| 256 | if (user != null) { |
| 257 | mUserManagerHelper.switchToUser(user); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public int getItemCount() { |
| 264 | return mUsers.size(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns the default user icon. This icon is a circle with a letter in it. The letter is |
| 269 | * the first character in the username. |
| 270 | * |
| 271 | * @param record the profile of the user for which the icon should be created |
| 272 | */ |
| 273 | private Bitmap getDefaultUserIcon(UserRecord record) { |
| 274 | CharSequence displayText; |
| 275 | boolean isAddUserText = false; |
| 276 | if (record.mIsAddUser) { |
| 277 | displayText = "+"; |
| 278 | isAddUserText = true; |
| 279 | } else { |
| 280 | displayText = record.mInfo.name.subSequence(0, 1); |
| 281 | } |
| 282 | Bitmap out = Bitmap.createBitmap(mPodImageAvatarWidth, mPodImageAvatarHeight, |
| 283 | Bitmap.Config.ARGB_8888); |
| 284 | Canvas canvas = new Canvas(out); |
| 285 | |
| 286 | // Draw the circle background. |
| 287 | GradientDrawable shape = new GradientDrawable(); |
| 288 | shape.setShape(GradientDrawable.RADIAL_GRADIENT); |
| 289 | shape.setGradientRadius(1.0f); |
| 290 | shape.setColor(mContext.getColor(R.color.car_user_switcher_no_user_image_bgcolor)); |
| 291 | shape.setBounds(0, 0, mPodImageAvatarWidth, mPodImageAvatarHeight); |
| 292 | shape.draw(canvas); |
| 293 | |
| 294 | // Draw the letter in the center. |
| 295 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 296 | paint.setColor(mContext.getColor(R.color.car_user_switcher_no_user_image_fgcolor)); |
| 297 | paint.setTextAlign(Align.CENTER); |
| 298 | if (isAddUserText) { |
| 299 | paint.setTextSize(mRes.getDimensionPixelSize( |
| 300 | R.dimen.car_touch_target_size)); |
| 301 | } else { |
| 302 | paint.setTextSize(mRes.getDimensionPixelSize( |
| 303 | R.dimen.car_fullscreen_user_pod_icon_text_size)); |
| 304 | } |
| 305 | |
| 306 | Paint.FontMetricsInt metrics = paint.getFontMetricsInt(); |
| 307 | // The Y coordinate is measured by taking half the height of the pod, but that would |
| 308 | // draw the character putting the bottom of the font in the middle of the pod. To |
| 309 | // correct this, half the difference between the top and bottom distance metrics of the |
| 310 | // font gives the offset of the font. Bottom is a positive value, top is negative, so |
| 311 | // the different is actually a sum. The "half" operation is then factored out. |
| 312 | canvas.drawText(displayText.toString(), mPodImageAvatarWidth / 2, |
| 313 | (mPodImageAvatarHeight - (metrics.bottom + metrics.top)) / 2, paint); |
| 314 | |
| 315 | return out; |
| 316 | } |
| 317 | |
| 318 | public class UserAdapterViewHolder extends RecyclerView.ViewHolder { |
| 319 | |
| 320 | public ImageView mUserAvatarImageView; |
| 321 | public TextView mUserNameTextView; |
| 322 | public View mView; |
| 323 | |
| 324 | public UserAdapterViewHolder(View view) { |
| 325 | super(view); |
| 326 | mView = view; |
| 327 | mUserAvatarImageView = (ImageView) view.findViewById(R.id.user_avatar); |
| 328 | mUserNameTextView = (TextView) view.findViewById(R.id.user_name); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Object wrapper class for the userInfo. Use it to distinguish if a profile is a |
| 335 | * guest profile, add user profile, or a current user. |
| 336 | */ |
| 337 | public static final class UserRecord { |
| 338 | |
| 339 | public final UserInfo mInfo; |
| 340 | public final boolean mIsGuest; |
| 341 | public final boolean mIsAddUser; |
| 342 | public final boolean mIsCurrent; |
| 343 | |
| 344 | public UserRecord(UserInfo userInfo, boolean isGuest, boolean isAddUser, |
| 345 | boolean isCurrent) { |
| 346 | mInfo = userInfo; |
| 347 | mIsGuest = isGuest; |
| 348 | mIsAddUser = isAddUser; |
| 349 | mIsCurrent = isCurrent; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Listener used to notify when a user has been selected |
| 355 | */ |
| 356 | interface UserSelectionListener { |
| 357 | |
| 358 | void onUserSelected(UserRecord record); |
| 359 | } |
| 360 | } |