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