Skip to content

Commit

Permalink
refactor(android/engine): Refactor detection of device orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
darcywong00 committed Jan 24, 2024
1 parent 4785c6b commit 4718b5b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
18 changes: 14 additions & 4 deletions android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down

0 comments on commit 4718b5b

Please sign in to comment.