|
|
@@ -1,6 +1,8 @@
|
|
|
package com.flutter_webview_plugin;
|
|
|
|
|
|
+import android.annotation.TargetApi;
|
|
|
import android.graphics.Bitmap;
|
|
|
+import android.os.Build;
|
|
|
import android.webkit.WebResourceRequest;
|
|
|
import android.webkit.WebResourceResponse;
|
|
|
import android.webkit.WebView;
|
|
|
@@ -8,14 +10,33 @@ import android.webkit.WebViewClient;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* Created by lejard_h on 20/12/2017.
|
|
|
*/
|
|
|
|
|
|
public class BrowserClient extends WebViewClient {
|
|
|
+ private Pattern invalidUrlPattern = null;
|
|
|
+
|
|
|
public BrowserClient() {
|
|
|
+ this(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public BrowserClient(String invalidUrlRegex) {
|
|
|
super();
|
|
|
+ if (invalidUrlRegex != null) {
|
|
|
+ invalidUrlPattern = Pattern.compile(invalidUrlRegex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateInvalidUrlRegex(String invalidUrlRegex) {
|
|
|
+ if (invalidUrlRegex != null) {
|
|
|
+ invalidUrlPattern = Pattern.compile(invalidUrlRegex);
|
|
|
+ } else {
|
|
|
+ invalidUrlPattern = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -40,6 +61,35 @@ public class BrowserClient extends WebViewClient {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
+ @Override
|
|
|
+ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
|
|
+ // returning true causes the current WebView to abort loading the URL,
|
|
|
+ // while returning false causes the WebView to continue loading the URL as usual.
|
|
|
+ String url = request.getUrl().toString();
|
|
|
+ boolean isInvalid = checkInvalidUrl(url);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("url", url);
|
|
|
+ data.put("type", isInvalid ? "abortLoad" : "shouldStart");
|
|
|
+
|
|
|
+ FlutterWebviewPlugin.channel.invokeMethod("onState", data);
|
|
|
+ return isInvalid;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
+ // returning true causes the current WebView to abort loading the URL,
|
|
|
+ // while returning false causes the WebView to continue loading the URL as usual.
|
|
|
+ boolean isInvalid = checkInvalidUrl(url);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("url", url);
|
|
|
+ data.put("type", isInvalid ? "abortLoad" : "shouldStart");
|
|
|
+
|
|
|
+ FlutterWebviewPlugin.channel.invokeMethod("onState", data);
|
|
|
+ return isInvalid;
|
|
|
+ }
|
|
|
+
|
|
|
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
@Override
|
|
|
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
|
|
|
super.onReceivedHttpError(view, request, errorResponse);
|
|
|
@@ -48,4 +98,22 @@ public class BrowserClient extends WebViewClient {
|
|
|
data.put("code", Integer.toString(errorResponse.getStatusCode()));
|
|
|
FlutterWebviewPlugin.channel.invokeMethod("onHttpError", data);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
+ super.onReceivedError(view, errorCode, description, failingUrl);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("url", failingUrl);
|
|
|
+ data.put("code", errorCode);
|
|
|
+ FlutterWebviewPlugin.channel.invokeMethod("onHttpError", data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkInvalidUrl(String url) {
|
|
|
+ if (invalidUrlPattern == null) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ Matcher matcher = invalidUrlPattern.matcher(url);
|
|
|
+ return matcher.lookingAt();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|