UserHandle.java revision f02b60aa4f367516f40cf3d60fffae0c6fe3e1b8
1742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani/* 2742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Copyright (C) 2011 The Android Open Source Project 3742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * 4742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Licensed under the Apache License, Version 2.0 (the "License"); 5742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * you may not use this file except in compliance with the License. 6742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * You may obtain a copy of the License at 7742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * 8742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * http://www.apache.org/licenses/LICENSE-2.0 9742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * 10742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Unless required by applicable law or agreed to in writing, software 11742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * distributed under the License is distributed on an "AS IS" BASIS, 12742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * See the License for the specific language governing permissions and 14742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * limitations under the License. 15742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 16742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 17742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasanipackage android.os; 18742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 19742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani/** 20f02b60aa4f367516f40cf3d60fffae0c6fe3e1b8Dianne Hackborn * Representation of a user on the device. 21742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 22742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 23f02b60aa4f367516f40cf3d60fffae0c6fe3e1b8Dianne Hackbornpublic final class UserHandle { 24742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 25742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Range of IDs allocated for a user. 26742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * 27742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 28742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 29742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final int PER_USER_RANGE = 100000; 30742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 318264408f5995534f8e3147b001664ea0df52aaa5Amith Yamasani /** A user id to indicate all users on the device */ 32742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final int USER_ALL = -1; 33742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 348264408f5995534f8e3147b001664ea0df52aaa5Amith Yamasani /** A user id to indicate the currently active user */ 358264408f5995534f8e3147b001664ea0df52aaa5Amith Yamasani public static final int USER_CURRENT = -2; 368264408f5995534f8e3147b001664ea0df52aaa5Amith Yamasani 37aac71ff465399251fa8e830407f2917b986988d9Christopher Tate /** A user id constant to indicate the "owner" user of the device */ 38aac71ff465399251fa8e830407f2917b986988d9Christopher Tate public static final int USER_OWNER = 0; 398264408f5995534f8e3147b001664ea0df52aaa5Amith Yamasani 40742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 41742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Enable multi-user related side effects. Set this to false if there are problems with single 42742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * user usecases. 43742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * */ 44742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final boolean MU_ENABLED = true; 45742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 46742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 47742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Checks to see if the user id is the same for the two uids, i.e., they belong to the same 48742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * user. 49742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 50742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 51742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final boolean isSameUser(int uid1, int uid2) { 52742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return getUserId(uid1) == getUserId(uid2); 53742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 54742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 55742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 56742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Checks to see if both uids are referring to the same app id, ignoring the user id part of the 57742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * uids. 58742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @param uid1 uid to compare 59742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @param uid2 other uid to compare 60742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @return whether the appId is the same for both uids 61742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 62742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 63742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final boolean isSameApp(int uid1, int uid2) { 64742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return getAppId(uid1) == getAppId(uid2); 65742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 66742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 67a573f6a1d9b12393fbdfd2c0850499973849854bDianne Hackborn public static final boolean isIsolated(int uid) { 68a573f6a1d9b12393fbdfd2c0850499973849854bDianne Hackborn uid = getAppId(uid); 69a573f6a1d9b12393fbdfd2c0850499973849854bDianne Hackborn return uid >= Process.FIRST_ISOLATED_UID && uid <= Process.LAST_ISOLATED_UID; 70a573f6a1d9b12393fbdfd2c0850499973849854bDianne Hackborn } 71a573f6a1d9b12393fbdfd2c0850499973849854bDianne Hackborn 728a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey public static boolean isApp(int uid) { 738a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey if (uid > 0) { 74f02b60aa4f367516f40cf3d60fffae0c6fe3e1b8Dianne Hackborn uid = UserHandle.getAppId(uid); 758a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID; 768a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey } else { 778a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey return false; 788a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey } 798a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey } 808a8b581e669f6187b397f856a567a76ed8aba2c2Jeff Sharkey 81742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 82742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Returns the user id for a given uid. 83742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 84742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 85742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final int getUserId(int uid) { 86742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani if (MU_ENABLED) { 87742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return uid / PER_USER_RANGE; 88742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } else { 89742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return 0; 90742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 91742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 92742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 9337ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani public static final int getCallingUserId() { 9437ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani return getUserId(Binder.getCallingUid()); 9537ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani } 9637ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani 97742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 98742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Returns the uid that is composed from the userId and the appId. 99742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 100742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 101742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final int getUid(int userId, int appId) { 102742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani if (MU_ENABLED) { 103742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return userId * PER_USER_RANGE + (appId % PER_USER_RANGE); 104742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } else { 105742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return appId; 106742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 107742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 108742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani 109742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani /** 110742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * Returns the app id (or base uid) for a given uid, stripping out the user id from it. 111742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani * @hide 112742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani */ 113742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani public static final int getAppId(int uid) { 114742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani return uid % PER_USER_RANGE; 115742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani } 116483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani 117483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani /** 118483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani * Returns the user id of the current process 119483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani * @return user id of the current process 120483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani */ 121483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani public static final int myUserId() { 122483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani return getUserId(Process.myUid()); 123483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani } 124742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani} 125