Skip to content

Commit

Permalink
restore libgdx keyboardheightprovider without androidx
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlahnota committed Oct 29, 2024
1 parent ea472ba commit 57ccf5d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 19 deletions.
6 changes: 6 additions & 0 deletions forge-gui-android/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -57,14 +56,14 @@ public class ForgeAndroidApplication extends Activity implements AndroidApplicat
protected final Array<Runnable> runnables = new Array<Runnable>();
protected final Array<Runnable> executedRunnables = new Array<Runnable>();
protected final SnapshotArray<LifecycleListener> lifecycleListeners = new SnapshotArray<LifecycleListener>(
LifecycleListener.class);
LifecycleListener.class);
private final Array<AndroidEventListener> androidEventListeners = new Array<AndroidEventListener>();
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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -263,7 +260,7 @@ protected void onPause () {
graphics.onPauseGLSurfaceView();

super.onPause();
//keyboardHeightProvider.setKeyboardHeightObserver(null);
keyboardHeightProvider.setKeyboardHeightObserver(null);
}

@Override
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -531,7 +528,7 @@ protected AndroidFiles createFiles () {
return new DefaultAndroidFiles(this.getAssets(), this, true);
}

/*public KeyboardHeightProvider getKeyboardHeightProvider () {
public KeyboardHeightProvider getKeyboardHeightProvider () {
return keyboardHeightProvider;
}*/
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 57ccf5d

Please sign in to comment.