Post notification icon on active alarm. 75/101475/8
authorKamil Lipiszko <k.lipiszko@samsung.com>
Thu, 1 Dec 2016 08:35:01 +0000 (09:35 +0100)
committerKamil Lipiszko <k.lipiszko@samsung.com>
Fri, 9 Dec 2016 09:52:25 +0000 (10:52 +0100)
This patch provides feature that posts alarm icon in indicator
whenever any alarm is active. On all alarms deactivation icon
notification is deleted.

Change-Id: I32b3e8a791ff0ad1656d5e1dbb5a74a316779b5e

clock/inc/Internal/AlarmProviderFile.h
clock/inc/Model/AlarmProvider.h
clock/inc/Utils/Notifier.h [new file with mode: 0644]
clock/shared/res/alarm_icon.png [new file with mode: 0644]
clock/src/Model/AlarmProvider.cpp
clock/src/Model/AlarmProviderFile.cpp
clock/src/Utils/Notifier.cpp [new file with mode: 0644]
clock/tizen-manifest.xml

index bee0b1e..9baff56 100644 (file)
@@ -35,6 +35,7 @@ namespace internal {
                        void Serialize(utils::IWriter &w) const;
                        int Capacity() const;
                        bool IsFull() const;
+                       bool HasActiveAlarms();
                private:
                        std::list<model::Alarm> alarms;
                        static const int db_schema_version;
index f7fc9a9..dbe1d1c 100644 (file)
@@ -31,6 +31,7 @@ namespace model {
                        static void Sync();
 
                        virtual std::vector<std::reference_wrapper<Alarm>> GetAlarms() = 0;
+                       virtual bool HasActiveAlarms() = 0;
                        virtual Alarm *GetAlarmWithId(AlarmId id) = 0;
                        virtual void Add(Alarm& alarm) = 0;
                        virtual void Remove(std::reference_wrapper<Alarm> alarm) = 0;
diff --git a/clock/inc/Utils/Notifier.h b/clock/inc/Utils/Notifier.h
new file mode 100644 (file)
index 0000000..68f0310
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef _CLOCK_UTILS_NOTIFIER_H
+#define _CLOCK_UTILS_NOTIFIER_H
+
+#include <notification.h>
+
+namespace utils {
+
+       class Notifier {
+       public:
+               virtual ~Notifier ();
+               static Notifier &GetInstance();
+
+               Notifier &operator=(const Notifier&) = delete;
+               Notifier(const Notifier &) = delete;
+
+               /*
+                * @brief Posts alarm notification for indicator
+                *
+                * Posting notification for indicator to display alarm
+                * icon if any is activated.
+                *
+                * @return true if succeed, otherwise false
+                */
+               bool AlarmNotiPost();
+
+               /*
+                * @brief Removes alarm notification from indicator
+                *
+                * Removes alarm icon from indicator bar.
+                *
+                * @return true if succeed, otherwise false
+                */
+               bool AlarmNotiDelete();
+       private:
+               Notifier();
+
+               const char *tag_;
+               notification_h noti_ = NULL;
+               notification_h NotificationCreate();
+       };
+
+} /* utils */
+
+
+#endif //_CLOCK_UTILS_NOTIFIER_H
diff --git a/clock/shared/res/alarm_icon.png b/clock/shared/res/alarm_icon.png
new file mode 100644 (file)
index 0000000..d3588c9
Binary files /dev/null and b/clock/shared/res/alarm_icon.png differ
index 1cac21e..333a229 100644 (file)
@@ -19,6 +19,7 @@
 #include "Utils/BinaryFileReader.h"
 #include "Utils/BinaryFileWriter.h"
 #include "Utils/Utils.h"
+#include "Utils/Notifier.h"
 
 static const char DATABASE_FILE[] = "alarmsdb.cfg";
 
@@ -32,6 +33,9 @@ AlarmProvider *AlarmProvider::GetInstance()
        if (!instance) {
                BinaryFileReader reader(Utils::APP_DIR_DATA, DATABASE_FILE);
                instance = new AlarmProviderFile(reader);
+
+               if (instance->HasActiveAlarms())
+                       Notifier::GetInstance().AlarmNotiPost();
        }
        return instance;
 }
@@ -43,4 +47,9 @@ void AlarmProvider::Sync()
        BinaryFileWriter writer(Utils::APP_DIR_DATA, DATABASE_FILE);
        instance->Serialize(writer);
        writer.Flush();
+
+       if (instance->HasActiveAlarms())
+               Notifier::GetInstance().AlarmNotiPost();
+       else
+               Notifier::GetInstance().AlarmNotiDelete();
 }
