Skip to content

Commit

Permalink
Catch exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Jan 26, 2024
1 parent 1a873d7 commit 9bdcc7b
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
package com.snowplowanalytics.core.tracker

import android.content.Context
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
import com.snowplowanalytics.snowplow.util.Size
Expand Down Expand Up @@ -223,9 +227,25 @@ class Subject(context: Context, config: SubjectConfigurationInterface?) {
* @param context the android context
*/
private fun setDefaultScreenResolution(context: Context) {
val width = context.resources.displayMetrics.widthPixels
val height = context.resources.displayMetrics.heightPixels
screenResolution = (Size(width, height))
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 9bdcc7b

Please sign in to comment.