restore libgdx keyboardheightprovider without androidx

- https://github.com/libgdx/libgdx/pull/7485
This commit is contained in:
Anthony Calosa
2024-10-29 20:44:01 +08:00
parent ea472ba708
commit 57ccf5dd9e
3 changed files with 107 additions and 19 deletions

View File

@@ -217,6 +217,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>26.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>

View File

@@ -31,10 +31,9 @@ import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.badlogic.gdx.*; import com.badlogic.gdx.*;
// androidx dependencies disabled so it will not crash Forge app on startup import com.badlogic.gdx.backends.android.keyboardheight.AndroidRKeyboardHeightProvider;
//import com.badlogic.gdx.backends.android.keyboardheight.AndroidXKeyboardHeightProvider; import com.badlogic.gdx.backends.android.keyboardheight.KeyboardHeightProvider;
//import com.badlogic.gdx.backends.android.keyboardheight.KeyboardHeightProvider; import com.badlogic.gdx.backends.android.keyboardheight.StandardKeyboardHeightProvider;
//import com.badlogic.gdx.backends.android.keyboardheight.StandardKeyboardHeightProvider;
import com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy; import com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy;
import com.badlogic.gdx.utils.*; import com.badlogic.gdx.utils.*;
@@ -57,14 +56,14 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
protected final Array<Runnable> runnables = new Array<Runnable>(); protected final Array<Runnable> runnables = new Array<Runnable>();
protected final Array<Runnable> executedRunnables = new Array<Runnable>(); protected final Array<Runnable> executedRunnables = new Array<Runnable>();
protected final SnapshotArray<LifecycleListener> lifecycleListeners = new SnapshotArray<LifecycleListener>( protected final SnapshotArray<LifecycleListener> lifecycleListeners = new SnapshotArray<LifecycleListener>(
LifecycleListener.class); LifecycleListener.class);
private final Array<AndroidEventListener> androidEventListeners = new Array<AndroidEventListener>(); private final Array<AndroidEventListener> androidEventListeners = new Array<AndroidEventListener>();
protected int logLevel = LOG_INFO; protected int logLevel = LOG_INFO;
protected ApplicationLogger applicationLogger; protected ApplicationLogger applicationLogger;
protected boolean useImmersiveMode = false; protected boolean useImmersiveMode = false;
private int wasFocusChanged = -1; private int wasFocusChanged = -1;
private boolean isWaitingForAudio = false; private boolean isWaitingForAudio = false;
//private KeyboardHeightProvider keyboardHeightProvider; private KeyboardHeightProvider keyboardHeightProvider;
protected boolean renderUnderCutout = false; protected boolean renderUnderCutout = false;
@@ -122,7 +121,7 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
config.nativeLoader.load(); config.nativeLoader.load();
setApplicationLogger(new AndroidApplicationLogger()); setApplicationLogger(new AndroidApplicationLogger());
graphics = new AndroidGraphics(this, config, 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); input = createInput(this, this, graphics.view, config);
audio = createAudio(this, config); audio = createAudio(this, config);
files = createFiles(); files = createFiles();
@@ -182,18 +181,16 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
setLayoutInDisplayCutoutMode(this.renderUnderCutout); setLayoutInDisplayCutoutMode(this.renderUnderCutout);
// As per the docs, it might work unreliable < 23 https://developer.android.com/jetpack/androidx/releases/core#1.5.0-alpha02 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// So, I guess since 23 is pretty rare we can use the old API for the users keyboardHeightProvider = new AndroidRKeyboardHeightProvider(this);
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
keyboardHeightProvider = new AndroidXKeyboardHeightProvider(this);
} else { } else {
keyboardHeightProvider = new StandardKeyboardHeightProvider(this); keyboardHeightProvider = new StandardKeyboardHeightProvider(this);
}*/ }
} }
protected FrameLayout.LayoutParams createLayoutParams () { protected FrameLayout.LayoutParams createLayoutParams () {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, 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; layoutParams.gravity = Gravity.CENTER;
return layoutParams; return layoutParams;
} }
@@ -263,7 +260,7 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
graphics.onPauseGLSurfaceView(); graphics.onPauseGLSurfaceView();
super.onPause(); super.onPause();
//keyboardHeightProvider.setKeyboardHeightObserver(null); keyboardHeightProvider.setKeyboardHeightObserver(null);
} }
@Override @Override
@@ -292,19 +289,19 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
this.isWaitingForAudio = false; this.isWaitingForAudio = false;
} }
super.onResume(); super.onResume();
/*keyboardHeightProvider.setKeyboardHeightObserver((DefaultAndroidInput)Gdx.input); keyboardHeightProvider.setKeyboardHeightObserver((DefaultAndroidInput)Gdx.input);
((AndroidGraphics)getGraphics()).getView().post(new Runnable() { ((AndroidGraphics)getGraphics()).getView().post(new Runnable() {
@Override @Override
public void run () { public void run () {
keyboardHeightProvider.start(); keyboardHeightProvider.start();
} }
});*/ });
} }
@Override @Override
protected void onDestroy () { protected void onDestroy () {
super.onDestroy(); super.onDestroy();
//keyboardHeightProvider.close(); keyboardHeightProvider.close();
} }
@Override @Override
@@ -531,7 +528,7 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
return new DefaultAndroidFiles(this.getAssets(), this, true); return new DefaultAndroidFiles(this.getAssets(), this, true);
} }
/*public KeyboardHeightProvider getKeyboardHeightProvider () { public KeyboardHeightProvider getKeyboardHeightProvider () {
return keyboardHeightProvider; return keyboardHeightProvider;
}*/ }
} }

View File

@@ -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;
}
}