Class ShadowUserManager
UserManager.- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic enumDescribes the current state of the user.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intThe default user ID user for secondary user testing, when the ID is not otherwise specified.static final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected void__constructor__(Context context, IUserManager service) Shadowing this is currently required because the real UserManager constructor stores the result of context.getApplicationContext(), which may be null due to a separate Robolectric bug.voidaddProfile(int userHandle, int profileUserHandle, UserInfo profileUserInfo) Add a profile to be returned bygetProfiles(int).voidaddProfile(int userHandle, int profileUserHandle, String profileName, int profileFlags) Add a profile to be returned bygetProfiles(int).Creates a user with the specified name, userId and flags.longaddUserProfile(UserHandle userHandle) Deprecated.use either addUser() or addProfile()protected booleanReturnstrueby default, or the value specified viasetCanSwitchUser(boolean).protected voidvoidclearUserRestrictions(UserHandle userHandle) Removes all user restrictions set of a user identified byuserHandle.protected UserHandlecreateProfile(String name, String userType, Set<String> disallowedPackages) voidenforcePermissionChecks(boolean enforcePermissions) protected List<UserHandle>protected BundlegetApplicationRestrictions(String packageName) Compared to real Android, there is no check that the package name matches the application package name and the method returns instantly.protected List<UserHandle>protected static intprotected UserInfogetProfileParent(int userId) getProfiles(int userHandle) If any profiles have been added usingaddProfile(int, int, java.lang.String, int), return those profiles.protected Stringprotected PersistableBundleprotected Stringprotected longgetSerialNumberForUser(UserHandle userHandle) protected long[]getSerialNumbersOfUsers(boolean excludeDying) protected UserHandlegetUserForSerialNumber(long serialNumber) protected intgetUserHandle(int serialNumber) protected List<UserHandle>getUserHandles(boolean excludeDying) protected Bitmapprotected UserInfogetUserInfo(int userHandle) protected StringReturns the name of the user.protected List<UserHandle>protected Bundleprotected BundlegetUserRestrictions(UserHandle userHandle) protected List<UserManager.EnforcingUser>getUserRestrictionSources(String restriction, UserHandle userHandle) getUsers()protected intgetUserSerialNumber(int userHandle) protected intprotected booleanhasUserRestriction(String restrictionKey, UserHandle userHandle) protected booleanhasUserRestrictionForUser(String restrictionKey, int userId) protected booleanhasUserRestrictionForUser(String restrictionKey, UserHandle userHandle) protected booleanIf permissions are enforced (seeenforcePermissionChecks(boolean)) and the application doesn't have theManifest.permission.MANAGE_USERSpermission, throws aSecurityManagerexception.protected booleanprotected static booleanprotected booleanprotected booleanIf permissions are enforced (seeenforcePermissionChecks(boolean)) and the application doesn't have theManifest.permission.MANAGE_USERSpermission, throws aSecurityManagerexception.protected booleanisManagedProfile(int userHandle) If permissions are enforced (seeenforcePermissionChecks(boolean)) and the application doesn't have theManifest.permission.MANAGE_USERSpermission, throws aSecurityManagerexception.protected booleanprotected booleanisQuietModeEnabled(UserHandle userHandle) Query whether the quiet mode is enabled for a managed profile.protected booleanReturns 'false' by default, or the value specified viasetIsRestrictedProfile(boolean).protected booleanisSameProfileGroup(UserHandle user, UserHandle otherUser) protected booleanprotected booleanprotected booleanisUserOfType(String userType) protected booleanisUserRunning(UserHandle handle) protected booleanisUserRunningOrStopping(UserHandle handle) protected booleanprotected booleanisUserUnlocked(UserHandle handle) protected booleanisUserUnlockingOrUnlocked(UserHandle handle) voidremoveSomeUserHasAccount(String accountName, String accountType) Removes user account set viasetSomeUserHasAccount(String, String).protected booleanremoveUser(int userHandle) protected booleanremoveUser(UserHandle user) protected intremoveUserWhenPossible(UserHandle user, boolean overrideDevicePolicy) protected booleanrequestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle) Request the quiet mode.protected booleanrequestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle, int flags) Request the quiet mode.static voidreset()protected voidsendQuietModeBroadcast(String action, UserHandle profileHandle) If the current application has the necessary rights, it will receive the background action too.voidsetApplicationRestrictions(String packageName, Bundle restrictions) Sets the value returned byUserManager.getApplicationRestrictions(String).voidsetCanSwitchUser(boolean canSwitchUser) Deprecated.usesetUserSwitchability(int)insteadvoidsetCloneProfile(boolean cloneProfile) Setter forUserManager.isCloneProfile().static voidsetHeadlessSystemUserMode(boolean isEnabled) Updates headless system user mode.voidsetIsDemoUser(boolean isDemoUser) Deprecated.UseaddUser(int, String, int)to create a demo user instead of changing default user flags.voidsetIsGuestUser(boolean isGuestUser) Deprecated.UseaddUser(int, String, int)to create a guest user instead of changing default user flags.voidsetIsLinkedUser(boolean isLinkedUser) Deprecated.UseaddUser(int, String, int)to create a linked user instead of changing default user flags.voidsetIsPrimaryUser(boolean isPrimaryUser) Deprecated.UseaddUser(int, String, int)to create a primary user instead of changing default user flags.voidsetIsRestrictedProfile(boolean isRestrictedProfile) Deprecated.useShadowUserManager#addUser()insteadvoidsetIsSystemUser(boolean isSystemUser) Deprecated.UseaddUser(int, String, int)to create a system user instead of changing default user flags.voidsetIsUserEnabled(int userId, boolean enabled) voidsetManagedProfile(boolean managedProfile) Setter forUserManager.isManagedProfile().voidsetMaxSupportedUsers(int maxSupportedUsers) voidsetProfileIsLocked(UserHandle profileHandle, boolean isLocked) voidsetSeedAccountName(String seedAccountName) Setter forUserManager.getSeedAccountName()voidsetSeedAccountOptions(PersistableBundle seedAccountOptions) Setter forUserManager.getSeedAccountOptions()voidsetSeedAccountType(String seedAccountType) Setter forUserManager.getSeedAccountType()voidsetSerialNumberForUser(UserHandle userHandle, long serialNumber) addUser(int, java.lang.String, int)uses UserHandle for serialNumber.voidsetSomeUserHasAccount(String accountName, String accountType) Setter forUserManager.someUserHasAccount(String, String).voidsetSupportsMultipleUsers(boolean isMultiUserSupported) Sets whether multiple users are supported; controls the return value ofUserManager#supportsMultipleUser.voidsetUserForeground(boolean foreground) Sets whether or not the current user is the foreground user.protected voidsetUserIcon(Bitmap icon) protected voidsetUserName(String name) voidsetUserRestriction(UserHandle userHandle, String restrictionKey, boolean value) Deprecated.When possible, please use the real Android framework APIUserManager#setUserRestriction().protected voidsetUserRestriction(String key, boolean value) protected voidsetUserRestriction(String key, boolean value, UserHandle userHandle) Shadows UserManager.setUserRestriction() API.voidsetUserState(UserHandle handle, ShadowUserManager.UserState state) Sets the current state for a given user, seeUserManager.isUserRunning(UserHandle)andUserManager.isUserRunningOrStopping(UserHandle)voidsetUserSwitchability(int switchability) Sets the user switchability for all users.voidsetUserUnlocked(boolean userUnlocked) Setter forUserManager.isUserUnlocked()protected booleansomeUserHasAccount(String accountName, String accountType) protected static booleanvoidswitchUser(int userId) Switches the current user touserHandle.
- 
Field Details- 
DEFAULT_SECONDARY_USER_IDpublic static final int DEFAULT_SECONDARY_USER_IDThe default user ID user for secondary user testing, when the ID is not otherwise specified.- See Also:
 
