Package com.oorian.html.js
Class ScreenApi
java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.ScreenApi
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intBreakpoint for mobile devices (max width in logical pixels).static final intBreakpoint for tablet devices (max width in logical pixels). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringReturns the client's IP address.static SizeReturns the previous screen size before the last update.static SizeReturns the previous window size before the last update.static OrientationReturns the current screen orientation.static intReturns the screen height in physical (device) pixels.static intReturns the screen width in physical (device) pixels.static intReturns the browser window height in physical (device) pixels.static intReturns the browser window width in physical (device) pixels.static floatReturns the device pixel ratio.static intReturns the screen height in logical (CSS) pixels.static SizeReturns the screen size in logical (CSS) pixels.static intReturns the screen width in logical (CSS) pixels.static StringReturns the client's user agent string.static intReturns the client's UTC timezone offset in minutes.static intReturns the browser window (viewport) height in logical (CSS) pixels.static OrientationReturns the current window orientation.static SizeReturns the browser window size in logical (CSS) pixels.static intReturns the browser window (viewport) width in logical (CSS) pixels.static booleanChecks if the window width indicates a desktop device.static booleanChecks if the device has a high-DPI (Retina) display.static booleanChecks if the screen is in landscape orientation.static booleanisMobile()Checks if the window width indicates a mobile device.static booleanChecks if the screen is in portrait orientation.static booleanisTablet()Checks if the window width indicates a tablet device.static booleanuserAgentContains(String keyword) Checks if the user agent contains a specified keyword.
-
Field Details
-
MOBILE_BREAKPOINT
public static final int MOBILE_BREAKPOINTBreakpoint for mobile devices (max width in logical pixels).- See Also:
-
TABLET_BREAKPOINT
public static final int TABLET_BREAKPOINTBreakpoint 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
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
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:
trueif the pixel ratio is 1.5 or higher,falseotherwise.
-
getOrientation
Returns the current screen orientation.- Returns:
- The screen orientation (PORTRAIT or LANDSCAPE).
-
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:
trueif the screen is taller than it is wide,falseotherwise.
-
isLandscape
public static boolean isLandscape()Checks if the screen is in landscape orientation.- Returns:
trueif the screen is wider than it is tall,falseotherwise.
-
isMobile
public static boolean isMobile()Checks if the window width indicates a mobile device.- Returns:
trueif 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:
trueif the window width is between mobile and tablet breakpoints.
-
isDesktop
public static boolean isDesktop()Checks if the window width indicates a desktop device.- Returns:
trueif the window width is greater than the tablet breakpoint.
-
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
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
Returns the client's user agent string.- Returns:
- The user agent string from the browser.
-
userAgentContains
Checks if the user agent contains a specified keyword.- Parameters:
keyword- The keyword to search for (case-insensitive).- Returns:
trueif the user agent contains the keyword,falseotherwise.
-
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
Returns the client's IP address.- Returns:
- The client's IP address as a string.
-