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