- 
FLAG_PRIMARYpublic static final int FLAG_PRIMARY- See Also:
 
- 
FLAG_ADMINpublic static final int FLAG_ADMIN- See Also:
 
- 
FLAG_GUESTpublic static final int FLAG_GUEST- See Also:
 
- 
FLAG_RESTRICTEDpublic static final int FLAG_RESTRICTED- See Also:
 
- 
FLAG_DEMOpublic static final int FLAG_DEMO- See Also:
 
- 
FLAG_MANAGED_PROFILEpublic static final int FLAG_MANAGED_PROFILE- See Also:
 
- 
FLAG_PROFILEpublic static final int FLAG_PROFILE- See Also:
 
- 
FLAG_FULLpublic static final int FLAG_FULL- See Also:
 
- 
FLAG_SYSTEMpublic static final int FLAG_SYSTEM- See Also:
 
- 
FLAG_MAINpublic static final int FLAG_MAIN- See Also:
 
 
- 
- 
Constructor Details- 
ShadowUserManagerpublic ShadowUserManager()
 
- 
- 
Method Details- 
__constructor__Shadowing this is currently required because the real UserManager constructor stores the result of context.getApplicationContext(), which may be null due to a separate Robolectric bug.
- 
getApplicationRestrictionsCompared to real Android, there is no check that the package name matches the application package name and the method returns instantly.
- 
setApplicationRestrictionsSets the value returned byUserManager.getApplicationRestrictions(String).
- 
addUserProfileDeprecated.use either addUser() or addProfile()Adds a profile associated for the user that the calling process is running on.The user is assigned an arbitrary unique serial number. - Returns:
- the user's serial number
 
