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 instance of SystemUI is running as 69 * a secondary user on MUMD system. 70 */ isSecondaryMUMDSystemUI()71 public static boolean isSecondaryMUMDSystemUI() { 72 UserHandle myUserHandle = Process.myUserHandle(); 73 return isMUMDSystemUI() 74 && !myUserHandle.isSystem() 75 && myUserHandle.getIdentifier() != ActivityManager.getCurrentUser(); 76 } 77 78 /** 79 * Helper function that returns {@code true} if the current instance of SystemUI is running as 80 * the system user on a MUPAND system. 81 */ isMUPANDSystemUI()82 public static boolean isMUPANDSystemUI() { 83 return UserManager.isVisibleBackgroundUsersOnDefaultDisplayEnabled() 84 && Process.myUserHandle().isSystem(); 85 } 86 87 /** 88 * Helper function that returns {@code true} if the specified displayId is associated with the 89 * current SystemUI instance. 90 */ isCurrentSystemUIDisplay( @onNull CarOccupantZoneManager carOccupantZoneManager, @NonNull CarOccupantZoneManager.OccupantZoneInfo occupantZone, int displayId)91 public static boolean isCurrentSystemUIDisplay( 92 @NonNull CarOccupantZoneManager carOccupantZoneManager, 93 @NonNull CarOccupantZoneManager.OccupantZoneInfo occupantZone, 94 int displayId) { 95 if (!isMUMDSystemUI()) { 96 return true; 97 } 98 return carOccupantZoneManager.getAllDisplaysForOccupant(occupantZone).stream().anyMatch( 99 d -> d.getDisplayId() == displayId); 100 } 101 } 102