Add timer to stop askuser-notification after being idle 05/168605/8
authorZofia Grzelewska <z.abramowska@samsung.com>
Mon, 29 Jan 2018 17:37:40 +0000 (18:37 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Wed, 28 Feb 2018 13:03:18 +0000 (13:03 +0000)
Timer is set for 1 seconds and if triggered and no event is
to process for askuser-notification, then whole service is shutdown.

Change-Id: I049e091494859d290232b6d10dc7f03a36442825

src/notification-daemon/Logic.cpp
src/notification-daemon/Logic.h

index 4c11f14..4967110 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co.
+ *  Copyright (c) 2017-2018 Samsung Electronics Co.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -42,6 +42,8 @@ namespace Notification {
 
 namespace {
 
+int ASKUSER_IDLE_TIMER_SEC = 4;
+
 int uiResponseToClientResponse(NResponseType response) {
     int clientResponse;
 
@@ -104,6 +106,10 @@ void Logic::addChannelFd(Protocol::ConnectionFd fd, const Protocol::Credentials
 
     std::string appId, pkgLabel;
     identifyApp(creds.label, appId, pkgLabel);
+
+    ALOGD("Proper client connected");
+    stopTimer();
+
     ConnectionInfo connInfo{appId, pkgLabel, creds.uid};
     m_connToInfo.insert(it, std::make_pair(fd, connInfo));
 }
@@ -131,6 +137,10 @@ void Logic::removeChannelFd(Protocol::ConnectionFd fd) {
     ALOGD("Removing channel with fd " << fd);
     m_connToInfo.erase(fd);
 
+    if (m_connToInfo.empty()) {
+        ALOGD("No more clients");
+        startTimer();
+    }
     auto it = m_fdInfo.find(fd);
     if (it == m_fdInfo.end()) {
         return;
@@ -335,6 +345,32 @@ Eina_Bool Logic::signalHandler(void *data, Ecore_Fd_Handler *) {
     return ECORE_CALLBACK_CANCEL;
 }
 
+void Logic::startTimer() {
+    if (m_timer)
+        return;
+    ALOGD("Starting timer");
+    m_timer = ecore_timer_add(ASKUSER_IDLE_TIMER_SEC, &Logic::timerHandler, this);
+    if (m_timer == nullptr)
+        ALOGE("Failed to add ecore timer");
+}
+
+void Logic::stopTimer() {
+    ALOGD("Stopping timer");
+    if (m_timer == nullptr)
+        return;
+    (void)ecore_timer_del(m_timer);
+    m_timer = nullptr;
+}
+
+Eina_Bool Logic::timerHandler(void *data) {
+    Logic *logic = static_cast<Logic*>(data);
+    if (logic->m_connToInfo.empty()) {
+        ALOGD("No connected clients to handle after timer run out. Closing...");
+        logic->stop();
+    }
+    return ECORE_CALLBACK_CANCEL;
+}
+
 void Logic::prepareChannel() {
     AskUser::Protocol::ServerCallbacksPtr callbacks(new AskUser::Protocol::ServerCallbacks(this));
     std::unique_ptr<AskUser::Protocol::ServerChannel> channel(new AskUser::Protocol::ServerChannel(std::move(callbacks)));
index 6c502bb..298d394 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co.
+ *  Copyright (c) 2017-2018 Samsung Electronics Co.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@
 #include <deque>
 #include <server-channel.h>
 
+#include <Elementary.h>
+
 #include "event/Event.h"
 #include "PrivaciesSequence.h"
 
@@ -43,7 +45,7 @@ public:
     private:
         std::string m_msg;
     };
-    Logic() : m_currentEvent{-1, -1} {}
+    Logic() : m_currentEvent{-1, -1}, m_timer(nullptr) {}
     void init();
     void run();
     void stop();
@@ -81,15 +83,21 @@ private:
     // Descriptor handlers
     static Eina_Bool clientChHandler(void *data, Ecore_Fd_Handler *handler);
     static Eina_Bool signalHandler(void *data, Ecore_Fd_Handler *handler);
+    static Eina_Bool timerHandler(void *data);
     //static Eina_Bool signalHandler(void *data, int type, void *event);
 
     void addEvent(Protocol::ConnectionFd fd, Protocol::RequestId id, const std::vector<Privacy> &privacies);
     Eina_Bool processChannel(int fd, int mask);
     bool identifyClient(const std::string &label, std::string &appId, std::string &pkgId);
+
     void processEvents();
     void processResponse(const ConnectionInfo &conn, int clientResponse, const Policy &level);
     bool isEventProcessed();
     void finishCurrentRequest();
+
+    void startTimer();
+    void stopTimer();
+
     void popupResponse(NResponseType response);
 
     bool processError(EventId id, int error);
@@ -120,11 +128,11 @@ private:
 
     std::map<Protocol::ConnectionFd, FdInfo> m_fdInfo;
 
-
-
     std::map<Protocol::ConnectionFd, ConnectionInfo> m_connToInfo;
 
     std::map<EventId, PrivaciesSequence> m_eventToPrivaciesSeq;
+
+    Ecore_Timer *m_timer;
 };
 
 } // namespace Notification