merge with master
[framework/osp/locations.git] / src / FLoc_LocationProviderImpl.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_LocationProviderImpl.h
20  * @brief       This is the header file for the %_LocationProviderImpl class.
21  *
22  * This header file contains the declarations of the %_LocationProviderImpl class.
23  */
24
25 #ifndef _FLOC_INTERNAL_LOCATION_PROVIDER_IMPL_H_
26 #define _FLOC_INTERNAL_LOCATION_PROVIDER_IMPL_H_
27
28 #include <unique_ptr.h>
29 #include <FAppIActiveAppEventListener.h>
30 #include <FBaseColAllElementsDeleter.h>
31 #include <FBaseRtIEventListener.h>
32 #include <FBaseRtITimerEventListener.h>
33 #include <FBaseRtTimer.h>
34 #include <FLocLocationCriteria.h>
35 #include <FSysAlarm.h>
36 #include <FSysIAlarmEventListener.h>
37 #include <FBaseRt_Event.h>
38 #include "FLoc_ILocationManagerListener.h"
39 #include "FLoc_ILocProviderEventListener.h"
40 #include "FLoc_LocProviderEventArg.h"
41 #include "FLoc_RegionInfo.h"
42
43 namespace Tizen { namespace Locations
44 {
45
46 class Coordinates;
47 class _LocationManager;
48 class _LocProviderEventArg;
49
50 enum LocationUpdateType
51 {
52         _LOCATION_UPDATE_TYPE_NONE,
53         _LOCATION_UPDATE_TYPE_INTERVAL,
54         _LOCATION_UPDATE_TYPE_DISTANCE
55 };
56
57 class _LocationProviderImpl
58         : public Tizen::Base::Runtime::_Event
59         , public Tizen::Locations::_ILocationManagerListener
60         , public Tizen::Locations::_ILocProviderEventListener
61         , public Tizen::App::IActiveAppEventListener
62         , public Tizen::Base::Runtime::ITimerEventListener
63         , public Tizen::System::IAlarmEventListener
64 {
65 public:
66         /**
67         * This is the default constructor of this class.
68         *
69         * @since 2.0
70         */
71         _LocationProviderImpl(void);
72
73         /**
74         * This is the destructor of this class
75         *
76         * @since 2.0
77         */
78         virtual ~_LocationProviderImpl(void);
79
80         /**
81          * @see @ref Tizen::Locations::LocationProvider::Construct()
82          */
83         result Construct(const LocationCriteria& criteria, ILocationProviderListener& listener);
84
85         /**
86          * @see @ref Tizen::Locations::LocationProvider::StartLocationUpdates()
87          */
88         result StartLocationUpdatesByInterval(int interval);
89
90         /**
91          * @see @ref Tizen::Locations::LocationProvider::StartLocationUpdates()
92          */
93         result StartLocationUpdatesByDistance(double distance);
94
95         /**
96          * @see @ref Tizen::Locations::LocationProvider::StopLocationUdpates()
97          */
98         result StopLocationUpdates(void);
99
100         /**
101          * @see @ref Tizen::Locations::LocationProvider::KeepLocationUpdateAwake()
102          */
103         void KeepLocationUpdateAwake(bool enable);
104
105         /**
106          * @see @ref Tizen::Locations::LocationProvider::AddRegionMonitoring()
107          */
108         result AddMonitoringRegion(const Tizen::Locations::Coordinates& regionCenter, double radius, RegionId& regionId);
109
110         /**
111          * @see @ref Tizen::Locations::LocationProvider::RemoveRegionMonitoring()
112          */
113         result RemoveMonitoringRegion(int regionId);
114
115         /**
116          * @see @ref Tizen::Locations::LocationProvider::RemoveAllRegionMonitoring()
117          */
118         void RemoveAllMonitoringRegions(void);
119
120         /**
121          * @see @ref Tizen::Locations::LocationProvider::GetLocationUpdateStatus()
122          */
123         LocationServiceStatus GetLocationUpdateStatus(void) const;
124
125         /**
126          * @see @ref Tizen::Locations::LocationProvider::GetRegionMonitoringStatus()
127          */
128         LocationServiceStatus GetRegionMonitoringStatus(void) const;
129
130         /**
131          * @see @ref Tizen::Locations::LocationProvider::GetCurrentAccuracy()
132          */
133         LocationAccuracy GetCurrentAccuracy(void) const;
134
135         /**
136          * @see @ref Tizen::Locations::LocationProvider::GetLocation()
137          */
138         static Location GetLocation(const LocationCriteria& criteria);
139
140         /**
141          * @see @ref Tizen::Locations::LocationProvider::GetLocation()
142          */
143         static Location GetLastKnownLocation(void);
144
145 private:
146         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
147         //
148         // @since 2.0
149         //
150         _LocationProviderImpl(const _LocationProviderImpl& rhs);
151
152         // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
153         //
154         // @since 2.0
155         //
156         _LocationProviderImpl& operator =(const _LocationProviderImpl& rhs);
157
158         /**
159          * @see @ref Tizen::Locations::_ILocationManagerListener::OnLocationUpdated()
160          */
161         virtual void OnLocationUpdated(RequestId reqId, const Tizen::Locations::Location& location);
162
163         /**
164          * @see @ref Tizen::Locations::_ILocProviderEventListener::OnLocationEventReceivedN()
165          */
166         virtual void OnLocationEventReceivedN(RequestId reqId, Tizen::Locations::Location& location);
167
168         /**
169          * @see @ref Tizen::Locations::_ILocProviderEventListener::OnLocationUpdateStatusChanged()
170          */
171         virtual void OnLocationUpdateStatusChanged(Tizen::Locations::LocationServiceStatus locSvcStatus);
172
173         /**
174          * @see @ref Tizen::Locations::_ILocProviderEventListener::OnRegionMonitoringStatusChanged()
175          */
176         virtual void OnRegionMonitoringStatusChanged(Tizen::Locations::LocationServiceStatus locSvcStatus);
177
178         /**
179          * @see @ref Tizen::App::IActiveAppEventListener::OnActiveAppChanged()
180          */
181         virtual void OnActiveAppChanged(const Tizen::App::AppId& appId);
182
183         /**
184          * @see @ref Tizen::System::IAlarmEventListener::OnAlarmExpired()
185          */
186         virtual void OnAlarmExpired(Tizen::System::Alarm& alarm);
187
188         /**
189          * @see @ref Tizen::Base::Runtime::ITimerEventListener::OnTimerExpired()
190          */
191         virtual void OnTimerExpired(Tizen::Base::Runtime::Timer& timer);
192
193         // This method requests the location update to the Location Manager.
194         //
195         // @since 2.0
196         //
197         result StartLocationUpdates(LocationUpdateType updateType, int interval, double distance);
198
199         // This method is used to check if the new position is at a certain distance from the original position.
200         //
201         // @since 2.0
202         //
203         bool CheckDistanceThreshold(const Tizen::Locations::Location& oldPosition, const Tizen::Locations::Location& newPosition);
204
205         // This method is used to reset the member variables.
206         //
207         // @since 2.0
208         //
209         void ResetLocationUpdates(void);
210
211         // @see @ref Tizen::Base::Runtime::Event::FireImpl()
212         //
213         // @since 2.0
214         //
215         virtual void FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg);
216
217         // This method is handles the location udpates.
218         //
219         // @since 2.0
220         //
221         void HandleLocationUpdate(Tizen::Locations::Location& location, bool isLocationValid);
222
223         // This method handles the region monitoring.
224         //
225         // @since 2.0
226         //
227         void HandleRegionMonitoring(Tizen::Locations::Location& location, bool isLocationValid);
228
229         // This method sends the callbacks to the applications regarding the Region events.
230         // @since 2.0
231         //
232         void NotifyRegionCrossedStatus(const Tizen::Locations::Location& location);
233
234         // This method finds the region status of a particular region w.r.t the given location.
235         // @since 2.0
236         //
237         _RegionState GetRegionCurrentState(const _RegionInfo& region, const Location& location);
238
239         // This method sends the service status update callback to the application.
240         // @since 2.0
241         //
242         void NotifyServiceStatus(_LocProviderEventType eventType, LocationServiceStatus svcStatus);
243
244         // This method returns the bool value depicting the privilege details depending on the location settings.
245         //
246         // @since 2.0
247         //
248         static bool GetUserPrivilege(void);
249
250         // This method starts the location updates and the timer for region monitoring.
251         //
252         // @since 2.0
253         //
254         result ActivateRegionMonitoring(void);
255
256         // This method stops the location updates and cancels the timer and alarm set for the area monitoring.
257         //
258         // @since 2.0
259         //
260         void StopRegionMonitoring(void);
261
262         // This method determines the time for the alarm to be set for next cycle of region monitoring.
263         //
264         // @since 2.0
265         //
266         void SetNextRegionMonitoringTime(void);
267
268 private:
269         class _LocationUpdater
270         {
271         public:
272                 _LocationUpdater(void)
273                         : firstLocationUpdate(true)
274                         , awakeEnabled(false)
275                         , updateInterval(0)
276                         , status(LOC_SVC_STATUS_IDLE)
277                         , type(_LOCATION_UPDATE_TYPE_NONE)                      
278                         , reqId(-1)
279                         , distanceThreshold(0.0)
280                         , pLocation(null)
281                 {
282                 }
283
284                 ~_LocationUpdater(void)
285                 {
286                 }
287                 
288         public:
289                 bool firstLocationUpdate;
290                 bool awakeEnabled;
291                 int updateInterval;
292                 LocationServiceStatus status;
293                 LocationUpdateType type;
294                 RequestId reqId;
295                 double distanceThreshold;
296                 std::unique_ptr<Tizen::Locations::Location> pLocation;
297         }__locationUpdater;
298         
299         class _RegionMonitor
300         {
301         public:
302                 _RegionMonitor(void)
303                         : reqId(-1)
304                         , status(LOC_SVC_STATUS_IDLE)
305                         , pTimer(null)
306                         , pAlarm(null)
307                         , pLocation(null)
308                         , pRegionList(null)
309                 {
310                 }
311
312                 ~_RegionMonitor(void)
313                 {
314                 }
315                 
316         public:
317                 RequestId reqId;
318                 LocationServiceStatus status;
319                 std::unique_ptr<Tizen::Base::Runtime::Timer> pTimer;
320                 std::unique_ptr<Tizen::System::Alarm> pAlarm;
321                 std::unique_ptr<Tizen::Locations::Location> pLocation;
322                 std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > pRegionList;
323         }__regionMonitor;
324
325         LocationAccuracy __lastLocationAccuracy;
326         LocationCriteria __criteria;
327         ILocationProviderListener* __pLocationListener;
328         _LocationManager* __pLocationManager;
329
330 }; // _LocationProviderImpl
331 }}  // Tizen::Locations
332
333 #endif // _FLOC_INTERNAL_LOCATION_PROVIDER_IMPL_H_