From 4718b5bb20103b8dd8fd3a4fd77f40f0c3587623 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Wed, 24 Jan 2024 12:53:38 +0700 Subject: [PATCH] refactor(android/engine): Refactor detection of device orientation --- .../kmapro/AdjustKeyboardHeightActivity.java | 8 ++------ .../com/tavultesoft/kmapro/MainActivity.java | 6 ++++-- .../main/java/com/keyman/engine/KMManager.java | 18 ++++++++++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustKeyboardHeightActivity.java b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustKeyboardHeightActivity.java index a959bb1df63..a371adc1292 100644 --- a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustKeyboardHeightActivity.java +++ b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustKeyboardHeightActivity.java @@ -8,12 +8,9 @@ import android.content.res.Configuration; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; -import android.view.Display; import android.view.MotionEvent; -import android.view.Surface; import android.view.View; import android.view.ViewGroup; -import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; @@ -110,9 +107,8 @@ public boolean onTouch(View view, MotionEvent event) { break; case MotionEvent.ACTION_UP: // Save the currentHeight when the user releases - Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); - int rotation = display.getRotation(); - String keyboardHeightKey = (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) ? + int orientation = KMManager.getOrientation(context);; + String keyboardHeightKey = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? KMManager.KMKey_KeyboardHeightLandscape : KMManager.KMKey_KeyboardHeightPortrait; editor.putInt(keyboardHeightKey, currentHeight); editor.commit(); diff --git a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/MainActivity.java b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/MainActivity.java index 16899f7c2d9..d135513502d 100644 --- a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/MainActivity.java +++ b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/MainActivity.java @@ -267,9 +267,11 @@ protected void onResume() { // onConfigurationChanged() only triggers when device is rotated while app is in foreground // This handles when device is rotated while app is in background + // using KMManager.getOrientation() since getConfiguration().orientation is unreliable #10241 Configuration newConfig = this.getResources().getConfiguration(); - if (newConfig != null && newConfig.orientation != lastOrientation) { - lastOrientation = newConfig.orientation; + int newOrientation = KMManager.getOrientation(context); + if (newOrientation != lastOrientation) { + lastOrientation = newOrientation; KMManager.onConfigurationChanged(newConfig); } resizeTextView(textView.isKeyboardVisible()); diff --git a/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java b/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java index c20dd6db59b..1d2593baf75 100644 --- a/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java +++ b/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java @@ -1998,6 +1998,17 @@ public static void removeKeyboardEventListener(OnKeyboardEventListener listener) KMKeyboard.removeOnKeyboardEventListener(listener); } + public static int getOrientation(Context context) { + Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + int rotation = display.getRotation(); + if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { + return Configuration.ORIENTATION_PORTRAIT; + } else if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { + return Configuration.ORIENTATION_LANDSCAPE; + } + return Configuration.ORIENTATION_UNDEFINED; + } + public static int getBannerHeight(Context context) { int bannerHeight = 0; if (InAppKeyboard != null && InAppKeyboard.getBanner() != BannerType.BLANK) { @@ -2012,11 +2023,10 @@ public static int getKeyboardHeight(Context context) { int defaultHeight = (int) context.getResources().getDimension(R.dimen.keyboard_height); SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE); - Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); - int rotation = display.getRotation(); - if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { + int orientation = getOrientation(context); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { return prefs.getInt(KMManager.KMKey_KeyboardHeightPortrait, defaultHeight); - } else if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { + } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { return prefs.getInt(KMManager.KMKey_KeyboardHeightLandscape, defaultHeight); }