Implement OnReceived for ambient-viewer 06/224906/13
authorInkyun Kil <inkyun.kil@samsung.com>
Fri, 14 Feb 2020 05:14:48 +0000 (14:14 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Mon, 2 Mar 2020 01:28:54 +0000 (10:28 +0900)
Change-Id: I8777d13767aabb178b1068e1cc4a39a6d83e9b8d
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
ambient-viewer/include/ambient_viewer.h
ambient-viewer/src/ambient-viewer.cc
ambient-viewer/src/ambient-viewer.h
ambient-viewer/src/stub.cc
unittest/src/test_ambient_viewer.cc

index a5c283b..3ade1e7 100644 (file)
@@ -98,4 +98,4 @@ int ambient_viewer_get_top_app_surface(ambient_viewer_h handle,
 #ifdef __cplusplus
 }
 #endif
-#endif /* __AMBIENT_VIEWER_INCLUDE_AMBIENT_VIEWER_H__ */
+#endif /* __AMBIENT_VIEWER_INCLUDE_AMBIENT_VIEWER_H__ */
\ No newline at end of file
index a90bd40..eb4ff02 100644 (file)
 #include "top-app-surface.h"
 #include "watch-surface.h"
 
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "AMBIENT_VIEWER"
+
+using namespace tizen_base;
 namespace ambient_viewer {
 
 AmbientViewer::AmbientViewer(std::shared_ptr<screen_connector::EvasObject> win)
     : win_(win) {
+  char appid_buf[512] = {0, };
+  if (aul_app_get_appid_bypid(
+      getpid(), appid_buf, sizeof(appid_buf)) != AUL_R_OK) {
+    LOGE("Failed to get appid (%d)", getpid());
+  }
+
+  char endpoint[530];
+  snprintf(endpoint, sizeof(endpoint), "send_extra_data:%s", appid_buf);
+
+  if (aul_app_com_create(endpoint, nullptr, OnReceiveSignal,
+      this, &receive_signal_conn_) != AUL_R_OK) {
+    LOGE("Failed to listen sendextra signal");
+  }
+
+  LOGD("CREATE AMBIENT VIEWER");
 }
 
 AmbientViewer::~AmbientViewer() = default;
@@ -45,13 +67,10 @@ void AmbientViewer::Unmonitor() {
   watch_surface_.reset();
 }
 
-void AmbientViewer::OnReceived(AmbientViewer::EventType ev, std::string sender,
-    bundle* extra) {
-}
-
 int AmbientViewer::NotifyAmbientEvent(bool enter, AmbientViewer::Direction dir,
-    bundle* extra) {
+    Bundle extra) {
   bundle* b;
+  bundle* extra_data;
   bundle_raw* raw = nullptr;
   int len;
   int ret;
@@ -62,10 +81,12 @@ int AmbientViewer::NotifyAmbientEvent(bool enter, AmbientViewer::Direction dir,
     return -1;
   }
 
+  extra_data = extra.GetHandle();
+
   snprintf(ambient_mode, sizeof(ambient_mode), "%d", (int)enter);
   bundle_add_str(b, "__AMBIENT_MODE__", ambient_mode);
 
-  bundle_encode(extra, &raw, &len);
+  bundle_encode(extra_data, &raw, &len);
   bundle_add_str(b, "__AMBIENT_EXTRA__", (const char*)raw);
 
   switch (dir) {
@@ -99,7 +120,6 @@ int AmbientViewer::NotifyAmbientEvent(bool enter, AmbientViewer::Direction dir,
   return 0;
 }
 
-
 const ISurface& AmbientViewer::GetWatchSurface() const {
   return *watch_surface_;
 }
@@ -108,5 +128,30 @@ const ISurface& AmbientViewer::GetTopAppSurface() const {
   return *top_app_surface_;
 }
 
+int AmbientViewer::OnReceiveSignal(const char* endpoint, aul_app_com_result_e e,
+    bundle* envelope, void* user_data)
+{
+  AmbientViewer* av = (AmbientViewer*)user_data;
+  LOGD("OnReceiveSignal");
+
+  if (!envelope) {
+    LOGE("Bad bundle data from application");
+    return -1;
+  }
+
+  Bundle b = Bundle(envelope, false, false);
+
+  std::string event_type= b.GetString("UI_APP_AMBIENT_EVENT");
+  int type = stoi(event_type);
+
+  std::string sender = b.GetString("UI_APP_AMBIENT_SENDER");
+
+  b.Delete("UI_APP_AMBIENT_EVENT");
+  b.Delete("UI_APP_AMBIENT_SENDER");
+
+  av->OnReceived((EventType)type, sender, b);
+
+  return 0;
+}
 
 }  // namespace ambient_viewer
index 48adc80..d500e57 100644 (file)
 #ifndef __AMBIENT_VIEWER_AMBIENT_VIEWER_H__
 #define __AMBIENT_VIEWER_AMBIENT_VIEWER_H__
 
+#include <aul.h>
+#include <aul_app_com.h>
 #include <bundle.h>
+#include <bundle_cpp.h>
 
 #include <string>
 #include <memory>
@@ -47,18 +50,24 @@ class EXPORT_API AmbientViewer : public IAmbientViewer {
 
  public:
   AmbientViewer(std::shared_ptr<screen_connector::EvasObject> win);
+
   virtual ~AmbientViewer();
+  virtual void OnReceived(EventType ev, std::string sender, tizen_base::Bundle extra) = 0;
+
   void Monitor();
   void Unmonitor();
-  void OnReceived(EventType ev, std::string sender, bundle *extra);
-  int NotifyAmbientEvent(bool enter, Direction dir, bundle *extra);
+  int NotifyAmbientEvent(bool enter, Direction dir, tizen_base::Bundle extra);
   const ISurface& GetWatchSurface() const;
   const ISurface& GetTopAppSurface() const;
 
  private:
+  static int OnReceiveSignal(const char *endpoint, aul_app_com_result_e e,
+      bundle *envelope, void *user_data);
+
   std::shared_ptr<screen_connector::EvasObject> win_;
   std::shared_ptr<ISurface> watch_surface_;
   std::shared_ptr<ISurface> top_app_surface_;
+  aul_app_com_connection_h receive_signal_conn_;
 };
 
 }  // namespace ambient_viewer
index 8e0f2b5..cfbb737 100644 (file)
@@ -25,6 +25,7 @@
 #include "isurface.h"
 
 using namespace std;
+using namespace tizen_base;
 using namespace ambient_viewer;
 using namespace screen_connector;
 
@@ -57,15 +58,17 @@ class MonitorCallbackInfo {
   void* user_data_;
 };
 
-class EventCallbackInfo {
+class ReceiveCallbackInfo {
  public:
-  EventCallbackInfo(ambient_viewer_event_cb cb, void* user_data)
+  ReceiveCallbackInfo(ambient_viewer_event_cb cb, void* user_data)
     : cb_(cb), user_data_(user_data) {
   }
 
-  void InvokeEventCallback(AmbientViewer::EventType event, std::string sender,
-      bundle *extra) {
-    cb_(static_cast<ambient_event_type_e>(event), sender.c_str(), extra,
+  void InvokeReceiveCallback(AmbientViewer::EventType event, std::string sender,
+      Bundle extra) {
+    bundle *b;
+    b = extra.GetHandle();
+    cb_(static_cast<ambient_event_type_e>(event), sender.c_str(), b,
         user_data_);
   }
 
@@ -92,8 +95,8 @@ class AmbientViewerStub : public AmbientViewer {
     monitor_cb_->InvokeRemoved(surface);
   }
 
-  void OnEvent(EventType ev, string sender, bundle* extra) {
-    event_cb_->InvokeEventCallback(ev, sender, extra);
+  void OnReceived(EventType ev, string sender, Bundle extra) {
+    event_cb_->InvokeReceiveCallback(ev, sender, extra);
   }
 
   int SetMonitorCallbackInfo(unique_ptr<MonitorCallbackInfo> info) {
@@ -106,19 +109,19 @@ class AmbientViewerStub : public AmbientViewer {
     return AMBIENT_VIEWER_ERROR_NONE;
   }
 
-  int SetEventCallbackInfo (unique_ptr<EventCallbackInfo> info) {
+  int SetReceiveCallbackInfo (unique_ptr<ReceiveCallbackInfo> info) {
     event_cb_ = move(info);
     return AMBIENT_VIEWER_ERROR_NONE;
   }
 
-  int ClearEventCallbackInfo() {
+  int ClearReceiveCallbackInfo() {
     event_cb_.reset();
     return AMBIENT_VIEWER_ERROR_NONE;
   }
 
  private:
   unique_ptr<MonitorCallbackInfo> monitor_cb_;
-  unique_ptr<EventCallbackInfo> event_cb_;
+  unique_ptr<ReceiveCallbackInfo> event_cb_;
 };
 
 extern "C" EXPORT_API int ambient_viewer_create(Evas_Object *win,
@@ -129,7 +132,7 @@ extern "C" EXPORT_API int ambient_viewer_create(Evas_Object *win,
     return AMBIENT_VIEWER_ERROR_INVALID_PARAMETER;
   }
 
-  AmbientViewerStub* h = new (std::nothrow) AmbientViewerStub(
+  AmbientViewerStub* h = new (nothrow) AmbientViewerStub(
     shared_ptr<EvasObject>(new EvasObject(win, false)));
   if (h == nullptr) {
     LOGE("out of memory");
@@ -164,7 +167,7 @@ extern "C" EXPORT_API int ambient_viewer_notify_ambient_event(
   }
 
   AmbientViewerStub* stub = static_cast<AmbientViewerStub*>(handle);
-  stub->NotifyAmbientEvent(enter, static_cast<AmbientViewer::Direction>(dir), extra);
+  stub->NotifyAmbientEvent(enter, static_cast<AmbientViewer::Direction>(dir), Bundle(extra, false, false));
 
   return AMBIENT_VIEWER_ERROR_NONE;
 }
@@ -217,14 +220,14 @@ extern "C" EXPORT_API int ambient_viewer_set_event_listener(
 
   AmbientViewerStub* stub = static_cast<AmbientViewerStub*>(handle);
 
-  unique_ptr<EventCallbackInfo> info (
-    new (nothrow)EventCallbackInfo(callback, user_data));
+  unique_ptr<ReceiveCallbackInfo> info (
+    new (nothrow)ReceiveCallbackInfo(callback, user_data));
   if (info.get() == nullptr) {
     LOGE("out of memory");
     return AMBIENT_VIEWER_ERROR_OUT_OF_MEMORY;
   }
 
-  stub->SetEventCallbackInfo(move(info));
+  stub->SetReceiveCallbackInfo(move(info));
 
   return AMBIENT_VIEWER_ERROR_NONE;
 }
@@ -238,7 +241,7 @@ extern "C" EXPORT_API int ambient_viewer_unset_event_listener(
   }
 
   AmbientViewerStub* stub = static_cast<AmbientViewerStub*>(handle);
-  stub->ClearEventCallbackInfo();
+  stub->ClearReceiveCallbackInfo();
 
   return AMBIENT_VIEWER_ERROR_NONE;
 }
index 24d088e..5dc8517 100644 (file)
@@ -23,6 +23,7 @@
 using namespace std;
 using namespace ambient_viewer;
 using namespace screen_connector;
+using namespace tizen_base;
 
 class AmbientViewerStub : public AmbientViewer {
  public:
@@ -32,6 +33,7 @@ class AmbientViewerStub : public AmbientViewer {
   void OnAdded(const ISurface& surface) override {}
   void OnUpdated(const ISurface& surface) override {}
   void OnRemoved(const ISurface& surface) override {}
+  void OnReceived(EventType ev, string sender, Bundle extra) override {}
 };
 
 class AmbientViewerTest : public ::testing::Test {