blob: f8feb7b4a693626eb9ade8625177bd38014dd7af [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;
Christopher Tatecbd7a6e2014-08-06 14:24:14 -070020import android.annotation.SystemApi;
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -070021import android.annotation.TestApi;
Jeff Sharkey8588bc12016-01-06 16:47:42 -070022import android.annotation.UserIdInt;
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +010023
Dianne Hackborna06de0f2012-12-11 16:34:47 -080024import java.io.PrintWriter;
25
Amith Yamasani742a6712011-05-04 14:49:28 -070026/**
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070027 * Representation of a user on the device.
Amith Yamasani742a6712011-05-04 14:49:28 -070028 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070029public final class UserHandle implements Parcelable {
Jeff Sharkey77a34502017-11-09 17:40:57 -070030 // NOTE: keep logic in sync with system/core/libcutils/multiuser.c
31
Amith Yamasani742a6712011-05-04 14:49:28 -070032 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070033 * @hide Range of uids allocated for a user.
Amith Yamasani742a6712011-05-04 14:49:28 -070034 */
35 public static final int PER_USER_RANGE = 100000;
36
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070037 /** @hide A user id to indicate all users on the device */
Jeff Sharkeycd654482016-01-08 17:42:11 -070038 public static final @UserIdInt int USER_ALL = -1;
Amith Yamasani742a6712011-05-04 14:49:28 -070039
Dianne Hackborn7767eac2012-08-23 18:25:40 -070040 /** @hide A user handle to indicate all users on the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060041 @SystemApi
42 @TestApi
Dianne Hackborn7767eac2012-08-23 18:25:40 -070043 public static final UserHandle ALL = new UserHandle(USER_ALL);
44
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070045 /** @hide A user id to indicate the currently active user */
Jeff Sharkeycd654482016-01-08 17:42:11 -070046 public static final @UserIdInt int USER_CURRENT = -2;
Amith Yamasani82644082012-08-03 13:09:11 -070047
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070048 /** @hide A user handle to indicate the current user of the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060049 @SystemApi
50 @TestApi
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070051 public static final UserHandle CURRENT = new UserHandle(USER_CURRENT);
52
53 /** @hide A user id to indicate that we would like to send to the current
54 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080055 * to the caller's user instead of failing with a security exception */
Jeff Sharkeycd654482016-01-08 17:42:11 -070056 public static final @UserIdInt int USER_CURRENT_OR_SELF = -3;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070057
58 /** @hide A user handle to indicate that we would like to send to the current
59 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080060 * to the caller's user instead of failing with a security exception */
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070061 public static final UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF);
62
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070063 /** @hide An undefined user id */
Jeff Sharkeycd654482016-01-08 17:42:11 -070064 public static final @UserIdInt int USER_NULL = -10000;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070065
Xiaohui Chen70f6c382015-04-28 14:21:43 -070066 /**
67 * @hide A user id constant to indicate the "owner" user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070068 * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or
69 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070070 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -070071 @Deprecated
Jeff Sharkeycd654482016-01-08 17:42:11 -070072 public static final @UserIdInt int USER_OWNER = 0;
Amith Yamasani82644082012-08-03 13:09:11 -070073
Xiaohui Chen70f6c382015-04-28 14:21:43 -070074 /**
75 * @hide A user handle to indicate the primary/owner user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070076 * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or
77 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070078 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -070079 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070080 public static final UserHandle OWNER = new UserHandle(USER_OWNER);
81
Xiaohui Chen70f6c382015-04-28 14:21:43 -070082 /** @hide A user id constant to indicate the "system" user of the device */
Jeff Sharkeycd654482016-01-08 17:42:11 -070083 public static final @UserIdInt int USER_SYSTEM = 0;
Xiaohui Chen70f6c382015-04-28 14:21:43 -070084
Jeff Sharkey47f71082016-02-01 17:03:54 -070085 /** @hide A user serial constant to indicate the "system" user of the device */
86 public static final int USER_SERIAL_SYSTEM = 0;
87
Xiaohui Chen70f6c382015-04-28 14:21:43 -070088 /** @hide A user handle to indicate the "system" user of the device */
Jeff Sharkeyec5f7d12018-08-08 09:15:04 -060089 @SystemApi
Philip P. Moltmannf80809f2018-04-04 11:20:44 -070090 @TestApi
Xiaohui Chen70f6c382015-04-28 14:21:43 -070091 public static final UserHandle SYSTEM = new UserHandle(USER_SYSTEM);
92
Amith Yamasani742a6712011-05-04 14:49:28 -070093 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070094 * @hide Enable multi-user related side effects. Set this to false if
95 * there are problems with single user use-cases.
96 */
Amith Yamasani742a6712011-05-04 14:49:28 -070097 public static final boolean MU_ENABLED = true;
98
Jeff Sharkey77a34502017-11-09 17:40:57 -070099 /** @hide */
100 public static final int ERR_GID = -1;
101 /** @hide */
102 public static final int AID_ROOT = android.os.Process.ROOT_UID;
103 /** @hide */
104 public static final int AID_APP_START = android.os.Process.FIRST_APPLICATION_UID;
105 /** @hide */
106 public static final int AID_APP_END = android.os.Process.LAST_APPLICATION_UID;
107 /** @hide */
108 public static final int AID_SHARED_GID_START = android.os.Process.FIRST_SHARED_APPLICATION_GID;
109 /** @hide */
110 public static final int AID_CACHE_GID_START = android.os.Process.FIRST_APPLICATION_CACHE_GID;
111
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700112 final int mHandle;
113
Amith Yamasani742a6712011-05-04 14:49:28 -0700114 /**
115 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
116 * user.
117 * @hide
118 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700119 public static boolean isSameUser(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700120 return getUserId(uid1) == getUserId(uid2);
121 }
122
123 /**
124 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
125 * uids.
126 * @param uid1 uid to compare
127 * @param uid2 other uid to compare
128 * @return whether the appId is the same for both uids
129 * @hide
130 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700131 public static boolean isSameApp(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700132 return getAppId(uid1) == getAppId(uid2);
133 }
134
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800135 /**
136 * Whether a UID is an "isolated" UID.
137 * @hide
138 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700139 public static boolean isIsolated(int uid) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700140 if (uid > 0) {
141 final int appId = getAppId(uid);
142 return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID;
143 } 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
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700230 public static @AppIdInt int getAppId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700231 return uid % PER_USER_RANGE;
232 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700233
234 /**
Robin Leee66b6892014-04-29 12:36:47 +0100235 * Returns the gid shared between all apps with this userId.
236 * @hide
237 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700238 public static int getUserGid(@UserIdInt int userId) {
Robin Leee66b6892014-04-29 12:36:47 +0100239 return getUid(userId, Process.SHARED_USER_GID);
240 }
241
Jeff Sharkey77a34502017-11-09 17:40:57 -0700242 /** @hide */
243 public static int getSharedAppGid(int uid) {
244 return getSharedAppGid(getUserId(uid), getAppId(uid));
245 }
246
247 /** @hide */
248 public static int getSharedAppGid(int userId, int appId) {
249 if (appId >= AID_APP_START && appId <= AID_APP_END) {
250 return (appId - AID_APP_START) + AID_SHARED_GID_START;
251 } else if (appId >= AID_ROOT && appId <= AID_APP_START) {
252 return appId;
253 } else {
254 return -1;
255 }
Kenny Roote091f222012-09-11 15:01:26 -0700256 }
257
258 /**
Adam Lesinski621e06a2015-08-13 22:06:31 -0700259 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid.
Adam Lesinskif4216d32015-06-26 16:33:24 -0700260 * @hide
261 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700262 public static @AppIdInt int getAppIdFromSharedAppGid(int gid) {
Adam Lesinski621e06a2015-08-13 22:06:31 -0700263 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID
264 - Process.FIRST_SHARED_APPLICATION_GID;
265 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) {
266 return -1;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700267 }
Adam Lesinski621e06a2015-08-13 22:06:31 -0700268 return appId;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700269 }
270
Jeff Sharkey77a34502017-11-09 17:40:57 -0700271 /** @hide */
272 public static int getCacheAppGid(int uid) {
273 return getCacheAppGid(getUserId(uid), getAppId(uid));
274 }
275
276 /** @hide */
277 public static int getCacheAppGid(int userId, int appId) {
278 if (appId >= AID_APP_START && appId <= AID_APP_END) {
279 return getUid(userId, (appId - AID_APP_START) + AID_CACHE_GID_START);
280 } else {
281 return -1;
282 }
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700283 }
284
285 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800286 * Generate a text representation of the uid, breaking out its individual
287 * components -- user, app, isolated, etc.
288 * @hide
289 */
290 public static void formatUid(StringBuilder sb, int uid) {
291 if (uid < Process.FIRST_APPLICATION_UID) {
292 sb.append(uid);
293 } else {
294 sb.append('u');
295 sb.append(getUserId(uid));
296 final int appId = getAppId(uid);
297 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
298 sb.append('i');
299 sb.append(appId - Process.FIRST_ISOLATED_UID);
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700300 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800301 sb.append('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700302 sb.append(appId - Process.FIRST_APPLICATION_UID);
303 } else {
304 sb.append('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800305 sb.append(appId);
306 }
307 }
308 }
309
310 /**
311 * Generate a text representation of the uid, breaking out its individual
312 * components -- user, app, isolated, etc.
313 * @hide
314 */
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700315 public static String formatUid(int uid) {
316 StringBuilder sb = new StringBuilder();
317 formatUid(sb, uid);
318 return sb.toString();
319 }
320
321 /**
322 * Generate a text representation of the uid, breaking out its individual
323 * components -- user, app, isolated, etc.
324 * @hide
325 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800326 public static void formatUid(PrintWriter pw, int uid) {
327 if (uid < Process.FIRST_APPLICATION_UID) {
328 pw.print(uid);
329 } else {
330 pw.print('u');
331 pw.print(getUserId(uid));
332 final int appId = getAppId(uid);
333 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
334 pw.print('i');
335 pw.print(appId - Process.FIRST_ISOLATED_UID);
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700336 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800337 pw.print('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700338 pw.print(appId - Process.FIRST_APPLICATION_UID);
339 } else {
340 pw.print('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800341 pw.print(appId);
342 }
343 }
344 }
345
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800346 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700347 public static @UserIdInt int parseUserArg(String arg) {
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800348 int userId;
349 if ("all".equals(arg)) {
350 userId = UserHandle.USER_ALL;
351 } else if ("current".equals(arg) || "cur".equals(arg)) {
352 userId = UserHandle.USER_CURRENT;
353 } else {
354 try {
355 userId = Integer.parseInt(arg);
356 } catch (NumberFormatException e) {
357 throw new IllegalArgumentException("Bad user number: " + arg);
358 }
359 }
360 return userId;
361 }
362
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800363 /**
Amith Yamasani483f3b02012-03-13 16:08:00 -0700364 * Returns the user id of the current process
365 * @return user id of the current process
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700366 * @hide
Amith Yamasani483f3b02012-03-13 16:08:00 -0700367 */
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700368 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700369 public static @UserIdInt int myUserId() {
Andreas Gamped281b422016-07-08 03:50:27 +0000370 return getUserId(Process.myUid());
Amith Yamasani483f3b02012-03-13 16:08:00 -0700371 }
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700372
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700373 /**
374 * Returns true if this UserHandle refers to the owner user; false otherwise.
375 * @return true if this UserHandle refers to the owner user; false otherwise.
376 * @hide
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700377 * @deprecated please use {@link #isSystem()} or check for
378 * {@link android.content.pm.UserInfo#isPrimary()}
379 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case.
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700380 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700381 @Deprecated
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700382 @SystemApi
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700383 public boolean isOwner() {
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700384 return this.equals(OWNER);
385 }
386
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700387 /**
388 * @return true if this UserHandle refers to the system user; false otherwise.
389 * @hide
390 */
391 @SystemApi
392 public boolean isSystem() {
393 return this.equals(SYSTEM);
394 }
395
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700396 /** @hide */
397 public UserHandle(int h) {
398 mHandle = h;
399 }
400
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700401 /**
402 * Returns the userId stored in this UserHandle.
403 * @hide
404 */
405 @SystemApi
Makoto Onukicb8936e2017-04-10 15:37:17 -0700406 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700407 public @UserIdInt int getIdentifier() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700408 return mHandle;
409 }
410
411 @Override
412 public String toString() {
413 return "UserHandle{" + mHandle + "}";
414 }
415
416 @Override
417 public boolean equals(Object obj) {
418 try {
419 if (obj != null) {
420 UserHandle other = (UserHandle)obj;
421 return mHandle == other.mHandle;
422 }
423 } catch (ClassCastException e) {
424 }
425 return false;
426 }
427
428 @Override
429 public int hashCode() {
430 return mHandle;
431 }
432
433 public int describeContents() {
434 return 0;
435 }
436
437 public void writeToParcel(Parcel out, int flags) {
438 out.writeInt(mHandle);
439 }
440
441 /**
442 * Write a UserHandle to a Parcel, handling null pointers. Must be
443 * read with {@link #readFromParcel(Parcel)}.
444 *
445 * @param h The UserHandle to be written.
446 * @param out The Parcel in which the UserHandle will be placed.
447 *
448 * @see #readFromParcel(Parcel)
449 */
450 public static void writeToParcel(UserHandle h, Parcel out) {
451 if (h != null) {
452 h.writeToParcel(out, 0);
453 } else {
454 out.writeInt(USER_NULL);
455 }
456 }
457
458 /**
459 * Read a UserHandle from a Parcel that was previously written
460 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
461 * a null or new object as appropriate.
462 *
463 * @param in The Parcel from which to read the UserHandle
464 * @return Returns a new UserHandle matching the previously written
465 * object, or null if a null had been written.
466 *
467 * @see #writeToParcel(UserHandle, Parcel)
468 */
469 public static UserHandle readFromParcel(Parcel in) {
470 int h = in.readInt();
471 return h != USER_NULL ? new UserHandle(h) : null;
472 }
473
474 public static final Parcelable.Creator<UserHandle> CREATOR
475 = new Parcelable.Creator<UserHandle>() {
476 public UserHandle createFromParcel(Parcel in) {
477 return new UserHandle(in);
478 }
479
480 public UserHandle[] newArray(int size) {
481 return new UserHandle[size];
482 }
483 };
484
485 /**
486 * Instantiate a new UserHandle from the data in a Parcel that was
487 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you
488 * must not use this with data written by
489 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
490 * to handle a null UserHandle here.
491 *
492 * @param in The Parcel containing the previously written UserHandle,
493 * positioned at the location in the buffer where it was written.
494 */
495 public UserHandle(Parcel in) {
496 mHandle = in.readInt();
497 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700498}