Alarm Conditional AppLaunch Refactoring
authordavid_kim31 <david_kim31@samsung.com>
Wed, 14 Aug 2013 08:55:26 +0000 (17:55 +0900)
committerdavid_kim31 <david_kim31@samsung.com>
Wed, 14 Aug 2013 08:55:26 +0000 (17:55 +0900)
Change-Id: Id53ee047fd6435b1217bdb55d5c4a26021b555c1
Signed-off-by: david_kim31 <david_kim31@samsung.com>
plugins/alarm-condition-handler/AlarmConditionHandler.cpp
plugins/alarm-condition-handler/AlarmConditionHandler.h
plugins/alarm-condition-handler/CMakeLists.txt

index 26ee91a..55f9966 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <new>
 #include <unique_ptr.h>
+#include <pthread.h>
 
 #include <alarm.h>
 #include <appfw/app.h>
@@ -56,6 +57,11 @@ namespace {
        static const int ALARM_PERIOD_MIN = 60;
        static const int WEEKDAYS_CNT = 7;
 }
+
+namespace Tizen { namespace System {
+
+static pthread_mutex_t alarm_lock;
+
 _AlarmConditionHandler* pAlarmConditionHandler = null;
 int RefAlarmConditionHandler = 0;
 _AlarmConditionHandler*
@@ -66,6 +72,7 @@ _AlarmConditionHandler::GetInstance()
                pAlarmConditionHandler = new (std::nothrow) _AlarmConditionHandler;
        }
        RefAlarmConditionHandler ++;
+
        return pAlarmConditionHandler;
 }
 
@@ -87,11 +94,36 @@ _AlarmConditionHandler::ReleaseInstance()
 
 _AlarmConditionHandler::_AlarmConditionHandler()
 {
+       int errorCode = 0;
+
        conditionList.Construct();
+
+       if(pthread_mutex_init(&alarm_lock, null))
+       {
+               SysLogException(NID_SYS, E_SYSTEM, "It is failed to init mutex.");
+       }
+
+       errorCode = vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, SettingEventCb, null);
+       if(errorCode != 0)
+       {
+               SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_WEEKOFDAY_FORMAT_INT event listener");
+       }
 }
 
 _AlarmConditionHandler::~_AlarmConditionHandler()
 {
+       int errorCode = 0;
+
+       errorCode = vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, SettingEventCb);
+       if(errorCode != 0)
+       {
+               SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_WEEKOFDAY_FORMAT_INT event listener");
+       }
+
+       if(pthread_mutex_destroy(&alarm_lock))
+       {
+               SysLogException(NID_SYS, E_SYSTEM, "It is failed to term mutex.");
+       }
 }
 
 result
@@ -265,7 +297,7 @@ _AlarmConditionHandler::Register( _AppLaunchCondition& appLaunchCondition)
 
                        if(r == E_OBJ_ALREADY_EXIST)
                        {
-                               SysLogException(NID_SYS, E_OBJ_ALREADY_EXIST, "The sspecified condition is already registered.");
+                               SysLogException(NID_SYS, E_OBJ_ALREADY_EXIST, "The specified condition is already registered.");
                                r = E_OBJ_ALREADY_EXIST;
                        }
                        else if(r != E_SUCCESS)
@@ -327,7 +359,7 @@ _AlarmConditionHandler::Register( _AppLaunchCondition& appLaunchCondition)
                expireTime.tm_mon -= _ADJUST_MONTH_FOR_TM;
 
                ret = service_create(&service);
-               SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to create service");
+               SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to create service");
 
                ret = app_get_package(&packageName);
                SysTryCatch(NID_SYS, ret == APP_ERROR_NONE, r = E_SYSTEM, r, "It is failed to get package name");
@@ -339,7 +371,7 @@ _AlarmConditionHandler::Register( _AppLaunchCondition& appLaunchCondition)
                SysTryCatch(NID_SYS, ret == SERVICE_ERROR_NONE, r = E_SYSTEM, r, "It is failed to create service for %s", packageName);
                
                ret = alarm_schedule_at_date(service, &expireTime, period * _SECOND_OF_MINUTE, &reservedAlarmId);
-               SysTryCatch(NID_SYS, ret == ALARM_ERROR_NONE, r = E_SYSTEM, r, "It is failed to set the Alarm time for %s", packageName);
+               SysTryCatch(NID_SYS, ret == ALARM_ERROR_NONE, r = E_SYSTEM, r, "It is failed to set the Alarm time for %s", packageName);
 
                time_t reserved, current;
                ret = alarmmgr_get_next_duetime(reservedAlarmId, &reserved);