- 
getUserProfiles
- 
getProfilesIf any profiles have been added usingaddProfile(int, int, java.lang.String, int), return those profiles.Otherwise follow real android behaviour. 
- 
getEnabledProfiles
- 
getAllProfiles
- 
getProfileParent
- 
createProfile@Implementation(minSdk=30) protected UserHandle createProfile(String name, String userType, Set<String> disallowedPackages) 
- 
addProfileAdd a profile to be returned bygetProfiles(int).
- 
addProfileAdd a profile to be returned bygetProfiles(int).
- 
setUserUnlockedpublic void setUserUnlocked(boolean userUnlocked) Setter forUserManager.isUserUnlocked()
- 
isUserUnlocked
- 
isUserUnlocked- See Also:
 
- 
isManagedProfileIf permissions are enforced (seeenforcePermissionChecks(boolean)) and the application doesn't have theManifest.permission.MANAGE_USERSpermission, throws aSecurityManagerexception.- Returns:
- false by default, or the value specified via setManagedProfile(boolean)
- See Also:
 
- 
isManagedProfileIf permissions are enforced (seeenforcePermissionChecks(boolean)) and the application doesn't have theManifest.permission.MANAGE_USERSpermission, throws aSecurityManagerexception.- Returns:
- true if the profile added has FLAG_MANAGED_PROFILE
- See Also:
 
- 
enforcePermissionCheckspublic void enforcePermissionChecks(boolean enforcePermissions) 
- 
setManagedProfilepublic void setManagedProfile(boolean managedProfile) Setter forUserManager.isManagedProfile().
- 
isCloneProfileIf permissions are enforced (seeenforcePermissionChecks(boolean)) and the application doesn't have theManifest.permission.MANAGE_USERSpermission, throws aSecurityManagerexception.- Returns:
- true if the user is clone, or the value specified via setCloneProfile(boolean)
- See Also:
 
