From 205c8c2d63580af66542a4a93c2144a28d29e294 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Mon, 13 May 2024 10:20:23 +0700 Subject: [PATCH] fix(android): Replace deprecated APIs for Display, Size, Metrics --- .../com/keyman/android/SystemKeyboard.java | 4 +- .../com/tavultesoft/kmapro/MainActivity.java | 7 +-- .../java/com/keyman/engine/KMKeyboard.java | 5 +- .../java/com/keyman/engine/KMManager.java | 47 ++++++++++++++++++- .../java/com/keyman/engine/KMPopoverView.java | 11 ++--- .../com/keyman/kmsample2/SystemKeyboard.java | 4 +- .../firstvoices/keyboards/SystemKeyboard.java | 8 +--- 7 files changed, 57 insertions(+), 29 deletions(-) diff --git a/android/KMAPro/kMAPro/src/main/java/com/keyman/android/SystemKeyboard.java b/android/KMAPro/kMAPro/src/main/java/com/keyman/android/SystemKeyboard.java index 10b040cc2ed..913a643a807 100644 --- a/android/KMAPro/kMAPro/src/main/java/com/keyman/android/SystemKeyboard.java +++ b/android/KMAPro/kMAPro/src/main/java/com/keyman/android/SystemKeyboard.java @@ -218,9 +218,7 @@ public void onComputeInsets(InputMethodService.Insets outInsets) { super.onComputeInsets(outInsets); // We should extend the touchable region so that Keyman sub keys menu can receive touch events outside the keyboard frame - WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); - Point size = new Point(0, 0); - wm.getDefaultDisplay().getSize(size); + Point size = KMManager.getWindowSize(getApplicationContext()); int inputViewHeight = 0; if (inputView != null) { 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 1de7decfe0d..1e5231f5215 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 @@ -603,8 +603,7 @@ public void resizeTextView(boolean isKeyboardVisible) { if (resourceId > 0) statusBarHeight = getResources().getDimensionPixelSize(resourceId); - Point size = new Point(0, 0); - getWindowManager().getDefaultDisplay().getSize(size); + Point size = KMManager.getWindowSize(context); int screenHeight = size.y; Log.d(TAG, "Main resizeTextView bannerHeight: " + bannerHeight); textView.setHeight(screenHeight - statusBarHeight - actionBarHeight - bannerHeight - keyboardHeight); @@ -867,9 +866,7 @@ public static void cleanupPackageInstall() { } public static Drawable getActionBarDrawable(Context context) { - Point size = new Point(); - WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); - wm.getDefaultDisplay().getSize(size); + Point size = KMManager.getWindowSize(context); int width = size.x; TypedValue outValue = new TypedValue(); diff --git a/android/KMEA/app/src/main/java/com/keyman/engine/KMKeyboard.java b/android/KMEA/app/src/main/java/com/keyman/engine/KMKeyboard.java index 7d741feec04..bc7dfc5cc6b 100644 --- a/android/KMEA/app/src/main/java/com/keyman/engine/KMKeyboard.java +++ b/android/KMEA/app/src/main/java/com/keyman/engine/KMKeyboard.java @@ -907,10 +907,7 @@ private void showSuggestionLongpress(Context context) { rotateSuggestions.setClickable(false); // Compute the actual display position (offset coordinate by actual screen pos of kbd) - WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); - DisplayMetrics metrics = new DisplayMetrics(); - wm.getDefaultDisplay().getMetrics(metrics); - float density = metrics.density; + float density = KMManager.getWindowDensity(context); int posX, posY; if (keyboardType == KeyboardType.KEYBOARD_TYPE_INAPP) { 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 b31fe425066..c0728e81b37 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 @@ -25,6 +25,8 @@ import android.content.pm.PackageManager; import android.content.res.AssetManager; import android.content.res.Configuration; +import android.content.res.Resources; +import android.graphics.Point; import android.graphics.Typeface; import android.inputmethodservice.InputMethodService; import android.net.ConnectivityManager; @@ -32,6 +34,7 @@ import android.os.Build; import android.os.IBinder; import android.text.InputType; +import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.view.Display; @@ -39,6 +42,7 @@ import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.view.WindowMetrics; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; @@ -2005,7 +2009,20 @@ public static void removeKeyboardEventListener(OnKeyboardEventListener listener) } public static int getOrientation(Context context) { - Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + Display display; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + // https://developer.android.com/reference/android/content/Context#getDisplay() + try { + display = context.getDisplay(); + } catch (UnsupportedOperationException e) { + // if the method is called on an instance that is not associated with any display. + return context.getResources().getConfiguration().orientation; + } + } else { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + // Deprecated in API 30 + display = wm.getDefaultDisplay(); + } int rotation = display.getRotation(); if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { return Configuration.ORIENTATION_PORTRAIT; @@ -2052,6 +2069,34 @@ public static void applyKeyboardHeight(Context context, int height) { } } + /** + * Get the size of the area the window would occupy. + * API 30+ + * https://developer.android.com/reference/android/view/WindowManager#getCurrentWindowMetrics() + * @param context + * @return Point (width, height) + */ + public static Point getWindowSize(Context context) { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + WindowMetrics windowMetrics = wm.getCurrentWindowMetrics(); + return new Point( + windowMetrics.getBounds().width(), + windowMetrics.getBounds().height()); + } + + // Deprecated in API 30 + Point size = new Point(0, 0); + wm.getDefaultDisplay().getSize(size); + return size; + } + + public static float getWindowDensity(Context context) { + DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + Log.d(TAG, "KMManager: metrics.density " + metrics.density); + return metrics.density; + } + protected static void setPersistentShouldShowHelpBubble(boolean flag) { SharedPreferences prefs = appContext.getSharedPreferences(appContext.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); diff --git a/android/KMEA/app/src/main/java/com/keyman/engine/KMPopoverView.java b/android/KMEA/app/src/main/java/com/keyman/engine/KMPopoverView.java index 498739c334c..7dcbda5ffdb 100644 --- a/android/KMEA/app/src/main/java/com/keyman/engine/KMPopoverView.java +++ b/android/KMEA/app/src/main/java/com/keyman/engine/KMPopoverView.java @@ -10,6 +10,7 @@ import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; +import android.graphics.Point; import android.graphics.RectF; import android.graphics.Shader; import android.util.AttributeSet; @@ -31,13 +32,11 @@ final class KMPopoverView extends View { public KMPopoverView(Context context, AttributeSet attrs) { super(context, attrs); - WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); - DisplayMetrics metrics = new DisplayMetrics(); - wm.getDefaultDisplay().getMetrics(metrics); - density = metrics.density; + Point size = KMManager.getWindowSize(context); + density = KMManager.getWindowDensity(context); - viewWidth = metrics.widthPixels; - viewHeight = metrics.heightPixels; + viewWidth = size.x; + viewHeight = size.y; borderRadius = 6 * density; strokeWidth = 2.0f; bgColor = context.getResources().getColor(R.color.popup_bg); diff --git a/android/Samples/KMSample2/app/src/main/java/com/keyman/kmsample2/SystemKeyboard.java b/android/Samples/KMSample2/app/src/main/java/com/keyman/kmsample2/SystemKeyboard.java index e4ab04e50f5..4ad462a76c1 100644 --- a/android/Samples/KMSample2/app/src/main/java/com/keyman/kmsample2/SystemKeyboard.java +++ b/android/Samples/KMSample2/app/src/main/java/com/keyman/kmsample2/SystemKeyboard.java @@ -176,9 +176,7 @@ public void onComputeInsets(InputMethodService.Insets outInsets) { super.onComputeInsets(outInsets); // We should extend the touchable region so that Keyman sub keys menu can receive touch events outside the keyboard frame - WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); - Point size = new Point(0, 0); - wm.getDefaultDisplay().getSize(size); + Point size = KMManager.getWindowSize(getApplicationContext()); int inputViewHeight = 0; if (inputView != null) diff --git a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/SystemKeyboard.java b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/SystemKeyboard.java index b2842f00bc2..f047bc6c781 100644 --- a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/SystemKeyboard.java +++ b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/SystemKeyboard.java @@ -202,13 +202,7 @@ public void onComputeInsets(Insets outInsets) { super.onComputeInsets(outInsets); // We should extend the touchable region so that Keyman sub keys menu can receive touch events outside the keyboard frame - WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); - if(wm == null) return; - Point size = new Point(0, 0); - Display display = wm.getDefaultDisplay(); - if(display == null) return; - - display.getSize(size); + Point size = KMManager.getWindowSize(getApplicationContext()); int inputViewHeight = 0; if (inputView != null)