blob: 2b1b32ed3df29938e54260aa0ed651e018ce3edb [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
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
Mathew Inwood5c0d3542018-08-14 13:54:31 +010019import android.annotation.UnsupportedAppUsage;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070020import android.os.Parcel;
21import android.os.Parcelable;
Kenny Guy1a447532014-02-20 21:55:32 +000022import android.os.SystemProperties;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070023import android.os.UserHandle;
Xiaohui Chen7cb69df2015-07-13 16:01:01 -070024import android.os.UserManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070025
26/**
27 * Per-user information.
28 * @hide
29 */
30public class UserInfo implements Parcelable {
Amith Yamasani258848d2012-08-10 17:06:33 -070031
Amith Yamasanieb437d42016-04-29 09:31:25 -070032 /** 16 bits for user type */
33 public static final int FLAG_MASK_USER_TYPE = 0x0000FFFF;
Amith Yamasani258848d2012-08-10 17:06:33 -070034
Amith Yamasani4b2e9342011-03-31 12:38:53 -070035 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070036 * *************************** NOTE ***************************
37 * These flag values CAN NOT CHANGE because they are written
38 * directly to storage.
39 */
40
41 /**
Xiaohui Chen70f6c382015-04-28 14:21:43 -070042 * Primary user. Only one user can have this flag set. It identifies the first human user
43 * on a device.
Amith Yamasani4b2e9342011-03-31 12:38:53 -070044 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +010045 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -070046 public static final int FLAG_PRIMARY = 0x00000001;
47
48 /**
49 * User with administrative privileges. Such a user can create and
50 * delete users.
51 */
52 public static final int FLAG_ADMIN = 0x00000002;
53
54 /**
55 * Indicates a guest user that may be transient.
56 */
57 public static final int FLAG_GUEST = 0x00000004;
58
Amith Yamasani258848d2012-08-10 17:06:33 -070059 /**
60 * Indicates the user has restrictions in privileges, in addition to those for normal users.
61 * Exact meaning TBD. For instance, maybe they can't install apps or administer WiFi access pts.
62 */
63 public static final int FLAG_RESTRICTED = 0x00000008;
64
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070065 /**
66 * Indicates that this user has gone through its first-time initialization.
67 */
68 public static final int FLAG_INITIALIZED = 0x00000010;
69
Kenny Guya52dc3e2014-02-11 15:33:14 +000070 /**
71 * Indicates that this user is a profile of another user, for example holding a users
72 * corporate data.
73 */
74 public static final int FLAG_MANAGED_PROFILE = 0x00000020;
75
Alexandra Gherghinadf35d572014-04-09 13:54:39 +010076 /**
77 * Indicates that this user is disabled.
Lenka Trochtova1ddda472016-02-12 10:42:12 +010078 *
79 * <p>Note: If an ephemeral user is disabled, it shouldn't be later re-enabled. Ephemeral users
80 * are disabled as their removal is in progress to indicate that they shouldn't be re-entered.
Alexandra Gherghinadf35d572014-04-09 13:54:39 +010081 */
82 public static final int FLAG_DISABLED = 0x00000040;
83
Rubin Xu0a29ecd2015-11-04 15:11:48 +000084 public static final int FLAG_QUIET_MODE = 0x00000080;
Kenny Guya52dc3e2014-02-11 15:33:14 +000085
Lenka Trochtovac4dd0212015-11-18 12:22:06 +010086 /**
87 * Indicates that this user is ephemeral. I.e. the user will be removed after leaving
88 * the foreground.
89 */
90 public static final int FLAG_EPHEMERAL = 0x00000100;
91
Amith Yamasanieb437d42016-04-29 09:31:25 -070092 /**
93 * User is for demo purposes only and can be removed at any time.
94 */
95 public static final int FLAG_DEMO = 0x00000200;
96
Fyodor Kupolov06a484a2015-08-21 16:33:20 -070097 public static final int NO_PROFILE_GROUP_ID = UserHandle.USER_NULL;
Kenny Guya52dc3e2014-02-11 15:33:14 +000098
Mathew Inwood5c0d3542018-08-14 13:54:31 +010099 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700100 public int id;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100101 @UnsupportedAppUsage
Amith Yamasani2a003292012-08-14 18:25:45 -0700102 public int serialNumber;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100103 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700104 public String name;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100105 @UnsupportedAppUsage
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700106 public String iconPath;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100107 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700108 public int flags;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100109 @UnsupportedAppUsage
Amith Yamasani920ace02012-09-20 22:15:37 -0700110 public long creationTime;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100111 @UnsupportedAppUsage
Amith Yamasani920ace02012-09-20 22:15:37 -0700112 public long lastLoggedInTime;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600113 public String lastLoggedInFingerprint;
Tony Makd3986be2016-11-01 18:12:10 +0000114 /**
115 * If this user is a parent user, it would be its own user id.
116 * If this user is a child user, it would be its parent user id.
117 * Otherwise, it would be {@link #NO_PROFILE_GROUP_ID}.
118 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100119 @UnsupportedAppUsage
Kenny Guy2a764942014-04-02 13:29:20 +0100120 public int profileGroupId;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -0700121 public int restrictedProfileParentId;
Kenny Guy02c89902016-11-15 19:36:38 +0000122 /** Which profile badge color/label to use. */
123 public int profileBadge;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700124
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700125 /** User is only partially created. */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100126 @UnsupportedAppUsage
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700127 public boolean partial;
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100128 @UnsupportedAppUsage
Adam Lesinskieddeb492014-09-08 17:50:03 -0700129 public boolean guestToRemove;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700130
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100131 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700132 public UserInfo(int id, String name, int flags) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700133 this(id, name, null, flags);
134 }
135
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100136 @UnsupportedAppUsage
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700137 public UserInfo(int id, String name, String iconPath, int flags) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700138 this.id = id;
139 this.name = name;
140 this.flags = flags;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700141 this.iconPath = iconPath;
Kenny Guy2a764942014-04-02 13:29:20 +0100142 this.profileGroupId = NO_PROFILE_GROUP_ID;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -0700143 this.restrictedProfileParentId = NO_PROFILE_GROUP_ID;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700144 }
145
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100146 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700147 public boolean isPrimary() {
148 return (flags & FLAG_PRIMARY) == FLAG_PRIMARY;
149 }
150
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100151 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700152 public boolean isAdmin() {
153 return (flags & FLAG_ADMIN) == FLAG_ADMIN;
154 }
155
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100156 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700157 public boolean isGuest() {
158 return (flags & FLAG_GUEST) == FLAG_GUEST;
159 }
160
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100161 @UnsupportedAppUsage
Amith Yamasani67df64b2012-12-14 12:09:36 -0800162 public boolean isRestricted() {
163 return (flags & FLAG_RESTRICTED) == FLAG_RESTRICTED;
164 }
165
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100166 @UnsupportedAppUsage
Kenny Guya52dc3e2014-02-11 15:33:14 +0000167 public boolean isManagedProfile() {
168 return (flags & FLAG_MANAGED_PROFILE) == FLAG_MANAGED_PROFILE;
169 }
170
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100171 @UnsupportedAppUsage
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100172 public boolean isEnabled() {
173 return (flags & FLAG_DISABLED) != FLAG_DISABLED;
174 }
175
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000176 public boolean isQuietModeEnabled() {
177 return (flags & FLAG_QUIET_MODE) == FLAG_QUIET_MODE;
178 }
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100179
180 public boolean isEphemeral() {
181 return (flags & FLAG_EPHEMERAL) == FLAG_EPHEMERAL;
182 }
183
Amith Yamasani12747872015-12-07 14:19:49 -0800184 public boolean isInitialized() {
185 return (flags & FLAG_INITIALIZED) == FLAG_INITIALIZED;
186 }
187
Amith Yamasanieb437d42016-04-29 09:31:25 -0700188 public boolean isDemo() {
189 return (flags & FLAG_DEMO) == FLAG_DEMO;
190 }
191
Kenny Guy1a447532014-02-20 21:55:32 +0000192 /**
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700193 * Returns true if the user is a split system user.
194 * <p>If {@link UserManager#isSplitSystemUser split system user mode} is not enabled,
195 * the method always returns false.
196 */
197 public boolean isSystemOnly() {
Xiaohui Chena4490622015-09-22 15:29:31 -0700198 return isSystemOnly(id);
199 }
200
201 /**
202 * Returns true if the given user is a split system user.
203 * <p>If {@link UserManager#isSplitSystemUser split system user mode} is not enabled,
204 * the method always returns false.
205 */
206 public static boolean isSystemOnly(int userId) {
207 return userId == UserHandle.USER_SYSTEM && UserManager.isSplitSystemUser();
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700208 }
209
210 /**
Kenny Guy1a447532014-02-20 21:55:32 +0000211 * @return true if this user can be switched to.
212 **/
213 public boolean supportsSwitchTo() {
Lenka Trochtova1ddda472016-02-12 10:42:12 +0100214 if (isEphemeral() && !isEnabled()) {
215 // Don't support switching to an ephemeral user with removal in progress.
216 return false;
217 }
Robin Lee434a74f2016-08-04 15:58:08 +0100218 return !isManagedProfile();
Kenny Guy1a447532014-02-20 21:55:32 +0000219 }
220
Xiaohui Chen7cb69df2015-07-13 16:01:01 -0700221 /**
222 * @return true if this user can be switched to by end user through UI.
223 */
224 public boolean supportsSwitchToByUser() {
225 // Hide the system user when it does not represent a human user.
226 boolean hideSystemUser = UserManager.isSplitSystemUser();
227 return (!hideSystemUser || id != UserHandle.USER_SYSTEM) && supportsSwitchTo();
228 }
229
Nicolas Prevotb8186812015-08-06 15:00:03 +0100230 /* @hide */
231 public boolean canHaveProfile() {
232 if (isManagedProfile() || isGuest() || isRestricted()) {
233 return false;
234 }
235 if (UserManager.isSplitSystemUser()) {
236 return id != UserHandle.USER_SYSTEM;
Xiaohui Chenbd0e03b2015-08-21 09:19:49 -0700237 } else {
238 return id == UserHandle.USER_SYSTEM;
Nicolas Prevotb8186812015-08-06 15:00:03 +0100239 }
Nicolas Prevotb8186812015-08-06 15:00:03 +0100240 }
241
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700242 public UserInfo() {
243 }
244
245 public UserInfo(UserInfo orig) {
246 name = orig.name;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700247 iconPath = orig.iconPath;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700248 id = orig.id;
249 flags = orig.flags;
Amith Yamasani2a003292012-08-14 18:25:45 -0700250 serialNumber = orig.serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -0700251 creationTime = orig.creationTime;
252 lastLoggedInTime = orig.lastLoggedInTime;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600253 lastLoggedInFingerprint = orig.lastLoggedInFingerprint;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700254 partial = orig.partial;
Kenny Guy2a764942014-04-02 13:29:20 +0100255 profileGroupId = orig.profileGroupId;
Amith Yamasani6f48d6e2016-03-23 14:28:25 -0700256 restrictedProfileParentId = orig.restrictedProfileParentId;
Adam Lesinskieddeb492014-09-08 17:50:03 -0700257 guestToRemove = orig.guestToRemove;
Kenny Guy02c89902016-11-15 19:36:38 +0000258 profileBadge = orig.profileBadge;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700259 }
260
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100261 @UnsupportedAppUsage
Jeff Sharkeyb049e212012-09-07 23:16:01 -0700262 public UserHandle getUserHandle() {
263 return new UserHandle(id);
264 }
265
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700266 @Override
267 public String toString() {
Amith Yamasani0b285492011-04-14 17:35:23 -0700268 return "UserInfo{" + id + ":" + name + ":" + Integer.toHexString(flags) + "}";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700269 }
270
271 public int describeContents() {
272 return 0;
273 }
274
275 public void writeToParcel(Parcel dest, int parcelableFlags) {
276 dest.writeInt(id);
277 dest.writeString(name);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700278 dest.writeString(iconPath);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700279 dest.writeInt(flags);
Amith Yamasani2a003292012-08-14 18:25:45 -0700280 dest.writeInt(serialNumber);
Amith Yamasani920ace02012-09-20 22:15:37 -0700281 dest.writeLong(creationTime);
282 dest.writeLong(lastLoggedInTime);
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600283 dest.writeString(lastLoggedInFingerprint);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700284 dest.writeInt(partial ? 1 : 0);
Kenny Guy2a764942014-04-02 13:29:20 +0100285 dest.writeInt(profileGroupId);
Adam Lesinskieddeb492014-09-08 17:50:03 -0700286 dest.writeInt(guestToRemove ? 1 : 0);
Fyodor Kupolov06a484a2015-08-21 16:33:20 -0700287 dest.writeInt(restrictedProfileParentId);
Kenny Guy02c89902016-11-15 19:36:38 +0000288 dest.writeInt(profileBadge);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700289 }
290
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100291 @UnsupportedAppUsage
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700292 public static final @android.annotation.NonNull Parcelable.Creator<UserInfo> CREATOR
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700293 = new Parcelable.Creator<UserInfo>() {
294 public UserInfo createFromParcel(Parcel source) {
295 return new UserInfo(source);
296 }
297 public UserInfo[] newArray(int size) {
298 return new UserInfo[size];
299 }
300 };
301
302 private UserInfo(Parcel source) {
303 id = source.readInt();
304 name = source.readString();
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700305 iconPath = source.readString();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700306 flags = source.readInt();
Amith Yamasani2a003292012-08-14 18:25:45 -0700307 serialNumber = source.readInt();
Amith Yamasani920ace02012-09-20 22:15:37 -0700308 creationTime = source.readLong();
309 lastLoggedInTime = source.readLong();
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600310 lastLoggedInFingerprint = source.readString();
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700311 partial = source.readInt() != 0;
Kenny Guy2a764942014-04-02 13:29:20 +0100312 profileGroupId = source.readInt();
Adam Lesinskieddeb492014-09-08 17:50:03 -0700313 guestToRemove = source.readInt() != 0;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -0700314 restrictedProfileParentId = source.readInt();
Kenny Guy02c89902016-11-15 19:36:38 +0000315 profileBadge = source.readInt();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700316 }
317}