- 
setCloneProfilepublic void setCloneProfile(boolean cloneProfile) Setter forUserManager.isCloneProfile().
- 
isProfile
- 
isUserOfType
- 
isSameProfileGroup@Implementation(minSdk=30) protected boolean isSameProfileGroup(UserHandle user, UserHandle otherUser) 
- 
hasUserRestriction
- 
setUserRestrictionShadows UserManager.setUserRestriction() API. This allows UserManager.hasUserRestriction() to return meaningful results in test environment; thus, allowing test to verify the invoking of UserManager.setUserRestriction().
- 
setUserRestriction
- 
setUserRestriction@Deprecated public void setUserRestriction(UserHandle userHandle, String restrictionKey, boolean value) Deprecated.When possible, please use the real Android framework APIUserManager#setUserRestriction().
- 
clearUserRestrictionsRemoves all user restrictions set of a user identified byuserHandle.
- 
getUserRestrictions
- 
getSerialNumberForUser
- 
setSerialNumberForUseraddUser(int, java.lang.String, int)uses UserHandle for serialNumber. setSerialNumberForUser() allows assigning an arbitrary serialNumber. Some test use serialNumber!=0 as secondary user check, so it's necessary to "fake" the serialNumber to a non-zero value.
- 
getUserForSerialNumber
- 
getUserSerialNumber
- 
getUserNameReturns the name of the user.On real Android, if a UserHandle.USER_SYSTEM user is found but does not have a name, it will return a name like "Owner". In Robolectric, the USER_SYSTEM user always has a name. 
- 
setUserName
- 
getUserIcon
- 
setUserIcon
- 
getUserHandle- Returns:
- user id for given user serial number.
 
- 
getUserHandles@HiddenApi @Implementation(minSdk=30) protected List<UserHandle> getUserHandles(boolean excludeDying) 
- 
getMaxSupportedUsers
- 
setMaxSupportedUserspublic void setMaxSupportedUsers(int maxSupportedUsers) 
- 
isDemoUser- Returns:
- false by default, or the value specified via setIsDemoUser(boolean)
 
- 
setIsDemoUserDeprecated.UseaddUser(int, String, int)to create a demo user instead of changing default user flags.Sets that the current user is a demo user; controls the return value ofUserManager.isDemoUser().
- 
isSystemUser- Returns:
- 'true' by default, or the value specified via setIsSystemUser(boolean)
 
- 
setIsSystemUserDeprecated.UseaddUser(int, String, int)to create a system user instead of changing default user flags.Sets that the current user is the system user; controls the return value ofUserManager.isSystemUser().
- 
setIsPrimaryUserDeprecated.UseaddUser(int, String, int)to create a primary user instead of changing default user flags.Sets that the current user is the primary user; controls the return value ofUserManager.isPrimaryUser().
- 
isLinkedUser- Returns:
- 'false' by default, or the value specified via setIsLinkedUser(boolean)
 
- 
setIsLinkedUserDeprecated.UseaddUser(int, String, int)to create a linked user instead of changing default user flags.Sets that the current user is the linked user; controls the return value ofUserManager.isLinkedUser().
- 
isRestrictedProfileReturns 'false' by default, or the value specified viasetIsRestrictedProfile(boolean).
- 
setIsRestrictedProfileDeprecated.useShadowUserManager#addUser()insteadSets this process running under a restricted profile; controls the return value ofUserManager.isRestrictedProfile().
- 
setIsGuestUserDeprecated.UseaddUser(int, String, int)to create a guest user instead of changing default user flags.Sets that the current user is the guest user; controls the return value ofUserManager.isGuestUser().
- 
setIsUserEnabledpublic void setIsUserEnabled(int userId, boolean enabled) 
- 
isUserRunning- See Also:
 
- 
isUserRunningOrStopping- See Also:
 
- 
isUserUnlockingOrUnlocked- See Also:
 
