Skip to content

Commit e073eef

Browse files
CopilotWhiredPlanck
andcommitted
Fix Android 11 crash by adding version check for WindowMetrics API
Co-authored-by: WhiredPlanck <47623588+WhiredPlanck@users.noreply.github.com>
1 parent 2a60c01 commit e073eef

File tree

1 file changed

+15
-6
lines changed
  • app/src/main/java/com/osfans/trime/ime/keyboard

1 file changed

+15
-6
lines changed

app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package com.osfans.trime.ime.keyboard
66

7+
import android.graphics.Point
8+
import android.os.Build
79
import android.view.KeyEvent
810
import android.view.WindowInsets
911
import android.view.WindowManager
@@ -98,12 +100,19 @@ class Keyboard(
98100
if (appContext.isLandscapeMode()) keyboardPaddingLand else keyboardPadding
99101
}
100102

101-
val windowMetrics = appContext.windowManager.currentWindowMetrics
102-
val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(
103-
WindowInsets.Type.systemBars() or WindowInsets.Type.displayCutout(),
104-
)
105-
106-
val safeWidth = windowMetrics.bounds.width() - insets.left - insets.right
103+
val safeWidth = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
104+
val windowMetrics = appContext.windowManager.currentWindowMetrics
105+
val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(
106+
WindowInsets.Type.systemBars() or WindowInsets.Type.displayCutout(),
107+
)
108+
windowMetrics.bounds.width() - insets.left - insets.right
109+
} else {
110+
@Suppress("DEPRECATION")
111+
val size = Point()
112+
@Suppress("DEPRECATION")
113+
appContext.windowManager.defaultDisplay.getSize(size)
114+
size.x
115+
}
107116
return safeWidth - 2 * appContext.dp(padding)
108117
}
109118

0 commit comments

Comments
 (0)