sync with master
[platform/framework/native/appfw.git] / src / system / FSys_AlarmImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_AlarmImpl.cpp
20  * @brief               This is the implementation file for _AlarmImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <unistd.h>
27
28 #include <FBaseRtIEventArg.h>
29 #include <FBaseSysLog.h>
30
31 #include <FBase_NativeError.h>
32 #include "FSys_AlarmImpl.h"
33 #include "FSys_AlarmManager.h"
34 #include "FSys_SystemTimeImpl.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Io;
40
41 namespace Tizen { namespace System
42 {
43
44 class _AlarmEventArg : public IEventArg
45 {
46 public:
47         _AlarmEventArg()
48         : pAlarm(null)
49         {
50         }
51         Alarm* pAlarm;
52 };
53
54 class _AlarmEvent : public Event
55 {
56 protected:
57         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
58         {
59                 IAlarmEventListener* pListener = dynamic_cast<IAlarmEventListener*> (&listener);
60                 const _AlarmEventArg* pArg = dynamic_cast<const _AlarmEventArg*>(&arg);
61
62                 if(pListener == null || pArg == null)
63                 {
64                         SysLogException(NID_SYS, E_SYSTEM, "listener or arg is null.");
65                         return;
66                 }
67
68                 if(pArg->pAlarm == null)
69                 {
70                         SysLogException(NID_SYS, E_SYSTEM, "Alarm data is invalid.");
71                         return;
72                 }
73
74                 pListener->OnAlarmExpired(*(pArg->pAlarm));
75         }
76 };
77
78 _AlarmImpl::_AlarmImpl(Alarm* pAlarm)
79         : __alarmId(0)
80         , __period(0)
81         , __pEndTime(null)
82         , __isSetEndTime(false)
83         , __pEvent(null)
84         , __pAlarm(pAlarm)
85 {
86
87 }
88
89 _AlarmImpl::~_AlarmImpl(void)
90 {
91         if(__alarmId.value != 0)
92         {
93                 Cancel();
94         }
95 }
96
97 result
98 _AlarmImpl::Construct(void)
99 {
100         return E_SUCCESS;
101 }
102
103 result
104 _AlarmImpl::Set(const DateTime& startTime, int period, const DateTime* pEndTime)
105 {
106         result r = E_SUCCESS;
107         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
108         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
109
110         __startTime = startTime;
111         __period = period;
112
113         if(pEndTime != null)
114         {
115                 DateTime* pRequiredEndTime = const_cast <DateTime*> (pEndTime);
116                 __pEndTime = pRequiredEndTime;
117                 __isSetEndTime = true;
118         }
119         else
120         {
121                 __isSetEndTime = false;
122         }
123
124         if(__alarmId.value == 0)
125         {
126                 r = pAlarmManager->RegisterAlarm(this);
127         }
128         else
129         {
130                 r = pAlarmManager->UpdateAlarm(this);
131         }
132
133         if(r != E_SUCCESS && r != E_INVALID_ARG)
134         {
135                 SysLog(NID_SYS, "It is failed to register alarm. [%s]", GetErrorMessage(r));
136                 r = E_SYSTEM;
137         }
138         return r;
139 }
140
141 result
142 _AlarmImpl::Cancel(void)
143 {
144         result r = E_SUCCESS;
145         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
146         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
147
148         r = pAlarmManager->UnregisterAlarm(this);
149
150         if(r != E_SUCCESS && r != E_INVALID_ARG)
151         {
152                 SysLog(NID_SYS, "It is failed to register alarm. [%s]", GetErrorMessage(r));
153                 r = E_SYSTEM;
154         }
155         return r;
156 }
157
158 const DateTime
159 _AlarmImpl::GetStartTime(void) const
160 {
161         ClearLastResult();
162         return __startTime;
163 }
164
165 int
166 _AlarmImpl::GetPeriod(void) const
167 {
168         ClearLastResult();
169         return __period.ToInt();
170 }
171
172 const DateTime*
173 _AlarmImpl::GetEndTime(void) const
174 {
175         ClearLastResult();
176         return __pEndTime;
177 }
178
179 result
180 _AlarmImpl::SetAlarmEventListener(IAlarmEventListener* pListener)
181 {
182         if(pListener == null)
183         {
184                 SysLog(NID_SYS, "Listener null set doesn't mean disalarm");
185                 if(__pEvent != null)
186                 {
187                         delete __pEvent;
188                         __pEvent = null;
189                 }
190         }
191         else
192         {
193                 if(__pEvent == null)
194                 {
195                         _AlarmEvent* pAlarmEvent = new (std::nothrow) _AlarmEvent();
196                         SysTryReturnResult(NID_SYS, pAlarmEvent != null, E_OUT_OF_MEMORY, "It is failed to create Event instance");
197                         __pEvent = pAlarmEvent;
198                 }
199                 __pEvent->AddListener(*pListener);
200         }
201         return E_SUCCESS;
202 }
203
204 void 
205 _AlarmImpl::OnAlarmExpired(int alarmId)
206 {
207         if(__pEvent != null)
208         {
209                 _AlarmEventArg* pAlarmEventArg= new (std::nothrow) _AlarmEventArg();
210                 if(pAlarmEventArg == null)
211                 {
212                         SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _AlarmEventArg class.");
213                         return;
214                 }
215                 pAlarmEventArg->pAlarm = this->__pAlarm;
216                 if(pAlarmEventArg != null)
217                 {
218                         __pEvent->Fire(*pAlarmEventArg);
219                 }
220                 SysLog(NID_SYS, "Alarm Expired for alarm id: %d and listener called.", alarmId);
221         }
222         else
223         {
224                 SysLog(NID_SYS, "Reserved alarm is expired. However, Listener point is unavailable.");
225         }
226 }
227
228
229 _AlarmImpl*
230 _AlarmImpl::GetInstance(Alarm* pAlarm)
231 {
232         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
233 }
234
235 const _AlarmImpl*
236 _AlarmImpl::GetInstance(const Alarm* pAlarm)
237 {
238         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
239 }
240
241 } } // Tizen::System