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 17 package android.content.pm; 18 19 import android.annotation.UnsupportedAppUsage; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.os.SystemProperties; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 /** 27 * Per-user information. 28 * @hide 29 */ 30 public class UserInfo implements Parcelable { 31 32 /** 16 bits for user type */ 33 public static final int FLAG_MASK_USER_TYPE = 0x0000FFFF; 34 35 /** 36 * *************************** NOTE *************************** 37 * These flag values CAN NOT CHANGE because they are written 38 * directly to storage. 39 */ 40 41 /** 42 * Primary user. Only one user can have this flag set. It identifies the first human user 43 * on a device. 44 */ 45 @UnsupportedAppUsage 46 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 59 /** 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 65 /** 66 * Indicates that this user has gone through its first-time initialization. 67 */ 68 public static final int FLAG_INITIALIZED = 0x00000010; 69 70 /** 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 76 /** 77 * Indicates that this user is disabled. 78 * 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. 81 */ 82 public static final int FLAG_DISABLED = 0x00000040; 83 84 public static final int FLAG_QUIET_MODE = 0x00000080; 85 86 /** 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 92 /** 93 * User is for demo purposes only and can be removed at any time. 94 */ 95 public static final int FLAG_DEMO = 0x00000200; 96 97 public static final int NO_PROFILE_GROUP_ID = UserHandle.USER_NULL; 98 99 @UnsupportedAppUsage 100 public int id; 101 @UnsupportedAppUsage 102 public int serialNumber; 103 @UnsupportedAppUsage 104 public String name; 105 @UnsupportedAppUsage 106 public String iconPath; 107 @UnsupportedAppUsage 108 public int flags; 109 @UnsupportedAppUsage 110 public long creationTime; 111 @UnsupportedAppUsage 112 public long lastLoggedInTime; 113 public String lastLoggedInFingerprint; 114 /** 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 */ 119 @UnsupportedAppUsage 120 public int profileGroupId; 121 public int restrictedProfileParentId; 122 /** Which profile badge color/label to use. */ 123 public int profileBadge; 124 125 /** User is only partially created. */ 126 @UnsupportedAppUsage 127 public boolean partial; 128 @UnsupportedAppUsage 129 public boolean guestToRemove; 130 131 @UnsupportedAppUsage UserInfo(int id, String name, int flags)132 public UserInfo(int id, String name, int flags) { 133 this(id, name, null, flags); 134 } 135 136 @UnsupportedAppUsage UserInfo(int id, String name, String iconPath, int flags)137 public UserInfo(int id, String name, String iconPath, int flags) { 138 this.id = id; 139 this.name = name; 140 this.flags = flags; 141 this.iconPath = iconPath; 142 this.profileGroupId = NO_PROFILE_GROUP_ID; 143 this.restrictedProfileParentId = NO_PROFILE_GROUP_ID; 144 } 145 146 @UnsupportedAppUsage isPrimary()147 public boolean isPrimary() { 148 return (flags & FLAG_PRIMARY) == FLAG_PRIMARY; 149 } 150 151 @UnsupportedAppUsage isAdmin()152 public boolean isAdmin() { 153 return (flags & FLAG_ADMIN) == FLAG_ADMIN; 154 } 155 156 @UnsupportedAppUsage isGuest()157 public boolean isGuest() { 158 return (flags & FLAG_GUEST) == FLAG_GUEST; 159 } 160 161 @UnsupportedAppUsage isRestricted()162 public boolean isRestricted() { 163 return (flags & FLAG_RESTRICTED) == FLAG_RESTRICTED; 164 } 165 166 @UnsupportedAppUsage isManagedProfile()167 public boolean isManagedProfile() { 168 return (flags & FLAG_MANAGED_PROFILE) == FLAG_MANAGED_PROFILE; 169 } 170 171 @UnsupportedAppUsage isEnabled()172 public boolean isEnabled() { 173 return (flags & FLAG_DISABLED) != FLAG_DISABLED; 174 } 175 isQuietModeEnabled()176 public boolean isQuietModeEnabled() { 177 return (flags & FLAG_QUIET_MODE) == FLAG_QUIET_MODE; 178 } 179 isEphemeral()180 public boolean isEphemeral() { 181 return (flags & FLAG_EPHEMERAL) == FLAG_EPHEMERAL; 182 } 183 isInitialized()184 public boolean isInitialized() { 185 return (flags & FLAG_INITIALIZED) == FLAG_INITIALIZED; 186 } 187 isDemo()188 public boolean isDemo() { 189 return (flags & FLAG_DEMO) == FLAG_DEMO; 190 } 191 192 /** 193 * 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 */ isSystemOnly()197 public boolean isSystemOnly() { 198 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 */ isSystemOnly(int userId)206 public static boolean isSystemOnly(int userId) { 207 return userId == UserHandle.USER_SYSTEM && UserManager.isSplitSystemUser(); 208 } 209 210 /** 211 * @return true if this user can be switched to. 212 **/ supportsSwitchTo()213 public boolean supportsSwitchTo() { 214 if (isEphemeral() && !isEnabled()) { 215 // Don't support switching to an ephemeral user with removal in progress. 216 return false; 217 } 218 return !isManagedProfile(); 219 } 220 221 /** 222 * @return true if this user can be switched to by end user through UI. 223 */ supportsSwitchToByUser()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 230 /* @hide */ canHaveProfile()231 public boolean canHaveProfile() { 232 if (isManagedProfile() || isGuest() || isRestricted()) { 233 return false; 234 } 235 if (UserManager.isSplitSystemUser()) { 236 return id != UserHandle.USER_SYSTEM; 237 } else { 238 return id == UserHandle.USER_SYSTEM; 239 } 240 } 241 UserInfo()242 public UserInfo() { 243 } 244 UserInfo(UserInfo orig)245 public UserInfo(UserInfo orig) { 246 name = orig.name; 247 iconPath = orig.iconPath; 248 id = orig.id; 249 flags = orig.flags; 250 serialNumber = orig.serialNumber; 251 creationTime = orig.creationTime; 252 lastLoggedInTime = orig.lastLoggedInTime; 253 lastLoggedInFingerprint = orig.lastLoggedInFingerprint; 254 partial = orig.partial; 255 profileGroupId = orig.profileGroupId; 256 restrictedProfileParentId = orig.restrictedProfileParentId; 257 guestToRemove = orig.guestToRemove; 258 profileBadge = orig.profileBadge; 259 } 260 261 @UnsupportedAppUsage getUserHandle()262 public UserHandle getUserHandle() { 263 return new UserHandle(id); 264 } 265 266 @Override toString()267 public String toString() { 268 return "UserInfo{" + id + ":" + name + ":" + Integer.toHexString(flags) + "}"; 269 } 270 describeContents()271 public int describeContents() { 272 return 0; 273 } 274 writeToParcel(Parcel dest, int parcelableFlags)275 public void writeToParcel(Parcel dest, int parcelableFlags) { 276 dest.writeInt(id); 277 dest.writeString(name); 278 dest.writeString(iconPath); 279 dest.writeInt(flags); 280 dest.writeInt(serialNumber); 281 dest.writeLong(creationTime); 282 dest.writeLong(lastLoggedInTime); 283 dest.writeString(lastLoggedInFingerprint); 284 dest.writeInt(partial ? 1 : 0); 285 dest.writeInt(profileGroupId); 286 dest.writeInt(guestToRemove ? 1 : 0); 287 dest.writeInt(restrictedProfileParentId); 288 dest.writeInt(profileBadge); 289 } 290 291 @UnsupportedAppUsage 292 public static final @android.annotation.NonNull Parcelable.Creator<UserInfo> CREATOR 293 = 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 UserInfo(Parcel source)302 private UserInfo(Parcel source) { 303 id = source.readInt(); 304 name = source.readString(); 305 iconPath = source.readString(); 306 flags = source.readInt(); 307 serialNumber = source.readInt(); 308 creationTime = source.readLong(); 309 lastLoggedInTime = source.readLong(); 310 lastLoggedInFingerprint = source.readString(); 311 partial = source.readInt() != 0; 312 profileGroupId = source.readInt(); 313 guestToRemove = source.readInt() != 0; 314 restrictedProfileParentId = source.readInt(); 315 profileBadge = source.readInt(); 316 } 317 } 318