-
Notifications
You must be signed in to change notification settings - Fork 157
/
RNWebView.java
196 lines (152 loc) · 6.47 KB
/
RNWebView.java
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
package com.burnweb.rnwebview;
import android.annotation.SuppressLint;
import android.net.Uri;
import android.graphics.Bitmap;
import android.os.Build;
import android.webkit.GeolocationPermissions;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.SystemClock;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
class RNWebView extends WebView implements LifecycleEventListener {
private final EventDispatcher mEventDispatcher;
private final RNWebViewManager mViewManager;
private String charset = "UTF-8";
private String baseUrl = "file:///";
private String injectedJavaScript = null;
private boolean allowUrlRedirect = false;
private String currentUrl = "";
private String shouldOverrideUrlLoadingUrl = "";
protected class EventWebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url){
int navigationType = 0;
if (currentUrl.equals(url) || url.equals("about:blank")) { // for regular .reload() and html reload.
navigationType = 3;
}
shouldOverrideUrlLoadingUrl = url;
mEventDispatcher.dispatchEvent(new ShouldOverrideUrlLoadingEvent(getId(), SystemClock.nanoTime(), url, navigationType));
return true;
}
public void onPageFinished(WebView view, String url) {
mEventDispatcher.dispatchEvent(new NavigationStateChangeEvent(getId(), SystemClock.nanoTime(), view.getTitle(), false, url, view.canGoBack(), view.canGoForward()));
currentUrl = url;
if(RNWebView.this.getInjectedJavaScript() != null) {
view.loadUrl("javascript:(function() {\n" + RNWebView.this.getInjectedJavaScript() + ";\n})();");
}
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mEventDispatcher.dispatchEvent(new NavigationStateChangeEvent(getId(), SystemClock.nanoTime(), view.getTitle(), true, url, view.canGoBack(), view.canGoForward()));
}
}
protected class CustomWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
getModule().showAlert(url, message, result);
return true;
}
// For Android 4.1+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
getModule().startFileChooserIntent(uploadMsg, acceptType);
}
// For Android 5.0+
@SuppressLint("NewApi")
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
return getModule().startFileChooserIntent(filePathCallback, fileChooserParams.createIntent());
}
}
protected class GeoWebChromeClient extends CustomWebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
}
public RNWebView(RNWebViewManager viewManager, ThemedReactContext reactContext) {
super(reactContext);
mViewManager = viewManager;
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
this.getSettings().setJavaScriptEnabled(true);
this.getSettings().setBuiltInZoomControls(false);
this.getSettings().setDomStorageEnabled(true);
this.getSettings().setGeolocationEnabled(false);
this.getSettings().setPluginState(WebSettings.PluginState.ON);
this.getSettings().setAllowFileAccess(true);
this.getSettings().setAllowFileAccessFromFileURLs(true);
this.getSettings().setAllowUniversalAccessFromFileURLs(true);
this.getSettings().setLoadsImagesAutomatically(true);
this.getSettings().setBlockNetworkImage(false);
this.getSettings().setBlockNetworkLoads(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
this.setWebViewClient(new EventWebClient());
this.setWebChromeClient(getCustomClient());
this.addJavascriptInterface(RNWebView.this, "webView");
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getCharset() {
return this.charset;
}
public void setAllowUrlRedirect(boolean a) {
this.allowUrlRedirect = a;
}
public boolean getAllowUrlRedirect() {
return this.allowUrlRedirect;
}
public void setInjectedJavaScript(String injectedJavaScript) {
this.injectedJavaScript = injectedJavaScript;
}
public String getInjectedJavaScript() {
return this.injectedJavaScript;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public void shouldOverrideWithResult(RNWebView view, ReadableArray args) {
if (!args.getBoolean(0)) {
view.loadUrl(shouldOverrideUrlLoadingUrl);
}
}
public String getBaseUrl() {
return this.baseUrl;
}
public CustomWebChromeClient getCustomClient() {
return new CustomWebChromeClient();
}
public GeoWebChromeClient getGeoClient() {
return new GeoWebChromeClient();
}
public RNWebViewModule getModule() {
return mViewManager.getPackage().getModule();
}
@Override
public void onHostResume() {
}
@Override
public void onHostPause() {
}
@Override
public void onHostDestroy() {
destroy();
}
@Override
public void onDetachedFromWindow() {
this.loadDataWithBaseURL(this.getBaseUrl(), "<html></html>", "text/html", this.getCharset(), null);
super.onDetachedFromWindow();
}
@JavascriptInterface
public void postMessage(String jsParamaters) {
mEventDispatcher.dispatchEvent(new MessageEvent(getId(), jsParamaters));
}
}