[SECIOTSRK-391] Notification popup for Tizen TV App
authori.metelytsia <i.metelytsia@samsung.com>
Wed, 30 Aug 2017 12:52:57 +0000 (15:52 +0300)
committeri.metelytsia <i.metelytsia@samsung.com>
Wed, 30 Aug 2017 12:52:57 +0000 (15:52 +0300)
tv-widget/proxy/CMakeLists.txt
tv-widget/proxy/inc/network/nm_types.h
tv-widget/proxy/inc/push_notification.h
tv-widget/proxy/packaging/iot-manager-dashboard-proxy.spec
tv-widget/proxy/src/network/network_manager.cpp
tv-widget/proxy/src/push_notification.cpp
tv-widget/proxy/ut/CMakeLists.txt

index c4d804f..1fc9113 100644 (file)
@@ -11,7 +11,7 @@ list(APPEND LIBJSON_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libjson
                              ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libjson/_internal/Source/JSONDefs)
 add_subdirectory(thirdparty)
 
-pkg_check_modules(PROXY_REQ_PKGS REQUIRED dlog boost global-function)
+pkg_check_modules(PROXY_REQ_PKGS REQUIRED dlog boost notification)
 
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-result -std=c++11") #-Werror
 
index 6e3b09d..2ced064 100644 (file)
@@ -84,12 +84,23 @@ typedef enum {
     LAST_CALLBACK = 2,
 } CallbackState;
 
+typedef enum
+{
+    NT_Undefined = 0,
+    NT_Notify = 1,
+    NT_RemoveAppRequest = 2,
+} NM_NotificationType;
+
 typedef struct
 {
-    int code;
+    NM_NotificationType type;
+    int pid;
+    int time;
     const char* title;
     const char* message;
-    int time;
+    const char* duid;
+    const char* policy;
+    const char* appname;
     CallbackState callbackState;
 } NM_NotificationData;
 
index e524cd9..2abd2e9 100644 (file)
@@ -13,7 +13,8 @@
 #define PUSH_NOTIFICATION_H_
 
 #include <string>
-#include <app_control.h>
+
+#include <notification.h>
 
 namespace iotswsec
 {
@@ -21,35 +22,22 @@ namespace iotswsec
 class PushNotification
 {
 public:
-    /**
-        @brief Return Notification singleton instance.
-        @return Notification instance.
-    */
-    static PushNotification& GetInstance();
-
-    /**
-        @brief Push Tizen notification with title and body text.
-        @param[in] title - title text.
-        @param[in] body - body text.
-    */
-    void Push(const std::string& title, const std::string& body) const;
+    PushNotification(const std::string& title, const std::string& body);
+    virtual ~PushNotification();
 
-    void LaunchDashboardApp();
-
-    ~PushNotification();
-private:
-    PushNotification();
-    void Init();
-    void Deinit();
     PushNotification(const PushNotification& obj) = delete;
     PushNotification& operator=(const PushNotification&) = delete;
-    static void OnButtonClickCallback(int itemId, int buttonIndex, void* userData);
-    static void OnNotificationTimeOutCB(int nItemId, void* userdata);
-    static void OnNotificationHideCallback(char* pAppName, void* userdata);
+
+    /**
+        @brief Push Tizen notification.
+        @return 0 on success, otherwise any other value on failure
+    */
+    int push() const;
 
 private:
-    app_control_h m_appContext;
+    notification_h m_notification;
 };
+
 }
 
 #endif /* PUSH_NOTIFICATION_H_ */
index 3f71cad..d42ead4 100755 (executable)
@@ -11,8 +11,8 @@ BuildRequires: pkgconfig(boost)
 BuildRequires: cmake >= 2.8
 BuildRequires: gtest
 BuildRequires: gtest-devel
-BuildRequires: pkgconfig(global-function)
 BuildRequires: pkgconfig(capi-appfw-application)
+BuildRequires: pkgconfig(notification)
 
 %define _proxy_packagedir /usr/apps/%{name}
 %define _proxy_bindir %{_proxy_packagedir}/bin
