UserHandle.java revision e66b6890ee59d108ae5c608f6ec8d4ad20cb06a8
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 java.io.PrintWriter; 20 21/** 22 * Representation of a user on the device. 23 */ 24public final class UserHandle implements Parcelable { 25 /** 26 * @hide Range of uids allocated for a user. 27 */ 28 public static final int PER_USER_RANGE = 100000; 29 30 /** @hide A user id to indicate all users on the device */ 31 public static final int USER_ALL = -1; 32 33 /** @hide A user handle to indicate all users on the device */ 34 public static final UserHandle ALL = new UserHandle(USER_ALL); 35 36 /** @hide A user id to indicate the currently active user */ 37 public static final int USER_CURRENT = -2; 38 39 /** @hide A user handle to indicate the current user of the device */ 40 public static final UserHandle CURRENT = new UserHandle(USER_CURRENT); 41 42 /** @hide A user id to indicate that we would like to send to the current 43 * user, but if this is calling from a user process then we will send it 44 * to the caller's user instead of failing wiht a security exception */ 45 public static final int USER_CURRENT_OR_SELF = -3; 46 47 /** @hide A user handle 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 wiht a security exception */ 50 public static final UserHandle CURRENT_OR_SELF = new UserHandle(USER_CURRENT_OR_SELF); 51 52 /** @hide An undefined user id */ 53 public static final int USER_NULL = -10000; 54 55 /** @hide A user id constant to indicate the "owner" user of the device */ 56 public static final int USER_OWNER = 0; 57 58 /** @hide A user handle to indicate the primary/owner user of the device */ 59 public static final UserHandle OWNER = new UserHandle(USER_OWNER); 60 61 /** 62 * @hide Enable multi-user related side effects. Set this to false if 63 * there are problems with single user use-cases. 64 */ 65 public static final boolean MU_ENABLED = true; 66 67 final int mHandle; 68 69 /** 70 * Checks to see if the user id is the same for the two uids, i.e., they belong to the same 71 * user. 72 * @hide 73 */ 74 public static final boolean isSameUser(int uid1, int uid2) { 75 return getUserId(uid1) == getUserId(uid2); 76 } 77 78 /** 79 * Checks to see if both uids are referring to the same app id, ignoring the user id part of the 80 * uids. 81 * @param uid1 uid to compare 82 * @param uid2 other uid to compare 83 * @return whether the appId is the same for both uids 84 * @hide 85 */ 86 public static final boolean isSameApp(int uid1, int uid2) { 87 return getAppId(uid1) == getAppId(uid2); 88 } 89 90 /** @hide */ 91 public static final boolean isIsolated(int uid) { 92 if (uid > 0) { 93 final int appId = getAppId(uid); 94 return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID; 95 } else { 96 return false; 97 } 98 } 99 100 /** @hide */ 101 public static boolean isApp(int uid) { 102 if (uid > 0) { 103 final int appId = getAppId(uid); 104 return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID; 105 } else { 106 return false; 107 } 108 } 109 110 /** 111 * Returns the user id for a given uid. 112 * @hide 113 */ 114 public static final int getUserId(int uid) { 115 if (MU_ENABLED) { 116 return uid / PER_USER_RANGE; 117 } else { 118 return 0; 119 } 120 } 121 122 /** @hide */ 123 public static final int getCallingUserId() { 124 return getUserId(Binder.getCallingUid()); 125 } 126 127 /** 128 * Returns the uid that is composed from the userId and the appId. 129 * @hide 130 */ 131 public static final int getUid(int userId, int appId) { 132 if (MU_ENABLED) { 133 return userId * PER_USER_RANGE + (appId % PER_USER_RANGE); 134 } else { 135 return appId; 136 } 137 } 138 139 /** 140 * Returns the app id (or base uid) for a given uid, stripping out the user id from it. 141 * @hide 142 */ 143 public static final int getAppId(int uid) { 144 return uid % PER_USER_RANGE; 145 } 146 147 /** 148 * Returns the gid shared between all apps with this userId. 149 * @hide 150 */ 151 public static final int getUserGid(int userId) { 152 return getUid(userId, Process.SHARED_USER_GID); 153 } 154 155 /** 156 * Returns the shared app gid for a given uid or appId. 157 * @hide 158 */ 159 public static final int getSharedAppGid(int id) { 160 return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE) 161 - Process.FIRST_APPLICATION_UID; 162 } 163 164 /** 165 * Generate a text representation of the uid, breaking out its individual 166 * components -- user, app, isolated, etc. 167 * @hide 168 */ 169 public static void formatUid(StringBuilder sb, int uid) { 170 if (uid < Process.FIRST_APPLICATION_UID) { 171 sb.append(uid); 172 } else { 173 sb.append('u'); 174 sb.append(getUserId(uid)); 175 final int appId = getAppId(uid); 176 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 177 sb.append('i'); 178 sb.append(appId - Process.FIRST_ISOLATED_UID); 179 } else if (appId >= Process.FIRST_APPLICATION_UID) { 180 sb.append('a'); 181 sb.append(appId - Process.FIRST_APPLICATION_UID); 182 } else { 183 sb.append('s'); 184 sb.append(appId); 185 } 186 } 187 } 188 189 /** 190 * Generate a text representation of the uid, breaking out its individual 191 * components -- user, app, isolated, etc. 192 * @hide 193 */ 194 public static void formatUid(PrintWriter pw, int uid) { 195 if (uid < Process.FIRST_APPLICATION_UID) { 196 pw.print(uid); 197 } else { 198 pw.print('u'); 199 pw.print(getUserId(uid)); 200 final int appId = getAppId(uid); 201 if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) { 202 pw.print('i'); 203 pw.print(appId - Process.FIRST_ISOLATED_UID); 204 } else if (appId >= Process.FIRST_APPLICATION_UID) { 205 pw.print('a'); 206 pw.print(appId - Process.FIRST_APPLICATION_UID); 207 } else { 208 pw.print('s'); 209 pw.print(appId); 210 } 211 } 212 } 213 214 /** 215 * Returns the user id of the current process 216 * @return user id of the current process 217 * @hide 218 */ 219 public static final int myUserId() { 220 return getUserId(Process.myUid()); 221 } 222 223 /** @hide */ 224 public UserHandle(int h) { 225 mHandle = h; 226 } 227 228 /** @hide */ 229 public int getIdentifier() { 230 return mHandle; 231 } 232 233 @Override 234 public String toString() { 235 return "UserHandle{" + mHandle + "}"; 236 } 237 238 @Override 239 public boolean equals(Object obj) { 240 try { 241 if (obj != null) { 242 UserHandle other = (UserHandle)obj; 243 return mHandle == other.mHandle; 244 } 245 } catch (ClassCastException e) { 246 } 247 return false; 248 } 249 250 @Override 251 public int hashCode() { 252 return mHandle; 253 } 254 255 public int describeContents() { 256 return 0; 257 } 258 259 public void writeToParcel(Parcel out, int flags) { 260 out.writeInt(mHandle); 261 } 262 263 /** 264 * Write a UserHandle to a Parcel, handling null pointers. Must be 265 * read with {@link #readFromParcel(Parcel)}. 266 * 267 * @param h The UserHandle to be written. 268 * @param out The Parcel in which the UserHandle will be placed. 269 * 270 * @see #readFromParcel(Parcel) 271 */ 272 public static void writeToParcel(UserHandle h, Parcel out) { 273 if (h != null) { 274 h.writeToParcel(out, 0); 275 } else { 276 out.writeInt(USER_NULL); 277 } 278 } 279 280 /** 281 * Read a UserHandle from a Parcel that was previously written 282 * with {@link #writeToParcel(UserHandle, Parcel)}, returning either 283 * a null or new object as appropriate. 284 * 285 * @param in The Parcel from which to read the UserHandle 286 * @return Returns a new UserHandle matching the previously written 287 * object, or null if a null had been written. 288 * 289 * @see #writeToParcel(UserHandle, Parcel) 290 */ 291 public static UserHandle readFromParcel(Parcel in) { 292 int h = in.readInt(); 293 return h != USER_NULL ? new UserHandle(h) : null; 294 } 295 296 public static final Parcelable.Creator<UserHandle> CREATOR 297 = new Parcelable.Creator<UserHandle>() { 298 public UserHandle createFromParcel(Parcel in) { 299 return new UserHandle(in); 300 } 301 302 public UserHandle[] newArray(int size) { 303 return new UserHandle[size]; 304 } 305 }; 306 307 /** 308 * Instantiate a new UserHandle from the data in a Parcel that was 309 * previously written with {@link #writeToParcel(Parcel, int)}. Note that you 310 * must not use this with data written by 311 * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible 312 * to handle a null UserHandle here. 313 * 314 * @param in The Parcel containing the previously written UserHandle, 315 * positioned at the location in the buffer where it was written. 316 */ 317 public UserHandle(Parcel in) { 318 mHandle = in.readInt(); 319 } 320} 321