Home | History | Annotate | Download | only in os
      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 
     17 package android.os;
     18 
     19 import java.io.PrintWriter;
     20 
     21 /**
     22  * Representation of a user on the device.
     23  */
     24 public 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 shared app gid for a given uid or appId.
    149      * @hide
    150      */
    151     public static final int getSharedAppGid(int id) {
    152         return Process.FIRST_SHARED_APPLICATION_GID + (id % PER_USER_RANGE)
    153                 - Process.FIRST_APPLICATION_UID;
    154     }
    155 
    156     /**
    157      * Generate a text representation of the uid, breaking out its individual
    158      * components -- user, app, isolated, etc.
    159      * @hide
    160      */
    161     public static void formatUid(StringBuilder sb, int uid) {
    162         if (uid < Process.FIRST_APPLICATION_UID) {
    163             sb.append(uid);
    164         } else {
    165             sb.append('u');
    166             sb.append(getUserId(uid));
    167             final int appId = getAppId(uid);
    168             if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
    169                 sb.append('i');
    170                 sb.append(appId - Process.FIRST_ISOLATED_UID);
    171             } else {
    172                 sb.append('a');
    173                 sb.append(appId);
    174             }
    175         }
    176     }
    177 
    178     /**
    179      * Generate a text representation of the uid, breaking out its individual
    180      * components -- user, app, isolated, etc.
    181      * @hide
    182      */
    183     public static void formatUid(PrintWriter pw, int uid) {
    184         if (uid < Process.FIRST_APPLICATION_UID) {
    185             pw.print(uid);
    186         } else {
    187             pw.print('u');
    188             pw.print(getUserId(uid));
    189             final int appId = getAppId(uid);
    190             if (appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID) {
    191                 pw.print('i');
    192                 pw.print(appId - Process.FIRST_ISOLATED_UID);
    193             } else {
    194                 pw.print('a');
    195                 pw.print(appId);
    196             }
    197         }
    198     }
    199 
    200     /**
    201      * Returns the user id of the current process
    202      * @return user id of the current process
    203      * @hide
    204      */
    205     public static final int myUserId() {
    206         return getUserId(Process.myUid());
    207     }
    208 
    209     /** @hide */
    210     public UserHandle(int h) {
    211         mHandle = h;
    212     }
    213 
    214     /** @hide */
    215     public int getIdentifier() {
    216         return mHandle;
    217     }
    218 
    219     @Override
    220     public String toString() {
    221         return "UserHandle{" + mHandle + "}";
    222     }
    223 
    224     @Override
    225     public boolean equals(Object obj) {
    226         try {
    227             if (obj != null) {
    228                 UserHandle other = (UserHandle)obj;
    229                 return mHandle == other.mHandle;
    230             }
    231         } catch (ClassCastException e) {
    232         }
    233         return false;
    234     }
    235 
    236     @Override
    237     public int hashCode() {
    238         return mHandle;
    239     }
    240 
    241     public int describeContents() {
    242         return 0;
    243     }
    244 
    245     public void writeToParcel(Parcel out, int flags) {
    246         out.writeInt(mHandle);
    247     }
    248 
    249     /**
    250      * Write a UserHandle to a Parcel, handling null pointers.  Must be
    251      * read with {@link #readFromParcel(Parcel)}.
    252      *
    253      * @param h The UserHandle to be written.
    254      * @param out The Parcel in which the UserHandle will be placed.
    255      *
    256      * @see #readFromParcel(Parcel)
    257      */
    258     public static void writeToParcel(UserHandle h, Parcel out) {
    259         if (h != null) {
    260             h.writeToParcel(out, 0);
    261         } else {
    262             out.writeInt(USER_NULL);
    263         }
    264     }
    265 
    266     /**
    267      * Read a UserHandle from a Parcel that was previously written
    268      * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
    269      * a null or new object as appropriate.
    270      *
    271      * @param in The Parcel from which to read the UserHandle
    272      * @return Returns a new UserHandle matching the previously written
    273      * object, or null if a null had been written.
    274      *
    275      * @see #writeToParcel(UserHandle, Parcel)
    276      */
    277     public static UserHandle readFromParcel(Parcel in) {
    278         int h = in.readInt();
    279         return h != USER_NULL ? new UserHandle(h) : null;
    280     }
    281 
    282     public static final Parcelable.Creator<UserHandle> CREATOR
    283             = new Parcelable.Creator<UserHandle>() {
    284         public UserHandle createFromParcel(Parcel in) {
    285             return new UserHandle(in);
    286         }
    287 
    288         public UserHandle[] newArray(int size) {
    289             return new UserHandle[size];
    290         }
    291     };
    292 
    293     /**
    294      * Instantiate a new UserHandle from the data in a Parcel that was
    295      * previously written with {@link #writeToParcel(Parcel, int)}.  Note that you
    296      * must not use this with data written by
    297      * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
    298      * to handle a null UserHandle here.
    299      *
    300      * @param in The Parcel containing the previously written UserHandle,
    301      * positioned at the location in the buffer where it was written.
    302      */
    303     public UserHandle(Parcel in) {
    304         mHandle = in.readInt();
    305     }
    306 }
    307