@Implements(value=android.os.UserManager.class, minSdk=17) public class ShadowUserManager extends Object
Robolectric implementation of UserManager
.
Modifier and Type | Class and Description |
---|---|
static class |
ShadowUserManager.UserState
Describes the current state of the user.
|
Modifier and Type | Field and 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_GUEST |
static int |
FLAG_PRIMARY |
static int |
FLAG_RESTRICTED |
Constructor and Description |
---|
ShadowUserManager() |
Modifier and Type | Method and Description |
---|---|
protected void |
__constructor__(Context context,
IUserManager service) |
void |
addProfile(int userHandle,
int profileUserHandle,
String profileName,
int profileFlags)
Add a profile to be returned by
getProfiles(int) . |
UserHandle |
addUser(int id,
String name,
int flags)
Creates a user with the specified name, userId and flags.
|
long |
addUserProfile(UserHandle userHandle)
Adds a profile associated for the user that the calling process is running on.
|
protected boolean |
canSwitchUsers()
Returns
false by default, or the value specified via setCanSwitchUser(boolean) . |
void |
clearUserRestrictions(UserHandle userHandle)
Removes all user restrictions set of a user identified by
userHandle . |
void |
enforcePermissionChecks(boolean enforcePermissions) |
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<UserInfo> |
getProfiles(int userHandle)
If any profiles have been added using
addProfile(int, int, java.lang.String, int) , return those profiles. |
protected String |
getSeedAccountType() |
protected long |
getSerialNumberForUser(UserHandle userHandle) |
protected UserHandle |
getUserForSerialNumber(long serialNumber) |
protected UserInfo |
getUserInfo(int userHandle) |
protected List<UserHandle> |
getUserProfiles() |
protected Bundle |
getUserRestrictions(UserHandle userHandle) |
protected List<UserInfo> |
getUsers() |
protected int |
getUserSerialNumber(int userHandle) |
protected boolean |
hasUserRestriction(String restrictionKey,
UserHandle userHandle) |
protected boolean |
isDemoUser() |
protected boolean |
isLinkedUser() |
protected boolean |
isManagedProfile()
If permissions are enforced (see
enforcePermissionChecks(boolean) ) and the application doesn’t have the Manifest.permission.MANAGE_USERS permission, throws a SecurityManager exception. |
boolean |
isRestrictedProfile()
Returns ‘false’ by default, or the value specified via
setIsRestrictedProfile(boolean) . |
protected boolean |
isSystemUser() |
protected boolean |
isUserRunning(UserHandle handle) |
protected boolean |
isUserRunningOrStopping(UserHandle handle) |
protected boolean |
isUserUnlocked() |
protected boolean |
removeUser(int userHandle) |
protected boolean |
removeUser(UserHandle user) |
static void |
reset() |
void |
setApplicationRestrictions(String packageName,
Bundle restrictions)
Sets the value returned by
UserManager.getApplicationRestrictions(String) . |
void |
setCanSwitchUser(boolean canSwitchUser)
Sets whether switching users is allowed or not; controls the return value of
UserManager.canSwitchUsers() |
void |
setIsDemoUser(boolean isDemoUser)
Deprecated.
Use
addUser(int, String, int) to create a demo user instead of changing default user flags. |
void |
setIsGuestUser(boolean isGuestUser)
Deprecated.
Use
addUser(int, String, int) to create a guest user instead of changing default user flags. |
void |
setIsLinkedUser(boolean isLinkedUser)
Deprecated.
Use
addUser(int, String, int) to create a linked user instead of changing default user flags. |
void |
setIsPrimaryUser(boolean isPrimaryUser)
Deprecated.
Use
addUser(int, String, int) to create a primary user instead of changing default user flags. |
void |
setIsRestrictedProfile(boolean isRestrictedProfile)
Sets this process running under a restricted profile; controls the return value of
UserManager.isRestrictedProfile() . |
void |
setIsSystemUser(boolean isSystemUser)
Deprecated.
Use
addUser(int, String, int) to create a system user instead of changing default user flags. |
void |
setManagedProfile(boolean managedProfile)
Setter for
UserManager.isManagedProfile() . |
void |
setSeedAccountType(String seedAccountType)
Setter for
UserManager.getSeedAccountType() |
void |
setSerialNumberForUser(UserHandle userHandle,
long serialNumber)
Deprecated.
prefer
addUserProfile(UserHandle) to ensure consistency of profiles known to the UserManager . Furthermore, calling this method for the current user, i.e: Process.myUserHandle() is no longer necessary as this user is always known to UserManager and has a preassigned serial number. |
void |
setSupportsMultipleUsers(boolean isMultiUserSupported)
Sets whether multiple users are supported; controls the return value of
UserManager#supportsMultipleUser . |
void |
setUserRestriction(UserHandle userHandle,
String restrictionKey,
boolean value) |
void |
setUserState(UserHandle handle,
ShadowUserManager.UserState state)
Sets the current state for a given user, see
UserManager.isUserRunning(UserHandle) and UserManager.isUserRunningOrStopping(UserHandle) |
void |
setUserUnlocked(boolean userUnlocked)
Setter for
UserManager.isUserUnlocked() |
protected static boolean |
supportsMultipleUsers() |
void |
switchUser(int userId)
Switches the current user to
userHandle . |
public static final int DEFAULT_SECONDARY_USER_ID
The default user ID user for secondary user testing, when the ID is not otherwise specified.
public static final int FLAG_PRIMARY
public static final int FLAG_ADMIN
public static final int FLAG_GUEST
public static final int FLAG_RESTRICTED
@Implementation protected void __constructor__(Context context, IUserManager service)
@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.
public void setApplicationRestrictions(String packageName, Bundle restrictions)
Sets the value returned by UserManager.getApplicationRestrictions(String)
.
public long addUserProfile(UserHandle userHandle)
Adds a profile associated for the user that the calling process is running on.
The user is assigned an arbitrary unique serial number.
@Implementation(minSdk=21) protected List<UserHandle> getUserProfiles()
@Implementation(minSdk=21) protected List<UserInfo> getProfiles(int userHandle)
If any profiles have been added using addProfile(int, int, java.lang.String, int)
, return those profiles.
Otherwise follow real android behaviour.
public void addProfile(int userHandle, int profileUserHandle, String profileName, int profileFlags)
Add a profile to be returned by getProfiles(int)
.
@Implementation(minSdk=24) protected boolean isUserUnlocked()
public void setUserUnlocked(boolean userUnlocked)
Setter for UserManager.isUserUnlocked()
@Implementation(minSdk=21) protected boolean isManagedProfile()
If permissions are enforced (see enforcePermissionChecks(boolean)
) and the application doesn’t have the Manifest.permission.MANAGE_USERS
permission, throws a SecurityManager
exception.
false
by default, or the value specified via setManagedProfile(boolean)
enforcePermissionChecks(boolean)
,
setManagedProfile(boolean)
public void enforcePermissionChecks(boolean enforcePermissions)
public void setManagedProfile(boolean managedProfile)
Setter for UserManager.isManagedProfile()
.
@Implementation(minSdk=21) protected boolean hasUserRestriction(String restrictionKey, UserHandle userHandle)
public void setUserRestriction(UserHandle userHandle, String restrictionKey, boolean value)
public void clearUserRestrictions(UserHandle userHandle)
Removes all user restrictions set of a user identified by userHandle
.
@Implementation(minSdk=18) protected Bundle getUserRestrictions(UserHandle userHandle)
@Implementation protected long getSerialNumberForUser(UserHandle userHandle)
addUserProfile(UserHandle)
@Deprecated public void setSerialNumberForUser(UserHandle userHandle, long serialNumber)
addUserProfile(UserHandle)
to ensure consistency of profiles known to the UserManager
. Furthermore, calling this method for the current user, i.e: Process.myUserHandle()
is no longer necessary as this user is always known to UserManager and has a preassigned serial number.@Implementation protected UserHandle getUserForSerialNumber(long serialNumber)
addUserProfile(UserHandle)
@Implementation protected int getUserSerialNumber(int userHandle)
addUserProfile(UserHandle)
@Implementation(minSdk=25) protected boolean isDemoUser()
false
by default, or the value specified via setIsDemoUser(boolean)
@Deprecated public void setIsDemoUser(boolean isDemoUser)
addUser(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 of UserManager.isDemoUser()
.
@Implementation(minSdk=23) protected boolean isSystemUser()
setIsSystemUser(boolean)
@Deprecated public void setIsSystemUser(boolean isSystemUser)
addUser(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 of UserManager.isSystemUser()
.
@Deprecated public void setIsPrimaryUser(boolean isPrimaryUser)
addUser(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 of UserManager.isPrimaryUser()
.
@Implementation(minSdk=18) protected boolean isLinkedUser()
setIsLinkedUser(boolean)
@Deprecated public void setIsLinkedUser(boolean isLinkedUser)
addUser(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 of UserManager.isLinkedUser()
.
public boolean isRestrictedProfile()
Returns ‘false’ by default, or the value specified via setIsRestrictedProfile(boolean)
.
public void setIsRestrictedProfile(boolean isRestrictedProfile)
Sets this process running under a restricted profile; controls the return value of UserManager.isRestrictedProfile()
.
@Deprecated public void setIsGuestUser(boolean isGuestUser)
addUser(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 of UserManager.isGuestUser()
.
@Implementation protected boolean isUserRunning(UserHandle handle)
setUserState(UserHandle, UserState)
@Implementation protected boolean isUserRunningOrStopping(UserHandle handle)
setUserState(UserHandle, UserState)
public void setUserState(UserHandle handle, ShadowUserManager.UserState state)
Sets the current state for a given user, see UserManager.isUserRunning(UserHandle)
and UserManager.isUserRunningOrStopping(UserHandle)
@Implementation protected List<UserInfo> getUsers()
@Implementation protected UserInfo getUserInfo(int userHandle)
@Implementation(minSdk=24) protected boolean canSwitchUsers()
Returns false
by default, or the value specified via setCanSwitchUser(boolean)
.
public void setCanSwitchUser(boolean canSwitchUser)
Sets whether switching users is allowed or not; controls the return value of UserManager.canSwitchUsers()
@Implementation(minSdk=10000) protected String getSeedAccountType()
public void setSeedAccountType(String seedAccountType)
Setter for UserManager.getSeedAccountType()
@Implementation(minSdk=17) protected boolean removeUser(int userHandle)
@Implementation(minSdk=10000) protected boolean removeUser(UserHandle user)
@Implementation(minSdk=24) protected static boolean supportsMultipleUsers()
public void setSupportsMultipleUsers(boolean isMultiUserSupported)
Sets whether multiple users are supported; controls the return value of UserManager#supportsMultipleUser
.
public void switchUser(int userId)
Switches the current user to userHandle
.
userId
- the integer handle of the user, where 0 is the primary user.public UserHandle addUser(int id, String name, int flags)
Creates a user with the specified name, userId and flags.
id
- the unique id of username
- name of the userflags
- 16 bits for user type. See UserInfo.flags
@Resetter public static void reset()