Skip to content

Commit

Permalink
grpc impl for AppStatus and notification sync
Browse files Browse the repository at this point in the history
Signed-off-by: ahs <[email protected]>
  • Loading branch information
amritaintel committed Jun 24, 2022
1 parent 575cd84 commit 9752167
Show file tree
Hide file tree
Showing 22 changed files with 776 additions and 709 deletions.
8 changes: 8 additions & 0 deletions penguinpeak/ClipboardAgent/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.MANAGE_NOTIFICATION_LISTENERS" />
<!-- TODO: update persistent and allowBackup to be true. -->
<application
android:name=".ClipboardAgent"
Expand All @@ -24,6 +25,13 @@
</intent-filter>
</activity>

<service android:name=".NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
<service
android:exported="false"
android:name=".GuestVsockCommService">
Expand Down
91 changes: 88 additions & 3 deletions penguinpeak/ClipboardAgent/jni/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,104 @@ cc_library_shared {
sdk_version: "current",
}

genrule {
name: "pgp-proto",
tool_files: [ ":TAF-proto-gen", ":TAF-templates", ":TAF-proto-gen-deps",],
srcs: [
"proto/appstatus-protogen.inp",
"proto/notification-protogen.inp",
],
out: [
"proto/appstatus-gen.proto",
"proto/notification-gen.proto",
],
cmd: "$(location) -I vendor/intel/external/apps/penguinpeak/ClipboardAgent/jni/ -o $(genDir) -i $(in) --proto",
}

genrule {
name: "pgp-headers",
tool_files: [ ":TAF-proto-gen", ":TAF-templates", ":TAF-proto-gen-deps",],
srcs: [
"proto/appstatus-protogen.inp",
"proto/notification-protogen.inp",
],
out: [
"proto/appstatus.h",
"proto/notification.h",
],
cmd: "$(location) -I vendor/intel/external/apps/penguinpeak/ClipboardAgent/jni/ -o $(genDir) -i $(in) --header",
}


genrule {
name: "pgp-gRPCGenStub_h",
tools: [
"aprotoc",
"protoc-gen-grpc-cpp-plugin",
],
cmd: "$(location aprotoc) -Iout/soong/.intermediates/vendor/intel/external/apps/penguinpeak/ClipboardAgent/jni/pgp-proto/gen -Ivendor/intel/external/apps/penguinpeak/ClipboardAgent/jni -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
srcs: [ ":pgp-proto",
"proto/appstatus.proto",
"proto/notification.proto",
],
out: [
"proto/appstatus.pb.h",
"proto/appstatus-gen.pb.h",
"proto/appstatus-gen.grpc.pb.h",
"proto/notification.pb.h",
"proto/notification-gen.pb.h",
"proto/notification-gen.grpc.pb.h",
],
}

genrule {
name: "pgp-gRPCGenStub_cc",
tools: [
"aprotoc",
"protoc-gen-grpc-cpp-plugin",
],
cmd: "$(location aprotoc) -Iout/soong/.intermediates/vendor/intel/external/apps/penguinpeak/ClipboardAgent/jni/pgp-proto/gen -Ivendor/intel/external/apps/penguinpeak/ClipboardAgent/jni -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
srcs: [
":pgp-proto",
"proto/appstatus.proto",
"proto/notification.proto",
],
out: [
"proto/appstatus.pb.cc",
"proto/appstatus-gen.pb.cc",
"proto/appstatus-gen.grpc.pb.cc",
"proto/notification.pb.cc",
"proto/notification-gen.pb.cc",
"proto/notification-gen.grpc.pb.cc",
],
}

cc_library_shared {
name: "libVsockMsgDispatch",
defaults: ["TAF-defaults",],
srcs: [
"VsockMsgDispatcher.cpp",
"DispatchHelper.cpp",
"adapter.cpp",
],
cflags: [
"-Wall",
"-Werror",
"-Wno-unused-parameter",
"-Wno-unused-label",
],
shared_libs: ["libbase", "liblog"],
generated_headers: [ "pgp-headers", "pgp-gRPCGenStub_h", ],
generated_sources: [ "pgp-gRPCGenStub_cc", ],
sdk_version: "current",
}


