UserHandle.java revision 7c69636c9a406265e1da368f3edfd8fb9651132c
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.SystemApi; 20import android.util.SparseArray; 21 22import java.io.PrintWriter; 23 24/** 25 * Representation of a user on the device. 26 */ 27public final class UserHandle implements Parcelable { 28 /** 29 * @hide Range of uids allocated for a user. 30 */ 31 public static final int PER_USER_RANGE = 100000; 32 33 /** @hide A user id to indicate all users on the device */ 34 public static final int USER_ALL = -1; 35 36 /** @hide A user handle to indicate all users on the device */ 37 public static final UserHandle ALL = new UserHandle(USER_ALL); 38 39 /** @hide A user id to indicate the currently active user */ 40 public static final int USER_CURRENT = -2; 41 42 /** @hide A user handle to indicate the current user of the device */ 43 public static final UserHandle CURRENT = new UserHandle(USER_CURRENT); 44 45 /** @hide A user id to indicate that we would like to send to the current 46 * user, but if this is calling from a user process then we will send it 47 * to the caller's user instead of failing with a security exception */ 48 public static final int USER_CURRENT_OR_SELF = -3; 49 50 /** @hide A user handle to indicate that we would like to send to the current 51 * user, but if this is calling from a user process then we will send it 52 * to the caller's user instead of failing with a security exception */ 53 public static final UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF); 54 55 /** @hide An undefined user id */ 56 public static final int USER_NULL = -10000; 57 58 /** 59 * @hide A user id constant to indicate the "owner" user of the device 60 * @deprecated Consider using either {@link UserHandle#USER_SYSTEM} constant or 61 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}. 62 */ 63 public static final int USER_OWNER = 0; 64 65 /** 66 * @hide A user handle to indicate the primary/owner user of the device 67 * @deprecated Consider using either {@link UserHandle#SYSTEM} constant or 68 * check the target user's flag {@link android.content.pm.UserInfo#isAdmin}. 69 */ 70 public static final UserHandle OWNER = new UserHandle(USER_OWNER); 71 72 /** @hide A user id constant to indicate the "system" user of the device */ 73 public static final int USER_SYSTEM = 0; 74 75 /** @hide A user handle to indicate the "system" user of the device */ 76 public static final UserHandle SYSTEM = new UserHandle(USER_SYSTEM); 77 78 /** 79 * @hide Enable multi-user related side effects. Set this to false if 80 * there are problems with single user use-cases. 81 */ 82 public static final boolean MU_ENABLED = true; 83 84 final int mHandle; 85 86 private static final SparseArray<UserHandle> userHandles = new SparseArray<UserHandle>(); 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 final 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 final boolean isSameApp(int uid1, int uid2) { 106 return getAppId(uid1) == getAppId(uid2); 107 } 108 109 /** @hide */ 110 public static final 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 final 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 final int getCallingUserId() { 143 return getUserId(Binder.getCallingUid()); 144 } 145 146 /** @hide */ 147 public static final UserHandle getCallingUserHandle() { 148 int userId = getUserId(Binder.getCallingUid()); 149 UserHandle userHandle = userHandles.get(userId); 150 // Intentionally not synchronized to save time 151 if (userHandle == null) { 152 userHandle = new UserHandle(userId); 153 userHandles.put(userId, userHandle); 154 } 155 return userHandle; 156 } 157 158 /** 159 * Returns the uid that is composed from the userId and the appId. 160 * @hide 161 */ 162 public static final int getUid(int userId, int appId) { 163 if (MU_ENABLED) { 164 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE); 165 } else { 166 return appId; 167 } 168 } 169 170 /** 171 * Returns the app id (or base uid) for a given uid, stripping out the user id from it. 172 * @hide 173 */ 174 public static final int getAppId(int uid) { 175 return uid % PER_USER_RANGE; 176 } 177 178 /** 179 * Returns the gid shared between all apps with this userId. 180 * @hide 181 */ 182 public static final int getUserGid(int userId) { 183 return getUid(userId, Process.SHARED_USER_GID); 184 } 185 186 /** 187 * Returns the shared app gid for a given uid or appId. 188 * @hide 189 */ 190 public static final int getSharedAppGid(int id) { 191 return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE) 192 - Process.FIRST_APPLICATION_UID; 193 } 194 195 /** 196 * Returns the app id for a given shared app gid. Returns -1 if the ID is invalid. 197 * @hide 198 */ 199 public static final int getAppIdFromSharedAppGid(int gid) { 200 final int appId = getAppId(gid) + Process.FIRST_APPLICATION_UID 201 - Process.FIRST_SHARED_APPLICATION_GID; 202 if (appId < 0 || appId >= Process.FIRST_SHARED_APPLICATION_GID) { 203 return -1; 204 } 205 return appId; 206 } 207 208 /** 209 * Generate a text representation of the uid, breaking out its individual 210 * components -- user, app, isolated, etc. 211 * @hide 212 */ 213 public static void formatUid(StringBuilder sb, int uid) { 214 if (uid < Process.FIRST_APPLICATION_UID) { 215 sb.append(uid); 216 } else { 217 sb.append('u'); 218 sb.append(getUserId(uid)); 219 final int appId = getAppId(uid); 220 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 221 sb.append('i'); 222 sb.append(appId - Process.FIRST_ISOLATED_UID); 223 } else if (appId >= Process.FIRST_APPLICATION_UID) { 224 sb.append('a'); 225 sb.append(appId - Process.FIRST_APPLICATION_UID); 226 } else { 227 sb.append('s'); 228 sb.append(appId); 229 } 230 } 231 } 232 233 /** 234 * Generate a text representation of the uid, breaking out its individual 235 * components -- user, app, isolated, etc. 236 * @hide 237 */ 238 public static String formatUid(int uid) { 239 StringBuilder sb = new StringBuilder(); 240 formatUid(sb, uid); 241 return sb.toString(); 242 } 243 244 /** 245 * Generate a text representation of the uid, breaking out its individual 246 * components -- user, app, isolated, etc. 247 * @hide 248 */ 249 public static void formatUid(PrintWriter pw, int uid) { 250 if (uid < Process.FIRST_APPLICATION_UID) { 251 pw.print(uid); 252 } else { 253 pw.print('u'); 254 pw.print(getUserId(uid)); 255 final int appId = getAppId(uid); 256 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 257 pw.print('i'); 258 pw.print(appId - Process.FIRST_ISOLATED_UID); 259 } else if (appId >= Process.FIRST_APPLICATION_UID) { 260 pw.print('a'); 261 pw.print(appId - Process.FIRST_APPLICATION_UID); 262 } else { 263 pw.print('s'); 264 pw.print(appId); 265 } 266 } 267 } 268 269 /** 270 * Returns the user id of the current process 271 * @return user id of the current process 272 * @hide 273 */ 274 @SystemApi 275 public static final int myUserId() { 276 return getUserId(Process.myUid()); 277 } 278 279 /** 280 * Returns true if this UserHandle refers to the owner user; false otherwise. 281 * @return true if this UserHandle refers to the owner user; false otherwise. 282 * @hide 283 * TODO: find an alternative to this Api. 284 */ 285 @SystemApi 286 public final boolean isOwner() { 287 return this.equals(OWNER); 288 } 289 290 /** @hide */ 291 public UserHandle(int h) { 292 mHandle = h; 293 } 294 295 /** 296 * Returns the userId stored in this UserHandle. 297 * @hide 298 */ 299 @SystemApi 300 public int getIdentifier() { 301 return mHandle; 302 } 303 304 @Override 305 public String toString() { 306 return "UserHandle{" + mHandle + "}"; 307 } 308 309 @Override 310 public boolean equals(Object obj) { 311 try { 312 if (obj != null) { 313 UserHandle other = (UserHandle)obj; 314 return mHandle == other.mHandle; 315 } 316 } catch (ClassCastException e) { 317 } 318 return false; 319 } 320 321 @Override 322 public int hashCode() { 323 return mHandle; 324 } 325 326 public int describeContents() { 327 return 0; 328 } 329 330 public void writeToParcel(Parcel out, int flags) { 331 out.writeInt(mHandle); 332 } 333 334 /** 335 * Write a UserHandle to a Parcel, handling null pointers. Must be 336 * read with {@link #readFromParcel(Parcel)}. 337 * 338 * @param h The UserHandle to be written. 339 * @param out The Parcel in which the UserHandle will be placed. 340 * 341 * @see #readFromParcel(Parcel) 342 */ 343 public static void writeToParcel(UserHandle h, Parcel out) { 344 if (h != null) { 345 h.writeToParcel(out, 0); 346 } else { 347 out.writeInt(USER_NULL); 348 } 349 } 350 351 /** 352 * Read a UserHandle from a Parcel that was previously written 353 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 354 * a null or new object as appropriate. 355 * 356 * @param in The Parcel from which to read the UserHandle 357 * @return Returns a new UserHandle matching the previously written 358 * object, or null if a null had been written. 359 * 360 * @see #writeToParcel(UserHandle, Parcel) 361 */ 362 public static UserHandle readFromParcel(Parcel in) { 363 int h = in.readInt(); 364 return h != USER_NULL ? new UserHandle(h) : null; 365 } 366 367 public static final Parcelable.Creator<UserHandle> CREATOR 368 = new Parcelable.Creator<UserHandle>() { 369 public UserHandle createFromParcel(Parcel in) { 370 return new UserHandle(in); 371 } 372 373 public UserHandle[] newArray(int size) { 374 return new UserHandle[size]; 375 } 376 }; 377 378 /** 379 * Instantiate a new UserHandle from the data in a Parcel that was 380 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 381 * must not use this with data written by 382 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 383 * to handle a null UserHandle here. 384 * 385 * @param in The Parcel containing the previously written UserHandle, 386 * positioned at the location in the buffer where it was written. 387 */ 388 public UserHandle(Parcel in) { 389 mHandle = in.readInt(); 390 } 391} 392