Class ShadowUserManager
- java.lang.Object
-
- org.robolectric.shadows.ShadowUserManager
-
@Implements(value=android.os.UserManager.class, minSdk=17) public class ShadowUserManager extends Object
Robolectric implementation ofUserManager
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ShadowUserManager.UserState
Describes the current state of the user.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_SECONDARY_USER_ID
The default user ID user for secondary user testing, when the ID is not otherwise specified.static int
FLAG_ADMIN
static int
FLAG_DEMO
static int
FLAG_FULL
static int
FLAG_GUEST
static int
FLAG_MANAGED_PROFILE
static int
FLAG_PRIMARY
static int
FLAG_PROFILE
static int
FLAG_RESTRICTED
static int
FLAG_SYSTEM
-
Constructor Summary
Constructors Constructor Description ShadowUserManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
__constructor__(Context context, IUserManager service)
void
addProfile(int userHandle, int profileUserHandle, String profileName, int profileFlags)
Add a profile to be returned bygetProfiles(int)
.*UserHandle
addUser(int id, String name, int flags)
Creates a user with the specified name, userId and flags.long
addUserProfile(UserHandle userHandle)
Deprecated.use either addUser() or addProfile()protected boolean
canSwitchUsers()
Returnstrue
by default, or the value specified viasetCanSwitchUser(boolean)
.protected void
clearSeedAccountData()
void
clearUserRestrictions(UserHandle userHandle)
Removes all user restrictions set of a user identified byuserHandle
.protected UserHandle
createProfile(String name, String userType, Set<String> disallowedPackages)
void
enforcePermissionChecks(boolean enforcePermissions)
protected List<UserHandle>
getAllProfiles()
protected Bundle
getApplicationRestrictions(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>
getEnabledProfiles()
protected static int
getMaxSupportedUsers()
protected UserInfo
getProfileParent(int userId)
protected List<UserInfo>
getProfiles(int userHandle)
If any profiles have been added usingaddProfile(int, int, java.lang.String, int)
, return those profiles.protected String
getSeedAccountName()
protected PersistableBundle
getSeedAccountOptions()
protected String
getSeedAccountType()
protected long
getSerialNumberForUser(UserHandle userHandle)
protected long[]
getSerialNumbersOfUsers(boolean excludeDying)
protected UserHandle
getUserForSerialNumber(long serialNumber)
protected int
getUserHandle(int serialNumber)
protected Bitmap
getUserIcon()
protected UserInfo
getUserInfo(int userHandle)
protected String
getUserName()
Returns the name of the user.protected List<UserHandle>
getUserProfiles()
protected Bundle
getUserRestrictions(UserHandle userHandle)
protected List<UserInfo>
getUsers()
protected int
getUserSerialNumber(int userHandle)
protected int
getUserSwitchability()
protected boolean
hasUserRestriction(String restrictionKey, UserHandle userHandle)
protected boolean
hasUserRestrictionForUser(String restrictionKey, UserHandle userHandle)
protected boolean
isDemoUser()
protected boolean
isLinkedUser()
protected boolean
isManagedProfile()
If permissions are enforced (seeenforcePermissionChecks(boolean)
) and the application doesn't have theManifest.permission.MANAGE_USERS
permission, throws aSecurityManager
exception.protected boolean
isManagedProfile(int userHandle)
If permissions are enforced (seeenforcePermissionChecks(boolean)
) and the application doesn't have theManifest.permission.MANAGE_USERS
permission, throws aSecurityManager
exception.protected boolean
isProfile()
protected boolean
isQuietModeEnabled(UserHandle userHandle)
Query whether the quiet mode is enabled for a managed profile.protected boolean
isRestrictedProfile()
Returns 'false' by default, or the value specified viasetIsRestrictedProfile(boolean)
.protected boolean
isSameProfileGroup(UserHandle user, UserHandle otherUser)
protected boolean
isSystemUser()
protected boolean
isUserOfType(String userType)
protected boolean
isUserRunning(UserHandle handle)
protected boolean
isUserRunningOrStopping(UserHandle handle)
protected boolean
isUserUnlocked()
protected boolean
isUserUnlocked(UserHandle handle)
protected boolean
isUserUnlockingOrUnlocked(UserHandle handle)
protected boolean
removeUser(int userHandle)
protected boolean
removeUser(UserHandle user)
protected boolean
requestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle)
Request the quiet mode.protected boolean
requestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle, int flags)
Request the quiet mode.static void
reset()
protected void
sendQuietModeBroadcast(String action, UserHandle profileHandle)
If the current application has the necessary rights, it will receive the background action too.void
setApplicationRestrictions(String packageName, Bundle restrictions)
Sets the value returned byUserManager.getApplicationRestrictions(String)
.void
setCanSwitchUser(boolean canSwitchUser)
Deprecated.usesetUserSwitchability(int)
insteadvoid
setIsDemoUser(boolean isDemoUser)
Deprecated.UseaddUser(int, String, int)
to create a demo user instead of changing default user flags.void
setIsGuestUser(boolean isGuestUser)
Deprecated.UseaddUser(int, String, int)
to create a guest user instead of changing default user flags.void
setIsLinkedUser(boolean isLinkedUser)
Deprecated.UseaddUser(int, String, int)
to create a linked user instead of changing default user flags.void
setIsPrimaryUser(boolean isPrimaryUser)
Deprecated.UseaddUser(int, String, int)
to create a primary user instead of changing default user flags.void
setIsRestrictedProfile(boolean isRestrictedProfile)
Deprecated.useShadowUserManager#addUser()
insteadvoid
setIsSystemUser(boolean isSystemUser)
Deprecated.UseaddUser(int, String, int)
to create a system user instead of changing default user flags.void
setIsUserEnabled(int userId, boolean enabled)
void
setManagedProfile(boolean managedProfile)
Setter forUserManager.isManagedProfile()
.void
setMaxSupportedUsers(int maxSupportedUsers)
void
setProfileIsLocked(UserHandle profileHandle, boolean isLocked)
void
setSeedAccountName(String seedAccountName)
Setter forUserManager.getSeedAccountName()
void
setSeedAccountOptions(PersistableBundle seedAccountOptions)
Setter forUserManager.getSeedAccountOptions()
void
setSeedAccountType(String seedAccountType)
Setter forUserManager.getSeedAccountType()
void
setSerialNumberForUser(UserHandle userHandle, long serialNumber)
addUser(int, java.lang.String, int)
uses UserHandle for serialNumber.void
setSupportsMultipleUsers(boolean isMultiUserSupported)
Sets whether multiple users are supported; controls the return value ofUserManager#supportsMultipleUser
.protected void
setUserIcon(Bitmap icon)
protected void
setUserName(String name)
void
setUserRestriction(UserHandle userHandle, String restrictionKey, boolean value)
Deprecated.When possible, please use the real Android framework APIUserManager#setUserRestriction()
.protected void
setUserRestriction(String key, boolean value, UserHandle userHandle)
Shadows UserManager.setUserRestriction() API.void
setUserState(UserHandle handle, ShadowUserManager.UserState state)
Sets the current state for a given user, seeUserManager.isUserRunning(UserHandle)
andUserManager.isUserRunningOrStopping(UserHandle)
void
setUserSwitchability(int switchability)
Sets the user switchability for all users.void
setUserUnlocked(boolean userUnlocked)
Setter forUserManager.isUserUnlocked()
protected static boolean
supportsMultipleUsers()
void
switchUser(int userId)
Switches the current user touserHandle
.
-
-
-
Field Detail
-
DEFAULT_SECONDARY_USER_ID
public static final int DEFAULT_SECONDARY_USER_ID
The default user ID user for secondary user testing, when the ID is not otherwise specified.- See Also:
- Constant Field Values
-
FLAG_PRIMARY
public static final int FLAG_PRIMARY
- See Also:
- Constant Field Values
-
FLAG_ADMIN
public static final int FLAG_ADMIN
- See Also:
- Constant Field Values
-
FLAG_GUEST
public static final int FLAG_GUEST
- See Also:
- Constant Field Values
-
FLAG_RESTRICTED
public static final int FLAG_RESTRICTED
- See Also:
- Constant Field Values
-
FLAG_DEMO
public static final int FLAG_DEMO
- See Also:
- Constant Field Values
-
FLAG_MANAGED_PROFILE
public static final int FLAG_MANAGED_PROFILE
- See Also:
- Constant Field Values
-
FLAG_PROFILE
public static final int FLAG_PROFILE
- See Also:
- Constant Field Values
-
FLAG_FULL
public static final int FLAG_FULL
- See Also:
- Constant Field Values
-
FLAG_SYSTEM
public static final int FLAG_SYSTEM
- See Also:
- Constant Field Values
-
-
Method Detail
-
__constructor__
@Implementation protected void __constructor__(Context context, IUserManager service)
-
getApplicationRestrictions
@Implementation(minSdk=18) protected Bundle getApplicationRestrictions(String packageName)
Compared to real Android, there is no check that the package name matches the application package name and the method returns instantly.
-
setApplicationRestrictions
public void setApplicationRestrictions(String packageName, Bundle restrictions)
Sets the value returned byUserManager.getApplicationRestrictions(String)
.
-
addUserProfile
@Deprecated public long addUserProfile(UserHandle userHandle)
Deprecated.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
@Implementation(minSdk=21) protected List<UserHandle> getUserProfiles()
-
getProfiles
@Implementation(minSdk=21) protected List<UserInfo> getProfiles(int userHandle)
If any profiles have been added usingaddProfile(int, int, java.lang.String, int)
, return those profiles.Otherwise follow real android behaviour.
-
getEnabledProfiles
@Implementation(minSdk=30) protected List<UserHandle> getEnabledProfiles()
-
getAllProfiles
@Implementation(minSdk=30) protected List<UserHandle> getAllProfiles()
-
getProfileParent
@Implementation(minSdk=21) protected UserInfo getProfileParent(int userId)
-
createProfile
@Implementation(minSdk=30) protected UserHandle createProfile(String name, String userType, Set<String> disallowedPackages)
-
addProfile
public void addProfile(int userHandle, int profileUserHandle, String profileName, int profileFlags)
Add a profile to be returned bygetProfiles(int)
.*
-
setUserUnlocked
public void setUserUnlocked(boolean userUnlocked)
Setter forUserManager.isUserUnlocked()
-
isUserUnlocked
@Implementation(minSdk=24) protected boolean isUserUnlocked()
-
isUserUnlocked
@Implementation(minSdk=24) protected boolean isUserUnlocked(UserHandle handle)
- See Also:
setUserState(UserHandle, UserState)
-
isManagedProfile
@Implementation(minSdk=21) protected boolean isManagedProfile()
If permissions are enforced (seeenforcePermissionChecks(boolean)
) and the application doesn't have theManifest.permission.MANAGE_USERS
permission, throws aSecurityManager
exception.- Returns:
- false by default, or the value specified via
setManagedProfile(boolean)
- See Also:
enforcePermissionChecks(boolean)
,setManagedProfile(boolean)
-
isManagedProfile
@Implementation(minSdk=24) protected boolean isManagedProfile(int userHandle)
If permissions are enforced (seeenforcePermissionChecks(boolean)
) and the application doesn't have theManifest.permission.MANAGE_USERS
permission, throws aSecurityManager
exception.- Returns:
- true if the profile added has FLAG_MANAGED_PROFILE
- See Also:
enforcePermissionChecks(boolean)
,addProfile(int, int, String, int)
,addUser(int, String, int)
-
enforcePermissionChecks
public void enforcePermissionChecks(boolean enforcePermissions)
-
setManagedProfile
public void setManagedProfile(boolean managedProfile)
Setter forUserManager.isManagedProfile()
.
-
isProfile
@Implementation(minSdk=30) protected boolean isProfile()
-
isUserOfType
@Implementation(minSdk=30) protected boolean isUserOfType(String userType)
-
isSameProfileGroup
@Implementation(minSdk=30) protected boolean isSameProfileGroup(UserHandle user, UserHandle otherUser)
-
hasUserRestriction
@Implementation(minSdk=21) protected boolean hasUserRestriction(String restrictionKey, UserHandle userHandle)
-
setUserRestriction
@Implementation(minSdk=18) protected void setUserRestriction(String key, boolean value, UserHandle userHandle)
Shadows 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
@Deprecated public void setUserRestriction(UserHandle userHandle, String restrictionKey, boolean value)
Deprecated.When possible, please use the real Android framework APIUserManager#setUserRestriction()
.
-
clearUserRestrictions
public void clearUserRestrictions(UserHandle userHandle)
Removes all user restrictions set of a user identified byuserHandle
.
-
getUserRestrictions
@Implementation(minSdk=18) protected Bundle getUserRestrictions(UserHandle userHandle)
-
getSerialNumberForUser
@Implementation protected long getSerialNumberForUser(UserHandle userHandle)
-
setSerialNumberForUser
public void setSerialNumberForUser(UserHandle userHandle, long serialNumber)
addUser(int, java.lang.String, int)
uses UserHandle for serialNumber. setSerialNumberForUser() allows assigning an arbitary serialNumber. Some test use serialNumber!=0 as secondary user check, so it's necessary to "fake" the serialNumber to a non-zero value.
-
getUserForSerialNumber
@Implementation protected UserHandle getUserForSerialNumber(long serialNumber)
-
getUserSerialNumber
@Implementation protected int getUserSerialNumber(int userHandle)
-
getUserName
@Implementation(minSdk=29) protected String getUserName()
Returns 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
@Implementation(minSdk=30) protected void setUserName(String name)
-
getUserIcon
@Implementation(minSdk=29) protected Bitmap getUserIcon()
-
setUserIcon
@Implementation(minSdk=29) protected void setUserIcon(Bitmap icon)
-
getUserHandle
@HiddenApi @Implementation(minSdk=17) protected int getUserHandle(int serialNumber)
- Returns:
- user id for given user serial number.
-
getMaxSupportedUsers
@HiddenApi @Implementation(minSdk=17) protected static int getMaxSupportedUsers()
-
setMaxSupportedUsers
public void setMaxSupportedUsers(int maxSupportedUsers)
-
isDemoUser
@Implementation(minSdk=25) protected boolean isDemoUser()
- Returns:
- false by default, or the value specified via
setIsDemoUser(boolean)
-
setIsDemoUser
@Deprecated public void setIsDemoUser(boolean isDemoUser)
Deprecated.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
@Implementation(minSdk=23) protected boolean isSystemUser()
- Returns:
- 'true' by default, or the value specified via
setIsSystemUser(boolean)
-
setIsSystemUser
@Deprecated public void setIsSystemUser(boolean isSystemUser)
Deprecated.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()
.
-
setIsPrimaryUser
@Deprecated public void setIsPrimaryUser(boolean isPrimaryUser)
Deprecated.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
@Implementation(minSdk=18) protected boolean isLinkedUser()
- Returns:
- 'false' by default, or the value specified via
setIsLinkedUser(boolean)
-
setIsLinkedUser
@Deprecated public void setIsLinkedUser(boolean isLinkedUser)
Deprecated.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()
.
-
isRestrictedProfile
@Implementation(minSdk=28) protected boolean isRestrictedProfile()
Returns 'false' by default, or the value specified viasetIsRestrictedProfile(boolean)
.
-
setIsRestrictedProfile
@Deprecated public void setIsRestrictedProfile(boolean isRestrictedProfile)
Deprecated.useShadowUserManager#addUser()
insteadSets this process running under a restricted profile; controls the return value ofUserManager.isRestrictedProfile()
.
-
setIsGuestUser
@Deprecated public void setIsGuestUser(boolean isGuestUser)
Deprecated.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()
.
-
setIsUserEnabled
public void setIsUserEnabled(int userId, boolean enabled)
-
isUserRunning
@Implementation protected boolean isUserRunning(UserHandle handle)
- See Also:
setUserState(UserHandle, UserState)
-
isUserRunningOrStopping
@Implementation protected boolean isUserRunningOrStopping(UserHandle handle)
- See Also:
setUserState(UserHandle, UserState)
-
isUserUnlockingOrUnlocked
@Implementation(minSdk=30) protected boolean isUserUnlockingOrUnlocked(UserHandle handle)
- See Also:
setUserState(UserHandle, UserState)
-
setUserState
public void setUserState(UserHandle handle, ShadowUserManager.UserState state)
Sets the current state for a given user, seeUserManager.isUserRunning(UserHandle)
andUserManager.isUserRunningOrStopping(UserHandle)
-
isQuietModeEnabled
@Implementation(minSdk=26) protected boolean isQuietModeEnabled(UserHandle userHandle)
Query 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 withtrue
for the managed profile, in which case it will always fail.
-
sendQuietModeBroadcast
protected void sendQuietModeBroadcast(String action, UserHandle profileHandle)
If the current application has the necessary rights, it will receive the background action too.
-
setProfileIsLocked
public void setProfileIsLocked(UserHandle profileHandle, boolean isLocked)
-
getSerialNumbersOfUsers
@Implementation(minSdk=24) protected long[] getSerialNumbersOfUsers(boolean excludeDying)
-
getUsers
@Implementation protected List<UserInfo> getUsers()
-
getUserInfo
@Implementation protected UserInfo getUserInfo(int userHandle)
-
setCanSwitchUser
@Deprecated public void setCanSwitchUser(boolean canSwitchUser)
Deprecated.usesetUserSwitchability(int)
insteadSets whether switching users is allowed or not; controls the return value ofUserManager#canSwitchUser()
-
getSeedAccountName
@Implementation(minSdk=24) protected String getSeedAccountName()
-
setSeedAccountName
public void setSeedAccountName(String seedAccountName)
Setter forUserManager.getSeedAccountName()
-
getSeedAccountType
@Implementation(minSdk=24) protected String getSeedAccountType()
-
setSeedAccountType
public void setSeedAccountType(String seedAccountType)
Setter forUserManager.getSeedAccountType()
-
getSeedAccountOptions
@Implementation(minSdk=24) protected PersistableBundle getSeedAccountOptions()
-
setSeedAccountOptions
public void setSeedAccountOptions(PersistableBundle seedAccountOptions)
Setter forUserManager.getSeedAccountOptions()
-
clearSeedAccountData
@Implementation(minSdk=24) protected void clearSeedAccountData()
-
removeUser
@Implementation(minSdk=17) protected boolean removeUser(int userHandle)
-
removeUser
@Implementation(minSdk=29) protected boolean removeUser(UserHandle user)
-
supportsMultipleUsers
@Implementation(minSdk=24) protected static boolean supportsMultipleUsers()
-
setSupportsMultipleUsers
public void setSupportsMultipleUsers(boolean isMultiUserSupported)
Sets whether multiple users are supported; controls the return value ofUserManager#supportsMultipleUser
.
-
switchUser
public void switchUser(int userId)
Switches the current user touserHandle
.- Parameters:
userId
- the integer handle of the user, where 0 is the primary user.
-
addUser
public UserHandle addUser(int id, String name, int flags)
Creates a user with the specified name, userId and flags.- Parameters:
id
- the unique id of username
- name of the userflags
- 16 bits for user type. SeeUserInfo.flags
- Returns:
- a handle to the new user
-
canSwitchUsers
@Implementation(minSdk=24, maxSdk=29) protected boolean canSwitchUsers()
Returnstrue
by default, or the value specified viasetCanSwitchUser(boolean)
.
-
getUserSwitchability
@Implementation(minSdk=29) protected int getUserSwitchability()
-
setUserSwitchability
public 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 withtrue
for 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.
-
reset
@Resetter public static void reset()
-
-