merge with master
[framework/osp/locations.git] / src / FLoc_LocationManager.h
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        FLoc_LocationManager.h
20  * @brief       This is the header file for the %_LocationManager class.
21  *
22  * This header file contains the declarations of the %_LocationManager class methods.
23  */
24
25 #ifndef _FLOC_INTERNAL_LOCATION_MANAGER_H_
26 #define _FLOC_INTERNAL_LOCATION_MANAGER_H_
27
28 #include <unique_ptr.h>
29 #include <location/locations.h>
30 #include <FBaseColAllElementsDeleter.h>
31 #include <FBaseRtEventDrivenThread.h>
32 #include <FLocLocation.h>
33 #include "FLoc_ILocationManagerListener.h"
34 #include "FLoc_Types.h"
35
36 namespace Tizen { namespace Locations
37 {
38
39 class _LocationMonitor;
40 class _LocationRequestInfo;
41 class _SyncLocationRequestInfo;
42
43 class _LocationManager
44         : public Tizen::Base::Runtime::EventDrivenThread
45         , public Tizen::Base::Runtime::ITimerEventListener
46 {
47 public:
48         // This method adds the location request into the list of requests and requests for location updates from native location provider.
49         //
50         // @since 2.0
51         //
52         result StartLocationUpdates(LocationAccuracy accuracy, int interval, _ILocationManagerListener* pListener, RequestId& reqId);
53
54         // This method removes the location request wrt to the reqId provided. If the list entries is zero, then stops the native location provider.
55         //
56         // @since 2.0
57         //
58         result StopLocationUpdates(RequestId reqId);
59
60         // This method updates the requested interval from the location provider.
61         //
62         // @since 2.0
63         //
64         result ChangeUpdateInterval(RequestId reqId, int interval);
65
66         // This method initiates the synchronous location retrieval. The location monitor instance provided is used to intimate back the location.
67         //
68         // @since 2.0
69         //
70         result RegisterLocationMonitor(_LocationMonitor* pLocationMonitor);
71
72         // This method returns the stored location.
73         //
74         // @since 2.0
75         //
76         Location GetLastKnownLocation(void);
77
78         // This method converts the horizontal accuracy in meters to the LocationAccuracy enum type.
79         //
80         // @since 2.0
81         //
82         LocationAccuracy GetAccuracyLevel(double horAcc) const;
83
84         // This method gets the location setting information of this particular application.
85         //
86         // @since 2.0
87         //
88         bool IsAppEnabled(void);
89
90         // This method returns the single instance of the location maanger.
91         //
92         // @since 2.0
93         //
94         static _LocationManager* GetInstance(void);
95
96 private:
97         // This default constructor is intentionally declared as private to implement the Singleton semantic.
98         //
99         // @since 2.0
100         //
101         _LocationManager(void);
102
103         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
104         //
105         // @since 2.0
106         //
107         _LocationManager(const _LocationManager& value);
108
109         // This destructor is intentionally declared as private to implement the Singleton semantic.
110         //
111         // @since 2.0
112         //
113         ~_LocationManager(void);
114
115         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
116         //
117         // @since 2.0
118         //
119         _LocationManager& operator =(const _LocationManager& rhs);
120
121         // The method adds the _LocationRequestInfo instance to the array list.
122         //
123         // @since 2.0
124         //
125         void AddToLocRequestInfoList(const _LocationRequestInfo* pLocRequestInfo);
126
127         // The method removes the _LocationRequestInfo instance corresponding to a reqId from the array list.
128         //
129         // @since 2.0
130         //
131         void RemoveFromLocRequestInfoList(RequestId reqId);
132
133         // The method adds the _SyncLocationRequestInfo instance to the array list.
134         //
135         // @since 2.0
136         //
137         void AddToSyncLocationRequestInfoList(_SyncLocationRequestInfo& syncLocRequestInfo);
138
139         // The method removes the _SyncLocationRequestInfo instance from the list.
140         //
141         // @since 2.0
142         //
143         void RemoveSyncLocRetrievalRequest(_SyncLocationRequestInfo& syncLocRequestInfo);
144
145         // The method restarts all (GPS, WPS and CPS) the native location providers.
146         //
147         // @since 2.0
148         //
149         void RestartLocationUpdates(void);
150
151         // The method restarts the update timer after calculating the GCD of the timeout values requested by each location provider.
152         //
153         // @since 2.0
154         //
155         void RestartUpdateTimer(void);
156
157         // The method is called everytime the sync retrieval timer is expired.
158         //
159         // @since 2.0
160         //
161         void HandleSyncRetrievalTimerExpiry(_SyncLocationRequestInfo& syncLocRequestInfo);
162
163         // The method is called everytime the Async update timer is expired to set the location information.
164         //
165         // @since 2.0
166         //
167         result SetLocationInformation(double latitude, double longitude, double altitude, time_t timestamp, location_method_e locMethod, Location* pLocation);
168
169         // The method is called to send back the callbacks in case of async location updates.
170         //
171         // @since 2.0
172         //
173         void SendLocationCallbacks(void);
174
175         // This method resets the member variables.
176         //
177         // @since 2.0
178         //
179         void Reset(void);
180
181         // The method calls the construct method of the event driven thread.
182         //
183         // @since 2.0
184         //
185         result Construct(void);
186
187         // This method is gets the last known location from Native side for the given method.
188         //
189         // @since 2.0
190         //
191         Location GetLastKnownLocation(location_method_e nativeLocMethod);
192
193         // This method returns the most recent location among available last known locations.
194         //
195         // @since 2.0
196         //
197         Location GetRecentLocationAvailable(void);
198
199         // This method updates the timer interval for a particular request Id.
200         //
201         // @since 2.0
202         //
203         void UpdateLocRequestInfoList(RequestId reqId, int interval);
204
205         // This method gets the better location among the three location providers.
206         //
207         // @since 2.0
208         //
209         const Location* FindBestLocation(void);
210
211         // This method gets the location from the native side depending on the location handle.
212         //
213         // @since 2.0
214         //
215         result GetLocation(location_method_e nativeLocMethod);
216
217         // @see @ref Tizen::Base::Runtime::EventDrivenThread::OnStart()
218         //
219         // @since 2.0
220         //
221         virtual bool OnStart(void);
222
223         // @see @ref Tizen::Base::Runtime::EventDrivenThread::OnStop()
224         //
225         // @since 2.0
226         //
227         virtual void OnStop(void);
228
229         // @see @ref Tizen::Base::Runtime::EventDrivenThread::OnUserEventReceivedN()
230         //
231         // @since 2.0
232         //
233         virtual void OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs);
234
235         // @see @ref Tizen::Base::Runtime::ITimerEventListener::OnTimerExpired()
236         //
237         // @since 2.0
238         //
239         virtual void OnTimerExpired(Tizen::Base::Runtime::Timer& timer);
240
241         // This callback is recieved from native location manager for satellite information.
242         //
243         // @since 2.0
244         //
245         static bool SatelliteInfoUpdated(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_active, void* user_data);
246
247         // This method is called by the native location provider when the service state of GPS method is changed.
248         //
249         // @since 2.0
250         //
251         static void GpsServiceUpdateCallback(location_service_state_e state, void* user_data);
252
253         // This method is called by the native location provider when the service state of WPS method is changed.
254         //
255         // @since 2.0
256         //
257         static void WpsServiceUpdateCallback(location_service_state_e state, void* user_data);
258
259         // This method is called by the native location provider when the service state of CPS method is changed.
260         //
261         // @since 2.0
262         //
263         static void CpsServiceUpdateCallback(location_service_state_e state, void* user_data);
264
265         // This method initializes the single instance of the location manager.
266         //
267         // @since 2.0
268         //
269         static void InitLocationManager(void);
270
271         // This method is called when the location manager thread is destroyed.
272         //
273         // @since 2.0
274         //
275         static void DestroyLocationManager(void);
276
277         // This method gets app's accessibility from native side, adding the app into the location
278         // setting, if it is not registerd.
279         //
280         // @since 2.0
281         //
282         static bool GetAppAccessibility(void);
283
284 private:
285         const static RequestId REQ_ID_START_LOC_UPDATES = 1;
286         const static RequestId REQ_ID_STOP_LOC_UPDATES = 2;
287         const static RequestId REQ_ID_RESTART_LOC_UPDATES = 3;
288         const static RequestId REQ_ID_SUSTAIN_GPS = 4;
289         const static RequestId REQ_ID_SUSTAIN_WPS = 5;
290         const static RequestId REQ_ID_SUSTAIN_CPS = 6;
291         const static RequestId REQ_ID_SYNC_LOC_RETRIEVAL = 7;
292         const static RequestId REQ_ID_GET_APP_ACCESSIBILITY = 8;
293         const static RequestId REQ_ID_GET_LAST_LOCATION = 9;
294         const static RequestId REQ_ID_UPDATE_INTERVAL = 10;
295
296         enum _LocationMethodRequested
297         {
298                 LOC_METHOD_REQUESTED_NONE,
299                 LOC_METHOD_REQUESTED_GPS,
300                 LOC_METHOD_REQUESTED_WPS,
301                 LOC_METHOD_REQUESTED_CPS,
302                 LOC_METHOD_REQUESTED_ALL,
303         }
304         __locMethodRequested;
305
306         enum _LocationManagerState
307         {
308                 LOC_MGR_STATE_IDLE,
309                 LOC_MGR_STATE_FAST_SENSING,
310                 LOC_MGR_STATE_FAST_SENSING_SETTLED
311         }
312         __locationMgrState;
313
314         class __LocationManagerHandle
315         {
316         public:
317                 __LocationManagerHandle(void)
318                         : serviceState(LOCATIONS_SERVICE_DISABLED)
319                         , handle(null)
320                         , pLocation(null)
321                 {
322                 }
323
324                 ~__LocationManagerHandle(void)
325                 {
326                 }
327                 
328         public:
329                 location_service_state_e serviceState;
330                 location_manager_h handle;
331                 std::unique_ptr< Tizen::Locations::Location > pLocation;
332         };
333
334         LocationAccuracy __minRequestedAccuracy;
335         int __timerInterval;
336         int __timerTicks;
337         __LocationManagerHandle __gpsHandler;
338         __LocationManagerHandle __wpsHandler;
339         __LocationManagerHandle __cpsHandler;
340         std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pLocRequestInfoList;
341         std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pSyncLocRequestInfoList;
342         std::unique_ptr< Tizen::Base::Runtime::Timer > __pLocUpdateTimer;
343         static _LocationManager* __pUniqueInstance;
344
345         friend class std::default_delete< _LocationManager >;
346 };      // class _LocationManager
347 }}  // Tizen::Locations
348 #endif // _FLOC_INTERNAL_LOCATION_MANAGER_H_