Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for broken keyboardHeight calculation when the device has display cutouts #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/android/CDVIonicKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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...
Expand All @@ -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);
Expand Down Expand Up @@ -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<Rect> list = displayCutout.getBoundingRects();
for (Rect rect : list) {
if (rect.top == 0) {
cutOffHeight += rect.bottom - rect.top;
}
}
}

}
return cutOffHeight;
}
};

mChildOfContent = content.getChildAt(0);
Expand Down