22
33import android .annotation .TargetApi ;
44import android .app .Activity ;
5+ import android .app .AlertDialog ;
56import android .content .Context ;
67import android .content .Intent ;
78import android .graphics .Bitmap ;
89import android .net .ConnectivityManager ;
910import android .net .NetworkInfo ;
1011import android .net .Uri ;
1112import android .os .Build ;
13+ import android .os .Handler ;
1214import android .os .Message ;
15+ import android .util .Log ;
1316import android .webkit .CookieManager ;
1417import android .webkit .WebChromeClient ;
1518import android .webkit .WebResourceError ;
@@ -140,37 +143,32 @@ public void onProgressChanged(WebView view, int newProgress) {
140143 webView .setWebViewClient (new WebViewClient () {
141144 @ Override
142145 public void onPageStarted (WebView view , String url , Bitmap favicon ) {
143- // prevent loading content that isn't ours
144- if (!url .startsWith (Constants .WEBAPP_URL )) {
145- // stop loading
146- view .stopLoading ();
147-
148- // open external URL in Browser/3rd party apps instead
149- Intent intent = new Intent (Intent .ACTION_VIEW , Uri .parse (url ));
150- activity .startActivity (intent );
151- }
152- // activate loading animation screen
153- uiManager .setLoading (true );
154146 super .onPageStarted (view , url , favicon );
147+ Log .d ("TAG" , "started url: " +url );
148+ handleUrlLoad (view , url );
155149 }
156150
157151 // handle loading error by showing the offline screen
158152 @ Deprecated
159153 @ Override
160154 public void onReceivedError (WebView view , int errorCode , String description , String failingUrl ) {
155+ Log .d ("TAG" , "receivedError Old" );
161156 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
162- uiManager . setOffline ( true );
157+ handleLoadError ( view , failingUrl , errorCode );
163158 }
164159 }
165160
166161 @ TargetApi (Build .VERSION_CODES .M )
167162 @ Override
168163 public void onReceivedError (WebView view , WebResourceRequest request , WebResourceError error ) {
164+ Log .d ("TAG" , "receivedError New" );
169165 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
170166 // new API method calls this on every error for each resource.
171167 // we only want to interfere if the page itself got problems.
172- if (view .getUrl ().equals (request .getUrl ().toString ())) {
173- uiManager .setOffline (true );
168+ String url = request .getUrl ().toString ();
169+ if (view .getUrl ().equals (url )) {
170+ Log .d ("TAG" , "receivedError New page match " + error .getDescription ().toString ());
171+ handleLoadError (view , url , error .getErrorCode ());
174172 }
175173 }
176174 }
@@ -186,6 +184,67 @@ public void onResume() {
186184 webView .onResume ();
187185 }
188186
187+ // show "no app found" dialog
188+ private void showNoAppDialog (Activity thisActivity ) {
189+ new AlertDialog .Builder (thisActivity )
190+ .setTitle (R .string .noapp_heading )
191+ .setMessage (R .string .noapp_description )
192+ .show ();
193+ }
194+ // handle load errors
195+ private void handleLoadError (WebView view , String url , int errorCode ) {
196+ if (errorCode != WebViewClient .ERROR_UNSUPPORTED_SCHEME ) {
197+ uiManager .setOffline (true );
198+ } else {
199+ Log .d ("TAG" , "unsupported scheme!" );
200+ // Unsupported Scheme, recover
201+ new Handler ().postDelayed (new Runnable () {
202+ @ Override
203+ public void run () {
204+ goBack ();
205+ }
206+ }, 100 );
207+ }
208+ }
209+
210+ // handle external urls
211+ private boolean handleUrlLoad (WebView view , String url ) {
212+ // prevent loading content that isn't ours
213+ if (!url .startsWith (Constants .WEBAPP_URL )) {
214+ // stop loading
215+ view .stopLoading ();
216+
217+ /*
218+ // handle non-http protocols, like mailto: or whatsapp:
219+ if (!url.startsWith("http")) {
220+ // this hit the WebView's onReceivedError callback, recover
221+ goBack();
222+ uiManager.setOffline(false);
223+ }
224+ */
225+
226+ // open external URL in Browser/3rd party apps instead
227+ try {
228+ Intent intent = new Intent (Intent .ACTION_VIEW , Uri .parse (url ));
229+ if (intent .resolveActivity (activity .getPackageManager ()) != null ) {
230+ activity .startActivity (intent );
231+ } else {
232+ showNoAppDialog (activity );
233+ }
234+ } catch (Exception e ) {
235+ showNoAppDialog (activity );
236+ }
237+ // return value for shouldOverrideUrlLoading
238+ return true ;
239+ } else {
240+ // let WebView load the page!
241+ // activate loading animation screen
242+ uiManager .setLoading (true );
243+ // return value for shouldOverrideUrlLoading
244+ return false ;
245+ }
246+ }
247+
189248 // handle back button press
190249 public boolean goBack () {
191250 if (webView .canGoBack ()) {
0 commit comments