@Implements(value=android.os.UserManager.class, minSdk=17) public class ShadowUserManager extends java.lang.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__(android.content.Context context,
android.os.IUserManager service) |
void |
addUser(int id,
java.lang.String name,
int flags)
Creates a user with the specified name, userId and flags.
|
long |
addUserProfile(android.os.UserHandle userHandle)
Adds a profile associated for the user that the calling process is running on.
|
protected boolean |
canSwitchUsers()
Returns
true by default, or the value specified via setCanSwitchUser(boolean) . |
void |
clearUserRestrictions(android.os.UserHandle userHandle)
Removes all user restrictions set of a user identified by
userHandle . |
void |
enforcePermissionChecks(boolean enforcePermissions) |
protected android.os.Bundle |
getApplicationRestrictions(java.lang.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 long |
getSerialNumberForUser(android.os.UserHandle userHandle) |
protected android.os.UserHandle |
getUserForSerialNumber(long serialNumber) |
protected android.content.pm.UserInfo |
getUserInfo(int userHandle) |
protected java.util.List<android.os.UserHandle> |
getUserProfiles() |
protected android.os.Bundle |
getUserRestrictions(android.os.UserHandle userHandle) |
protected java.util.List<android.content.pm.UserInfo> |
getUsers() |
protected boolean |
hasUserRestriction(java.lang.String restrictionKey,
android.os.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. |
protected boolean |
isSystemUser() |
protected boolean |
isUserRunning(android.os.UserHandle handle) |
protected boolean |
isUserRunningOrStopping(android.os.UserHandle handle) |
protected boolean |
isUserUnlocked() |
protected boolean |
removeUser(int userHandle) |
static void |
reset() |
void |
setApplicationRestrictions(java.lang.String packageName,
android.os.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#canSwitchUser() |
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 |
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 |
setSerialNumberForUser(android.os.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 |
setUserRestriction(android.os.UserHandle userHandle,
java.lang.String restrictionKey,
boolean value) |
void |
setUserState(android.os.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() |
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__(android.content.Context context, android.os.IUserManager service)
@Implementation(minSdk=18) protected android.os.Bundle getApplicationRestrictions(java.lang.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(java.lang.String packageName, android.os.Bundle restrictions)
Sets the value returned by UserManager.getApplicationRestrictions(String)
.
public long addUserProfile(android.os.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 java.util.List<android.os.UserHandle> getUserProfiles()
@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(java.lang.String restrictionKey, android.os.UserHandle userHandle)
public void setUserRestriction(android.os.UserHandle userHandle, java.lang.String restrictionKey, boolean value)
public void clearUserRestrictions(android.os.UserHandle userHandle)
Removes all user restrictions set of a user identified by userHandle
.
@Implementation(minSdk=18) protected android.os.Bundle getUserRestrictions(android.os.UserHandle userHandle)
@Implementation protected long getSerialNumberForUser(android.os.UserHandle userHandle)
addUserProfile(UserHandle)
@Deprecated public void setSerialNumberForUser(android.os.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 android.os.UserHandle getUserForSerialNumber(long serialNumber)
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()
.
@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(android.os.UserHandle handle)
setUserState(UserHandle, UserState)
@Implementation protected boolean isUserRunningOrStopping(android.os.UserHandle handle)
setUserState(UserHandle, UserState)
public void setUserState(android.os.UserHandle handle, ShadowUserManager.UserState state)
Sets the current state for a given user, see UserManager.isUserRunning(UserHandle)
and UserManager.isUserRunningOrStopping(UserHandle)
@Implementation protected java.util.List<android.content.pm.UserInfo> getUsers()
@Implementation protected android.content.pm.UserInfo getUserInfo(int userHandle)
@Implementation(minSdk=24) protected boolean canSwitchUsers()
Returns true
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#canSwitchUser()
@Implementation(minSdk=17) protected boolean removeUser(int userHandle)
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 void addUser(int id, java.lang.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()