1 /* 2 * Copyright (C) 2023 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.car.qc; 18 19 import static com.android.systemui.car.users.CarSystemUIUserUtil.getCurrentUserHandle; 20 21 import android.car.Car; 22 import android.car.app.CarActivityManager; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.graphics.drawable.Drawable; 26 import android.os.UserManager; 27 import android.util.AttributeSet; 28 import android.widget.ImageView; 29 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.systemui.R; 33 import com.android.systemui.car.statusbar.UserNameViewController; 34 import com.android.systemui.car.userswitcher.UserIconProvider; 35 36 /** 37 * One of {@link QCFooterButtonView} for quick control panels, which shows user information 38 * and opens the user picker. 39 */ 40 41 public class QCUserPickerButton extends QCFooterButtonView { 42 private static final String TAG = QCUserPickerButton.class.getSimpleName(); 43 44 private final Context mContext; 45 private CarActivityManager mCarActivityManager; 46 private UserIconProvider mUserIconProvider; 47 private UserManager mUserManager; 48 @VisibleForTesting 49 UserNameViewController mUserNameViewController; 50 QCUserPickerButton(Context context)51 public QCUserPickerButton(Context context) { 52 this(context, null); 53 } 54 QCUserPickerButton(Context context, AttributeSet attrs)55 public QCUserPickerButton(Context context, AttributeSet attrs) { 56 this(context, attrs, 0); 57 } 58 QCUserPickerButton(Context context, AttributeSet attrs, int defStyleAttr)59 public QCUserPickerButton(Context context, AttributeSet attrs, int defStyleAttr) { 60 this(context, attrs, defStyleAttr, 0); 61 } 62 QCUserPickerButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)63 public QCUserPickerButton(Context context, AttributeSet attrs, int defStyleAttr, 64 int defStyleRes) { 65 super(context, attrs, defStyleAttr, defStyleRes); 66 mContext = context; 67 mUserManager = mContext.getSystemService(UserManager.class); 68 69 mCarServiceLifecycleListener = (car, ready) -> { 70 if (!ready) { 71 return; 72 } 73 mCarActivityManager = (CarActivityManager) car.getCarManager( 74 Car.CAR_ACTIVITY_SERVICE); 75 }; 76 77 Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER, 78 mCarServiceLifecycleListener); 79 80 mOnClickListener = v -> openUserPicker(); 81 setOnClickListener(mOnClickListener); 82 } 83 84 @Override onAttachedToWindow()85 protected void onAttachedToWindow() { 86 super.onAttachedToWindow(); 87 88 if (mUserTracker == null) { 89 return; 90 } 91 92 ImageView userIconView = findViewById(R.id.user_icon); 93 if (userIconView != null) { 94 // Set user icon as the first letter of the user name. 95 mUserIconProvider = new UserIconProvider(); 96 Drawable circleIcon = mUserIconProvider.getRoundedUserIcon( 97 mUserTracker.getUserInfo(), mContext); 98 userIconView.setImageDrawable(circleIcon); 99 } 100 101 if (mBroadcastDispatcher != null) { 102 mUserNameViewController = new UserNameViewController( 103 mContext, mUserTracker, mUserManager, mBroadcastDispatcher); 104 mUserNameViewController.addUserNameView(this); 105 } 106 } 107 108 @Override onDetachedFromWindow()109 protected void onDetachedFromWindow() { 110 super.onDetachedFromWindow(); 111 112 if (mUserNameViewController != null) { 113 mUserNameViewController.removeUserNameView(this); 114 } 115 } 116 openUserPicker()117 private void openUserPicker() { 118 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS), 119 getCurrentUserHandle(mContext, mUserTracker)); 120 if (mCarActivityManager != null) { 121 mCarActivityManager.startUserPickerOnDisplay(mContext.getDisplayId()); 122 } 123 } 124 } 125