blob: 40c48a0746bbc452ec633cd4322dbec653dc1b6f [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) {
Martijn Coenen01e719b2018-12-05 16:01:38 +0100141 return Process.isIsolated(uid);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700142 } else {
143 return false;
144 }
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800145 }
146
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800147 /**
148 * Whether a UID belongs to a regular app. *Note* "Not a regular app" does not mean
149 * "it's system", because of isolated UIDs. Use {@link #isCore} for that.
150 * @hide
151 */
Philip P. Moltmann33115152018-04-11 13:39:36 -0700152 @TestApi
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700153 public static boolean isApp(int uid) {
154 if (uid > 0) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700155 final int appId = getAppId(uid);
156 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID;
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700157 } else {
158 return false;
159 }
160 }
161
Amith Yamasani742a6712011-05-04 14:49:28 -0700162 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800163 * Whether a UID belongs to a system core component or not.
164 * @hide
165 */
166 public static boolean isCore(int uid) {
Makoto Onukicbc3cce2018-03-28 16:49:47 -0700167 if (uid >= 0) {
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800168 final int appId = getAppId(uid);
169 return appId < Process.FIRST_APPLICATION_UID;
170 } else {
171 return false;
172 }
173 }
174
175 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800176 * Returns the user for a given uid.
177 * @param uid A uid for an application running in a particular user.
178 * @return A {@link UserHandle} for that user.
179 */
180 public static UserHandle getUserHandleForUid(int uid) {
181 return of(getUserId(uid));
182 }
183
184 /**
Amith Yamasani742a6712011-05-04 14:49:28 -0700185 * Returns the user id for a given uid.
186 * @hide
187 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700188 public static @UserIdInt int getUserId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700189 if (MU_ENABLED) {
190 return uid / PER_USER_RANGE;
191 } else {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700192 return UserHandle.USER_SYSTEM;
Amith Yamasani742a6712011-05-04 14:49:28 -0700193 }
194 }
195
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700196 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700197 public static @UserIdInt int getCallingUserId() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800198 return getUserId(Binder.getCallingUid());
199 }
200
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100201 /** @hide */
Jeff Sharkeyb74799882017-07-28 16:55:41 -0600202 public static @AppIdInt int getCallingAppId() {
203 return getAppId(Binder.getCallingUid());
204 }
205
206 /** @hide */
Fyodor Kupolov940e8572016-01-26 12:03:51 -0800207 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700208 public static UserHandle of(@UserIdInt int userId) {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700209 return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId);
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100210 }
211
Amith Yamasani742a6712011-05-04 14:49:28 -0700212 /**
213 * Returns the uid that is composed from the userId and the appId.
214 * @hide
215 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700216 public static int getUid(@UserIdInt int userId, @AppIdInt int appId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700217 if (MU_ENABLED) {
218 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
219 } else {
220 return appId;
221 }
222 }
223
224 /**
225 * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
226 * @hide
227 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -0700228 @TestApi
Remi NGUYEN VAN9e70a422019-01-31 12:36:54 +0900229 @SystemApi
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);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100297 if (isIsolated(appId)) {
298 if (appId > Process.FIRST_ISOLATED_UID) {
299 sb.append('i');
300 sb.append(appId - Process.FIRST_ISOLATED_UID);
301 } else {
302 sb.append("ai");
303 sb.append(appId - Process.FIRST_APP_ZYGOTE_ISOLATED_UID);
304 }
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700305 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800306 sb.append('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700307 sb.append(appId - Process.FIRST_APPLICATION_UID);
308 } else {
309 sb.append('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800310 sb.append(appId);
311 }
312 }
313 }
314
315 /**
316 * Generate a text representation of the uid, breaking out its individual
317 * components -- user, app, isolated, etc.
318 * @hide
319 */
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700320 public static String formatUid(int uid) {
321 StringBuilder sb = new StringBuilder();
322 formatUid(sb, uid);
323 return sb.toString();
324 }
325
326 /**
327 * Generate a text representation of the uid, breaking out its individual
328 * components -- user, app, isolated, etc.
329 * @hide
330 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800331 public static void formatUid(PrintWriter pw, int uid) {
332 if (uid < Process.FIRST_APPLICATION_UID) {
333 pw.print(uid);
334 } else {
335 pw.print('u');
336 pw.print(getUserId(uid));
337 final int appId = getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100338 if (isIsolated(appId)) {
339 if (appId > Process.FIRST_ISOLATED_UID) {
340 pw.print('i');
341 pw.print(appId - Process.FIRST_ISOLATED_UID);
342 } else {
343 pw.print("ai");
344 pw.print(appId - Process.FIRST_APP_ZYGOTE_ISOLATED_UID);
345 }
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700346 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800347 pw.print('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700348 pw.print(appId - Process.FIRST_APPLICATION_UID);
349 } else {
350 pw.print('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800351 pw.print(appId);
352 }
353 }
354 }
355
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800356 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700357 public static @UserIdInt int parseUserArg(String arg) {
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800358 int userId;
359 if ("all".equals(arg)) {
360 userId = UserHandle.USER_ALL;
361 } else if ("current".equals(arg) || "cur".equals(arg)) {
362 userId = UserHandle.USER_CURRENT;
363 } else {
364 try {
365 userId = Integer.parseInt(arg);
366 } catch (NumberFormatException e) {
367 throw new IllegalArgumentException("Bad user number: " + arg);
368 }
369 }
370 return userId;
371 }
372
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800373 /**
Amith Yamasani483f3b02012-03-13 16:08:00 -0700374 * Returns the user id of the current process
375 * @return user id of the current process
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700376 * @hide
Amith Yamasani483f3b02012-03-13 16:08:00 -0700377 */
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700378 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700379 public static @UserIdInt int myUserId() {
Andreas Gamped281b422016-07-08 03:50:27 +0000380 return getUserId(Process.myUid());
Amith Yamasani483f3b02012-03-13 16:08:00 -0700381 }
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700382
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700383 /**
384 * Returns true if this UserHandle refers to the owner user; false otherwise.
385 * @return true if this UserHandle refers to the owner user; false otherwise.
386 * @hide
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700387 * @deprecated please use {@link #isSystem()} or check for
388 * {@link android.content.pm.UserInfo#isPrimary()}
389 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case.
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700390 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700391 @Deprecated
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700392 @SystemApi
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700393 public boolean isOwner() {
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700394 return this.equals(OWNER);
395 }
396
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700397 /**
398 * @return true if this UserHandle refers to the system user; false otherwise.
399 * @hide
400 */
401 @SystemApi
402 public boolean isSystem() {
403 return this.equals(SYSTEM);
404 }
405
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700406 /** @hide */
407 public UserHandle(int h) {
408 mHandle = h;
409 }
410
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700411 /**
412 * Returns the userId stored in this UserHandle.
413 * @hide
414 */
415 @SystemApi
Makoto Onukicb8936e2017-04-10 15:37:17 -0700416 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700417 public @UserIdInt int getIdentifier() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700418 return mHandle;
419 }
420
421 @Override
422 public String toString() {
423 return "UserHandle{" + mHandle + "}";
424 }
425
426 @Override
427 public boolean equals(Object obj) {
428 try {
429 if (obj != null) {
430 UserHandle other = (UserHandle)obj;
431 return mHandle == other.mHandle;
432 }
433 } catch (ClassCastException e) {
434 }
435 return false;
436 }
437
438 @Override
439 public int hashCode() {
440 return mHandle;
441 }
442
443 public int describeContents() {
444 return 0;
445 }
446
447 public void writeToParcel(Parcel out, int flags) {
448 out.writeInt(mHandle);
449 }
450
451 /**
452 * Write a UserHandle to a Parcel, handling null pointers. Must be
453 * read with {@link #readFromParcel(Parcel)}.
454 *
455 * @param h The UserHandle to be written.
456 * @param out The Parcel in which the UserHandle will be placed.
457 *
458 * @see #readFromParcel(Parcel)
459 */
460 public static void writeToParcel(UserHandle h, Parcel out) {
461 if (h != null) {
462 h.writeToParcel(out, 0);
463 } else {
464 out.writeInt(USER_NULL);
465 }
466 }
467
468 /**
469 * Read a UserHandle from a Parcel that was previously written
470 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
471 * a null or new object as appropriate.
472 *
473 * @param in The Parcel from which to read the UserHandle
474 * @return Returns a new UserHandle matching the previously written
475 * object, or null if a null had been written.
476 *
477 * @see #writeToParcel(UserHandle, Parcel)
478 */
479 public static UserHandle readFromParcel(Parcel in) {
480 int h = in.readInt();
481 return h != USER_NULL ? new UserHandle(h) : null;
482 }
483
484 public static final Parcelable.Creator<UserHandle> CREATOR
485 = new Parcelable.Creator<UserHandle>() {
486 public UserHandle createFromParcel(Parcel in) {
487 return new UserHandle(in);
488 }
489
490 public UserHandle[] newArray(int size) {
491 return new UserHandle[size];
492 }
493 };
494
495 /**
496 * Instantiate a new UserHandle from the data in a Parcel that was
497 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you
498 * must not use this with data written by
499 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
500 * to handle a null UserHandle here.
501 *
502 * @param in The Parcel containing the previously written UserHandle,
503 * positioned at the location in the buffer where it was written.
504 */
505 public UserHandle(Parcel in) {
506 mHandle = in.readInt();
507 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700508}