Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 19 | import android.annotation.AppIdInt; |
Christopher Tate | cbd7a6e | 2014-08-06 14:24:14 -0700 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Jeff Sharkey | a73b8fd | 2016-01-06 17:02:08 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 22 | import android.annotation.UserIdInt; |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 23 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 24 | import java.io.PrintWriter; |
| 25 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 26 | /** |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 27 | * Representation of a user on the device. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 28 | */ |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 29 | public final class UserHandle implements Parcelable { |
Jeff Sharkey | 77a3450 | 2017-11-09 17:40:57 -0700 | [diff] [blame] | 30 | // NOTE: keep logic in sync with system/core/libcutils/multiuser.c |
| 31 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 32 | /** |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 33 | * @hide Range of uids allocated for a user. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 34 | */ |
| 35 | public static final int PER_USER_RANGE = 100000; |
| 36 | |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 37 | /** @hide A user id to indicate all users on the device */ |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 38 | public static final @UserIdInt int USER_ALL = -1; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 39 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 40 | /** @hide A user handle to indicate all users on the device */ |
Jeff Sharkey | ec5f7d1 | 2018-08-08 09:15:04 -0600 | [diff] [blame] | 41 | @SystemApi |
| 42 | @TestApi |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 43 | public static final UserHandle ALL = new UserHandle(USER_ALL); |
| 44 | |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 45 | /** @hide A user id to indicate the currently active user */ |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 46 | public static final @UserIdInt int USER_CURRENT = -2; |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 47 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 48 | /** @hide A user handle to indicate the current user of the device */ |
Jeff Sharkey | ec5f7d1 | 2018-08-08 09:15:04 -0600 | [diff] [blame] | 49 | @SystemApi |
| 50 | @TestApi |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 51 | 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 Viverette | b6e0cb9 | 2014-11-24 15:13:43 -0800 | [diff] [blame] | 55 | * to the caller's user instead of failing with a security exception */ |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 56 | public static final @UserIdInt int USER_CURRENT_OR_SELF = -3; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 57 | |
| 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 Viverette | b6e0cb9 | 2014-11-24 15:13:43 -0800 | [diff] [blame] | 60 | * to the caller's user instead of failing with a security exception */ |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 61 | public static final UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF); |
| 62 | |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 63 | /** @hide An undefined user id */ |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 64 | public static final @UserIdInt int USER_NULL = -10000; |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 65 | |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @hide A user id constant to indicate the "owner" user of the device |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 68 | * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or |
| 69 | * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}. |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 70 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 71 | @Deprecated |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 72 | public static final @UserIdInt int USER_OWNER = 0; |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 73 | |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 74 | /** |
| 75 | * @hide A user handle to indicate the primary/owner user of the device |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 76 | * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or |
| 77 | * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}. |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 78 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 79 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 80 | public static final UserHandle OWNER = new UserHandle(USER_OWNER); |
| 81 | |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 82 | /** @hide A user id constant to indicate the "system" user of the device */ |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 83 | public static final @UserIdInt int USER_SYSTEM = 0; |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 85 | /** @hide A user serial constant to indicate the "system" user of the device */ |
| 86 | public static final int USER_SERIAL_SYSTEM = 0; |
| 87 | |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 88 | /** @hide A user handle to indicate the "system" user of the device */ |
Jeff Sharkey | ec5f7d1 | 2018-08-08 09:15:04 -0600 | [diff] [blame] | 89 | @SystemApi |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 90 | @TestApi |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 91 | public static final UserHandle SYSTEM = new UserHandle(USER_SYSTEM); |
| 92 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 93 | /** |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 94 | * @hide Enable multi-user related side effects. Set this to false if |
| 95 | * there are problems with single user use-cases. |
| 96 | */ |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 97 | public static final boolean MU_ENABLED = true; |
| 98 | |
Jeff Sharkey | 77a3450 | 2017-11-09 17:40:57 -0700 | [diff] [blame] | 99 | /** @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 Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 112 | final int mHandle; |
| 113 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 114 | /** |
| 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 Chen | eecb981 | 2015-09-16 11:02:18 -0700 | [diff] [blame] | 119 | public static boolean isSameUser(int uid1, int uid2) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 120 | 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 Chen | eecb981 | 2015-09-16 11:02:18 -0700 | [diff] [blame] | 131 | public static boolean isSameApp(int uid1, int uid2) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 132 | return getAppId(uid1) == getAppId(uid2); |
| 133 | } |
| 134 | |
Makoto Onuki | 9cc471c | 2018-01-23 12:33:56 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Whether a UID is an "isolated" UID. |
| 137 | * @hide |
| 138 | */ |
Xiaohui Chen | eecb981 | 2015-09-16 11:02:18 -0700 | [diff] [blame] | 139 | public static boolean isIsolated(int uid) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 140 | 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 Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Makoto Onuki | 9cc471c | 2018-01-23 12:33:56 -0800 | [diff] [blame] | 148 | /** |
| 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. Moltmann | 3311515 | 2018-04-11 13:39:36 -0700 | [diff] [blame] | 153 | @TestApi |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 154 | public static boolean isApp(int uid) { |
| 155 | if (uid > 0) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 156 | final int appId = getAppId(uid); |
| 157 | return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 158 | } else { |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 163 | /** |
Makoto Onuki | 9cc471c | 2018-01-23 12:33:56 -0800 | [diff] [blame] | 164 | * Whether a UID belongs to a system core component or not. |
| 165 | * @hide |
| 166 | */ |
| 167 | public static boolean isCore(int uid) { |
Makoto Onuki | cbc3cce | 2018-03-28 16:49:47 -0700 | [diff] [blame] | 168 | if (uid >= 0) { |
Makoto Onuki | 9cc471c | 2018-01-23 12:33:56 -0800 | [diff] [blame] | 169 | final int appId = getAppId(uid); |
| 170 | return appId < Process.FIRST_APPLICATION_UID; |
| 171 | } else { |
| 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
Jeff Davidson | 48aa86b | 2016-02-10 14:15:42 -0800 | [diff] [blame] | 177 | * 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 186 | * Returns the user id for a given uid. |
| 187 | * @hide |
| 188 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 189 | public static @UserIdInt int getUserId(int uid) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 190 | if (MU_ENABLED) { |
| 191 | return uid / PER_USER_RANGE; |
| 192 | } else { |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 193 | return UserHandle.USER_SYSTEM; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 197 | /** @hide */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 198 | public static @UserIdInt int getCallingUserId() { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 199 | return getUserId(Binder.getCallingUid()); |
| 200 | } |
| 201 | |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 202 | /** @hide */ |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 203 | public static @AppIdInt int getCallingAppId() { |
| 204 | return getAppId(Binder.getCallingUid()); |
| 205 | } |
| 206 | |
| 207 | /** @hide */ |
Fyodor Kupolov | 940e857 | 2016-01-26 12:03:51 -0800 | [diff] [blame] | 208 | @SystemApi |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 209 | public static UserHandle of(@UserIdInt int userId) { |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 210 | return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Returns the uid that is composed from the userId and the appId. |
| 215 | * @hide |
| 216 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 217 | public static int getUid(@UserIdInt int userId, @AppIdInt int appId) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 218 | 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 Sharkey | a73b8fd | 2016-01-06 17:02:08 -0700 | [diff] [blame] | 229 | @TestApi |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 230 | public static @AppIdInt int getAppId(int uid) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 231 | return uid % PER_USER_RANGE; |
| 232 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 233 | |
| 234 | /** |
Robin Lee | e66b689 | 2014-04-29 12:36:47 +0100 | [diff] [blame] | 235 | * Returns the gid shared between all apps with this userId. |
| 236 | * @hide |
| 237 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 238 | public static int getUserGid(@UserIdInt int userId) { |
Robin Lee | e66b689 | 2014-04-29 12:36:47 +0100 | [diff] [blame] | 239 | return getUid(userId, Process.SHARED_USER_GID); |
| 240 | } |
| 241 | |
Jeff Sharkey | 77a3450 | 2017-11-09 17:40:57 -0700 | [diff] [blame] | 242 | /** @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 Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Adam Lesinski | 621e06a | 2015-08-13 22:06:31 -0700 | [diff] [blame] | 259 | * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid. |
Adam Lesinski | f4216d3 | 2015-06-26 16:33:24 -0700 | [diff] [blame] | 260 | * @hide |
| 261 | */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 262 | public static @AppIdInt int getAppIdFromSharedAppGid(int gid) { |
Adam Lesinski | 621e06a | 2015-08-13 22:06:31 -0700 | [diff] [blame] | 263 | 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 Lesinski | f4216d3 | 2015-06-26 16:33:24 -0700 | [diff] [blame] | 267 | } |
Adam Lesinski | 621e06a | 2015-08-13 22:06:31 -0700 | [diff] [blame] | 268 | return appId; |
Adam Lesinski | f4216d3 | 2015-06-26 16:33:24 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jeff Sharkey | 77a3450 | 2017-11-09 17:40:57 -0700 | [diff] [blame] | 271 | /** @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 Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 286 | * 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 Hackborn | a4cc205 | 2013-07-08 17:31:25 -0700 | [diff] [blame] | 300 | } else if (appId >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 301 | sb.append('a'); |
Dianne Hackborn | a4cc205 | 2013-07-08 17:31:25 -0700 | [diff] [blame] | 302 | sb.append(appId - Process.FIRST_APPLICATION_UID); |
| 303 | } else { |
| 304 | sb.append('s'); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 305 | 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 Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 315 | 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 Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 326 | 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 Hackborn | a4cc205 | 2013-07-08 17:31:25 -0700 | [diff] [blame] | 336 | } else if (appId >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 337 | pw.print('a'); |
Dianne Hackborn | a4cc205 | 2013-07-08 17:31:25 -0700 | [diff] [blame] | 338 | pw.print(appId - Process.FIRST_APPLICATION_UID); |
| 339 | } else { |
| 340 | pw.print('s'); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 341 | pw.print(appId); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 346 | /** @hide */ |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 347 | public static @UserIdInt int parseUserArg(String arg) { |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 348 | 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 Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 363 | /** |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 364 | * Returns the user id of the current process |
| 365 | * @return user id of the current process |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 366 | * @hide |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 367 | */ |
Amith Yamasani | 0e8d7d6 | 2014-09-03 13:17:28 -0700 | [diff] [blame] | 368 | @SystemApi |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 369 | public static @UserIdInt int myUserId() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 370 | return getUserId(Process.myUid()); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 371 | } |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 372 | |
Christopher Tate | cbd7a6e | 2014-08-06 14:24:14 -0700 | [diff] [blame] | 373 | /** |
| 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 Chen | 1fa7680 | 2015-10-12 15:56:25 -0700 | [diff] [blame] | 377 | * @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 Tate | cbd7a6e | 2014-08-06 14:24:14 -0700 | [diff] [blame] | 380 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 381 | @Deprecated |
Christopher Tate | cbd7a6e | 2014-08-06 14:24:14 -0700 | [diff] [blame] | 382 | @SystemApi |
Xiaohui Chen | eecb981 | 2015-09-16 11:02:18 -0700 | [diff] [blame] | 383 | public boolean isOwner() { |
Christopher Tate | cbd7a6e | 2014-08-06 14:24:14 -0700 | [diff] [blame] | 384 | return this.equals(OWNER); |
| 385 | } |
| 386 | |
Xiaohui Chen | 1fa7680 | 2015-10-12 15:56:25 -0700 | [diff] [blame] | 387 | /** |
| 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 Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 396 | /** @hide */ |
| 397 | public UserHandle(int h) { |
| 398 | mHandle = h; |
| 399 | } |
| 400 | |
Amith Yamasani | 0e8d7d6 | 2014-09-03 13:17:28 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Returns the userId stored in this UserHandle. |
| 403 | * @hide |
| 404 | */ |
| 405 | @SystemApi |
Makoto Onuki | cb8936e | 2017-04-10 15:37:17 -0700 | [diff] [blame] | 406 | @TestApi |
Jeff Sharkey | 8588bc1 | 2016-01-06 16:47:42 -0700 | [diff] [blame] | 407 | public @UserIdInt int getIdentifier() { |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 408 | 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 Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 498 | } |