Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / FUiEffects_EffectTimer.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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://floralicense.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  * @file    FUiEffects_EffectTimer.h
19  * @brief               This is the header file for the _EffectTimer class
20  */
21
22 #ifndef FUI_EFFECTS_INTERNAL_EFFECTTIMER_H_
23 #define FUI_EFFECTS_INTERNAL_EFFECTTIMER_H_
24
25 #include <FBase.h>
26 #include <FBaseSysLog.h>
27 #include <FSysSystemTime.h>
28 #include <FUiEffectsTypes.h>
29
30 namespace Tizen { namespace Ui { namespace Effects
31 {
32
33 /**
34  * @class       _EffectTimer
35  * @brief       Class for effect timer
36  *
37  * @since 2.0
38  */
39 class _EffectTimer
40         : public Tizen::Base::Runtime::Timer
41 {
42 public:
43
44         /**
45         * Constructor
46         *
47         * @since 2.0
48         *
49         * @param [in]   _type                           Set type of this timer
50         *
51         */
52         _EffectTimer(void): __effectID(-1), __timeout(-1), __t1(-1), __countGreater(0), __count(0)
53         {
54
55         }
56
57         /**
58         * Timer Initialisation
59         *
60         * @since 2.0
61         *
62         * @param [in]   TimerEventListener      OnTimerExpired event listener
63         * @param [in]   _effectID                       id of effect assigned to this timer
64         * @param [in]   _timeout                        A __timeout interval in milliseconds
65         *
66         */
67         result Construct(Tizen::Base::Runtime::ITimerEventListener &TimerEventListener, long effectID, int timeout)
68         {
69                 __effectID = effectID;
70                 __timeout = timeout;
71                 return Tizen::Base::Runtime::Timer::Construct(TimerEventListener);
72         }
73
74         /**
75          * Gets Id of Effect assigned to this timer
76          *
77          * @since 2.0
78          *
79          * @return              EffectIDType            Effect id
80          */
81         long GetEffectIdType(void)
82         {
83                 return __effectID;
84         }
85
86         /**
87          * Stores current system time for next realtime interval calculation
88          *
89          * @since 2.0
90          *
91          * @see                 _EffectTimer::CalculationTimeStep
92          */
93         void Reset(void)
94         {
95                 Tizen::System::SystemTime::GetTicks(__t1);
96                 __countGreater = 0;
97                 __count = 0;
98
99                 return;
100         }
101
102         /**
103          * Starts the timer
104          *
105          * @since 2.0
106          *
107          */
108         void Start(int t = 0)
109         {
110                 if (t == 0)
111                 {
112                         Timer::Start(__timeout);
113                 }
114                 else
115                 {
116                         Timer::Start(t);
117                 }
118                 IncreaseCount();
119
120                 return;
121         }
122
123         /**
124          * Increases the variable count means a number of calling function OnTimerExpired
125          *
126          * @since 2.0
127          *
128          */
129         void IncreaseCount()
130         {
131                 __count++;
132
133                 return;
134         }
135
136         /**
137          * Stops the timer
138          *
139          * @since 2.0
140          *
141          */
142         void Stop()
143         {
144                 if (__countGreater != 0)
145                 {
146                         int c = (int)(__countGreater * 1.f / __count * 100);
147                         SysLog(NID_UI_EFFECT, "Timer timeout for effect with %i ID is greater than a threshold in %i%% of cases.", __effectID, c);
148                 }
149                 Tizen::Base::Runtime::Timer::Cancel();
150                 return;
151         }
152
153
154         /**
155          * Resets and then starts. Use this method for first run
156          *
157          * @since 2.0
158          *
159          */
160         void Restart()
161         {
162                 Reset();
163                 Start();
164
165                 return;
166         }
167
168
169         /**
170          * Precise realtime interval calculation.
171          *
172          * @since 2.0
173          *
174          * @return              float
175          */
176         float CalculationTimeStep(void)
177         {
178                 const long long t0 = __t1;
179                 Tizen::System::SystemTime::GetTicks(__t1);
180
181                 int sub = __t1 - t0;
182
183                 if (sub > 3*__timeout)
184                 {
185                         sub = 3*__timeout;
186                         if (__countGreater == 0)
187                         {
188                                 SysLogException(NID_UI_EFFECT, E_TIMEOUT, "Timer timeout for effect with %i ID is too small! Your device is too slow for specified timeout. You may increase the timer time.", __effectID);
189                         }
190                         __countGreater++;
191                 }
192
193                 return sub * 0.001f;
194         }
195
196 private:
197         long __effectID;                        /**< id of parent effect */
198         int __timeout;                  /**< period in milliseconds */
199         long long __t1;                 /**< internal use */
200         long __countGreater;
201         long __count;
202 };
203
204 } } } // Tizen::Ui::Effects
205
206 #endif //FUI_EFFECTS_INTERNAL_EFFECTTIMER_H_