Skip to content

Commit

Permalink
Updated code docs, Using Android WebViewClientCompat for Chromium-bas…
Browse files Browse the repository at this point in the history
…ed WebView if the WebView package major version is >= 73 (https://bugs.chromium.org/p/chromium/issues/detail?id=925887), fix #1422
  • Loading branch information
pichillilorenzo committed Dec 3, 2022
1 parent fc98712 commit 459875f
Show file tree
Hide file tree
Showing 161 changed files with 6,328 additions and 5,103 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 6.0.0-beta.20

- Using Android `WebViewClientCompat` for Chromium-based WebView if the WebView package major version is >= 73 (https://bugs.chromium.org/p/chromium/issues/detail?id=925887)
- Updated code docs
- Fixed "Unexpected addWebMessageListener behaviour" [#1422](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1422)

## 6.0.0-beta.19

- Updated code docs
Expand Down Expand Up @@ -164,6 +170,10 @@
- Removed `URLProtectionSpace.iosIsProxy` property
- `historyUrl` and `baseUrl` of `InAppWebViewInitialData` can be `null`

## 5.7.2+2

- Fixed "Unexpected addWebMessageListener behaviour" [#1422](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1422)

## 5.7.2+1

- Fixed "Cannot Grant Permission at Android 21" [#1447](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1447)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pichillilorenzo.flutter_inappwebview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
Expand Down Expand Up @@ -82,28 +83,15 @@ public void onReceiveValue(Boolean value) {
result.success(false);
break;
case "getCurrentWebViewPackage":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && plugin != null && (plugin.activity != null || plugin.applicationContext != null)) {
Context context = plugin.activity;
Context context = null;
if (plugin != null) {
context = plugin.activity;
if (context == null) {
context = plugin.applicationContext;
}
result.success(convertWebViewPackageToMap(WebViewCompat.getCurrentWebViewPackage(context)));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//with Android Lollipop (API 21) they started to update the WebView
//as a separate APK with the PlayStore and they added the
//getLoadedPackageInfo() method to the WebViewFactory class and this
//should handle the Android 7.0 behaviour changes too
try {
Class webViewFactory = Class.forName("android.webkit.WebViewFactory");
Method method = webViewFactory.getMethod("getLoadedPackageInfo");
PackageInfo pInfo = (PackageInfo) method.invoke(null);
result.success(convertWebViewPackageToMap(pInfo));
} catch (Exception e) {
result.success(null);
}
} else {
result.success(null);
}
PackageInfo packageInfo = context != null ? WebViewCompat.getCurrentWebViewPackage(context) : null;
result.success(packageInfo != null ? convertWebViewPackageToMap(packageInfo) : null);
break;
case "setWebContentsDebuggingEnabled":
{
Expand Down Expand Up @@ -135,10 +123,8 @@ public void onReceiveValue(Boolean value) {
}
}

public Map<String, Object> convertWebViewPackageToMap(PackageInfo webViewPackageInfo) {
if (webViewPackageInfo == null) {
return null;
}
@NonNull
public Map<String, Object> convertWebViewPackageToMap(@NonNull PackageInfo webViewPackageInfo) {
HashMap<String, Object> webViewPackageInfoMap = new HashMap<>();

webViewPackageInfoMap.put("versionName", webViewPackageInfo.versionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import android.os.Build;
import android.webkit.WebResourceError;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.webkit.WebResourceErrorCompat;
import androidx.webkit.WebViewFeature;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -24,6 +27,19 @@ static public WebResourceErrorExt fromWebResourceError(@NonNull WebResourceError
return new WebResourceErrorExt(error.getErrorCode(), error.getDescription().toString());
}

@RequiresApi(Build.VERSION_CODES.M)
static public WebResourceErrorExt fromWebResourceError(@NonNull WebResourceErrorCompat error) {
int type = -1;
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_RESOURCE_ERROR_GET_CODE)) {
type = error.getErrorCode();
}
String description = "";
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_RESOURCE_ERROR_GET_DESCRIPTION)) {
description = error.getDescription().toString();
}
return new WebResourceErrorExt(type, description);
}

public Map<String, Object> toMap() {
Map<String, Object> webResourceErrorMap = new HashMap<>();
webResourceErrorMap.put("type", getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public void onPageFinished(WebView view, String url) {
if (webView.inAppWebViewChromeClient != null) {
webView.inAppWebViewChromeClient.dispose();
}
if (webView.inAppWebViewClientCompat != null) {
webView.inAppWebViewClientCompat.dispose();
}
if (webView.inAppWebViewClient != null) {
webView.inAppWebViewClient.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -47,6 +48,7 @@
import android.webkit.WebHistoryItem;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebViewClient;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
Expand All @@ -59,6 +61,7 @@
import androidx.webkit.WebViewFeature;

import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin;
import com.pichillilorenzo.flutter_inappwebview.InAppWebViewStatic;
import com.pichillilorenzo.flutter_inappwebview.R;
import com.pichillilorenzo.flutter_inappwebview.Util;
import com.pichillilorenzo.flutter_inappwebview.content_blocker.ContentBlocker;
Expand Down Expand Up @@ -124,6 +127,8 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie
@Nullable
public InAppWebViewClient inAppWebViewClient;
@Nullable
public InAppWebViewClientCompat inAppWebViewClientCompat;
@Nullable
public InAppWebViewChromeClient inAppWebViewChromeClient;
@Nullable
public InAppWebViewRenderProcessClient inAppWebViewRenderProcessClient;
Expand Down Expand Up @@ -199,6 +204,36 @@ public InAppWebView(Context context, @NonNull InAppWebViewFlutterPlugin plugin,
}
}

public WebViewClient createWebViewClient(InAppBrowserDelegate inAppBrowserDelegate) {
// bug https://bugs.chromium.org/p/chromium/issues/detail?id=925887
PackageInfo packageInfo = WebViewCompat.getCurrentWebViewPackage(getContext());
if (packageInfo == null) {
Log.d(LOG_TAG, "Using InAppWebViewClient implementation");
return new InAppWebViewClient(inAppBrowserDelegate);
}

boolean isChromiumWebView = "com.android.webview".equals(packageInfo.packageName) ||
"com.google.android.webview".equals(packageInfo.packageName) ||
"com.android.chrome".equals(packageInfo.packageName);
boolean isChromiumWebViewBugFixed = false;
if (isChromiumWebView) {
String versionName = packageInfo.versionName != null ? packageInfo.versionName : "";
try {
int majorVersion = versionName.contains(".") ?
Integer.parseInt(versionName.split("\\.")[0]) : 0;
isChromiumWebViewBugFixed = majorVersion >= 73;
} catch (NumberFormatException ignored) {}
}

if (isChromiumWebViewBugFixed || !isChromiumWebView) {
Log.d(LOG_TAG, "Using InAppWebViewClientCompat implementation");
return new InAppWebViewClientCompat(inAppBrowserDelegate);
} else {
Log.d(LOG_TAG, "Using InAppWebViewClient implementation");
return new InAppWebViewClient(inAppBrowserDelegate);
}
}

@SuppressLint("RestrictedApi")
public void prepare() {
if (plugin != null) {
Expand All @@ -211,8 +246,14 @@ public void prepare() {
inAppWebViewChromeClient = new InAppWebViewChromeClient(plugin, this, inAppBrowserDelegate);
setWebChromeClient(inAppWebViewChromeClient);

inAppWebViewClient = new InAppWebViewClient(inAppBrowserDelegate);
setWebViewClient(inAppWebViewClient);
WebViewClient webViewClient = createWebViewClient(inAppBrowserDelegate);
if (webViewClient instanceof InAppWebViewClientCompat) {
inAppWebViewClientCompat = (InAppWebViewClientCompat) webViewClient;
setWebViewClient(inAppWebViewClientCompat);
} else if (webViewClient instanceof InAppWebViewClient) {
inAppWebViewClient = (InAppWebViewClient) webViewClient;
setWebViewClient(inAppWebViewClient);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && WebViewFeature.isFeatureSupported(WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE)) {
inAppWebViewRenderProcessClient = new InAppWebViewRenderProcessClient();
Expand Down Expand Up @@ -1982,6 +2023,7 @@ public void dispose() {
evaluateJavaScriptContentWorldCallbacks.clear();
inAppBrowserDelegate = null;
inAppWebViewChromeClient = null;
inAppWebViewClientCompat = null;
inAppWebViewClient = null;
javaScriptBridgeInterface = null;
inAppWebViewRenderProcessClient = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,9 @@ public void onProgressChanged(WebView view, int progress) {

InAppWebView webView = (InAppWebView) view;

if (webView.inAppWebViewClient != null) {
if (webView.inAppWebViewClientCompat != null) {
webView.inAppWebViewClientCompat.loadCustomJavaScriptOnPageStarted(view);
} else if (webView.inAppWebViewClient != null) {
webView.inAppWebViewClient.loadCustomJavaScriptOnPageStarted(view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ public boolean shouldOverrideUrlLoading(WebView webView, String url) {
return false;
}

private void allowShouldOverrideUrlLoading(WebView webView, String url, Map<String, String> headers, boolean isForMainFrame) {
private void allowShouldOverrideUrlLoading(WebView webView, String url,
@Nullable Map<String, String> headers,
boolean isForMainFrame) {
if (isForMainFrame) {
// There isn't any way to load an URL for a frame that is not the main frame,
// so call this only on main frame.
Expand All @@ -124,8 +126,11 @@ private void allowShouldOverrideUrlLoading(WebView webView, String url, Map<Stri
webView.loadUrl(url);
}
}
public void onShouldOverrideUrlLoading(final InAppWebView webView, final String url, final String method, final Map<String, String> headers,
final boolean isForMainFrame, boolean hasGesture, boolean isRedirect) {
public void onShouldOverrideUrlLoading(final InAppWebView webView, final String url,
final String method,
@Nullable final Map<String, String> headers,
final boolean isForMainFrame, boolean hasGesture,
boolean isRedirect) {
URLRequest request = new URLRequest(url, method, null, headers);
NavigationAction navigationAction = new NavigationAction(
request,
Expand Down
Loading

0 comments on commit 459875f

Please sign in to comment.