2
2
3
3
import android .annotation .TargetApi ;
4
4
import android .app .Activity ;
5
+ import android .app .AlertDialog ;
5
6
import android .content .Context ;
6
7
import android .content .Intent ;
7
8
import android .graphics .Bitmap ;
8
9
import android .net .ConnectivityManager ;
9
10
import android .net .NetworkInfo ;
10
11
import android .net .Uri ;
11
12
import android .os .Build ;
13
+ import android .os .Handler ;
12
14
import android .os .Message ;
15
+ import android .util .Log ;
13
16
import android .webkit .CookieManager ;
14
17
import android .webkit .WebChromeClient ;
15
18
import android .webkit .WebResourceError ;
@@ -140,37 +143,32 @@ public void onProgressChanged(WebView view, int newProgress) {
140
143
webView .setWebViewClient (new WebViewClient () {
141
144
@ Override
142
145
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 );
154
146
super .onPageStarted (view , url , favicon );
147
+ Log .d ("TAG" , "started url: " +url );
148
+ handleUrlLoad (view , url );
155
149
}
156
150
157
151
// handle loading error by showing the offline screen
158
152
@ Deprecated
159
153
@ Override
160
154
public void onReceivedError (WebView view , int errorCode , String description , String failingUrl ) {
155
+ Log .d ("TAG" , "receivedError Old" );
161
156
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
162
- uiManager . setOffline ( true );
157
+ handleLoadError ( view , failingUrl , errorCode );
163
158
}
164
159
}
165
160
166
161
@ TargetApi (Build .VERSION_CODES .M )
167
162
@ Override
168
163
public void onReceivedError (WebView view , WebResourceRequest request , WebResourceError error ) {
164
+ Log .d ("TAG" , "receivedError New" );
169
165
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
170
166
// new API method calls this on every error for each resource.
171
167
// 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 ());
174
172
}
175
173
}
176
174
}
@@ -186,6 +184,67 @@ public void onResume() {
186
184
webView .onResume ();
187
185
}
188
186
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
+
189
248
// handle back button press
190
249
public boolean goBack () {
191
250
if (webView .canGoBack ()) {
0 commit comments