Add LCOV remarkers to increase line coverage rate
[platform/core/api/maps-service.git] / src / view / gesture_detector.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include "gesture_detector.h"
19 #include <glib.h>
20 #include <math.h>
21
22
23 extern bool _maps_view_is_gesture_available(maps_view_h view,
24                                            maps_view_gesture_e gesture);
25
26
27 /*----------------------------------------------------------------------------*/
28
29 int view::gesture_detector::__CLICK_AREA = 50;
30
31 //LCOV_EXCL_START
32 view::gesture_detector::gesture_detector(maps_view_h v)
33         : _view(v)
34         , _gp(this)
35         , _tap_timer(NULL)
36         , _long_press_timer(NULL)
37 {
38         int dpi;
39         if (maps_get_screen_dpi(&dpi) == MAPS_ERROR_NONE)
40                 __CLICK_AREA = dpi / 5; /* expected about 0.5cm */
41 }
42
43 view::gesture_detector::~gesture_detector()
44 {
45         stop_tap_timer();
46         stop_long_press_timer();
47 }
48
49 void view::gesture_detector::halt_gesture()
50 {
51 }
52
53 void view::gesture_detector::reset()
54 {
55         MAPS_LOGI("%c[%d;%d;%dm"
56                   "%s"
57                   "%c[%d;%d;%dm",
58                   0x1B, 1, 41, FG_GREEN,
59                   "RESET RUNTIME INFO",
60                   0x1B, 0, 0, 0);
61
62         /* Store the previous gesture data for possible future analysis */
63         _info_history = _info;
64
65         /* Save Map View state for complex multi-click gestures */
66         const map_state saved_state = _info._start_view_state;
67
68         /* Reset current gesture info */
69         _info.reset();
70
71         /* We would use this state in the complex gestures */
72         _info._start_view_state = saved_state;
73
74         /* There is definitelly not long enough for "long press" gesture */
75         stop_long_press_timer();
76
77         /*MAPS_LOGI("Historic timestamps: tap: %d, move: %d, up: %d",
78                   _info_history._finger_down[0]._timestamp,
79                   _info_history._finger_move[0]._timestamp,
80                   _info_history._finger_up[0]._timestamp);*/
81 }
82
83 void view::gesture_detector::tap(int finger_no, const touch_point &tp)
84 {
85 }
86
87 void view::gesture_detector::up(int finger_no, const touch_point &tp)
88 {
89 }
90
91 void view::gesture_detector::move(int finger_no, const touch_point &tp)
92 {
93 }
94
95 void view::gesture_detector::on_tap_timer()
96 {
97 }
98
99 void view::gesture_detector::start_tap_timer()
100 {
101         log("START TAP TIMER", FG_MAGENTA);
102         if(_tap_timer)
103                 stop_tap_timer();
104         _tap_timer = ecore_timer_add(__TAP_DURATION,
105                                         __on_tap_timer,
106                                         this);
107 }
108
109 void view::gesture_detector::stop_tap_timer()
110 {
111         log("STOP TAP TIMER", FG_MAGENTA);
112         if(_tap_timer)
113                 ecore_timer_del(_tap_timer);
114         _tap_timer = NULL;
115 }
116
117 Eina_Bool view::gesture_detector::__on_tap_timer(void *data)
118 {
119         gesture_detector *d = (gesture_detector *)data;
120         if (!d)
121                 return ECORE_CALLBACK_CANCEL;
122
123         d->log("------- TAP TIMER EVENT -------", FG_MAGENTA);
124         d->_tap_timer = NULL;
125         d->on_tap_timer();
126
127         return ECORE_CALLBACK_CANCEL;
128 }
129
130 void view::gesture_detector::on_long_press_timer()
131 {
132 }
133
134 void view::gesture_detector::start_long_press_timer()
135 {
136         log("START LONG PRESS TIMER", FG_MAGENTA);
137         if(_long_press_timer)
138                 stop_long_press_timer();
139         _long_press_timer = ecore_timer_add(__LONG_PRESS_DURATION,
140                                             __on_long_press_timer,
141                                             this);
142 }
143
144 void view::gesture_detector::stop_long_press_timer()
145 {
146         log("STOP LONG PRESS TIMER", FG_MAGENTA);
147         if(_long_press_timer)
148                 ecore_timer_del(_long_press_timer);
149         _long_press_timer = NULL;
150 }
151
152 Eina_Bool view::gesture_detector::__on_long_press_timer(void *data)
153 {
154         gesture_detector *d = (gesture_detector *)data;
155         if (!d)
156                 return ECORE_CALLBACK_CANCEL;
157
158         d->log("------- LONG PRESS TIMER EVENT -------", FG_MAGENTA);
159         d->_long_press_timer = NULL;
160         d->on_long_press_timer();
161
162         return ECORE_CALLBACK_CANCEL;
163 }
164
165 bool view::gesture_detector::is_gesture_available(maps_view_gesture_e gesture)
166 {
167         return _maps_view_is_gesture_available(_view, gesture);
168 }
169
170 bool view::gesture_detector::finger_dragged_enough(int finger_no)
171 {
172         const touch_point p1 = _info._finger_down[finger_no];
173         const touch_point p2 = _info._finger_move[finger_no];
174         return (get_trajectory_effective_length(p1, p2) >= __CLICK_AREA);
175 }
176
177 bool view::gesture_detector::finger_pressed_enough(int finger_no,
178                                                    unsigned int duration_min,
179                                                    unsigned int duration_max)
180 {
181         unsigned int press_time = _info._finger_down[finger_no]._timestamp;
182         unsigned int up_time = _info._finger_up[finger_no]._timestamp;
183
184         unsigned int press_duration_tmp = up_time - press_time;
185         MAPS_LOGI("finger_pressed_enough: finger: %d, press: %d, up: %d [%d]",
186                   finger_no, press_time, up_time, press_duration_tmp);
187
188         if(up_time < press_time)
189                 return false; /* Not yet unpressed */
190         unsigned int press_duration = up_time - press_time;
191         if ((press_duration >= duration_min) && (press_duration <= duration_max))
192                 return true;
193         return false;
194 }
195
196 int view::gesture_detector::get_trajectory_effective_length(
197                                                         const touch_point &p1,
198                                                         const touch_point &p2)
199 {
200         int dx = p2._x - p1._x;
201         int dy = p2._y - p1._y;
202         int etl = sqrt(dx * dx + dy * dy);
203         return etl;
204 }
205
206 void view::gesture_detector::log(const char *str, log_colors color)
207 {
208         MAPS_LOGI("%c[%d;%d;%dm"
209                   "%s"
210                   "%c[%d;%d;%dm",
211                   0x1B, 1, 0, color,
212                   str,
213                   0x1B, 0, 0, 0);
214 }
215
216
217 void view::gesture_detector::log_map_center(int color)
218 {
219         maps_coordinates_h central_coords = NULL;
220         maps_view_get_center(_view, &central_coords);
221         double lat = .0, lon = .0;
222         maps_coordinates_get_latitude_longitude(central_coords, &lat, &lon);
223         maps_coordinates_destroy(central_coords);
224         MAPS_LOGI("%c[%d;%d;%dm"
225                   "central coordinates: (%f, %f)"
226                   "%c[%d;%d;%dm",
227                   0x1B, 1, 0, color,
228                   lat, lon,
229                   0x1B, 0, 0, 0);
230 }
231 //LCOV_EXCL_STOP