From d4961e14162e562fff54e4bb7b445637309bbe6b Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Thu, 1 Sep 2016 17:55:40 +0300 Subject: [PATCH] use RCTDeviceEventEmitter to send events to javascript; implements #195 Former-commit-id: 82bc347735fd93f312ab8cb4df2f46932cd7d7cb --- android/app/build.gradle | 2 +- .../com/statusim/geth/module/GethModule.java | 23 +++++-------------- .../statusim/geth/service/GethMessages.java | 4 ++++ .../statusim/geth/service/GethService.java | 13 ++++++++--- src/status_im/components/geth.cljs | 10 ++------ src/status_im/components/react.cljs | 1 + 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 9f35cceaba..16252077d3 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -139,7 +139,7 @@ dependencies { compile project(':react-native-image-crop-picker') compile project(':react-native-webview-bridge') //compile(name:'statusgo-android-16', ext:'aar') - compile(group: 'status-im', name: 'status-go', version: '0.1.0-574f67', ext: 'aar') + compile(group: 'status-im', name: 'status-go', version: '0.1.0-574f68-service', ext: 'aar') compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"]) } diff --git a/android/app/src/main/java/com/statusim/geth/module/GethModule.java b/android/app/src/main/java/com/statusim/geth/module/GethModule.java index 9ef4326521..0d89f154e7 100644 --- a/android/app/src/main/java/com/statusim/geth/module/GethModule.java +++ b/android/app/src/main/java/com/statusim/geth/module/GethModule.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.os.Message; import android.os.RemoteException; import com.facebook.react.bridge.*; +import com.facebook.react.modules.core.DeviceEventManagerModule; import com.statusim.geth.service.ConnectorHandler; import com.statusim.geth.service.GethConnector; import com.statusim.geth.service.GethMessages; @@ -113,6 +114,11 @@ class GethModule extends ReactContextBaseJavaModule implements LifecycleEventLis callback.invoke(result); } break; + case GethMessages.MSG_GETH_EVENT: + String event = data.getString("event"); + WritableMap params = Arguments.createMap(); + params.putString("jsonEvent", event); + getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("gethEvent", params); default: isClaimed = false; } @@ -233,21 +239,4 @@ class GethModule extends ReactContextBaseJavaModule implements LifecycleEventLis geth.completeTransaction(callbackIdentifier, hash, password); } - - private static Callback signalEventCallback; - - //todo: move this method to GethService - public static void signalEvent(String jsonEvent) { - Log.d(TAG, "Signal event: " + jsonEvent); - if(signalEventCallback != null) { - signalEventCallback.invoke(jsonEvent); - } - } - - @ReactMethod - public void registerSignalEventCallback(Callback callback) { - Log.d(TAG, "registerSignalEventCallback"); - signalEventCallback = callback; - } - } diff --git a/android/app/src/main/java/com/statusim/geth/service/GethMessages.java b/android/app/src/main/java/com/statusim/geth/service/GethMessages.java index a87c7cb86e..2194cbdd08 100644 --- a/android/app/src/main/java/com/statusim/geth/service/GethMessages.java +++ b/android/app/src/main/java/com/statusim/geth/service/GethMessages.java @@ -63,4 +63,8 @@ public class GethMessages { */ public static final int MSG_TRANSACTION_COMPLETED = 11; + /** + * Geth event + */ + public static final int MSG_GETH_EVENT = 12; } diff --git a/android/app/src/main/java/com/statusim/geth/service/GethService.java b/android/app/src/main/java/com/statusim/geth/service/GethService.java index b444ca6b93..2724a824b5 100644 --- a/android/app/src/main/java/com/statusim/geth/service/GethService.java +++ b/android/app/src/main/java/com/statusim/geth/service/GethService.java @@ -21,6 +21,8 @@ public class GethService extends Service { private static String dataFolder; + private static Messenger applicationMessenger = null; + private static class IncomingHandler extends Handler { private final WeakReference service; @@ -49,7 +51,10 @@ public class GethService extends Service { Log.d(TAG, "Signal event: " + jsonEvent); Bundle replyData = new Bundle(); replyData.putString("event", jsonEvent); - //createAndSendReply(message, GethMessages.MSG_SIGNAL_EVENT, replyData); + + Message replyMessage = Message.obtain(null, GethMessages.MSG_GETH_EVENT, 0, 0, null); + replyMessage.setData(replyData); + sendReply(applicationMessenger, replyMessage); } @Nullable @@ -221,6 +226,7 @@ public class GethService extends Service { } private void login(Message message) { + applicationMessenger = message.replyTo; Bundle data = message.getData(); String address = data.getString("address"); String password = data.getString("password"); @@ -230,6 +236,9 @@ public class GethService extends Service { Bundle replyData = new Bundle(); replyData.putString("result", result); createAndSendReply(message, GethMessages.MSG_LOGGED_IN, replyData); + + // Test signalEvent + //signalEvent("{ \"type\": \"test\", \"event\": \"test event\" }"); } private void completeTransaction(Message message){ @@ -271,9 +280,7 @@ public class GethService extends Service { private static void sendReply(Messenger messenger, Message message) { try { messenger.send(message); - } catch (Exception e) { - Log.e(TAG, "Exception sending message id: " + message.what, e); } } diff --git a/src/status_im/components/geth.cljs b/src/status_im/components/geth.cljs index c2632dc1a8..5ede7e3b68 100644 --- a/src/status_im/components/geth.cljs +++ b/src/status_im/components/geth.cljs @@ -6,14 +6,8 @@ (when (exists? (.-NativeModules r/react-native)) (.-Geth (.-NativeModules r/react-native)))) -(defn register-signal-callback [] - (when geth - (.registerSignalEventCallback - geth - #(do (dispatch [:signal-event %]) - (register-signal-callback))))) - -(register-signal-callback) +(.addListener r/device-event-emitter "gethEvent" + #(dispatch [:signal-event (.-jsonEvent %)])) (defn start-node [on-result on-already-running] (when geth diff --git a/src/status_im/components/react.cljs b/src/status_im/components/react.cljs index 4654774612..d81104d02b 100644 --- a/src/status_im/components/react.cljs +++ b/src/status_im/components/react.cljs @@ -5,6 +5,7 @@ (def react-native (u/require "react-native")) (def native-modules (.-NativeModules react-native)) +(def device-event-emitter (.-DeviceEventEmitter react-native)) (def geth (.-Geth native-modules)) (def react-native-dialogs (u/require "react-native-dialogs"))