From 719ddb3ef84a5f8deecdb8a0e8c8ef0f11d50eae Mon Sep 17 00:00:00 2001 From: Miranda Wilson Date: Fri, 12 Apr 2024 16:32:02 +0100 Subject: [PATCH 1/2] Add SubjectConfig option --- .../snowplowanalytics/core/tracker/Subject.kt | 66 ++++++++++++------- .../tracker/SubjectConfigurationInterface.kt | 1 + .../core/tracker/SubjectControllerImpl.kt | 7 ++ .../configuration/SubjectConfiguration.kt | 17 +++++ 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt index 92c33e9c6..b2fc304c6 100755 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt @@ -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 } @@ -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.") } @@ -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.") } } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt index 21a6073b2..bb630cc81 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt @@ -25,4 +25,5 @@ interface SubjectConfigurationInterface { var screenResolution: Size? var screenViewPort: Size? var colorDepth: Int? + var useContextResourcesScreenResolution: Boolean? } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt index ff54153d4..14f042012 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt @@ -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() diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt index 617d4b4e4..56568cd1d 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt @@ -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 @@ -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() @@ -181,6 +197,7 @@ open class SubjectConfiguration() : Configuration, SubjectConfigurationInterface .screenResolution(screenResolution) .screenViewPort(screenViewPort) .colorDepth(colorDepth) + .useContextResourcesScreenResolution(useContextResourcesScreenResolution) } // JSON Formatter From 9ad9ef833fbfc82c084d85e9461e8cc7fa59bc50 Mon Sep 17 00:00:00 2001 From: Miranda Wilson Date: Mon, 15 Apr 2024 11:04:35 +0100 Subject: [PATCH 2/2] Make flag non-optional --- .../snowplowanalytics/core/tracker/Subject.kt | 21 +++++-------------- .../tracker/SubjectConfigurationInterface.kt | 2 +- .../core/tracker/SubjectControllerImpl.kt | 2 +- .../configuration/SubjectConfiguration.kt | 6 +++--- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt index b2fc304c6..8af9aca43 100755 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/Subject.kt @@ -180,22 +180,12 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) { 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 - } + /** + * Whether to get the size from the context resources or not. + * By default this is false, the size is obtained from WindowManager. + */ + var useContextResourcesScreenResolution: Boolean = false - init { setDefaultTimezone() setDefaultLanguage() @@ -212,7 +202,6 @@ 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.") } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt index bb630cc81..d416d5c68 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectConfigurationInterface.kt @@ -25,5 +25,5 @@ interface SubjectConfigurationInterface { var screenResolution: Size? var screenViewPort: Size? var colorDepth: Int? - var useContextResourcesScreenResolution: Boolean? + var useContextResourcesScreenResolution: Boolean } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt index 14f042012..bf77e9b46 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/SubjectControllerImpl.kt @@ -92,7 +92,7 @@ class SubjectControllerImpl // Constructors subject.colorDepth = colorDepth } - override var useContextResourcesScreenResolution: Boolean? + override var useContextResourcesScreenResolution: Boolean get() = subject.useContextResourcesScreenResolution set(useContextResourcesScreenResolution) { dirtyConfig.useContextResourcesScreenResolution = useContextResourcesScreenResolution diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt index 56568cd1d..cf1c66999 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/SubjectConfiguration.kt @@ -78,8 +78,8 @@ open class SubjectConfiguration() : Configuration, SubjectConfigurationInterface set(value) { _colorDepth = value } private var _useContextResourcesScreenResolution: Boolean? = null - override var useContextResourcesScreenResolution: Boolean? - get() = _useContextResourcesScreenResolution ?: sourceConfig?.useContextResourcesScreenResolution + override var useContextResourcesScreenResolution: Boolean + get() = _useContextResourcesScreenResolution ?: sourceConfig?.useContextResourcesScreenResolution ?: false set(value) { _useContextResourcesScreenResolution = value } // Builder methods @@ -179,7 +179,7 @@ open class SubjectConfiguration() : Configuration, SubjectConfigurationInterface * 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 { + fun useContextResourcesScreenResolution(useContextResourcesScreenResolution: Boolean): SubjectConfiguration { this.useContextResourcesScreenResolution = useContextResourcesScreenResolution return this }