@Implements(value=android.os.UserManager.class, minSdk=17) public class ShadowUserManager extends Object
UserManager
.Modifier and Type | Class | Description |
---|---|---|
static class |
ShadowUserManager.UserState |
Describes the current state of the user.
|
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_GUEST |
|
static int |
FLAG_MANAGED_PROFILE |
|
static int |
FLAG_PRIMARY |
|
static int |
FLAG_RESTRICTED |
|
protected Map<Integer,UserInfo> |
userInfoMap |
Holds the UserInfo for all registered users and profiles, indexed by UserHandle.id
|
protected static Map<Integer,Integer> |
userPidMap |
|
protected Map<Integer,List<UserHandle>> |
userProfilesListMap |
Each user holds a list of UserHandles of assocated profiles and user itself.
|
protected com.google.common.collect.BiMap<Integer,Long> |
userSerialNumbers |
Holds the serial numbers for all users and profiles, indexed by UserHandle.id
|
protected Map<Integer,ShadowUserManager.UserState> |
userState |
Holds all UserStates, indexed by UserHandle.id
|
Constructor | Description |
---|---|
ShadowUserManager() |
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 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) |
Deprecated.
use either addUser() or addProfile()
|
protected boolean |
canSwitchUsers() |
Returns
true 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 UserInfo |
getProfileParent(int userId) |
|
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 int |
getUserHandle(int serialNumber) |
|
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 |
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 |
isManagedProfile(int userHandle) |
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 |
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 via
setIsRestrictedProfile(boolean) . |
protected boolean |
isSystemUser() |
|
protected boolean |
isUserRunning(UserHandle handle) |
|
protected boolean |
isUserRunningOrStopping(UserHandle handle) |
|
protected boolean |
isUserUnlocked() |
|
protected boolean |
isUserUnlocked(UserHandle handle) |
|
protected boolean |
removeUser(int userHandle) |
|
protected boolean |
removeUser(UserHandle user) |
|
protected boolean |
requestQuietModeEnabled(boolean enableQuietMode,
UserHandle userHandle) |
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 by
UserManager.getApplicationRestrictions(String) . |
void |
setCanSwitchUser(boolean canSwitchUser) |
Deprecated.
use
setUserSwitchability(int) instead |
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 |
setProfileIsLocked(UserHandle profileHandle,
boolean isLocked) |
|
void |
setSeedAccountType(String seedAccountType) |
Setter for
UserManager.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 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 |
setUserSwitchability(int switchability) |
Sets the user switchability for all users.
|
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
public static final int FLAG_PRIMARY
public static final int FLAG_ADMIN
public static final int FLAG_GUEST
public static final int FLAG_RESTRICTED
public static final int FLAG_DEMO
public static final int FLAG_MANAGED_PROFILE
protected com.google.common.collect.BiMap<Integer,Long> userSerialNumbers
protected Map<Integer,ShadowUserManager.UserState> userState
protected Map<Integer,UserInfo> userInfoMap
protected Map<Integer,List<UserHandle>> userProfilesListMap
@Implementation protected void __constructor__(Context context, IUserManager service)
@Implementation(minSdk=18) protected Bundle getApplicationRestrictions(String packageName)
public void setApplicationRestrictions(String packageName, Bundle restrictions)
UserManager.getApplicationRestrictions(String)
.@Deprecated public long addUserProfile(UserHandle userHandle)
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)
addProfile(int, int, java.lang.String, int)
, return those profiles.
Otherwise follow real android behaviour.@Implementation(minSdk=21) protected UserInfo getProfileParent(int userId)
public void addProfile(int userHandle, int profileUserHandle, String profileName, int profileFlags)
getProfiles(int)
.public void setUserUnlocked(boolean userUnlocked)
UserManager.isUserUnlocked()
@Implementation(minSdk=24) protected boolean isUserUnlocked()
@Implementation(minSdk=24) protected boolean isUserUnlocked(UserHandle handle)
setUserState(UserHandle, UserState)
@Implementation(minSdk=21) protected boolean isManagedProfile()
enforcePermissionChecks(boolean)
) and the application
doesn't have the Manifest.permission.MANAGE_USERS
permission, throws a SecurityManager
exception.setManagedProfile(boolean)
enforcePermissionChecks(boolean)
,
setManagedProfile(boolean)
@Implementation(minSdk=24) protected boolean isManagedProfile(int userHandle)
enforcePermissionChecks(boolean)
) and the application
doesn't have the Manifest.permission.MANAGE_USERS
permission, throws a SecurityManager
exception.enforcePermissionChecks(boolean)
,
addProfile(int, int, String, int)
,
addUser(int, String, int)
public void enforcePermissionChecks(boolean enforcePermissions)
public void setManagedProfile(boolean managedProfile)
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)
userHandle
.@Implementation(minSdk=18) protected Bundle getUserRestrictions(UserHandle userHandle)
@Implementation protected long getSerialNumberForUser(UserHandle userHandle)
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.@Implementation protected UserHandle getUserForSerialNumber(long serialNumber)
@Implementation protected int getUserSerialNumber(int userHandle)
@Implementation(minSdk=29) protected String getUserName()
@HiddenApi @Implementation(minSdk=17) protected int getUserHandle(int serialNumber)
@Implementation(minSdk=25) protected boolean isDemoUser()
setIsDemoUser(boolean)
@Deprecated public void setIsDemoUser(boolean isDemoUser)
addUser(int, String, int)
to create a demo user
instead of changing default user flags.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.UserManager.isSystemUser()
.@Deprecated public void setIsPrimaryUser(boolean isPrimaryUser)
addUser(int, String, int)
to create a primary user
instead of changing default user flags.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.UserManager.isLinkedUser()
.@Implementation(minSdk=28) protected boolean isRestrictedProfile()
setIsRestrictedProfile(boolean)
.public void setIsRestrictedProfile(boolean isRestrictedProfile)
UserManager.isRestrictedProfile()
.@Deprecated public void setIsGuestUser(boolean isGuestUser)
addUser(int, String, int)
to create a guest user
instead of changing default user flags.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)
UserManager.isUserRunning(UserHandle)
and UserManager.isUserRunningOrStopping(UserHandle)
@Implementation(minSdk=26) protected boolean isQuietModeEnabled(UserHandle userHandle)
This method checks whether the user handle corresponds to a managed profile, and then query its state. When quiet, the user is not running.
@Implementation(minSdk=29) protected boolean requestQuietModeEnabled(boolean enableQuietMode, UserHandle userHandle)
setProfileIsLocked(UserHandle, boolean)
is called with
true
for the managed profile, in which case it will always fail.protected void sendQuietModeBroadcast(String action, UserHandle profileHandle)
public void setProfileIsLocked(UserHandle profileHandle, boolean isLocked)
@Implementation protected List<UserInfo> getUsers()
@Implementation protected UserInfo getUserInfo(int userHandle)
@Deprecated public void setCanSwitchUser(boolean canSwitchUser)
setUserSwitchability(int)
insteadUserManager#canSwitchUser()
@Implementation(minSdk=29) protected String getSeedAccountType()
public void setSeedAccountType(String seedAccountType)
UserManager.getSeedAccountType()
@Implementation(minSdk=17) protected boolean removeUser(int userHandle)
@Implementation(minSdk=29) protected boolean removeUser(UserHandle user)
@Implementation(minSdk=24) protected static boolean supportsMultipleUsers()
public void setSupportsMultipleUsers(boolean isMultiUserSupported)
UserManager#supportsMultipleUser
.public void switchUser(int userId)
userHandle
.userId
- the integer handle of the user, where 0 is the primary user.public UserHandle addUser(int id, String name, int flags)
id
- the unique id of username
- name of the userflags
- 16 bits for user type. See UserInfo.flags
@Implementation(minSdk=24, maxSdk=29) protected boolean canSwitchUsers()
true
by default, or the value specified via setCanSwitchUser(boolean)
.@Implementation(minSdk=29) protected int getUserSwitchability()
public void setUserSwitchability(int switchability)
@Resetter public static void reset()