浏览代码

feat(Android): Implement direct communication between Android code and JS (#1203)

ivari 4 年前
父节点
当前提交
c88e380762
没有帐户链接到提交者的电子邮件

+ 39
- 2
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

@@ -41,12 +41,15 @@ import com.facebook.react.views.scroll.ScrollEvent;
41 41
 import com.facebook.react.views.scroll.ScrollEventType;
42 42
 import com.facebook.react.views.scroll.OnScrollDispatchHelper;
43 43
 import com.facebook.react.bridge.Arguments;
44
+import com.facebook.react.bridge.CatalystInstance;
44 45
 import com.facebook.react.bridge.LifecycleEventListener;
45 46
 import com.facebook.react.bridge.ReactContext;
46 47
 import com.facebook.react.bridge.ReadableArray;
47 48
 import com.facebook.react.bridge.ReadableMap;
48 49
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
49 50
 import com.facebook.react.bridge.WritableMap;
51
+import com.facebook.react.bridge.WritableNativeArray;
52
+import com.facebook.react.bridge.WritableNativeMap;
50 53
 import com.facebook.react.common.MapBuilder;
51 54
 import com.facebook.react.common.build.ReactBuildConfig;
52 55
 import com.facebook.react.module.annotations.ReactModule;
@@ -973,6 +976,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
973 976
     protected boolean messagingEnabled = false;
974 977
     protected @Nullable
975 978
     RNCWebViewClient mRNCWebViewClient;
979
+    protected @Nullable
980
+    CatalystInstance mCatalystInstance;
976 981
     protected boolean sendContentSizeChangeEvents = false;
977 982
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
978 983
     protected boolean hasScrollEvent = false;
@@ -1047,6 +1052,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1047 1052
       return new RNCWebViewBridge(webView);
1048 1053
     }
1049 1054
 
1055
+    protected void createCatalystInstance() {
1056
+      ReactContext reactContext = (ReactContext) this.getContext();
1057
+
1058
+      if (reactContext != null) {
1059
+        mCatalystInstance = reactContext.getCatalystInstance();
1060
+      }
1061
+    }
1062
+
1050 1063
     @SuppressLint("AddJavascriptInterface")
1051 1064
     public void setMessagingEnabled(boolean enabled) {
1052 1065
       if (messagingEnabled == enabled) {
@@ -1057,6 +1070,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1057 1070
 
1058 1071
       if (enabled) {
1059 1072
         addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
1073
+        this.createCatalystInstance();
1060 1074
       } else {
1061 1075
         removeJavascriptInterface(JAVASCRIPT_INTERFACE);
1062 1076
       }
@@ -1085,6 +1099,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1085 1099
     }
1086 1100
 
1087 1101
     public void onMessage(String message) {
1102
+      ReactContext reactContext = (ReactContext) this.getContext();
1103
+      RNCWebView mContext = this;
1104
+
1088 1105
       if (mRNCWebViewClient != null) {
1089 1106
         WebView webView = this;
1090 1107
         webView.post(new Runnable() {
@@ -1095,16 +1112,36 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1095 1112
             }
1096 1113
             WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
1097 1114
             data.putString("data", message);
1098
-            dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1115
+
1116
+            if (mCatalystInstance != null) {
1117
+              mContext.sendDirectMessage(data);
1118
+            } else {
1119
+              dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1120
+            }
1099 1121
           }
1100 1122
         });
1101 1123
       } else {
1102 1124
         WritableMap eventData = Arguments.createMap();
1103 1125
         eventData.putString("data", message);
1104
-        dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1126
+
1127
+        if (mCatalystInstance != null) {
1128
+          this.sendDirectMessage(eventData);
1129
+        } else {
1130
+          dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1131
+        }
1105 1132
       }
1106 1133
     }
1107 1134
 
1135
+    protected void sendDirectMessage(WritableMap data) {
1136
+      WritableNativeMap event = new WritableNativeMap();
1137
+      event.putMap("nativeEvent", data);
1138
+
1139
+      WritableNativeArray params = new WritableNativeArray();
1140
+      params.pushMap(event);
1141
+
1142
+      mCatalystInstance.callFunction("WebViewMessageHandler", "onMessage", params);
1143
+    }
1144
+
1108 1145
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
1109 1146
       super.onScrollChanged(x, y, oldX, oldY);
1110 1147
 

+ 6
- 0
src/WebView.android.tsx 查看文件

@@ -10,6 +10,8 @@ import {
10 10
   findNodeHandle,
11 11
 } from 'react-native';
12 12
 
13
+import BatchedBridge from 'react-native/Libraries/BatchedBridge/BatchedBridge';
14
+
13 15
 import invariant from 'invariant';
14 16
 
15 17
 import {
@@ -70,6 +72,10 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
70 72
 
71 73
   webViewRef = React.createRef<NativeWebViewAndroid>();
72 74
 
75
+  componentDidMount = () => {
76
+    BatchedBridge.registerCallableModule('WebViewMessageHandler', this);
77
+  }
78
+
73 79
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
74 80
 
75 81
   goForward = () => {