UserInfo.java revision 2a764949c943681a4d25a17a0b203a0127a4a486
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;
23
24/**
25 * Per-user information.
26 * @hide
27 */
28public class UserInfo implements Parcelable {
29
30    /** 6 bits for user type */
31    public static final int FLAG_MASK_USER_TYPE = 0x0000003F;
32
33    /**
34     * *************************** NOTE ***************************
35     * These flag values CAN NOT CHANGE because they are written
36     * directly to storage.
37     */
38
39    /**
40     * Primary user. Only one user can have this flag set. Meaning of this
41     * flag TBD.
42     */
43    public static final int FLAG_PRIMARY = 0x00000001;
44
45    /**
46     * User with administrative privileges. Such a user can create and
47     * delete users.
48     */
49    public static final int FLAG_ADMIN   = 0x00000002;
50
51    /**
52     * Indicates a guest user that may be transient.
53     */
54    public static final int FLAG_GUEST   = 0x00000004;
55
56    /**
57     * Indicates the user has restrictions in privileges, in addition to those for normal users.
58     * Exact meaning TBD. For instance, maybe they can't install apps or administer WiFi access pts.
59     */
60    public static final int FLAG_RESTRICTED = 0x00000008;
61
62    /**
63     * Indicates that this user has gone through its first-time initialization.
64     */
65    public static final int FLAG_INITIALIZED = 0x00000010;
66
67    /**
68     * Indicates that this user is a profile of another user, for example holding a users
69     * corporate data.
70     */
71    public static final int FLAG_MANAGED_PROFILE = 0x00000020;
72
73
74    public static final int NO_PROFILE_GROUP_ID = -1;
75
76    public int id;
77    public int serialNumber;
78    public String name;
79    public String iconPath;
80    public int flags;
81    public long creationTime;
82    public long lastLoggedInTime;
83    public int profileGroupId;
84
85    /** User is only partially created. */
86    public boolean partial;
87
88    public UserInfo(int id, String name, int flags) {
89        this(id, name, null, flags);
90    }
91
92    public UserInfo(int id, String name, String iconPath, int flags) {
93        this.id = id;
94        this.name = name;
95        this.flags = flags;
96        this.iconPath = iconPath;
97        this.profileGroupId = NO_PROFILE_GROUP_ID;
98    }
99
100    public boolean isPrimary() {
101        return (flags & FLAG_PRIMARY) == FLAG_PRIMARY;
102    }
103
104    public boolean isAdmin() {
105        return (flags & FLAG_ADMIN) == FLAG_ADMIN;
106    }
107
108    public boolean isGuest() {
109        return (flags & FLAG_GUEST) == FLAG_GUEST;
110    }
111
112    public boolean isRestricted() {
113        return (flags & FLAG_RESTRICTED) == FLAG_RESTRICTED;
114    }
115
116    public boolean isManagedProfile() {
117        return (flags & FLAG_MANAGED_PROFILE) == FLAG_MANAGED_PROFILE;
118    }
119
120    /**
121     * @return true if this user can be switched to.
122     **/
123    public boolean supportsSwitchTo() {
124        // TODO remove fw.show_hidden_users when we have finished developing managed profiles.
125        return !isManagedProfile() || SystemProperties.getBoolean("fw.show_hidden_users", false);
126    }
127
128    public UserInfo() {
129    }
130
131    public UserInfo(UserInfo orig) {
132        name = orig.name;
133        iconPath = orig.iconPath;
134        id = orig.id;
135        flags = orig.flags;
136        serialNumber = orig.serialNumber;
137        creationTime = orig.creationTime;
138        lastLoggedInTime = orig.lastLoggedInTime;
139        partial = orig.partial;
140        profileGroupId = orig.profileGroupId;
141    }
142
143    public UserHandle getUserHandle() {
144        return new UserHandle(id);
145    }
146
147    @Override
148    public String toString() {
149        return "UserInfo{" + id + ":" + name + ":" + Integer.toHexString(flags) + "}";
150    }
151
152    public int describeContents() {
153        return 0;
154    }
155
156    public void writeToParcel(Parcel dest, int parcelableFlags) {
157        dest.writeInt(id);
158        dest.writeString(name);
159        dest.writeString(iconPath);
160        dest.writeInt(flags);
161        dest.writeInt(serialNumber);
162        dest.writeLong(creationTime);
163        dest.writeLong(lastLoggedInTime);
164        dest.writeInt(partial ? 1 : 0);
165        dest.writeInt(profileGroupId);
166    }
167
168    public static final Parcelable.Creator<UserInfo> CREATOR
169            = new Parcelable.Creator<UserInfo>() {
170        public UserInfo createFromParcel(Parcel source) {
171            return new UserInfo(source);
172        }
173        public UserInfo[] newArray(int size) {
174            return new UserInfo[size];
175        }
176    };
177
178    private UserInfo(Parcel source) {
179        id = source.readInt();
180        name = source.readString();
181        iconPath = source.readString();
182        flags = source.readInt();
183        serialNumber = source.readInt();
184        creationTime = source.readLong();
185        lastLoggedInTime = source.readLong();
186        partial = source.readInt() != 0;
187        profileGroupId = source.readInt();
188    }
189}
190