Skip to content

Commit 3b7efdd

Browse files
committed
load startpage from offline container instead of simplay reloading, to avoid deadlocks on bad links
1 parent 13b3b47 commit 3b7efdd

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

app/src/main/java/at/xtools/pwawrapper/ui/UIManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public UIManager(Activity activity) {
3737
offlineContainer.setOnClickListener(new View.OnClickListener() {
3838
@Override
3939
public void onClick(View v) {
40-
webView.reload();
40+
webView.loadUrl(Constants.WEBAPP_URL);
4141
setOffline(false);
4242
}
4343
});

app/src/main/java/at/xtools/pwawrapper/webview/WebViewHelper.java

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import android.annotation.TargetApi;
44
import android.app.Activity;
5+
import android.app.AlertDialog;
56
import android.content.Context;
67
import android.content.Intent;
78
import android.graphics.Bitmap;
89
import android.net.ConnectivityManager;
910
import android.net.NetworkInfo;
1011
import android.net.Uri;
1112
import android.os.Build;
13+
import android.os.Handler;
1214
import android.os.Message;
15+
import android.util.Log;
1316
import android.webkit.CookieManager;
1417
import android.webkit.WebChromeClient;
1518
import 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()) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<resources>
22
<string name="offline_heading">Keine Internetverbindung.</string>
33
<string name="offline_description">Hier tippen um es erneut zu versuchen!</string>
4+
<string name="noapp_heading">Keine App gefunden.</string>
5+
<string name="noapp_description">Keine App auf diesem Gerät kann die gewählte Aktion ausführen.</string>
46
</resources>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<resources>
22
<string name="offline_heading">You are offline.</string>
33
<string name="offline_description">Tap here to retry!</string>
4+
<string name="noapp_heading">App not found.</string>
5+
<string name="noapp_description">No App on this device can carry out this action.</string>
46
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
<string name="app_name" translatable="false">Leasing Rechner</string>
33
<string name="offline_heading">You are offline.</string>
44
<string name="offline_description">Tap here to retry!</string>
5+
<string name="noapp_heading">App not found.</string>
6+
<string name="noapp_description">No App on this device can carry out this action.</string>
57
</resources>

0 commit comments

Comments
 (0)