8
8
package com .facebook .react .views .webview ;
9
9
10
10
import android .annotation .TargetApi ;
11
- import android .content .Context ;
12
- import com .facebook .react .uimanager .UIManagerModule ;
13
- import java .util .LinkedList ;
14
- import java .util .List ;
15
- import java .util .regex .Pattern ;
16
- import javax .annotation .Nullable ;
17
-
18
- import java .io .UnsupportedEncodingException ;
19
- import java .net .URLEncoder ;
20
- import java .util .ArrayList ;
21
- import java .util .HashMap ;
22
- import java .util .Locale ;
23
- import java .util .Map ;
24
-
25
11
import android .content .ActivityNotFoundException ;
12
+ import android .content .Context ;
26
13
import android .content .Intent ;
27
14
import android .graphics .Bitmap ;
28
15
import android .graphics .Picture ;
53
40
import com .facebook .react .module .annotations .ReactModule ;
54
41
import com .facebook .react .uimanager .SimpleViewManager ;
55
42
import com .facebook .react .uimanager .ThemedReactContext ;
43
+ import com .facebook .react .uimanager .UIManagerModule ;
56
44
import com .facebook .react .uimanager .annotations .ReactProp ;
57
45
import com .facebook .react .uimanager .events .ContentSizeChangeEvent ;
58
46
import com .facebook .react .uimanager .events .Event ;
62
50
import com .facebook .react .views .webview .events .TopLoadingStartEvent ;
63
51
import com .facebook .react .views .webview .events .TopMessageEvent ;
64
52
import java .io .UnsupportedEncodingException ;
53
+ import java .net .URLEncoder ;
65
54
import java .util .ArrayList ;
66
55
import java .util .HashMap ;
56
+ import java .util .LinkedList ;
57
+ import java .util .List ;
67
58
import java .util .Locale ;
68
59
import java .util .Map ;
60
+ import java .util .regex .Pattern ;
69
61
import javax .annotation .Nullable ;
70
62
import org .json .JSONException ;
71
63
import org .json .JSONObject ;
@@ -140,10 +132,10 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) {
140
132
mLastLoadFailed = false ;
141
133
142
134
dispatchEvent (
143
- webView ,
144
- new TopLoadingStartEvent (
145
- webView .getId (),
146
- createWebViewEvent (webView , url )));
135
+ webView ,
136
+ new TopLoadingStartEvent (
137
+ webView .getId (),
138
+ createWebViewEvent (webView , url )));
147
139
}
148
140
149
141
@ Override
@@ -153,7 +145,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
153
145
// url blacklisting
154
146
if (mUrlPrefixesForDefaultIntent != null && mUrlPrefixesForDefaultIntent .size () > 0 ) {
155
147
ArrayList <Object > urlPrefixesForDefaultIntent =
156
- mUrlPrefixesForDefaultIntent .toArrayList ();
148
+ mUrlPrefixesForDefaultIntent .toArrayList ();
157
149
for (Object urlPrefix : urlPrefixesForDefaultIntent ) {
158
150
if (url .startsWith ((String ) urlPrefix )) {
159
151
launchIntent (view .getContext (), url );
@@ -196,10 +188,10 @@ private boolean shouldHandleURL(List<Pattern> originWhitelist, String url) {
196
188
197
189
@ Override
198
190
public void onReceivedError (
199
- WebView webView ,
200
- int errorCode ,
201
- String description ,
202
- String failingUrl ) {
191
+ WebView webView ,
192
+ int errorCode ,
193
+ String description ,
194
+ String failingUrl ) {
203
195
super .onReceivedError (webView , errorCode , description , failingUrl );
204
196
mLastLoadFailed = true ;
205
197
@@ -212,16 +204,16 @@ public void onReceivedError(
212
204
eventData .putString ("description" , description );
213
205
214
206
dispatchEvent (
215
- webView ,
216
- new TopLoadingErrorEvent (webView .getId (), eventData ));
207
+ webView ,
208
+ new TopLoadingErrorEvent (webView .getId (), eventData ));
217
209
}
218
210
219
211
protected void emitFinishEvent (WebView webView , String url ) {
220
212
dispatchEvent (
221
- webView ,
222
- new TopLoadingFinishEvent (
223
- webView .getId (),
224
- createWebViewEvent (webView , url )));
213
+ webView ,
214
+ new TopLoadingFinishEvent (
215
+ webView .getId (),
216
+ createWebViewEvent (webView , url )));
225
217
}
226
218
227
219
protected WritableMap createWebViewEvent (WebView webView , String url ) {
@@ -342,8 +334,8 @@ protected void evaluateJavascriptWithFallback(String script) {
342
334
343
335
public void callInjectedJavaScript () {
344
336
if (getSettings ().getJavaScriptEnabled () &&
345
- injectedJS != null &&
346
- !TextUtils .isEmpty (injectedJS )) {
337
+ injectedJS != null &&
338
+ !TextUtils .isEmpty (injectedJS )) {
347
339
evaluateJavascriptWithFallback ("(function() {\n " + injectedJS + ";\n })();" );
348
340
}
349
341
}
@@ -366,9 +358,9 @@ public void onReceiveValue(String value) {
366
358
evaluateJavascriptWithFallback ("(" +
367
359
"window.originalPostMessage = window.postMessage," +
368
360
"window.postMessage = function(data) {" +
369
- BRIDGE_NAME + ".postMessage(String(data));" +
361
+ BRIDGE_NAME + ".postMessage(String(data));" +
370
362
"}" +
371
- ")" );
363
+ ")" );
372
364
}
373
365
}
374
366
@@ -438,8 +430,8 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
438
430
439
431
// Fixes broken full-screen modals/galleries due to body height being 0.
440
432
webView .setLayoutParams (
441
- new LayoutParams (LayoutParams .MATCH_PARENT ,
442
- LayoutParams .MATCH_PARENT ));
433
+ new LayoutParams (LayoutParams .MATCH_PARENT ,
434
+ LayoutParams .MATCH_PARENT ));
443
435
444
436
setGeolocationEnabled (webView , false );
445
437
if (ReactBuildConfig .DEBUG && Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
@@ -511,7 +503,7 @@ public void setSource(WebView view, @Nullable ReadableMap source) {
511
503
String html = source .getString ("html" );
512
504
if (source .hasKey ("baseUrl" )) {
513
505
view .loadDataWithBaseURL (
514
- source .getString ("baseUrl" ), html , HTML_MIME_TYPE , HTML_ENCODING , null );
506
+ source .getString ("baseUrl" ), html , HTML_MIME_TYPE , HTML_ENCODING , null );
515
507
} else {
516
508
view .loadData (html , HTML_MIME_TYPE , HTML_ENCODING );
517
509
}
@@ -588,14 +580,21 @@ public void setMixedContentMode(WebView view, @Nullable String mixedContentMode)
588
580
589
581
@ ReactProp (name = "urlPrefixesForDefaultIntent" )
590
582
public void setUrlPrefixesForDefaultIntent (
591
- WebView view ,
592
- @ Nullable ReadableArray urlPrefixesForDefaultIntent ) {
583
+ WebView view ,
584
+ @ Nullable ReadableArray urlPrefixesForDefaultIntent ) {
593
585
ReactWebViewClient client = ((ReactWebView ) view ).getReactWebViewClient ();
594
586
if (client != null && urlPrefixesForDefaultIntent != null ) {
595
587
client .setUrlPrefixesForDefaultIntent (urlPrefixesForDefaultIntent );
596
588
}
597
589
}
598
590
591
+ @ ReactProp (name = "allowFileAccess" )
592
+ public void setAllowFileAccess (
593
+ WebView view ,
594
+ @ Nullable Boolean allowFileAccess ) {
595
+ view .getSettings ().setAllowFileAccess (allowFileAccess != null && allowFileAccess );
596
+ }
597
+
599
598
@ ReactProp (name = "geolocationEnabled" )
600
599
public void setGeolocationEnabled (
601
600
WebView view ,
@@ -626,13 +625,13 @@ protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
626
625
@ Override
627
626
public @ Nullable Map <String , Integer > getCommandsMap () {
628
627
return MapBuilder .of (
629
- "goBack" , COMMAND_GO_BACK ,
630
- "goForward" , COMMAND_GO_FORWARD ,
631
- "reload" , COMMAND_RELOAD ,
632
- "stopLoading" , COMMAND_STOP_LOADING ,
633
- "postMessage" , COMMAND_POST_MESSAGE ,
634
- "injectJavaScript" , COMMAND_INJECT_JAVASCRIPT
635
- );
628
+ "goBack" , COMMAND_GO_BACK ,
629
+ "goForward" , COMMAND_GO_FORWARD ,
630
+ "reload" , COMMAND_RELOAD ,
631
+ "stopLoading" , COMMAND_STOP_LOADING ,
632
+ "postMessage" , COMMAND_POST_MESSAGE ,
633
+ "injectJavaScript" , COMMAND_INJECT_JAVASCRIPT
634
+ );
636
635
}
637
636
638
637
@ Override
@@ -659,13 +658,13 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
659
658
"var event;" +
660
659
"var data = " + eventInitDict .toString () + ";" +
661
660
"try {" +
662
- "event = new MessageEvent('message', data);" +
661
+ "event = new MessageEvent('message', data);" +
663
662
"} catch (e) {" +
664
- "event = document.createEvent('MessageEvent');" +
665
- "event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
663
+ "event = document.createEvent('MessageEvent');" +
664
+ "event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
666
665
"}" +
667
666
"document.dispatchEvent(event);" +
668
- "})();" );
667
+ "})();" );
669
668
} catch (JSONException e ) {
670
669
throw new RuntimeException (e );
671
670
}
0 commit comments