chore(android): Cleanup code

This commit is contained in:
Darcy Wong 2025-10-07 09:03:33 +07:00
parent 295dfd9e92
commit 0e2011eca8
3 changed files with 16 additions and 72 deletions

View file

@ -246,17 +246,13 @@ public class SystemKeyboard extends InputMethodService implements OnKeyboardEven
inputViewHeight = inputView.getHeight();
}
int navigationHeight = KMManager.getNavigationBarHeight(this);
int bannerHeight = KMManager.getBannerHeight(this);
int kbHeight = KMManager.getKeyboardHeight(this);
int bottomInset = KMManager.getBottomInset();
outInsets.contentTopInsets = inputViewHeight - bannerHeight - kbHeight - bottomInset;
outInsets.contentTopInsets = inputViewHeight - bannerHeight - kbHeight - navigationHeight;
outInsets.visibleTopInsets = outInsets.contentTopInsets;
outInsets.touchableInsets = InputMethodService.Insets.TOUCHABLE_INSETS_REGION;
outInsets.touchableRegion.set(0, outInsets.contentTopInsets, size.x, size.y);
//Log.d(TAG, String.format("inputView: %d, , bannerHeight: %d, kbHeight: %d, navigationHeight: %d",
// inputViewHeight, bannerHeight, kbHeight, navigationHeight));
}
@Override

View file

@ -101,7 +101,7 @@ public class BaseActivity extends AppCompatActivity {
insets.bottom);
KMManager.applyInsetsToKeyboard(
insets.left, insets.right, insets.bottom);
KMManager.KeyboardType.KEYBOARD_TYPE_INAPP, insets.left, insets.right, insets.bottom);
return windowInsets;
});
}

View file

@ -715,16 +715,17 @@ public final class KMManager {
/**
* Applies padding to the keyboard so it's not covered by system insets
* @param {KeyboardType} keyboard
* @param left int padding to account for notch/navigation bar on left side of screen
* @param right int padding to account for notch/navigation bar on right side of screen
* @param bottom int padding to account for navigation bar on bottom of screen
*/
public static void applyInsetsToKeyboard(int left, int right, int bottom) {
if (InAppKeyboard != null) {
public static void applyInsetsToKeyboard(KeyboardType keyboardType, int left, int right, int bottom) {
if (keyboardType == KeyboardType.KEYBOARD_TYPE_INAPP && InAppKeyboard != null) {
applyInsetsPaddingToKeyboardView(InAppKeyboard, left, right, bottom);
}
if (SystemKeyboard != null) {
if (keyboardType == KeyboardType.KEYBOARD_TYPE_SYSTEM && SystemKeyboard != null) {
applyInsetsPaddingToKeyboardView(SystemKeyboard, left, right, bottom);
}
}
@ -755,10 +756,6 @@ public final class KMManager {
}
}
public static int getBottomInset() {
return bottomInset;
}
private static void initKeyboard(Context appContext, KeyboardType keyboardType) {
KMKeyboard keyboard = null;
KMKeyboardWebViewClient webViewClient = null;
@ -915,20 +912,13 @@ public final class KMManager {
private static void installSystemKeyboardInsetsListener(View anchor) {
ViewCompat.setOnApplyWindowInsetsListener(anchor, (v, insets) -> {
// System bars (status bar and navigation bar)
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Insets systemBarInsets = insets.getInsets(
WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
Insets gestureInsets = insets.getInsets(WindowInsetsCompat.Type.systemGestures());
Insets navigationInsets = insets.getInsets(WindowInsetsCompat.Type.navigationBars());
// Cutout (camera notch) safe insets
DisplayCutoutCompat cutout = insets.getDisplayCutout();
int safeLeft = (cutout != null) ? cutout.getSafeInsetLeft() : 0;
int safeRight = (cutout != null) ? cutout.getSafeInsetRight() : 0;
int leftInset = Math.max(systemBars.left, safeLeft);
int rightInset = Math.max(systemBars.right, safeRight);
boolean imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime());
int bottomInset = systemBars.bottom;
applyInsetsToKeyboard(leftInset, rightInset, bottomInset);
bottomInset = systemBarInsets.bottom;
applyInsetsToKeyboard(KeyboardType.KEYBOARD_TYPE_SYSTEM, systemBarInsets.left, systemBarInsets.right, bottomInset);
return insets;
});
@ -2504,56 +2494,14 @@ public final class KMManager {
KeyboardHeight_Context_Landscape_Default = (int) newResources.getDimension(R.dimen.keyboard_height);
}
public static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
/**
* Get the navigation bar height (if visible) in pixels
* @param context - the context
* @param context - The context
* @return int - navigation bar height in pixels
*/
public static int getNavigationBarHeight(Context context) {
int navigationBarHeight = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
WindowInsets insets = windowManager.getCurrentWindowMetrics().getWindowInsets();
android.graphics.Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
android.graphics.Insets gestureInsets = insets.getInsets(WindowInsetsCompat.Type.systemGestures());
android.graphics.Insets navigationInsets = insets.getInsets(WindowInsetsCompat.Type.navigationBars());
if (gestureInsets.bottom != navigationInsets.bottom) {
navigationBarHeight = gestureInsets.bottom;
} else {
navigationBarHeight = systemBarInsets.bottom;
}
Log.d(TAG, String.format("gesture.bottom: %d; navigation.bottom: %d; systemBar.bottom %d",
gestureInsets.bottom, navigationInsets.bottom, systemBarInsets.bottom));
}
} else {
// Determine if navigation bar is visible
int resourceId = context.getResources().getIdentifier(
"config_showNavigationBar", "bool", "android");
if (resourceId <= 0) {
return navigationBarHeight;
}
// Navigation bar visible so get the height
resourceId = context.getResources().getIdentifier(
"navigation_bar_height", "dimen", "android");
navigationBarHeight = (resourceId > 0) ?
context.getResources().getDimensionPixelSize(resourceId) : 0;
}
return navigationBarHeight;
// bottomInset already updated from the inset listener
return bottomInset;
}
/**