UserHandle.java revision c1cf161af036e0f337b58ef0739a8ff2e42f01e7
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.util.SparseArray; 20 21import java.io.PrintWriter; 22import java.util.HashMap; 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 wiht 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 wiht 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 public static final int myUserId() { 237 return getUserId(Process.myUid()); 238 } 239 240 /** @hide */ 241 public UserHandle(int h) { 242 mHandle = h; 243 } 244 245 /** @hide */ 246 public int getIdentifier() { 247 return mHandle; 248 } 249 250 @Override 251 public String toString() { 252 return "UserHandle{" + mHandle + "}"; 253 } 254 255 @Override 256 public boolean equals(Object obj) { 257 try { 258 if (obj != null) { 259 UserHandle other = (UserHandle)obj; 260 return mHandle == other.mHandle; 261 } 262 } catch (ClassCastException e) { 263 } 264 return false; 265 } 266 267 @Override 268 public int hashCode() { 269 return mHandle; 270 } 271 272 public int describeContents() { 273 return 0; 274 } 275 276 public void writeToParcel(Parcel out, int flags) { 277 out.writeInt(mHandle); 278 } 279 280 /** 281 * Write a UserHandle to a Parcel, handling null pointers. Must be 282 * read with {@link #readFromParcel(Parcel)}. 283 * 284 * @param h The UserHandle to be written. 285 * @param out The Parcel in which the UserHandle will be placed. 286 * 287 * @see #readFromParcel(Parcel) 288 */ 289 public static void writeToParcel(UserHandle h, Parcel out) { 290 if (h != null) { 291 h.writeToParcel(out, 0); 292 } else { 293 out.writeInt(USER_NULL); 294 } 295 } 296 297 /** 298 * Read a UserHandle from a Parcel that was previously written 299 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 300 * a null or new object as appropriate. 301 * 302 * @param in The Parcel from which to read the UserHandle 303 * @return Returns a new UserHandle matching the previously written 304 * object, or null if a null had been written. 305 * 306 * @see #writeToParcel(UserHandle, Parcel) 307 */ 308 public static UserHandle readFromParcel(Parcel in) { 309 int h = in.readInt(); 310 return h != USER_NULL ? new UserHandle(h) : null; 311 } 312 313 public static final Parcelable.Creator<UserHandle> CREATOR 314 = new Parcelable.Creator<UserHandle>() { 315 public UserHandle createFromParcel(Parcel in) { 316 return new UserHandle(in); 317 } 318 319 public UserHandle[] newArray(int size) { 320 return new UserHandle[size]; 321 } 322 }; 323 324 /** 325 * Instantiate a new UserHandle from the data in a Parcel that was 326 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 327 * must not use this with data written by 328 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 329 * to handle a null UserHandle here. 330 * 331 * @param in The Parcel containing the previously written UserHandle, 332 * positioned at the location in the buffer where it was written. 333 */ 334 public UserHandle(Parcel in) { 335 mHandle = in.readInt(); 336 } 337} 338