-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathWebViewHelper.java
More file actions
271 lines (243 loc) · 9.6 KB
/
WebViewHelper.java
File metadata and controls
271 lines (243 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package at.xtools.pwawrapper.webview;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import at.xtools.pwawrapper.Constants;
import at.xtools.pwawrapper.R;
import at.xtools.pwawrapper.ui.UIManager;
public class WebViewHelper {
// Instance variables
private Activity activity;
private UIManager uiManager;
private WebView webView;
private WebSettings webSettings;
public WebViewHelper(Activity activity, UIManager uiManager) {
this.activity = activity;
this.uiManager = uiManager;
this.webView = (WebView) activity.findViewById(R.id.webView);
this.webSettings = webView.getSettings();
}
/**
* Simple helper method checking if connected to Network.
* Doesn't check for actual Internet connection!
* @return {boolean} True if connected to Network.
*/
private boolean isNetworkAvailable() {
ConnectivityManager manager =
(ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
// Wifi or Mobile Network is present and connected
isAvailable = true;
}
return isAvailable;
}
// manipulate cache settings to make sure our PWA gets updated
private void useCache(Boolean use) {
if (use) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
} else {
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
}
}
// public method changing cache settings according to network availability.
// retrieve content from cache primarily if not connected,
// allow fetching from web too otherwise to get updates.
public void forceCacheIfOffline() {
useCache(!isNetworkAvailable());
}
// handles initial setup of webview
public void setupWebView() {
// accept cookies
CookieManager.getInstance().setAcceptCookie(true);
// enable JS
webSettings.setJavaScriptEnabled(true);
// must be set for our js-popup-blocker:
webSettings.setSupportMultipleWindows(true);
// PWA settings
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webSettings.setDatabasePath(activity.getApplicationContext().getFilesDir().getAbsolutePath());
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
webSettings.setAppCacheMaxSize(Long.MAX_VALUE);
}
webSettings.setDomStorageEnabled(true);
webSettings.setAppCachePath(activity.getApplicationContext().getCacheDir().getAbsolutePath());
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
// enable mixed content mode conditionally
if (Constants.ENABLE_MIXED_CONTENT
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
// retrieve content from cache primarily if not connected
forceCacheIfOffline();
// set User Agent
if (Constants.OVERRIDE_USER_AGENT || Constants.POSTFIX_USER_AGENT) {
String userAgent = webSettings.getUserAgentString();
if (Constants.OVERRIDE_USER_AGENT) {
userAgent = Constants.USER_AGENT;
}
if (Constants.POSTFIX_USER_AGENT) {
userAgent = userAgent + " " + Constants.USER_AGENT_POSTFIX;
}
webSettings.setUserAgentString(userAgent);
}
// enable HTML5-support
webView.setWebChromeClient(new WebChromeClient() {
//simple yet effective redirect/popup blocker
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
Message href = view.getHandler().obtainMessage();
view.requestFocusNodeHref(href);
final String popupUrl = href.getData().getString("url");
if (popupUrl != null) {
//it's null for most rouge browser hijack ads
webView.loadUrl(popupUrl);
return true;
}
return false;
}
// update ProgressBar
@Override
public void onProgressChanged(WebView view, int newProgress) {
uiManager.setLoadingProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
// Set up Webview client
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
handleUrlLoad(view, url);
}
// handle loading error by showing the offline screen
@Deprecated
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
handleLoadError(errorCode);
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// new API method calls this on every error for each resource.
// we only want to interfere if the page itself got problems.
String url = request.getUrl().toString();
if (view.getUrl().equals(url)) {
handleLoadError(error.getErrorCode());
}
}
}
//Handle if request comes from same hostname. If not, it may be an intent
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if((String.valueOf(request.getUrl())).contains(Constants.WEBAPP_HOST)) {
view.loadUrl(String.valueOf(request.getUrl()));
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
}
return true;
}
});
}
// Lifecycle callbacks
public void onPause() {
webView.onPause();
}
public void onResume() {
webView.onResume();
}
// show "no app found" dialog
private void showNoAppDialog(Activity thisActivity) {
new AlertDialog.Builder(thisActivity)
.setTitle(R.string.noapp_heading)
.setMessage(R.string.noapp_description)
.show();
}
// handle load errors
private void handleLoadError(int errorCode) {
if (errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME) {
uiManager.setOffline(true);
} else {
// Unsupported Scheme, recover
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
goBack();
}
}, 100);
}
}
// handle external urls
private boolean handleUrlLoad(WebView view, String url) {
// prevent loading content that isn't ours
if (!url.startsWith(Constants.WEBAPP_URL)) {
// stop loading
// stopping only would cause the PWA to freeze, need to reload the app as a workaround
view.stopLoading();
view.reload();
// open external URL in Browser/3rd party apps instead
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(intent);
} else {
showNoAppDialog(activity);
}
} catch (Exception e) {
showNoAppDialog(activity);
}
// return value for shouldOverrideUrlLoading
return true;
} else {
// let WebView load the page!
// activate loading animation screen
uiManager.setLoading(true);
// return value for shouldOverrideUrlLoading
return false;
}
}
// handle back button press
public boolean goBack() {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
// load app startpage
public void loadHome() {
webView.loadUrl(Constants.WEBAPP_URL);
}
// load URL from intent
public void loadIntentUrl(String url) {
if (!url.equals("") && url.contains(Constants.WEBAPP_HOST)) {
webView.loadUrl(url);
} else {
// Fallback
loadHome();
}
}
}