UserHandle.java revision e8e322fe3ce9159b31bb9f4d991a991a1469f7d5
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 USER_SYSTEM constant or 61 * UserInfo.isPrimary(). 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 SYSTEM constant or 68 * UserInfo.isPrimary(). 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. 197 * @hide 198 */ 199 public static final int getAppIdFromSharedAppGid(int gid) { 200 final int noUserGid = getAppId(gid); 201 if (noUserGid < Process.FIRST_SHARED_APPLICATION_GID || 202 noUserGid > Process.LAST_SHARED_APPLICATION_GID) { 203 throw new IllegalArgumentException(Integer.toString(gid) + " is not a shared app gid"); 204 } 205 return (noUserGid + Process.FIRST_APPLICATION_UID) - Process.FIRST_SHARED_APPLICATION_GID; 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 void formatUid(PrintWriter pw, int uid) { 239 if (uid < Process.FIRST_APPLICATION_UID) { 240 pw.print(uid); 241 } else { 242 pw.print('u'); 243 pw.print(getUserId(uid)); 244 final int appId = getAppId(uid); 245 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 246 pw.print('i'); 247 pw.print(appId - Process.FIRST_ISOLATED_UID); 248 } else if (appId >= Process.FIRST_APPLICATION_UID) { 249 pw.print('a'); 250 pw.print(appId - Process.FIRST_APPLICATION_UID); 251 } else { 252 pw.print('s'); 253 pw.print(appId); 254 } 255 } 256 } 257 258 /** 259 * Returns the user id of the current process 260 * @return user id of the current process 261 * @hide 262 */ 263 @SystemApi 264 public static final int myUserId() { 265 return getUserId(Process.myUid()); 266 } 267 268 /** 269 * Returns true if this UserHandle refers to the owner user; false otherwise. 270 * @return true if this UserHandle refers to the owner user; false otherwise. 271 * @hide 272 */ 273 @SystemApi 274 public final boolean isOwner() { 275 return this.equals(OWNER); 276 } 277 278 /** @hide */ 279 public UserHandle(int h) { 280 mHandle = h; 281 } 282 283 /** 284 * Returns the userId stored in this UserHandle. 285 * @hide 286 */ 287 @SystemApi 288 public int getIdentifier() { 289 return mHandle; 290 } 291 292 @Override 293 public String toString() { 294 return "UserHandle{" + mHandle + "}"; 295 } 296 297 @Override 298 public boolean equals(Object obj) { 299 try { 300 if (obj != null) { 301 UserHandle other = (UserHandle)obj; 302 return mHandle == other.mHandle; 303 } 304 } catch (ClassCastException e) { 305 } 306 return false; 307 } 308 309 @Override 310 public int hashCode() { 311 return mHandle; 312 } 313 314 public int describeContents() { 315 return 0; 316 } 317 318 public void writeToParcel(Parcel out, int flags) { 319 out.writeInt(mHandle); 320 } 321 322 /** 323 * Write a UserHandle to a Parcel, handling null pointers. Must be 324 * read with {@link #readFromParcel(Parcel)}. 325 * 326 * @param h The UserHandle to be written. 327 * @param out The Parcel in which the UserHandle will be placed. 328 * 329 * @see #readFromParcel(Parcel) 330 */ 331 public static void writeToParcel(UserHandle h, Parcel out) { 332 if (h != null) { 333 h.writeToParcel(out, 0); 334 } else { 335 out.writeInt(USER_NULL); 336 } 337 } 338 339 /** 340 * Read a UserHandle from a Parcel that was previously written 341 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 342 * a null or new object as appropriate. 343 * 344 * @param in The Parcel from which to read the UserHandle 345 * @return Returns a new UserHandle matching the previously written 346 * object, or null if a null had been written. 347 * 348 * @see #writeToParcel(UserHandle, Parcel) 349 */ 350 public static UserHandle readFromParcel(Parcel in) { 351 int h = in.readInt(); 352 return h != USER_NULL ? new UserHandle(h) : null; 353 } 354 355 public static final Parcelable.Creator<UserHandle> CREATOR 356 = new Parcelable.Creator<UserHandle>() { 357 public UserHandle createFromParcel(Parcel in) { 358 return new UserHandle(in); 359 } 360 361 public UserHandle[] newArray(int size) { 362 return new UserHandle[size]; 363 } 364 }; 365 366 /** 367 * Instantiate a new UserHandle from the data in a Parcel that was 368 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 369 * must not use this with data written by 370 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 371 * to handle a null UserHandle here. 372 * 373 * @param in The Parcel containing the previously written UserHandle, 374 * positioned at the location in the buffer where it was written. 375 */ 376 public UserHandle(Parcel in) { 377 mHandle = in.readInt(); 378 } 379} 380