blob: b3f44536214bb259cf6e350684dfa764ac083c5f [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 {
Amith Yamasani742a6712011-05-04 14:49:28 -070030 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070031 * @hide Range of uids allocated for a user.
Amith Yamasani742a6712011-05-04 14:49:28 -070032 */
33 public static final int PER_USER_RANGE = 100000;
34
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070035 /** @hide A user id to indicate all users on the device */
Jeff Sharkeycd654482016-01-08 17:42:11 -070036 public static final @UserIdInt int USER_ALL = -1;
Amith Yamasani742a6712011-05-04 14:49:28 -070037
Dianne Hackborn7767eac2012-08-23 18:25:40 -070038 /** @hide A user handle to indicate all users on the device */
39 public static final UserHandle ALL = new UserHandle(USER_ALL);
40
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070041 /** @hide A user id to indicate the currently active user */
Jeff Sharkeycd654482016-01-08 17:42:11 -070042 public static final @UserIdInt int USER_CURRENT = -2;
Amith Yamasani82644082012-08-03 13:09:11 -070043
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070044 /** @hide A user handle to indicate the current user of the device */
45 public static final UserHandle CURRENT = new UserHandle(USER_CURRENT);
46
47 /** @hide A user id to indicate that we would like to send to the current
48 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080049 * to the caller's user instead of failing with a security exception */
Jeff Sharkeycd654482016-01-08 17:42:11 -070050 public static final @UserIdInt int USER_CURRENT_OR_SELF = -3;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070051
52 /** @hide A user handle to indicate that we would like to send to the current
53 * user, but if this is calling from a user process then we will send it
Alan Viveretteb6e0cb92014-11-24 15:13:43 -080054 * to the caller's user instead of failing with a security exception */
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070055 public static final UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF);
56
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070057 /** @hide An undefined user id */
Jeff Sharkeycd654482016-01-08 17:42:11 -070058 public static final @UserIdInt int USER_NULL = -10000;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070059
Xiaohui Chen70f6c382015-04-28 14:21:43 -070060 /**
61 * @hide A user id constant to indicate the "owner" user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070062 * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or
63 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070064 */
Jeff Sharkeycd654482016-01-08 17:42:11 -070065 public static final @UserIdInt int USER_OWNER = 0;
Amith Yamasani82644082012-08-03 13:09:11 -070066
Xiaohui Chen70f6c382015-04-28 14:21:43 -070067 /**
68 * @hide A user handle to indicate the primary/owner user of the device
Xiaohui Chen7c696362015-09-16 09:56:14 -070069 * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or
70 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}.
Xiaohui Chen70f6c382015-04-28 14:21:43 -070071 */
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070072 public static final UserHandle OWNER = new UserHandle(USER_OWNER);
73
Xiaohui Chen70f6c382015-04-28 14:21:43 -070074 /** @hide A user id constant to indicate the "system" user of the device */
Jeff Sharkeycd654482016-01-08 17:42:11 -070075 public static final @UserIdInt int USER_SYSTEM = 0;
Xiaohui Chen70f6c382015-04-28 14:21:43 -070076
Jeff Sharkey47f71082016-02-01 17:03:54 -070077 /** @hide A user serial constant to indicate the "system" user of the device */
78 public static final int USER_SERIAL_SYSTEM = 0;
79
Xiaohui Chen70f6c382015-04-28 14:21:43 -070080 /** @hide A user handle to indicate the "system" user of the device */
81 public static final UserHandle SYSTEM = new UserHandle(USER_SYSTEM);
82
Amith Yamasani742a6712011-05-04 14:49:28 -070083 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070084 * @hide Enable multi-user related side effects. Set this to false if
85 * there are problems with single user use-cases.
86 */
Amith Yamasani742a6712011-05-04 14:49:28 -070087 public static final boolean MU_ENABLED = true;
88
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070089 final int mHandle;
90
Amith Yamasani742a6712011-05-04 14:49:28 -070091 /**
92 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
93 * user.
94 * @hide
95 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -070096 public static boolean isSameUser(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -070097 return getUserId(uid1) == getUserId(uid2);
98 }
99
100 /**
101 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
102 * uids.
103 * @param uid1 uid to compare
104 * @param uid2 other uid to compare
105 * @return whether the appId is the same for both uids
106 * @hide
107 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700108 public static boolean isSameApp(int uid1, int uid2) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700109 return getAppId(uid1) == getAppId(uid2);
110 }
111
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700112 /** @hide */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700113 public static boolean isIsolated(int uid) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700114 if (uid > 0) {
115 final int appId = getAppId(uid);
116 return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID;
117 } else {
118 return false;
119 }
Dianne Hackborna573f6a2012-02-09 16:12:18 -0800120 }
121
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700122 /** @hide */
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700123 public static boolean isApp(int uid) {
124 if (uid > 0) {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700125 final int appId = getAppId(uid);
126 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID;
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700127 } else {
128 return false;
129 }
130 }
131
Amith Yamasani742a6712011-05-04 14:49:28 -0700132 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800133 * Returns the user for a given uid.
134 * @param uid A uid for an application running in a particular user.
135 * @return A {@link UserHandle} for that user.
136 */
137 public static UserHandle getUserHandleForUid(int uid) {
138 return of(getUserId(uid));
139 }
140
141 /**
Amith Yamasani742a6712011-05-04 14:49:28 -0700142 * Returns the user id for a given uid.
143 * @hide
144 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700145 public static @UserIdInt int getUserId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700146 if (MU_ENABLED) {
147 return uid / PER_USER_RANGE;
148 } else {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700149 return UserHandle.USER_SYSTEM;
Amith Yamasani742a6712011-05-04 14:49:28 -0700150 }
151 }
152
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700153 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700154 public static @UserIdInt int getCallingUserId() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800155 return getUserId(Binder.getCallingUid());
156 }
157
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100158 /** @hide */
Fyodor Kupolov940e8572016-01-26 12:03:51 -0800159 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700160 public static UserHandle of(@UserIdInt int userId) {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700161 return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId);
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100162 }
163
Amith Yamasani742a6712011-05-04 14:49:28 -0700164 /**
165 * Returns the uid that is composed from the userId and the appId.
166 * @hide
167 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700168 public static int getUid(@UserIdInt int userId, @AppIdInt int appId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700169 if (MU_ENABLED) {
170 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
171 } else {
172 return appId;
173 }
174 }
175
176 /**
177 * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
178 * @hide
179 */
Jeff Sharkeya73b8fd2016-01-06 17:02:08 -0700180 @TestApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700181 public static @AppIdInt int getAppId(int uid) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700182 return uid % PER_USER_RANGE;
183 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700184
185 /**
Robin Leee66b6892014-04-29 12:36:47 +0100186 * Returns the gid shared between all apps with this userId.
187 * @hide
188 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700189 public static int getUserGid(@UserIdInt int userId) {
Robin Leee66b6892014-04-29 12:36:47 +0100190 return getUid(userId, Process.SHARED_USER_GID);
191 }
192
193 /**
Kenny Roote091f222012-09-11 15:01:26 -0700194 * Returns the shared app gid for a given uid or appId.
195 * @hide
196 */
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700197 public static int getSharedAppGid(int id) {
Kenny Roote091f222012-09-11 15:01:26 -0700198 return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE)
199 - Process.FIRST_APPLICATION_UID;
200 }
201
202 /**
Adam Lesinski621e06a2015-08-13 22:06:31 -0700203 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid.
Adam Lesinskif4216d32015-06-26 16:33:24 -0700204 * @hide
205 */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700206 public static @AppIdInt int getAppIdFromSharedAppGid(int gid) {
Adam Lesinski621e06a2015-08-13 22:06:31 -0700207 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID
208 - Process.FIRST_SHARED_APPLICATION_GID;
209 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) {
210 return -1;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700211 }
Adam Lesinski621e06a2015-08-13 22:06:31 -0700212 return appId;
Adam Lesinskif4216d32015-06-26 16:33:24 -0700213 }
214
215 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800216 * Generate a text representation of the uid, breaking out its individual
217 * components -- user, app, isolated, etc.
218 * @hide
219 */
220 public static void formatUid(StringBuilder sb, int uid) {
221 if (uid < Process.FIRST_APPLICATION_UID) {
222 sb.append(uid);
223 } else {
224 sb.append('u');
225 sb.append(getUserId(uid));
226 final int appId = getAppId(uid);
227 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
228 sb.append('i');
229 sb.append(appId - Process.FIRST_ISOLATED_UID);
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700230 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800231 sb.append('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700232 sb.append(appId - Process.FIRST_APPLICATION_UID);
233 } else {
234 sb.append('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800235 sb.append(appId);
236 }
237 }
238 }
239
240 /**
241 * Generate a text representation of the uid, breaking out its individual
242 * components -- user, app, isolated, etc.
243 * @hide
244 */
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700245 public static String formatUid(int uid) {
246 StringBuilder sb = new StringBuilder();
247 formatUid(sb, uid);
248 return sb.toString();
249 }
250
251 /**
252 * Generate a text representation of the uid, breaking out its individual
253 * components -- user, app, isolated, etc.
254 * @hide
255 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800256 public static void formatUid(PrintWriter pw, int uid) {
257 if (uid < Process.FIRST_APPLICATION_UID) {
258 pw.print(uid);
259 } else {
260 pw.print('u');
261 pw.print(getUserId(uid));
262 final int appId = getAppId(uid);
263 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
264 pw.print('i');
265 pw.print(appId - Process.FIRST_ISOLATED_UID);
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700266 } else if (appId >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800267 pw.print('a');
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700268 pw.print(appId - Process.FIRST_APPLICATION_UID);
269 } else {
270 pw.print('s');
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800271 pw.print(appId);
272 }
273 }
274 }
275
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800276 /** @hide */
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700277 public static @UserIdInt int parseUserArg(String arg) {
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -0800278 int userId;
279 if ("all".equals(arg)) {
280 userId = UserHandle.USER_ALL;
281 } else if ("current".equals(arg) || "cur".equals(arg)) {
282 userId = UserHandle.USER_CURRENT;
283 } else {
284 try {
285 userId = Integer.parseInt(arg);
286 } catch (NumberFormatException e) {
287 throw new IllegalArgumentException("Bad user number: " + arg);
288 }
289 }
290 return userId;
291 }
292
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800293 /**
Amith Yamasani483f3b02012-03-13 16:08:00 -0700294 * Returns the user id of the current process
295 * @return user id of the current process
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700296 * @hide
Amith Yamasani483f3b02012-03-13 16:08:00 -0700297 */
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700298 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700299 public static @UserIdInt int myUserId() {
Andreas Gamped281b422016-07-08 03:50:27 +0000300 return getUserId(Process.myUid());
Amith Yamasani483f3b02012-03-13 16:08:00 -0700301 }
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700302
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700303 /**
304 * Returns true if this UserHandle refers to the owner user; false otherwise.
305 * @return true if this UserHandle refers to the owner user; false otherwise.
306 * @hide
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700307 * @deprecated please use {@link #isSystem()} or check for
308 * {@link android.content.pm.UserInfo#isPrimary()}
309 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case.
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700310 */
311 @SystemApi
Xiaohui Cheneecb9812015-09-16 11:02:18 -0700312 public boolean isOwner() {
Christopher Tatecbd7a6e2014-08-06 14:24:14 -0700313 return this.equals(OWNER);
314 }
315
Xiaohui Chen1fa76802015-10-12 15:56:25 -0700316 /**
317 * @return true if this UserHandle refers to the system user; false otherwise.
318 * @hide
319 */
320 @SystemApi
321 public boolean isSystem() {
322 return this.equals(SYSTEM);
323 }
324
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700325 /** @hide */
326 public UserHandle(int h) {
327 mHandle = h;
328 }
329
Amith Yamasani0e8d7d62014-09-03 13:17:28 -0700330 /**
331 * Returns the userId stored in this UserHandle.
332 * @hide
333 */
334 @SystemApi
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700335 public @UserIdInt int getIdentifier() {
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700336 return mHandle;
337 }
338
339 @Override
340 public String toString() {
341 return "UserHandle{" + mHandle + "}";
342 }
343
344 @Override
345 public boolean equals(Object obj) {
346 try {
347 if (obj != null) {
348 UserHandle other = (UserHandle)obj;
349 return mHandle == other.mHandle;
350 }
351 } catch (ClassCastException e) {
352 }
353 return false;
354 }
355
356 @Override
357 public int hashCode() {
358 return mHandle;
359 }
360
361 public int describeContents() {
362 return 0;
363 }
364
365 public void writeToParcel(Parcel out, int flags) {
366 out.writeInt(mHandle);
367 }
368
369 /**
370 * Write a UserHandle to a Parcel, handling null pointers. Must be
371 * read with {@link #readFromParcel(Parcel)}.
372 *
373 * @param h The UserHandle to be written.
374 * @param out The Parcel in which the UserHandle will be placed.
375 *
376 * @see #readFromParcel(Parcel)
377 */
378 public static void writeToParcel(UserHandle h, Parcel out) {
379 if (h != null) {
380 h.writeToParcel(out, 0);
381 } else {
382 out.writeInt(USER_NULL);
383 }
384 }
385
386 /**
387 * Read a UserHandle from a Parcel that was previously written
388 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
389 * a null or new object as appropriate.
390 *
391 * @param in The Parcel from which to read the UserHandle
392 * @return Returns a new UserHandle matching the previously written
393 * object, or null if a null had been written.
394 *
395 * @see #writeToParcel(UserHandle, Parcel)
396 */
397 public static UserHandle readFromParcel(Parcel in) {
398 int h = in.readInt();
399 return h != USER_NULL ? new UserHandle(h) : null;
400 }
401
402 public static final Parcelable.Creator<UserHandle> CREATOR
403 = new Parcelable.Creator<UserHandle>() {
404 public UserHandle createFromParcel(Parcel in) {
405 return new UserHandle(in);
406 }
407
408 public UserHandle[] newArray(int size) {
409 return new UserHandle[size];
410 }
411 };
412
413 /**
414 * Instantiate a new UserHandle from the data in a Parcel that was
415 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you
416 * must not use this with data written by
417 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
418 * to handle a null UserHandle here.
419 *
420 * @param in The Parcel containing the previously written UserHandle,
421 * positioned at the location in the buffer where it was written.
422 */
423 public UserHandle(Parcel in) {
424 mHandle = in.readInt();
425 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700426}