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