UserHandle.java revision 940e8577a1569140521fb605336997863529f7ad
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 @SystemApi 148 public static UserHandle of(@UserIdInt int userId) { 149 return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId); 150 } 151 152 /** 153 * Returns the uid that is composed from the userId and the appId. 154 * @hide 155 */ 156 public static int getUid(@UserIdInt int userId, @AppIdInt int appId) { 157 if (MU_ENABLED) { 158 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE); 159 } else { 160 return appId; 161 } 162 } 163 164 /** 165 * Returns the app id (or base uid) for a given uid, stripping out the user id from it. 166 * @hide 167 */ 168 @TestApi 169 public static @AppIdInt int getAppId(int uid) { 170 return uid % PER_USER_RANGE; 171 } 172 173 /** 174 * Returns the gid shared between all apps with this userId. 175 * @hide 176 */ 177 public static int getUserGid(@UserIdInt int userId) { 178 return getUid(userId, Process.SHARED_USER_GID); 179 } 180 181 /** 182 * Returns the shared app gid for a given uid or appId. 183 * @hide 184 */ 185 public static int getSharedAppGid(int id) { 186 return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE) 187 - Process.FIRST_APPLICATION_UID; 188 } 189 190 /** 191 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid. 192 * @hide 193 */ 194 public static @AppIdInt int getAppIdFromSharedAppGid(int gid) { 195 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID 196 - Process.FIRST_SHARED_APPLICATION_GID; 197 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) { 198 return -1; 199 } 200 return appId; 201 } 202 203 /** 204 * Generate a text representation of the uid, breaking out its individual 205 * components -- user, app, isolated, etc. 206 * @hide 207 */ 208 public static void formatUid(StringBuilder sb, int uid) { 209 if (uid < Process.FIRST_APPLICATION_UID) { 210 sb.append(uid); 211 } else { 212 sb.append('u'); 213 sb.append(getUserId(uid)); 214 final int appId = getAppId(uid); 215 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 216 sb.append('i'); 217 sb.append(appId - Process.FIRST_ISOLATED_UID); 218 } else if (appId >= Process.FIRST_APPLICATION_UID) { 219 sb.append('a'); 220 sb.append(appId - Process.FIRST_APPLICATION_UID); 221 } else { 222 sb.append('s'); 223 sb.append(appId); 224 } 225 } 226 } 227 228 /** 229 * Generate a text representation of the uid, breaking out its individual 230 * components -- user, app, isolated, etc. 231 * @hide 232 */ 233 public static String formatUid(int uid) { 234 StringBuilder sb = new StringBuilder(); 235 formatUid(sb, uid); 236 return sb.toString(); 237 } 238 239 /** 240 * Generate a text representation of the uid, breaking out its individual 241 * components -- user, app, isolated, etc. 242 * @hide 243 */ 244 public static void formatUid(PrintWriter pw, int uid) { 245 if (uid < Process.FIRST_APPLICATION_UID) { 246 pw.print(uid); 247 } else { 248 pw.print('u'); 249 pw.print(getUserId(uid)); 250 final int appId = getAppId(uid); 251 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 252 pw.print('i'); 253 pw.print(appId - Process.FIRST_ISOLATED_UID); 254 } else if (appId >= Process.FIRST_APPLICATION_UID) { 255 pw.print('a'); 256 pw.print(appId - Process.FIRST_APPLICATION_UID); 257 } else { 258 pw.print('s'); 259 pw.print(appId); 260 } 261 } 262 } 263 264 /** @hide */ 265 public static @UserIdInt int parseUserArg(String arg) { 266 int userId; 267 if ("all".equals(arg)) { 268 userId = UserHandle.USER_ALL; 269 } else if ("current".equals(arg) || "cur".equals(arg)) { 270 userId = UserHandle.USER_CURRENT; 271 } else { 272 try { 273 userId = Integer.parseInt(arg); 274 } catch (NumberFormatException e) { 275 throw new IllegalArgumentException("Bad user number: " + arg); 276 } 277 } 278 return userId; 279 } 280 281 /** 282 * Returns the user id of the current process 283 * @return user id of the current process 284 * @hide 285 */ 286 @SystemApi 287 public static @UserIdInt int myUserId() { 288 return getUserId(Process.myUid()); 289 } 290 291 /** 292 * Returns true if this UserHandle refers to the owner user; false otherwise. 293 * @return true if this UserHandle refers to the owner user; false otherwise. 294 * @hide 295 * @deprecated please use {@link #isSystem()} or check for 296 * {@link android.content.pm.UserInfo#isPrimary()} 297 * {@link android.content.pm.UserInfo#isAdmin()} based on your particular use case. 298 */ 299 @SystemApi 300 public boolean isOwner() { 301 return this.equals(OWNER); 302 } 303 304 /** 305 * @return true if this UserHandle refers to the system user; false otherwise. 306 * @hide 307 */ 308 @SystemApi 309 public boolean isSystem() { 310 return this.equals(SYSTEM); 311 } 312 313 /** @hide */ 314 public UserHandle(int h) { 315 mHandle = h; 316 } 317 318 /** 319 * Returns the userId stored in this UserHandle. 320 * @hide 321 */ 322 @SystemApi 323 public @UserIdInt int getIdentifier() { 324 return mHandle; 325 } 326 327 @Override 328 public String toString() { 329 return "UserHandle{" + mHandle + "}"; 330 } 331 332 @Override 333 public boolean equals(Object obj) { 334 try { 335 if (obj != null) { 336 UserHandle other = (UserHandle)obj; 337 return mHandle == other.mHandle; 338 } 339 } catch (ClassCastException e) { 340 } 341 return false; 342 } 343 344 @Override 345 public int hashCode() { 346 return mHandle; 347 } 348 349 public int describeContents() { 350 return 0; 351 } 352 353 public void writeToParcel(Parcel out, int flags) { 354 out.writeInt(mHandle); 355 } 356 357 /** 358 * Write a UserHandle to a Parcel, handling null pointers. Must be 359 * read with {@link #readFromParcel(Parcel)}. 360 * 361 * @param h The UserHandle to be written. 362 * @param out The Parcel in which the UserHandle will be placed. 363 * 364 * @see #readFromParcel(Parcel) 365 */ 366 public static void writeToParcel(UserHandle h, Parcel out) { 367 if (h != null) { 368 h.writeToParcel(out, 0); 369 } else { 370 out.writeInt(USER_NULL); 371 } 372 } 373 374 /** 375 * Read a UserHandle from a Parcel that was previously written 376 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 377 * a null or new object as appropriate. 378 * 379 * @param in The Parcel from which to read the UserHandle 380 * @return Returns a new UserHandle matching the previously written 381 * object, or null if a null had been written. 382 * 383 * @see #writeToParcel(UserHandle, Parcel) 384 */ 385 public static UserHandle readFromParcel(Parcel in) { 386 int h = in.readInt(); 387 return h != USER_NULL ? new UserHandle(h) : null; 388 } 389 390 public static final Parcelable.Creator<UserHandle> CREATOR 391 = new Parcelable.Creator<UserHandle>() { 392 public UserHandle createFromParcel(Parcel in) { 393 return new UserHandle(in); 394 } 395 396 public UserHandle[] newArray(int size) { 397 return new UserHandle[size]; 398 } 399 }; 400 401 /** 402 * Instantiate a new UserHandle from the data in a Parcel that was 403 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 404 * must not use this with data written by 405 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 406 * to handle a null UserHandle here. 407 * 408 * @param in The Parcel containing the previously written UserHandle, 409 * positioned at the location in the buffer where it was written. 410 */ 411 public UserHandle(Parcel in) { 412 mHandle = in.readInt(); 413 } 414} 415