feat(Android): Implement direct communication between Android code and JS (#1203)
This commit is contained in:
parent
9fbcedd6b8
commit
c88e380762
|
@ -41,12 +41,15 @@ import com.facebook.react.views.scroll.ScrollEvent;
|
||||||
import com.facebook.react.views.scroll.ScrollEventType;
|
import com.facebook.react.views.scroll.ScrollEventType;
|
||||||
import com.facebook.react.views.scroll.OnScrollDispatchHelper;
|
import com.facebook.react.views.scroll.OnScrollDispatchHelper;
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
|
import com.facebook.react.bridge.CatalystInstance;
|
||||||
import com.facebook.react.bridge.LifecycleEventListener;
|
import com.facebook.react.bridge.LifecycleEventListener;
|
||||||
import com.facebook.react.bridge.ReactContext;
|
import com.facebook.react.bridge.ReactContext;
|
||||||
import com.facebook.react.bridge.ReadableArray;
|
import com.facebook.react.bridge.ReadableArray;
|
||||||
import com.facebook.react.bridge.ReadableMap;
|
import com.facebook.react.bridge.ReadableMap;
|
||||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||||
import com.facebook.react.bridge.WritableMap;
|
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.MapBuilder;
|
||||||
import com.facebook.react.common.build.ReactBuildConfig;
|
import com.facebook.react.common.build.ReactBuildConfig;
|
||||||
import com.facebook.react.module.annotations.ReactModule;
|
import com.facebook.react.module.annotations.ReactModule;
|
||||||
|
@ -973,6 +976,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||||
protected boolean messagingEnabled = false;
|
protected boolean messagingEnabled = false;
|
||||||
protected @Nullable
|
protected @Nullable
|
||||||
RNCWebViewClient mRNCWebViewClient;
|
RNCWebViewClient mRNCWebViewClient;
|
||||||
|
protected @Nullable
|
||||||
|
CatalystInstance mCatalystInstance;
|
||||||
protected boolean sendContentSizeChangeEvents = false;
|
protected boolean sendContentSizeChangeEvents = false;
|
||||||
private OnScrollDispatchHelper mOnScrollDispatchHelper;
|
private OnScrollDispatchHelper mOnScrollDispatchHelper;
|
||||||
protected boolean hasScrollEvent = false;
|
protected boolean hasScrollEvent = false;
|
||||||
|
@ -1047,6 +1052,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||||
return new RNCWebViewBridge(webView);
|
return new RNCWebViewBridge(webView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void createCatalystInstance() {
|
||||||
|
ReactContext reactContext = (ReactContext) this.getContext();
|
||||||
|
|
||||||
|
if (reactContext != null) {
|
||||||
|
mCatalystInstance = reactContext.getCatalystInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("AddJavascriptInterface")
|
@SuppressLint("AddJavascriptInterface")
|
||||||
public void setMessagingEnabled(boolean enabled) {
|
public void setMessagingEnabled(boolean enabled) {
|
||||||
if (messagingEnabled == enabled) {
|
if (messagingEnabled == enabled) {
|
||||||
|
@ -1057,6 +1070,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
|
addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
|
||||||
|
this.createCatalystInstance();
|
||||||
} else {
|
} else {
|
||||||
removeJavascriptInterface(JAVASCRIPT_INTERFACE);
|
removeJavascriptInterface(JAVASCRIPT_INTERFACE);
|
||||||
}
|
}
|
||||||
|
@ -1085,6 +1099,9 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onMessage(String message) {
|
public void onMessage(String message) {
|
||||||
|
ReactContext reactContext = (ReactContext) this.getContext();
|
||||||
|
RNCWebView mContext = this;
|
||||||
|
|
||||||
if (mRNCWebViewClient != null) {
|
if (mRNCWebViewClient != null) {
|
||||||
WebView webView = this;
|
WebView webView = this;
|
||||||
webView.post(new Runnable() {
|
webView.post(new Runnable() {
|
||||||
|
@ -1095,16 +1112,36 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||||
}
|
}
|
||||||
WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
|
WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
|
||||||
data.putString("data", message);
|
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 {
|
} else {
|
||||||
WritableMap eventData = Arguments.createMap();
|
WritableMap eventData = Arguments.createMap();
|
||||||
eventData.putString("data", message);
|
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) {
|
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
|
||||||
super.onScrollChanged(x, y, oldX, oldY);
|
super.onScrollChanged(x, y, oldX, oldY);
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ import {
|
||||||
findNodeHandle,
|
findNodeHandle,
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
|
|
||||||
|
import BatchedBridge from 'react-native/Libraries/BatchedBridge/BatchedBridge';
|
||||||
|
|
||||||
import invariant from 'invariant';
|
import invariant from 'invariant';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -70,6 +72,10 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
|
||||||
|
|
||||||
webViewRef = React.createRef<NativeWebViewAndroid>();
|
webViewRef = React.createRef<NativeWebViewAndroid>();
|
||||||
|
|
||||||
|
componentDidMount = () => {
|
||||||
|
BatchedBridge.registerCallableModule('WebViewMessageHandler', this);
|
||||||
|
}
|
||||||
|
|
||||||
getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
|
getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
|
||||||
|
|
||||||
goForward = () => {
|
goForward = () => {
|
||||||
|
|
Loading…
Reference in New Issue