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