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 /**
     20  * @hide
     21  */
     22 public final class UserId {
     23     /**
     24      * Range of IDs allocated for a user.
     25      *
     26      * @hide
     27      */
     28     public static final int PER_USER_RANGE = 100000;
     29 
     30     public static final int USER_ALL = -1;
     31 
     32     /**
     33      * Enable multi-user related side effects. Set this to false if there are problems with single
     34      * user usecases.
     35      * */
     36     public static final boolean MU_ENABLED = true;
     37 
     38     /**
     39      * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
     40      * user.
     41      * @hide
     42      */
     43     public static final boolean isSameUser(int uid1, int uid2) {
     44         return getUserId(uid1) == getUserId(uid2);
     45     }
     46 
     47     /**
     48      * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
     49      * uids.
     50      * @param uid1 uid to compare
     51      * @param uid2 other uid to compare
     52      * @return whether the appId is the same for both uids
     53      * @hide
     54      */
     55     public static final boolean isSameApp(int uid1, int uid2) {
     56         return getAppId(uid1) == getAppId(uid2);
     57     }
     58 
     59     public static final boolean isIsolated(int uid) {
     60         uid = getAppId(uid);
     61         return uid >= Process.FIRST_ISOLATED_UID && uid <= Process.LAST_ISOLATED_UID;
     62     }
     63 
     64     public static boolean isApp(int uid) {
     65         if (uid > 0) {
     66             uid = UserId.getAppId(uid);
     67             return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID;
     68         } else {
     69             return false;
     70         }
     71     }
     72 
     73     /**
     74      * Returns the user id for a given uid.
     75      * @hide
     76      */
     77     public static final int getUserId(int uid) {
     78         if (MU_ENABLED) {
     79             return uid / PER_USER_RANGE;
     80         } else {
     81             return 0;
     82         }
     83     }
     84 
     85     public static final int getCallingUserId() {
     86         return getUserId(Binder.getCallingUid());
     87     }
     88 
     89     /**
     90      * Returns the uid that is composed from the userId and the appId.
     91      * @hide
     92      */
     93     public static final int getUid(int userId, int appId) {
     94         if (MU_ENABLED) {
     95             return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
     96         } else {
     97             return appId;
     98         }
     99     }
    100 
    101     /**
    102      * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
    103      * @hide
    104      */
    105     public static final int getAppId(int uid) {
    106         return uid % PER_USER_RANGE;
    107     }
    108 
    109     /**
    110      * Returns the user id of the current process
    111      * @return user id of the current process
    112      */
    113     public static final int myUserId() {
    114         return getUserId(Process.myUid());
    115     }
    116 }
    117