Compare commits

...

3 Commits

Author SHA1 Message Date
Manuel Beck
bc2d92e3d8 feat: improve web view detection on older android versions
- Generated-By: GPT-5.3-Codex, Visual Studio Code Copilot
2026-03-12 12:38:30 +01:00
Manuel Beck
2981046b5b feat: block futher loading, when web view is too old
- Generated-By: GPT-5.3-Codex, Visual Studio Code
2026-03-12 12:37:13 +01:00
Manuel Beck
d49d5c05d3 feat: add web view version check by preference AndroidMinimumWebViewVersion
- If preference `AndroidMinimumWebViewVersion` is set, it will check if the current installed web view suits the version. If not, an error message is shown and the app exists, after the user clicks on OK.
- The strings can be internationalized by adding a `strings.xml` with appropriates strings to the appropriate `values` directory.
- Generated-By: Claude Haiku 4.5, Visual Studio Code
2026-03-12 11:52:03 +01:00
2 changed files with 142 additions and 0 deletions

View File

@@ -48,6 +48,10 @@ import androidx.core.splashscreen.SplashScreen;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.webkit.WebViewCompat;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
/**
* This class is the main Android activity that represents the Cordova
@@ -107,6 +111,11 @@ public class CordovaActivity extends AppCompatActivity {
private boolean canEdgeToEdge = false;
private boolean isFullScreen = false;
/**
* Flag set in {@link #checkWebViewVersion} indicating whether the WebView version
* is below the minimum required version specified in config.xml.
*/
private boolean isWebViewVersionBlocked = false;
/**
* Called when the activity is first created.
@@ -121,6 +130,9 @@ public class CordovaActivity extends AppCompatActivity {
// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
loadConfig();
// Check WebView version requirement
checkWebViewVersion();
canEdgeToEdge = preferences.getBoolean("AndroidEdgeToEdge", false)
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM;
@@ -165,6 +177,10 @@ public class CordovaActivity extends AppCompatActivity {
}
protected void init() {
if (isWebViewVersionBlocked) {
return;
}
appView = makeWebView();
createViews();
if (!appView.isInitialized()) {
@@ -279,6 +295,10 @@ public class CordovaActivity extends AppCompatActivity {
* Load the url into the WebView.
*/
public void loadUrl(String url) {
if (isWebViewVersionBlocked) {
return;
}
if (appView == null) {
init();
}
@@ -588,6 +608,124 @@ public class CordovaActivity extends AppCompatActivity {
}
/**
* Check if the WebView version meets the minimum required version specified by
* preference `AndroidMinimumWebViewVersion` in config.xml.
* If not, display an error dialog and block the app from loading further.
*/
private void checkWebViewVersion() {
String minimumWebViewVersion = preferences.getString("AndroidMinimumWebViewVersion", null);
if (minimumWebViewVersion == null || minimumWebViewVersion.isEmpty()) {
return; // No minimum version requirement set
}
try {
String currentWebViewVersion = getWebViewVersion();
if (currentWebViewVersion != null && !isWebViewVersionSufficient(currentWebViewVersion, minimumWebViewVersion)) {
isWebViewVersionBlocked = true;
String title = getWebViewVersionTitle();
String message = getWebViewVersionMessage();
String button = getWebViewVersionButtonText();
displayError(title, message, button, true);
}
} catch (Exception e) {
LOG.e(TAG, "Error checking WebView version: " + e.getMessage());
}
}
/**
* Get the WebView version check dialog title from string resources.
*/
private String getWebViewVersionTitle() {
int resId = getResources().getIdentifier("webview_version_too_old_title", "string", getPackageName());
return resId != 0 ? getString(resId) : "WebView Update Required";
}
/**
* Get the WebView version check dialog message from string resources.
*/
private String getWebViewVersionMessage() {
int resId = getResources().getIdentifier("webview_version_too_old_message", "string", getPackageName());
return resId != 0 ? getString(resId) : "Your Android System WebView version is too old. Please update it through the Google Play Store.";
}
/**
* Get the WebView version check dialog button text from string resources.
*/
private String getWebViewVersionButtonText() {
int resId = getResources().getIdentifier("webview_version_ok_button", "string", getPackageName());
return resId != 0 ? getString(resId) : "OK";
}
/**
* Get the current WebView version string.
* @return Version string (e.g., "80.0.1234.56") or null if unable to determine
*/
private String getWebViewVersion() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
PackageInfo webViewPackage = WebViewCompat.getCurrentWebViewPackage(this);
if (webViewPackage != null) {
return webViewPackage.versionName;
}
} catch (Exception e) {
LOG.d(TAG, "Could not get WebView version using WebViewCompat: " + e.getMessage());
}
}
// Fallback for older API levels
PackageManager pm = getPackageManager();
String[] fallbackPackages = new String[] {
"com.google.android.webview",
"com.android.webview",
"com.android.chrome"
};
for (String packageName : fallbackPackages) {
try {
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi.versionName;
} catch (PackageManager.NameNotFoundException ignored) {
// Try next package
}
}
LOG.d(TAG, "Could not find WebView package");
return null;
}
/**
* Compare two version strings and determine if current version is >= minimum version.
* @param currentVersion Current version string (e.g., "80.0.1234.56")
* @param minimumVersion Minimum required version string (e.g., "80.0")
* @return true if currentVersion >= minimumVersion
*/
private boolean isWebViewVersionSufficient(String currentVersion, String minimumVersion) {
try {
String[] currentParts = currentVersion.split("\\.");
String[] minimumParts = minimumVersion.split("\\.");
int maxLength = Math.max(currentParts.length, minimumParts.length);
for (int i = 0; i < maxLength; i++) {
int currentPart = i < currentParts.length ? Integer.parseInt(currentParts[i]) : 0;
int minimumPart = i < minimumParts.length ? Integer.parseInt(minimumParts[i]) : 0;
if (currentPart > minimumPart) {
return true;
} else if (currentPart < minimumPart) {
return false;
}
}
return true; // Versions are equal
} catch (NumberFormatException e) {
LOG.e(TAG, "Error parsing version strings: " + e.getMessage());
return true; // If we can't parse, assume it's OK
}
}
/**
* Indicates whether to show the splash screen while the WebView is initially loading.
* <p>

View File

@@ -24,4 +24,8 @@
<string name="launcher_name">@string/app_name</string>
<!-- App label shown on the task switcher. -->
<string name="activity_name">@string/launcher_name</string>
<!-- WebView version check messages -->
<string name="webview_version_too_old_title">WebView Update Required</string>
<string name="webview_version_too_old_message">Your Android System WebView version is too old. Please update it through the Google Play Store.</string>
<string name="webview_version_ok_button">OK</string>
</resources>