cc_binary {
name: "comm_host_agent",
host_supported: true,
defaults: ["TAF-defaults",],
srcs: [
"appstatus.cpp",
"notification.cpp",
],
generated_headers: [ "pgp-headers", "pgp-gRPCGenStub_h", ],
generated_sources: [ "pgp-gRPCGenStub_cc", ],
}
150 changes: 27 additions & 123 deletions penguinpeak/ClipboardAgent/jni/DispatchHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,142 +1,46 @@
#include "VsockMsgDispatcher.h"
#include "DispatchHelper.h"
#include <string.h>
#include <jni.h>
#include "adapter.h"

using namespace vsock;
std::map< std::string, std::vector<MSG_TYPE> > comp_msg_map {
{"ClipboardComponent", {MSG_TYPE_CLIPBOARD}},
{"AppstatusComponent", {MSG_TYPE_APPSTATUS}}
};
std::map< std::string, jclass > jclass_map;
#undef LOG_TAG
#define LOG_TAG "DispatchHelper"

static JavaVM* gVm = nullptr;
JNIEnv* getenv() {
JNIEnv *env = nullptr;
int getEnvStat = gVm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) {
LOGIT("GetEnv: not attached");
if (gVm->AttachCurrentThread(&env, NULL) != 0) {
LOGIT("Failed to attach");
}
} else if (getEnvStat == JNI_OK) {
//
} else if (getEnvStat == JNI_EVERSION) {
LOGIT("GetEnv: version not supported");
}
return env;
}

class JavaComponent:public Component {
public:
std::string java_class_name;
std::vector<MSG_TYPE> msg_list;
JavaComponent(std::string name) {
std::map< std::string, std::vector<MSG_TYPE> >::iterator it;
java_class_name = name;
it = comp_msg_map.find(name);
if (it != comp_msg_map.end()) {
msg_list = it->second;
}
}
virtual ~JavaComponent(){
JNIEnv* env = getenv();
jclass reqClass = GetJClass();
jobject singleInstance = GetSingletonInstance(reqClass);
jmethodID reqMethod = env->GetMethodID(reqClass, "stop", "()V");
env->CallVoidMethod(singleInstance, reqMethod);

}
virtual void init() {
LOGIT("init");
JNIEnv* env = getenv();
jclass reqClass = GetJClass();
jobject singleInstance = GetSingletonInstance(reqClass);
jmethodID reqMethod = env->GetMethodID(reqClass, "init", "()V");
env->CallVoidMethod(singleInstance, reqMethod);
}

virtual void ProcessMsg(Message& msg, uint64_t hndl) {
LOGIT("Process msg - %s\n", msg.payload);
JNIEnv *env = getenv();
jclass reqClass = GetJClass();
jobject singleInstance = GetSingletonInstance(reqClass);
jmethodID reqMethod = env->GetMethodID(reqClass, "processMsg", "(Ljava/lang/String;J)V");
jstring str = env->NewStringUTF(msg.payload);
env->CallVoidMethod(singleInstance, reqMethod, str, static_cast<jlong>(hndl));
}
private:
jclass GetJClass() {
std::map< std::string, jclass >::iterator it;
jclass reqClass = nullptr;
it = jclass_map.find(java_class_name.c_str());
if (it != jclass_map.end()) {
reqClass = it->second;
}
return reqClass;
}

jobject GetSingletonInstance(jclass reqClass) {
JNIEnv *env = getenv();
std::string sig = "()Lcom/intel/clipboardagent/"+java_class_name+";";
jmethodID instMethod = env->GetStaticMethodID(reqClass, "getInstance", sig.c_str());
jobject singleInstance = env->CallStaticObjectMethod(reqClass, instMethod);
return singleInstance;
}
};

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
LOGIT("In OnLoad, failed to GetEnv");
return JNI_ERR;
}
jclass tmp = nullptr;
tmp = env->FindClass("com/intel/clipboardagent/ClipboardComponent");
if (tmp!= nullptr) {
jclass_map.insert({"ClipboardComponent", (jclass)env->NewGlobalRef(tmp)});
}
tmp = env->FindClass("com/intel/clipboardagent/AppstatusComponent");
if (tmp!= nullptr) {
jclass_map.insert({"AppstatusComponent", (jclass)env->NewGlobalRef(tmp)});
}
return JNI_VERSION_1_6;
}


