Skip to content

Commit

Permalink
Add SubjectConfig option
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Apr 17, 2024
1 parent 55f5656 commit 719ddb3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,28 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) {
field = depth
standardPairs[Parameters.COLOR_DEPTH] = depth.toString()
}

var useContextResourcesScreenResolution: Boolean? = false
/**
* Whether to get the size from the context resources or not.
* By default this is false, the size is obtained from WindowManager.
*
* @param useContextResourcesScreenResolution
*/
set(useContextResourcesScreenResolution) {
if (useContextResourcesScreenResolution == null) {
return
}

field = useContextResourcesScreenResolution
}


init {
setDefaultTimezone()
setDefaultLanguage()
setDefaultScreenResolution(context)
setDefaultScreenResolution(context, config?.useContextResourcesScreenResolution)

if (config != null) {
config.userId?.let { userId = it }
config.networkUserId?.let { networkUserId = it }
Expand All @@ -197,6 +212,7 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) {
config.screenResolution?.let { screenResolution = it }
config.screenViewPort?.let { screenViewPort = it }
config.colorDepth?.let { colorDepth = it }
config.useContextResourcesScreenResolution?.let { useContextResourcesScreenResolution = it }
}
v(TAG, "Subject created successfully.")
}
Expand All @@ -220,31 +236,35 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) {
}

/**
* Sets the default screen resolution
* of the device the Tracker is running
* on.
*
* @param context the android context
* Sets the default screen resolution of the device the tracker is running on.
* @param context the Android context
* @param useContextResourcesScreenResolution whether to get the size from the context resources or not
*/
private fun setDefaultScreenResolution(context: Context) {
try {
screenResolution = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val metrics =
context.getSystemService(WindowManager::class.java).currentWindowMetrics
Size(metrics.bounds.width(), metrics.bounds.height())
} else {
val windowManager =
context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
val display = windowManager?.defaultDisplay
val metrics = if (display != null) {
DisplayMetrics().also { display.getRealMetrics(it) }
private fun setDefaultScreenResolution(context: Context, useContextResourcesScreenResolution: Boolean?) {
if (useContextResourcesScreenResolution == true) {
val width = context.resources.displayMetrics.widthPixels
val height = context.resources.displayMetrics.heightPixels
screenResolution = Size(width, height)
} else {
try {
screenResolution = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val metrics =
context.getSystemService(WindowManager::class.java).currentWindowMetrics
Size(metrics.bounds.width(), metrics.bounds.height())
} else {
Resources.getSystem().displayMetrics
val windowManager =
context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
val display = windowManager?.defaultDisplay
val metrics = if (display != null) {
DisplayMetrics().also { display.getRealMetrics(it) }
} else {
Resources.getSystem().displayMetrics
}
Size(metrics.widthPixels, metrics.heightPixels)
}
Size(metrics.widthPixels, metrics.heightPixels)
} catch (e: Throwable) {
Logger.e(TAG, "Failed to set default screen resolution.")
}
} catch (e: Exception) {
Logger.e(TAG, "Failed to set default screen resolution.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ interface SubjectConfigurationInterface {
var screenResolution: Size?
var screenViewPort: Size?
var colorDepth: Int?
var useContextResourcesScreenResolution: Boolean?
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class SubjectControllerImpl // Constructors
subject.colorDepth = colorDepth
}

override var useContextResourcesScreenResolution: Boolean?
get() = subject.useContextResourcesScreenResolution
set(useContextResourcesScreenResolution) {
dirtyConfig.useContextResourcesScreenResolution = useContextResourcesScreenResolution
subject.useContextResourcesScreenResolution = useContextResourcesScreenResolution
}

// Private methods
private val subject: Subject
get() = serviceProvider.getOrMakeSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ open class SubjectConfiguration() : Configuration, SubjectConfigurationInterface
override var colorDepth: Int?
get() = _colorDepth ?: sourceConfig?.colorDepth
set(value) { _colorDepth = value }

private var _useContextResourcesScreenResolution: Boolean? = null
override var useContextResourcesScreenResolution: Boolean?
get() = _useContextResourcesScreenResolution ?: sourceConfig?.useContextResourcesScreenResolution
set(value) { _useContextResourcesScreenResolution = value }

// Builder methods

Expand Down Expand Up @@ -168,6 +173,17 @@ open class SubjectConfiguration() : Configuration, SubjectConfigurationInterface
return this
}

/**
* Set this flag to true to define the default screen resolution (at tracker initialization)
* based on the context's Resources display metrics, rather than the deprecated WindowManager.
* NB: the height value will be smaller using Resources as it doesn't include the menu bar.
* Defaults to false.
*/
fun useContextResourcesScreenResolution(useContextResourcesScreenResolution: Boolean?): SubjectConfiguration {
this.useContextResourcesScreenResolution = useContextResourcesScreenResolution
return this
}

// Copyable
override fun copy(): SubjectConfiguration {
return SubjectConfiguration()
Expand All @@ -181,6 +197,7 @@ open class SubjectConfiguration() : Configuration, SubjectConfigurationInterface
.screenResolution(screenResolution)
.screenViewPort(screenViewPort)
.colorDepth(colorDepth)
.useContextResourcesScreenResolution(useContextResourcesScreenResolution)
}

// JSON Formatter
Expand Down

0 comments on commit 719ddb3

Please sign in to comment.