Skip to content
Merged
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
66 changes: 10 additions & 56 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand Down Expand Up @@ -51,7 +50,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
Expand Down Expand Up @@ -294,63 +292,19 @@ public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {

@SuppressLint("WebViewApiAvailability")
public boolean isMinimumWebViewInstalled() {
PackageManager pm = getContext().getPackageManager();

// Check getCurrentWebViewPackage() directly if above Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PackageInfo info = WebView.getCurrentWebViewPackage();
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(info.versionName);
if (matcher.find()) {
String majorVersionStr = matcher.group(0);
int majorVersion = Integer.parseInt(majorVersionStr);
if (info.packageName.equals("com.huawei.webview")) {
return majorVersion >= config.getMinHuaweiWebViewVersion();
}
return majorVersion >= config.getMinWebViewVersion();
} else {
return false;
}
}

// Otherwise manually check WebView versions
try {
PackageInfo info = InternalUtils.getPackageInfo(pm, "com.android.chrome");
String majorVersionStr = info.versionName.split("\\.")[0];
int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion >= config.getMinWebViewVersion();
} catch (Exception ex) {
Logger.warn("Unable to get package info for 'com.google.android.webview'" + ex.toString());
}

try {
PackageInfo info = InternalUtils.getPackageInfo(pm, "com.android.webview");
String majorVersionStr = info.versionName.split("\\.")[0];
PackageInfo info = WebView.getCurrentWebViewPackage();
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(info.versionName);
if (matcher.find()) {
String majorVersionStr = matcher.group(0);
int majorVersion = Integer.parseInt(majorVersionStr);
if (info.packageName.equals("com.huawei.webview")) {
return majorVersion >= config.getMinHuaweiWebViewVersion();
}
return majorVersion >= config.getMinWebViewVersion();
} catch (Exception ex) {
Logger.warn("Unable to get package info for 'com.android.webview'" + ex.toString());
}

final int amazonFireMajorWebViewVersion = extractWebViewMajorVersion(pm, "com.amazon.webview.chromium");
if (amazonFireMajorWebViewVersion >= config.getMinWebViewVersion()) {
return true;
}

// Could not detect any webview, return false
return false;
}

private int extractWebViewMajorVersion(final PackageManager pm, final String webViewPackageName) {
try {
final PackageInfo info = InternalUtils.getPackageInfo(pm, webViewPackageName);
final String majorVersionStr = info.versionName.split("\\.")[0];
final int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion;
} catch (Exception ex) {
Logger.warn(String.format("Unable to get package info for '%s' with err '%s'", webViewPackageName, ex));
} else {
return false;
}
return 0;
}

public boolean launchIntent(Uri url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.getcapacitor.plugin.util;

import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import com.getcapacitor.Bridge;
Expand Down Expand Up @@ -211,9 +210,7 @@ public void setRequestBody(PluginCall call, JSValue body, String bodyType) throw
this.writeRequestBody(dataString != null ? dataString : "");
} else if (bodyType != null && bodyType.equals("file")) {
try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
os.write(Base64.getDecoder().decode(body.toString()));
}
os.write(Base64.getDecoder().decode(body.toString()));
os.flush();
}
} else if (contentType.contains("application/x-www-form-urlencoded")) {
Expand Down Expand Up @@ -295,13 +292,7 @@ private void writeFormDataRequestBody(String boundary, JSArray entries) throws I
os.writeBytes("Content-Type: " + fileContentType + lineEnd);
os.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
os.writeBytes(lineEnd);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
os.write(Base64.getDecoder().decode(value));
} else {
os.write(android.util.Base64.decode(value, android.util.Base64.DEFAULT));
}

os.write(Base64.getDecoder().decode(value));
os.writeBytes(lineEnd);
}
}
Expand Down
Loading