1 /*
2  * Copyright (C) 2020 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.userswitcher;
18 
19 import android.car.user.CarUserManager;
20 import android.content.Context;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.systemui.car.CarServiceProvider;
27 import com.android.systemui.car.users.CarSystemUIUserUtil;
28 import com.android.systemui.car.window.OverlayViewMediator;
29 import com.android.systemui.settings.UserTracker;
30 
31 import javax.inject.Inject;
32 
33 /**
34  * Registers listeners that subscribe to events that show or hide CarUserSwitchingDialog that is
35  * mounted to SystemUiOverlayWindow.
36  */
37 public class UserSwitchTransitionViewMediator implements OverlayViewMediator,
38         CarUserManager.UserSwitchUiCallback {
39     private static final String TAG = "UserSwitchTransitionViewMediator";
40 
41     private final Context mContext;
42     private final CarServiceProvider mCarServiceProvider;
43     private final UserTracker mUserTracker;
44     private final UserSwitchTransitionViewController mUserSwitchTransitionViewController;
45 
46     @VisibleForTesting
47     final UserTracker.Callback mUserChangedCallback = new UserTracker.Callback() {
48         @Override
49         public void onBeforeUserSwitching(int newUser) {
50             mUserSwitchTransitionViewController.handleShow(newUser);
51         }
52 
53         @Override
54         public void onUserChanging(int newUser, @NonNull Context userContext) {
55             mUserSwitchTransitionViewController.handleSwitching(newUser);
56         }
57 
58         @Override
59         public void onUserChanged(int newUser, @NonNull Context userContext) {
60             mUserSwitchTransitionViewController.handleHide();
61         }
62     };
63 
64     @Inject
UserSwitchTransitionViewMediator( Context context, CarServiceProvider carServiceProvider, UserTracker userTracker, UserSwitchTransitionViewController userSwitchTransitionViewController)65     public UserSwitchTransitionViewMediator(
66             Context context,
67             CarServiceProvider carServiceProvider,
68             UserTracker userTracker,
69             UserSwitchTransitionViewController userSwitchTransitionViewController) {
70         mContext = context;
71         mCarServiceProvider = carServiceProvider;
72         mUserTracker = userTracker;
73         mUserSwitchTransitionViewController = userSwitchTransitionViewController;
74     }
75 
76     @Override
registerListeners()77     public void registerListeners() {
78         if (!CarSystemUIUserUtil.isSecondaryMUMDSystemUI()) {
79             // TODO(b/335664913): allow for callback from non-system user (and per user).
80             mCarServiceProvider.addListener(car -> {
81                 CarUserManager carUserManager = car.getCarManager(CarUserManager.class);
82 
83                 if (carUserManager != null) {
84                     carUserManager.setUserSwitchUiCallback(this);
85                 } else {
86                     Log.e(TAG, "registerListeners: CarUserManager could not be obtained.");
87                 }
88             });
89         }
90 
91         mUserTracker.addCallback(mUserChangedCallback, mContext.getMainExecutor());
92     }
93 
94     @Override
setUpOverlayContentViewControllers()95     public void setUpOverlayContentViewControllers() {
96         // no-op.
97     }
98 
99     @Override
showUserSwitchDialog(int userId)100     public void showUserSwitchDialog(int userId) {
101         mUserSwitchTransitionViewController.handleShow(userId);
102     }
103 }
104