FileDocCategorySizeDatePackage
UserHandle.javaAPI DocAndroid 5.1 API10988Thu Mar 12 22:22:10 GMT 2015android.os

UserHandle

public final class UserHandle extends Object implements Parcelable
Representation of a user on the device.

Fields Summary
public static final int
PER_USER_RANGE
public static final int
USER_ALL
public static final UserHandle
ALL
public static final int
USER_CURRENT
public static final UserHandle
CURRENT
public static final int
USER_CURRENT_OR_SELF
public static final UserHandle
CURRENT_OR_SELF
public static final int
USER_NULL
public static final int
USER_OWNER
public static final UserHandle
OWNER
public static final boolean
MU_ENABLED
final int
mHandle
private static final android.util.SparseArray
userHandles
public static final Parcelable.Creator
CREATOR
Constructors Summary
public UserHandle(int h)

hide

        mHandle = h;
    
public UserHandle(Parcel in)
Instantiate a new UserHandle from the data in a Parcel that was previously written with {@link #writeToParcel(Parcel, int)}. Note that you must not use this with data written by {@link #writeToParcel(UserHandle, Parcel)} since it is not possible to handle a null UserHandle here.

param
in The Parcel containing the previously written UserHandle, positioned at the location in the buffer where it was written.


                                                                          
       
        mHandle = in.readInt();
    
Methods Summary
public intdescribeContents()

        return 0;
    
public booleanequals(java.lang.Object obj)

        try {
            if (obj != null) {
                UserHandle other = (UserHandle)obj;
                return mHandle == other.mHandle;
            }
        } catch (ClassCastException e) {
        }
        return false;
    
public static voidformatUid(java.lang.StringBuilder sb, int uid)
Generate a text representation of the uid, breaking out its individual components -- user, app, isolated, etc.

hide

        if (uid < Process.FIRST_APPLICATION_UID) {
            sb.append(uid);
        } else {
            sb.append('u");
            sb.append(getUserId(uid));
            final int appId = getAppId(uid);
            if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
                sb.append('i");
                sb.append(appId - Process.FIRST_ISOLATED_UID);
            } else if (appId >= Process.FIRST_APPLICATION_UID) {
                sb.append('a");
                sb.append(appId - Process.FIRST_APPLICATION_UID);
            } else {
                sb.append('s");
                sb.append(appId);
            }
        }
    
public static voidformatUid(java.io.PrintWriter pw, int uid)
Generate a text representation of the uid, breaking out its individual components -- user, app, isolated, etc.

hide

        if (uid < Process.FIRST_APPLICATION_UID) {
            pw.print(uid);
        } else {
            pw.print('u");
            pw.print(getUserId(uid));
            final int appId = getAppId(uid);
            if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
                pw.print('i");
                pw.print(appId - Process.FIRST_ISOLATED_UID);
            } else if (appId >= Process.FIRST_APPLICATION_UID) {
                pw.print('a");
                pw.print(appId - Process.FIRST_APPLICATION_UID);
            } else {
                pw.print('s");
                pw.print(appId);
            }
        }
    
public static final intgetAppId(int uid)
Returns the app id (or base uid) for a given uid, stripping out the user id from it.

hide

        return uid % PER_USER_RANGE;
    
public static final android.os.UserHandlegetCallingUserHandle()

hide

        int userId = getUserId(Binder.getCallingUid());
        UserHandle userHandle = userHandles.get(userId);
        // Intentionally not synchronized to save time
        if (userHandle == null) {
            userHandle = new UserHandle(userId);
            userHandles.put(userId, userHandle);
        }
        return userHandle;
    
public static final intgetCallingUserId()

hide

        return getUserId(Binder.getCallingUid());
    
public intgetIdentifier()
Returns the userId stored in this UserHandle.

hide

        return mHandle;
    
public static final intgetSharedAppGid(int id)
Returns the shared app gid for a given uid or appId.

hide

        return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE)
                - Process.FIRST_APPLICATION_UID;
    
public static final intgetUid(int userId, int appId)
Returns the uid that is composed from the userId and the appId.

hide

        if (MU_ENABLED) {
            return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
        } else {
            return appId;
        }
    
public static final intgetUserGid(int userId)
Returns the gid shared between all apps with this userId.

hide

        return getUid(userId, Process.SHARED_USER_GID);
    
public static final intgetUserId(int uid)
Returns the user id for a given uid.

hide

        if (MU_ENABLED) {
            return uid / PER_USER_RANGE;
        } else {
            return 0;
        }
    
public inthashCode()

        return mHandle;
    
public static booleanisApp(int uid)

hide

        if (uid > 0) {
            final int appId = getAppId(uid);
            return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID;
        } else {
            return false;
        }
    
public static final booleanisIsolated(int uid)

hide

        if (uid > 0) {
            final int appId = getAppId(uid);
            return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID;
        } else {
            return false;
        }
    
public final booleanisOwner()
Returns true if this UserHandle refers to the owner user; false otherwise.

return
true if this UserHandle refers to the owner user; false otherwise.
hide

        return this.equals(OWNER);
    
public static final booleanisSameApp(int uid1, int uid2)
Checks to see if both uids are referring to the same app id, ignoring the user id part of the uids.

param
uid1 uid to compare
param
uid2 other uid to compare
return
whether the appId is the same for both uids
hide

        return getAppId(uid1) == getAppId(uid2);
    
public static final booleanisSameUser(int uid1, int uid2)
Checks to see if the user id is the same for the two uids, i.e., they belong to the same user.

hide


                               
            
        return getUserId(uid1) == getUserId(uid2);
    
public static final intmyUserId()
Returns the user id of the current process

return
user id of the current process
hide

        return getUserId(Process.myUid());
    
public static android.os.UserHandlereadFromParcel(Parcel in)
Read a UserHandle from a Parcel that was previously written with {@link #writeToParcel(UserHandle, Parcel)}, returning either a null or new object as appropriate.

param
in The Parcel from which to read the UserHandle
return
Returns a new UserHandle matching the previously written object, or null if a null had been written.
see
#writeToParcel(UserHandle, Parcel)

        int h = in.readInt();
        return h != USER_NULL ? new UserHandle(h) : null;
    
public java.lang.StringtoString()

        return "UserHandle{" + mHandle + "}";
    
public voidwriteToParcel(Parcel out, int flags)

        out.writeInt(mHandle);
    
public static voidwriteToParcel(android.os.UserHandle h, Parcel out)
Write a UserHandle to a Parcel, handling null pointers. Must be read with {@link #readFromParcel(Parcel)}.

param
h The UserHandle to be written.
param
out The Parcel in which the UserHandle will be placed.
see
#readFromParcel(Parcel)

        if (h != null) {
            h.writeToParcel(out, 0);
        } else {
            out.writeInt(USER_NULL);
        }