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