blob: 286185b8bda5d824c38156648077c6b90a9116fa [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;
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +010024
Dianne Hackborna06de0f2012-12-11 16:34:47 -080025import java.io.PrintWriter;
26
Amith Yamasani742a6712011-05-04 14:49:28 -070027/**
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070028 * Representation of a user on the device.
Amith Yamasani742a6712011-05-04 14:49:28 -070029 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070030public final class UserHandle implements Parcelable {
Jeff Sharkey77a34502017-11-09 17:40:57 -070031 // NOTE: keep logic in sync with system/core/libcutils/multiuser.c
32
Amith Yamasani742a6712011-05-04 14:49:28 -070033 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070034 * @hide Range of uids allocated for a user.
Amith Yamasani742a6712011-05-04 14:49:28 -070035 */
36 public static final int PER_USER_RANGE = 100000;
37
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070038 /** @hide A user id to indicate all users on the device */
Jeff Sharkeycd654482016-01-08 17:42:11 -070039 public static final @UserIdInt int USER_ALL = -1;
Amith Yamasani742a6712011-05-04 14:49:28 -070040
Dianne Hackborn7767eac2012-08-23 18:25:40 -070041 /** @hide A user handle to indicate all users on the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060042 @SystemApi
43 @TestApi
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070044 public static final @NonNull UserHandle ALL = new UserHandle(USER_ALL);
Dianne Hackborn7767eac2012-08-23 18:25:40 -070045
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070046 /** @hide A user id to indicate the currently active user */
Jeff Sharkeycd654482016-01-08 17:42:11 -070047 public static final @UserIdInt int USER_CURRENT = -2;
Amith Yamasani82644082012-08-03 13:09:11 -070048
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070049 /** @hide A user handle to indicate the current user of the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060050 @SystemApi
51 @TestApi
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070052 public static final @NonNull UserHandle CURRENT = new UserHandle(USER_CURRENT);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070053
54 /** @hide A user id to indicate that we would like to send to the current
55 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080056 * to the caller's user instead of failing with a security exception */
Jeff Sharkeycd654482016-01-08 17:42:11 -070057 public static final @UserIdInt int USER_CURRENT_OR_SELF = -3;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070058
59 /** @hide A user handle 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 */
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070062 public static final @NonNull UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070063
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070064 /** @hide An undefined user id */
Jeff Sharkeycd654482016-01-08 17:42:11 -070065 public static final @UserIdInt int USER_NULL = -10000;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070066
Xiaohui Chen70f6c382015-04-28 14:21:43 -070067 /**
68 * @hide A user id constant to indicate the "owner" user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070069 * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or
70 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070071 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -070072 @Deprecated
Jeff Sharkeycd654482016-01-08 17:42:11 -070073 public static final @UserIdInt int USER_OWNER = 0;
Amith Yamasani82644082012-08-03 13:09:11 -070074
Xiaohui Chen70f6c382015-04-28 14:21:43 -070075 /**
76 * @hide A user handle to indicate the primary/owner user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070077 * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or
78 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070079 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -070080 @Deprecated
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070081 public static final @NonNull UserHandle OWNER = new UserHandle(USER_OWNER);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070082
Xiaohui Chen70f6c382015-04-28 14:21:43 -070083 /** @hide A user id constant to indicate the "system" user of the device */
Jeff Sharkeycd654482016-01-08 17:42:11 -070084 public static final @UserIdInt int USER_SYSTEM = 0;
Xiaohui Chen70f6c382015-04-28 14:21:43 -070085
Jeff Sharkey47f71082016-02-01 17:03:54 -070086 /** @hide A user serial constant to indicate the "system" user of the device */
87 public static final int USER_SERIAL_SYSTEM = 0;
88
Xiaohui Chen70f6c382015-04-28 14:21:43 -070089 /** @hide A user handle to indicate the "system" user of the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060090 @SystemApi
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070091 @TestApi
Jeff Sharkeyba4acd52019-03-05 19:46:54 -070092 public static final @NonNull UserHandle SYSTEM = new UserHandle(USER_SYSTEM);
Xiaohui Chen70f6c382015-04-28 14:21:43 -070093
Amith Yamasani742a6712011-05-04 14:49:28 -070094 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070095 * @hide Enable multi-user related side effects. Set this to false if
96 * there are problems with single user use-cases.
97 */
Amith Yamasani742a6712011-05-04 14:49:28 -070098 public static final boolean MU_ENABLED = true;
99
Jeff Sharkey77a34502017-11-09 17:40:57 -0700100 /** @hide */
101 public static final int ERR_GID = -1;
102 /** @hide */
103 public static final int AID_ROOT = android.os.Process.ROOT_UID;
104 /** @hide */
105 public static final int AID_APP_START = android.os.Process.FIRST_APPLICATION_UID;
106 /** @hide */
107 public static final int AID_APP_END = android.os.Process.LAST_APPLICATION_UID;
108 /** @hide */
109 public static final int AID_SHARED_GID_START = android.os.Process.FIRST_SHARED_APPLICATION_GID;
110 /** @hide */
111 public static final int AID_CACHE_GID_START = android.os.Process.FIRST_APPLICATION_CACHE_GID;
112
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700113 final int mHandle;
114
Amith Yamasani742a6712011-05-04 14:49:28 -0700115 /**
116 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
117 * user.
118 * @hide
119 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700120 public static boolean isSameUser(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700121 return getUserId(uid1) == getUserId(uid2);
122 }
123
124 /**
125 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
126 * uids.
127 * @param uid1 uid to compare
128 * @param uid2 other uid to compare
129 * @return whether the appId is the same for both uids
130 * @hide
131 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700132 public static boolean isSameApp(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700133 return getAppId(uid1) == getAppId(uid2);
134 }
135
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800136 /**
137 * Whether a UID is an "isolated" UID.
138 * @hide
139 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700140 public static boolean isIsolated(int uid) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700141 if (uid > 0) {
Martijn Coenen01e719b2018-12-05 16:01:38 +0100142 return Process.isIsolated(uid);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700143 } else {
144 return false;
145 }
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800146 }
147
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800148 /**
149 * Whether a UID belongs to a regular app. *Note* "Not a regular app" does not mean
150 * "it's system", because of isolated UIDs. Use {@link #isCore} for that.
151 * @hide
152 */
Philip P. Moltmann33115152018-04-11 13:39:36 -0700153 @TestApi
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700154 public static boolean isApp(int uid) {
155 if (uid > 0) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700156 final int appId = getAppId(uid);
157 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID;
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700158 } else {
159 return false;
160 }
161 }
162
Amith Yamasani742a6712011-05-04 14:49:28 -0700163 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800164 * Whether a UID belongs to a system core component or not.
165 * @hide
166 */
167 public static boolean isCore(int uid) {
Makoto Onukicbc3cce2018-03-28 16:49:47 -0700168 if (uid >= 0) {
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800169 final int appId = getAppId(uid);
170 return appId < Process.FIRST_APPLICATION_UID;
171 } else {
172 return false;
173 }
174 }
175
176 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800177 * Returns the user for a given uid.
178 * @param uid A uid for an application running in a particular user.
179 * @return A {@link UserHandle} for that user.
180 */
181 public static UserHandle getUserHandleForUid(int uid) {
182 return of(getUserId(uid));
183 }
184
185 /**
Amith Yamasani742a6712011-05-04 14:49:28 -0700186 * Returns the user id for a given uid.
187 * @hide
188 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700189 public static @UserIdInt int getUserId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700190 if (MU_ENABLED) {
191 return uid / PER_USER_RANGE;
192 } else {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700193 return UserHandle.USER_SYSTEM;
Amith Yamasani742a6712011-05-04 14:49:28 -0700194 }
195 }
196
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700197 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700198 public static @UserIdInt int getCallingUserId() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800199 return getUserId(Binder.getCallingUid());
200 }
201
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100202 /** @hide */
Jeff Sharkeyb74799882017-07-28 16:55:41 -0600203 public static @AppIdInt int getCallingAppId() {
204 return getAppId(Binder.getCallingUid());
205 }
206
207 /** @hide */
Fyodor Kupolov940e8572016-01-26 12:03:51 -0800208 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700209 public static UserHandle of(@UserIdInt int userId) {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700210 return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId);
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100211 }
212
Amith Yamasani742a6712011-05-04 14:49:28 -0700213 /**
214 * Returns the uid that is composed from the userId and the appId.
215 * @hide
216 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700217 public static int getUid(@UserIdInt int userId, @AppIdInt int appId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700218 if (MU_ENABLED) {
219 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
220 } else {
221 return appId;
222 }
223 }
224
225 /**
226 * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
227 * @hide
228 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -0700229 @TestApi
Remi NGUYEN VAN9e70a422019-01-31 12:36:54 +0900230 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700231 public static @AppIdInt int getAppId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700232 return uid % PER_USER_RANGE;
233 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700234
235 /**
Robin Leee66b6892014-04-29 12:36:47 +0100236 * Returns the gid shared between all apps with this userId.
237 * @hide
238 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700239 public static int getUserGid(@UserIdInt int userId) {
Robin Leee66b6892014-04-29 12:36:47 +0100240 return getUid(userId, Process.SHARED_USER_GID);
241 }
242
Jeff Sharkey77a34502017-11-09 17:40:57 -0700243 /** @hide */
244 public static int getSharedAppGid(int uid) {
245 return getSharedAppGid(getUserId(uid), getAppId(uid));
246 }
247
248 /** @hide */
249 public static int getSharedAppGid(int userId, int appId) {
250 if (appId >= AID_APP_START && appId <= AID_APP_END) {
251 return (appId - AID_APP_START) + AID_SHARED_GID_START;
252 } else if (appId >= AID_ROOT && appId <= AID_APP_START) {
253 return appId;
254 } else {
255 return -1;
256 }
Kenny Roote091f222012-09-11 15:01:26 -0700257 }
258
259 /**
Adam Lesinski621e06a2015-08-13 22:06:31 -0700260 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid.
Adam Lesinskif4216d32015-06-26 16:33:24 -0700261 * @hide
262 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700263 public static @AppIdInt int getAppIdFromSharedAppGid(int gid) {
Adam Lesinski621e06a2015-08-13 22:06:31 -0700264 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID
265 - Process.FIRST_SHARED_APPLICATION_GID;
266 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) {
267 return -1;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700268 }
Adam Lesinski621e06a2015-08-13 22:06:31 -0700269 return appId;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700270 }
271
Jeff Sharkey77a34502017-11-09 17:40:57 -0700272 /** @hide */
273 public static int getCacheAppGid(int uid) {
274 return getCacheAppGid(getUserId(uid), getAppId(uid));
275 }
276
277 /** @hide */
278 public static int getCacheAppGid(int userId, int appId) {
279 if (appId >= AID_APP_START && appId <= AID_APP_END) {
280 return getUid(userId, (appId - AID_APP_START) + AID_CACHE_GID_START);
281 } else {
282 return -1;
283 }
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700284 }
285
286 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800287 * Generate a text representation of the uid, breaking out its individual
288 * components -- user, app, isolated, etc.
289 * @hide
290 */
291 public static void formatUid(StringBuilder sb, int uid) {
292 if (uid < Process.FIRST_APPLICATION_UID) {
293 sb.append(uid);
294 } else {
295 sb.append('u');
296 sb.append(getUserId(uid));
297 final int appId = getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100298 if (isIsolated(appId)) {
299 if (appId > Process.FIRST_ISOLATED_UID) {
300 sb.append('i');
301 sb.append(appId - Process.FIRST_ISOLATED_UID);
302 } else {
303 sb.append("ai");
304 sb.append(appId - Process.FIRST_APP_ZYGOTE_ISOLATED_UID);
305 }
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700306 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800307 sb.append('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700308 sb.append(appId - Process.FIRST_APPLICATION_UID);
309 } else {
310 sb.append('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800311 sb.append(appId);
312 }
313 }
314 }
315
316 /**
317 * Generate a text representation of the uid, breaking out its individual
318 * components -- user, app, isolated, etc.
319 * @hide
320 */
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700321 public static String formatUid(int uid) {
322 StringBuilder sb = new StringBuilder();
323 formatUid(sb, uid);
324 return sb.toString();
325 }
326
327 /**
328 * Generate a text representation of the uid, breaking out its individual
329 * components -- user, app, isolated, etc.
330 * @hide
331 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800332 public static void formatUid(PrintWriter pw, int uid) {
333 if (uid < Process.FIRST_APPLICATION_UID) {
334 pw.print(uid);
335 } else {
336 pw.print('u');
337 pw.print(getUserId(uid));
338 final int appId = getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100339 if (isIsolated(appId)) {
340 if (appId > Process.FIRST_ISOLATED_UID) {
341 pw.print('i');
342 pw.print(appId - Process.FIRST_ISOLATED_UID);
343 } else {
344 pw.print("ai");
345 pw.print(appId - Process.FIRST_APP_ZYGOTE_ISOLATED_UID);
346 }
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700347 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800348 pw.print('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700349 pw.print(appId - Process.FIRST_APPLICATION_UID);
350 } else {
351 pw.print('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800352 pw.print(appId);
353 }
354 }
355 }
356
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800357 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700358 public static @UserIdInt int parseUserArg(String arg) {
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800359 int userId;
360 if ("all".equals(arg)) {
361 userId = UserHandle.USER_ALL;
362 } else if ("current".equals(arg) || "cur".equals(arg)) {
363 userId = UserHandle.USER_CURRENT;
364 } else {
365 try {
366 userId = Integer.parseInt(arg);
367 } catch (NumberFormatException e) {
368 throw new IllegalArgumentException("Bad user number: " + arg);
369 }
370 }
371 return userId;
372 }
373
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800374 /**
Amith Yamasani483f3b02012-03-13 16:08:00 -0700375 * Returns the user id of the current process
376 * @return user id of the current process
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700377 * @hide
Amith Yamasani483f3b02012-03-13 16:08:00 -0700378 */
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700379 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700380 public static @UserIdInt int myUserId() {
Andreas Gamped281b422016-07-08 03:50:27 +0000381 return getUserId(Process.myUid());
Amith Yamasani483f3b02012-03-13 16:08:00 -0700382 }
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700383
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700384 /**
385 * Returns true if this UserHandle refers to the owner user; false otherwise.
386 * @return true if this UserHandle refers to the owner user; false otherwise.
387 * @hide
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700388 * @deprecated please use {@link #isSystem()} or check for
389 * {@link android.content.pm.UserInfo#isPrimary()}
390 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case.
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700391 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700392 @Deprecated
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700393 @SystemApi
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700394 public boolean isOwner() {
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700395 return this.equals(OWNER);
396 }
397
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700398 /**
399 * @return true if this UserHandle refers to the system user; false otherwise.
400 * @hide
401 */
402 @SystemApi
403 public boolean isSystem() {
404 return this.equals(SYSTEM);
405 }
406
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700407 /** @hide */
408 public UserHandle(int h) {
409 mHandle = h;
410 }
411
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700412 /**
413 * Returns the userId stored in this UserHandle.
414 * @hide
415 */
416 @SystemApi
Makoto Onukicb8936e2017-04-10 15:37:17 -0700417 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700418 public @UserIdInt int getIdentifier() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700419 return mHandle;
420 }
421
422 @Override
423 public String toString() {
424 return "UserHandle{" + mHandle + "}";
425 }
426
427 @Override
428 public boolean equals(Object obj) {
429 try {
430 if (obj != null) {
431 UserHandle other = (UserHandle)obj;
432 return mHandle == other.mHandle;
433 }
434 } catch (ClassCastException e) {
435 }
436 return false;
437 }
438
439 @Override
440 public int hashCode() {
441 return mHandle;
442 }
443
444 public int describeContents() {
445 return 0;
446 }
447
448 public void writeToParcel(Parcel out, int flags) {
449 out.writeInt(mHandle);
450 }
451
452 /**
453 * Write a UserHandle to a Parcel, handling null pointers. Must be
454 * read with {@link #readFromParcel(Parcel)}.
455 *
456 * @param h The UserHandle to be written.
457 * @param out The Parcel in which the UserHandle will be placed.
458 *
459 * @see #readFromParcel(Parcel)
460 */
461 public static void writeToParcel(UserHandle h, Parcel out) {
462 if (h != null) {
463 h.writeToParcel(out, 0);
464 } else {
465 out.writeInt(USER_NULL);
466 }
467 }
468
469 /**
470 * Read a UserHandle from a Parcel that was previously written
471 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
472 * a null or new object as appropriate.
473 *
474 * @param in The Parcel from which to read the UserHandle
475 * @return Returns a new UserHandle matching the previously written
476 * object, or null if a null had been written.
477 *
478 * @see #writeToParcel(UserHandle, Parcel)
479 */
480 public static UserHandle readFromParcel(Parcel in) {
481 int h = in.readInt();
482 return h != USER_NULL ? new UserHandle(h) : null;
483 }
484
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700485 public static final @android.annotation.NonNull Parcelable.Creator<UserHandle> CREATOR
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700486 = new Parcelable.Creator<UserHandle>() {
487 public UserHandle createFromParcel(Parcel in) {
488 return new UserHandle(in);
489 }
490
491 public UserHandle[] newArray(int size) {
492 return new UserHandle[size];
493 }
494 };
495
496 /**
497 * Instantiate a new UserHandle from the data in a Parcel that was
498 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you
499 * must not use this with data written by
500 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
501 * to handle a null UserHandle here.
502 *
503 * @param in The Parcel containing the previously written UserHandle,
504 * positioned at the location in the buffer where it was written.
505 */
506 public UserHandle(Parcel in) {
507 mHandle = in.readInt();
508 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700509}