Class ShadowUserManager

java.lang.Object
org.robolectric.shadows.ShadowUserManager

@Implements(android.os.UserManager.class) public class ShadowUserManager extends Object
Robolectric implementation of UserManager.
  • Field Details

  • Constructor Details

    • ShadowUserManager

      public ShadowUserManager()
  • Method Details

    • __constructor__

      @Implementation protected void __constructor__(Context context, IUserManager service)
    • getApplicationRestrictions

      @Implementation 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.
      See Also:
    • setApplicationRestrictions

      public void setApplicationRestrictions(String packageName, Bundle restrictions)
    • 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 using addProfile(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 by getProfiles(int).
    • addProfile

      public void addProfile(int userHandle, int profileUserHandle, UserInfo profileUserInfo)
      Add a profile to be returned by getProfiles(int).
    • setUserUnlocked

      public void setUserUnlocked(boolean userUnlocked)
    • isUserUnlocked

      @Implementation(minSdk=24) protected boolean isUserUnlocked()
    • isUserUnlocked

      @Implementation(minSdk=24) protected boolean isUserUnlocked(UserHandle handle)
      See Also:
    • isManagedProfile

      @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.
      Returns:
      false by default, or the value specified via setManagedProfile(boolean)
      See Also:
    • isManagedProfile

      @Implementation(minSdk=24) 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.
      Returns:
      true if the profile added has FLAG_MANAGED_PROFILE
      See Also:
    • enforcePermissionChecks

      public void enforcePermissionChecks(boolean enforcePermissions)
    • setManagedProfile

      public void setManagedProfile(boolean managedProfile)
    • isCloneProfile

      @Implementation(minSdk=31) protected boolean isCloneProfile()
      If permissions are enforced (see enforcePermissionChecks(boolean)) and the application doesn't have the Manifest.permission.MANAGE_USERS permission, throws a SecurityManager exception.
      Returns:
      true if the user is clone, or the value specified via setCloneProfile(boolean)
      See Also:
    • setCloneProfile

      public void setCloneProfile(boolean cloneProfile)
    • 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 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

      @Implementation protected void setUserRestriction(String key, boolean value)
    • setUserRestriction

      @Deprecated public void setUserRestriction(UserHandle userHandle, String restrictionKey, boolean value)
      Deprecated.
      When possible, please use the real Android framework API UserManager#setUserRestriction().
    • clearUserRestrictions

      public void clearUserRestrictions(UserHandle userHandle)
      Removes all user restrictions set of a user identified by userHandle.
    • getUserRestrictions

      @Implementation protected Bundle getUserRestrictions(UserHandle userHandle)
    • getSerialNumberForUser

      @Implementation protected long getSerialNumberForUser(UserHandle userHandle)
      See Also:
    • 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)
      See Also:
    • getUserSerialNumber

      @Implementation protected int getUserSerialNumber(int userHandle)
      See Also:
    • 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 protected int getUserHandle(int serialNumber)
      Returns:
      user id for given user serial number.
    • getUserHandles

      @HiddenApi @Implementation(minSdk=30) protected List<UserHandle> getUserHandles(boolean excludeDying)
    • getMaxSupportedUsers

      @HiddenApi @Implementation 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.
      Use 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().
    • 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.
      Use 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().
    • setIsPrimaryUser

      @Deprecated public void setIsPrimaryUser(boolean isPrimaryUser)
      Deprecated.
      Use 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().
    • isLinkedUser

      @Implementation protected boolean isLinkedUser()
      Returns:
      'false' by default, or the value specified via setIsLinkedUser(boolean)
    • setIsLinkedUser

      @Deprecated public void setIsLinkedUser(boolean isLinkedUser)
      Deprecated.
      Use 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().
    • isRestrictedProfile

      @Implementation(minSdk=28) protected boolean isRestrictedProfile()
      Returns 'false' by default, or the value specified via setIsRestrictedProfile(boolean).
    • setIsRestrictedProfile

      @Deprecated public void setIsRestrictedProfile(boolean isRestrictedProfile)
      Deprecated.
      use ShadowUserManager#addUser() instead
      Sets this process running under a restricted profile; controls the return value of UserManager.isRestrictedProfile().
    • setIsGuestUser

      @Deprecated public void setIsGuestUser(boolean isGuestUser)
      Deprecated.
      Use 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().
    • setIsUserEnabled

      public void setIsUserEnabled(int userId, boolean enabled)
    • isUserRunning

      @Implementation protected boolean isUserRunning(UserHandle handle)
      See Also:
    • isUserRunningOrStopping

      @Implementation protected boolean isUserRunningOrStopping(UserHandle handle)
      See Also:
    • isUserUnlockingOrUnlocked

      @Implementation(minSdk=30) protected boolean isUserUnlockingOrUnlocked(UserHandle handle)
      See Also:
    • setUserState

      public void setUserState(UserHandle handle, ShadowUserManager.UserState state)
    • isQuietModeEnabled

      @Implementation(minSdk=24) 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 with true 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.
      Sets whether switching users is allowed or not; controls the return value of UserManager#canSwitchUser()
    • getSeedAccountName

      @Implementation(minSdk=24) protected String getSeedAccountName()
    • setSeedAccountName

      public void setSeedAccountName(String seedAccountName)
    • getSeedAccountType

      @Implementation(minSdk=24) protected String getSeedAccountType()
    • setSeedAccountType

      public void setSeedAccountType(String seedAccountType)
    • getSeedAccountOptions

      @Implementation(minSdk=24) protected PersistableBundle getSeedAccountOptions()
    • setSeedAccountOptions

      public void setSeedAccountOptions(PersistableBundle seedAccountOptions)
    • clearSeedAccountData

      @Implementation(minSdk=24) protected void clearSeedAccountData()
    • removeUser

      @Implementation protected boolean removeUser(int userHandle)
    • removeUser

      @Implementation(minSdk=29) protected boolean removeUser(UserHandle user)
    • removeUserWhenPossible

      @Implementation(minSdk=33) protected int removeUserWhenPossible(UserHandle user, boolean overrideDevicePolicy)
    • supportsMultipleUsers

      @Implementation(minSdk=24) protected static boolean supportsMultipleUsers()
    • setSupportsMultipleUsers

      public void setSupportsMultipleUsers(boolean isMultiUserSupported)
      Sets whether multiple users are supported; controls the return value of UserManager#supportsMultipleUser.
    • switchUser

      public void switchUser(int userId)
      Switches the current user to userHandle.
      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 user
      name - name of the user
      flags - 16 bits for user type. See UserInfo.flags
      Returns:
      a handle to the new user
    • canSwitchUsers

      @Implementation(minSdk=24, maxSdk=29) protected boolean canSwitchUsers()
      Returns true by default, or the value specified via setCanSwitchUser(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 with true for the managed profile a request to disable the quiet mode will fail and return false (i.e. as if the user refused to authenticate). Otherwise, the call will always succeed and return true.

      This method simply re-directs to requestQuietModeEnabled(boolean, UserHandle) as it already has the desired behavior irrespective of the flag's value.

    • isHeadlessSystemUserMode

      @Implementation(minSdk=31) protected static boolean isHeadlessSystemUserMode()
    • setHeadlessSystemUserMode

      public static void setHeadlessSystemUserMode(boolean isEnabled)
      Updates headless system user mode.
    • getUserRestrictions

      @Implementation(minSdk=33) protected Bundle getUserRestrictions()
    • hasUserRestrictionForUser

      @Implementation(minSdk=33) protected boolean hasUserRestrictionForUser(String restrictionKey, int userId)
    • reset

      @Resetter public static void reset()
    • someUserHasAccount

      @Implementation(minSdk=33) protected boolean someUserHasAccount(String accountName, String accountType)
    • setSomeUserHasAccount

      public void setSomeUserHasAccount(String accountName, String accountType)
    • removeSomeUserHasAccount

      public void removeSomeUserHasAccount(String accountName, String accountType)
      Removes user account set via setSomeUserHasAccount(String, String).
    • setUserForeground

      public void setUserForeground(boolean foreground)
      Sets whether or not the current user is the foreground user.
    • isUserForeground

      @Implementation(minSdk=31) protected boolean isUserForeground()
    • getUserRestrictionSources

      @Implementation(minSdk=26) protected List<UserManager.EnforcingUser> getUserRestrictionSources(String restriction, UserHandle userHandle)