|
| 1 | +package com.itsaky.androidide.utils |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.res.Configuration |
| 5 | +import android.os.Build |
| 6 | +import android.util.DisplayMetrics |
| 7 | +import android.view.WindowManager |
| 8 | + |
| 9 | +private const val TABLET_MIN_SMALLEST_WIDTH_DP = 500 |
| 10 | + |
| 11 | +data class DeviceFormFactor( |
| 12 | + val isTablet: Boolean, |
| 13 | + val isDexMode: Boolean, |
| 14 | +) { |
| 15 | + val isLargeScreenLike: Boolean |
| 16 | + get() = isTablet || isDexMode |
| 17 | +} |
| 18 | + |
| 19 | +object DeviceFormFactorUtils { |
| 20 | + |
| 21 | + fun getCurrent(context: Context): DeviceFormFactor { |
| 22 | + return DeviceFormFactor( |
| 23 | + isTablet = isTablet(context), |
| 24 | + isDexMode = isDexMode(context), |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + fun isDexMode(context: Context): Boolean { |
| 29 | + val uiModeType = context.resources.configuration.uiMode and Configuration.UI_MODE_TYPE_MASK |
| 30 | + if (uiModeType == Configuration.UI_MODE_TYPE_DESK) { |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + if (isSamsungDexModeModern(context)) { |
| 35 | + return true |
| 36 | + } |
| 37 | + |
| 38 | + return isSamsungDexModeLegacy(context.resources.configuration) |
| 39 | + } |
| 40 | + |
| 41 | + fun isTablet(context: Context): Boolean { |
| 42 | + if (context.resources.configuration.smallestScreenWidthDp >= TABLET_MIN_SMALLEST_WIDTH_DP) { |
| 43 | + return true |
| 44 | + } |
| 45 | + |
| 46 | + return isPhysicalLargeScreen(context) |
| 47 | + } |
| 48 | + |
| 49 | + private fun isPhysicalLargeScreen(context: Context): Boolean { |
| 50 | + return try { |
| 51 | + val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager |
| 52 | + |
| 53 | + val smallestPhysicalWidthDp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 54 | + val metrics = windowManager.maximumWindowMetrics |
| 55 | + val density = context.resources.configuration.densityDpi / 160f |
| 56 | + val widthDp = metrics.bounds.width() / density |
| 57 | + val heightDp = metrics.bounds.height() / density |
| 58 | + widthDp.coerceAtMost(heightDp) |
| 59 | + } else { |
| 60 | + val display = windowManager.defaultDisplay |
| 61 | + val realMetrics = DisplayMetrics() |
| 62 | + @Suppress("DEPRECATION") |
| 63 | + display.getRealMetrics(realMetrics) |
| 64 | + val widthDp = realMetrics.widthPixels / realMetrics.density |
| 65 | + val heightDp = realMetrics.heightPixels / realMetrics.density |
| 66 | + widthDp.coerceAtMost(heightDp) |
| 67 | + } |
| 68 | + |
| 69 | + smallestPhysicalWidthDp >= TABLET_MIN_SMALLEST_WIDTH_DP |
| 70 | + } catch (_: Exception) { |
| 71 | + false |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private fun isSamsungDexModeModern(context: Context): Boolean { |
| 76 | + return try { |
| 77 | + val desktopModeManager = context.applicationContext.getSystemService("desktopmode") ?: return false |
| 78 | + |
| 79 | + val getDesktopModeStateMethod = desktopModeManager.javaClass.getDeclaredMethod("getDesktopModeState") |
| 80 | + val desktopModeState = getDesktopModeStateMethod.invoke(desktopModeManager) ?: return false |
| 81 | + val desktopModeStateClass = desktopModeState.javaClass |
| 82 | + |
| 83 | + val getEnabledMethod = desktopModeStateClass.getDeclaredMethod("getEnabled") |
| 84 | + val enabled = getEnabledMethod.invoke(desktopModeState) as Int |
| 85 | + val enabledConstant = desktopModeStateClass.getDeclaredField("ENABLED").getInt(desktopModeStateClass) |
| 86 | + |
| 87 | + enabled == enabledConstant |
| 88 | + } catch (_: Exception) { |
| 89 | + false |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun isSamsungDexModeLegacy(configuration: Configuration): Boolean { |
| 94 | + val enabledValue = readSamsungDesktopModeValue( |
| 95 | + target = configuration.javaClass, |
| 96 | + fieldName = "SEM_DESKTOP_MODE_ENABLED", |
| 97 | + targetClass = configuration.javaClass, |
| 98 | + ) ?: return false |
| 99 | + |
| 100 | + val currentValue = readSamsungDesktopModeValue( |
| 101 | + target = configuration, |
| 102 | + fieldName = "semDesktopModeEnabled", |
| 103 | + targetClass = configuration.javaClass, |
| 104 | + ) ?: return false |
| 105 | + |
| 106 | + return currentValue == enabledValue |
| 107 | + } |
| 108 | + |
| 109 | + private fun readSamsungDesktopModeValue( |
| 110 | + target: Any?, |
| 111 | + fieldName: String, |
| 112 | + targetClass: Class<*>, |
| 113 | + ): Int? { |
| 114 | + return runCatching { |
| 115 | + targetClass.getField(fieldName).getInt(target) |
| 116 | + }.recoverCatching { |
| 117 | + targetClass.getDeclaredField(fieldName).apply { |
| 118 | + isAccessible = true |
| 119 | + }.getInt(target) |
| 120 | + }.getOrNull() |
| 121 | + } |
| 122 | +} |
0 commit comments