- 
setUserStateSets the current state for a given user, seeUserManager.isUserRunning(UserHandle)andUserManager.isUserRunningOrStopping(UserHandle)
- 
isQuietModeEnabledQuery whether the quiet mode is enabled for a managed profile.This method checks whether the user handle corresponds to a managed profile, and then query its state. When quiet, the user is not running. 
- 
requestQuietModeEnabled@Implementation(minSdk=29) protected boolean requestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle) Request the quiet mode.This will succeed unless setProfileIsLocked(UserHandle, boolean)is called withtruefor the managed profile, in which case it will always fail.
- 
sendQuietModeBroadcastIf the current application has the necessary rights, it will receive the background action too.
- 
setProfileIsLocked
- 
getSerialNumbersOfUsers
- 
getUsers
- 
getUserInfo
- 
setCanSwitchUserDeprecated.usesetUserSwitchability(int)insteadSets whether switching users is allowed or not; controls the return value ofUserManager#canSwitchUser()
- 
getSeedAccountName
- 
setSeedAccountNameSetter forUserManager.getSeedAccountName()
- 
getSeedAccountType
- 
setSeedAccountTypeSetter forUserManager.getSeedAccountType()
- 
getSeedAccountOptions
- 
setSeedAccountOptionsSetter forUserManager.getSeedAccountOptions()
- 
clearSeedAccountData
- 
removeUser
- 
removeUser
- 
removeUserWhenPossible@Implementation(minSdk=33) protected int removeUserWhenPossible(UserHandle user, boolean overrideDevicePolicy) 
- 
supportsMultipleUsers
- 
setSupportsMultipleUserspublic void setSupportsMultipleUsers(boolean isMultiUserSupported) Sets whether multiple users are supported; controls the return value ofUserManager#supportsMultipleUser.
- 
switchUserpublic void switchUser(int userId) Switches the current user touserHandle.- Parameters:
- userId- the integer handle of the user, where 0 is the primary user.
 
- 
addUserCreates a user with the specified name, userId and flags.- Parameters:
- id- the unique id of user
- name- name of the user
- flags- 16 bits for user type. See- UserInfo.flags
- Returns:
- a handle to the new user
 
- 
canSwitchUsersReturnstrueby default, or the value specified viasetCanSwitchUser(boolean).
- 
getUserSwitchability
- 
setUserSwitchabilitypublic void setUserSwitchability(int switchability) Sets the user switchability for all users.
- 
hasUserRestrictionForUser@Implementation(minSdk=30) protected boolean hasUserRestrictionForUser(String restrictionKey, UserHandle userHandle) 
- 
requestQuietModeEnabled@Implementation(minSdk=30) protected boolean requestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle, int flags) Request the quiet mode.If setProfileIsLocked(UserHandle, boolean)is called withtruefor the managed profile a request to disable the quiet mode will fail and returnfalse(i.e. as if the user refused to authenticate). Otherwise, the call will always succeed and returntrue.This method simply re-directs to requestQuietModeEnabled(boolean, UserHandle)as it already has the desired behavior irrespective of the flag's value.
- 
isHeadlessSystemUserMode
- 
setHeadlessSystemUserModepublic static void setHeadlessSystemUserMode(boolean isEnabled) Updates headless system user mode.
- 
getUserRestrictions
- 
hasUserRestrictionForUser@Implementation(minSdk=33) protected boolean hasUserRestrictionForUser(String restrictionKey, int userId) 
- 
reset
- 
someUserHasAccount@Implementation(minSdk=33) protected boolean someUserHasAccount(String accountName, String accountType) 
- 
setSomeUserHasAccountSetter forUserManager.someUserHasAccount(String, String).
- 
removeSomeUserHasAccountRemoves user account set viasetSomeUserHasAccount(String, String).
- 
setUserForegroundpublic void setUserForeground(boolean foreground) Sets whether or not the current user is the foreground user.
- 
isUserForeground
- 
getUserRestrictionSources@Implementation(minSdk=26) protected List<UserManager.EnforcingUser> getUserRestrictionSources(String restriction, UserHandle userHandle) 
 
-