diff --git a/README.md b/README.md index 26e6f7e..cd02cc0 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,8 @@ versions of Android. If unsupported, the corresponding key is omitted. | Key | Value | Notes | |-|-|-| | `screenOrientation`| portrait, landscape, unknown | +| `screenWidth` | String | Screen width in density-independent pixels. Beware: this value is different when there is a different screen orientation. | +| `screenHeight` | String | Screen height in density-independent pixels. Beware: this value is different when there is a different screen orientation. | ### System diff --git a/q42stats/src/main/java/com/q42/q42stats/library/Q42Stats.kt b/q42stats/src/main/java/com/q42/q42stats/library/Q42Stats.kt index ccbed37..1209191 100644 --- a/q42stats/src/main/java/com/q42/q42stats/library/Q42Stats.kt +++ b/q42stats/src/main/java/com/q42/q42stats/library/Q42Stats.kt @@ -19,7 +19,7 @@ internal const val TAG = "Q42Stats" * Version code for the data format that is sent to the server. Increment by 1 every time * you add / remove / change a field in any of the Collector classes */ -internal const val DATA_MODEL_VERSION = 4 +internal const val DATA_MODEL_VERSION = 5 class Q42Stats(private val config: Q42StatsConfig) { diff --git a/q42stats/src/main/java/com/q42/q42stats/library/collector/AccessibilityCollector.kt b/q42stats/src/main/java/com/q42/q42stats/library/collector/AccessibilityCollector.kt index 4c2e475..15fc6ef 100644 --- a/q42stats/src/main/java/com/q42/q42stats/library/collector/AccessibilityCollector.kt +++ b/q42stats/src/main/java/com/q42/q42stats/library/collector/AccessibilityCollector.kt @@ -6,9 +6,11 @@ import android.content.Context.ACCESSIBILITY_SERVICE import android.content.Context.CAPTIONING_SERVICE import android.content.res.Configuration.ORIENTATION_LANDSCAPE import android.content.res.Configuration.ORIENTATION_PORTRAIT +import android.graphics.Point import android.os.Build import android.provider.Settings import android.util.DisplayMetrics +import android.view.WindowManager import android.view.accessibility.AccessibilityManager import android.view.accessibility.CaptioningManager import androidx.annotation.RequiresApi @@ -82,7 +84,7 @@ internal object AccessibilityCollector { ) } } - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { put( "isAnimationsDisabled", isAnimationsDisabled(context) @@ -101,6 +103,16 @@ internal object AccessibilityCollector { } }) + getScreenSize(context)?.let { + put( + "screenWidth", + it.first + ) + put( + "screenHeight", + it.second + ) + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getSystemIntAsBool( @@ -146,7 +158,7 @@ internal object AccessibilityCollector { */ @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1) private fun isMagnificationEnabled(context: Context, serviceNames: List): Boolean? = try { - val isMagnificationByTripleTapGesturesEnabled = getSystemIntAsBool(context,"accessibility_display_magnification_enabled") ?: false + val isMagnificationByTripleTapGesturesEnabled = getSystemIntAsBool(context, "accessibility_display_magnification_enabled") ?: false val isMagnificationByVolumeButtonsEnabled = serviceNames.map { s -> s.lowercase() }.contains("com.example.android.apis.accessibility.magnificationservice") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { @@ -154,11 +166,11 @@ internal object AccessibilityCollector { Settings.Secure.getString(context.contentResolver, "accessibility_button_targets").lowercase().contains("com.android.server.accessibility.magnificationcontroller") isMagnificationByTripleTapGesturesEnabled || isMagnificationByVolumeButtonsEnabled || isMagnificationByNavigationButtonEnabled - }else{ + } else { isMagnificationByTripleTapGesturesEnabled || isMagnificationByVolumeButtonsEnabled } } catch (e: Throwable) { - Q42StatsLogger.e(TAG, "Could not read magnification. Returning null", e) + Q42StatsLogger.w(TAG, "Could not read magnification, user likely has never used magnification before. Returning null.") null } @@ -181,4 +193,45 @@ internal object AccessibilityCollector { Q42StatsLogger.e(TAG, "Could not read system int $name. Returning null", e) null } + + /** + * Gets the screen size in density independent pixels with portrait orientation. + * + * Note: calculates size given portrait mode. + */ + private fun getScreenSize(context: Context): Pair? { + return try { + val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager + val resources = context.resources + + // get screen size in pixels + val pixelScreenSize = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val windowMetrics = windowManager.currentWindowMetrics + with(windowMetrics.bounds) { + Pair(right, bottom) + } + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + val point = Point() + @Suppress("DEPRECATION") + windowManager.defaultDisplay.getRealSize(point) + Pair(point.x, point.y) + } else { + with(resources.displayMetrics) { + Pair(widthPixels, heightPixels) + } + } + + val portraitDpScreenSize = with(pixelScreenSize) { + val density = resources.displayMetrics.density + Pair( + (first / density).toInt(), + (second / density).toInt() + ) + } + portraitDpScreenSize + } catch (e: Throwable) { + Q42StatsLogger.e(TAG, "Could not read screen size. Returning null", e) + null + } + } }