@@ -350,6 +382,7 @@ _AlarmConditionHandler::Register( _AppLaunchCondition& appLaunchCondition)
                        SysLog(NID_SYS, "current %lld, reserved: %lld", current, reserved);
                        alarm_cancel(reservedAlarmId);
                        r = E_INVALID_CONDITION;
+                       SysLog(NID_SYS, "It is failed to register requested condition.");
                        goto CATCH;
                }
 
@@ -391,6 +424,7 @@ _AlarmConditionHandler::Unregister( _AppLaunchCondition& appLaunchCondition)
        _AppLaunchCondition* pAppLaunchCondition = null;
        result r = E_SYSTEM;
 
+       SysLog(NID_SYS, "Unregister alarm condition is %ls", appLaunchCondition.GetConditionString().GetPointer());
        SysTryReturnResult(NID_SYS, pMapEnum != null, E_SYSTEM, "Reserved condition list is empty");
 
        ArrayList garbageCollector;
@@ -470,6 +504,121 @@ _AlarmConditionHandler::Launch(const Tizen::App::_AppLaunchCondition& condition)
        return Fire(condition);
 }
 
+void
+_AlarmConditionHandler::SettingEventCb(keynode_t* node, void* userData)
+{
+       int ret = 0;
+       result r = E_SYSTEM;
+       Integer* pKey = null;
+       _AlarmConditionHandler* pAlarmCondHandler = null;
+       _AppLaunchCondition* pAppLaunchCondition = null;
+       _AppLaunchCondition* pNewAppLaunchCondition;
+       IMapEnumerator* pMapEnum;
+       IEnumerator* pEnum;
+       Tizen::Base::Collection::HashMap updateList;
+
+       SysLog(NID_SYS, "Alarm setting event cb is invoked");
+
+       pthread_mutex_lock(&alarm_lock);
+       pAlarmCondHandler = _AlarmConditionHandler::GetInstance();
+       if(pAlarmCondHandler == null)
+       {
+               SysLog(NID_SYS, "There is no _AlarmConditionHandler");
+               return;
+       }
+
+       pMapEnum = pAlarmCondHandler->GetAlarmConditionListN();
+       if(pMapEnum == null)
+       {
+               SysLog(NID_SYS, "Reserved condition list is empty");
+               return;
+       }
+
+       if (strcmp(VCONFKEY_SYSTEM_TIME_CHANGED, vconf_keynode_get_name(node)) == 0)
+       {
+               SysLog(NID_SYS, "System time is changed");
+
+               updateList.Construct();
+
+               while(pMapEnum->MoveNext() == E_SUCCESS)
+               {
+                       pKey = static_cast<Integer *>(pMapEnum->GetKey());
+                       pAppLaunchCondition = static_cast<_AppLaunchCondition *>(pMapEnum->GetValue());
+
+                       AppId appId(pAppLaunchCondition->GetAppId());
+                       String condition(pAppLaunchCondition->GetConditionString());
+
+                       pNewAppLaunchCondition = pAlarmCondHandler->GetAppLaunchConditionN(*(new AppId(appId)), *(new String(condition)));
+                       SysTryCatch(NID_SYS, pNewAppLaunchCondition != null, r = E_OUT_OF_MEMORY, r, "It is failed to create pAppLaunchCondition");
+
+                       updateList.Add(pKey, pNewAppLaunchCondition);
+               }
+
+               delete pMapEnum;
+               pMapEnum = updateList.GetMapEnumeratorN();
+               SysTryCatch(NID_SYS, pMapEnum != null, r = E_SYSTEM, r, "Reserved condition list is empty");
+
+               while(pMapEnum->MoveNext() == E_SUCCESS)
+               {
+                       pAppLaunchCondition = static_cast<_AppLaunchCondition *>(pMapEnum->GetValue());
+
+                       SysLog(NID_SYS, "Alarm Unregister : %ls", pAppLaunchCondition->GetConditionString().GetPointer());
+                       ret = pAlarmCondHandler->Unregister(*pAppLaunchCondition);
+                       SysTryCatch(NID_SYS, ret == E_SUCCESS, r = E_SYSTEM, r, "It is failed to unregister the alarm condition %ls", pAppLaunchCondition->GetConditionString().GetPointer());
+               }
+
+               pMapEnum->Reset();
+               while(pMapEnum->MoveNext() == E_SUCCESS)
+               {
+                       pAppLaunchCondition = static_cast<_AppLaunchCondition *>(pMapEnum->GetValue());
+
+                       SysLog(NID_SYS, "Alarm Register : %ls", pAppLaunchCondition->GetConditionString().GetPointer());
+                       ret = pAlarmCondHandler->Register(*pAppLaunchCondition);
+                       SysTryCatch(NID_SYS, ret == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register the alarm condition %ls", pAppLaunchCondition->GetConditionString().GetPointer());
+               }
+       }
+
+CATCH:
+       if(pMapEnum != null)
+               delete pMapEnum;
+
+       pAlarmCondHandler->ReleaseInstance();
+       pthread_mutex_unlock(&alarm_lock);
+}
+
+IMapEnumerator*
+_AlarmConditionHandler::GetAlarmConditionListN(void)
+{
+       IMapEnumerator* pMapEnum = conditionList.GetMapEnumeratorN();
+       if(pMapEnum == null)
+       {
+               SysLog(NID_SYS, "Reserved condition list is empty");
+               return null;
+       }
+
+       return pMapEnum;
+}
+
+_AppLaunchCondition*
+_AlarmConditionHandler::GetAppLaunchConditionN(const AppId& appId, const Tizen::Base::String& condition)
+{
+       result r = E_SYSTEM;
+
+       _AppLaunchCondition* pAppLaunchCondition = new (std::nothrow) _AppLaunchCondition();
+       SysTryCatch(NID_SYS, pAppLaunchCondition != null, r = E_OUT_OF_MEMORY, r, "It is failed to create pAppLaunchCondition");
+
+       r = pAppLaunchCondition->Construct(appId, condition, null, AppManager::LAUNCH_OPTION_DEFAULT, null);
+       SysTryCatch(NID_SYS, !IsFailed(r), r, r, "It is failed to pAppLaunchCondition->Constructor");
+
+       return pAppLaunchCondition;
+
+CATCH:
+       if(pAppLaunchCondition != null)
+               delete pAppLaunchCondition;
+
+       return null;
+}
+
 //////////////////////////////////////////////////////////////
 //     All plugins must provide both a creation and a destruction function
 /////////////////////////////////////////////////////////////
@@ -502,4 +651,6 @@ OnAlarmForLaunch(int alarmId)
        _AlarmConditionHandler::ReleaseInstance();
 }
 
