1 /*
2  * Copyright (C) 2021 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 android.car.builtin.os;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.annotation.UserIdInt;
22 import android.content.Context;
23 import android.content.pm.UserInfo;
24 import android.os.SystemProperties;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Helper for User related operations.
33  *
34  * @hide
35  */
36 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
37 public final class UserManagerHelper {
UserManagerHelper()38     private UserManagerHelper() {
39         throw new UnsupportedOperationException();
40     }
41 
42     /** user id for invalid user */
43     public static final @UserIdInt int USER_NULL = UserHandle.USER_NULL;
44 
45     /** A user id constant to indicate the "system" user of the device */
46     public static final @UserIdInt int USER_SYSTEM = UserHandle.USER_SYSTEM;
47 
48     /** A user id constant to indicate "all" users of the device */
49     public static final @UserIdInt int USER_ALL = UserHandle.USER_ALL;
50 
51     // Flags copied from UserInfo.
52     public static final int FLAG_PRIMARY = UserInfo.FLAG_PRIMARY;
53     public static final int FLAG_ADMIN = UserInfo.FLAG_ADMIN;
54     public static final int FLAG_GUEST = UserInfo.FLAG_GUEST;
55     public static final int FLAG_RESTRICTED = UserInfo.FLAG_RESTRICTED;
56     public static final int FLAG_INITIALIZED = UserInfo.FLAG_INITIALIZED;
57     public static final int FLAG_MANAGED_PROFILE = UserInfo.FLAG_MANAGED_PROFILE;
58     public static final int FLAG_DISABLED = UserInfo.FLAG_DISABLED;
59     public static final int FLAG_QUIET_MODE = UserInfo.FLAG_QUIET_MODE;
60     public static final int FLAG_EPHEMERAL = UserInfo.FLAG_EPHEMERAL;
61     public static final int FLAG_DEMO = UserInfo.FLAG_DEMO;
62     public static final int FLAG_FULL = UserInfo.FLAG_FULL;
63     public static final int FLAG_SYSTEM = UserInfo.FLAG_SYSTEM;
64     public static final int FLAG_PROFILE = UserInfo.FLAG_PROFILE;
65 
66     /**
67      * Returns all user handles.
68      */
69     @NonNull
getUserHandles(@onNull UserManager userManager, boolean excludeDying)70     public static List<UserHandle> getUserHandles(@NonNull UserManager userManager,
71             boolean excludeDying) {
72         return userManager.getUserHandles(excludeDying);
73     }
74 
75     /**
76      * Returns all users based on the boolean flags.
77      *
78      * @deprecated Use {@link #getUserHandles(UserManager, boolean)} instead.
79      */
80     @Deprecated
81     @NonNull
getUserHandles(@onNull UserManager userManager, boolean excludePartial, boolean excludeDying, boolean excludePreCreated)82     public static List<UserHandle> getUserHandles(@NonNull UserManager userManager,
83             boolean excludePartial, boolean excludeDying, boolean excludePreCreated) {
84         List<UserInfo> users = userManager.getUsers(excludePartial, excludeDying,
85                 excludePreCreated);
86 
87         List<UserHandle> result = new ArrayList<>(users.size());
88         for (UserInfo user : users) {
89             result.add(user.getUserHandle());
90         }
91         return result;
92     }
93 
94     /**
95      * Checks if a user is ephemeral.
96      */
isEphemeralUser(@onNull UserManager userManager, @NonNull UserHandle user)97     public static boolean isEphemeralUser(@NonNull UserManager userManager,
98             @NonNull UserHandle user) {
99         return userManager.isUserEphemeral(user.getIdentifier());
100     }
101 
102     /** Checks if the user is guest. */
isGuestUser(@onNull UserManager userManager, @NonNull UserHandle user)103     public static boolean isGuestUser(@NonNull UserManager userManager, @NonNull UserHandle user) {
104         return userManager.isGuestUser(user.getIdentifier());
105     }
106 
107     /** Checks if the user is a full user. */
isFullUser(@onNull UserManager userManager, @NonNull UserHandle user)108     public static boolean isFullUser(@NonNull UserManager userManager, @NonNull UserHandle user) {
109         UserInfo info = userManager.getUserInfo(user.getIdentifier());
110         return info != null && info.isFull();
111     }
112 
113     /**
114      * Checks if a user is enabled.
115      */
isEnabledUser(@onNull UserManager userManager, @NonNull UserHandle user)116     public static boolean isEnabledUser(@NonNull UserManager userManager,
117             @NonNull UserHandle user) {
118         return userManager.getUserInfo(user.getIdentifier()).isEnabled();
119     }
120 
121     /**
122      * Checks if a user is initialized.
123      */
isInitializedUser(@onNull UserManager userManager, @NonNull UserHandle user)124     public static boolean isInitializedUser(@NonNull UserManager userManager,
125             @NonNull UserHandle user) {
126         return userManager.getUserInfo(user.getIdentifier()).isInitialized();
127     }
128 
129     /**
130      * Gets DefaultUserType given userInfo flags.
131      */
getDefaultUserTypeForUserInfoFlags(int userInfoFlag)132     public static String getDefaultUserTypeForUserInfoFlags(int userInfoFlag) {
133         return UserInfo.getDefaultUserType(userInfoFlag);
134     }
135 
136     /**
137      * Gets the default name for a user.
138      */
139     @NonNull
getDefaultUserName(@onNull Context context)140     public static String getDefaultUserName(@NonNull Context context) {
141         return context.getResources().getString(com.android.internal.R.string.owner_name);
142     }
143 
144     /**
145      * Gets the maximum number of users that can be running at any given time.
146      */
getMaxRunningUsers(@onNull Context context)147     public static int getMaxRunningUsers(@NonNull Context context) {
148         return context.getResources()
149                 .getInteger(com.android.internal.R.integer.config_multiuserMaxRunningUsers);
150     }
151 
152     /**
153      * Marks guest for deletion
154      */
markGuestForDeletion(@onNull UserManager userManager, @NonNull UserHandle user)155     public static boolean markGuestForDeletion(@NonNull UserManager userManager,
156             @NonNull UserHandle user) {
157         return userManager.markGuestForDeletion(user.getIdentifier());
158     }
159 
160     /**
161      * Returns the user id for a given uid.
162      */
getUserId(int uid)163     public static @UserIdInt int getUserId(int uid) {
164         return UserHandle.getUserId(uid);
165     }
166 
167     /** Check {@link UserManager#isVisibleBackgroundUsersSupported()}. */
isVisibleBackgroundUsersSupported(@onNull UserManager userManager)168     public static boolean isVisibleBackgroundUsersSupported(@NonNull UserManager userManager) {
169         return userManager.isVisibleBackgroundUsersSupported();
170     }
171 
172     /** Check {@link UserManager#isVisibleBackgroundUsersOnDefaultDisplaySupported()}. */
isVisibleBackgroundUsersOnDefaultDisplaySupported( @onNull UserManager userManager)173     public static boolean isVisibleBackgroundUsersOnDefaultDisplaySupported(
174             @NonNull UserManager userManager) {
175         return userManager.isVisibleBackgroundUsersOnDefaultDisplaySupported();
176     }
177 
178     /** Check {@link UserManager#getMaxSupportedUsers()}. */
getMaxSupportedUsers(@onNull Context context)179     public static int getMaxSupportedUsers(@NonNull Context context) {
180         return Math.max(1, SystemProperties.getInt("fw.max_users",
181                 context.getResources().getSystem().getInteger(
182                         com.android.internal.R.integer.config_multiuserMaximumUsers)));
183     }
184 
185     /** Check {@link UserManager#getMainDisplayIdAssignedToUser()}. */
getMainDisplayIdAssignedToUser(@onNull UserManager userManager)186     public static int getMainDisplayIdAssignedToUser(@NonNull UserManager userManager) {
187         return userManager.getMainDisplayIdAssignedToUser();
188     }
189 }
190