Add state tracking plumbing

Summary:
The plan is to get SonarState to take update events and aggregate them into an object, that can be displayed in the UI so you can see what sonar is currently trying to do, and what if anything is failing.
This is pretty rubbish right now, as it just uses a single string to represent state, this is just the initial version I'll iterate on.
I also need to pass the SonarState into the SonarWebSocketImpl, since that's where a lot of the heavy lifting is done, but as long as something is logging state updates here, it proves the concept.
SonarStateUpdateListener is the interface that will be implemented by android and iOS implementations. These implementations will notify an activity or screen that the state has changed and they should reflect that.

Reviewed By: passy

Differential Revision: D8932114

fbshipit-source-id: fb03babfe92be53771eb4d8f10de7ecdc7f1a6d8
This commit is contained in:
John Knox
2018-08-03 07:34:32 -07:00
committed by Facebook Github Bot
parent 3d66b1c9d0
commit fb447e4f84
11 changed files with 259 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
#include <Sonar/SonarWebSocket.h>
#include <Sonar/SonarConnection.h>
#include <Sonar/SonarResponder.h>
#include <Sonar/SonarStateUpdateListener.h>
using namespace facebook;
using namespace facebook::sonar;
@@ -198,6 +199,37 @@ class JSonarPlugin : public jni::JavaClass<JSonarPlugin> {
}
};
class JSonarStateUpdateListener : public jni::JavaClass<JSonarStateUpdateListener> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarStateUpdateListener;";
void onUpdate() {
static const auto method = javaClassStatic()->getMethod<void()>("onUpdate");
method(self());
}
void onStepStarted(std::string step) {
static const auto method = javaClassStatic()->getMethod<void(std::string)>("onStepStarted");
method(self(), step);
}
void onStepSuccess(std::string step) {
static const auto method = javaClassStatic()->getMethod<void(std::string)>("onStepSuccess");
method(self(), step);
}
void onStepFailed(std::string step, std::string errorMessage) {
static const auto method = javaClassStatic()->getMethod<void(std::string, std::string)>("onStepFailed");
method(self(), step, errorMessage);
}
};
class AndroidSonarStateUpdateListener : public SonarStateUpdateListener {
public:
AndroidSonarStateUpdateListener(jni::alias_ref<JSonarStateUpdateListener> stateListener);
void onUpdate();
private:
jni::global_ref<JSonarStateUpdateListener> jStateListener;
};
class JSonarPluginWrapper : public SonarPlugin {
public:
jni::global_ref<JSonarPlugin> jplugin;
@@ -229,7 +261,10 @@ class JSonarClient : public jni::HybridClass<JSonarClient> {
makeNativeMethod("stop", JSonarClient::stop),
makeNativeMethod("addPlugin", JSonarClient::addPlugin),
makeNativeMethod("removePlugin", JSonarClient::removePlugin),
makeNativeMethod("subscribeForUpdates", JSonarClient::subscribeForUpdates),
makeNativeMethod("unsubscribe", JSonarClient::unsubscribe),
makeNativeMethod("getPlugin", JSonarClient::getPlugin),
makeNativeMethod("getState", JSonarClient::getState),
});
}
@@ -256,6 +291,22 @@ class JSonarClient : public jni::HybridClass<JSonarClient> {
client->removePlugin(client->getPlugin(plugin->identifier()));
}
void subscribeForUpdates(jni::alias_ref<JSonarStateUpdateListener> stateListener) {
auto client = SonarClient::instance();
mStateListener = std::make_shared<AndroidSonarStateUpdateListener>(stateListener);
client->setStateListener(mStateListener);
}
void unsubscribe() {
auto client = SonarClient::instance();
mStateListener = nullptr;
client->setStateListener(nullptr);
}
std::string getState() {
return SonarClient::instance()->getState();
}
jni::alias_ref<JSonarPlugin> getPlugin(const std::string& identifier) {
auto plugin = SonarClient::instance()->getPlugin(identifier);
if (plugin) {
@@ -295,6 +346,7 @@ class JSonarClient : public jni::HybridClass<JSonarClient> {
private:
friend HybridBase;
std::shared_ptr<SonarStateUpdateListener> mStateListener = nullptr;
JSonarClient() {}
};
@@ -309,3 +361,11 @@ jint JNI_OnLoad(JavaVM* vm, void*) {
JEventBase::registerNatives();
});
}
AndroidSonarStateUpdateListener::AndroidSonarStateUpdateListener(jni::alias_ref<JSonarStateUpdateListener> stateListener) {
jStateListener = jni::make_global(stateListener);
}
void AndroidSonarStateUpdateListener::onUpdate() {
jStateListener->onUpdate();
}