JNIEXPORT void JNICALL Java_com_intel_clipboardagent_DispatchHelper_registerComponent(JNIEnv *env, jobject thisObject, jstring className) {
MsgDispatcher* dispatcher = MsgDispatcher::getInstance();
env->GetJavaVM(&gVm);
std::string name = env->GetStringUTFChars(className, 0);
JavaComponent* javaComponent = new JavaComponent(name);
dispatcher->RegisterComponent(javaComponent->msg_list, javaComponent);
LOG_INFO("About to register Service %s\n", name.c_str());
ServiceAdapter* adapter = AdapterFactory::GetAdapter(name);
if (adapter != nullptr) {
LOG_INFO("Found adapter for Service %s\n", name.c_str());
adapter->Register();
LOG_INFO("Registered Service %s\n", name.c_str());
} else {
LOG_INFO("FATAL!! Service adapter not found for %s\n", name.c_str());
}
}


JNIEXPORT void JNICALL Java_com_intel_clipboardagent_DispatchHelper_sendMsg(JNIEnv *env, jobject thisObject, jstring className, jstring msg, jlong handle) {
MsgDispatcher* dispatcher = MsgDispatcher::getInstance();
std::string payload = env->GetStringUTFChars(msg, 0);
int size = env->GetStringUTFLength(msg);
std::vector<MSG_TYPE> msg_list;
std::map< std::string, std::vector<MSG_TYPE> >::iterator it;
JNIEXPORT void JNICALL Java_com_intel_clipboardagent_DispatchHelper_sendMsg(JNIEnv *env, jobject thisObject, jstring className, jobject msg, jlong handle) {
JavaObjectHelper jobjHelper(msg);
std::string name = env->GetStringUTFChars(className, 0);
it = comp_msg_map.find(name);
if (it != comp_msg_map.end()) {
msg_list = it->second;
ServiceAdapter* adapter = AdapterFactory::GetAdapter(name);
if (adapter == nullptr) {
LOG_INFO("FATAL!! Service adapter not found for %s\n", name.c_str());
return;
}
if (handle == 0) {
handle = dispatcher->GetHandleForMsgType(msg_list.front());
}
dispatcher->SendMsg(handle, msg_list.front(), payload.c_str(), size);
adapter->SendResponse(&jobjHelper);
}


JNIEXPORT void JNICALL Java_com_intel_clipboardagent_DispatchHelper_start(JNIEnv *env, jobject thisObject) {
MsgDispatcher* dispatcher = MsgDispatcher::getInstance();
dispatcher->Start();
if (!g_server_->Start()) {
LOG_INFO("FATAL! Failed to start server");
}
}

JNIEXPORT void JNICALL Java_com_intel_clipboardagent_DispatchHelper_stop(JNIEnv *env, jobject thisObject) {
MsgDispatcher* dispatcher = MsgDispatcher::getInstance();
dispatcher->Stop();
g_server_->Stop();
delete g_server_;
g_server_ = nullptr;
AdapterFactory::RemoveAll();
}

4 changes: 2 additions & 2 deletions penguinpeak/ClipboardAgent/jni/DispatchHelper.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9752167

Please sign in to comment.