blob: 577a8c61c57f6e7486ea8bd49b214e84b7e6aedd [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
19/**
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070020 * Representation of a user on the device.
Amith Yamasani742a6712011-05-04 14:49:28 -070021 * @hide
22 */
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070023public final class UserHandle {
Amith Yamasani742a6712011-05-04 14:49:28 -070024 /**
25 * Range of IDs allocated for a user.
26 *
27 * @hide
28 */
29 public static final int PER_USER_RANGE = 100000;
30
Amith Yamasani82644082012-08-03 13:09:11 -070031 /** A user id to indicate all users on the device */
Amith Yamasani742a6712011-05-04 14:49:28 -070032 public static final int USER_ALL = -1;
33
Amith Yamasani82644082012-08-03 13:09:11 -070034 /** A user id to indicate the currently active user */
35 public static final int USER_CURRENT = -2;
36
Christopher Tateaac71ff2012-08-13 17:36:14 -070037 /** A user id constant to indicate the "owner" user of the device */
38 public static final int USER_OWNER = 0;
Amith Yamasani82644082012-08-03 13:09:11 -070039
Amith Yamasani742a6712011-05-04 14:49:28 -070040 /**
41 * Enable multi-user related side effects. Set this to false if there are problems with single
42 * user usecases.
43 * */
44 public static final boolean MU_ENABLED = true;
45
46 /**
47 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
48 * user.
49 * @hide
50 */
51 public static final boolean isSameUser(int uid1, int uid2) {
52 return getUserId(uid1) == getUserId(uid2);
53 }
54
55 /**
56 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
57 * uids.
58 * @param uid1 uid to compare
59 * @param uid2 other uid to compare
60 * @return whether the appId is the same for both uids
61 * @hide
62 */
63 public static final boolean isSameApp(int uid1, int uid2) {
64 return getAppId(uid1) == getAppId(uid2);
65 }
66
Dianne Hackborna573f6a2012-02-09 16:12:18 -080067 public static final boolean isIsolated(int uid) {
68 uid = getAppId(uid);
69 return uid >= Process.FIRST_ISOLATED_UID && uid <= Process.LAST_ISOLATED_UID;
70 }
71
Jeff Sharkey8a8b5812012-03-21 18:13:36 -070072 public static boolean isApp(int uid) {
73 if (uid > 0) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074 uid = UserHandle.getAppId(uid);
Jeff Sharkey8a8b5812012-03-21 18:13:36 -070075 return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID;
76 } else {
77 return false;
78 }
79 }
80
Amith Yamasani742a6712011-05-04 14:49:28 -070081 /**
82 * Returns the user id for a given uid.
83 * @hide
84 */
85 public static final int getUserId(int uid) {
86 if (MU_ENABLED) {
87 return uid / PER_USER_RANGE;
88 } else {
89 return 0;
90 }
91 }
92
Amith Yamasani37ce3a82012-02-06 12:04:42 -080093 public static final int getCallingUserId() {
94 return getUserId(Binder.getCallingUid());
95 }
96
Amith Yamasani742a6712011-05-04 14:49:28 -070097 /**
98 * Returns the uid that is composed from the userId and the appId.
99 * @hide
100 */
101 public static final int getUid(int userId, int appId) {
102 if (MU_ENABLED) {
103 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
104 } else {
105 return appId;
106 }
107 }
108
109 /**
110 * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
111 * @hide
112 */
113 public static final int getAppId(int uid) {
114 return uid % PER_USER_RANGE;
115 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700116
117 /**
118 * Returns the user id of the current process
119 * @return user id of the current process
120 */
121 public static final int myUserId() {
122 return getUserId(Process.myUid());
123 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700124}