-
Notifications
You must be signed in to change notification settings - Fork 18
ADFA-3644 | Support pop-out tooltip display for tablets and DeX #1164
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
Merged
+168
−13
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
common/src/main/java/com/itsaky/androidide/utils/DeviceFormFactor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package com.itsaky.androidide.utils | ||
|
|
||
| import android.content.Context | ||
| import android.content.res.Configuration | ||
| import android.os.Build | ||
| import android.util.DisplayMetrics | ||
| import android.view.WindowManager | ||
|
|
||
| private const val TABLET_MIN_SMALLEST_WIDTH_DP = 500 | ||
|
|
||
| data class DeviceFormFactor( | ||
| val isTablet: Boolean, | ||
| val isDexMode: Boolean, | ||
| ) { | ||
| val isLargeScreenLike: Boolean | ||
| get() = isTablet || isDexMode | ||
| } | ||
|
|
||
| object DeviceFormFactorUtils { | ||
|
|
||
| fun getCurrent(context: Context): DeviceFormFactor { | ||
| return DeviceFormFactor( | ||
| isTablet = isTablet(context), | ||
| isDexMode = isDexMode(context), | ||
| ) | ||
| } | ||
|
|
||
| fun isDexMode(context: Context): Boolean { | ||
| val uiModeType = context.resources.configuration.uiMode and Configuration.UI_MODE_TYPE_MASK | ||
| if (uiModeType == Configuration.UI_MODE_TYPE_DESK) { | ||
| return true | ||
| } | ||
|
|
||
| if (isSamsungDexModeModern(context)) { | ||
| return true | ||
| } | ||
|
|
||
| return isSamsungDexModeLegacy(context.resources.configuration) | ||
| } | ||
|
|
||
| fun isTablet(context: Context): Boolean { | ||
| if (context.resources.configuration.smallestScreenWidthDp >= TABLET_MIN_SMALLEST_WIDTH_DP) { | ||
| return true | ||
| } | ||
|
|
||
| return isPhysicalLargeScreen(context) | ||
| } | ||
|
|
||
| private fun isPhysicalLargeScreen(context: Context): Boolean { | ||
| return try { | ||
| val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager | ||
|
|
||
| val smallestPhysicalWidthDp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| val metrics = windowManager.maximumWindowMetrics | ||
| val density = context.resources.configuration.densityDpi / 160f | ||
| val widthDp = metrics.bounds.width() / density | ||
| val heightDp = metrics.bounds.height() / density | ||
| widthDp.coerceAtMost(heightDp) | ||
| } else { | ||
| val display = windowManager.defaultDisplay | ||
| val realMetrics = DisplayMetrics() | ||
| @Suppress("DEPRECATION") | ||
| display.getRealMetrics(realMetrics) | ||
| val widthDp = realMetrics.widthPixels / realMetrics.density | ||
| val heightDp = realMetrics.heightPixels / realMetrics.density | ||
| widthDp.coerceAtMost(heightDp) | ||
| } | ||
|
|
||
| smallestPhysicalWidthDp >= TABLET_MIN_SMALLEST_WIDTH_DP | ||
| } catch (_: Exception) { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| private fun isSamsungDexModeModern(context: Context): Boolean { | ||
| return try { | ||
| val desktopModeManager = context.applicationContext.getSystemService("desktopmode") ?: return false | ||
|
|
||
| val getDesktopModeStateMethod = desktopModeManager.javaClass.getDeclaredMethod("getDesktopModeState") | ||
| val desktopModeState = getDesktopModeStateMethod.invoke(desktopModeManager) ?: return false | ||
| val desktopModeStateClass = desktopModeState.javaClass | ||
|
|
||
| val getEnabledMethod = desktopModeStateClass.getDeclaredMethod("getEnabled") | ||
| val enabled = getEnabledMethod.invoke(desktopModeState) as Int | ||
| val enabledConstant = desktopModeStateClass.getDeclaredField("ENABLED").getInt(desktopModeStateClass) | ||
|
|
||
| enabled == enabledConstant | ||
| } catch (_: Exception) { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| private fun isSamsungDexModeLegacy(configuration: Configuration): Boolean { | ||
| val enabledValue = readSamsungDesktopModeValue( | ||
| target = configuration.javaClass, | ||
| fieldName = "SEM_DESKTOP_MODE_ENABLED", | ||
| targetClass = configuration.javaClass, | ||
| ) ?: return false | ||
|
|
||
| val currentValue = readSamsungDesktopModeValue( | ||
| target = configuration, | ||
| fieldName = "semDesktopModeEnabled", | ||
| targetClass = configuration.javaClass, | ||
| ) ?: return false | ||
|
|
||
| return currentValue == enabledValue | ||
| } | ||
|
|
||
| private fun readSamsungDesktopModeValue( | ||
| target: Any?, | ||
| fieldName: String, | ||
| targetClass: Class<*>, | ||
| ): Int? { | ||
| return runCatching { | ||
| targetClass.getField(fieldName).getInt(target) | ||
| }.recoverCatching { | ||
| targetClass.getDeclaredField(fieldName).apply { | ||
| isAccessible = true | ||
| }.getInt(target) | ||
| }.getOrNull() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.