1 /*
2  * Copyright (C) 2022 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.users;
18 
19 import android.app.IActivityManager;
20 import android.content.Context;
21 import android.os.Handler;
22 import android.os.UserManager;
23 
24 import com.android.systemui.dump.DumpManager;
25 import com.android.systemui.flags.FeatureFlagsClassic;
26 import com.android.systemui.settings.UserTrackerImpl;
27 
28 import javax.inject.Provider;
29 
30 import kotlinx.coroutines.CoroutineDispatcher;
31 import kotlinx.coroutines.CoroutineScope;
32 
33 /**
34  * Custom user tracking class extended from {@link UserTrackerImpl} which defines custom behavior
35  * when CarSystemUI is running as a secondary user on a multi-display device.
36  *
37  */
38 public class CarUserTrackerImpl extends UserTrackerImpl {
39     // Indicates whether or not a UserTracker instance should ignore user switch events. This is
40     // typically used for background users who should not be influenced by foreground user switches.
41     private final boolean mShouldIgnoreUserSwitch;
42 
CarUserTrackerImpl(Context context, Provider<FeatureFlagsClassic> featureFlagsClassic, UserManager userManager, IActivityManager iActivityManager, DumpManager dumpManager, CoroutineScope appScope, CoroutineDispatcher backgroundContext, Handler backgroundHandler, boolean ignoreUserSwitch)43     public CarUserTrackerImpl(Context context, Provider<FeatureFlagsClassic> featureFlagsClassic,
44             UserManager userManager, IActivityManager iActivityManager, DumpManager dumpManager,
45             CoroutineScope appScope, CoroutineDispatcher backgroundContext,
46             Handler backgroundHandler, boolean ignoreUserSwitch) {
47         super(context, featureFlagsClassic, userManager, iActivityManager, dumpManager, appScope,
48                 backgroundContext, backgroundHandler);
49         mShouldIgnoreUserSwitch = ignoreUserSwitch;
50     }
51 
52     @Override
handleBeforeUserSwitching(int newUserId)53     public void handleBeforeUserSwitching(int newUserId) {
54         if (mShouldIgnoreUserSwitch) {
55             // Secondary user SystemUI instances are not running on foreground users, so they should
56             // not be impacted by foreground user switches.
57             return;
58         }
59         super.handleBeforeUserSwitching(newUserId);
60     }
61 
62     @Override
handleUserSwitching(int newUserId)63     public void handleUserSwitching(int newUserId) {
64         if (mShouldIgnoreUserSwitch) {
65             // Secondary user SystemUI instances are not running on foreground users, so they should
66             // not be impacted by foreground user switches.
67             return;
68         }
69         super.handleUserSwitching(newUserId);
70     }
71 
72     @Override
handleUserSwitchComplete(int newUserId)73     public void handleUserSwitchComplete(int newUserId) {
74         if (mShouldIgnoreUserSwitch) {
75             // Secondary user SystemUI instances are not running on foreground users, so they should
76             // not be impacted by foreground user switches.
77             return;
78         }
79         super.handleUserSwitchComplete(newUserId);
80     }
81 
82     @Override
handleProfilesChanged()83     protected void handleProfilesChanged() {
84         if (mShouldIgnoreUserSwitch) {
85             // Profile changes are only sent for the primary user, so they should be ignored by
86             // secondary users.
87             return;
88         }
89         super.handleProfilesChanged();
90     }
91 }
92