Class ExtendedClientDetails

java.lang.Object
com.vaadin.flow.component.page.ExtendedClientDetails
All Implemented Interfaces:
Serializable

public class ExtendedClientDetails extends Object implements Serializable
Provides extended information about the web browser, such as screen resolution and time zone.

Browser details are automatically fetched on the first call to Page.getExtendedClientDetails() and cached for the lifetime of the UI. The fetch happens asynchronously, so the first call may return null while the data is being retrieved. To update the cached values with fresh data from the browser, use refresh(SerializableConsumer)}.

Since:
2.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • ExtendedClientDetails

      public ExtendedClientDetails(UI ui, String screenWidth, String screenHeight, String windowInnerWidth, String windowInnerHeight, String bodyClientWidth, String bodyClientHeight, String tzOffset, String rawTzOffset, String dstShift, String dstInEffect, String tzId, String curDate, String touchDevice, String devicePixelRatio, String windowName, String navigatorPlatform, String colorScheme, String themeName)
      For internal use only. Updates all properties in the class according to the given information.
      Parameters:
      ui - the UI instance that owns this ExtendedClientDetails
      screenWidth - Screen width
      screenHeight - Screen height
      windowInnerWidth - Window width
      windowInnerHeight - Window height
      bodyClientWidth - Body element width
      bodyClientHeight - Body element height
      tzOffset - TimeZone offset in minutes from GMT
      rawTzOffset - raw TimeZone offset in minutes from GMT (w/o DST adjustment)
      dstShift - the difference between the raw TimeZone and DST in minutes
      dstInEffect - is DST currently active in the region or not?
      tzId - time zone id
      curDate - the current date in milliseconds since the epoch
      touchDevice - whether browser responds to touch events
      devicePixelRatio - the ratio of the display's resolution in physical pixels to the resolution in CSS pixels
      windowName - a unique browser window name which persists on reload
      navigatorPlatform - navigation platform received from the browser
      colorScheme - the current color scheme
      themeName - the theme name (e.g., "lumo", "aura")
  • Method Details

    • getScreenWidth

      public int getScreenWidth()
      Gets the width of the screen in pixels. This is the full screen resolution and not the width available for the application.
      Returns:
      the width of the screen in pixels.
    • getScreenHeight

      public int getScreenHeight()
      Gets the height of the screen in pixels. This is the full screen resolution and not the height available for the application.
      Returns:
      the height of the screen in pixels.
    • getWindowInnerHeight

      public int getWindowInnerHeight()
      Gets the inner height of the browser window window.innerHeight in pixels. This includes the scrollbar, if it is visible.

      Delegates to the window size signal on the Page. Returns -1 if no UI is available.

      Returns:
      the browser window inner height in pixels, or -1
    • getWindowInnerWidth

      public int getWindowInnerWidth()
      Gets the inner width of the browser window window.innerWidth in pixels. This includes the scrollbar, if it is visible.

      Delegates to the window size signal on the Page. Returns -1 if no UI is available.

      Returns:
      the browser window inner width in pixels, or -1
    • getBodyClientHeight

      public int getBodyClientHeight()
      Gets the height of the body element in the document in pixels.
      Returns:
      the height of the body element
    • getBodyClientWidth

      public int getBodyClientWidth()
      Gets the width of the body element in the document in pixels.
      Returns:
      the width of the body element
    • getTimezoneOffset

      public int getTimezoneOffset()
      Returns the browser-reported TimeZone offset in milliseconds from GMT. This includes possible daylight saving adjustments, to figure out which TimeZone the user actually might be in, see getRawTimezoneOffset().
      Returns:
      timezone offset in milliseconds, 0 if not available
      See Also:
    • getTimeZoneId

      public String getTimeZoneId()
      Returns the TimeZone Id (like "Europe/Helsinki") provided by the browser (if the browser supports this feature).
      Returns:
      the TimeZone Id if provided by the browser, null otherwise.
      See Also:
    • getRawTimezoneOffset

      public int getRawTimezoneOffset()
      Returns the browser-reported TimeZone offset in milliseconds from GMT ignoring possible daylight saving adjustments that may be in effect in the browser.

      You can use this to figure out which TimeZones the user could actually be in by calling TimeZone.getAvailableIDs(int).

      If getRawTimezoneOffset() and getTimezoneOffset() returns the same value, the browser is either in a zone that does not currently have daylight saving time, or in a zone that never has daylight saving time.

      Returns:
      timezone offset in milliseconds excluding DST, 0 if not available
    • getDSTSavings

      public int getDSTSavings()
      Returns the offset in milliseconds between the browser's GMT TimeZone and DST.
      Returns:
      the number of milliseconds that the TimeZone shifts when DST is in effect
    • isDSTInEffect

      public boolean isDSTInEffect()
      Returns whether daylight saving time (DST) is currently in effect in the region of the browser or not.
      Returns:
      true if the browser resides at a location that currently is in DST
    • getCurrentDate

      public Date getCurrentDate()
      Returns the current date and time of the browser. This will not be entirely accurate due to varying network latencies, but should provide a close-enough value for most cases. Also note that the returned Date object uses servers default time zone, not the clients.

      To get the actual date and time shown in the end users computer, you can do something like:

       WebBrowser browser = ...;
       SimpleTimeZone timeZone = new SimpleTimeZone(browser.getTimezoneOffset(), "Fake client time zone");
       DateFormat format = DateFormat.getDateTimeInstance();
       format.setTimeZone(timeZone);
       myLabel.setValue(format.format(browser.getCurrentDate()));
       
      Returns:
      the current date and time of the browser.
      See Also:
    • isTouchDevice

      public boolean isTouchDevice()
      Checks if the browser supports touch events.
      Returns:
      true if the browser is detected to support touch events, false otherwise
    • getDevicePixelRatio

      public double getDevicePixelRatio()
      Gets the device pixel ratio, window.devicePixelRatio. See more from MDN web docs.

      A value of -1 indicates that the value was not reported by the browser correctly.

      Returns:
      double-precision floating-point value indicating the ratio of the display's resolution in physical pixels to the resolution in CSS pixels
    • getWindowName

      public String getWindowName()
      Returns a unique browser window identifier. For internal use only.
      Returns:
      An id which persists if the UI is reloaded in the same browser window/tab.
    • isIPad

      public boolean isIPad()
      Check if the browser is run on IPad.
      Returns:
      true if run on IPad false if the user is not using IPad or if no information from the browser is present
    • isIOS

      public boolean isIOS()
      Check if the browser is run on IOS.
      Returns:
      true if run on IOS , false if the user is not using IOS or if no information from the browser is present
    • getColorScheme

      public ColorScheme.Value getColorScheme()
      Gets the color scheme.
      Returns:
      the color scheme, never null
    • getThemeName

      public String getThemeName()
      Gets the theme name.
      Returns:
      the theme name (e.g., "lumo", "aura"), or empty string if not detected
    • fromJson

      public static ExtendedClientDetails fromJson(UI ui, tools.jackson.databind.JsonNode json)
      Creates an ExtendedClientDetails instance from browser details JSON object. This is intended for internal use when browser details are provided as JSON (e.g., during UI initialization or refresh).

      For internal use only.

      Parameters:
      ui - the UI instance that owns this ExtendedClientDetails
      json - the JSON object containing browser details parameters
      Returns:
      a new ExtendedClientDetails instance
      Throws:
      RuntimeException - if the JSON is not a valid object
    • refresh

      public void refresh(SerializableConsumer<ExtendedClientDetails> callback)
      Refreshes the browser details by fetching updated values from the browser. The refresh happens asynchronously. The cached values in this instance will be updated when the browser responds, and then the provided callback will be invoked with the updated details.
      Parameters:
      callback - a callback that will be invoked with the updated ExtendedClientDetails when the refresh is complete