2
2
3
3
import android .webkit .WebView ;
4
4
5
- import com .facebook .react .bridge .ReactContext ;
6
5
import com .facebook .react .bridge .ReadableArray ;
6
+ import com .facebook .react .uimanager .ThemedReactContext ;
7
7
import com .facebook .react .views .webview .ReactWebViewManager ;
8
- import com .facebook .react .views . webview . WebViewConfig ;
8
+ import com .facebook .react .uimanager . annotations . ReactProp ;
9
9
10
+ import java .util .ArrayList ;
10
11
import java .util .Map ;
11
12
12
13
import javax .annotation .Nullable ;
13
14
14
15
public class WebViewBridgeManager extends ReactWebViewManager {
15
- private static final String REACT_CLASS = "RCTWebViewBridge" ;
16
-
17
- public static final int COMMAND_INJECT_BRIDGE_SCRIPT = 100 ;
18
- public static final int COMMAND_SEND_TO_BRIDGE = 101 ;
19
-
20
- private boolean initializedBridge ;
21
-
22
- public WebViewBridgeManager () {
23
- super ();
24
- initializedBridge = false ;
25
- }
26
-
27
- public WebViewBridgeManager (WebViewConfig webViewConfig ) {
28
- super (webViewConfig );
29
- initializedBridge = false ;
30
- }
31
-
32
- @ Override
33
- public String getName () {
34
- return REACT_CLASS ;
35
- }
36
-
37
- @ Override
38
- public @ Nullable Map <String , Integer > getCommandsMap () {
39
- Map <String , Integer > commandsMap = super .getCommandsMap ();
40
-
41
- commandsMap .put ("injectBridgeScript" , COMMAND_INJECT_BRIDGE_SCRIPT );
42
- commandsMap .put ("sendToBridge" , COMMAND_SEND_TO_BRIDGE );
43
-
44
- return commandsMap ;
45
- }
46
-
47
- @ Override
48
- public void receiveCommand (WebView root , int commandId , @ Nullable ReadableArray args ) {
49
- super .receiveCommand (root , commandId , args );
50
-
51
- switch (commandId ) {
52
- case COMMAND_INJECT_BRIDGE_SCRIPT :
53
- injectBridgeScript (root );
54
- break ;
55
- case COMMAND_SEND_TO_BRIDGE :
56
- sendToBridge (root , args .getString (0 ));
57
- break ;
58
- default :
59
- //do nothing!!!!
16
+ private static final String REACT_CLASS = "RCTWebViewBridge" ;
17
+
18
+ public static final int COMMAND_SEND_TO_BRIDGE = 101 ;
19
+
20
+ @ Override
21
+ public String getName () {
22
+ return REACT_CLASS ;
23
+ }
24
+
25
+ private ArrayList <Integer > webViewsWithBridgeScript = new ArrayList <Integer >();
26
+
27
+ @ Override
28
+ public
29
+ @ Nullable
30
+ Map <String , Integer > getCommandsMap () {
31
+ Map <String , Integer > commandsMap = super .getCommandsMap ();
32
+
33
+ commandsMap .put ("sendToBridge" , COMMAND_SEND_TO_BRIDGE );
34
+
35
+ return commandsMap ;
36
+ }
37
+
38
+ @ Override
39
+ protected WebView createViewInstance (ThemedReactContext reactContext ) {
40
+ WebView root = super .createViewInstance (reactContext );
41
+ root .addJavascriptInterface (new JavascriptBridge (root ), "WebViewBridge" );
42
+ return root ;
43
+ }
44
+
45
+ @ Override
46
+ public void receiveCommand (WebView root , int commandId , @ Nullable ReadableArray args ) {
47
+ super .receiveCommand (root , commandId , args );
48
+ // if (true) return;
49
+
50
+ switch (commandId ) {
51
+ case COMMAND_SEND_TO_BRIDGE :
52
+ sendToBridge (root , args .getString (0 ));
53
+ break ;
54
+ default :
55
+ //do nothing!!!!
56
+ }
57
+ }
58
+
59
+ private void sendToBridge (WebView root , String message ) {
60
+ //root.loadUrl("javascript:(function() {\n" + script + ";\n})();");
61
+ String script = "WebViewBridge.onMessage('" + message + "');" ;
62
+ WebViewBridgeManager .evaluateJavascript (root , script );
63
+ }
64
+
65
+ static private void evaluateJavascript (WebView root , String javascript ) {
66
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
67
+ root .evaluateJavascript (javascript , null );
68
+ } else {
69
+ root .loadUrl ("javascript:" + javascript );
70
+ }
60
71
}
61
- }
62
-
63
- private void sendToBridge (WebView root , String message ) {
64
- //root.loadUrl("javascript:(function() {\n" + script + ";\n})();");
65
- String script = "WebViewBridge.onMessage('" + message + "');" ;
66
- WebViewBridgeManager .evaluateJavascript (root , script );
67
- }
68
-
69
- private void injectBridgeScript (WebView root ) {
70
- //this code needs to be called once per context
71
- if (!initializedBridge ) {
72
- root .addJavascriptInterface (new JavascriptBridge ((ReactContext ) root .getContext ()), "WebViewBridgeAndroid" );
73
- initializedBridge = true ;
74
- root .reload ();
72
+
73
+ @ ReactProp (name = "allowFileAccessFromFileURLs" )
74
+ public void setAllowFileAccessFromFileURLs (WebView root , boolean allows ) {
75
+ root .getSettings ().setAllowFileAccessFromFileURLs (allows );
75
76
}
76
77
77
- // this code needs to be executed everytime a url changes.
78
- WebViewBridgeManager .evaluateJavascript (root , ""
79
- + "(function() {"
80
- + "if (window.WebViewBridge) return;"
81
- + "var customEvent = document.createEvent('Event');"
82
- + "var WebViewBridge = {"
83
- + "send: function(message) { WebViewBridgeAndroid.send(message); },"
84
- + "onMessage: function() {}"
85
- + "};"
86
- + "window.WebViewBridge = WebViewBridge;"
87
- + "customEvent.initEvent('WebViewBridge', true, true);"
88
- + "document.dispatchEvent(customEvent);"
89
- + "}());" );
90
- }
91
-
92
- static private void evaluateJavascript (WebView root , String javascript ) {
93
- if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
94
- root .evaluateJavascript (javascript , null );
95
- } else {
96
- root .loadUrl ("javascript:" + javascript );
78
+ @ ReactProp (name = "allowUniversalAccessFromFileURLs" )
79
+ public void setAllowUniversalAccessFromFileURLs (WebView root , boolean allows ) {
80
+ root .getSettings ().setAllowUniversalAccessFromFileURLs (allows );
97
81
}
98
- }
99
- }
82
+ }
0 commit comments