UserHandle.java revision 7767eac3232ba2fb9828766813cdb481d6a97584
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 */
22public final class UserHandle implements Parcelable {
23    /**
24     * @hide Range of uids allocated for a user.
25     */
26    public static final int PER_USER_RANGE = 100000;
27
28    /** @hide A user id to indicate all users on the device */
29    public static final int USER_ALL = -1;
30
31    /** @hide A user handle to indicate all users on the device */
32    public static final UserHandle ALL = new UserHandle(USER_ALL);
33
34    /** @hide A user id to indicate the currently active user */
35    public static final int USER_CURRENT = -2;
36
37    /** @hide An undefined user id */
38    public static final int USER_NULL = -10000;
39
40    /** @hide A user id constant to indicate the "owner" user of the device */
41    public static final int USER_OWNER = 0;
42
43    /**
44     * @hide Enable multi-user related side effects. Set this to false if
45     * there are problems with single user use-cases.
46     */
47    public static final boolean MU_ENABLED = true;
48
49    final int mHandle;
50
51    /**
52     * Checks to see if the user id is the same for the two uids, i.e., they belong to the same
53     * user.
54     * @hide
55     */
56    public static final boolean isSameUser(int uid1, int uid2) {
57        return getUserId(uid1) == getUserId(uid2);
58    }
59
60    /**
61     * Checks to see if both uids are referring to the same app id, ignoring the user id part of the
62     * uids.
63     * @param uid1 uid to compare
64     * @param uid2 other uid to compare
65     * @return whether the appId is the same for both uids
66     * @hide
67     */
68    public static final boolean isSameApp(int uid1, int uid2) {
69        return getAppId(uid1) == getAppId(uid2);
70    }
71
72    /** @hide */
73    public static final boolean isIsolated(int uid) {
74        uid = getAppId(uid);
75        return uid >= Process.FIRST_ISOLATED_UID && uid <= Process.LAST_ISOLATED_UID;
76    }
77
78    /** @hide */
79    public static boolean isApp(int uid) {
80        if (uid > 0) {
81            uid = UserHandle.getAppId(uid);
82            return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID;
83        } else {
84            return false;
85        }
86    }
87
88    /**
89     * Returns the user id for a given uid.
90     * @hide
91     */
92    public static final int getUserId(int uid) {
93        if (MU_ENABLED) {
94            return uid / PER_USER_RANGE;
95        } else {
96            return 0;
97        }
98    }
99
100    /** @hide */
101    public static final int getCallingUserId() {
102        return getUserId(Binder.getCallingUid());
103    }
104
105    /**
106     * Returns the uid that is composed from the userId and the appId.
107     * @hide
108     */
109    public static final int getUid(int userId, int appId) {
110        if (MU_ENABLED) {
111            return userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
112        } else {
113            return appId;
114        }
115    }
116
117    /**
118     * Returns the app id (or base uid) for a given uid, stripping out the user id from it.
119     * @hide
120     */
121    public static final int getAppId(int uid) {
122        return uid % PER_USER_RANGE;
123    }
124
125    /**
126     * Returns the user id of the current process
127     * @return user id of the current process
128     * @hide
129     */
130    public static final int myUserId() {
131        return getUserId(Process.myUid());
132    }
133
134    /** @hide */
135    public UserHandle(int h) {
136        mHandle = h;
137    }
138
139    /** @hide */
140    public int getIdentifier() {
141        return mHandle;
142    }
143
144    @Override
145    public String toString() {
146        return "UserHandle{" + mHandle + "}";
147    }
148
149    @Override
150    public boolean equals(Object obj) {
151        try {
152            if (obj != null) {
153                UserHandle other = (UserHandle)obj;
154                return mHandle == other.mHandle;
155            }
156        } catch (ClassCastException e) {
157        }
158        return false;
159    }
160
161    @Override
162    public int hashCode() {
163        return mHandle;
164    }
165
166    public int describeContents() {
167        return 0;
168    }
169
170    public void writeToParcel(Parcel out, int flags) {
171        out.writeInt(mHandle);
172    }
173
174    /**
175     * Write a UserHandle to a Parcel, handling null pointers.  Must be
176     * read with {@link #readFromParcel(Parcel)}.
177     *
178     * @param h The UserHandle to be written.
179     * @param out The Parcel in which the UserHandle will be placed.
180     *
181     * @see #readFromParcel(Parcel)
182     */
183    public static void writeToParcel(UserHandle h, Parcel out) {
184        if (h != null) {
185            h.writeToParcel(out, 0);
186        } else {
187            out.writeInt(USER_NULL);
188        }
189    }
190
191    /**
192     * Read a UserHandle from a Parcel that was previously written
193     * with {@link #writeToParcel(UserHandle, Parcel)}, returning either
194     * a null or new object as appropriate.
195     *
196     * @param in The Parcel from which to read the UserHandle
197     * @return Returns a new UserHandle matching the previously written
198     * object, or null if a null had been written.
199     *
200     * @see #writeToParcel(UserHandle, Parcel)
201     */
202    public static UserHandle readFromParcel(Parcel in) {
203        int h = in.readInt();
204        return h != USER_NULL ? new UserHandle(h) : null;
205    }
206
207    public static final Parcelable.Creator<UserHandle> CREATOR
208            = new Parcelable.Creator<UserHandle>() {
209        public UserHandle createFromParcel(Parcel in) {
210            return new UserHandle(in);
211        }
212
213        public UserHandle[] newArray(int size) {
214            return new UserHandle[size];
215        }
216    };
217
218    /**
219     * Instantiate a new UserHandle from the data in a Parcel that was
220     * previously written with {@link #writeToParcel(Parcel, int)}.  Note that you
221     * must not use this with data written by
222     * {@link #writeToParcel(UserHandle, Parcel)} since it is not possible
223     * to handle a null UserHandle here.
224     *
225     * @param in The Parcel containing the previously written UserHandle,
226     * positioned at the location in the buffer where it was written.
227     */
228    public UserHandle(Parcel in) {
229        mHandle = in.readInt();
230    }
231}
232