Implement Notification manager
authorSeungkeun Lee <sngn.lee@samsung.com>
Mon, 11 May 2015 07:04:32 +0000 (16:04 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Tue, 12 May 2015 00:30:49 +0000 (09:30 +0900)
Change-Id: I358b4231147f946c2480df312ed5affa9bf5ae22

packaging/wrt.spec
src/runtime/CMakeLists.txt
src/runtime/notification_manager.cc [new file with mode: 0755]
src/runtime/notification_manager.h [new file with mode: 0755]
src/runtime/web_application.cc

index b248aeb..d202200 100755 (executable)
@@ -28,6 +28,7 @@ BuildRequires: pkgconfig(uuid)
 BuildRequires: pkgconfig(gio-2.0)
 BuildRequires: pkgconfig(aul)
 BuildRequires: pkgconfig(ecore)
+BuildRequires: pkgconfig(notification)
 BuildRequires: boost-devel
 %if %{with x}
 BuildRequires: pkgconfig(ecore-x)
index 1363a21..38fce98 100755 (executable)
@@ -19,6 +19,7 @@ PKG_CHECK_MODULES(TARGET_RUNTIME_DEPS
   efl-assist
   deviced
   ecore
+  notification
   REQUIRED
 )
 
@@ -47,6 +48,7 @@ SET(TARGET_RUNTIME_SRCS
   ${BASE_SRCDIR}/runtime/web_view_impl.cc
   ${BASE_SRCDIR}/runtime/vibration_manager.cc
   ${BASE_SRCDIR}/runtime/app_db.cc
+  ${BASE_SRCDIR}/runtime/notification_manager.cc
 )
 
 # Compiler Flags
diff --git a/src/runtime/notification_manager.cc b/src/runtime/notification_manager.cc
new file mode 100755 (executable)
index 0000000..d582c94
--- /dev/null
@@ -0,0 +1,106 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "runtime/notification_manager.h"
+
+#include <notification.h>
+#include <memory>
+
+#include "common/logger.h"
+
+namespace wrt {
+
+NotificationManager* NotificationManager::GetInstance() {
+  static NotificationManager instance;
+  return &instance;
+}
+
+NotificationManager::NotificationManager() {
+}
+
+bool NotificationManager::Show(uint64_t tag,
+                               const std::string& title,
+                               const std::string& body,
+                               const std::string& icon_path) {
+  auto found = keymapper_.find(tag);
+  if (found != keymapper_.end()) {
+    Hide(tag);
+  }
+
+  notification_h noti_h = NULL;
+  int ret = NOTIFICATION_ERROR_NONE;
+  noti_h = notification_new(
+      NOTIFICATION_TYPE_NOTI,
+      NOTIFICATION_GROUP_ID_DEFAULT,
+      NOTIFICATION_PRIV_ID_NONE);
+  if (noti_h == NULL) {
+    LOGGER(ERROR) << "Can't create notification handle";
+    return false;
+  }
+
+  std::unique_ptr<std::remove_pointer<notification_h>::type,
+                  decltype(notification_free)*>
+      auto_release {noti_h, notification_free};
+
+  // set notification title
+  ret = notification_set_text(
+      noti_h,
+      NOTIFICATION_TEXT_TYPE_TITLE,
+      title.c_str(),
+      NULL,
+      NOTIFICATION_VARIABLE_TYPE_NONE);
+  if (ret != NOTIFICATION_ERROR_NONE) {
+    LOGGER(ERROR) << "Can't set title";
+    return false;
+  }
+
+  // set notification content
+  ret = notification_set_text(
+      noti_h,
+      NOTIFICATION_TEXT_TYPE_CONTENT,
+      body.c_str(),
+      NULL,
+      NOTIFICATION_VARIABLE_TYPE_NONE);
+  if (ret != NOTIFICATION_ERROR_NONE) {
+    LOGGER(ERROR) << "Can't set content";
+    return false;
+  }
+
+  if (!icon_path.empty()) {
+    ret = notification_set_image(
+        noti_h,
+        NOTIFICATION_IMAGE_TYPE_ICON,
+        icon_path.c_str());
+    if (ret != NOTIFICATION_ERROR_NONE) {
+      LOGGER(ERROR) << "Can't set icon";
+      return false;
+    }
+  }
+
+  // insert notification
+  int platform_key = NOTIFICATION_PRIV_ID_NONE;
+  ret = notification_insert(noti_h, &platform_key);
+  if (ret != NOTIFICATION_ERROR_NONE) {
+    LOGGER(ERROR) << "Can't insert notification";
+    return false;
+  }
+  keymapper_[tag] = platform_key;
+  return true;
+}
+
+bool NotificationManager::Hide(uint64_t tag) {
+  auto found = keymapper_.find(tag);
+  if (found == keymapper_.end()) {
+    LOGGER(ERROR) << "Can't find notification";
+    return false;
+  }
+  notification_delete_by_priv_id(NULL,
+                                 NOTIFICATION_TYPE_NOTI,
+                                 found->second);
+  keymapper_.erase(found);
+  return true;
+}
+
+
+}  // namespace wrt
diff --git a/src/runtime/notification_manager.h b/src/runtime/notification_manager.h
new file mode 100755 (executable)
index 0000000..7df5109
--- /dev/null
@@ -0,0 +1,26 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WRT_RUNTIME_NOTIFICATION_MANAGER_H_
+#define WRT_RUNTIME_NOTIFICATION_MANAGER_H_
+
+#include <string>
+#include <map>
+
+namespace wrt {
+class NotificationManager {
+ public:
+  static NotificationManager* GetInstance();
+  bool Show(uint64_t tag,
+            const std::string& title,
+            const std::string& body,
+            const std::string& icon_path);
+  bool Hide(uint64_t tag);
+ private:
+  NotificationManager();
+  std::map<uint64_t, int> keymapper_;
+};
+}  // namespace wrt
+
+#endif   // WRT_RUNTIME_NOTIFICATION_MANAGER_H_
index baf6b3e..8ac9048 100755 (executable)
@@ -25,6 +25,7 @@
 #include "common/application_data.h"
 #include "common/resource_manager.h"
 #include "runtime/app_db.h"
+#include "runtime/notification_manager.h"
 
 namespace wrt {
 
@@ -79,6 +80,7 @@ const char* kLocationPrivilege =
     "http://tizen.org/privilege/location";
 const char* kStoragePrivilege =
     "http://tizen.org/privilege/unlimitedstorage";
+const char* kNotiIconFile = "noti_icon.png";
 
 const char* kVisibilitySuspendFeature = "visibility,suspend";
 const char* kMediastreamRecordFeature = "mediastream,record";
@@ -127,6 +129,38 @@ static void SendDownloadRequest(const std::string& url) {
   request.LaunchRequest();
 }
 
+static void InitializeNotificationCallback(Ewk_Context* ewk_context,
+                                           WebApplication* app) {
+  auto show = [](Ewk_Context*,
+                 Ewk_Notification* noti,
+                 void* user_data) {
+    WebApplication* self = static_cast<WebApplication*>(user_data);
+    if (self == NULL)
+      return;
+    uint64_t id = ewk_notification_id_get(noti);
+    std::string title(ewk_notification_title_get(noti) ?
+                      ewk_notification_title_get(noti) : "");
+    std::string body(ewk_notification_body_get(noti) ?
+                     ewk_notification_body_get(noti) : "");
+    std::string icon_path = self->data_path() + "/" + kNotiIconFile;
+    if (!ewk_notification_icon_save_as_png(noti, icon_path.c_str())) {
+      icon_path = "";
+    }
+    if (NotificationManager::GetInstance()->Show(id, title, body, icon_path))
+      ewk_notification_showed(id);
+  };
+  auto hide = [](Ewk_Context*,
+                 uint64_t noti_id,
+                 void *) {
+    NotificationManager::GetInstance()->Hide(noti_id);
+    ewk_notification_closed(noti_id, EINA_FALSE);
+  };
+  ewk_context_notification_callbacks_set(ewk_context,
+                                         show,
+                                         hide,
+                                         app);
+}
+
 }  // namespace
 
 WebApplication::WebApplication(
@@ -190,6 +224,7 @@ bool WebApplication::Initialize() {
   ewk_context_did_start_download_callback_set(ewk_context_,
                                               download_callback,
                                               this);
+  InitializeNotificationCallback(ewk_context_, this);
 
   if (FindPrivilege(app_data_.get(), kFullscreenPrivilege)) {
     ewk_context_tizen_extensible_api_string_set(ewk_context_,