Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subject config flag to avoid WindowManager bug #683

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,18 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) {
field = depth
standardPairs[Parameters.COLOR_DEPTH] = depth.toString()
}


/**
* 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()
setDefaultScreenResolution(context)
setDefaultScreenResolution(context, config?.useContextResourcesScreenResolution)

if (config != null) {
config.userId?.let { userId = it }
config.networkUserId?.let { networkUserId = it }
Expand Down Expand Up @@ -220,31 +225,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 ?: false
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
Loading