Skip to content

Commit

Permalink
Handle errors when fetching screen resolution in Subject (close #657)
Browse files Browse the repository at this point in the history
* Remove use of WindowManager (close #657)

* Catch exceptions

* Add test for default screen resolution in Subject

---------

Co-authored-by: Matúš Tomlein <[email protected]>
  • Loading branch information
mscwilson and matus-tomlein authored Jan 27, 2024
1 parent 394d907 commit 334ebbe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.test.platform.app.InstrumentationRegistry
import com.snowplowanalytics.core.tracker.Logger.updateLogLevel
import com.snowplowanalytics.core.tracker.Subject
import com.snowplowanalytics.snowplow.Snowplow.createTracker
import com.snowplowanalytics.snowplow.network.HttpMethod
import com.snowplowanalytics.snowplow.util.Size
import org.junit.Assert
import org.junit.Test
Expand Down Expand Up @@ -131,6 +130,14 @@ class SubjectTest {
Assert.assertNull(subject.getSubject(true)["ip"])
}

@Test
fun testSetsScreenResolutionAutomaticaly() {
val subject = createSubject()
Assert.assertNotNull(subject.screenResolution)
Assert.assertTrue((subject.screenResolution?.width ?: 0) > 0)
Assert.assertTrue((subject.screenResolution?.height ?: 0) > 0)
}

// Helper Methods
private fun createSubject(): Subject {
updateLogLevel(LogLevel.DEBUG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package com.snowplowanalytics.core.tracker

import android.content.Context
import android.graphics.Point
import android.content.res.Resources
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager
import com.snowplowanalytics.core.constants.Parameters
import com.snowplowanalytics.core.tracker.Logger.v
Expand Down Expand Up @@ -225,11 +227,25 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) {
* @param context the android context
*/
private fun setDefaultScreenResolution(context: Context) {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
val display = windowManager?.defaultDisplay
val size = Point()
display?.getSize(size)
screenResolution = (Size(size.x, size.y))
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) }
} else {
Resources.getSystem().displayMetrics
}
Size(metrics.widthPixels, metrics.heightPixels)
}
} catch (e: Exception) {
Logger.e(TAG, "Failed to set default screen resolution.")
}
}

/**
Expand Down

0 comments on commit 334ebbe

Please sign in to comment.