From febe17d7ec1b016fb82afb49705c08bb1e9eb350 Mon Sep 17 00:00:00 2001 From: Zakaria El Marouri Date: Thu, 16 Mar 2023 11:31:19 +0700 Subject: [PATCH 1/3] support huawei devices --- .../src/main/java/com/getcapacitor/Bridge.java | 15 ++++++++++----- .../src/main/java/com/getcapacitor/CapConfig.java | 15 +++++++++++++++ cli/src/declarations.ts | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 3bcae73f55..8db3166cd3 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -32,7 +32,6 @@ import com.getcapacitor.cordova.MockCordovaInterfaceImpl; import com.getcapacitor.cordova.MockCordovaWebViewImpl; import com.getcapacitor.util.HostMask; -import com.getcapacitor.util.InternalUtils; import com.getcapacitor.util.PermissionHelper; import com.getcapacitor.util.WebColor; import java.io.File; @@ -88,6 +87,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; @@ -324,6 +325,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(); @@ -335,7 +341,7 @@ public boolean isMinimumWebViewInstalled() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { webViewPackage = "com.android.chrome"; } - PackageInfo info = InternalUtils.getPackageInfo(pm, webViewPackage); + PackageInfo info = pm.getPackageInfo(webViewPackage, 0); String majorVersionStr = info.versionName.split("\\.")[0]; int majorVersion = Integer.parseInt(majorVersionStr); return majorVersion >= config.getMinWebViewVersion(); @@ -344,7 +350,7 @@ public boolean isMinimumWebViewInstalled() { } try { - PackageInfo info = InternalUtils.getPackageInfo(pm, "com.android.webview"); + PackageInfo info = pm.getPackageInfo("com.android.webview", 0); String majorVersionStr = info.versionName.split("\\.")[0]; int majorVersion = Integer.parseInt(majorVersionStr); return majorVersion >= config.getMinWebViewVersion(); @@ -391,8 +397,7 @@ private boolean isNewBinary() { String lastVersionName = prefs.getString(LAST_BINARY_VERSION_NAME, null); try { - PackageManager pm = getContext().getPackageManager(); - PackageInfo pInfo = InternalUtils.getPackageInfo(pm, getContext().getPackageName()); + PackageInfo pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0); versionCode = Integer.toString((int) PackageInfoCompat.getLongVersionCode(pInfo)); versionName = pInfo.versionName; } catch (Exception ex) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java index 2f417a7f58..447a59b985 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java +++ b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java @@ -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; @@ -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 @@ -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 @@ -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); @@ -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) { @@ -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; diff --git a/cli/src/declarations.ts b/cli/src/declarations.ts index b8b5c96e44..eaeeceaa59 100644 --- a/cli/src/declarations.ts +++ b/cli/src/declarations.ts @@ -226,6 +226,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 From 7ff97eba9b8f106e5fa3dc18f49e28a84b98a887 Mon Sep 17 00:00:00 2001 From: Zakaria El Marouri Date: Wed, 22 Mar 2023 19:32:12 +0700 Subject: [PATCH 2/3] fix following markemer comments --- .../capacitor/src/main/java/com/getcapacitor/Bridge.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 8db3166cd3..38cc063baf 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -341,7 +341,7 @@ public boolean isMinimumWebViewInstalled() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { webViewPackage = "com.android.chrome"; } - PackageInfo info = pm.getPackageInfo(webViewPackage, 0); + PackageInfo info = InternalUtils.getPackageInfo(pm, webViewPackage); String majorVersionStr = info.versionName.split("\\.")[0]; int majorVersion = Integer.parseInt(majorVersionStr); return majorVersion >= config.getMinWebViewVersion(); @@ -350,7 +350,7 @@ public boolean isMinimumWebViewInstalled() { } try { - PackageInfo info = pm.getPackageInfo("com.android.webview", 0); + PackageInfo info = InternalUtils.getPackageInfo(pm, "com.android.webview"); String majorVersionStr = info.versionName.split("\\.")[0]; int majorVersion = Integer.parseInt(majorVersionStr); return majorVersion >= config.getMinWebViewVersion(); @@ -397,7 +397,8 @@ private boolean isNewBinary() { String lastVersionName = prefs.getString(LAST_BINARY_VERSION_NAME, null); try { - PackageInfo pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0); + PackageManager pm = getContext().getPackageManager(); + PackageInfo pInfo = InternalUtils.getPackageInfo(pm, getContext().getPackageName()); versionCode = Integer.toString((int) PackageInfoCompat.getLongVersionCode(pInfo)); versionName = pInfo.versionName; } catch (Exception ex) { From d668d4fa064a51efb4bcb9d6308a17f030371a02 Mon Sep 17 00:00:00 2001 From: Zakaria El Marouri Date: Wed, 22 Mar 2023 19:39:10 +0700 Subject: [PATCH 3/3] restore missing import --- android/capacitor/src/main/java/com/getcapacitor/Bridge.java | 1 + 1 file changed, 1 insertion(+) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index b5907b00a5..b4319b972b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -32,6 +32,7 @@ import com.getcapacitor.cordova.MockCordovaInterfaceImpl; import com.getcapacitor.cordova.MockCordovaWebViewImpl; import com.getcapacitor.util.HostMask; +import com.getcapacitor.util.InternalUtils; import com.getcapacitor.util.PermissionHelper; import com.getcapacitor.util.WebColor; import java.io.File;