From 2f4bd94a8aaa28c3d890788ab608323bdd818132 Mon Sep 17 00:00:00 2001 From: "angel.teran" Date: Sat, 1 Jun 2019 11:08:13 +0200 Subject: [PATCH] Check if DisplayCutouts is not null. --- src/android/SafeInsets.java | 64 +++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/src/android/SafeInsets.java b/src/android/SafeInsets.java index eea9bc4..746df85 100644 --- a/src/android/SafeInsets.java +++ b/src/android/SafeInsets.java @@ -44,8 +44,6 @@ public class SafeInsets extends CordovaPlugin { private final JSONObject insetsJSON = new JSONObject(); - private float density; - @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); @@ -54,29 +52,39 @@ public class SafeInsets extends CordovaPlugin { final Activity activity = cordova.getActivity(); final Window window = activity.getWindow(); final View rootView = window.getDecorView().getRootView(); - - density = activity.getResources().getDisplayMetrics().density; + final float density = activity.getResources().getDisplayMetrics().density; rootView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @Override public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) { - if (Build.VERSION.SDK_INT < 28) { - updateInsets( - insets.getSystemWindowInsetTop(), - insets.getSystemWindowInsetRight(), - insets.getSystemWindowInsetBottom(), - insets.getStableInsetLeft() - ); - } else { - DisplayCutout cutout = insets.getDisplayCutout(); - updateInsets( - cutout.getSafeInsetTop(), - cutout.getSafeInsetRight(), - cutout.getSafeInsetBottom(), - cutout.getSafeInsetLeft() - ); - } rootView.onApplyWindowInsets(insets); + + int top = insets.getSystemWindowInsetTop(), + right = insets.getSystemWindowInsetRight(), + bottom = insets.getSystemWindowInsetBottom(), + left = insets.getSystemWindowInsetLeft(); + + if (Build.VERSION.SDK_INT >= 28) { + DisplayCutout cutout = insets.getDisplayCutout(); + if (cutout != null) { + top = cutout.getSafeInsetTop(); + right = cutout.getSafeInsetRight(); + bottom = cutout.getSafeInsetBottom(); + left = cutout.getSafeInsetLeft(); + } + } + + try { + insetsJSON.put("top", top / density); + insetsJSON.put("bottom", bottom / density); + insetsJSON.put("left", left / density); + insetsJSON.put("right", right / density); + } catch (JSONException ex) {} + + if (callbackContext != null) { + sendInsets(); + } + return insets; } }); @@ -88,27 +96,13 @@ public class SafeInsets extends CordovaPlugin { @Override public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) { if ("check".equals(action)) { - PluginResult result = new PluginResult(PluginResult.Status.OK, insetsJSON); - result.setKeepCallback(true); this.callbackContext = callbackContext; - callbackContext.sendPluginResult(result); + sendInsets(); return true; } return false; } - private void updateInsets(int top, int right, int bottom, int left) { - try { - insetsJSON.put("top", top / density); - insetsJSON.put("bottom", bottom / density); - insetsJSON.put("left", left / density); - insetsJSON.put("right", right / density); - } catch (JSONException ex) {} - if (callbackContext != null) { - sendInsets(); - } - } - private void sendInsets() { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, insetsJSON); pluginResult.setKeepCallback(true);