UserHandle.java revision 08c7116ab9cd04ad6dd3c04aa1017237e7f409ac
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 /** @hide A user id constant to indicate the "owner" user of the device */ 59 public static final int USER_OWNER = 0; 60 61 /** @hide A user handle to indicate the primary/owner user of the device */ 62 public static final UserHandle OWNER = new UserHandle(USER_OWNER); 63 64 /** 65 * @hide Enable multi-user related side effects. Set this to false if 66 * there are problems with single user use-cases. 67 */ 68 public static final boolean MU_ENABLED = true; 69 70 final int mHandle; 71 72 private static final SparseArray<UserHandle> userHandles = new SparseArray<UserHandle>(); 73 74 /** 75 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same 76 * user. 77 * @hide 78 */ 79 public static final boolean isSameUser(int uid1, int uid2) { 80 return getUserId(uid1) == getUserId(uid2); 81 } 82 83 /** 84 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the 85 * uids. 86 * @param uid1 uid to compare 87 * @param uid2 other uid to compare 88 * @return whether the appId is the same for both uids 89 * @hide 90 */ 91 public static final boolean isSameApp(int uid1, int uid2) { 92 return getAppId(uid1) == getAppId(uid2); 93 } 94 95 /** @hide */ 96 public static final boolean isIsolated(int uid) { 97 if (uid > 0) { 98 final int appId = getAppId(uid); 99 return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID; 100 } else { 101 return false; 102 } 103 } 104 105 /** @hide */ 106 public static boolean isApp(int uid) { 107 if (uid > 0) { 108 final int appId = getAppId(uid); 109 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID; 110 } else { 111 return false; 112 } 113 } 114 115 /** 116 * Returns the user id for a given uid. 117 * @hide 118 */ 119 public static final int getUserId(int uid) { 120 if (MU_ENABLED) { 121 return uid / PER_USER_RANGE; 122 } else { 123 return 0; 124 } 125 } 126 127 /** @hide */ 128 public static final int getCallingUserId() { 129 return getUserId(Binder.getCallingUid()); 130 } 131 132 /** @hide */ 133 public static final UserHandle getCallingUserHandle() { 134 int userId = getUserId(Binder.getCallingUid()); 135 UserHandle userHandle = userHandles.get(userId); 136 // Intentionally not synchronized to save time 137 if (userHandle == null) { 138 userHandle = new UserHandle(userId); 139 userHandles.put(userId, userHandle); 140 } 141 return userHandle; 142 } 143 144 /** 145 * Returns the uid that is composed from the userId and the appId. 146 * @hide 147 */ 148 public static final int getUid(int userId, int appId) { 149 if (MU_ENABLED) { 150 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE); 151 } else { 152 return appId; 153 } 154 } 155 156 /** 157 * Returns the app id (or base uid) for a given uid, stripping out the user id from it. 158 * @hide 159 */ 160 public static final int getAppId(int uid) { 161 return uid % PER_USER_RANGE; 162 } 163 164 /** 165 * Returns the gid shared between all apps with this userId. 166 * @hide 167 */ 168 public static final int getUserGid(int userId) { 169 return getUid(userId, Process.SHARED_USER_GID); 170 } 171 172 /** 173 * Returns the shared app gid for a given uid or appId. 174 * @hide 175 */ 176 public static final int getSharedAppGid(int id) { 177 return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE) 178 - Process.FIRST_APPLICATION_UID; 179 } 180 181 /** 182 * Generate a text representation of the uid, breaking out its individual 183 * components -- user, app, isolated, etc. 184 * @hide 185 */ 186 public static void formatUid(StringBuilder sb, int uid) { 187 if (uid < Process.FIRST_APPLICATION_UID) { 188 sb.append(uid); 189 } else { 190 sb.append('u'); 191 sb.append(getUserId(uid)); 192 final int appId = getAppId(uid); 193 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 194 sb.append('i'); 195 sb.append(appId - Process.FIRST_ISOLATED_UID); 196 } else if (appId >= Process.FIRST_APPLICATION_UID) { 197 sb.append('a'); 198 sb.append(appId - Process.FIRST_APPLICATION_UID); 199 } else { 200 sb.append('s'); 201 sb.append(appId); 202 } 203 } 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(PrintWriter pw, int uid) { 212 if (uid < Process.FIRST_APPLICATION_UID) { 213 pw.print(uid); 214 } else { 215 pw.print('u'); 216 pw.print(getUserId(uid)); 217 final int appId = getAppId(uid); 218 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 219 pw.print('i'); 220 pw.print(appId - Process.FIRST_ISOLATED_UID); 221 } else if (appId >= Process.FIRST_APPLICATION_UID) { 222 pw.print('a'); 223 pw.print(appId - Process.FIRST_APPLICATION_UID); 224 } else { 225 pw.print('s'); 226 pw.print(appId); 227 } 228 } 229 } 230 231 /** 232 * Returns the user id of the current process 233 * @return user id of the current process 234 * @hide 235 */ 236 @SystemApi 237 public static final int myUserId() { 238 return getUserId(Process.myUid()); 239 } 240 241 /** 242 * Returns true if this UserHandle refers to the owner user; false otherwise. 243 * @return true if this UserHandle refers to the owner user; false otherwise. 244 * @hide 245 */ 246 @SystemApi 247 public final boolean isOwner() { 248 return this.equals(OWNER); 249 } 250 251 /** @hide */ 252 public UserHandle(int h) { 253 mHandle = h; 254 } 255 256 /** 257 * Returns the userId stored in this UserHandle. 258 * @hide 259 */ 260 @SystemApi 261 public int getIdentifier() { 262 return mHandle; 263 } 264 265 @Override 266 public String toString() { 267 return "UserHandle{" + mHandle + "}"; 268 } 269 270 @Override 271 public boolean equals(Object obj) { 272 try { 273 if (obj != null) { 274 UserHandle other = (UserHandle)obj; 275 return mHandle == other.mHandle; 276 } 277 } catch (ClassCastException e) { 278 } 279 return false; 280 } 281 282 @Override 283 public int hashCode() { 284 return mHandle; 285 } 286 287 public int describeContents() { 288 return 0; 289 } 290 291 public void writeToParcel(Parcel out, int flags) { 292 out.writeInt(mHandle); 293 } 294 295 /** 296 * Write a UserHandle to a Parcel, handling null pointers. Must be 297 * read with {@link #readFromParcel(Parcel)}. 298 * 299 * @param h The UserHandle to be written. 300 * @param out The Parcel in which the UserHandle will be placed. 301 * 302 * @see #readFromParcel(Parcel) 303 */ 304 public static void writeToParcel(UserHandle h, Parcel out) { 305 if (h != null) { 306 h.writeToParcel(out, 0); 307 } else { 308 out.writeInt(USER_NULL); 309 } 310 } 311 312 /** 313 * Read a UserHandle from a Parcel that was previously written 314 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 315 * a null or new object as appropriate. 316 * 317 * @param in The Parcel from which to read the UserHandle 318 * @return Returns a new UserHandle matching the previously written 319 * object, or null if a null had been written. 320 * 321 * @see #writeToParcel(UserHandle, Parcel) 322 */ 323 public static UserHandle readFromParcel(Parcel in) { 324 int h = in.readInt(); 325 return h != USER_NULL ? new UserHandle(h) : null; 326 } 327 328 public static final Parcelable.Creator<UserHandle> CREATOR 329 = new Parcelable.Creator<UserHandle>() { 330 public UserHandle createFromParcel(Parcel in) { 331 return new UserHandle(in); 332 } 333 334 public UserHandle[] newArray(int size) { 335 return new UserHandle[size]; 336 } 337 }; 338 339 /** 340 * Instantiate a new UserHandle from the data in a Parcel that was 341 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 342 * must not use this with data written by 343 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 344 * to handle a null UserHandle here. 345 * 346 * @param in The Parcel containing the previously written UserHandle, 347 * positioned at the location in the buffer where it was written. 348 */ 349 public UserHandle(Parcel in) { 350 mHandle = in.readInt(); 351 } 352} 353