1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/**
* @file FLoc_LocationProviderImpl.cpp
* @brief This is the implementation file for the %_LocationProviderImpl class.
*
* This implementation file contains the definitions of the %_LocationProviderImpl class methods.
*/
#include <unique_ptr.h>
#include <FApp_AppManagerImpl.h>
#include <FAppApp.h>
#include <FAppTypes.h>
#include <FAppUiApp.h>
#include <FBaseColArrayList.h>
#include <FBaseDouble.h>
#include <FBaseRtEvent.h>
#include <FBaseRtTimer.h>
#include <FBaseSysLog.h>
#include <FLocLocationCriteria.h>
#include <FLocCoordinates.h>
#include <FLocILocationProviderListener.h>
#include <FLocLocation.h>
#include <FLocTypes.h>
#include <FSysPowerManager.h>
#include <FSysSystemTime.h>
#include <FSys_SettingInfoImpl.h>
#include "FLoc_LocationImpl.h"
#include "FLoc_LocationManager.h"
#include "FLoc_LocationMonitor.h"
#include "FLoc_LocationProviderImpl.h"
#include "FLoc_MathUtils.h"
#include "FLoc_Types.h"
using namespace Tizen::App;
using namespace Tizen::Base;
using namespace Tizen::Base::Collection;
using namespace Tizen::Base::Runtime;
using namespace Tizen::Base::Utility;
using namespace Tizen::System;
using namespace std;
namespace Tizen { namespace Locations
{
_LocationProviderImpl::_LocationProviderImpl(void)
: Tizen::Base::Runtime::_Event()
, __firstLocationUpdate(true)
, __regionMonitoringActive(false)
, __awakeEnabled(false)
, __updateInterval(0)
, __locationUpdateType(_LOCATION_UPDATE_TYPE_NONE)
, __locationUpdateStatus(LOC_SVC_STATUS_IDLE)
, __regionMonitorStatus(LOC_SVC_STATUS_IDLE)
, __lastLocationAccuracy(LOC_ACCURACY_INVALID)
, __reqId(-1)
, __regionReqId(-1)
, __distanceThreshold(0.0)
, __pLocationListener(null)
, __pLocationManager(null)
, __pLastLocation(null)
, __pRegionList(null)
{
}
_LocationProviderImpl::~_LocationProviderImpl(void)
{
UiApp* pAppInstance = Tizen::App::UiApp::GetInstance();
if (pAppInstance)
{
_AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
if (pAppManager)
{
pAppManager->RemoveActiveAppEventListener(*this);
}
}
StopLocationUpdates();
RemoveAllMonitoringRegions();
}
result
_LocationProviderImpl::Construct(const LocationCriteria& criteria, ILocationProviderListener& listener)
{
result r = E_SUCCESS;
_LocationManager* pLocationManager = null;
pLocationManager = _LocationManager::GetInstance();
SysTryReturn(NID_LOC, pLocationManager != null, GetLastResult(), GetLastResult(), "[%s] Failed to get the location manager instance.", GetErrorMessage(GetLastResult()));
std::unique_ptr< Tizen::Base::Collection::ArrayList, AllElementsDeleter > pRegionList(new (std::nothrow) ArrayList());
SysTryReturn(NID_LOC, pRegionList != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
r = pRegionList->Construct();
SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to construct the list. Propagating.", GetErrorMessage(r));
UiApp* pAppInstance = Tizen::App::UiApp::GetInstance();
if (pAppInstance != null)
{
_AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
SysTryReturn(NID_LOC, pAppManager, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] System error occured.");
r = pAppManager->AddActiveAppEventListener(*this);
SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Error occured during adding the event listener to app manager. Propagating.", GetErrorMessage(r));
}
std::unique_ptr< Tizen::Locations::Location > pLocation(_LocationImpl::GetLocationInstanceN());
SysTryReturn(NID_LOC, pLocation != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
_Event::Initialize();
_Event::AddListener(*this);
__pRegionList = std::move(pRegionList);
__pLastLocation = std::move(pLocation);
__criteria = criteria;
__pLocationListener = &listener;
__pLocationManager = pLocationManager;
SysLog(NID_LOC, "Location provider constructed with the accuracy (%x).", criteria.GetAccuracy());
return E_SUCCESS;
}
result
_LocationProviderImpl::StartLocationUpdatesByInterval(int interval)
{
bool userConsent = GetUserPrivilege();
SysTryReturn(NID_LOC, userConsent, E_USER_NOT_CONSENTED, E_USER_NOT_CONSENTED, "[E_USER_NOT_CONSENTED] The user has disabled the required settings.");
SysTryReturn(NID_LOC, interval >= 1, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The interval(%d) should be greater than or equal to 1", interval);
if (__locationUpdateType == _LOCATION_UPDATE_TYPE_INTERVAL && __updateInterval == interval)
{
return E_SUCCESS;
}
else if (__locationUpdateType == _LOCATION_UPDATE_TYPE_DISTANCE || __locationUpdateType == _LOCATION_UPDATE_TYPE_INTERVAL)
{
__pLocationManager->StopLocationUpdates(__reqId);
__locationUpdateStatus = LOC_SVC_STATUS_IDLE;
}
const double INVALID_DISTANCE_THRESHOLD = 0.0;
return StartLocationUpdates(_LOCATION_UPDATE_TYPE_INTERVAL, interval, INVALID_DISTANCE_THRESHOLD);
}
result
_LocationProviderImpl::StartLocationUpdatesByDistance(double distance)
{
bool userConsent = GetUserPrivilege();
SysTryReturn(NID_LOC, userConsent, E_USER_NOT_CONSENTED, E_USER_NOT_CONSENTED, "[E_USER_NOT_CONSENTED] The user has disabled the required settings.");
SysTryReturn(NID_LOC, distance > 0.0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The distance(%lf) should be greater than 0.0", distance);
SysTryReturn(NID_LOC, Double::IsNaN(distance) == false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The distance is NaN.");
if (__locationUpdateType == _LOCATION_UPDATE_TYPE_DISTANCE && (Double::Compare(__distanceThreshold, distance) == 0))
{
return E_SUCCESS;
}
else if (__locationUpdateType == _LOCATION_UPDATE_TYPE_DISTANCE || __locationUpdateType == _LOCATION_UPDATE_TYPE_INTERVAL)
{
__pLocationManager->StopLocationUpdates(__reqId);
__locationUpdateStatus = LOC_SVC_STATUS_IDLE;
}
const int INVALID_INTERVAL = 0;
return StartLocationUpdates(_LOCATION_UPDATE_TYPE_DISTANCE, INVALID_INTERVAL, distance);
}
result
_LocationProviderImpl::StopLocationUpdates(void)
{
SysLog(NID_LOC, "Stopping the location updates for the request ID (%ld)", __reqId);
SysTryReturn(NID_LOC, __locationUpdateType != _LOCATION_UPDATE_TYPE_NONE, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Location update has not been requested.");
result r = __pLocationManager->StopLocationUpdates(__reqId);
ResetLocationUpdates();
return r;
}
void
_LocationProviderImpl::KeepLocationUpdateAwake(bool enable)
{
if (__awakeEnabled == enable)
{
return;
}
__awakeEnabled = enable;
UiApp* appInstance = Tizen::App::UiApp::GetInstance();
if (appInstance == null) // This is service APP. So should be handled now.
{
SysLog(NID_LOC, "Handling the request awake mode(%d) for the service application.", enable);
if (enable == true)
{
if (__locationUpdateType != _LOCATION_UPDATE_TYPE_NONE && __locationUpdateStatus == LOC_SVC_STATUS_PAUSED)
{
SysLog(NID_LOC, "Requesting to start the location updates as the update type is (%x)", __locationUpdateType);
__locationUpdateStatus = LOC_SVC_STATUS_NOT_FIXED;
__pLocationManager->StartLocationUpdates(__criteria.GetAccuracy(), __updateInterval, this, __reqId);
NotifyServiceStatus(_LOC_PRV_EVENT_SEND_LOC_SVC_CB, __locationUpdateStatus);
}
}
else
{
if (__locationUpdateType != _LOCATION_UPDATE_TYPE_NONE && (__locationUpdateStatus == LOC_SVC_STATUS_RUNNING || __locationUpdateStatus == LOC_SVC_STATUS_NOT_FIXED))
{
SysLog(NID_LOC, "Requesting to stop the location updates as the update type is (%x)", __locationUpdateType);
__locationUpdateStatus = LOC_SVC_STATUS_PAUSED;
__pLocationManager->StopLocationUpdates(__reqId);
NotifyServiceStatus(_LOC_PRV_EVENT_SEND_LOC_SVC_CB, __locationUpdateStatus);
}
}
}
}
result
_LocationProviderImpl::AddMonitoringRegion(const Coordinates& regionCenter, double radius, RegionId& regionId)
{
bool userConsent = GetUserPrivilege();
SysTryReturn(NID_LOC, userConsent, E_USER_NOT_CONSENTED, E_USER_NOT_CONSENTED, "[E_USER_NOT_CONSENTED] The user has disabled the required settings.");
SysTryReturn(NID_LOC, radius >= 50.0 && radius <= 100000.00, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The radius is not within the specified limits.");
SysTryReturn(NID_LOC, (!Double::IsNaN(radius) && !Double::IsNaN(regionCenter.GetLatitude()) && !Double::IsNaN(regionCenter.GetLongitude())),
E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] One of the value is NaN.");
SysLog(NID_LOC, "Requested to add the monitoring region with center (Latitude: %lf, Longitude %lf) and radius (%lf).", regionCenter.GetLatitude(), regionCenter.GetLongitude(), radius);
const int DEAULT_REGION_MONITOR_INTERVAL = 5;
static int nextRegionId = 0;
result r = E_SUCCESS;
std::unique_ptr< _RegionInfo > pRegionInfo(new (std::nothrow) _RegionInfo(regionCenter, radius, nextRegionId));
SysTryReturn(NID_LOC, pRegionInfo != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
r = __pRegionList->Add(*pRegionInfo.get());
SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to add the Region info into the list. Propogated.", GetErrorMessage(r));
pRegionInfo.release();
regionId = nextRegionId;
nextRegionId++;
if (__regionMonitoringActive == true)
{
return E_SUCCESS;
}
r = __pLocationManager->StartLocationUpdates(__criteria.GetAccuracy(), DEAULT_REGION_MONITOR_INTERVAL, this, __regionReqId);
SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to start the location updates. Propogating.", GetErrorMessage(r));
__regionMonitoringActive = true;
__regionMonitorStatus = LOC_SVC_STATUS_NOT_FIXED;
NotifyServiceStatus(_LOC_PRV_EVENT_SEND_MONITOR_SVC_CB, __regionMonitorStatus);
return E_SUCCESS;
}
result
_LocationProviderImpl::RemoveMonitoringRegion(RegionId regionId)
{
int count = __pRegionList->GetCount();
result r = E_SUCCESS;
bool isIdValid = false;
SysLog(NID_LOC, "Total regions currently monitored is (%d).", count);
for (int i = 0; i < count; i++)
{
_RegionInfo* pRegionInfo = static_cast< _RegionInfo* >(__pRegionList->GetAt(i));
if (regionId == pRegionInfo->GetRegionId())
{
__pRegionList->RemoveAt(i, true);
isIdValid = true;
break;
}
}
SysTryReturn(NID_LOC, isIdValid == true, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The region ID is invalid.");
if (__pRegionList->GetCount() == 0)
{
__regionMonitoringActive = false;
__regionMonitorStatus = LOC_SVC_STATUS_IDLE;
if (__locationUpdateStatus == LOC_SVC_STATUS_IDLE) // As we are using the same variable for both region monitoring and location updates, this set is required here.
{
__lastLocationAccuracy = LOC_ACCURACY_INVALID;
}
r = __pLocationManager->StopLocationUpdates(__regionReqId);
SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] All regions are removed but failed to stop the location updates. Propagating.", GetErrorMessage(r));
}
return E_SUCCESS;
}
void
_LocationProviderImpl::RemoveAllMonitoringRegions(void)
{
result r = E_SUCCESS;
__pRegionList->RemoveAll(true);
if (__regionMonitoringActive == true)
{
__regionMonitoringActive = false;
__regionMonitorStatus = LOC_SVC_STATUS_IDLE;
if (__locationUpdateStatus == LOC_SVC_STATUS_IDLE) // As we are using the same variable for both region monitoring and location updates, this set is required here.
{
__lastLocationAccuracy = LOC_ACCURACY_INVALID;
}
r = __pLocationManager->StopLocationUpdates(__regionReqId);
SysTryReturnVoidResult(NID_LOC, r == E_SUCCESS, r, "[%s] All regions are removed but failed to stop the location updates. Ignored.", GetErrorMessage(r));
}
return;
}
LocationServiceStatus
_LocationProviderImpl::GetLocationUpdateStatus(void) const
{
return __locationUpdateStatus;
}
LocationServiceStatus
_LocationProviderImpl::GetRegionMonitoringStatus(void) const
{
return __regionMonitorStatus;
}
LocationAccuracy
_LocationProviderImpl::GetCurrentAccuracy(void) const
{
return __lastLocationAccuracy;
}
Location
_LocationProviderImpl::GetLocation(const LocationCriteria& criteria)
{
Location retLocation(_LocationImpl::GetLocationInstance());
bool userConsent = GetUserPrivilege();
SysTryReturn(NID_LOC, userConsent, retLocation, E_USER_NOT_CONSENTED, "[E_USER_NOT_CONSENTED] The user has disabled the required settings.");
Location* pLocation = null;
const int MAX_TIMEOUT = 30;
result r = E_SUCCESS;
_LocationManager* pLocationManager = null;
SysLog(NID_LOC, "Requesting for single location with criteria (%x).", criteria.GetAccuracy());
std::unique_ptr< _LocationMonitor > pLocMonitor(new (std::nothrow) _LocationMonitor());
SysTryCatch(NID_LOC, pLocMonitor, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
r = pLocMonitor->Construct(MAX_TIMEOUT, criteria.GetAccuracy());
SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Failed to construct the Location Monitor.", GetErrorMessage(r));
pLocationManager = _LocationManager::GetInstance();
SysTryCatch(NID_LOC, pLocationManager, , r, "[%s] Failed to get the location manager instance.", GetErrorMessage(r));
r = pLocationManager->RegisterLocationMonitor(pLocMonitor.get());
SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Failed to register the location monitor.", GetErrorMessage(r));
r = pLocMonitor->Wait();
if (!IsFailed(r))
{
pLocation = pLocMonitor->GetLocationN();
}
if (pLocation)
{
retLocation = *pLocation;
}
else
{
SetLastResult(E_LOCATION_UNAVAILABLE);
}
delete pLocation;
return retLocation;
CATCH:
return retLocation;
}
Location
_LocationProviderImpl::GetLastKnownLocation(void)
{
Location retLocation(_LocationImpl::GetLocationInstance());
bool userConsent = GetUserPrivilege();
SysTryReturn(NID_LOC, userConsent, retLocation, E_USER_NOT_CONSENTED, "[E_USER_NOT_CONSENTED] The user has disabled the required settings.");
_LocationManager* pLocationManager = _LocationManager::GetInstance();
SysTryReturn(NID_LOC, pLocationManager, retLocation, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
retLocation = pLocationManager->GetLastKnownLocation();
if (!retLocation.IsValid())
{
SetLastResult(E_LOCATION_UNAVAILABLE);
}
else
{
ClearLastResult();
}
return retLocation;
}
// Private members
void
_LocationProviderImpl::OnLocationUpdated(RequestId reqId, const Tizen::Locations::Location& location)
{
SysLog(NID_LOC, "Location is updated from Location Manager for the request ID (%ld).", reqId);
std::unique_ptr< Location > pLocation(new (std::nothrow) Location(location));
SysTryReturnVoidResult(NID_LOC, pLocation, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
std::unique_ptr< _LocProviderEventArg > pLocProviderEventArg(new (std::nothrow) _LocProviderEventArg());
SysTryReturnVoidResult(NID_LOC, pLocProviderEventArg, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
pLocProviderEventArg->SetEventType(_LOC_PRV_EVENT_SEND_LOC);
pLocProviderEventArg->SetLocation(pLocation.get());
pLocProviderEventArg->SetRequestId(reqId);
pLocation.release();
result r = _Event::FireAsync(*pLocProviderEventArg);
SysTryReturnVoidResult(NID_LOC, r == E_SUCCESS, r, "[%s] Failed to fire the event.", GetErrorMessage(r));
pLocProviderEventArg.release();
return;
}
void
_LocationProviderImpl::OnLocationEventReceivedN(RequestId reqId, Tizen::Locations::Location& location)
{
SysLog(NID_LOC, "Location Event received.");
bool isNew = false;
LocationAccuracy currentAccuracy = LOC_ACCURACY_INVALID;
long long lastLocationTime = 0;
Location* pLocation = &location;
_LocationImpl* pLocationImpl = _LocationImpl::GetInstance(*pLocation);
if (__pLastLocation)
{
lastLocationTime = _LocationImpl::GetInstance(*__pLastLocation.get())->GetTimestampInMs();
}
long long timeDifference = abs(pLocationImpl->GetTimestampInMs() - lastLocationTime);
SysLog(NID_LOC, "Time difference between last location timestamp (%lld) and current location timestamp (%lld) is (%lld).", lastLocationTime, pLocationImpl->GetTimestampInMs(), timeDifference);
if (timeDifference > 0)
{
isNew = true;
currentAccuracy = __pLocationManager->GetAccuracyLevel(pLocation->GetHorizontalAccuracy());
}
if (currentAccuracy != __lastLocationAccuracy)
{
SysLog(NID_LOC, "Notify the accuracy change.");
__lastLocationAccuracy = currentAccuracy;
__pLocationListener->OnAccuracyChanged(currentAccuracy);
}
if (reqId == __reqId)
{
HandleLocationUpdate(location, isNew);
}
else if (reqId == __regionReqId)
{
HandleRegionMonitoring(location, isNew);
}
delete pLocation;
}
void
_LocationProviderImpl::OnLocationUpdateStatusChanged(Tizen::Locations::LocationServiceStatus locSvcStatus)
{
__pLocationListener->OnLocationUpdateStatusChanged(locSvcStatus);
}
void
_LocationProviderImpl::OnRegionMonitoringStatusChanged(Tizen::Locations::LocationServiceStatus locSvcStatus)
{
__pLocationListener->OnRegionMonitoringStatusChanged(locSvcStatus);
}
void
_LocationProviderImpl::OnActiveAppChanged(const Tizen::App::AppId& appId)
{
Tizen::App::App* pApp = Tizen::App::App::GetInstance();
Tizen::App::AppId currentAppId = pApp->GetAppId();
SysLog(NID_LOC, "Active App ID is (%ls) and the current app Id is (%ls)", appId.GetPointer(), currentAppId.GetPointer());
if (currentAppId == appId)
{
SysLog(NID_LOC, "Application is active.");
if (__locationUpdateStatus == LOC_SVC_STATUS_PAUSED)
{
SysLog(NID_LOC, "Start the location updates as the location update status is PAUSED.");
__pLocationManager->StartLocationUpdates(__criteria.GetAccuracy(), __updateInterval, this, __reqId);
__locationUpdateStatus = LOC_SVC_STATUS_NOT_FIXED;
__pLocationListener->OnLocationUpdateStatusChanged(__locationUpdateStatus);
}
}
else
{
SysLog(NID_LOC, "Application is not active.");
if (__awakeEnabled == false && (__locationUpdateStatus == LOC_SVC_STATUS_RUNNING || __locationUpdateStatus == LOC_SVC_STATUS_NOT_FIXED))
{
SysLog(NID_LOC, "Stop the location updates as application is not active with awake mode as (%d) and location update state as (%x).", __awakeEnabled, __locationUpdateStatus);
__pLocationManager->StopLocationUpdates(__reqId);
__locationUpdateStatus = LOC_SVC_STATUS_PAUSED;
__pLocationListener->OnLocationUpdateStatusChanged(__locationUpdateStatus);
}
}
}
result
_LocationProviderImpl::StartLocationUpdates(LocationUpdateType updateType, int interval, double distance)
{
result r = E_SUCCESS;
const int CHECKING_INTERVAL = 10;
if (updateType == _LOCATION_UPDATE_TYPE_INTERVAL)
{
__updateInterval = interval;
}
else if (updateType == _LOCATION_UPDATE_TYPE_DISTANCE)
{
__updateInterval = CHECKING_INTERVAL;
__distanceThreshold = distance;
}
__locationUpdateType = updateType;
if (__awakeEnabled == true)
{
r = __pLocationManager->StartLocationUpdates(__criteria.GetAccuracy(), __updateInterval, this, __reqId);
SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to start the Native location updates.", GetErrorMessage(r));
__locationUpdateStatus = LOC_SVC_STATUS_NOT_FIXED;
}
else
{
UiApp* pAppInstance = Tizen::App::UiApp::GetInstance();
if (pAppInstance == null)
{
__locationUpdateStatus = LOC_SVC_STATUS_PAUSED;
}
else
{
AppUiState appUiState = pAppInstance->GetAppUiState();
if (appUiState == APP_UI_STATE_FOREGROUND || appUiState == APP_UI_STATE_PARTIAL_BACKGROUND)
{
r = __pLocationManager->StartLocationUpdates(__criteria.GetAccuracy(), __updateInterval, this, __reqId);
SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Failed to start the location updates. Propagating.", GetErrorMessage(r));
__locationUpdateStatus = LOC_SVC_STATUS_NOT_FIXED;
}
else
{
__locationUpdateStatus = LOC_SVC_STATUS_PAUSED;
}
}
}
SysLog(NID_LOC, "Update type is (%x). The request Id is (%ld) and the update status is (%x).", __locationUpdateType, __reqId, __locationUpdateStatus);
NotifyServiceStatus(_LOC_PRV_EVENT_SEND_LOC_SVC_CB, __locationUpdateStatus);
return E_SUCCESS;
CATCH:
__locationUpdateType = _LOCATION_UPDATE_TYPE_NONE;
return r;
}
bool
_LocationProviderImpl::CheckDistanceThreshold(const Location& oldPosition, const Location& newPosition)
{
double displacement = 0.0;
const Coordinates coordOld = oldPosition.GetCoordinates();
const Coordinates coordNew = newPosition.GetCoordinates();
if (__firstLocationUpdate)
{
SysLog(NID_LOC, "First location update. So send true.");
__firstLocationUpdate = false;
return true;
}
displacement = coordOld.GetDistanceTo(coordNew);
SysLog(NID_LOC, "Displacement is (%lf)", displacement);
return ((displacement > __distanceThreshold) ? true : false);
}
void
_LocationProviderImpl::ResetLocationUpdates(void)
{
__firstLocationUpdate = true;
__reqId = -1;
__lastLocationAccuracy = LOC_ACCURACY_INVALID;
__locationUpdateType = _LOCATION_UPDATE_TYPE_NONE;
__locationUpdateStatus = LOC_SVC_STATUS_IDLE;
__updateInterval = 0;
__distanceThreshold = 0.0;
}
void
_LocationProviderImpl::FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg)
{
_ILocProviderEventListener* pLocProviderEventListener = dynamic_cast< _ILocProviderEventListener* >(&listener);
SysTryReturnVoidResult(NID_LOC, pLocProviderEventListener, E_SYSTEM, "[E_INVALID_ARG] The listener is null.");
IEventArg* pArg = const_cast< IEventArg* >(&arg);
_LocProviderEventArg* pEventArg = dynamic_cast< _LocProviderEventArg* >(pArg);
SysTryReturnVoidResult(NID_LOC, pEventArg, E_SYSTEM, "[E_INVALID_ARG] Event argument is null.");
_LocProviderEventType eventType = pEventArg->GetEventType();
switch (eventType)
{
case _LOC_PRV_EVENT_SEND_LOC:
pLocProviderEventListener->OnLocationEventReceivedN(pEventArg->GetRequestId(), *pEventArg->GetLocationN());
break;
case _LOC_PRV_EVENT_SEND_LOC_SVC_CB:
pLocProviderEventListener->OnLocationUpdateStatusChanged(pEventArg->GetLocServiceStatus());
break;
case _LOC_PRV_EVENT_SEND_MONITOR_SVC_CB:
pLocProviderEventListener->OnRegionMonitoringStatusChanged(pEventArg->GetLocServiceStatus());
break;
}
}
void
_LocationProviderImpl::HandleLocationUpdate(Tizen::Locations::Location& location, bool isNew)
{
LocationServiceStatus newLocationUpdateStatus = __locationUpdateStatus;
if (isNew)
{
if (__locationUpdateType != _LOCATION_UPDATE_TYPE_NONE && __locationUpdateStatus != LOC_SVC_STATUS_PAUSED)
{
newLocationUpdateStatus = LOC_SVC_STATUS_RUNNING;
}
}
else if (_LocationImpl::GetInstance(location)->IsDenied())
{
SysLog(NID_LOC, "User consent not available.");
if (__locationUpdateType != _LOCATION_UPDATE_TYPE_NONE && __locationUpdateStatus != LOC_SVC_STATUS_PAUSED)
{
newLocationUpdateStatus = LOC_SVC_STATUS_DENIED;
}
}
else
{
SysLog(NID_LOC, "Invalid Location Update.");
if (__locationUpdateType != _LOCATION_UPDATE_TYPE_NONE && __locationUpdateStatus != LOC_SVC_STATUS_PAUSED)
{
newLocationUpdateStatus = LOC_SVC_STATUS_NOT_FIXED;
}
}
if (newLocationUpdateStatus != __locationUpdateStatus)
{
SysLog(NID_LOC, "Location Update Satus changed to (%x). Notify the status.", newLocationUpdateStatus);
__locationUpdateStatus = newLocationUpdateStatus;
__pLocationListener->OnLocationUpdateStatusChanged(__locationUpdateStatus);
}
if (newLocationUpdateStatus == LOC_SVC_STATUS_RUNNING)
{
if (__locationUpdateType == _LOCATION_UPDATE_TYPE_DISTANCE)
{
if (CheckDistanceThreshold(*__pLastLocation.get(), location) == true)
{
SysLog(NID_LOC, "Location displacement exceeds the distance threshold (%lf). Notify the location.", __distanceThreshold);
__pLocationListener->OnLocationUpdated(location);
*__pLastLocation.get() = location;
}
}
else if (__locationUpdateType == _LOCATION_UPDATE_TYPE_INTERVAL)
{
SysLog(NID_LOC, "Location time interval expired. Notify the location.");
__pLocationListener->OnLocationUpdated(location);
*__pLastLocation.get() = location;
}
}
}
void
_LocationProviderImpl::HandleRegionMonitoring(Tizen::Locations::Location& location, bool isNew)
{
LocationServiceStatus newRegionMonitorStatus = __regionMonitorStatus;
if (isNew)
{
newRegionMonitorStatus = LOC_SVC_STATUS_RUNNING;
}
else if (_LocationImpl::GetInstance(location)->IsDenied())
{
newRegionMonitorStatus = LOC_SVC_STATUS_DENIED;
}
else
{
newRegionMonitorStatus = LOC_SVC_STATUS_NOT_FIXED;
}
if (newRegionMonitorStatus != __regionMonitorStatus)
{
SysLog(NID_LOC, "Region Monitoring Satus changed to (%x). Notify the status.", newRegionMonitorStatus);
__regionMonitorStatus = newRegionMonitorStatus;
__pLocationListener->OnRegionMonitoringStatusChanged(__regionMonitorStatus);
}
if (newRegionMonitorStatus == LOC_SVC_STATUS_RUNNING)
{
NotifyRegionCrossedStatus(location);
*__pLastLocation.get() = location;
}
}
void
_LocationProviderImpl::NotifyRegionCrossedStatus(const Tizen::Locations::Location& location)
{
int count = __pRegionList->GetCount();
SysLog(NID_LOC, "Number of regions currently monitored is (%d)", count);
for (int i = 0; i < count; i++)
{
_RegionInfo* pRegionInfo = static_cast< _RegionInfo* >(__pRegionList->GetAt(i));
if (pRegionInfo)
{
Coordinates regionCoordinate = pRegionInfo->GetCoordinate();
_RegionState currentState = REGION_STATE_UNKNOWN;
_RegionState previousState = pRegionInfo->GetPreviousValidState();
bool notifyStateChange = false;
currentState = GetRegionCurrentState(*pRegionInfo, location);
SysLog(NID_LOC, "Current Region state is (%d) and Previous Region state is (%d)", currentState, previousState);
if (currentState != REGION_STATE_UNKNOWN && previousState != currentState)
{
notifyStateChange = true;
pRegionInfo->SetValidPreviousState(currentState);
}
if ((pRegionInfo->GetRegionState() == REGION_STATE_INSIDE || pRegionInfo->GetRegionState() == REGION_STATE_UNKNOWN) && currentState == REGION_STATE_OUTSIDE)
{
if (notifyStateChange)
{
SysLog(NID_LOC, "Notify the boundary crossed event as previous valid region state is INSIDE and current state is OUTSIDE.");
__pLocationListener->OnRegionLeft(pRegionInfo->GetRegionId());
}
}
else if ((pRegionInfo->GetRegionState() == REGION_STATE_OUTSIDE || pRegionInfo->GetRegionState() == REGION_STATE_UNKNOWN) && currentState == REGION_STATE_INSIDE)
{
if (notifyStateChange)
{
SysLog(NID_LOC, "Notify the boundary crossed event as previous valid region state is OUTSIDE and current state is INSIDE.");
__pLocationListener->OnRegionEntered(pRegionInfo->GetRegionId());
}
}
pRegionInfo->SetRegionState(currentState);
}
}
}
_RegionState
_LocationProviderImpl::GetRegionCurrentState(const _RegionInfo& region, const Location& location)
{
TryReturn(location.GetHorizontalAccuracy() >= 0.0, REGION_STATE_UNKNOWN, "Location received with invalid accuracy");
SysLog(NID_LOC, "[RegionID %d] Region Information is (Center latitude: %lf, Center longitude: %lf, Region radius:%lf", region.GetRegionId(), region.GetCoordinate().GetLatitude(),
region.GetCoordinate().GetLongitude(), region.GetRadius());
SysLog(NID_LOC, "[RegionID %d] Location Information is (Latitude: %lf, Longitude: %lf, Horizontal accuracy:%lf", region.GetRegionId(), location.GetCoordinates().GetLatitude(),
location.GetCoordinates().GetLongitude(), location.GetHorizontalAccuracy());
_RegionState regionState = REGION_STATE_UNKNOWN;
double distanceBtwCenters = region.GetCoordinate().GetDistanceTo(location.GetCoordinates());
double regionRadius = region.GetRadius();
double locationRadius = location.GetHorizontalAccuracy();
SysLog(NID_LOC, "[RegionID %d] The distance between centers is (%lf)", region.GetRegionId(), distanceBtwCenters);
if (distanceBtwCenters >= (regionRadius + locationRadius))
{
regionState = REGION_STATE_OUTSIDE;
}
else if (distanceBtwCenters < regionRadius && Double::Compare(locationRadius, 0.0) == 0)
{
SysLog(NID_LOC, "[RegionID %d] Location Radius is 0 and distance < regionRadius", region.GetRegionId());
regionState = REGION_STATE_INSIDE;
}
else
{
double radiusThreshold = (1 / Math::Sqrt(2)) * locationRadius;
if (regionRadius < radiusThreshold)
{
SysLog(NID_LOC, "[RegionID %d] Region circle is less than 50 percent area of the location circle.", region.GetRegionId());
regionState = (distanceBtwCenters >= locationRadius) ? REGION_STATE_OUTSIDE : REGION_STATE_UNKNOWN;
return regionState;
}
else
{
double overlapRegion = _MathUtils::CalculateOverlapRegion(region, location);
double upperThreshold = 0;
double lowerThreshold = 0;
double occupancy = 0;
SysLog(NID_LOC, "[RegionID %d] OverlapRegion is (%lf)", region.GetRegionId(), overlapRegion);
if (regionRadius >= locationRadius)
{
// Calculate occupancy % with the location circle
SysLog(NID_LOC, "[RegionID %d] Region radius is bigger.", region.GetRegionId());
occupancy = (overlapRegion / (Math::GetPi() * locationRadius * locationRadius)) * 100;
upperThreshold = 60;
lowerThreshold = 40;
}
else if (regionRadius >= radiusThreshold)
{
SysLog(NID_LOC, "[RegionID %d] The ratio of Region radius to location radius is between 0.707 and 1.", region.GetRegionId());
double thresholdValue = -136.51 * (regionRadius / locationRadius) + 146.51; // This equation varies the threshold value from 10 (for R/r = 1) to 50 (for R/r = 0.707)
SysLog(NID_LOC, "[RegionID %d] Threshold value is %lf.", region.GetRegionId(), thresholdValue);
occupancy = (overlapRegion / (Math::GetPi() * locationRadius * locationRadius)) * 100;
upperThreshold = 50 + thresholdValue;
lowerThreshold = 50 - thresholdValue;
}
// Decide the state with as per the inner and outer thresholds
SysLog(NID_LOC, "[RegionID %d] Occupancy is (%lf), Upper threshold is (%lf) and lower threshold is (%lf).", region.GetRegionId(), occupancy, upperThreshold, lowerThreshold);
regionState = (occupancy >= upperThreshold) ? REGION_STATE_INSIDE : ((occupancy <= lowerThreshold) ? REGION_STATE_OUTSIDE : REGION_STATE_UNKNOWN);
}
}
SysLog(NID_LOC, "[RegionID %d] Returning region state as (%d).", region.GetRegionId(), regionState);
return regionState;
}
void
_LocationProviderImpl::NotifyServiceStatus(_LocProviderEventType eventType, LocationServiceStatus svcStatus)
{
std::unique_ptr< _LocProviderEventArg > pLocProviderEventArg(new (std::nothrow) _LocProviderEventArg());
if (pLocProviderEventArg)
{
pLocProviderEventArg->SetEventType(eventType);
pLocProviderEventArg->SetLocServiceStatus(svcStatus);
result r = _Event::FireAsync(*pLocProviderEventArg.get());
SysTryReturnVoidResult(NID_LOC, r == E_SUCCESS, r, "[%s] Failed to fire the event.", GetErrorMessage(r));
pLocProviderEventArg.release();
}
return;
}
bool
_LocationProviderImpl::GetUserPrivilege(void)
{
_LocationManager* pLocMgr = _LocationManager::GetInstance();
SysTryReturn(NID_LOC, pLocMgr, false, E_SYSTEM, "[E_SYSTEM] Failed to get the location manager instance.");
bool appSettingEnabled = pLocMgr->IsAppEnabled();
if (appSettingEnabled == false)
{
return false;
}
bool hasPrivilege = false;
result gps = E_SUCCESS;
result wps = E_SUCCESS;
bool gpsEnabled = true;
bool wpsEnabled = true;
gps = _SettingInfoImpl::GetValue(L"http://tizen.org/setting/location.gps", gpsEnabled);
wps = _SettingInfoImpl::GetValue(L"http://tizen.org/setting/location.wps", wpsEnabled);
hasPrivilege = gpsEnabled | wpsEnabled;
if (gps != E_SUCCESS || wps != E_SUCCESS || hasPrivilege == false)
{
return false;
}
return true;
}
}}
|