index e35aee5..9a81e4c 100644 (file)
@@ -241,17 +241,39 @@ static void PushNotificationCallback(NM_NotificationData data, void* userData)
 {
     LOG_DBG("BEGIN");
 
-    if (data.message && data.title)
+    switch(data.type)
     {
-        PushNotification::GetInstance().Push(data.title, data.message);
+    case NT_Notify:
+        {
+            if(data.title && data.message)
+            {
+                try
+                {
+                    PushNotification(data.title, data.message).push();
+                }
+                catch(const std::runtime_error& ex)
+                {
+                    LOG_ERR("Failed to push notification, exception was thrown: %s", ex.what());
+                }
+            }
+            else
+            {
+                LOG_ERR("Invalid notification data.");
+            }
+        }
+        break;
 
-        /*NetworkManager* nm = reinterpret_cast<NetworkManager*>(userData);
-        nm->NotifySubscribers(data.message, data.title);*/
-    }
-    else
-    {
-        LOG_ERR("Notification generated invalid data");
+    case NT_RemoveAppRequest:
+        {
+
+        }
+        break;
+
+    default:
+        LOG_ERR("Invalid notification type.");
+        break;
     }
+
     LOG_DBG("END");
 }
 
index ab954da..d9a775f 100644 (file)
 **/
 
 #include <stdexcept>
-#include <notification_data_manager.h>
+#include <cassert>
+
+#include <app_control.h>
+#include <notification.h>
+#include <notification_internal.h>
 
-#include "push_notification.h"
-#include "utils/log.h"
 #include "common_types.h"
