Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): Fix for #6258, Add support for modern Huawei devices #6402

Merged
merged 10 commits into from
Mar 27, 2023
7 changes: 7 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class Bridge {
public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_";
public static final int DEFAULT_ANDROID_WEBVIEW_VERSION = 60;
public static final int MINIMUM_ANDROID_WEBVIEW_VERSION = 55;
public static final int DEFAULT_HUAWEI_WEBVIEW_VERSION = 10;
public static final int MINIMUM_HUAWEI_WEBVIEW_VERSION = 10;

// Loaded Capacitor config
private CapConfig config;
Expand Down Expand Up @@ -324,6 +326,11 @@ public boolean isMinimumWebViewInstalled() {
// Check getCurrentWebViewPackage() directly if above Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PackageInfo info = WebView.getCurrentWebViewPackage();
if (info.packageName.equals("com.huawei.webview")) {
String majorVersionStr = info.versionName.split("\\.")[0];
int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion >= config.getMinHuaweiWebViewVersion();
}
String majorVersionStr = info.versionName.split("\\.")[0];
int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion >= config.getMinWebViewVersion();
Expand Down
15 changes: 15 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static com.getcapacitor.Bridge.CAPACITOR_HTTP_SCHEME;
import static com.getcapacitor.Bridge.DEFAULT_ANDROID_WEBVIEW_VERSION;
import static com.getcapacitor.Bridge.DEFAULT_HUAWEI_WEBVIEW_VERSION;
import static com.getcapacitor.Bridge.MINIMUM_ANDROID_WEBVIEW_VERSION;
import static com.getcapacitor.Bridge.MINIMUM_HUAWEI_WEBVIEW_VERSION;
import static com.getcapacitor.FileUtils.readFileFromAssets;

import android.content.Context;
Expand Down Expand Up @@ -48,6 +50,7 @@ public class CapConfig {
private boolean initialFocus = true;
private boolean useLegacyBridge = false;
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;
private int minHuaweiWebViewVersion = DEFAULT_HUAWEI_WEBVIEW_VERSION;
private String errorPath;

// Embedded
Expand Down Expand Up @@ -172,6 +175,7 @@ private CapConfig(Builder builder) {
this.initialFocus = builder.initialFocus;
this.useLegacyBridge = builder.useLegacyBridge;
this.minWebViewVersion = builder.minWebViewVersion;
this.minHuaweiWebViewVersion = builder.minHuaweiWebViewVersion;
this.errorPath = builder.errorPath;

// Embedded
Expand Down Expand Up @@ -263,6 +267,7 @@ private void deserializeConfig(@Nullable Context context) {
JSONUtils.getBoolean(configJSON, "allowMixedContent", allowMixedContent)
);
minWebViewVersion = JSONUtils.getInt(configJSON, "android.minWebViewVersion", DEFAULT_ANDROID_WEBVIEW_VERSION);
minHuaweiWebViewVersion = JSONUtils.getInt(configJSON, "android.minHuaweiWebViewVersion", DEFAULT_HUAWEI_WEBVIEW_VERSION);
captureInput = JSONUtils.getBoolean(configJSON, "android.captureInput", captureInput);
useLegacyBridge = JSONUtils.getBoolean(configJSON, "android.useLegacyBridge", useLegacyBridge);
webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", isDebug);
Expand Down Expand Up @@ -376,6 +381,15 @@ public int getMinWebViewVersion() {
return minWebViewVersion;
}

public int getMinHuaweiWebViewVersion() {
if (minHuaweiWebViewVersion < MINIMUM_HUAWEI_WEBVIEW_VERSION) {
Logger.warn("Specified minimum Huawei webview version is too low, defaulting to " + MINIMUM_HUAWEI_WEBVIEW_VERSION);
return MINIMUM_HUAWEI_WEBVIEW_VERSION;
}

return minHuaweiWebViewVersion;
}

public PluginConfig getPluginConfiguration(String pluginId) {
PluginConfig pluginConfig = pluginsConfiguration.get(pluginId);
if (pluginConfig == null) {
Expand Down Expand Up @@ -535,6 +549,7 @@ public static class Builder {
private boolean initialFocus = false;
private boolean useLegacyBridge = false;
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;
private int minHuaweiWebViewVersion = DEFAULT_HUAWEI_WEBVIEW_VERSION;

// Embedded
private String startPath = null;
Expand Down
14 changes: 14 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ export interface CapacitorConfig {
*/
minWebViewVersion?: number;

/**
* The minimum supported Huawei webview version on Android supported by your app.
*
* The minimum supported cannot be lower than version `10`, which is required for Capacitor.
*
* If the device uses a lower WebView version, an error message will be shown on Logcat.
* If `server.errorPath` is configured, the WebView will redirect to that file, so can be
* used to show a custom error.
*
* @since 4.6.4
* @default 10
*/
minHuaweiWebViewVersion?: number;

buildOptions?: {
/**
* Path to your keystore
Expand Down