Class ShadowUserManager


  • @Implements(value=android.os.UserManager.class,
                minSdk=17)
    public class ShadowUserManager
    extends java.lang.Object
    Robolectric implementation of UserManager.
    • 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
      • userPidMap

        protected static java.util.Map<java.lang.Integer,​java.lang.Integer> userPidMap
      • userSerialNumbers

        protected com.google.common.collect.BiMap<java.lang.Integer,​java.lang.Long> userSerialNumbers
        Holds the serial numbers for all users and profiles, indexed by UserHandle.id
      • userState

        protected java.util.Map<java.lang.Integer,​ShadowUserManager.UserState> userState
        Holds all UserStates, indexed by UserHandle.id
      • userInfoMap

        protected java.util.Map<java.lang.Integer,​android.content.pm.UserInfo> userInfoMap
        Holds the UserInfo for all registered users and profiles, indexed by UserHandle.id
      • userProfilesListMap

        protected java.util.Map<java.lang.Integer,​java.util.List<android.os.UserHandle>> userProfilesListMap
        Each user holds a list of UserHandles of assocated profiles and user itself. User is indexed by UserHandle.id. See UserManager.getProfiles(userId).
    • Constructor Detail

      • ShadowUserManager

        public ShadowUserManager()
    • Method Detail

      • __constructor__

        @Implementation
        protected void __constructor__​(android.content.Context context,
                                       android.os.IUserManager service)
      • getApplicationRestrictions

        @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.
        See Also:
        setApplicationRestrictions(String, Bundle)
      • setApplicationRestrictions

        public void setApplicationRestrictions​(java.lang.String packageName,
                                               android.os.Bundle restrictions)
        Sets the value returned by UserManager.getApplicationRestrictions(String).
      • addUserProfile

        @Deprecated
        public long addUserProfile​(android.os.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 java.util.List<android.os.UserHandle> getUserProfiles()
      • getProfiles

        @Implementation(minSdk=21)
        protected java.util.List<android.content.pm.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.
      • getProfileParent

        @Implementation(minSdk=21)
        protected android.content.pm.UserInfo getProfileParent​(int userId)
      • addProfile

        public void addProfile​(int userHandle,
                               int profileUserHandle,
                               java.lang.String profileName,
                               int profileFlags)
        Add a profile to be returned by getProfiles(int).
      • setUserUnlocked

        public void setUserUnlocked​(boolean userUnlocked)
        Setter for UserManager.isUserUnlocked()
      • isUserUnlocked

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

        public void enforcePermissionChecks​(boolean enforcePermissions)
      • setManagedProfile

        public void setManagedProfile​(boolean managedProfile)
        Setter for UserManager.isManagedProfile().
      • hasUserRestriction

        @Implementation(minSdk=21)
        protected boolean hasUserRestriction​(java.lang.String restrictionKey,
                                             android.os.UserHandle userHandle)
      • setUserRestriction

        public void setUserRestriction​(android.os.UserHandle userHandle,
                                       java.lang.String restrictionKey,
                                       boolean value)
      • clearUserRestrictions

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

        @Implementation(minSdk=18)
        protected android.os.Bundle getUserRestrictions​(android.os.UserHandle userHandle)
      • setSerialNumberForUser

        public void setSerialNumberForUser​(android.os.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.
      • getUserName

        @Implementation(minSdk=29)
        protected java.lang.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.
      • getUserHandle

        @HiddenApi
        @Implementation(minSdk=17)
        protected int getUserHandle​(int serialNumber)
        Returns:
        user id for given user serial number.
      • 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(minSdk=18)
        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

        public void setIsRestrictedProfile​(boolean isRestrictedProfile)
        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().
      • setUserState

        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)
      • isQuietModeEnabled

        @Implementation(minSdk=26)
        protected boolean isQuietModeEnabled​(android.os.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,
                                                  android.os.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​(java.lang.String action,
                                              android.os.UserHandle profileHandle)
        If the current application has the necessary rights, it will receive the background action too.
      • setProfileIsLocked

        public void setProfileIsLocked​(android.os.UserHandle profileHandle,
                                       boolean isLocked)
      • getUsers

        @Implementation
        protected java.util.List<android.content.pm.UserInfo> getUsers()
      • getUserInfo

        @Implementation
        protected android.content.pm.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()
      • getSeedAccountType

        @Implementation(minSdk=29)
        protected java.lang.String getSeedAccountType()
      • setSeedAccountType

        public void setSeedAccountType​(java.lang.String seedAccountType)
        Setter for UserManager.getSeedAccountType()
      • removeUser

        @Implementation(minSdk=17)
        protected boolean removeUser​(int userHandle)
      • removeUser

        @Implementation(minSdk=29)
        protected boolean removeUser​(android.os.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 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 android.os.UserHandle addUser​(int id,
                                             java.lang.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.
      • reset

        @Resetter
        public static void reset()