From e4ad1ee6c92647ec688e682ace3e372bb2b5e2fd Mon Sep 17 00:00:00 2001 From: Menardi Date: Mon, 11 Dec 2023 14:50:40 +0000 Subject: [PATCH] Add support for insets on Android 9 and below --- .../statusbar/safearea/SafeAreaPlugin.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/dev/ashu/capacitor/statusbar/safearea/SafeAreaPlugin.java b/android/src/main/java/dev/ashu/capacitor/statusbar/safearea/SafeAreaPlugin.java index 4dd3e11..6f71669 100644 --- a/android/src/main/java/dev/ashu/capacitor/statusbar/safearea/SafeAreaPlugin.java +++ b/android/src/main/java/dev/ashu/capacitor/statusbar/safearea/SafeAreaPlugin.java @@ -38,8 +38,10 @@ public void getSafeAreaInsets(PluginCall call) { Resources res = getActivity().getApplicationContext().getResources(); WindowInsets windowInsets = getActivity().getWindow().getDecorView().getRootWindowInsets(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if (windowInsets != null) { + if (windowInsets != null) { + float density = res.getDisplayMetrics().density; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { final DisplayCutout cutout = windowInsets.getDisplayCutout(); leftInset = cutout != null ? cutout.getSafeInsetLeft() : 0; @@ -48,10 +50,15 @@ public void getSafeAreaInsets(PluginCall call) { bottomInset = cutout != null ? cutout.getSafeInsetBottom() : 0; Insets insets = windowInsets.getSystemWindowInsets(); - leftInset = Math.max(leftInset, insets.left) / res.getDisplayMetrics().density; - rightInset = Math.max(rightInset, insets.right) / res.getDisplayMetrics().density; - topInset = Math.max(topInset, insets.top) / res.getDisplayMetrics().density; - bottomInset = Math.max(bottomInset, insets.bottom) / res.getDisplayMetrics().density; + leftInset = Math.max(leftInset, insets.left) / density; + rightInset = Math.max(rightInset, insets.right) / density; + topInset = Math.max(topInset, insets.top) / density; + bottomInset = Math.max(bottomInset, insets.bottom) / density; + } else { + leftInset = windowInsets.getSystemWindowInsetLeft() / density; + rightInset = windowInsets.getSystemWindowInsetRight() / density; + topInset = windowInsets.getSystemWindowInsetTop() / density; + bottomInset = windowInsets.getSystemWindowInsetBottom() / density; } } }