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>
</exclusions>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>26.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<profiles>

View File

@@ -31,10 +31,9 @@ import android.view.Window;
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.*;
@@ -64,7 +63,7 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
protected boolean useImmersiveMode = false;
private int wasFocusChanged = -1;
private boolean isWaitingForAudio = false;
//private KeyboardHeightProvider keyboardHeightProvider;
private KeyboardHeightProvider keyboardHeightProvider;
protected boolean renderUnderCutout = false;
@@ -182,13 +181,11 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
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 () {
@@ -263,7 +260,7 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
graphics.onPauseGLSurfaceView();
super.onPause();
//keyboardHeightProvider.setKeyboardHeightObserver(null);
keyboardHeightProvider.setKeyboardHeightObserver(null);
}
@Override
@@ -292,19 +289,19 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
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 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
return new DefaultAndroidFiles(this.getAssets(), this, true);
}
/*public KeyboardHeightProvider getKeyboardHeightProvider () {
public KeyboardHeightProvider getKeyboardHeightProvider () {
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;
}
}