• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ActivityManager;
20 import android.car.CarOccupantZoneManager;
21 import android.content.Context;
22 import android.os.Process;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import com.android.systemui.settings.UserTracker;
30 
31 /**
32  * Class containing utility functions relating to users in CarSystemUI.
33  */
34 public final class CarSystemUIUserUtil {
CarSystemUIUserUtil()35     private CarSystemUIUserUtil() {};
36 
37     /**
38      * Attempt to get the current user handle for the user associated with this SystemUI process.
39      * Ideally the classes calling this will have an instance of UserTracker that can return the
40      * UserHandle as the central source of truth but in the event that UserTracker is not set,
41      * we can use some assumptions about the system to infer what the correct user is.
42      */
getCurrentUserHandle(Context context, @Nullable UserTracker userTracker)43     public static UserHandle getCurrentUserHandle(Context context,
44             @Nullable UserTracker userTracker) {
45         if (userTracker != null) {
46             return userTracker.getUserHandle();
47         }
48         // If the UserTracker has not been set, we can try to guess the current user based on the
49         // context user.
50         if (context.getUserId() == UserHandle.USER_SYSTEM
51                 && UserManager.isHeadlessSystemUserMode()) {
52             // SystemUI is running as system user (which is not the current foreground user) -
53             // return the current foreground user.
54             return UserHandle.of(ActivityManager.getCurrentUser());
55         }
56         // SystemUI is running as a non-headless system user - this is probably the correct user
57         return UserHandle.of(context.getUserId());
58     }
59 
60     /**
61      * Helper function that returns {@code true} if the current system supports MUMD.
62      */
isMUMDSystemUI()63     public static boolean isMUMDSystemUI() {
64         return UserManager.isVisibleBackgroundUsersEnabled();
65     }
66 
67     /**
68      * Helper function that returns {@code true} if the current SystemUI instance is the driver
69      * (primary) SystemUI on an MUMD system.
70      */
isDriverMUMDSystemUI()71     public static boolean isDriverMUMDSystemUI() {
72         UserHandle myUserHandle = Process.myUserHandle();
73         return isMUMDSystemUI() && !UserManager.isVisibleBackgroundUsersOnDefaultDisplayEnabled()
74                 && myUserHandle.isSystem();
75     }
76 
77     /**
78      * Helper function that returns {@code true} if the current instance of SystemUI is running as
79      * a secondary user on MUMD system.
80      */
isSecondaryMUMDSystemUI()81     public static boolean isSecondaryMUMDSystemUI() {
82         UserHandle myUserHandle = Process.myUserHandle();
83         return isMUMDSystemUI()
84                 && !myUserHandle.isSystem()
85                 && myUserHandle.getIdentifier() != ActivityManager.getCurrentUser();
86     }
87 
88     /**
89      * Helper function that returns {@code true} if the current instance of SystemUI is running as
90      * the system user on a MUPAND system.
91      */
isMUPANDSystemUI()92     public static boolean isMUPANDSystemUI() {
93         return UserManager.isVisibleBackgroundUsersOnDefaultDisplayEnabled()
94                 && Process.myUserHandle().isSystem();
95     }
96 
97     /**
98      * Helper function that returns {@code true} if the specified displayId is associated with the
99      * current SystemUI instance.
100      */
isCurrentSystemUIDisplay( @onNull CarOccupantZoneManager carOccupantZoneManager, @NonNull CarOccupantZoneManager.OccupantZoneInfo occupantZone, int displayId)101     public static boolean isCurrentSystemUIDisplay(
102             @NonNull CarOccupantZoneManager carOccupantZoneManager,
103             @NonNull CarOccupantZoneManager.OccupantZoneInfo occupantZone,
104             int displayId) {
105         if (!isMUMDSystemUI()) {
106             return true;
107         }
108         return carOccupantZoneManager.getAllDisplaysForOccupant(occupantZone).stream().anyMatch(
109                 d -> d.getDisplayId() == displayId);
110     }
111 }
112