Class ShadowUserManager

    • Constructor Detail

      • ShadowUserManager

        public ShadowUserManager()
    • Method Detail

      • __constructor__

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

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

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

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

        public void enforcePermissionChecks​(boolean enforcePermissions)
      • 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(minSdk=18)
        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(minSdk=18)
        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(minSdk=18)
        protected Bundle getUserRestrictions​(UserHandle userHandle)
      • 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.
      • 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(minSdk=17)
        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(minSdk=17)
        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)
      • isSystemUser

        @Implementation(minSdk=23)
        protected boolean isSystemUser()
        Returns:
        'true' by default, or the value specified via setIsSystemUser(boolean)
      • isLinkedUser

        @Implementation(minSdk=18)
        protected boolean isLinkedUser()
        Returns:
        'false' by default, or the value specified via setIsLinkedUser(boolean)
      • 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().
      • setIsUserEnabled

        public void setIsUserEnabled​(int userId,
                                     boolean enabled)
      • 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()
      • getSeedAccountType

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

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

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

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

        @Implementation(minSdk=29)
        protected boolean removeUser​(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 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)