Tizen 2.0 Release
[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 initiates the synchronous location retrieval. The location monitor instance provided is used to intimate back the location.
61         //
62         // @since 2.0
63         //
64         result RegisterLocationMonitor(_LocationMonitor* pLocationMonitor);
65
66         // This method returns the stored location.
67         //
68         // @since 2.0
69         //
70         Location GetLastKnownLocation(void);
71
72         // This method converts the horizontal accuracy in meters to the LocationAccuracy enum type.
73         //
74         // @since 2.0
75         //
76         LocationAccuracy GetAccuracyLevel(double horAcc) const;
77
78         // This method gets the location setting information of this particular application.
79         //
80         // @since 2.0
81         //
82         bool IsAppEnabled(void);
83
84         // This method returns the single instance of the location maanger.
85         //
86         // @since 2.0
87         //
88         static _LocationManager* GetInstance(void);
89
90 private:
91         // This default constructor is intentionally declared as private to implement the Singleton semantic.
92         //
93         // @since 2.0
94         //
95         _LocationManager(void);
96
97         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
98         //
99         // @since 2.0
100         //
101         _LocationManager(const _LocationManager& value);
102
103         // This destructor is intentionally declared as private to implement the Singleton semantic.
104         //
105         // @since 2.0
106         //
107         ~_LocationManager(void);
108
109         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
110         //
111         // @since 2.0
112         //
113         _LocationManager& operator =(const _LocationManager& rhs);
114
115         // The method adds the _LocationRequestInfo instance to the array list.
116         //
117         // @since 2.0
118         //
119         void AddToLocRequestInfoList(const _LocationRequestInfo* pLocRequestInfo);
120
121         // The method removes the _LocationRequestInfo instance corresponding to a reqId from the array list.
122         //
123         // @since 2.0
124         //
125         void RemoveFromLocRequestInfoList(RequestId reqId);
126
127         // The method adds the _SyncLocationRequestInfo instance to the array list.
128         //
129         // @since 2.0
130         //
131         void AddToSyncLocationRequestInfoList(_SyncLocationRequestInfo& syncLocRequestInfo);
132
133         // The method removes the _SyncLocationRequestInfo instance from the list.
134         //
135         // @since 2.0
136         //
137         void RemoveSyncLocRetrievalRequest(_SyncLocationRequestInfo& syncLocRequestInfo);
138
139         // The method restarts all (GPS, WPS and CPS) the native location providers.
140         //
141         // @since 2.0
142         //
143         void RestartLocationUpdates(void);
144
145         // The method restarts the update timer after calculating the GCD of the timeout values requested by each location provider.
146         //
147         // @since 2.0
148         //
149         void RestartUpdateTimer(void);
150
151         // The method is called everytime the sync retrieval timer is expired.
152         //
153         // @since 2.0
154         //
155         void HandleSyncRetrievalTimerExpiry(_SyncLocationRequestInfo& syncLocRequestInfo);
156
157         // The method is called everytime the Async update timer is expired to set the location information.
158         //
159         // @since 2.0
160         //
161         result SetLocationInformation(double latitude, double longitude, double altitude, time_t timestamp, location_method_e locMethod);
162
163         // The method is called to send back the callbacks in case of async location updates.
164         //
165         // @since 2.0
166         //
167         void SendLocationCallbacks(void);
168
169         // This method resets the member variables.
170         //
171         // @since 2.0
172         //
173         void Reset(void);
174
175         // The method calls the construct method of the event driven thread.
176         //
177         // @since 2.0
178         //
179         result Construct(void);
180
181         // This method is gets the last known location from Native side for the given method.
182         //
183         // @since 2.0
184         //
185         Location GetLastKnownLocation(location_method_e nativeLocMethod);
186
187         // @see @ref Tizen::Base::Runtime::EventDrivenThread::OnStart()
188         //
189         // @since 2.0
190         //
191         virtual bool OnStart(void);
192
193         // @see @ref Tizen::Base::Runtime::EventDrivenThread::OnStop()
194         //
195         // @since 2.0
196         //
197         virtual void OnStop(void);
198
199         // @see @ref Tizen::Base::Runtime::EventDrivenThread::OnUserEventReceivedN()
200         //
201         // @since 2.0
202         //
203         virtual void OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs);
204
205         // @see @ref Tizen::Base::Runtime::ITimerEventListener::OnTimerExpired()
206         //
207         // @since 2.0
208         //
209         virtual void OnTimerExpired(Tizen::Base::Runtime::Timer& timer);
210
211         // This callback is recieved from native location manager for satellite information.
212         //
213         // @since 2.0
214         //
215         static bool SatelliteInfoUpdated(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_active, void* user_data);
216
217         // This method is called by the native location provider when the service state of GPS method is changed.
218         //
219         // @since 2.0
220         //
221         static void GpsServiceUpdateCallback(location_service_state_e state, void* user_data);
222
223         // This method is called by the native location provider when the service state of WPS method is changed.
224         //
225         // @since 2.0
226         //
227         static void WpsServiceUpdateCallback(location_service_state_e state, void* user_data);
228
229         // This method is called by the native location provider when the service state of CPS method is changed.
230         //
231         // @since 2.0
232         //
233         static void CpsServiceUpdateCallback(location_service_state_e state, void* user_data);
234
235         // This method initializes the single instance of the location manager.
236         //
237         // @since 2.0
238         //
239         static void InitLocationManager(void);
240
241         // This method is called when the location manager thread is destroyed.
242         //
243         // @since 2.0
244         //
245         static void DestroyLocationManager(void);
246
247         // This method gets app's accessibility from native side, adding the app into the location
248         // setting, if it is not registerd.
249         //
250         // @since 2.0
251         //
252         static bool GetAppAccessibility(void);
253
254         // This method returns the most recent location among available last known locations.
255         //
256         // @since 2.0
257         //
258         Location GetRecentLocationAvailable(void);
259
260 private:
261         const static RequestId REQ_ID_START_LOC_UPDATES = 1;
262         const static RequestId REQ_ID_STOP_LOC_UPDATES = 2;
263         const static RequestId REQ_ID_RESTART_LOC_UPDATES = 3;
264         const static RequestId REQ_ID_SUSTAIN_GPS = 4;
265         const static RequestId REQ_ID_SUSTAIN_WPS = 5;
266         const static RequestId REQ_ID_SUSTAIN_CPS = 6;
267         const static RequestId REQ_ID_SYNC_LOC_RETRIEVAL = 7;
268         const static RequestId REQ_ID_GET_APP_ACCESSIBILITY = 8;
269         const static RequestId REQ_ID_GET_LAST_LOCATION = 9;
270
271         enum _LocationMethodRequested
272         {
273                 LOC_METHOD_REQUESTED_NONE,
274                 LOC_METHOD_REQUESTED_GPS,
275                 LOC_METHOD_REQUESTED_WPS,
276                 LOC_METHOD_REQUESTED_CPS,
277                 LOC_METHOD_REQUESTED_ALL,
278         }
279         __locMethodRequested;
280
281         enum _LocationManagerState
282         {
283                 LOC_MGR_STATE_IDLE,
284                 LOC_MGR_STATE_FAST_SENSING,
285                 LOC_MGR_STATE_FAST_SENSING_SETTLED
286         }
287         __locationMgrState;
288
289         LocationAccuracy __minRequestedAccuracy;
290         location_service_state_e __nativeGPSServiceState;
291         location_service_state_e __nativeWPSServiceState;
292         location_service_state_e __nativeCPSServiceState;
293         int __timerInterval;
294         int __timerTicks;
295         location_manager_h __gpsHandler;
296         location_manager_h __wpsHandler;
297         location_manager_h __cpsHandler;
298         std::unique_ptr< Tizen::Locations::Location > __pCurrentLocation;
299         std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pLocRequestInfoList;
300         std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > __pSyncLocRequestInfoList;
301         std::unique_ptr< Tizen::Base::Runtime::Mutex > __pLocMgrMutex;
302         std::unique_ptr< Tizen::Base::Runtime::Timer > __pLocUpdateTimer;
303         static _LocationManager* __pUniqueInstance;
304
305         friend class std::default_delete< _LocationManager >;
306 };      // class _LocationManager
307 }}  // Tizen::Locations
308 #endif // _FLOC_INTERNAL_LOCATION_MANAGER_H_