11package com .flutter_webview_plugin ;
22
3+ import android .annotation .TargetApi ;
34import android .graphics .Bitmap ;
5+ import android .os .Build ;
46import android .webkit .WebResourceRequest ;
57import android .webkit .WebResourceResponse ;
68import android .webkit .WebView ;
79import android .webkit .WebViewClient ;
810
911import java .util .HashMap ;
1012import java .util .Map ;
13+ import java .util .regex .Matcher ;
14+ import java .util .regex .Pattern ;
1115
1216/**
1317 * Created by lejard_h on 20/12/2017.
1418 */
1519
1620public class BrowserClient extends WebViewClient {
21+ private Pattern invalidUrlPattern = null ;
22+
1723 public BrowserClient () {
24+ this (null );
25+ }
26+
27+ public BrowserClient (String invalidUrlRegex ) {
1828 super ();
29+ if (invalidUrlRegex != null ) {
30+ invalidUrlPattern = Pattern .compile (invalidUrlRegex );
31+ }
32+ }
33+
34+ public void updateInvalidUrlRegex (String invalidUrlRegex ) {
35+ if (invalidUrlRegex != null ) {
36+ invalidUrlPattern = Pattern .compile (invalidUrlRegex );
37+ } else {
38+ invalidUrlPattern = null ;
39+ }
1940 }
2041
2142 @ Override
@@ -40,6 +61,35 @@ public void onPageFinished(WebView view, String url) {
4061
4162 }
4263
64+ @ TargetApi (Build .VERSION_CODES .LOLLIPOP )
65+ @ Override
66+ public boolean shouldOverrideUrlLoading (WebView view , WebResourceRequest request ) {
67+ // returning true causes the current WebView to abort loading the URL,
68+ // while returning false causes the WebView to continue loading the URL as usual.
69+ String url = request .getUrl ().toString ();
70+ boolean isInvalid = checkInvalidUrl (url );
71+ Map <String , Object > data = new HashMap <>();
72+ data .put ("url" , url );
73+ data .put ("type" , isInvalid ? "abortLoad" : "shouldStart" );
74+
75+ FlutterWebviewPlugin .channel .invokeMethod ("onState" , data );
76+ return isInvalid ;
77+ }
78+
79+ @ Override
80+ public boolean shouldOverrideUrlLoading (WebView view , String url ) {
81+ // returning true causes the current WebView to abort loading the URL,
82+ // while returning false causes the WebView to continue loading the URL as usual.
83+ boolean isInvalid = checkInvalidUrl (url );
84+ Map <String , Object > data = new HashMap <>();
85+ data .put ("url" , url );
86+ data .put ("type" , isInvalid ? "abortLoad" : "shouldStart" );
87+
88+ FlutterWebviewPlugin .channel .invokeMethod ("onState" , data );
89+ return isInvalid ;
90+ }
91+
92+ @ TargetApi (Build .VERSION_CODES .LOLLIPOP )
4393 @ Override
4494 public void onReceivedHttpError (WebView view , WebResourceRequest request , WebResourceResponse errorResponse ) {
4595 super .onReceivedHttpError (view , request , errorResponse );
@@ -48,4 +98,22 @@ public void onReceivedHttpError(WebView view, WebResourceRequest request, WebRes
4898 data .put ("code" , Integer .toString (errorResponse .getStatusCode ()));
4999 FlutterWebviewPlugin .channel .invokeMethod ("onHttpError" , data );
50100 }
101+
102+ @ Override
103+ public void onReceivedError (WebView view , int errorCode , String description , String failingUrl ) {
104+ super .onReceivedError (view , errorCode , description , failingUrl );
105+ Map <String , Object > data = new HashMap <>();
106+ data .put ("url" , failingUrl );
107+ data .put ("code" , errorCode );
108+ FlutterWebviewPlugin .channel .invokeMethod ("onHttpError" , data );
109+ }
110+
111+ private boolean checkInvalidUrl (String url ) {
112+ if (invalidUrlPattern == null ) {
113+ return false ;
114+ } else {
115+ Matcher matcher = invalidUrlPattern .matcher (url );
116+ return matcher .lookingAt ();
117+ }
118+ }
51119}
0 commit comments