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