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

This commit is contained in:
ivari
2020-04-06 10:25:28 -07:00
committed by GitHub
parent 9fbcedd6b8
commit c88e380762
2 changed files with 45 additions and 2 deletions
@@ -41,12 +41,15 @@ import com.facebook.react.views.scroll.ScrollEvent;
import com.facebook.react.views.scroll.ScrollEventType;
import com.facebook.react.views.scroll.OnScrollDispatchHelper;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.module.annotations.ReactModule;
@@ -973,6 +976,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
protected boolean messagingEnabled = false;
protected @Nullable
RNCWebViewClient mRNCWebViewClient;
protected @Nullable
CatalystInstance mCatalystInstance;
protected boolean sendContentSizeChangeEvents = false;
private OnScrollDispatchHelper mOnScrollDispatchHelper;
protected boolean hasScrollEvent = false;
@@ -1047,6 +1052,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
return new RNCWebViewBridge(webView);
}
protected void createCatalystInstance() {
ReactContext reactContext = (ReactContext) this.getContext();
if (reactContext != null) {
mCatalystInstance = reactContext.getCatalystInstance();
}
}
@SuppressLint("AddJavascriptInterface")
public void setMessagingEnabled(boolean enabled) {
if (messagingEnabled == enabled) {
@@ -1057,6 +1070,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
if (enabled) {
addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
this.createCatalystInstance();
} else {
removeJavascriptInterface(JAVASCRIPT_INTERFACE);
}
@@ -1085,6 +1099,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
public void onMessage(String message) {
ReactContext reactContext = (ReactContext) this.getContext();
RNCWebView mContext = this;
if (mRNCWebViewClient != null) {
WebView webView = this;
webView.post(new Runnable() {
@@ -1095,16 +1112,36 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
}
WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
data.putString("data", message);
dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
if (mCatalystInstance != null) {
mContext.sendDirectMessage(data);
} else {
dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
}
}
});
} else {
WritableMap eventData = Arguments.createMap();
eventData.putString("data", message);
dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
if (mCatalystInstance != null) {
this.sendDirectMessage(eventData);
} else {
dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
}
}
}
protected void sendDirectMessage(WritableMap data) {
WritableNativeMap event = new WritableNativeMap();
event.putMap("nativeEvent", data);
WritableNativeArray params = new WritableNativeArray();
params.pushMap(event);
mCatalystInstance.callFunction("WebViewMessageHandler", "onMessage", params);
}
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);