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

feature / screen size πŸ“± #20

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all data types are strings, right? But I might remember incorrectly. As documentation, it would be more informative to list how the backend should parse the string. Reading this table does not answer the question of whether it is an int or float

| `screenHeight` | String | Screen height in density-independent pixels. Beware: this value is different when there is a different screen orientation. |

### System

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -146,19 +158,19 @@ internal object AccessibilityCollector {
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun isMagnificationEnabled(context: Context, serviceNames: List<String>): 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) {
val isMagnificationByNavigationButtonEnabled =
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
}

Expand All @@ -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<Int, Int>? {
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error or warning? Warning seems most appropriate.

null
}
}
}