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.content.pm;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.os.SystemProperties;
22import android.os.UserHandle;
23import android.os.UserManager;
24
25/**
26 * Per-user information.
27 * @hide
28 */
29public class UserInfo implements Parcelable {
30
31    /** 16 bits for user type */
32    public static final int FLAG_MASK_USER_TYPE = 0x0000FFFF;
33
34    /**
35     * *************************** NOTE ***************************
36     * These flag values CAN NOT CHANGE because they are written
37     * directly to storage.
38     */
39
40    /**
41     * Primary user. Only one user can have this flag set. It identifies the first human user
42     * on a device.
43     */
44    public static final int FLAG_PRIMARY = 0x00000001;
45
46    /**
47     * User with administrative privileges. Such a user can create and
48     * delete users.
49     */
50    public static final int FLAG_ADMIN   = 0x00000002;
51
52    /**
53     * Indicates a guest user that may be transient.
54     */
55    public static final int FLAG_GUEST   = 0x00000004;
56
57    /**
58     * Indicates the user has restrictions in privileges, in addition to those for normal users.
59     * Exact meaning TBD. For instance, maybe they can't install apps or administer WiFi access pts.
60     */
61    public static final int FLAG_RESTRICTED = 0x00000008;
62
63    /**
64     * Indicates that this user has gone through its first-time initialization.
65     */
66    public static final int FLAG_INITIALIZED = 0x00000010;
67
68    /**
69     * Indicates that this user is a profile of another user, for example holding a users
70     * corporate data.
71     */
72    public static final int FLAG_MANAGED_PROFILE = 0x00000020;
73
74    /**
75     * Indicates that this user is disabled.
76     *
77     * <p>Note: If an ephemeral user is disabled, it shouldn't be later re-enabled. Ephemeral users
78     * are disabled as their removal is in progress to indicate that they shouldn't be re-entered.
79     */
80    public static final int FLAG_DISABLED = 0x00000040;
81
82    public static final int FLAG_QUIET_MODE = 0x00000080;
83
84    /**
85     * Indicates that this user is ephemeral. I.e. the user will be removed after leaving
86     * the foreground.
87     */
88    public static final int FLAG_EPHEMERAL = 0x00000100;
89
90    /**
91     * User is for demo purposes only and can be removed at any time.
92     */
93    public static final int FLAG_DEMO = 0x00000200;
94
95    public static final int NO_PROFILE_GROUP_ID = UserHandle.USER_NULL;
96
97    public int id;
98    public int serialNumber;
99    public String name;
100    public String iconPath;
101    public int flags;
102    public long creationTime;
103    public long lastLoggedInTime;
104    public String lastLoggedInFingerprint;
105    public int profileGroupId;
106    public int restrictedProfileParentId;
107
108    /** User is only partially created. */
109    public boolean partial;
110    public boolean guestToRemove;
111
112    public UserInfo(int id, String name, int flags) {
113        this(id, name, null, flags);
114    }
115
116    public UserInfo(int id, String name, String iconPath, int flags) {
117        this.id = id;
118        this.name = name;
119        this.flags = flags;
120        this.iconPath = iconPath;
121        this.profileGroupId = NO_PROFILE_GROUP_ID;
122        this.restrictedProfileParentId = NO_PROFILE_GROUP_ID;
123    }
124
125    public boolean isPrimary() {
126        return (flags & FLAG_PRIMARY) == FLAG_PRIMARY;
127    }
128
129    public boolean isAdmin() {
130        return (flags & FLAG_ADMIN) == FLAG_ADMIN;
131    }
132
133    public boolean isGuest() {
134        return (flags & FLAG_GUEST) == FLAG_GUEST;
135    }
136
137    public boolean isRestricted() {
138        return (flags & FLAG_RESTRICTED) == FLAG_RESTRICTED;
139    }
140
141    public boolean isManagedProfile() {
142        return (flags & FLAG_MANAGED_PROFILE) == FLAG_MANAGED_PROFILE;
143    }
144
145    public boolean isEnabled() {
146        return (flags & FLAG_DISABLED) != FLAG_DISABLED;
147    }
148
149    public boolean isQuietModeEnabled() {
150        return (flags & FLAG_QUIET_MODE) == FLAG_QUIET_MODE;
151    }
152
153    public boolean isEphemeral() {
154        return (flags & FLAG_EPHEMERAL) == FLAG_EPHEMERAL;
155    }
156
157    public boolean isInitialized() {
158        return (flags & FLAG_INITIALIZED) == FLAG_INITIALIZED;
159    }
160
161    public boolean isDemo() {
162        return (flags & FLAG_DEMO) == FLAG_DEMO;
163    }
164
165    /**
166     * Returns true if the user is a split system user.
167     * <p>If {@link UserManager#isSplitSystemUser split system user mode} is not enabled,
168     * the method always returns false.
169     */
170    public boolean isSystemOnly() {
171        return isSystemOnly(id);
172    }
173
174    /**
175     * Returns true if the given user is a split system user.
176     * <p>If {@link UserManager#isSplitSystemUser split system user mode} is not enabled,
177     * the method always returns false.
178     */
179    public static boolean isSystemOnly(int userId) {
180        return userId == UserHandle.USER_SYSTEM && UserManager.isSplitSystemUser();
181    }
182
183    /**
184     * @return true if this user can be switched to.
185     **/
186    public boolean supportsSwitchTo() {
187        if (isEphemeral() && !isEnabled()) {
188            // Don't support switching to an ephemeral user with removal in progress.
189            return false;
190        }
191        // TODO remove fw.show_hidden_users when we have finished developing managed profiles.
192        return !isManagedProfile() || SystemProperties.getBoolean("fw.show_hidden_users", false);
193    }
194
195    /**
196     * @return true if this user can be switched to by end user through UI.
197     */
198    public boolean supportsSwitchToByUser() {
199        // Hide the system user when it does not represent a human user.
200        boolean hideSystemUser = UserManager.isSplitSystemUser();
201        return (!hideSystemUser || id != UserHandle.USER_SYSTEM) && supportsSwitchTo();
202    }
203
204    /* @hide */
205    public boolean canHaveProfile() {
206        if (isManagedProfile() || isGuest() || isRestricted()) {
207            return false;
208        }
209        if (UserManager.isSplitSystemUser()) {
210            return id != UserHandle.USER_SYSTEM;
211        } else {
212            return id == UserHandle.USER_SYSTEM;
213        }
214    }
215
216    public UserInfo() {
217    }
218
219    public UserInfo(UserInfo orig) {
220        name = orig.name;
221        iconPath = orig.iconPath;
222        id = orig.id;
223        flags = orig.flags;
224        serialNumber = orig.serialNumber;
225        creationTime = orig.creationTime;
226        lastLoggedInTime = orig.lastLoggedInTime;
227        lastLoggedInFingerprint = orig.lastLoggedInFingerprint;
228        partial = orig.partial;
229        profileGroupId = orig.profileGroupId;
230        restrictedProfileParentId = orig.restrictedProfileParentId;
231        guestToRemove = orig.guestToRemove;
232    }
233
234    public UserHandle getUserHandle() {
235        return new UserHandle(id);
236    }
237
238    @Override
239    public String toString() {
240        return "UserInfo{" + id + ":" + name + ":" + Integer.toHexString(flags) + "}";
241    }
242
243    public int describeContents() {
244        return 0;
245    }
246
247    public void writeToParcel(Parcel dest, int parcelableFlags) {
248        dest.writeInt(id);
249        dest.writeString(name);
250        dest.writeString(iconPath);
251        dest.writeInt(flags);
252        dest.writeInt(serialNumber);
253        dest.writeLong(creationTime);
254        dest.writeLong(lastLoggedInTime);
255        dest.writeString(lastLoggedInFingerprint);
256        dest.writeInt(partial ? 1 : 0);
257        dest.writeInt(profileGroupId);
258        dest.writeInt(guestToRemove ? 1 : 0);
259        dest.writeInt(restrictedProfileParentId);
260    }
261
262    public static final Parcelable.Creator<UserInfo> CREATOR
263            = new Parcelable.Creator<UserInfo>() {
264        public UserInfo createFromParcel(Parcel source) {
265            return new UserInfo(source);
266        }
267        public UserInfo[] newArray(int size) {
268            return new UserInfo[size];
269        }
270    };
271
272    private UserInfo(Parcel source) {
273        id = source.readInt();
274        name = source.readString();
275        iconPath = source.readString();
276        flags = source.readInt();
277        serialNumber = source.readInt();
278        creationTime = source.readLong();
279        lastLoggedInTime = source.readLong();
280        lastLoggedInFingerprint = source.readString();
281        partial = source.readInt() != 0;
282        profileGroupId = source.readInt();
283        guestToRemove = source.readInt() != 0;
284        restrictedProfileParentId = source.readInt();
285    }
286}
287