UserHandle.java revision cd65448ccd13c4c2d0fe9e9623fec3a898ab9372
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 17package android.os; 18 19import android.annotation.AppIdInt; 20import android.annotation.SystemApi; 21import android.annotation.TestApi; 22import android.annotation.UserIdInt; 23 24import java.io.PrintWriter; 25 26/** 27 * Representation of a user on the device. 28 */ 29public final class UserHandle implements Parcelable { 30 /** 31 * @hide Range of uids allocated for a user. 32 */ 33 public static final int PER_USER_RANGE = 100000; 34 35 /** @hide A user id to indicate all users on the device */ 36 public static final @UserIdInt int USER_ALL = -1; 37 38 /** @hide A user handle to indicate all users on the device */ 39 public static final UserHandle ALL = new UserHandle(USER_ALL); 40 41 /** @hide A user id to indicate the currently active user */ 42 public static final @UserIdInt int USER_CURRENT = -2; 43 44 /** @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 49 * to the caller's user instead of failing with a security exception */ 50 public static final @UserIdInt int USER_CURRENT_OR_SELF = -3; 51 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 54 * to the caller's user instead of failing with a security exception */ 55 public static final UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF); 56 57 /** @hide An undefined user id */ 58 public static final @UserIdInt int USER_NULL = -10000; 59 60 /** 61 * @hide A user id constant to indicate the "owner" user of the device 62 * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or 63 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}. 64 */ 65 public static final @UserIdInt int USER_OWNER = 0; 66 67 /** 68 * @hide A user handle to indicate the primary/owner user of the device 69 * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or 70 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}. 71 */ 72 public static final UserHandle OWNER = new UserHandle(USER_OWNER); 73 74 /** @hide A user id constant to indicate the "system" user of the device */ 75 public static final @UserIdInt int USER_SYSTEM = 0; 76 77 /** @hide A user handle to indicate the "system" user of the device */ 78 public static final UserHandle SYSTEM = new UserHandle(USER_SYSTEM); 79 80 /** 81 * @hide Enable multi-user related side effects. Set this to false if 82 * there are problems with single user use-cases. 83 */ 84 public static final boolean MU_ENABLED = true; 85 86 final int mHandle; 87 88 /** 89 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same 90 * user. 91 * @hide 92 */ 93 public static boolean isSameUser(int uid1, int uid2) { 94 return getUserId(uid1) == getUserId(uid2); 95 } 96 97 /** 98 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the 99 * uids. 100 * @param uid1 uid to compare 101 * @param uid2 other uid to compare 102 * @return whether the appId is the same for both uids 103 * @hide 104 */ 105 public static boolean isSameApp(int uid1, int uid2) { 106 return getAppId(uid1) == getAppId(uid2); 107 } 108 109 /** @hide */ 110 public static boolean isIsolated(int uid) { 111 if (uid > 0) { 112 final int appId = getAppId(uid); 113 return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID; 114 } else { 115 return false; 116 } 117 } 118 119 /** @hide */ 120 public static boolean isApp(int uid) { 121 if (uid > 0) { 122 final int appId = getAppId(uid); 123 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID; 124 } else { 125 return false; 126 } 127 } 128 129 /** 130 * Returns the user id for a given uid. 131 * @hide 132 */ 133 public static @UserIdInt int getUserId(int uid) { 134 if (MU_ENABLED) { 135 return uid / PER_USER_RANGE; 136 } else { 137 return UserHandle.USER_SYSTEM; 138 } 139 } 140 141 /** @hide */ 142 public static @UserIdInt int getCallingUserId() { 143 return getUserId(Binder.getCallingUid()); 144 } 145 146 /** @hide */ 147 public static UserHandle of(@UserIdInt int userId) { 148 return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId); 149 } 150 151 /** 152 * Returns the uid that is composed from the userId and the appId. 153 * @hide 154 */ 155 public static int getUid(@UserIdInt int userId, @AppIdInt int appId) { 156 if (MU_ENABLED) { 157 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE); 158 } else { 159 return appId; 160 } 161 } 162 163 /** 164 * Returns the app id (or base uid) for a given uid, stripping out the user id from it. 165 * @hide 166 */ 167 @TestApi 168 public static @AppIdInt int getAppId(int uid) { 169 return uid % PER_USER_RANGE; 170 } 171 172 /** 173 * Returns the gid shared between all apps with this userId. 174 * @hide 175 */ 176 public static int getUserGid(@UserIdInt int userId) { 177 return getUid(userId, Process.SHARED_USER_GID); 178 } 179 180 /** 181 * Returns the shared app gid for a given uid or appId. 182 * @hide 183 */ 184 public static int getSharedAppGid(int id) { 185 return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE) 186 - Process.FIRST_APPLICATION_UID; 187 } 188 189 /** 190 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid. 191 * @hide 192 */ 193 public static @AppIdInt int getAppIdFromSharedAppGid(int gid) { 194 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID 195 - Process.FIRST_SHARED_APPLICATION_GID; 196 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) { 197 return -1; 198 } 199 return appId; 200 } 201 202 /** 203 * Generate a text representation of the uid, breaking out its individual 204 * components -- user, app, isolated, etc. 205 * @hide 206 */ 207 public static void formatUid(StringBuilder sb, int uid) { 208 if (uid < Process.FIRST_APPLICATION_UID) { 209 sb.append(uid); 210 } else { 211 sb.append('u'); 212 sb.append(getUserId(uid)); 213 final int appId = getAppId(uid); 214 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 215 sb.append('i'); 216 sb.append(appId - Process.FIRST_ISOLATED_UID); 217 } else if (appId >= Process.FIRST_APPLICATION_UID) { 218 sb.append('a'); 219 sb.append(appId - Process.FIRST_APPLICATION_UID); 220 } else { 221 sb.append('s'); 222 sb.append(appId); 223 } 224 } 225 } 226 227 /** 228 * Generate a text representation of the uid, breaking out its individual 229 * components -- user, app, isolated, etc. 230 * @hide 231 */ 232 public static String formatUid(int uid) { 233 StringBuilder sb = new StringBuilder(); 234 formatUid(sb, uid); 235 return sb.toString(); 236 } 237 238 /** 239 * Generate a text representation of the uid, breaking out its individual 240 * components -- user, app, isolated, etc. 241 * @hide 242 */ 243 public static void formatUid(PrintWriter pw, int uid) { 244 if (uid < Process.FIRST_APPLICATION_UID) { 245 pw.print(uid); 246 } else { 247 pw.print('u'); 248 pw.print(getUserId(uid)); 249 final int appId = getAppId(uid); 250 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 251 pw.print('i'); 252 pw.print(appId - Process.FIRST_ISOLATED_UID); 253 } else if (appId >= Process.FIRST_APPLICATION_UID) { 254 pw.print('a'); 255 pw.print(appId - Process.FIRST_APPLICATION_UID); 256 } else { 257 pw.print('s'); 258 pw.print(appId); 259 } 260 } 261 } 262 263 /** @hide */ 264 public static @UserIdInt int parseUserArg(String arg) { 265 int userId; 266 if ("all".equals(arg)) { 267 userId = UserHandle.USER_ALL; 268 } else if ("current".equals(arg) || "cur".equals(arg)) { 269 userId = UserHandle.USER_CURRENT; 270 } else { 271 try { 272 userId = Integer.parseInt(arg); 273 } catch (NumberFormatException e) { 274 throw new IllegalArgumentException("Bad user number: " + arg); 275 } 276 } 277 return userId; 278 } 279 280 /** 281 * Returns the user id of the current process 282 * @return user id of the current process 283 * @hide 284 */ 285 @SystemApi 286 public static @UserIdInt int myUserId() { 287 return getUserId(Process.myUid()); 288 } 289 290 /** 291 * Returns true if this UserHandle refers to the owner user; false otherwise. 292 * @return true if this UserHandle refers to the owner user; false otherwise. 293 * @hide 294 * @deprecated please use {@link #isSystem()} or check for 295 * {@link android.content.pm.UserInfo#isPrimary()} 296 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case. 297 */ 298 @SystemApi 299 public boolean isOwner() { 300 return this.equals(OWNER); 301 } 302 303 /** 304 * @return true if this UserHandle refers to the system user; false otherwise. 305 * @hide 306 */ 307 @SystemApi 308 public boolean isSystem() { 309 return this.equals(SYSTEM); 310 } 311 312 /** @hide */ 313 public UserHandle(int h) { 314 mHandle = h; 315 } 316 317 /** 318 * Returns the userId stored in this UserHandle. 319 * @hide 320 */ 321 @SystemApi 322 public @UserIdInt int getIdentifier() { 323 return mHandle; 324 } 325 326 @Override 327 public String toString() { 328 return "UserHandle{" + mHandle + "}"; 329 } 330 331 @Override 332 public boolean equals(Object obj) { 333 try { 334 if (obj != null) { 335 UserHandle other = (UserHandle)obj; 336 return mHandle == other.mHandle; 337 } 338 } catch (ClassCastException e) { 339 } 340 return false; 341 } 342 343 @Override 344 public int hashCode() { 345 return mHandle; 346 } 347 348 public int describeContents() { 349 return 0; 350 } 351 352 public void writeToParcel(Parcel out, int flags) { 353 out.writeInt(mHandle); 354 } 355 356 /** 357 * Write a UserHandle to a Parcel, handling null pointers. Must be 358 * read with {@link #readFromParcel(Parcel)}. 359 * 360 * @param h The UserHandle to be written. 361 * @param out The Parcel in which the UserHandle will be placed. 362 * 363 * @see #readFromParcel(Parcel) 364 */ 365 public static void writeToParcel(UserHandle h, Parcel out) { 366 if (h != null) { 367 h.writeToParcel(out, 0); 368 } else { 369 out.writeInt(USER_NULL); 370 } 371 } 372 373 /** 374 * Read a UserHandle from a Parcel that was previously written 375 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 376 * a null or new object as appropriate. 377 * 378 * @param in The Parcel from which to read the UserHandle 379 * @return Returns a new UserHandle matching the previously written 380 * object, or null if a null had been written. 381 * 382 * @see #writeToParcel(UserHandle, Parcel) 383 */ 384 public static UserHandle readFromParcel(Parcel in) { 385 int h = in.readInt(); 386 return h != USER_NULL ? new UserHandle(h) : null; 387 } 388 389 public static final Parcelable.Creator<UserHandle> CREATOR 390 = new Parcelable.Creator<UserHandle>() { 391 public UserHandle createFromParcel(Parcel in) { 392 return new UserHandle(in); 393 } 394 395 public UserHandle[] newArray(int size) { 396 return new UserHandle[size]; 397 } 398 }; 399 400 /** 401 * Instantiate a new UserHandle from the data in a Parcel that was 402 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 403 * must not use this with data written by 404 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 405 * to handle a null UserHandle here. 406 * 407 * @param in The Parcel containing the previously written UserHandle, 408 * positioned at the location in the buffer where it was written. 409 */ 410 public UserHandle(Parcel in) { 411 mHandle = in.readInt(); 412 } 413} 414