forked from react-native-webview/react-native-webview
/
RNCWebView.java
441 lines (373 loc) · 14.8 KB
/
RNCWebView.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package com.reactnativecommunity.webview;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.events.ContentSizeChangeEvent;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.views.scroll.OnScrollDispatchHelper;
import com.facebook.react.views.scroll.ScrollEvent;
import com.facebook.react.views.scroll.ScrollEventType;
import com.reactnativecommunity.webview.events.TopCustomMenuSelectionEvent;
import com.reactnativecommunity.webview.events.TopMessageEvent;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import java.util.Map;
public class RNCWebView extends WebView implements LifecycleEventListener {
protected @Nullable
String injectedJS;
protected @Nullable
String injectedJSBeforeContentLoaded;
protected static final String JAVASCRIPT_INTERFACE = "ReactNativeWebView";
protected @Nullable
RNCWebViewBridge bridge;
/**
* android.webkit.WebChromeClient fundamentally does not support JS injection into frames other
* than the main frame, so these two properties are mostly here just for parity with iOS & macOS.
*/
protected boolean injectedJavaScriptForMainFrameOnly = true;
protected boolean injectedJavaScriptBeforeContentLoadedForMainFrameOnly = true;
protected boolean messagingEnabled = false;
protected @Nullable
String messagingModuleName;
protected @Nullable
RNCWebViewMessagingModule mMessagingJSModule;
protected @Nullable
RNCWebViewClient mRNCWebViewClient;
protected boolean sendContentSizeChangeEvents = false;
private OnScrollDispatchHelper mOnScrollDispatchHelper;
protected boolean hasScrollEvent = false;
protected boolean nestedScrollEnabled = false;
protected ProgressChangedFilter progressChangedFilter;
/**
* WebView must be created with an context of the current activity
* <p>
* Activity Context is required for creation of dialogs internally by WebView
* Reactive Native needed for access to ReactNative internal system functionality
*/
public RNCWebView(ThemedReactContext reactContext) {
super(reactContext);
mMessagingJSModule = ((ThemedReactContext) this.getContext()).getReactApplicationContext().getJSModule(RNCWebViewMessagingModule.class);
progressChangedFilter = new ProgressChangedFilter();
}
public void setIgnoreErrFailedForThisURL(String url) {
mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
}
public void setBasicAuthCredential(RNCBasicAuthCredential credential) {
mRNCWebViewClient.setBasicAuthCredential(credential);
}
public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
}
public void setHasScrollEvent(boolean hasScrollEvent) {
this.hasScrollEvent = hasScrollEvent;
}
public void setNestedScrollEnabled(boolean nestedScrollEnabled) {
this.nestedScrollEnabled = nestedScrollEnabled;
}
@Override
public void onHostResume() {
// do nothing
}
@Override
public void onHostPause() {
// do nothing
}
@Override
public void onHostDestroy() {
cleanupCallbacksAndDestroy();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.nestedScrollEnabled) {
requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
if (sendContentSizeChangeEvents) {
dispatchEvent(
this,
new ContentSizeChangeEvent(
RNCWebViewWrapper.getReactTagFromWebView(this),
w,
h
)
);
}
}
protected @Nullable
List<Map<String, String>> menuCustomItems;
public void setMenuCustomItems(List<Map<String, String>> menuCustomItems) {
this.menuCustomItems = menuCustomItems;
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
if(menuCustomItems == null ){
return super.startActionMode(callback, type);
}
return super.startActionMode(new ActionMode.Callback2() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
for (int i = 0; i < menuCustomItems.size(); i++) {
menu.add(Menu.NONE, i, i, (menuCustomItems.get(i)).get("label"));
}
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
WritableMap wMap = Arguments.createMap();
RNCWebView.this.evaluateJavascript(
"(function(){return {selection: window.getSelection().toString()} })()",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String selectionJson) {
Map<String, String> menuItemMap = menuCustomItems.get(item.getItemId());
wMap.putString("label", menuItemMap.get("label"));
wMap.putString("key", menuItemMap.get("key"));
String selectionText = "";
try {
selectionText = new JSONObject(selectionJson).getString("selection");
} catch (JSONException ignored) {}
wMap.putString("selectedText", selectionText);
dispatchEvent(RNCWebView.this, new TopCustomMenuSelectionEvent(RNCWebViewWrapper.getReactTagFromWebView(RNCWebView.this), wMap));
mode.finish();
}
}
);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mode = null;
}
@Override
public void onGetContentRect (ActionMode mode,
View view,
Rect outRect){
if (callback instanceof ActionMode.Callback2) {
((ActionMode.Callback2) callback).onGetContentRect(mode, view, outRect);
} else {
super.onGetContentRect(mode, view, outRect);
}
}
}, type);
}
@Override
public void setWebViewClient(WebViewClient client) {
super.setWebViewClient(client);
if (client instanceof RNCWebViewClient) {
mRNCWebViewClient = (RNCWebViewClient) client;
mRNCWebViewClient.setProgressChangedFilter(progressChangedFilter);
}
}
WebChromeClient mWebChromeClient;
@Override
public void setWebChromeClient(WebChromeClient client) {
this.mWebChromeClient = client;
super.setWebChromeClient(client);
if (client instanceof RNCWebChromeClient) {
((RNCWebChromeClient) client).setProgressChangedFilter(progressChangedFilter);
}
}
public WebChromeClient getWebChromeClient() {
return this.mWebChromeClient;
}
public @Nullable
RNCWebViewClient getRNCWebViewClient() {
return mRNCWebViewClient;
}
public boolean getMessagingEnabled() {
return this.messagingEnabled;
}
protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
if (bridge == null) {
bridge = new RNCWebViewBridge(webView);
addJavascriptInterface(bridge, JAVASCRIPT_INTERFACE);
}
return bridge;
}
@SuppressLint("AddJavascriptInterface")
public void setMessagingEnabled(boolean enabled) {
if (messagingEnabled == enabled) {
return;
}
messagingEnabled = enabled;
if (enabled) {
createRNCWebViewBridge(this);
}
}
protected void evaluateJavascriptWithFallback(String script) {
evaluateJavascript(script, null);
}
public void callInjectedJavaScript() {
if (getSettings().getJavaScriptEnabled() &&
injectedJS != null &&
!TextUtils.isEmpty(injectedJS)) {
evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");
}
}
public void callInjectedJavaScriptBeforeContentLoaded() {
if (getSettings().getJavaScriptEnabled() &&
injectedJSBeforeContentLoaded != null &&
!TextUtils.isEmpty(injectedJSBeforeContentLoaded)) {
evaluateJavascriptWithFallback("(function() {\n" + injectedJSBeforeContentLoaded + ";\n})();");
}
}
public void setInjectedJavaScriptObject(String obj) {
if (getSettings().getJavaScriptEnabled()) {
RNCWebViewBridge b = createRNCWebViewBridge(this);
b.setInjectedObjectJson(obj);
}
}
public void onMessage(String message) {
ThemedReactContext reactContext = getThemedReactContext();
RNCWebView mWebView = this;
if (mRNCWebViewClient != null) {
WebView webView = this;
webView.post(new Runnable() {
@Override
public void run() {
if (mRNCWebViewClient == null) {
return;
}
WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
data.putString("data", message);
if (mMessagingJSModule != null) {
dispatchDirectMessage(data);
} else {
dispatchEvent(webView, new TopMessageEvent(RNCWebViewWrapper.getReactTagFromWebView(webView), data));
}
}
});
} else {
WritableMap eventData = Arguments.createMap();
eventData.putString("data", message);
if (mMessagingJSModule != null) {
dispatchDirectMessage(eventData);
} else {
dispatchEvent(this, new TopMessageEvent(RNCWebViewWrapper.getReactTagFromWebView(this), eventData));
}
}
}
protected void dispatchDirectMessage(WritableMap data) {
WritableNativeMap event = new WritableNativeMap();
event.putMap("nativeEvent", data);
event.putString("messagingModuleName", messagingModuleName);
mMessagingJSModule.onMessage(event);
}
protected boolean dispatchDirectShouldStartLoadWithRequest(WritableMap data) {
WritableNativeMap event = new WritableNativeMap();
event.putMap("nativeEvent", data);
event.putString("messagingModuleName", messagingModuleName);
mMessagingJSModule.onShouldStartLoadWithRequest(event);
return true;
}
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (!hasScrollEvent) {
return;
}
if (mOnScrollDispatchHelper == null) {
mOnScrollDispatchHelper = new OnScrollDispatchHelper();
}
if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
ScrollEvent event = ScrollEvent.obtain(
RNCWebViewWrapper.getReactTagFromWebView(this),
ScrollEventType.SCROLL,
x,
y,
mOnScrollDispatchHelper.getXFlingVelocity(),
mOnScrollDispatchHelper.getYFlingVelocity(),
this.computeHorizontalScrollRange(),
this.computeVerticalScrollRange(),
this.getWidth(),
this.getHeight());
dispatchEvent(this, event);
}
}
protected void dispatchEvent(WebView webView, Event event) {
ThemedReactContext reactContext = getThemedReactContext();
int reactTag = RNCWebViewWrapper.getReactTagFromWebView(webView);
UIManagerHelper.getEventDispatcherForReactTag(reactContext, reactTag).dispatchEvent(event);
}
protected void cleanupCallbacksAndDestroy() {
setWebViewClient(null);
destroy();
}
@Override
public void destroy() {
if (mWebChromeClient != null) {
mWebChromeClient.onHideCustomView();
}
super.destroy();
}
public ThemedReactContext getThemedReactContext() {
return (ThemedReactContext) this.getContext();
}
public ReactApplicationContext getReactApplicationContext() {
return this.getThemedReactContext().getReactApplicationContext();
}
protected class RNCWebViewBridge {
private String TAG = "RNCWebViewBridge";
RNCWebView mWebView;
String injectedObjectJson;
RNCWebViewBridge(RNCWebView c) {
mWebView = c;
}
public void setInjectedObjectJson(String s) {
injectedObjectJson = s;
}
/**
* This method is called whenever JavaScript running within the web view calls:
* - window[JAVASCRIPT_INTERFACE].postMessage
*/
@JavascriptInterface
public void postMessage(String message) {
if (mWebView.getMessagingEnabled()) {
mWebView.onMessage(message);
} else {
FLog.w(TAG, "ReactNativeWebView.postMessage method was called but messaging is disabled. Pass an onMessage handler to the WebView.");
}
}
@JavascriptInterface
public String injectedObjectJson() { return injectedObjectJson; }
}
protected static class ProgressChangedFilter {
private boolean waitingForCommandLoadUrl = false;
public void setWaitingForCommandLoadUrl(boolean isWaiting) {
waitingForCommandLoadUrl = isWaiting;
}
public boolean isWaitingForCommandLoadUrl() {
return waitingForCommandLoadUrl;
}
}
}