Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.getcapacitor.WebViewListener;
import com.getcapacitor.annotation.CapacitorPlugin;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@CapacitorPlugin
public class SystemBars extends Plugin {
Expand All @@ -38,6 +40,7 @@ public class SystemBars extends Plugin {

// https://issues.chromium.org/issues/40699457
private static final int WEBVIEW_VERSION_WITH_SAFE_AREA_FIX = 140;
private static final Pattern WEBVIEW_MAJOR_VERSION_PATTERN = Pattern.compile("\\d+");

static final String viewportMetaJSFunction = """
function capacitorSystemBarsCheckMetaViewport() {
Expand Down Expand Up @@ -360,12 +363,24 @@ public int getThemeColor(Context context, int attrRes) {

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 info != null ? parseWebViewMajorVersion(info.versionName) : 0;
}

static int parseWebViewMajorVersion(String versionName) {
if (versionName == null) {
return 0;
}

return 0;
Matcher matcher = WEBVIEW_MAJOR_VERSION_PATTERN.matcher(versionName);
if (!matcher.find()) {
return 0;
}

try {
return Integer.parseInt(matcher.group());
} catch (NumberFormatException e) {
return 0;
}
}

private int getBottomInset(Insets systemBarsInsets, Insets imeInsets, boolean keyboardVisible) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,41 @@ public void bottomInsetIsNavigationBarWhenFloatingKeyboardCoversNothing() throws
assertEquals(63, invokeGetBottomInset(63, 0, true));
}

@Test
public void webViewMajorVersionIsParsedFromReleaseVersionName() {
assertEquals(140, SystemBars.parseWebViewMajorVersion("140.0.7339.207"));
}

@Test
public void webViewMajorVersionIsZeroForDeveloperBuild() {
assertEquals(0, SystemBars.parseWebViewMajorVersion("Developer Build"));
}

@Test
public void webViewMajorVersionIsZeroForEmptyVersionName() {
assertEquals(0, SystemBars.parseWebViewMajorVersion(""));
}

@Test
public void webViewMajorVersionIsZeroForDotOnlyVersionName() {
assertEquals(0, SystemBars.parseWebViewMajorVersion("."));
}

@Test
public void webViewMajorVersionIgnoresLeadingWhitespace() {
assertEquals(140, SystemBars.parseWebViewMajorVersion(" 140.0.7339.207"));
}

@Test
public void webViewMajorVersionIsZeroWhenNumberOverflows() {
assertEquals(0, SystemBars.parseWebViewMajorVersion("99999999999.0"));
}

@Test
public void webViewMajorVersionIsZeroForNullVersionName() {
assertEquals(0, SystemBars.parseWebViewMajorVersion(null));
}

private int invokeGetBottomInset(int navigationBarBottom, int imeBottom, boolean keyboardVisible) throws Exception {
Method getBottomInset = SystemBars.class.getDeclaredMethod("getBottomInset", Insets.class, Insets.class, boolean.class);
getBottomInset.setAccessible(true);
Expand Down
Loading