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 android.car.app.CarActivityManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.drawable.Drawable;
23 import android.os.UserManager;
24 import android.widget.ImageView;
25 
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.systemui.R;
29 import com.android.systemui.broadcast.BroadcastDispatcher;
30 import com.android.systemui.car.CarServiceProvider;
31 import com.android.systemui.car.statusbar.UserNameViewController;
32 import com.android.systemui.car.systembar.element.CarSystemBarElementController;
33 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController;
34 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController;
35 import com.android.systemui.car.userswitcher.UserIconProvider;
36 import com.android.systemui.settings.UserTracker;
37 
38 import dagger.assisted.Assisted;
39 import dagger.assisted.AssistedFactory;
40 import dagger.assisted.AssistedInject;
41 
42 /**
43  * One of {@link QCFooterView} for quick control panels, which shows user information
44  * and opens the user picker.
45  */
46 
47 public class QCUserPickerButtonController extends QCFooterViewController {
48     private final Context mContext;
49     private final UserTracker mUserTracker;
50     private final CarServiceProvider mCarServiceProvider;
51     private final BroadcastDispatcher mBroadcastDispatcher;
52     private final UserManager mUserManager;
53     private CarActivityManager mCarActivityManager;
54     @VisibleForTesting
55     UserNameViewController mUserNameViewController;
56 
57     private final CarServiceProvider.CarServiceOnConnectedListener mCarServiceLifecycleListener =
58             car -> {
59                 mCarActivityManager = car.getCarManager(CarActivityManager.class);
60             };
61 
62     @AssistedInject
QCUserPickerButtonController(@ssisted QCFooterView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Context context, UserTracker userTracker, CarServiceProvider carServiceProvider, BroadcastDispatcher broadcastDispatcher)63     protected QCUserPickerButtonController(@Assisted QCFooterView view,
64             CarSystemBarElementStatusBarDisableController disableController,
65             CarSystemBarElementStateController stateController, Context context,
66             UserTracker userTracker, CarServiceProvider carServiceProvider,
67             BroadcastDispatcher broadcastDispatcher) {
68         super(view, disableController, stateController, context, userTracker);
69         mContext = context;
70         mUserTracker = userTracker;
71         mCarServiceProvider = carServiceProvider;
72         mBroadcastDispatcher = broadcastDispatcher;
73         mUserManager = mContext.getSystemService(UserManager.class);
74     }
75 
76     @AssistedFactory
77     public interface Factory extends
78             CarSystemBarElementController.Factory<QCFooterView, QCUserPickerButtonController> {}
79 
80     @Override
onInit()81     protected void onInit() {
82         super.onInit();
83         mView.setOnClickListener(v -> openUserPicker());
84 
85         ImageView userIconView = mView.findViewById(R.id.user_icon);
86         if (userIconView != null) {
87             // Set user icon as the first letter of the username.
88             UserIconProvider userIconProvider = new UserIconProvider();
89             Drawable circleIcon = userIconProvider.getRoundedUserIcon(
90                     mUserTracker.getUserInfo(), mContext);
91             userIconView.setImageDrawable(circleIcon);
92         }
93     }
94 
95     @Override
onViewAttached()96     protected void onViewAttached() {
97         super.onViewAttached();
98         mCarServiceProvider.addListener(mCarServiceLifecycleListener);
99         mUserNameViewController = new UserNameViewController(
100                 mContext, mUserTracker, mUserManager, mBroadcastDispatcher);
101         mUserNameViewController.addUserNameView(mView);
102 
103     }
104 
105     @Override
onViewDetached()106     protected void onViewDetached() {
107         super.onViewDetached();
108         mCarServiceProvider.removeListener(mCarServiceLifecycleListener);
109         if (mUserNameViewController != null) {
110             mUserNameViewController.removeUserNameView(mView);
111             mUserNameViewController = null;
112         }
113     }
114 
openUserPicker()115     private void openUserPicker() {
116         mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS),
117                 mUserTracker.getUserHandle());
118         if (mCarActivityManager != null) {
119             mCarActivityManager.startUserPickerOnDisplay(getContext().getDisplayId());
120         }
121     }
122 }
123