index 956a42a..9417a89 100644 (file)
@@ -75,6 +75,16 @@ std::vector<std::reference_wrapper<model::Alarm>> AlarmProviderFile::GetAlarms()
                        alarms.end());
 }
 
+bool AlarmProviderFile::HasActiveAlarms()
+{
+       for (auto &alarm: alarms) {
+               if (alarm.IsActivated())
+                       return true;
+       }
+
+       return false;
+}
+
 Alarm* AlarmProviderFile::GetAlarmWithId(AlarmId id)
 {
        auto it = std::find(alarms.begin(), alarms.end(), id);
diff --git a/clock/src/Utils/Notifier.cpp b/clock/src/Utils/Notifier.cpp
new file mode 100644 (file)
index 0000000..a7a6488
--- /dev/null
@@ -0,0 +1,109 @@
+#include <string>
+
+#include "Utils/Notifier.h"
+#include "Utils/Log.h"
+#include "Utils/Utils.h"
+
+#define ALARM_ICON "alarm_icon.png"
+#define ALARM_NOTI_TAG "system_clock_alarm"
+
+namespace utils {
+
+Notifier &Notifier::GetInstance()
+{
+       static Notifier instance;
+       return instance;
+}
+
+Notifier::Notifier() : tag_(ALARM_NOTI_TAG)
+{
+       noti_ = notification_load_by_tag(tag_);
+       if (noti_) {
+               DBG("notification_load_by_tag succeed");
+               return;
+       }
+
+       noti_ = NotificationCreate();
+       if (!noti_)
+               ERR("Could not create notification");
+}
+
+Notifier::~Notifier()
+{
+       notification_free(noti_);
+}
+
+notification_h Notifier::NotificationCreate()
+{
+       notification_h noti = notification_create(NOTIFICATION_TYPE_NOTI);
+       if (!noti) {
+               ERR("notification_create failed");
+               return nullptr;
+       }
+
+       std::string path = Utils::GetAppResourcePath(Utils::APP_DIR_SHARED_RESOURCE, ALARM_ICON);
+       int ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, path.c_str());
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_set_image failed[%d]: %s", ret, get_error_message(ret));
+               notification_free(noti);
+               return nullptr;
+       }
+
+
+       ret = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_INDICATOR);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_set_display_applist failed[%d]: %s", ret, get_error_message(ret));
+               notification_free(noti);
+               return nullptr;
+       }
+
+       ret = notification_set_tag(noti, tag_);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_set_tag failed[%d]: %s", ret, get_error_message(ret));
+               notification_free(noti);
+               return nullptr;
+       }
+
+       ret = notification_set_sound(noti, NOTIFICATION_SOUND_TYPE_NONE, NULL);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_set_sound failed[%d]: %s", ret, get_error_message(ret));
+               notification_free(noti);
+               return nullptr;
+       }
+
+       return noti;
+}
+
+bool Notifier::AlarmNotiPost()
+{
+       if (!noti_) {
+               ERR("Notification post failed: noti_ == NULL");
+               return false;
+       }
+
+       int ret = notification_post(noti_);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_post failed[%d]: %s", ret, get_error_message(ret));
+               return false;
+       }
+
+       return true;
+}
+
+bool Notifier::AlarmNotiDelete()
+{
+       if (!noti_) {
+               ERR("noti_ == NULL");
+               return false;
+       }
+
+       int ret = notification_delete(noti_);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_delete failed[%d]: %s", ret, get_error_message(ret));
+               return false;
+       }
+
+       return true;
+}
+
+} // namespace utils
index 2e81fbd..b1284da 100644 (file)
@@ -11,5 +11,6 @@
        <privilege>http://tizen.org/privilege/window.priority.set</privilege>
        <privilege>http://tizen.org/privilege/haptic</privilege>
        <privilege>http://tizen.org/privilege/volume.set</privilege>
+       <privilege>http://tizen.org/privilege/notification</privilege>
     </privileges>
 </manifest>