-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore libgdx keyboardheightprovider without androidx
- Loading branch information
1 parent
ea472ba
commit 57ccf5d
Showing
3 changed files
with
107 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
.../src/com/badlogic/gdx/backends/android/keyboardheight/AndroidRKeyboardHeightProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |