diff --git a/src/android/CDVIonicKeyboard.java b/src/android/CDVIonicKeyboard.java index b7ec455..c56238c 100644 --- a/src/android/CDVIonicKeyboard.java +++ b/src/android/CDVIonicKeyboard.java @@ -12,9 +12,11 @@ import android.content.Context; import android.graphics.Rect; import android.util.DisplayMetrics; +import android.view.DisplayCutout; import android.view.View; import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnGlobalLayoutListener; +import android.view.WindowInsets; import android.view.inputmethod.InputMethodManager; // import additionally required classes for calculating screen height @@ -23,6 +25,8 @@ import android.os.Build; import android.widget.FrameLayout; +import java.util.List; + public class CDVIonicKeyboard extends CordovaPlugin { private OnGlobalLayoutListener list; private View rootView; @@ -64,7 +68,7 @@ public void run() { if ("init".equals(action)) { cordova.getThreadPool().execute(new Runnable() { public void run() { - //calculate density-independent pixels (dp) + //calculate density-independent pixels (dp) //http://developer.android.com/guide/practices/screens_support.html DisplayMetrics dm = new DisplayMetrics(); cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); @@ -104,7 +108,7 @@ public void onGlobalLayout() { screenHeight = rootViewHeight; } - int heightDiff = screenHeight - resultBottom; + int heightDiff = screenHeight + topCutoutHeight() - resultBottom; int pixelHeightDiff = (int)(heightDiff / density); if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard... @@ -114,7 +118,7 @@ public void onGlobalLayout() { callbackContext.sendPluginResult(result); } else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){ - String msg = "H"; + String msg = "H"; result = new PluginResult(PluginResult.Status.OK, msg); result.setKeepCallback(true); callbackContext.sendPluginResult(result); @@ -142,6 +146,26 @@ private int computeUsableHeight() { mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top); } + + private int topCutoutHeight() { + View decorView = cordova.getActivity().getWindow().getDecorView(); + + int cutOffHeight = 0; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) { + WindowInsets windowInsets = decorView.getRootWindowInsets(); + DisplayCutout displayCutout = windowInsets.getDisplayCutout(); + if (displayCutout != null) { + List list = displayCutout.getBoundingRects(); + for (Rect rect : list) { + if (rect.top == 0) { + cutOffHeight += rect.bottom - rect.top; + } + } + } + + } + return cutOffHeight; + } }; mChildOfContent = content.getChildAt(0);