+}
+}
 }//extern "C"
index 6a4fa7e..a838d94 100644 (file)
  * @brief      This is the implementation file of the _AlarmConditionHandler class.
  *
  */
-
+#include <vconf.h>
 #include <FSysAlarm.h>
 #include <FBaseColHashMap.h>
 
+#include "FApp_AppLaunchCondition.h"
 #include "FApp_AppLaunchConditionHandlerBase.h"
 
 /**
@@ -30,6 +31,8 @@
  * @brief      This class implements conditional app launch for alarm condition. (DateTime, DueTime)
  * @final      This class is not intended for extension.
  */
+namespace Tizen { namespace System {
+
 class _AlarmConditionHandler:
        public Tizen::App::_AppLaunchConditionHandlerBase
 {
@@ -45,11 +48,18 @@ public:
        result Launch(const Tizen::App::_AppLaunchCondition& condition);
        result Convert(Tizen::App::_AppLaunchCondition& appLaunchCondition, struct tm* pConvertedTime, int& period);
        result Convert(Tizen::Base::String& appLaunchCondition, struct tm* pConvertedTime, int& week);
+       Tizen::Base::Collection::IMapEnumerator* GetAlarmConditionListN(void);
+       Tizen::App::_AppLaunchCondition* GetAppLaunchConditionN(const Tizen::App::AppId& appId, const Tizen::Base::String& condition);
 
 private:
        _AlarmConditionHandler(void);
        virtual ~_AlarmConditionHandler(void);
+       static void SettingEventCb(keynode_t* node, void* userData);
 
 private:
        Tizen::Base::Collection::HashMap        conditionList;
+       Tizen::Base::Collection::HashMap        updateList;
 };
+}
+}
+
index 805e688..59e5067 100644 (file)
@@ -2,6 +2,7 @@ SET (this_target osp-cond-alarm)
 
 INCLUDE_DIRECTORIES (
        /usr/include/osp
+       /usr/include/vconf
        /usr/include/osp/app
        /usr/include/osp/base
        /usr/include/osp/io
@@ -36,5 +37,6 @@ TARGET_LINK_LIBRARIES(${this_target} "-lalarm")
 TARGET_LINK_LIBRARIES(${this_target} "-lcapi-appfw-application")
 TARGET_LINK_LIBRARIES(${this_target} "-lcapi-appfw-app-manager")
 TARGET_LINK_LIBRARIES(${this_target} "-lcapi-appfw-package-manager")
+TARGET_LINK_LIBRARIES(${this_target} "-lvconf")