Class ScreenApi


public class ScreenApi extends JavaScriptApi
Provides access to screen and window information from server-side Java code.

This class wraps the browser's Screen API and provides information about the client's display, including screen dimensions, window size, pixel ratio, and orientation.

Screen information is collected from the client and stored in the session's ClientProfile. This class provides convenient static accessors to that information.

Dimension Types:

  • Logical pixels - CSS pixels, device-independent units used for layout
  • Physical pixels - Actual hardware pixels (logical * pixelRatio)

Usage:


 // Get screen dimensions
 int screenWidth = ScreenApi.getScreenWidth();
 int screenHeight = ScreenApi.getScreenHeight();

 // Get window (viewport) dimensions
 int windowWidth = ScreenApi.getWindowWidth();
 int windowHeight = ScreenApi.getWindowHeight();

 // Check device capabilities
 float pixelRatio = ScreenApi.getPixelRatio();
 boolean isRetina = ScreenApi.isHighDpi();

 // Check orientation
 if (ScreenApi.isPortrait()) {
     // Mobile portrait layout
 }

 // Responsive breakpoints
 if (ScreenApi.isMobile()) {
     // Mobile layout
 } else if (ScreenApi.isTablet()) {
     // Tablet layout
 }
 
Author:
Marvin P. Warble Jr.
See Also:
  • Field Details

    • MOBILE_BREAKPOINT

      public static final int MOBILE_BREAKPOINT
      Breakpoint for mobile devices (max width in logical pixels).
      See Also:
    • TABLET_BREAKPOINT

      public static final int TABLET_BREAKPOINT
      Breakpoint for tablet devices (max width in logical pixels).
      See Also:
  • Constructor Details

    • ScreenApi

      public ScreenApi()
  • Method Details

    • getScreenWidth

      public static int getScreenWidth()
      Returns the screen width in logical (CSS) pixels.
      Returns:
      The screen width in logical pixels.
    • getScreenHeight

      public static int getScreenHeight()
      Returns the screen height in logical (CSS) pixels.
      Returns:
      The screen height in logical pixels.
    • getScreenSize

      public static Size getScreenSize()
      Returns the screen size in logical (CSS) pixels.
      Returns:
      The Size object containing screen width and height.
    • getPhysicalScreenWidth

      public static int getPhysicalScreenWidth()
      Returns the screen width in physical (device) pixels.
      Returns:
      The screen width in physical pixels.
    • getPhysicalScreenHeight

      public static int getPhysicalScreenHeight()
      Returns the screen height in physical (device) pixels.
      Returns:
      The screen height in physical pixels.
    • getWindowWidth

      public static int getWindowWidth()
      Returns the browser window (viewport) width in logical (CSS) pixels.
      Returns:
      The window width in logical pixels.
    • getWindowHeight

      public static int getWindowHeight()
      Returns the browser window (viewport) height in logical (CSS) pixels.
      Returns:
      The window height in logical pixels.
    • getWindowSize

      public static Size getWindowSize()
      Returns the browser window size in logical (CSS) pixels.
      Returns:
      The Size object containing window width and height.
    • getPhysicalWindowWidth

      public static int getPhysicalWindowWidth()
      Returns the browser window width in physical (device) pixels.
      Returns:
      The window width in physical pixels.
    • getPhysicalWindowHeight

      public static int getPhysicalWindowHeight()
      Returns the browser window height in physical (device) pixels.
      Returns:
      The window height in physical pixels.
    • getPixelRatio

      public static float getPixelRatio()
      Returns the device pixel ratio.

      The pixel ratio indicates the relationship between physical pixels and logical (CSS) pixels. Standard displays have a ratio of 1.0, while high-DPI displays like Retina screens have ratios of 2.0 or higher.

      Returns:
      The device pixel ratio.
    • isHighDpi

      public static boolean isHighDpi()
      Checks if the device has a high-DPI (Retina) display.
      Returns:
      true if the pixel ratio is 1.5 or higher, false otherwise.
    • getOrientation

      public static Orientation getOrientation()
      Returns the current screen orientation.
      Returns:
      The screen orientation (PORTRAIT or LANDSCAPE).
    • getWindowOrientation

      public static Orientation getWindowOrientation()
      Returns the current window orientation.
      Returns:
      The window orientation (PORTRAIT or LANDSCAPE).
    • isPortrait

      public static boolean isPortrait()
      Checks if the screen is in portrait orientation.
      Returns:
      true if the screen is taller than it is wide, false otherwise.
    • isLandscape

      public static boolean isLandscape()
      Checks if the screen is in landscape orientation.
      Returns:
      true if the screen is wider than it is tall, false otherwise.
    • isMobile

      public static boolean isMobile()
      Checks if the window width indicates a mobile device.
      Returns:
      true if the window width is less than or equal to the mobile breakpoint.
    • isTablet

      public static boolean isTablet()
      Checks if the window width indicates a tablet device.
      Returns:
      true if the window width is between mobile and tablet breakpoints.
    • isDesktop

      public static boolean isDesktop()
      Checks if the window width indicates a desktop device.
      Returns:
      true if the window width is greater than the tablet breakpoint.
    • getLastScreenSize

      public static Size getLastScreenSize()
      Returns the previous screen size before the last update.

      This can be used to detect screen size changes.

      Returns:
      The Size object containing the previous screen dimensions.
    • getLastWindowSize

      public static Size getLastWindowSize()
      Returns the previous window size before the last update.

      This can be used to detect window resize events.

      Returns:
      The Size object containing the previous window dimensions.
    • getUserAgent

      public static String getUserAgent()
      Returns the client's user agent string.
      Returns:
      The user agent string from the browser.
    • userAgentContains

      public static boolean userAgentContains(String keyword)
      Checks if the user agent contains a specified keyword.
      Parameters:
      keyword - The keyword to search for (case-insensitive).
      Returns:
      true if the user agent contains the keyword, false otherwise.
    • getUtcOffset

      public static int getUtcOffset()
      Returns the client's UTC timezone offset in minutes.
      Returns:
      The UTC offset in minutes (negative for west of UTC, positive for east).
    • getIpAddress

      public static String getIpAddress()
      Returns the client's IP address.
      Returns:
      The client's IP address as a string.