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