Class ClientProfile
This class maintains a profile of the client including screen dimensions, device pixel ratio, timezone offset, IP address, user agent, and window size. It is used throughout the Oorian framework to make responsive layout decisions, handle timezone conversions, track client identity, and adapt the UI to different devices and browsers.
Features:
- Screen size tracking in both logical and physical pixels
- Window size monitoring with history for resize detection
- Device pixel ratio for high-DPI display support
- Screen orientation detection (portrait/landscape)
- Client IP address extraction (proxy-aware)
- User agent string parsing
- UTC timezone offset for date/time conversions
- WebBrowser instance management
Usage:
// Get the client profile for the current session
ClientProfile profile = ClientProfile.get();
// Check screen dimensions
int width = profile.getLogicalScreenWidth();
int height = profile.getLogicalScreenHeight();
Orientation orientation = profile.getScreenOrientation();
// Get client information
String ip = profile.getIpAddress();
int timezone = profile.getUtcOffset();
Day today = profile.getToday(); // Client's local date
// Check device capabilities
float pixelRatio = profile.getPixelRatio();
boolean isRetina = pixelRatio >= 2.0f;
// Detect browser
boolean isMobile = profile.userAgentContains("Mobile");
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ClientProfileget()Returns the ClientProfile for the current OorianSession.Returns the client's IP address.Returns the previous screen size before the last update.Returns the previous window size before the last update.intReturns the logical screen height in CSS pixels.intReturns the logical screen width in CSS pixels.intReturns the logical browser window height in CSS pixels.intReturns the logical browser window width in CSS pixels.Returns the device orientation.intReturns the physical screen height in device pixels.intReturns the physical screen width in device pixels.intReturns the physical browser window height in device pixels.intReturns the physical browser window width in device pixels.floatReturns the device pixel ratio.Calculates the screen orientation based on screen dimensions.Returns the current screen size.final DaygetToday()Returns the current day adjusted for the client's timezone.final longReturns the current time in milliseconds for the client's timezone.Returns the client's user agent string.intReturns the client's UTC timezone offset in minutes.Calculates the window orientation based on window dimensions.Returns the current browser window size.voidinitialize(OorianHttpRequest request) Initializes the client profile from the HTTP request.booleanChecks if the client profile has been initialized.voidsetScreenSize(int screenWidth, int screenHeight, float pixelRatio, Orientation orientation) Sets the client's screen size and display properties.voidsetUtcOffset(int utcOffset) Sets the client's UTC timezone offset in minutes.voidsetWindowSize(int winWidth, int winHeight) Sets the client's browser window size.booleanuserAgentContains(String keyword) Checks if the user agent string contains a specified keyword.
-
Constructor Details
-
ClientProfile
public ClientProfile()Constructs a new ClientProfile with default size values.Initializes all Size objects to default values. The profile must be initialized via
initialize(OorianHttpRequest)before it contains valid client data.
-
-
Method Details
-
get
Returns the ClientProfile for the current OorianSession.This is a convenience method that retrieves the client profile from the current session. It is equivalent to calling
OorianSession.get().getClientProfile().- Returns:
- the ClientProfile instance for the current session
- See Also:
-
initialize
Initializes the client profile from the HTTP request.This method should be called once when the session is first created. It extracts the client's IP address and user agent string from the request headers. Subsequent calls to this method have no effect.
- Parameters:
request- the HTTP servlet request containing client information
-
setUtcOffset
public void setUtcOffset(int utcOffset) Sets the client's UTC timezone offset in minutes.The offset is provided by the client's JavaScript (from
new Date().getTimezoneOffset()) and is negated because JavaScript returns a reversed offset (e.g., -300 for UTC-5).- Parameters:
utcOffset- the UTC offset in minutes from the client (will be negated for storage)
-
setScreenSize
public void setScreenSize(int screenWidth, int screenHeight, float pixelRatio, Orientation orientation) Sets the client's screen size and display properties.This method updates the screen dimensions, pixel ratio, and orientation. The previous screen size is saved to
lastScreenSizefor resize detection.- Parameters:
screenWidth- the screen width in logical (CSS) pixelsscreenHeight- the screen height in logical (CSS) pixelspixelRatio- the device pixel ratio (e.g., 1.0, 2.0 for Retina)orientation- the screen orientation (PORTRAIT or LANDSCAPE)
-
setWindowSize
public void setWindowSize(int winWidth, int winHeight) Sets the client's browser window size.This method updates the window dimensions. The previous window size is saved to
lastWindowSizefor resize detection.- Parameters:
winWidth- the window width in logical (CSS) pixelswinHeight- the window height in logical (CSS) pixels
-
isInitialized
public boolean isInitialized()Checks if the client profile has been initialized.- Returns:
trueif initialized viainitialize(OorianHttpRequest),falseotherwise
-
getIpAddress
Returns the client's IP address.The IP address is extracted from various proxy headers (X-Forwarded-For, etc.) if available, falling back to the direct remote address.
- Returns:
- the client's IP address as a string
-
getUtcOffset
public int getUtcOffset()Returns the client's UTC timezone offset in minutes.This offset can be used to convert UTC times to the client's local time. For example, UTC-5 (EST) would return -300 minutes.
- Returns:
- the UTC offset in minutes (negative for west of UTC, positive for east)
-
getToday
Returns the current day adjusted for the client's timezone.This method calculates what "today" is for the client based on their timezone offset, which may differ from the server's date.
- Returns:
- a
Dayobject representing today in the client's timezone
-
getTodayTime
public final long getTodayTime()Returns the current time in milliseconds for the client's timezone.This is a convenience method equivalent to
getToday().getTime().- Returns:
- the current time in milliseconds since epoch for the client's timezone
-
getUserAgent
Returns the client's user agent string.The user agent identifies the browser, version, and operating system. This can be used for browser detection and feature support checks.
- Returns:
- the user agent string from the HTTP request header
-
userAgentContains
Checks if the user agent string contains a specified keyword.This method performs a case-insensitive search for the keyword in the user agent. Useful for detecting mobile browsers, specific browser names, or platform identifiers.
- Parameters:
keyword- the keyword to search for (case-insensitive, e.g., "Mobile", "Chrome", "iPhone")- Returns:
trueif the user agent contains the keyword,falseotherwise
-
getPixelRatio
public float getPixelRatio()Returns the device pixel ratio.The pixel ratio indicates the relationship between physical pixels and logical (CSS) pixels. High-DPI displays like Retina screens typically have ratios of 2.0 or higher.
- Returns:
- the device pixel ratio (e.g., 1.0 for standard displays, 2.0 for Retina displays)
-
getOrientation
Returns the device orientation.This is the orientation reported by the client, which may be explicitly set or calculated from screen dimensions.
- Returns:
- the current screen orientation (PORTRAIT or LANDSCAPE)
-
getScreenOrientation
Calculates the screen orientation based on screen dimensions.Determines orientation by comparing width and height. LANDSCAPE is returned when width is greater than or equal to height, otherwise PORTRAIT.
- Returns:
Orientation.LANDSCAPEif width >= height,Orientation.PORTRAITotherwise
-
getWindowOrientation
Calculates the window orientation based on window dimensions.Determines orientation by comparing window width and height. LANDSCAPE is returned when width is greater than or equal to height, otherwise PORTRAIT.
- Returns:
Orientation.LANDSCAPEif width >= height,Orientation.PORTRAITotherwise
-
getScreenSize
Returns the current screen size.- Returns:
- the
Sizeobject containing screen width and height in logical pixels
-
getWindowSize
Returns the current browser window size.- Returns:
- the
Sizeobject containing window width and height in logical pixels
-
getLastScreenSize
Returns the previous screen size before the last update.This can be used to detect screen size changes and handle resize events.
- Returns:
- the
Sizeobject containing the previous screen dimensions
-
getLastWindowSize
Returns the previous window size before the last update.This can be used to detect window resize events and respond accordingly.
- Returns:
- the
Sizeobject containing the previous window dimensions
-
getLogicalScreenWidth
public int getLogicalScreenWidth()Returns the logical screen width in CSS pixels.Logical (CSS) pixels are device-independent and used for layout calculations.
- Returns:
- the screen width in logical pixels
-
getLogicalScreenHeight
public int getLogicalScreenHeight()Returns the logical screen height in CSS pixels.Logical (CSS) pixels are device-independent and used for layout calculations.
- Returns:
- the screen height in logical pixels
-
getPhysicalScreenWidth
public int getPhysicalScreenWidth()Returns the physical screen width in device pixels.Physical pixels represent the actual hardware pixels. This is calculated by multiplying the logical width by the pixel ratio.
- Returns:
- the screen width in physical device pixels
-
getPhysicalScreenHeight
public int getPhysicalScreenHeight()Returns the physical screen height in device pixels.Physical pixels represent the actual hardware pixels. This is calculated by multiplying the logical height by the pixel ratio.
- Returns:
- the screen height in physical device pixels
-
getLogicalWindowWidth
public int getLogicalWindowWidth()Returns the logical browser window width in CSS pixels.This is the viewport width available for content, excluding browser chrome.
- Returns:
- the window width in logical pixels
-
getLogicalWindowHeight
public int getLogicalWindowHeight()Returns the logical browser window height in CSS pixels.This is the viewport height available for content, excluding browser chrome.
- Returns:
- the window height in logical pixels
-
getPhysicalWindowWidth
public int getPhysicalWindowWidth()Returns the physical browser window width in device pixels.This is calculated by multiplying the logical window width by the pixel ratio.
- Returns:
- the window width in physical device pixels
-
getPhysicalWindowHeight
public int getPhysicalWindowHeight()Returns the physical browser window height in device pixels.This is calculated by multiplying the logical window height by the pixel ratio.
- Returns:
- the window height in physical device pixels
-