Skip to content

Commit

Permalink
fixup! 15: subtract width of screen cutouts when calculating keyboard…
Browse files Browse the repository at this point in the history
… width
  • Loading branch information
muhomorr authored and thestinger committed Oct 22, 2024
1 parent bdc88aa commit 3b73224
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions java/src/com/android/inputmethod/latin/utils/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Insets;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.Surface;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.WindowMetrics;

import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.R;
Expand Down Expand Up @@ -186,14 +190,21 @@ private static boolean fulfillsCondition(final HashMap<String,String> keyValuePa
}

public static int getDefaultKeyboardWidth(final Resources res, final Context context) {
final DisplayMetrics dm = res.getDisplayMetrics();
int width = dm.widthPixels;
Display display = context.getDisplay();
DisplayCutout cutout = display.getCutout();
if (cutout != null) {
width -= cutout.getSafeInsetLeft() + cutout.getSafeInsetRight();
if (Build.VERSION.SDK_INT < 35) {
final DisplayMetrics dm = res.getDisplayMetrics();
return dm.widthPixels;
}
return width;

// Since Android 15, insets aren't subtracted from DisplayMetrics.widthPixels, despite
// targetSdk remaining set to 30.
WindowManager wm = context.getSystemService(WindowManager.class);
WindowMetrics windowMetrics = wm.getCurrentWindowMetrics();
Rect windowBounds = windowMetrics.getBounds();
WindowInsets windowInsets = windowMetrics.getWindowInsets();

int insetTypes = WindowInsets.Type.systemBars() | WindowInsets.Type.displayCutout();
Insets insets = windowInsets.getInsetsIgnoringVisibility(insetTypes);
return windowBounds.width() - insets.left - insets.right;
}

public static int getKeyboardHeight(final Resources res, final SettingsValues settingsValues) {
Expand Down

0 comments on commit 3b73224

Please # to comment.