Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / MultiProcServiceApp / MultiProcServiceApp / project / src / SampleTimer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include "SampleTimer.h"
19
20 using namespace Osp::App;
21 using namespace Osp::Base;
22 using namespace Osp::Base::Collection;
23 using namespace Osp::Base::Runtime;
24
25 static int TIMER_INTERVAL_READY = 5000; // Timer interval for ready state (in msec)
26 static int TIMER_INTERVAL_RUN = 1000; // Timer interval for running state (in msec)
27
28 SampleTimer::SampleTimer(void)
29 : _pTimer(null)
30 , _isRepeatable(true)
31 , _isRunning(false)
32 {
33 }
34
35 SampleTimer::~SampleTimer(void)
36 {
37         delete _pTimer;
38 }
39
40 result
41 SampleTimer::Construct(void)
42 {
43         result r = E_SUCCESS;
44
45         _pTimer = new Timer;
46         TryReturn(_pTimer != null, E_FAILURE, "SampleServiceApp : [E_FAILURE] Failed to create _pTimer.");
47         AppLog("SampleServiceApp : _pTimer is created.");
48
49         r = _pTimer->Construct(*this);
50         TryReturn(IsFailed(r) != true, r, "SampleServiceApp : [%s] Failed to construct _pTimer", GetErrorMessage(r));
51         AppLog("SampleServiceApp : _pTimer is constructed.");
52
53         _pTimer->Start(TIMER_INTERVAL_READY);
54
55         return r;
56 }
57
58 result
59 SampleTimer::Start(void)
60 {
61         result r = E_SUCCESS;
62
63         TryReturn(_pTimer != null, E_OBJ_NOT_FOUND, "SampleServiceApp : _pTimer is not constructed yet. create _pTimer instance and try Start() again.");
64
65         r = _pTimer->Cancel();
66         TryReturn(IsFailed(r) != true, r, "SampleServiceApp : [%s] Failed to cancel _pTimer", GetErrorMessage(r));
67
68         r = _pTimer->Start(TIMER_INTERVAL_RUN);
69         TryReturn(IsFailed(r) != true, r, "SampleServiceApp : [%s] Failed to start _pTimer", GetErrorMessage(r));
70
71         _isRepeatable = true;
72         _isRunning = true;
73         AppLog("SampleServiceApp : _pTimer is started.");
74
75         return r;
76 }
77
78 result
79 SampleTimer::Stop(void)
80 {
81         result r = E_SUCCESS;
82
83         TryReturn(_pTimer != null, E_OBJ_NOT_FOUND, "SampleServiceApp : _pTimer is not constructed yet. create _pTimer instance and try Stop() again.");
84
85         r = _pTimer->Cancel();
86         TryReturn(!IsFailed(r), r, "SampleServiceApp : [%s] Failed to cancel _pTimer", GetErrorMessage(r));
87
88         r = _pTimer->Start(TIMER_INTERVAL_READY);
89         TryReturn(!IsFailed(r), r, "SampleServiceApp : [%s] Failed to start _pTimer", GetErrorMessage(r));
90
91         _isRepeatable = false;
92         _isRunning = false;
93         AppLog("SampleServiceApp : _pTimer will be stopped.");
94
95         return r;
96 }
97
98 void
99 SampleTimer::OnTimerExpired(Timer& timer)
100 {
101         AppLog("SampleServiceApp : _pTimer is expired.");
102
103         if (_isRunning)
104         {
105                 App* pApp = App::GetInstance();
106                 if (pApp != null)
107                 {
108                         ArrayList* pList = new ArrayList();
109                         pList->Construct();
110
111                         pApp->SendUserEvent(TIMER_EXPIRED, pList);
112
113                         pList->RemoveAll(true);
114                         delete pList;
115                 }
116
117                 if (_isRepeatable)
118                 {
119                         timer.Start(TIMER_INTERVAL_RUN);
120                 }
121         }
122         else
123         {
124                 timer.Start(TIMER_INTERVAL_READY);
125         }
126 }
127
128
129
130