blob: b92fb47d11128d913df20ccfa5cdb2e454d0abfd [file] [log] [blame]
Amith Yamasani742a6712011-05-04 14:49:28 -07001/*
2 * Copyright (C) 2011 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
17package android.os;
18
Jeff Sharkey8588bc12016-01-06 16:47:42 -070019import android.annotation.AppIdInt;
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070020import android.annotation.NonNull;
Christopher Tatecbd7a6e2014-08-06 14:24:14 -070021import android.annotation.SystemApi;
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -070022import android.annotation.TestApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070023import android.annotation.UserIdInt;
Artur Satayevafdb23a2019-12-10 17:47:53 +000024import android.compat.annotation.UnsupportedAppUsage;
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +010025
Dianne Hackborna06de0f2012-12-11 16:34:47 -080026import java.io.PrintWriter;
27
Amith Yamasani742a6712011-05-04 14:49:28 -070028/**
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070029 * Representation of a user on the device.
Amith Yamasani742a6712011-05-04 14:49:28 -070030 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070031public final class UserHandle implements Parcelable {
Jeff Sharkey77a34502017-11-09 17:40:57 -070032 // NOTE: keep logic in sync with system/core/libcutils/multiuser.c
33
Amith Yamasani742a6712011-05-04 14:49:28 -070034 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070035 * @hide Range of uids allocated for a user.
Amith Yamasani742a6712011-05-04 14:49:28 -070036 */
Andrei Onea24ec3212019-03-15 17:35:05 +000037 @UnsupportedAppUsage
Amith Yamasani742a6712011-05-04 14:49:28 -070038 public static final int PER_USER_RANGE = 100000;
39
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070040 /** @hide A user id to indicate all users on the device */
Andrei Onea24ec3212019-03-15 17:35:05 +000041 @UnsupportedAppUsage
Makoto Onukiacf38582019-10-04 11:53:42 -070042 @TestApi
Jeff Sharkeycd654482016-01-08 17:42:11 -070043 public static final @UserIdInt int USER_ALL = -1;
Amith Yamasani742a6712011-05-04 14:49:28 -070044
Dianne Hackborn7767eac2012-08-23 18:25:40 -070045 /** @hide A user handle to indicate all users on the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060046 @SystemApi
47 @TestApi
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070048 public static final @NonNull UserHandle ALL = new UserHandle(USER_ALL);
Dianne Hackborn7767eac2012-08-23 18:25:40 -070049
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070050 /** @hide A user id to indicate the currently active user */
Andrei Onea24ec3212019-03-15 17:35:05 +000051 @UnsupportedAppUsage
Jeff Sharkeycd654482016-01-08 17:42:11 -070052 public static final @UserIdInt int USER_CURRENT = -2;
Amith Yamasani82644082012-08-03 13:09:11 -070053
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070054 /** @hide A user handle to indicate the current user of the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060055 @SystemApi
56 @TestApi
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070057 public static final @NonNull UserHandle CURRENT = new UserHandle(USER_CURRENT);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070058
59 /** @hide A user id to indicate that we would like to send to the current
60 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080061 * to the caller's user instead of failing with a security exception */
Andrei Onea24ec3212019-03-15 17:35:05 +000062 @UnsupportedAppUsage
Jeff Sharkeycd654482016-01-08 17:42:11 -070063 public static final @UserIdInt int USER_CURRENT_OR_SELF = -3;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070064
65 /** @hide A user handle to indicate that we would like to send to the current
66 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080067 * to the caller's user instead of failing with a security exception */
Andrei Onea24ec3212019-03-15 17:35:05 +000068 @UnsupportedAppUsage
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070069 public static final @NonNull UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070070
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070071 /** @hide An undefined user id */
Mathew Inwood3a75f262019-06-27 12:47:38 +010072 @UnsupportedAppUsage
Makoto Onukiacf38582019-10-04 11:53:42 -070073 @TestApi
Jeff Sharkeycd654482016-01-08 17:42:11 -070074 public static final @UserIdInt int USER_NULL = -10000;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070075
Makoto Onukiacf38582019-10-04 11:53:42 -070076 private static final @NonNull UserHandle NULL = new UserHandle(USER_NULL);
77
Xiaohui Chen70f6c382015-04-28 14:21:43 -070078 /**
79 * @hide A user id constant to indicate the "owner" user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070080 * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or
81 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070082 */
Andrei Onea24ec3212019-03-15 17:35:05 +000083 @UnsupportedAppUsage
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -070084 @Deprecated
Jeff Sharkeycd654482016-01-08 17:42:11 -070085 public static final @UserIdInt int USER_OWNER = 0;
Amith Yamasani82644082012-08-03 13:09:11 -070086
Xiaohui Chen70f6c382015-04-28 14:21:43 -070087 /**
88 * @hide A user handle to indicate the primary/owner user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070089 * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or
90 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070091 */
Andrei Onea24ec3212019-03-15 17:35:05 +000092 @UnsupportedAppUsage
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -070093 @Deprecated
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070094 public static final @NonNull UserHandle OWNER = new UserHandle(USER_OWNER);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070095
Xiaohui Chen70f6c382015-04-28 14:21:43 -070096 /** @hide A user id constant to indicate the "system" user of the device */
Andrei Onea24ec3212019-03-15 17:35:05 +000097 @UnsupportedAppUsage
Makoto Onukiacf38582019-10-04 11:53:42 -070098 @TestApi
Jeff Sharkeycd654482016-01-08 17:42:11 -070099 public static final @UserIdInt int USER_SYSTEM = 0;
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700100
Jeff Sharkey47f71082016-02-01 17:03:54 -0700101 /** @hide A user serial constant to indicate the "system" user of the device */
Andrei Onea24ec3212019-03-15 17:35:05 +0000102 @UnsupportedAppUsage
Jeff Sharkey47f71082016-02-01 17:03:54 -0700103 public static final int USER_SERIAL_SYSTEM = 0;
104
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700105 /** @hide A user handle to indicate the "system" user of the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -0600106 @SystemApi
Philip P. Moltmannf80809f2018-04-04 11:20:44 -0700107 @TestApi
Jeff Sharkeyba4acd52019-03-05 19:46:54 -0700108 public static final @NonNull UserHandle SYSTEM = new UserHandle(USER_SYSTEM);
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700109
Amith Yamasani742a6712011-05-04 14:49:28 -0700110 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700111 * @hide Enable multi-user related side effects. Set this to false if
112 * there are problems with single user use-cases.
113 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000114 @UnsupportedAppUsage
Amith Yamasani742a6712011-05-04 14:49:28 -0700115 public static final boolean MU_ENABLED = true;
116
Jeff Sharkey77a34502017-11-09 17:40:57 -0700117 /** @hide */
Makoto Onukiacf38582019-10-04 11:53:42 -0700118 @TestApi
119 public static final int MIN_SECONDARY_USER_ID = 10;
120
121 /**
122 * Arbitrary user handle cache size. We use the cache even when {@link #MU_ENABLED} is false
123 * anyway, so we can always assume in CTS that UserHandle.of(10) returns a cached instance
124 * even on non-multiuser devices.
125 */
126 private static final int NUM_CACHED_USERS = 4;
127
128 private static final UserHandle[] CACHED_USER_INFOS = new UserHandle[NUM_CACHED_USERS];
129
130 static {
131 // Not lazily initializing the cache, so that we can share them across processes.
132 // (We'll create them in zygote.)
133 for (int i = 0; i < CACHED_USER_INFOS.length; i++) {
134 CACHED_USER_INFOS[i] = new UserHandle(MIN_SECONDARY_USER_ID + i);
135 }
136 }
137
138 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000139 @UnsupportedAppUsage
Jeff Sharkey77a34502017-11-09 17:40:57 -0700140 public static final int ERR_GID = -1;
141 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000142 @UnsupportedAppUsage
Jeff Sharkey77a34502017-11-09 17:40:57 -0700143 public static final int AID_ROOT = android.os.Process.ROOT_UID;
144 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000145 @UnsupportedAppUsage
Jeff Sharkey77a34502017-11-09 17:40:57 -0700146 public static final int AID_APP_START = android.os.Process.FIRST_APPLICATION_UID;
147 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000148 @UnsupportedAppUsage
Jeff Sharkey77a34502017-11-09 17:40:57 -0700149 public static final int AID_APP_END = android.os.Process.LAST_APPLICATION_UID;
150 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000151 @UnsupportedAppUsage
Jeff Sharkey77a34502017-11-09 17:40:57 -0700152 public static final int AID_SHARED_GID_START = android.os.Process.FIRST_SHARED_APPLICATION_GID;
153 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000154 @UnsupportedAppUsage
Jeff Sharkey77a34502017-11-09 17:40:57 -0700155 public static final int AID_CACHE_GID_START = android.os.Process.FIRST_APPLICATION_CACHE_GID;
156
Bookatzf56f2582019-09-04 16:06:41 -0700157 /** The userId represented by this UserHandle. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000158 @UnsupportedAppUsage
Bookatzf56f2582019-09-04 16:06:41 -0700159 final @UserIdInt int mHandle;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700160
Amith Yamasani742a6712011-05-04 14:49:28 -0700161 /**
162 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
163 * user.
164 * @hide
165 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700166 public static boolean isSameUser(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700167 return getUserId(uid1) == getUserId(uid2);
168 }
169
170 /**
171 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
172 * uids.
173 * @param uid1 uid to compare
174 * @param uid2 other uid to compare
175 * @return whether the appId is the same for both uids
176 * @hide
177 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000178 @UnsupportedAppUsage
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700179 public static boolean isSameApp(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700180 return getAppId(uid1) == getAppId(uid2);
181 }
182
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800183 /**
184 * Whether a UID is an "isolated" UID.
185 * @hide
186 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000187 @UnsupportedAppUsage
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700188 public static boolean isIsolated(int uid) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700189 if (uid > 0) {
Martijn Coenen01e719b2018-12-05 16:01:38 +0100190 return Process.isIsolated(uid);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700191 } else {
192 return false;
193 }
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800194 }
195
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800196 /**
197 * Whether a UID belongs to a regular app. *Note* "Not a regular app" does not mean
198 * "it's system", because of isolated UIDs. Use {@link #isCore} for that.
199 * @hide
200 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000201 @UnsupportedAppUsage
Philip P. Moltmann33115152018-04-11 13:39:36 -0700202 @TestApi
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700203 public static boolean isApp(int uid) {
204 if (uid > 0) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700205 final int appId = getAppId(uid);
206 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID;
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700207 } else {
208 return false;
209 }
210 }
211
Amith Yamasani742a6712011-05-04 14:49:28 -0700212 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800213 * Whether a UID belongs to a system core component or not.
214 * @hide
215 */
216 public static boolean isCore(int uid) {
Makoto Onukicbc3cce2018-03-28 16:49:47 -0700217 if (uid >= 0) {
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800218 final int appId = getAppId(uid);
219 return appId < Process.FIRST_APPLICATION_UID;
220 } else {
221 return false;
222 }
223 }
224
225 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800226 * Returns the user for a given uid.
227 * @param uid A uid for an application running in a particular user.
228 * @return A {@link UserHandle} for that user.
229 */
230 public static UserHandle getUserHandleForUid(int uid) {
231 return of(getUserId(uid));
232 }
233
234 /**
Amith Yamasani742a6712011-05-04 14:49:28 -0700235 * Returns the user id for a given uid.
236 * @hide
237 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000238 @UnsupportedAppUsage
Makoto Onukiacf38582019-10-04 11:53:42 -0700239 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700240 public static @UserIdInt int getUserId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700241 if (MU_ENABLED) {
242 return uid / PER_USER_RANGE;
243 } else {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700244 return UserHandle.USER_SYSTEM;
Amith Yamasani742a6712011-05-04 14:49:28 -0700245 }
246 }
247
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700248 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000249 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700250 public static @UserIdInt int getCallingUserId() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800251 return getUserId(Binder.getCallingUid());
252 }
253
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100254 /** @hide */
Jeff Sharkeyb74799882017-07-28 16:55:41 -0600255 public static @AppIdInt int getCallingAppId() {
256 return getAppId(Binder.getCallingUid());
257 }
258
259 /** @hide */
Makoto Onukiacf38582019-10-04 11:53:42 -0700260 @TestApi
Fyodor Kupolov940e8572016-01-26 12:03:51 -0800261 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700262 public static UserHandle of(@UserIdInt int userId) {
Makoto Onukiacf38582019-10-04 11:53:42 -0700263 if (userId == USER_SYSTEM) {
264 return SYSTEM; // Most common.
265 }
266 // These are sequential; so use a switch. Maybe they'll be optimized to a table lookup.
267 switch (userId) {
268 case USER_ALL:
269 return ALL;
270
271 case USER_CURRENT:
272 return CURRENT;
273
274 case USER_CURRENT_OR_SELF:
275 return CURRENT_OR_SELF;
276 }
277 if (userId >= MIN_SECONDARY_USER_ID
278 && userId < (MIN_SECONDARY_USER_ID + CACHED_USER_INFOS.length)) {
279 return CACHED_USER_INFOS[userId - MIN_SECONDARY_USER_ID];
280 }
281 if (userId == USER_NULL) { // Not common.
282 return NULL;
283 }
284 return new UserHandle(userId);
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100285 }
286
Amith Yamasani742a6712011-05-04 14:49:28 -0700287 /**
288 * Returns the uid that is composed from the userId and the appId.
289 * @hide
290 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000291 @UnsupportedAppUsage
Makoto Onukiacf38582019-10-04 11:53:42 -0700292 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700293 public static int getUid(@UserIdInt int userId, @AppIdInt int appId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700294 if (MU_ENABLED) {
295 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
296 } else {
297 return appId;
298 }
299 }
300
301 /**
302 * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
303 * @hide
304 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -0700305 @TestApi
Remi NGUYEN VAN9e70a422019-01-31 12:36:54 +0900306 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700307 public static @AppIdInt int getAppId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700308 return uid % PER_USER_RANGE;
309 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700310
311 /**
Robin Leee66b6892014-04-29 12:36:47 +0100312 * Returns the gid shared between all apps with this userId.
313 * @hide
314 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700315 public static int getUserGid(@UserIdInt int userId) {
Robin Leee66b6892014-04-29 12:36:47 +0100316 return getUid(userId, Process.SHARED_USER_GID);
317 }
318
Jeff Sharkey77a34502017-11-09 17:40:57 -0700319 /** @hide */
320 public static int getSharedAppGid(int uid) {
321 return getSharedAppGid(getUserId(uid), getAppId(uid));
322 }
323
324 /** @hide */
Bookatzf56f2582019-09-04 16:06:41 -0700325 public static int getSharedAppGid(@UserIdInt int userId, @AppIdInt int appId) {
Jeff Sharkey77a34502017-11-09 17:40:57 -0700326 if (appId >= AID_APP_START && appId <= AID_APP_END) {
327 return (appId - AID_APP_START) + AID_SHARED_GID_START;
328 } else if (appId >= AID_ROOT && appId <= AID_APP_START) {
329 return appId;
330 } else {
331 return -1;
332 }
Kenny Roote091f222012-09-11 15:01:26 -0700333 }
334
335 /**
Adam Lesinski621e06a2015-08-13 22:06:31 -0700336 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid.
Adam Lesinskif4216d32015-06-26 16:33:24 -0700337 * @hide
338 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000339 @UnsupportedAppUsage
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700340 public static @AppIdInt int getAppIdFromSharedAppGid(int gid) {
Adam Lesinski621e06a2015-08-13 22:06:31 -0700341 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID
342 - Process.FIRST_SHARED_APPLICATION_GID;
343 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) {
344 return -1;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700345 }
Adam Lesinski621e06a2015-08-13 22:06:31 -0700346 return appId;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700347 }
348
Jeff Sharkey77a34502017-11-09 17:40:57 -0700349 /** @hide */
350 public static int getCacheAppGid(int uid) {
351 return getCacheAppGid(getUserId(uid), getAppId(uid));
352 }
353
354 /** @hide */
Bookatzf56f2582019-09-04 16:06:41 -0700355 public static int getCacheAppGid(@UserIdInt int userId, @AppIdInt int appId) {
Jeff Sharkey77a34502017-11-09 17:40:57 -0700356 if (appId >= AID_APP_START && appId <= AID_APP_END) {
357 return getUid(userId, (appId - AID_APP_START) + AID_CACHE_GID_START);
358 } else {
359 return -1;
360 }
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700361 }
362
363 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800364 * Generate a text representation of the uid, breaking out its individual
365 * components -- user, app, isolated, etc.
366 * @hide
367 */
368 public static void formatUid(StringBuilder sb, int uid) {
369 if (uid < Process.FIRST_APPLICATION_UID) {
370 sb.append(uid);
371 } else {
372 sb.append('u');
373 sb.append(getUserId(uid));
374 final int appId = getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100375 if (isIsolated(appId)) {
376 if (appId > Process.FIRST_ISOLATED_UID) {
377 sb.append('i');
378 sb.append(appId - Process.FIRST_ISOLATED_UID);
379 } else {
380 sb.append("ai");
381 sb.append(appId - Process.FIRST_APP_ZYGOTE_ISOLATED_UID);
382 }
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700383 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800384 sb.append('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700385 sb.append(appId - Process.FIRST_APPLICATION_UID);
386 } else {
387 sb.append('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800388 sb.append(appId);
389 }
390 }
391 }
392
393 /**
394 * Generate a text representation of the uid, breaking out its individual
395 * components -- user, app, isolated, etc.
Kweku Adams835283f2019-11-20 14:41:22 -0800396 *
397 * @param uid The uid to format
398 * @return A string representing the UID with its individual components broken out
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800399 * @hide
400 */
Kweku Adams835283f2019-11-20 14:41:22 -0800401 @SystemApi
402 @NonNull
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700403 public static String formatUid(int uid) {
404 StringBuilder sb = new StringBuilder();
405 formatUid(sb, uid);
406 return sb.toString();
407 }
408
409 /**
410 * Generate a text representation of the uid, breaking out its individual
411 * components -- user, app, isolated, etc.
412 * @hide
413 */
Artur Satayev70507ed2019-07-29 13:18:27 +0100414 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800415 public static void formatUid(PrintWriter pw, int uid) {
416 if (uid < Process.FIRST_APPLICATION_UID) {
417 pw.print(uid);
418 } else {
419 pw.print('u');
420 pw.print(getUserId(uid));
421 final int appId = getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100422 if (isIsolated(appId)) {
423 if (appId > Process.FIRST_ISOLATED_UID) {
424 pw.print('i');
425 pw.print(appId - Process.FIRST_ISOLATED_UID);
426 } else {
427 pw.print("ai");
428 pw.print(appId - Process.FIRST_APP_ZYGOTE_ISOLATED_UID);
429 }
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700430 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800431 pw.print('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700432 pw.print(appId - Process.FIRST_APPLICATION_UID);
433 } else {
434 pw.print('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800435 pw.print(appId);
436 }
437 }
438 }
439
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800440 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700441 public static @UserIdInt int parseUserArg(String arg) {
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800442 int userId;
443 if ("all".equals(arg)) {
444 userId = UserHandle.USER_ALL;
445 } else if ("current".equals(arg) || "cur".equals(arg)) {
446 userId = UserHandle.USER_CURRENT;
447 } else {
448 try {
449 userId = Integer.parseInt(arg);
450 } catch (NumberFormatException e) {
451 throw new IllegalArgumentException("Bad user number: " + arg);
452 }
453 }
454 return userId;
455 }
456
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800457 /**
Amith Yamasani483f3b02012-03-13 16:08:00 -0700458 * Returns the user id of the current process
459 * @return user id of the current process
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700460 * @hide
Amith Yamasani483f3b02012-03-13 16:08:00 -0700461 */
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700462 @SystemApi
Makoto Onukiacf38582019-10-04 11:53:42 -0700463 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700464 public static @UserIdInt int myUserId() {
Andreas Gamped281b422016-07-08 03:50:27 +0000465 return getUserId(Process.myUid());
Amith Yamasani483f3b02012-03-13 16:08:00 -0700466 }
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700467
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700468 /**
469 * Returns true if this UserHandle refers to the owner user; false otherwise.
470 * @return true if this UserHandle refers to the owner user; false otherwise.
471 * @hide
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700472 * @deprecated please use {@link #isSystem()} or check for
473 * {@link android.content.pm.UserInfo#isPrimary()}
474 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case.
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700475 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700476 @Deprecated
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700477 @SystemApi
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700478 public boolean isOwner() {
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700479 return this.equals(OWNER);
480 }
481
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700482 /**
483 * @return true if this UserHandle refers to the system user; false otherwise.
484 * @hide
485 */
486 @SystemApi
487 public boolean isSystem() {
488 return this.equals(SYSTEM);
489 }
490
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700491 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000492 @UnsupportedAppUsage
Bookatzf56f2582019-09-04 16:06:41 -0700493 public UserHandle(@UserIdInt int userId) {
494 mHandle = userId;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700495 }
496
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700497 /**
498 * Returns the userId stored in this UserHandle.
499 * @hide
500 */
501 @SystemApi
Makoto Onukicb8936e2017-04-10 15:37:17 -0700502 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700503 public @UserIdInt int getIdentifier() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700504 return mHandle;
505 }
506
507 @Override
508 public String toString() {
509 return "UserHandle{" + mHandle + "}";
510 }
511
512 @Override
513 public boolean equals(Object obj) {
514 try {
515 if (obj != null) {
516 UserHandle other = (UserHandle)obj;
517 return mHandle == other.mHandle;
518 }
519 } catch (ClassCastException e) {
520 }
521 return false;
522 }
523
524 @Override
525 public int hashCode() {
526 return mHandle;
527 }
Kweku Adams835283f2019-11-20 14:41:22 -0800528
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700529 public int describeContents() {
530 return 0;
531 }
532
533 public void writeToParcel(Parcel out, int flags) {
534 out.writeInt(mHandle);
535 }
536
537 /**
538 * Write a UserHandle to a Parcel, handling null pointers. Must be
539 * read with {@link #readFromParcel(Parcel)}.
Kweku Adams835283f2019-11-20 14:41:22 -0800540 *
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700541 * @param h The UserHandle to be written.
542 * @param out The Parcel in which the UserHandle will be placed.
Kweku Adams835283f2019-11-20 14:41:22 -0800543 *
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700544 * @see #readFromParcel(Parcel)
545 */
546 public static void writeToParcel(UserHandle h, Parcel out) {
547 if (h != null) {
548 h.writeToParcel(out, 0);
549 } else {
550 out.writeInt(USER_NULL);
551 }
552 }
Kweku Adams835283f2019-11-20 14:41:22 -0800553
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700554 /**
555 * Read a UserHandle from a Parcel that was previously written
556 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
557 * a null or new object as appropriate.
Kweku Adams835283f2019-11-20 14:41:22 -0800558 *
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700559 * @param in The Parcel from which to read the UserHandle
560 * @return Returns a new UserHandle matching the previously written
561 * object, or null if a null had been written.
Kweku Adams835283f2019-11-20 14:41:22 -0800562 *
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700563 * @see #writeToParcel(UserHandle, Parcel)
564 */
565 public static UserHandle readFromParcel(Parcel in) {
566 int h = in.readInt();
567 return h != USER_NULL ? new UserHandle(h) : null;
568 }
Kweku Adams835283f2019-11-20 14:41:22 -0800569
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700570 public static final @android.annotation.NonNull Parcelable.Creator<UserHandle> CREATOR
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700571 = new Parcelable.Creator<UserHandle>() {
572 public UserHandle createFromParcel(Parcel in) {
Makoto Onukiacf38582019-10-04 11:53:42 -0700573 // Try to avoid allocation; use of() here. Keep this and the constructor below
574 // in sync.
575 return UserHandle.of(in.readInt());
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700576 }
577
578 public UserHandle[] newArray(int size) {
579 return new UserHandle[size];
580 }
581 };
582
583 /**
584 * Instantiate a new UserHandle from the data in a Parcel that was
585 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you
586 * must not use this with data written by
587 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
588 * to handle a null UserHandle here.
Kweku Adams835283f2019-11-20 14:41:22 -0800589 *
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700590 * @param in The Parcel containing the previously written UserHandle,
591 * positioned at the location in the buffer where it was written.
592 */
593 public UserHandle(Parcel in) {
Makoto Onukiacf38582019-10-04 11:53:42 -0700594 mHandle = in.readInt(); // Keep this and createFromParcel() in sync.
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700595 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700596}