Skip to content

Commit

Permalink
Merge pull request #11436 from keymanapp/fix/android/deprecated-get-size
Browse files Browse the repository at this point in the history
fix(android): Replace deprecated APIs for Display, Size, Metrics
  • Loading branch information
darcywong00 authored May 17, 2024
2 parents c9cbc85 + 0be1937 commit 73ca23b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 46 additions & 1 deletion android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@
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;
import android.net.NetworkInfo;
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;
import android.view.Surface;
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
// Deprecated in API 30
Point size = new Point(0, 0);
wm.getDefaultDisplay().getSize(size);
return size;
}

WindowMetrics windowMetrics = wm.getCurrentWindowMetrics();
return new Point(
windowMetrics.getBounds().width(),
windowMetrics.getBounds().height());
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 73ca23b

Please sign in to comment.