+#include "utils/log.h"
+
+#include "push_notification.h"
 
 #define DASHBOARD_APP_ID "62UffoiBtd.Dashboard"
 
 namespace iotswsec
 {
 
-PushNotification::PushNotification():
-    m_appContext(nullptr)
+PushNotification::PushNotification(const std::string& title, const std::string& body) : m_notification(nullptr)
 {
     LOG_DBG("BEGIN");
-    Init();
-    LOG_DBG("END");
-}
-
-PushNotification& PushNotification::GetInstance()
-{
-    static PushNotification notification;
-
-    return notification;
-}
-
-void PushNotification::Push(const std::string& title, const std::string& body) const
-{
-    LOG_INFO("BEGIN");
-
-    notification_item_info_s notiItemInfo = {0};
-
-    notiItemInfo.pAppName = (char*)"iot-manager-dashboard-proxy";
-    notiItemInfo.nItemId = 1;
-    notiItemInfo.bFlagAutoDelete = 1;
-    notiItemInfo.bFlagBannerShow = 1;
-    notiItemInfo.nBannerHideAlarmDuration = 30;
-    notiItemInfo.nItemType = ITEM_TYPE_TEXT;
-    notiItemInfo.nDefaultFocus = 1;
-    notiItemInfo.pIconPath = (char*)"/usr/apps/iot-manager-dashboard-proxy/shared/images/icon.png";
-    notiItemInfo.pTitleText = (char*)title.c_str();
-    notiItemInfo.nButtonNum = 2;
-    notiItemInfo.pFirstButtonText =  (char*)"Show more";
-    notiItemInfo.pSecondButtonText = (char*)"Dismiss";
-    notiItemInfo.pDescriptionText =  (char*)body.c_str();
-
-    int res = 0;
-
-    if ((res = add_notification_data(&notiItemInfo)) != 0)
-    {
-        LOG_INFO("Couldn't create global notification, error code: %d", res);
-    }
-
-    LOG_INFO("END");
-}
-
-void PushNotification::Init()
-{
-    LOG_INFO("BEGIN");
-    int res = 0;
 
-    if ((res = init_notification_data_manager("iot-manager-dashboard-proxy", 0))
-            != 0)
+    if((m_notification = notification_create(NOTIFICATION_TYPE_NOTI)) == nullptr)
     {
-        LOG_INFO("Couldn't init global notification instance, error code: %d", res);
-
-        throw std::runtime_error(std::string(std::string("Couldn't init global notification instance, error code: ") + std::to_string(res)).c_str());
+        throw std::runtime_error("Couldn't create notification instance.");
     }
 
-    register_notification_button_callback(OnButtonClickCallback, this);
-    register_notification_timeout_callback(OnNotificationTimeOutCB, this);
-    register_notofication_hide_callback(OnNotificationHideCallback, this);
-
-    int appControlRes = 0;
-
-    if ((appControlRes = app_control_create(&m_appContext)) != 0)
+    notification_set_display_applist(m_notification, NOTIFICATION_DISPLAY_APP_ACTIVE);
+    notification_set_layout(m_notification, NOTIFICATION_LY_NONE);
+    notification_set_auto_remove(m_notification, false);
+    notification_set_time(m_notification, time(NULL));
+    notification_set_hide_timeout(m_notification, 60);
+
+    notification_set_text(m_notification, NOTIFICATION_TEXT_TYPE_TITLE, title.c_str(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+    notification_set_text(m_notification, NOTIFICATION_TEXT_TYPE_CONTENT, body.c_str(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+    notification_add_button(m_notification, NOTIFICATION_BUTTON_1);
+    notification_set_text(m_notification, NOTIFICATION_TEXT_TYPE_BUTTON_1, "Show more", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+    notification_add_button(m_notification, NOTIFICATION_BUTTON_2);
+    notification_set_text(m_notification, NOTIFICATION_TEXT_TYPE_BUTTON_2, "Dismiss", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+    notification_set_default_button(m_notification, NOTIFICATION_BUTTON_1);
+
+    app_control_h app_control = NULL;
+    app_control_create(&app_control);
+    if(!app_control)
     {
-        LOG_INFO("Couldn't init app context, error code: %d", appControlRes);
-
-        throw std::runtime_error(std::string(std::string("Couldn't init app context, error code: ") + std::to_string(appControlRes)).c_str());
+        throw std::runtime_error("Couldn't create app_control instance.");
     }
-
-    if ((res = app_control_set_app_id(m_appContext, DASHBOARD_APP_ID))
-            != 0)
+    app_control_set_app_id(app_control, DASHBOARD_APP_ID);
+    app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
+    notification_set_event_handler(m_notification, NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1, app_control);
+    app_control_destroy(app_control);
+
+    app_control = NULL;
+    app_control_create(&app_control);
+    if(!app_control)
     {
-        LOG_INFO("Couldn't set app id, error code: %d", res);
-
-        throw std::runtime_error(std::string(std::string("Couldn't set app id, error code: ") + std::to_string(res)).c_str());
-
+        throw std::runtime_error("Couldn't create app_control instance.");
     }
+    notification_set_event_handler(m_notification, NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_2, app_control);
+    app_control_destroy(app_control);
 
-    LOG_INFO("END");
-}
-
-void PushNotification::Deinit()
-{
-    close_notification_data_manager();
-    app_control_destroy(m_appContext);
-    m_appContext = nullptr;
+    LOG_DBG("END");
 }
 
 PushNotification::~PushNotification()
 {
-    Deinit();
-}
-
-void PushNotification::OnButtonClickCallback(int, int buttonIndex, void *userData)
-{
-    LOG_INFO("BEGIN");
-
-    if (!userData)
-    {
-        LOG_INFO("Error: invalid parameter");
-        return;
-    }
-
-    PushNotification* pThis = reinterpret_cast<PushNotification*>(userData);
-
-    if (buttonIndex == 0)
-    {
-        LOG_INFO("Clicked first button");
-        pThis->LaunchDashboardApp();
-    }
-    else
-    {
-        LOG_INFO("Clicked second button");
-    }
-    LOG_INFO("END");
-}
-
-void PushNotification::LaunchDashboardApp()
-{
-    LOG_INFO("BEGIN");
-    int res = 0;
-
-    if ((res = app_control_send_launch_request(m_appContext, NULL, NULL))
-            != 0)
-    {
-        LOG_INFO("Couldn't launch the application, error code: %d", res);
-    }
-    LOG_INFO("END");
-}
-
-void PushNotification::OnNotificationTimeOutCB(int nItemId, void *userdata)
-{
-    LOG_INFO("OnNotificationTimeOutCB");
-    LOG_INFO("Hide by timeout");
+    notification_delete(m_notification);
+    notification_free(m_notification);
+    m_notification = nullptr;
 }
 
-void PushNotification::OnNotificationHideCallback(char *pAppName, void *userdata)
+int PushNotification::push() const
 {
-    LOG_INFO("OnNotificationHideCallback");
-    LOG_INFO("Hide by action");
+    assert(m_notification);
+    return notification_post(m_notification);
 }
 
 } //namespace iotswsec
index bf51fc7..f6671bd 100644 (file)
@@ -1,6 +1,6 @@
 project(${PROXY_PROJ_NAME}-ut C CXX)
 
-pkg_check_modules(${PROJECT_NAME}_REQ_PKGS REQUIRED dlog boost global-function)
+pkg_check_modules(${PROJECT_NAME}_REQ_PKGS REQUIRED dlog boost notification)
 
 GENERATE_UT_STRUCTURE_VARIABLE(../ ${PROXY_PROJ_NAME})