Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame^] | 20 | * Representation of a user on the device. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 21 | * @hide |
| 22 | */ |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame^] | 23 | public final class UserHandle { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Range of IDs allocated for a user. |
| 26 | * |
| 27 | * @hide |
| 28 | */ |
| 29 | public static final int PER_USER_RANGE = 100000; |
| 30 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 31 | /** A user id to indicate all users on the device */ |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 32 | public static final int USER_ALL = -1; |
| 33 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 34 | /** A user id to indicate the currently active user */ |
| 35 | public static final int USER_CURRENT = -2; |
| 36 | |
Christopher Tate | aac71ff | 2012-08-13 17:36:14 -0700 | [diff] [blame] | 37 | /** A user id constant to indicate the "owner" user of the device */ |
| 38 | public static final int USER_OWNER = 0; |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 39 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 72 | public static boolean isApp(int uid) { |
| 73 | if (uid > 0) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame^] | 74 | uid = UserHandle.getAppId(uid); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 75 | return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID; |
| 76 | } else { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 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 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 93 | public static final int getCallingUserId() { |
| 94 | return getUserId(Binder.getCallingUid()); |
| 95 | } |
| 96 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 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 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 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 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 124 | } |