summaryrefslogtreecommitdiffstats
path: root/platform/android/java
diff options
context:
space:
mode:
authorFredia Huya-Kouadio <fhuyakou@gmail.com>2024-05-13 00:59:03 -0700
committerFredia Huya-Kouadio <fhuyakou@gmail.com>2024-05-13 00:59:03 -0700
commitab4fbbcaa743d96a70f945c13b2c3093a23f5ec4 (patch)
treefcce8f32ca3d426b13d2a63b0384ce5c456f0c9b /platform/android/java
parentbdc0316217940a8ccc80ce536547d42e6477adf4 (diff)
downloadredot-engine-ab4fbbcaa743d96a70f945c13b2c3093a23f5ec4.tar.gz
Switch to the WindowInsetsAnimationCompat api
Replace the use of WindowInsetsAnimation with WindowInsetsAnimationCompat; the former was only introdcued in api 30 and caused a crash on older versions of Android. Fixes https://github.com/godotengine/godot/issues/91773
Diffstat (limited to 'platform/android/java')
-rw-r--r--platform/android/java/lib/src/org/godotengine/godot/Godot.kt81
1 files changed, 33 insertions, 48 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
index fbdf07e6c2..290be727ab 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
+++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
@@ -38,7 +38,6 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
-import android.graphics.Rect
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
@@ -46,10 +45,12 @@ import android.hardware.SensorManager
import android.os.*
import android.util.Log
import android.view.*
-import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.FrameLayout
import androidx.annotation.Keep
import androidx.annotation.StringRes
+import androidx.core.view.ViewCompat
+import androidx.core.view.WindowInsetsAnimationCompat
+import androidx.core.view.WindowInsetsCompat
import com.google.android.vending.expansion.downloader.*
import org.godotengine.godot.input.GodotEditText
import org.godotengine.godot.io.directory.DirectoryAccessHandler
@@ -418,58 +419,42 @@ class Godot(private val context: Context) : SensorEventListener {
io?.setEdit(editText)
// Listeners for keyboard height.
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
- // Report the height of virtual keyboard as it changes during the animation.
- val decorView = activity.window.decorView
- decorView.setWindowInsetsAnimationCallback(object : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
- var startBottom = 0
- var endBottom = 0
- override fun onPrepare(animation: WindowInsetsAnimation) {
- startBottom = decorView.rootWindowInsets.getInsets(WindowInsets.Type.ime()).bottom
- }
+ val decorView = activity.window.decorView
+ // Report the height of virtual keyboard as it changes during the animation.
+ ViewCompat.setWindowInsetsAnimationCallback(decorView, object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_STOP) {
+ var startBottom = 0
+ var endBottom = 0
+ override fun onPrepare(animation: WindowInsetsAnimationCompat) {
+ startBottom = ViewCompat.getRootWindowInsets(decorView)?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
+ }
- override fun onStart(animation: WindowInsetsAnimation, bounds: WindowInsetsAnimation.Bounds): WindowInsetsAnimation.Bounds {
- endBottom = decorView.rootWindowInsets.getInsets(WindowInsets.Type.ime()).bottom
- return bounds
- }
+ override fun onStart(animation: WindowInsetsAnimationCompat, bounds: WindowInsetsAnimationCompat.BoundsCompat): WindowInsetsAnimationCompat.BoundsCompat {
+ endBottom = ViewCompat.getRootWindowInsets(decorView)?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
+ return bounds
+ }
- override fun onProgress(windowInsets: WindowInsets, list: List<WindowInsetsAnimation>): WindowInsets {
- // Find the IME animation.
- var imeAnimation: WindowInsetsAnimation? = null
- for (animation in list) {
- if (animation.typeMask and WindowInsets.Type.ime() != 0) {
- imeAnimation = animation
- break
- }
- }
- // Update keyboard height based on IME animation.
- if (imeAnimation != null) {
- val interpolatedFraction = imeAnimation.interpolatedFraction
- // Linear interpolation between start and end values.
- val keyboardHeight = startBottom * (1.0f - interpolatedFraction) + endBottom * interpolatedFraction
- GodotLib.setVirtualKeyboardHeight(keyboardHeight.toInt())
+ override fun onProgress(windowInsets: WindowInsetsCompat, animationsList: List<WindowInsetsAnimationCompat>): WindowInsetsCompat {
+ // Find the IME animation.
+ var imeAnimation: WindowInsetsAnimationCompat? = null
+ for (animation in animationsList) {
+ if (animation.typeMask and WindowInsetsCompat.Type.ime() != 0) {
+ imeAnimation = animation
+ break
}
- return windowInsets
}
- override fun onEnd(animation: WindowInsetsAnimation) {}
- })
- } else {
- // Infer the virtual keyboard height using visible area.
- renderView?.view?.viewTreeObserver?.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
- // Don't allocate a new Rect every time the callback is called.
- val visibleSize = Rect()
- override fun onGlobalLayout() {
- renderView?.let {
- val surfaceView = it.view
-
- surfaceView.getWindowVisibleDisplayFrame(visibleSize)
- val keyboardHeight = surfaceView.height - visibleSize.bottom
- GodotLib.setVirtualKeyboardHeight(keyboardHeight)
- }
+ // Update keyboard height based on IME animation.
+ if (imeAnimation != null) {
+ val interpolatedFraction = imeAnimation.interpolatedFraction
+ // Linear interpolation between start and end values.
+ val keyboardHeight = startBottom * (1.0f - interpolatedFraction) + endBottom * interpolatedFraction
+ GodotLib.setVirtualKeyboardHeight(keyboardHeight.toInt())
}
- })
- }
+ return windowInsets
+ }
+
+ override fun onEnd(animation: WindowInsetsAnimationCompat) {}
+ })
if (host == primaryHost) {
renderView?.queueOnRenderThread {