From 57ccf5dd9ed100da47d82212f6bc4a7e87dd2c24 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Tue, 29 Oct 2024 20:44:01 +0800 Subject: [PATCH] restore libgdx keyboardheightprovider without androidx - https://github.com/libgdx/libgdx/pull/7485 --- forge-gui-android/pom.xml | 6 ++ .../android/ForgeAndroidApplication.java | 35 ++++---- .../AndroidRKeyboardHeightProvider.java | 85 +++++++++++++++++++ 3 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 forge-gui-android/src/com/badlogic/gdx/backends/android/keyboardheight/AndroidRKeyboardHeightProvider.java diff --git a/forge-gui-android/pom.xml b/forge-gui-android/pom.xml index 230da7d3ad2..9db663b01db 100644 --- a/forge-gui-android/pom.xml +++ b/forge-gui-android/pom.xml @@ -217,6 +217,12 @@ + + org.jetbrains + annotations + 26.0.1 + compile + diff --git a/forge-gui-android/src/com/badlogic/gdx/backends/android/ForgeAndroidApplication.java b/forge-gui-android/src/com/badlogic/gdx/backends/android/ForgeAndroidApplication.java index a1c08bab642..b522e247083 100644 --- a/forge-gui-android/src/com/badlogic/gdx/backends/android/ForgeAndroidApplication.java +++ b/forge-gui-android/src/com/badlogic/gdx/backends/android/ForgeAndroidApplication.java @@ -31,10 +31,9 @@ import android.view.WindowManager; import android.widget.FrameLayout; import com.badlogic.gdx.*; -// androidx dependencies disabled so it will not crash Forge app on startup -//import com.badlogic.gdx.backends.android.keyboardheight.AndroidXKeyboardHeightProvider; -//import com.badlogic.gdx.backends.android.keyboardheight.KeyboardHeightProvider; -//import com.badlogic.gdx.backends.android.keyboardheight.StandardKeyboardHeightProvider; +import com.badlogic.gdx.backends.android.keyboardheight.AndroidRKeyboardHeightProvider; +import com.badlogic.gdx.backends.android.keyboardheight.KeyboardHeightProvider; +import com.badlogic.gdx.backends.android.keyboardheight.StandardKeyboardHeightProvider; import com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy; import com.badlogic.gdx.utils.*; @@ -57,14 +56,14 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat protected final Array runnables = new Array(); protected final Array executedRunnables = new Array(); protected final SnapshotArray lifecycleListeners = new SnapshotArray( - LifecycleListener.class); + LifecycleListener.class); private final Array androidEventListeners = new Array(); protected int logLevel = LOG_INFO; protected ApplicationLogger applicationLogger; protected boolean useImmersiveMode = false; private int wasFocusChanged = -1; private boolean isWaitingForAudio = false; - //private KeyboardHeightProvider keyboardHeightProvider; + private KeyboardHeightProvider keyboardHeightProvider; protected boolean renderUnderCutout = false; @@ -122,7 +121,7 @@ private void init (ApplicationListener listener, AndroidApplicationConfiguration config.nativeLoader.load(); setApplicationLogger(new AndroidApplicationLogger()); graphics = new AndroidGraphics(this, config, - config.resolutionStrategy == null ? new FillResolutionStrategy() : config.resolutionStrategy); + config.resolutionStrategy == null ? new FillResolutionStrategy() : config.resolutionStrategy); input = createInput(this, this, graphics.view, config); audio = createAudio(this, config); files = createFiles(); @@ -182,18 +181,16 @@ public void dispose () { setLayoutInDisplayCutoutMode(this.renderUnderCutout); - // As per the docs, it might work unreliable < 23 https://developer.android.com/jetpack/androidx/releases/core#1.5.0-alpha02 - // So, I guess since 23 is pretty rare we can use the old API for the users - /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - keyboardHeightProvider = new AndroidXKeyboardHeightProvider(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + keyboardHeightProvider = new AndroidRKeyboardHeightProvider(this); } else { keyboardHeightProvider = new StandardKeyboardHeightProvider(this); - }*/ + } } protected FrameLayout.LayoutParams createLayoutParams () { FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, - android.view.ViewGroup.LayoutParams.MATCH_PARENT); + android.view.ViewGroup.LayoutParams.MATCH_PARENT); layoutParams.gravity = Gravity.CENTER; return layoutParams; } @@ -263,7 +260,7 @@ protected void onPause () { graphics.onPauseGLSurfaceView(); super.onPause(); - //keyboardHeightProvider.setKeyboardHeightObserver(null); + keyboardHeightProvider.setKeyboardHeightObserver(null); } @Override @@ -292,19 +289,19 @@ protected void onResume () { this.isWaitingForAudio = false; } super.onResume(); - /*keyboardHeightProvider.setKeyboardHeightObserver((DefaultAndroidInput)Gdx.input); + keyboardHeightProvider.setKeyboardHeightObserver((DefaultAndroidInput)Gdx.input); ((AndroidGraphics)getGraphics()).getView().post(new Runnable() { @Override public void run () { keyboardHeightProvider.start(); } - });*/ + }); } @Override protected void onDestroy () { super.onDestroy(); - //keyboardHeightProvider.close(); + keyboardHeightProvider.close(); } @Override @@ -531,7 +528,7 @@ protected AndroidFiles createFiles () { return new DefaultAndroidFiles(this.getAssets(), this, true); } - /*public KeyboardHeightProvider getKeyboardHeightProvider () { + public KeyboardHeightProvider getKeyboardHeightProvider () { return keyboardHeightProvider; - }*/ + } } \ No newline at end of file diff --git a/forge-gui-android/src/com/badlogic/gdx/backends/android/keyboardheight/AndroidRKeyboardHeightProvider.java b/forge-gui-android/src/com/badlogic/gdx/backends/android/keyboardheight/AndroidRKeyboardHeightProvider.java new file mode 100644 index 00000000000..10fd2bc047a --- /dev/null +++ b/forge-gui-android/src/com/badlogic/gdx/backends/android/keyboardheight/AndroidRKeyboardHeightProvider.java @@ -0,0 +1,85 @@ + +package com.badlogic.gdx.backends.android.keyboardheight; + +import android.annotation.RequiresApi; +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.res.Configuration; +import android.os.Build.VERSION_CODES; +import android.view.View; +import android.view.View.OnApplyWindowInsetsListener; +import android.view.WindowInsets; +import android.graphics.Insets; +import org.jetbrains.annotations.NotNull; + +@RequiresApi(api = VERSION_CODES.R) +public class AndroidRKeyboardHeightProvider implements KeyboardHeightProvider { + + private final View view; + private final Activity activity; + private KeyboardHeightObserver observer; + + /** The cached landscape height of the keyboard */ + private static int keyboardLandscapeHeight; + + /** The cached portrait height of the keyboard */ + private static int keyboardPortraitHeight; + + public AndroidRKeyboardHeightProvider (final Activity activity) { + this.view = activity.findViewById(android.R.id.content); + this.activity = activity; + } + + @Override + public void start () { + view.setOnApplyWindowInsetsListener(new OnApplyWindowInsetsListener() { + @NotNull + @Override + public WindowInsets onApplyWindowInsets (@NotNull View v, @NotNull WindowInsets windowInsets) { + if (observer == null) return windowInsets; + int orientation = activity.getResources().getConfiguration().orientation; + boolean isVisible = windowInsets.isVisible(WindowInsets.Type.ime()); + if (isVisible) { + Insets insets = windowInsets.getInsets(WindowInsets.Type.ime()); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + keyboardPortraitHeight = insets.bottom; + } else { + keyboardLandscapeHeight = insets.bottom; + } + + // I don't know whether I went completly insane now, but WindowInsets.Type.all() isn't existing? + @SuppressLint("WrongConstant") + int leftInset = windowInsets.getInsets(0xFFFFFFFF).left; + @SuppressLint("WrongConstant") + int rightInset = windowInsets.getInsets(0xFFFFFFFF).right; + + observer.onKeyboardHeightChanged(insets.bottom, leftInset, rightInset, orientation); + } else { + observer.onKeyboardHeightChanged(0, 0, 0, orientation); + } + + return windowInsets; + } + }); + } + + @Override + public void close () { + view.setOnApplyWindowInsetsListener(null); + } + + @Override + public void setKeyboardHeightObserver (KeyboardHeightObserver observer) { + this.observer = observer; + } + + @Override + public int getKeyboardLandscapeHeight () { + return keyboardLandscapeHeight; + } + + @Override + public int getKeyboardPortraitHeight () { + return keyboardPortraitHeight; + } +} \ No newline at end of file