Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.getcapacitor.plugin;

import android.content.pm.PackageInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.View;
Expand All @@ -12,6 +13,7 @@
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.webkit.WebViewCompat;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
Expand Down Expand Up @@ -145,6 +147,9 @@ public void onDOMReady() {

private Insets calcSafeAreaInsets(WindowInsetsCompat insets) {
Insets safeArea = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
if (insets.isVisible(WindowInsetsCompat.Type.ime())) {
return Insets.of(safeArea.left, safeArea.top, safeArea.right, 0);
}
return Insets.of(safeArea.left, safeArea.top, safeArea.right, safeArea.bottom);
}

Expand All @@ -162,21 +167,25 @@ private void initSafeAreaInsets() {
private void initWindowInsetsListener() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM && insetHandlingEnabled) {
ViewCompat.setOnApplyWindowInsetsListener((View) getBridge().getWebView().getParent(), (v, insets) -> {
boolean hasBrokenWebViewVersion = getWebViewMajorVersion() <= 139;

if (hasViewportCover) {
Insets safeAreaInsets = calcSafeAreaInsets(insets);
boolean keyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime());

if (keyboardVisible) {
safeAreaInsets = Insets.of(safeAreaInsets.left, safeAreaInsets.top, safeAreaInsets.right, 0);
injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
}

Insets imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime());
setViewMargins(v, Insets.of(0, 0, 0, imeInsets.bottom));
} else {
setViewMargins(v, Insets.NONE);
if (hasBrokenWebViewVersion) {
if (hasViewportCover && v.hasWindowFocus() && v.isShown()) {
boolean keyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime());
if (keyboardVisible) {
Insets imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime());
setViewMargins(v, Insets.of(0, 0, 0, imeInsets.bottom));
} else {
setViewMargins(v, Insets.NONE);
}

return WindowInsetsCompat.CONSUMED;
}

injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
return WindowInsetsCompat.CONSUMED;
}

return insets;
Expand Down Expand Up @@ -272,4 +281,14 @@ private String getStyleForTheme() {
}
return STYLE_DARK;
}

private Integer getWebViewMajorVersion() {
PackageInfo info = WebViewCompat.getCurrentWebViewPackage(getContext());
if (info != null && info.versionName != null) {
String[] versionSegments = info.versionName.split("\\.");
return Integer.valueOf(versionSegments[0]);
}

return 0;
}
}