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 ;
9
8
9
+ import java .util .ArrayList ;
10
10
import java .util .Map ;
11
11
12
12
import javax .annotation .Nullable ;
13
13
14
14
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!!!!
15
+ private static final String REACT_CLASS = "RCTWebViewBridge" ;
16
+
17
+ public static final int COMMAND_SEND_TO_BRIDGE = 101 ;
18
+
19
+ @ Override
20
+ public String getName () {
21
+ return REACT_CLASS ;
22
+ }
23
+
24
+ private ArrayList <Integer > webViewsWithBridgeScript = new ArrayList <Integer >();
25
+
26
+ @ Override
27
+ public
28
+ @ Nullable
29
+ Map <String , Integer > getCommandsMap () {
30
+ Map <String , Integer > commandsMap = super .getCommandsMap ();
31
+
32
+ commandsMap .put ("sendToBridge" , COMMAND_SEND_TO_BRIDGE );
33
+
34
+ return commandsMap ;
35
+ }
36
+
37
+ @ Override
38
+ protected WebView createViewInstance (ThemedReactContext reactContext ) {
39
+ WebView root = super .createViewInstance (reactContext );
40
+ root .getSettings ().setAllowFileAccessFromFileURLs (true );
41
+ root .getSettings ().setAllowUniversalAccessFromFileURLs (true );
42
+ root .addJavascriptInterface (new JavascriptBridge (root ), "WebViewBridge" );
43
+ return root ;
60
44
}
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 ();
45
+
46
+ @ Override
47
+ public void receiveCommand (WebView root , int commandId , @ Nullable ReadableArray args ) {
48
+ super .receiveCommand (root , commandId , args );
49
+ // if (true) return;
50
+
51
+ switch (commandId ) {
52
+ case COMMAND_SEND_TO_BRIDGE :
53
+ sendToBridge (root , args .getString (0 ));
54
+ break ;
55
+ default :
56
+ //do nothing!!!!
57
+ }
58
+ }
59
+
60
+ private void sendToBridge (WebView root , String message ) {
61
+ //root.loadUrl("javascript:(function() {\n" + script + ";\n})();");
62
+ String script = "WebViewBridge.onMessage('" + message + "');" ;
63
+ WebViewBridgeManager .evaluateJavascript (root , script );
75
64
}
76
65
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 );
66
+ static private void evaluateJavascript (WebView root , String javascript ) {
67
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
68
+ root .evaluateJavascript (javascript , null );
69
+ } else {
70
+ root .loadUrl ("javascript:" + javascript );
71
+ }
97
72
}
98
- }
99
- }
73
+ }
0 commit comments