| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.TestApi; |
| 23 |
import android.annotation.UserIdInt; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.os.UserHandle; |
| 28 |
import android.os.UserManager; |
| 29 |
import android.util.DebugUtils; |
| 30 |
|
| 31 |
import com.android.internal.annotations.VisibleForTesting; |
| 32 |
|
| 33 |
import java.lang.annotation.Retention; |
| 34 |
import java.lang.annotation.RetentionPolicy; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@TestApi |
| 53 |
public class UserInfo implements Parcelable { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@UnsupportedAppUsage |
| 66 |
public static final int FLAG_PRIMARY = 0x00000001; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static final int FLAG_ADMIN = 0x00000002; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@Deprecated |
| 79 |
public static final int FLAG_GUEST = 0x00000004; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@Deprecated |
| 87 |
public static final int FLAG_RESTRICTED = 0x00000008; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final int FLAG_INITIALIZED = 0x00000010; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@Deprecated |
| 100 |
public static final int FLAG_MANAGED_PROFILE = 0x00000020; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static final int FLAG_DISABLED = 0x00000040; |
| 109 |
|
| 110 |
public static final int FLAG_QUIET_MODE = 0x00000080; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static final int FLAG_EPHEMERAL = 0x00000100; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
@Deprecated |
| 123 |
public static final int FLAG_DEMO = 0x00000200; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static final int FLAG_FULL = 0x00000400; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static final int FLAG_SYSTEM = 0x00000800; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static final int FLAG_PROFILE = 0x00001000; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static final int FLAG_EPHEMERAL_ON_CREATE = 0x00002000; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
@IntDef(flag = true, prefix = "FLAG_", value = { |
| 165 |
FLAG_PRIMARY, |
| 166 |
FLAG_ADMIN, |
| 167 |
FLAG_GUEST, |
| 168 |
FLAG_RESTRICTED, |
| 169 |
FLAG_INITIALIZED, |
| 170 |
FLAG_MANAGED_PROFILE, |
| 171 |
FLAG_DISABLED, |
| 172 |
FLAG_QUIET_MODE, |
| 173 |
FLAG_EPHEMERAL, |
| 174 |
FLAG_DEMO, |
| 175 |
FLAG_FULL, |
| 176 |
FLAG_SYSTEM, |
| 177 |
FLAG_PROFILE, |
| 178 |
FLAG_EPHEMERAL_ON_CREATE |
| 179 |
}) |
| 180 |
@Retention(RetentionPolicy.SOURCE) |
| 181 |
public @interface UserInfoFlag { |
| 182 |
} |
| 183 |
|
| 184 |
public static final int NO_PROFILE_GROUP_ID = UserHandle.USER_NULL; |
| 185 |
|
| 186 |
@UnsupportedAppUsage |
| 187 |
public @UserIdInt int id; |
| 188 |
@UnsupportedAppUsage |
| 189 |
public int serialNumber; |
| 190 |
@UnsupportedAppUsage |
| 191 |
public @Nullable String name; |
| 192 |
@UnsupportedAppUsage |
| 193 |
public String iconPath; |
| 194 |
@UnsupportedAppUsage |
| 195 |
public @UserInfoFlag int flags; |
| 196 |
@UnsupportedAppUsage |
| 197 |
public long creationTime; |
| 198 |
@UnsupportedAppUsage |
| 199 |
public long lastLoggedInTime; |
| 200 |
public String lastLoggedInFingerprint; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public String userType; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
@UnsupportedAppUsage |
| 214 |
public int profileGroupId; |
| 215 |
public int restrictedProfileParentId; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public int profileBadge; |
| 224 |
|
| 225 |
|
| 226 |
@UnsupportedAppUsage |
| 227 |
public boolean partial; |
| 228 |
@UnsupportedAppUsage |
| 229 |
public boolean guestToRemove; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public boolean preCreated; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public boolean convertedFromPreCreated; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@UnsupportedAppUsage |
| 256 |
public UserInfo(int id, String name, int flags) { |
| 257 |
this(id, name, null, flags); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
@UnsupportedAppUsage |
| 265 |
public UserInfo(int id, String name, String iconPath, int flags) { |
| 266 |
this(id, name, iconPath, flags, getDefaultUserType(flags)); |
| 267 |
} |
| 268 |
|
| 269 |
public UserInfo(int id, String name, String iconPath, int flags, String userType) { |
| 270 |
this.id = id; |
| 271 |
this.name = name; |
| 272 |
this.flags = flags; |
| 273 |
this.userType = userType; |
| 274 |
this.iconPath = iconPath; |
| 275 |
this.profileGroupId = NO_PROFILE_GROUP_ID; |
| 276 |
this.restrictedProfileParentId = NO_PROFILE_GROUP_ID; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public static @NonNull String getDefaultUserType(@UserInfoFlag int userInfoFlag) { |
| 293 |
if ((userInfoFlag & FLAG_SYSTEM) != 0) { |
| 294 |
throw new IllegalArgumentException("Cannot getDefaultUserType for flags " |
| 295 |
+ Integer.toHexString(userInfoFlag) + " because it corresponds to a " |
| 296 |
+ "SYSTEM user type."); |
| 297 |
} |
| 298 |
final int supportedFlagTypes = |
| 299 |
FLAG_GUEST | FLAG_RESTRICTED | FLAG_MANAGED_PROFILE | FLAG_DEMO; |
| 300 |
switch (userInfoFlag & supportedFlagTypes) { |
| 301 |
case 0 : return UserManager.USER_TYPE_FULL_SECONDARY; |
| 302 |
case FLAG_GUEST: return UserManager.USER_TYPE_FULL_GUEST; |
| 303 |
case FLAG_RESTRICTED: return UserManager.USER_TYPE_FULL_RESTRICTED; |
| 304 |
case FLAG_MANAGED_PROFILE: return UserManager.USER_TYPE_PROFILE_MANAGED; |
| 305 |
case FLAG_DEMO: return UserManager.USER_TYPE_FULL_DEMO; |
| 306 |
default: |
| 307 |
throw new IllegalArgumentException("Cannot getDefaultUserType for flags " |
| 308 |
+ Integer.toHexString(userInfoFlag) + " because it doesn't correspond to a " |
| 309 |
+ "valid user type."); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
@UnsupportedAppUsage |
| 314 |
public boolean isPrimary() { |
| 315 |
return (flags & FLAG_PRIMARY) == FLAG_PRIMARY; |
| 316 |
} |
| 317 |
|
| 318 |
@UnsupportedAppUsage |
| 319 |
public boolean isAdmin() { |
| 320 |
return (flags & FLAG_ADMIN) == FLAG_ADMIN; |
| 321 |
} |
| 322 |
|
| 323 |
@UnsupportedAppUsage |
| 324 |
public boolean isGuest() { |
| 325 |
return UserManager.isUserTypeGuest(userType); |
| 326 |
} |
| 327 |
|
| 328 |
@UnsupportedAppUsage |
| 329 |
public boolean isRestricted() { |
| 330 |
return UserManager.isUserTypeRestricted(userType); |
| 331 |
} |
| 332 |
|
| 333 |
public boolean isProfile() { |
| 334 |
return (flags & FLAG_PROFILE) != 0; |
| 335 |
} |
| 336 |
|
| 337 |
@UnsupportedAppUsage |
| 338 |
public boolean isManagedProfile() { |
| 339 |
return UserManager.isUserTypeManagedProfile(userType); |
| 340 |
} |
| 341 |
|
| 342 |
public boolean isCloneProfile() { |
| 343 |
return UserManager.isUserTypeCloneProfile(userType); |
| 344 |
} |
| 345 |
|
| 346 |
@UnsupportedAppUsage |
| 347 |
public boolean isEnabled() { |
| 348 |
return (flags & FLAG_DISABLED) != FLAG_DISABLED; |
| 349 |
} |
| 350 |
|
| 351 |
public boolean isQuietModeEnabled() { |
| 352 |
return (flags & FLAG_QUIET_MODE) == FLAG_QUIET_MODE; |
| 353 |
} |
| 354 |
|
| 355 |
public boolean isEphemeral() { |
| 356 |
return (flags & FLAG_EPHEMERAL) == FLAG_EPHEMERAL; |
| 357 |
} |
| 358 |
|
| 359 |
public boolean isInitialized() { |
| 360 |
return (flags & FLAG_INITIALIZED) == FLAG_INITIALIZED; |
| 361 |
} |
| 362 |
|
| 363 |
public boolean isDemo() { |
| 364 |
return UserManager.isUserTypeDemo(userType) || (flags & FLAG_DEMO) != 0; |
| 365 |
} |
| 366 |
|
| 367 |
public boolean isFull() { |
| 368 |
return (flags & FLAG_FULL) == FLAG_FULL; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public boolean isSystemOnly() { |
| 377 |
return isSystemOnly(id); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public static boolean isSystemOnly(int userId) { |
| 386 |
return userId == UserHandle.USER_SYSTEM && UserManager.isSplitSystemUser(); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public boolean supportsSwitchTo() { |
| 393 |
if (partial || !isEnabled()) { |
| 394 |
|
| 395 |
|
| 396 |
return false; |
| 397 |
} |
| 398 |
if (preCreated) { |
| 399 |
|
| 400 |
return false; |
| 401 |
} |
| 402 |
return !isProfile(); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public boolean supportsSwitchToByUser() { |
| 409 |
|
| 410 |
boolean hideSystemUser = UserManager.isHeadlessSystemUserMode(); |
| 411 |
return (!hideSystemUser || id != UserHandle.USER_SYSTEM) && supportsSwitchTo(); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public boolean canHaveProfile() { |
| 417 |
if (isProfile() || isGuest() || isRestricted()) { |
| 418 |
return false; |
| 419 |
} |
| 420 |
if (UserManager.isSplitSystemUser() || UserManager.isHeadlessSystemUserMode()) { |
| 421 |
return id != UserHandle.USER_SYSTEM; |
| 422 |
} else { |
| 423 |
return id == UserHandle.USER_SYSTEM; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
@Deprecated |
| 433 |
@VisibleForTesting |
| 434 |
public UserInfo() { |
| 435 |
} |
| 436 |
|
| 437 |
public UserInfo(UserInfo orig) { |
| 438 |
name = orig.name; |
| 439 |
iconPath = orig.iconPath; |
| 440 |
id = orig.id; |
| 441 |
flags = orig.flags; |
| 442 |
userType = orig.userType; |
| 443 |
serialNumber = orig.serialNumber; |
| 444 |
creationTime = orig.creationTime; |
| 445 |
lastLoggedInTime = orig.lastLoggedInTime; |
| 446 |
lastLoggedInFingerprint = orig.lastLoggedInFingerprint; |
| 447 |
partial = orig.partial; |
| 448 |
preCreated = orig.preCreated; |
| 449 |
convertedFromPreCreated = orig.convertedFromPreCreated; |
| 450 |
profileGroupId = orig.profileGroupId; |
| 451 |
restrictedProfileParentId = orig.restrictedProfileParentId; |
| 452 |
guestToRemove = orig.guestToRemove; |
| 453 |
profileBadge = orig.profileBadge; |
| 454 |
} |
| 455 |
|
| 456 |
@UnsupportedAppUsage |
| 457 |
public UserHandle getUserHandle() { |
| 458 |
return UserHandle.of(id); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
@Override |
| 463 |
public String toString() { |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
return "UserInfo{" + id + ":" + name + ":" + Integer.toHexString(flags) + "}"; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
public String toFullString() { |
| 472 |
return "UserInfo[id=" + id |
| 473 |
+ ", name=" + name |
| 474 |
+ ", type=" + userType |
| 475 |
+ ", flags=" + flagsToString(flags) |
| 476 |
+ (preCreated ? " (pre-created)" : "") |
| 477 |
+ (convertedFromPreCreated ? " (converted)" : "") |
| 478 |
+ (partial ? " (partial)" : "") |
| 479 |
+ "]"; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
public static String flagsToString(int flags) { |
| 484 |
return DebugUtils.flagsToString(UserInfo.class, "FLAG_", flags); |
| 485 |
} |
| 486 |
|
| 487 |
@Override |
| 488 |
public int describeContents() { |
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
@Override |
| 493 |
public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 494 |
dest.writeInt(id); |
| 495 |
dest.writeString8(name); |
| 496 |
dest.writeString8(iconPath); |
| 497 |
dest.writeInt(flags); |
| 498 |
dest.writeString8(userType); |
| 499 |
dest.writeInt(serialNumber); |
| 500 |
dest.writeLong(creationTime); |
| 501 |
dest.writeLong(lastLoggedInTime); |
| 502 |
dest.writeString8(lastLoggedInFingerprint); |
| 503 |
dest.writeBoolean(partial); |
| 504 |
dest.writeBoolean(preCreated); |
| 505 |
dest.writeInt(profileGroupId); |
| 506 |
dest.writeBoolean(guestToRemove); |
| 507 |
dest.writeInt(restrictedProfileParentId); |
| 508 |
dest.writeInt(profileBadge); |
| 509 |
} |
| 510 |
|
| 511 |
@UnsupportedAppUsage |
| 512 |
public static final @android.annotation.NonNull Parcelable.Creator<UserInfo> CREATOR |
| 513 |
= new Parcelable.Creator<UserInfo>() { |
| 514 |
public UserInfo createFromParcel(Parcel source) { |
| 515 |
return new UserInfo(source); |
| 516 |
} |
| 517 |
public UserInfo[] newArray(int size) { |
| 518 |
return new UserInfo[size]; |
| 519 |
} |
| 520 |
}; |
| 521 |
|
| 522 |
private UserInfo(Parcel source) { |
| 523 |
id = source.readInt(); |
| 524 |
name = source.readString8(); |
| 525 |
iconPath = source.readString8(); |
| 526 |
flags = source.readInt(); |
| 527 |
userType = source.readString8(); |
| 528 |
serialNumber = source.readInt(); |
| 529 |
creationTime = source.readLong(); |
| 530 |
lastLoggedInTime = source.readLong(); |
| 531 |
lastLoggedInFingerprint = source.readString8(); |
| 532 |
partial = source.readBoolean(); |
| 533 |
preCreated = source.readBoolean(); |
| 534 |
profileGroupId = source.readInt(); |
| 535 |
guestToRemove = source.readBoolean(); |
| 536 |
restrictedProfileParentId = source.readInt(); |
| 537 |
profileBadge = source.readInt(); |
| 538 |
} |
| 539 |
} |