Class Idle

java.lang.Object
org.vaadin.addons.idle.Idle

public class Idle extends Object
Vaadin extension for tracking user activity / inactivity.

Track user activity/inactivity changes. There are two ways to react to user inactivity: Change the CSS styles or receive notification when user becomes inactive. Both can be used at the same time.

When user becomes inactive a style userinactive is application to BODY element. When user comes back active this style is replaced with classname useractive.

Furthermore, if listeners have been attached using addUserActiveListener(Idle.UserActiveListener) and addUserInactiveListener(Idle.UserInactiveListener), they will be invoked.

User activity is tracked by following the mouse and keyboard events on the browser window.

The inactivity timeout period can be specified using the setTimeout(long) method.

Author:
Sami Ekblad
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Basic Idle add-on event
    static class 
    User active event.
    static interface 
    Listener interface for user activity status changes to active.
    static class 
    User inactive event.
    static interface 
    Listener interface for user activity status changes to inactive.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Idle(com.vaadin.flow.component.UI ui)
    Creates a new Idle instance.
    Idle(com.vaadin.flow.component.UI ui, long timeoutMs)
    Creates a new Idle instance.
    Idle(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass)
    Creates a new Idle instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.vaadin.flow.shared.Registration
    Adds the user active listener.
    com.vaadin.flow.shared.Registration
    Adds the user inactive listener.
    protected void
    checkNotTracked(com.vaadin.flow.component.UI ui)
    Checks if the given UI is already monitored by Idle.
    protected void
    protected void
    static Idle
    get(com.vaadin.flow.component.UI ui)
    Gets the Idle instance a monitored UI.
    long
    User inactivity timeout in milliseconds.
    boolean
    Do we apply css class when user becomes active/inactive.
    protected void
    loadResources(com.vaadin.flow.component.UI ui)
    Load the necessary JS and CSS files.
    void
    setTimeout(long timeout)
    User inactivity timeout in milliseconds.
    void
    setUpdateCss(boolean updateCssClass)
    Apply css class to body when user becomes active/inactive.
    static Idle
    track(com.vaadin.flow.component.UI ui)
    Create new user activity tracker for UI with default timeout and no server-side listener.
    static Idle
    track(com.vaadin.flow.component.UI ui, long timeoutMs)
    Create new user activity tracker for UI with timeout.
    static Idle
    track(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass)
    Create new user activity tracker for UI with timeout.
    protected void
    trackUI(com.vaadin.flow.component.UI ui, long timeout)
    This is an internal method for tracking the UI.
    void
    Stop tracking the UI.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Idle

      public Idle(com.vaadin.flow.component.UI ui) throws IllegalArgumentException
      Creates a new Idle instance.
      Parameters:
      ui - UI instance to monitor
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
    • Idle

      public Idle(com.vaadin.flow.component.UI ui, long timeoutMs) throws IllegalArgumentException
      Creates a new Idle instance.
      Parameters:
      ui - UI instance to monitor
      timeoutMs - Inactivity timeout in milliseconds
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
    • Idle

      public Idle(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass) throws IllegalArgumentException
      Creates a new Idle instance.
      Parameters:
      ui - UI instance to monitor
      timeoutMs - Inactivity timeout in milliseconds
      updateCssClass - CSS styles are updated when user becomes active/inactive.
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
  • Method Details

    • loadResources

      protected void loadResources(com.vaadin.flow.component.UI ui)
      Load the necessary JS and CSS files. If you need to customize the resources, you can override this method.
    • get

      public static Idle get(com.vaadin.flow.component.UI ui)
      Gets the Idle instance a monitored UI.
      Parameters:
      ui - A monitored UI instance
      Returns:
      Idle instance of the UI or null, if the UI is not monitored.
    • track

      public static Idle track(com.vaadin.flow.component.UI ui) throws IllegalArgumentException
      Create new user activity tracker for UI with default timeout and no server-side listener.
      Parameters:
      ui - UI instance to monitor
      Returns:
      Idle Created instance
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
    • track

      public static Idle track(com.vaadin.flow.component.UI ui, long timeoutMs) throws IllegalArgumentException
      Create new user activity tracker for UI with timeout.
      Parameters:
      ui - UI instance to monitor
      timeoutMs - Inactivity timeout in milliseconds
      Returns:
      Idle Created instance
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
    • track

      public static Idle track(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass) throws IllegalArgumentException
      Create new user activity tracker for UI with timeout.
      Parameters:
      ui - UI instance to monitor
      timeoutMs - Inactivity timeout in milliseconds
      updateCssClass - CSS styles are updated when user becomes active/inactive.
      Returns:
      Idle Created instance
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
    • trackUI

      protected void trackUI(com.vaadin.flow.component.UI ui, long timeout)
      This is an internal method for tracking the UI.
      Parameters:
      ui - UI instance to track
      timeout - Inactivity timeout in milliseconds
    • checkNotTracked

      protected void checkNotTracked(com.vaadin.flow.component.UI ui) throws IllegalArgumentException
      Checks if the given UI is already monitored by Idle.
      Parameters:
      ui - UI instance to check
      Throws:
      IllegalArgumentException - If the UI is already monitored by Idle
    • getTimeout

      public long getTimeout()
      User inactivity timeout in milliseconds.
      Returns:
      Current timeout in milliseconds.
    • setTimeout

      public void setTimeout(long timeout)
      User inactivity timeout in milliseconds.

      This is the time that needs to pass before user is considered inactive. Default timeout is 5000ms (5 seconds).

      Parameters:
      timeout - New timeout.
    • isUpdateCss

      public boolean isUpdateCss()
      Do we apply css class when user becomes active/inactive.
      Returns:
      If true, the CSS styles are updated when user becomes active/inactive.
    • setUpdateCss

      public void setUpdateCss(boolean updateCssClass)
      Apply css class to body when user becomes active/inactive.
      Parameters:
      updateCssClass - If true, the CSS styles are updated when user becomes
    • fireUserActive

      protected void fireUserActive()
    • addUserActiveListener

      public com.vaadin.flow.shared.Registration addUserActiveListener(Idle.UserActiveListener listener)
      Adds the user active listener.
      Parameters:
      listener - the Listener to be added.
      Returns:
      A registration object for removing the listener.
      See Also:
      • Registration
    • fireUserInactive

      protected void fireUserInactive()
    • addUserInactiveListener

      public com.vaadin.flow.shared.Registration addUserInactiveListener(Idle.UserInactiveListener listener)
      Adds the user inactive listener.
      Parameters:
      listener - The Listener to be added.
      Returns:
      A registration object for removing the listener.
      See Also:
      • Registration
    • untrackUI

      public void untrackUI()
      Stop tracking the UI.