2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FLoc_LocationManager.cpp
20 * @brief This is the implementation file for the %_LocationManager class.
22 * This implementation file contains the definitions of the %_LocationManager class.
28 #include <unique_ptr.h>
29 #include <FBaseColArrayList.h>
30 #include <FBaseInteger.h>
31 #include <FBaseBoolean.h>
32 #include <FBaseRtMutex.h>
33 #include <FBaseRtTimer.h>
34 #include <FBaseRtMonitor.h>
35 #include <FBaseRtWaitingLoop.h>
36 #include <FBaseSysLog.h>
37 #include <FLocCoordinates.h>
38 #include <FSysSystemTime.h>
39 #include <FSys_PowerManagerImpl.h>
40 #include "FLoc_Config.h"
41 #include "FLoc_LocationImpl.h"
42 #include "FLoc_LocationManager.h"
43 #include "FLoc_LocationMonitor.h"
44 #include "FLoc_LocationRequestInfo.h"
45 #include "FLoc_SyncLocationRequestInfo.h"
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Base::Utility;
51 using namespace Tizen::System;
54 namespace Tizen { namespace Locations
57 _LocationManager* _LocationManager::__pUniqueInstance = null;
59 _LocationManager::_LocationManager(void)
60 : Tizen::Base::Runtime::EventDrivenThread()
61 , __locMethodRequested(LOC_METHOD_REQUESTED_NONE)
62 , __locationMgrState(LOC_MGR_STATE_IDLE)
63 , __minRequestedAccuracy(LOC_ACCURACY_INVALID)
66 , __pLocRequestInfoList(null)
67 , __pSyncLocRequestInfoList(null)
68 , __pLocUpdateTimer(null)
69 , __initialized(false)
73 _LocationManager::~_LocationManager(void)
78 _LocationManager::StartLocationUpdates(LocationAccuracy accuracy, int interval, _ILocationManagerListener* pListener, RequestId& reqId)
80 static RequestId nextLocRequestId = 1;
81 const int ARRAY_LIST_CAPACITY = 1;
83 std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > pArgList(new (std::nothrow) ArrayList());
84 SysTryReturn(NID_LOC, pArgList, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
86 std::unique_ptr< _LocationRequestInfo > pLocRequestInfo(new (std::nothrow) _LocationRequestInfo(accuracy, interval, pListener, nextLocRequestId));
87 SysTryReturn(NID_LOC, pLocRequestInfo, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
89 result r = pArgList->Construct(ARRAY_LIST_CAPACITY);
90 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Array Construct failed. Propogating.", GetErrorMessage(r));
91 r = pArgList->Add(*pLocRequestInfo);
92 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Arraylist addition of an object failed. Propogating", GetErrorMessage(r));
94 pLocRequestInfo.release();
96 r = SendUserEvent(REQ_ID_START_LOC_UPDATES, pArgList.get());
97 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to send the user event. Propagating.", GetErrorMessage(r));
99 reqId = nextLocRequestId;
104 SysLog(NID_LOC, "Request ID (%ld) is assigned for the location request with accuracy (%d) and interval (%d).", reqId, accuracy, interval);
109 _LocationManager::StopLocationUpdates(RequestId reqId)
111 SysLog(NID_LOC, "Location update stop requested for request ID (%d).", reqId);
112 const int ARRAY_LIST_CAPACITY = 1;
114 std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > pArgList(new (std::nothrow) ArrayList());
115 SysTryReturn(NID_LOC, pArgList, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
117 std::unique_ptr< Integer > pReqId(new (std::nothrow) Integer(static_cast< int >(reqId)));
118 SysTryReturn(NID_LOC, pReqId, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
120 result r = pArgList->Construct(ARRAY_LIST_CAPACITY);
121 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Array Construct failed. Propogating.", GetErrorMessage(r));
122 r = pArgList->Add(*pReqId);
123 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Arraylist addition of an object failed. Propogating.", GetErrorMessage(r));
127 r = SendUserEvent(REQ_ID_STOP_LOC_UPDATES, pArgList.get());
128 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to send the user event. Propagating.", GetErrorMessage(r));
135 _LocationManager::ChangeUpdateInterval(RequestId reqId, int interval)
137 SysLog(NID_LOC, "Interval update requested for request ID (%d).", reqId);
138 const int ARRAY_LIST_CAPACITY = 2;
140 std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > pArgList(new (std::nothrow) ArrayList());
141 SysTryReturn(NID_LOC, pArgList, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
142 result r = pArgList->Construct(ARRAY_LIST_CAPACITY);
143 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Array Construct failed. Propogating.", GetErrorMessage(r));
145 std::unique_ptr< Integer > pReqId(new (std::nothrow) Integer(static_cast< int >(reqId)));
146 SysTryReturn(NID_LOC, pReqId, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
148 r = pArgList->Add(*pReqId);
149 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Arraylist addition of an object failed. Propogating.", GetErrorMessage(r));
153 std::unique_ptr< Integer > pInterval(new (std::nothrow) Integer(interval));
154 SysTryReturn(NID_LOC, pInterval, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
156 r = pArgList->Add(*pInterval);
157 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Arraylist addition of an object failed. Propogating.", GetErrorMessage(r));
161 r = SendUserEvent(REQ_ID_UPDATE_INTERVAL, pArgList.get());
162 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Failed to send the user event. Propagating.", GetErrorMessage(r));
169 _LocationManager::RegisterLocationMonitor(_LocationMonitor* pLocationMonitor)
172 const int ARRAY_LIST_CAPACITY = 1;
174 result r = StartLocationUpdates(pLocationMonitor->GetAccuracy(), 1, null, reqId);
175 SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Error to request location updates.", GetErrorMessage(r));
177 std::unique_ptr< _SyncLocationRequestInfo > pSyncLocationRequestInfo(new (std::nothrow) _SyncLocationRequestInfo(pLocationMonitor, reqId));
178 std::unique_ptr< Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter > pArgList(new (std::nothrow) ArrayList());
179 SysTryCatch(NID_LOC, pSyncLocationRequestInfo != null && pArgList != null, , E_OUT_OF_MEMORY,
180 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
182 r = pArgList->Construct(ARRAY_LIST_CAPACITY);
183 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Array Construct failed. Propogating.", GetErrorMessage(r));
184 r = pArgList->Add(*pSyncLocationRequestInfo.get());
185 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Arraylist addition of an object failed. Propogating.", GetErrorMessage(r));
187 pSyncLocationRequestInfo.release();
189 r = SendUserEvent(REQ_ID_SYNC_LOC_RETRIEVAL, pArgList.get());
190 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Failed to send the user event. Propagating.", GetErrorMessage(r));
196 StopLocationUpdates(reqId);
201 _LocationManager::GetLastKnownLocation(void)
203 Location location = _LocationImpl::GetLocationInstance();
204 Monitor synchronizer;
205 result r = synchronizer.Construct();
206 SysTryReturn(NID_LOC, r == E_SUCCESS, location, r, "[%s] Propogating.", GetErrorMessage(r));
208 std::unique_ptr< Tizen::Base::Collection::ArrayList > pArgList(new (std::nothrow) ArrayList());
209 SysTryReturn(NID_LOC, pArgList, location, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
210 r = pArgList->Construct();
211 SysTryReturn(NID_LOC, r == E_SUCCESS, location, r, "[%s] Propogating.", GetErrorMessage(r));
213 r = pArgList->Add(location);
214 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Propogating", GetErrorMessage(r));
215 r = pArgList->Add(synchronizer);
216 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Propogating", GetErrorMessage(r));
218 r = synchronizer.Enter();
219 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Propogating", GetErrorMessage(r));
221 r = SendUserEvent(REQ_ID_GET_LAST_LOCATION, pArgList.get());
222 SysTryCatch(NID_LOC, r == E_SUCCESS, synchronizer.Exit(), r, "[%s] Propogating", GetErrorMessage(r));
225 SysLog(NID_LOC, "Wait till location manager gets the last location.");
226 r = synchronizer.Wait();
227 SysTryCatch(NID_LOC, r == E_SUCCESS, synchronizer.Exit(), r, "[%s] Propogating", GetErrorMessage(r));
229 SysLog(NID_LOC, "Last location(validity: %x, method: %ls) retrieved.",
230 location.IsValid(), location.GetExtraInfo(L"location_method").GetPointer());
235 pArgList->RemoveAll(false);
240 _LocationManager::GetAccuracyLevel(double horAcc) const
244 return LOC_ACCURACY_FINEST;
246 else if (horAcc <= 10.0)
248 return LOC_ACCURACY_TEN_METERS;
250 else if (horAcc <= 100.0)
252 return LOC_ACCURACY_HUNDRED_METERS;
254 else if (horAcc <= 1000.0)
256 return LOC_ACCURACY_ONE_KILOMETER;
258 else if (horAcc <= 10000.0)
260 return LOC_ACCURACY_ANY;
264 return LOC_ACCURACY_INVALID;
269 _LocationManager::IsAppEnabled(void)
271 SysTryReturn(NID_LOC, __initialized, false, E_SYSTEM, "[%s] Location manager is not initialized.", GetErrorMessage(E_SYSTEM));
273 Boolean enable(false);
274 Monitor synchronizer;
275 result r = synchronizer.Construct();
276 SysTryReturn(NID_LOC, r == E_SUCCESS, false, r, "[%s] Propogating.", GetErrorMessage(r));
278 std::unique_ptr< Tizen::Base::Collection::ArrayList > pArgList(new (std::nothrow) ArrayList());
279 SysTryReturn(NID_LOC, pArgList, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
280 r = pArgList->Construct();
281 SysTryReturn(NID_LOC, r == E_SUCCESS, false, r, "[%s] Propogating.", GetErrorMessage(r));
283 r = pArgList->Add(enable);
284 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Propogating", GetErrorMessage(r));
285 r = pArgList->Add(synchronizer);
286 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Propogating", GetErrorMessage(r));
288 r = synchronizer.Enter();
289 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Propogating", GetErrorMessage(r));
291 r = SendUserEvent(REQ_ID_GET_APP_ACCESSIBILITY, pArgList.get());
292 SysTryCatch(NID_LOC, r == E_SUCCESS, synchronizer.Exit(), r, "[%s] Propogating", GetErrorMessage(r));
295 SysLog(NID_LOC, "Wait till location manager checks if the app is allowed to use location.");
296 r = synchronizer.Wait();
297 SysTryCatch(NID_LOC, r == E_SUCCESS, synchronizer.Exit(), r, "[%s] Propogating", GetErrorMessage(r));
298 SysLog(NID_LOC, "Application setting state is '%ls'.", enable.ToString().GetPointer());
301 return enable.ToBool();
308 _LocationManager::GetInstance(void)
310 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
311 if (__pUniqueInstance == null)
314 pthread_once(&onceBlock, InitLocationManager);
316 result r = GetLastResult();
319 onceBlock = PTHREAD_ONCE_INIT;
322 return __pUniqueInstance;
326 _LocationManager::AddToLocRequestInfoList(const _LocationRequestInfo* pLocRequestInfo)
328 std::unique_ptr< _LocationRequestInfo > pNewLocRequestInfo(new (std::nothrow) _LocationRequestInfo(*pLocRequestInfo));
329 SysTryReturnVoidResult(NID_LOC, pNewLocRequestInfo, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
330 __pLocRequestInfoList->Add(*pNewLocRequestInfo.get());
331 pNewLocRequestInfo.release();
333 if (__minRequestedAccuracy == LOC_ACCURACY_INVALID)
335 result r = _PowerManagerImpl::PowerControl(1, 1);
336 SysTryReturnVoidResult(NID_LOC, r == E_SUCCESS, r, "[%s] Failed to keep the CPU in awake state.", GetErrorMessage(r));
338 SysLog(NID_LOC, "Total count of request info is (%d).", __pLocRequestInfoList->GetCount());
339 RestartLocationUpdates();
340 RestartUpdateTimer();
344 _LocationManager::RemoveFromLocRequestInfoList(RequestId reqId)
346 const int LIST_COUNT_ZERO = 0;
347 int count = __pLocRequestInfoList->GetCount();
349 for (int i = 0; i < count; i++)
351 const _LocationRequestInfo* pLocRequestInfo = static_cast< const _LocationRequestInfo* >(__pLocRequestInfoList->GetAt(i));
352 if (reqId == pLocRequestInfo->GetRequestId())
354 __pLocRequestInfoList->RemoveAt(i);
358 SysLog(NID_LOC, "Total count of request info is (%d).", __pLocRequestInfoList->GetCount());
359 if (__pLocRequestInfoList->GetCount() == LIST_COUNT_ZERO)
361 _PowerManagerImpl::PowerControl(1, 0);
363 __pLocUpdateTimer->Cancel();
364 location_manager_stop(__gpsHandler.handle);
365 location_manager_stop(__wpsHandler.handle);
371 RestartLocationUpdates();
372 RestartUpdateTimer();
377 _LocationManager::AddToSyncLocationRequestInfoList(_SyncLocationRequestInfo& syncLocRequestInfo)
379 static bool hasRetrievalStarted = false;
381 result r = __pSyncLocRequestInfoList->Add(syncLocRequestInfo);
382 SysTryCatch(NID_LOC, r == E_SUCCESS, , r, "[%s] Failed to add the sync request into the list.", GetErrorMessage(r));
384 if (hasRetrievalStarted == true)
386 SysLog(NID_LOC, "The retrieval under process.");
389 while (__pSyncLocRequestInfoList->GetCount() != 0)
391 hasRetrievalStarted = true;
392 const int WAIT_TIME = 1000;
393 WaitingLoop::GetInstance()->Wait(WAIT_TIME);
395 int count = __pSyncLocRequestInfoList->GetCount();
396 for (int i = 0; i < count; i++)
398 _SyncLocationRequestInfo* pSyncLocRequestInfo = static_cast< _SyncLocationRequestInfo* >(__pSyncLocRequestInfoList->GetAt(i));
399 if (pSyncLocRequestInfo != null)
401 SysLog(NID_LOC, "Handle the sync location request with request ID (%ld)", pSyncLocRequestInfo->GetRequestId());
403 HandleSyncRetrievalTimerExpiry(*pSyncLocRequestInfo);
407 hasRetrievalStarted = false;
411 StopLocationUpdates(syncLocRequestInfo.GetRequestId());
412 syncLocRequestInfo.GetLocationMonitor()->Notify();
413 delete &syncLocRequestInfo;
417 _LocationManager::RemoveSyncLocRetrievalRequest(_SyncLocationRequestInfo& syncLocRequestInfo)
419 StopLocationUpdates(syncLocRequestInfo.GetRequestId());
420 __pSyncLocRequestInfoList->Remove(syncLocRequestInfo, true);
424 _LocationManager::RestartLocationUpdates(void)
426 int count = __pLocRequestInfoList->GetCount();
428 bool isAccuracyChanged = false;
430 for (int i = 0; i < count; i++)
432 const _LocationRequestInfo* pLocRequestInfo = static_cast< const _LocationRequestInfo* >(__pLocRequestInfoList->GetAt(i));
433 if (__minRequestedAccuracy > pLocRequestInfo->GetAccuracy() || __minRequestedAccuracy == LOC_ACCURACY_INVALID)
435 __minRequestedAccuracy = pLocRequestInfo->GetAccuracy();
436 isAccuracyChanged = true;
439 if (!isAccuracyChanged)
443 SysLog(NID_LOC, "Location updates are restarted and new minimum requested Accuracy is (%d).", __minRequestedAccuracy);
445 switch (__locMethodRequested)
447 case LOC_METHOD_REQUESTED_NONE:
449 SysLog(NID_LOC, "None of the methods running. Start all");
451 result = location_manager_start(__gpsHandler.handle);
452 SysTryLog(NID_LOC, result == 0, "Failed to start the location updates for GPS.");
454 result = location_manager_start(__wpsHandler.handle);
455 SysTryLog(NID_LOC, result == 0, "Failed to start the location updates for WPS.");
459 case LOC_METHOD_REQUESTED_GPS:
461 SysLog(NID_LOC, "GPS Running. Start WPS.");
463 result = location_manager_start(__wpsHandler.handle);
464 SysTryLog(NID_LOC, result == 0, "Failed to start the location updates for WPS.");
468 case LOC_METHOD_REQUESTED_WPS:
470 SysLog(NID_LOC, "WPS Running. Start GPS.");
472 result = location_manager_start(__gpsHandler.handle);
473 SysTryLog(NID_LOC, result == 0, "Failed to start the location updates for GPS.");
477 case LOC_METHOD_REQUESTED_ALL:
479 SysLog(NID_LOC, "All the providers are already started.");
484 __locationMgrState = LOC_MGR_STATE_FAST_SENSING;
485 __locMethodRequested = LOC_METHOD_REQUESTED_ALL;
489 _LocationManager::RestartUpdateTimer(void)
492 int count = __pLocRequestInfoList->GetCount();
494 __pLocUpdateTimer->Cancel();
498 const _LocationRequestInfo* pLocRequestInfo = static_cast< _LocationRequestInfo* >(__pLocRequestInfoList->GetAt(0));
499 gcd = pLocRequestInfo->GetInterval();
501 for (int i = 1; i < count; i++)
503 pLocRequestInfo = static_cast< const _LocationRequestInfo* >(__pLocRequestInfoList->GetAt(i));
504 int interval = pLocRequestInfo->GetInterval();
507 while (gcd != 0 && interval != 0)
509 ((gcd > interval) ? gcd : interval) %= ((gcd > interval) ? interval : gcd);
515 __timerInterval = gcd;
518 SysLog(NID_LOC, "Updated Timer interval is (%d).", __timerInterval);
519 result r = __pLocUpdateTimer->Start(__timerInterval * 1000);
522 SysLog(NID_LOC, "Failed to start the Location update timer.");
527 _LocationManager::HandleSyncRetrievalTimerExpiry(_SyncLocationRequestInfo& syncLocRequestInfo)
529 LocationAccuracy accuracy = LOC_ACCURACY_INVALID;
530 DateTime timestamp = DateTime::GetMinValue();
532 Location location = GetRecentLocationAvailable();
533 SysLog(NID_LOC, "Location(timestamp: %ls, validity: %x, accuracy: %f) is retrieved.",
534 location.GetTimestamp().ToString().GetPointer(), location.IsValid(), location.GetHorizontalAccuracy());
535 if (location.IsValid())
537 accuracy = GetAccuracyLevel(location.GetHorizontalAccuracy());
538 timestamp = location.GetTimestamp();
541 bool inTime = syncLocRequestInfo.IsInTime(timestamp);
542 bool accurate = syncLocRequestInfo.IsAccuracySatisfying(accuracy);
543 if (inTime && accurate)
545 unique_ptr< Location > pLocation(new (std::nothrow) Location(location));
546 SysTryCatchLabel(NID_LOC, pLocation != null, , NOTIFY, E_OUT_OF_MEMORY,
547 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
548 syncLocRequestInfo.GetLocationMonitor()->SetLocation(pLocation.get());
553 if (syncLocRequestInfo.GetTickCount() > MAX_WAIT_TIME_FOR_SYNC_LOC)
557 unique_ptr< Location > pLocation(new (std::nothrow) Location(location));
558 SysTryCatchLabel(NID_LOC, pLocation != null, , NOTIFY, E_OUT_OF_MEMORY,
559 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
560 syncLocRequestInfo.GetLocationMonitor()->SetLocation(pLocation.get());
567 SysLog(NID_LOC, "Unable to fix the current location in %d ticks.", MAX_WAIT_TIME_FOR_SYNC_LOC);
572 SysLog(NID_LOC, "Wait for next location(Tick: %d).", syncLocRequestInfo.GetTickCount());
573 syncLocRequestInfo.IncrementTickCount();
577 syncLocRequestInfo.GetLocationMonitor()->Notify();
578 RemoveSyncLocRetrievalRequest(syncLocRequestInfo);
582 _LocationManager::SetLocationInformation(double latitude, double longitude, double altitude, time_t timestamp, location_method_e locMethod, Location* pLocation)
584 SysLog(NID_LOC, "Location Information is: Latitude (%lf), Longitude (%lf), Altitude (%lf), TimeStamp (%ld), Location method (%d)", latitude, longitude, altitude, timestamp, locMethod);
587 LocationAccuracy requiredAcc;
591 location_accuracy_level_e accLevel = LOCATIONS_ACCURACY_NONE;
592 double horAcc = Tizen::Locations::NaN;
593 double verAcc = Tizen::Locations::NaN;
596 double climb = Tizen::Locations::NaN;
597 double direction = Tizen::Locations::NaN;
598 double speed = Tizen::Locations::NaN;
599 time_t time_stamp = 0;
602 Location locationData = _LocationImpl::GetLocationInstance();
603 _LocationImpl* pLocDataImpl = _LocationImpl::GetInstance(locationData);
607 case LOCATIONS_METHOD_GPS:
609 res = location_manager_get_accuracy(__gpsHandler.handle, &accLevel, &horAcc, &verAcc);
610 SysLog(NID_LOC, "Get Accuracy: Result (%d), Horizontal Acc (%lf), Vertical Acc (%lf)", res, horAcc, verAcc);
611 SysTryReturnResult(NID_LOC, res == 0, E_SYSTEM, "Invalid accuracy values from Native Location Provider.");
613 res = location_manager_get_velocity(__gpsHandler.handle, &climb, &direction, &speed, &time_stamp);
614 SysLog(NID_LOC, "Get Velocity: Result (%d), Climb (%lf), Direction (%lf), Speed (%lf), Time stamp (%ld)", res, climb, direction, speed, time_stamp);
615 SysTryReturnResult(NID_LOC, res == 0, E_SYSTEM, "Invalid accuracy values from Native Location Provider.");
617 pLocDataImpl->SetExtraInfo(L"location_method", L"gps");
618 requestId = REQ_ID_SUSTAIN_GPS;
622 case LOCATIONS_METHOD_WPS:
624 res = location_manager_get_accuracy(__wpsHandler.handle, &accLevel, &horAcc, &verAcc);
625 SysLog(NID_LOC, "Get Accuracy: Result (%d), Horizontal Acc (%lf), Vertical Acc (%lf)", res, horAcc, verAcc);
626 SysTryReturnResult(NID_LOC, res == 0, E_SYSTEM, "Invalid accuracy values from Native Location Provider.");
628 res = location_manager_get_velocity(__wpsHandler.handle, &climb, &direction, &speed, &time_stamp);
629 SysTryLog(NID_LOC, res == 0, "Get Velocity: Result (%d), Climb (%lf), Direction (%lf), Speed (%lf), Time stamp (%ld)", res, climb, direction, speed, time_stamp);
631 pLocDataImpl->SetExtraInfo(L"location_method", L"network");
632 requestId = REQ_ID_SUSTAIN_WPS;
637 SysTryReturn(NID_LOC, false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The location method is not valid");
640 Coordinates coordinates;
641 coordinates.Set(latitude, longitude, altitude);
642 pLocDataImpl->SetCoordinates(coordinates);
643 pLocDataImpl->SetHorizontalAccuracy(horAcc);
644 pLocDataImpl->SetVerticalAccuracy(verAcc);
645 pLocDataImpl->SetTimestamp((long long) timestamp * 1000);
646 pLocDataImpl->SetCourse(direction);
647 pLocDataImpl->SetSpeed(speed);
648 pLocDataImpl->SetValidity(true);
649 *pLocation = locationData;
651 requiredAcc = __minRequestedAccuracy;
654 case LOC_ACCURACY_FINEST:
655 rangeValue = ACCURACY_FINEST;
658 case LOC_ACCURACY_TEN_METERS:
659 rangeValue = ACCURACY_TEN_MTRS;
662 case LOC_ACCURACY_HUNDRED_METERS:
663 rangeValue = ACCURACY_HUNDRED_MTRS;
666 case LOC_ACCURACY_ONE_KILOMETER:
667 rangeValue = ACCURACY_ONE_KILOMETER;
670 case LOC_ACCURACY_ANY:
671 rangeValue = ACCURACY_ANY;
674 case LOC_ACCURACY_INVALID:
677 SysAssertf(false, "The Accuracy value is not defined.");
680 if (__locationMgrState == LOC_MGR_STATE_FAST_SENSING && horAcc <= rangeValue)
682 SysLog(NID_LOC, "State is LOC_MGR_STATE_FAST_SENSING and accuracy is within range. RequestId is (%d)", requestId);
683 __locationMgrState = LOC_MGR_STATE_FAST_SENSING_SETTLED;
684 SendUserEvent(requestId, null);
686 else if (__locationMgrState == LOC_MGR_STATE_FAST_SENSING_SETTLED && horAcc > rangeValue)
688 SysLog(NID_LOC, "State is LOC_MGR_STATE_FAST_SENSING_SETTLED and accuracy is out of range. Restart location updates.");
689 SendUserEvent(REQ_ID_RESTART_LOC_UPDATES, null);
695 _LocationManager::SendLocationCallbacks(void)
697 bool isAllowed = GetAppAccessibility();
698 const Location* pBestLocation = FindBestLocation();
700 SysLog(NID_LOC, "Send location(validity: %x) and accessibility(%x) through the callback.", pBestLocation->IsValid(), isAllowed);
701 unique_ptr< Location > pLocation(new (std::nothrow) Location(*pBestLocation));
702 SysTryReturnVoidResult(NID_LOC, pLocation != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
704 int interval = __timerTicks * __timerInterval;
705 for (int i = 0; i < __pLocRequestInfoList->GetCount(); i++)
707 const _LocationRequestInfo* pLocRequestInfo = static_cast< const _LocationRequestInfo* >(__pLocRequestInfoList->GetAt(i));
708 if (interval % pLocRequestInfo->GetInterval() == 0 && pLocRequestInfo->GetListener() != null)
712 _LocationImpl::GetInstance(*pLocation)->TurnOnDeniedFlag();
714 pLocRequestInfo->GetListener()->OnLocationUpdated(pLocRequestInfo->GetRequestId(), *pLocation.get());
720 _LocationManager::Reset(void)
722 __locMethodRequested = LOC_METHOD_REQUESTED_NONE;
723 __locationMgrState = LOC_MGR_STATE_IDLE;
724 __minRequestedAccuracy = LOC_ACCURACY_INVALID;
730 _LocationManager::Construct()
732 return EventDrivenThread::Construct();
736 _LocationManager::GetLastKnownLocation(location_method_e nativeLocMethod)
738 String locationMethod;
739 location_manager_h nativeHandle = null;
740 switch (nativeLocMethod)
742 case LOCATIONS_METHOD_GPS:
744 nativeHandle = __gpsHandler.handle;
745 locationMethod = L"gps";
749 case LOCATIONS_METHOD_WPS:
751 nativeHandle = __wpsHandler.handle;
752 locationMethod = L"network";
759 SysLog(NID_LOC, "Last location requested(method: %x, handle: %x).", nativeLocMethod, nativeHandle);
761 Location location = _LocationImpl::GetLocationInstance();
762 if (nativeHandle != null)
764 _LocationImpl* pLocationImpl = _LocationImpl::GetInstance(location);
766 double altitude = Tizen::Locations::NaN;
767 double latitude = Tizen::Locations::NaN;
768 double longitude = Tizen::Locations::NaN;
769 time_t timestampPosition = 0;
771 int res = location_manager_get_last_position(nativeHandle, &altitude, &latitude, &longitude, ×tampPosition);
772 SysTryCatch(NID_LOC, res == 0, , E_SYSTEM, "[E_SYSTEM] Failed to fetch last position from nativeHandle(%x).", nativeHandle);
773 SysLog(NID_LOC, "Last position(latitude: %lf, longitude: %lf, altitude: %lf, timestamp: %ld",
774 latitude, longitude, altitude, timestampPosition);
777 coord.Set(latitude, longitude, altitude);
778 pLocationImpl->SetCoordinates(coord);
780 double horAcc = Tizen::Locations::NaN;
781 double verAcc = Tizen::Locations::NaN;
782 location_accuracy_level_e level;
783 res = location_manager_get_last_accuracy(nativeHandle, &level, &horAcc, &verAcc);
786 SysLog(NID_LOC, "Last accuracy(horAcc: %lf, vAcc: %lf, level: %x)", horAcc, verAcc, level);
787 pLocationImpl->SetHorizontalAccuracy(horAcc);
788 pLocationImpl->SetVerticalAccuracy(verAcc);
791 double climb = Tizen::Locations::NaN;
792 double direction = Tizen::Locations::NaN;
793 double speed = Tizen::Locations::NaN;
794 time_t timestampVelocity = 0;
795 res = location_manager_get_last_velocity(nativeHandle, &climb, &direction, &speed, ×tampVelocity);
798 SysLog(NID_LOC, "Last velocity(climb: %lf, direction: %lf, speed: %lf, timestamp: %ld)",
799 climb, direction, speed, timestampVelocity);
800 pLocationImpl->SetCourse(direction);
801 pLocationImpl->SetSpeed(speed);
804 if (nativeLocMethod == LOCATIONS_METHOD_GPS)
806 int satUsedCount = 0;
807 int satViewCount = 0;
808 time_t timestampSatellite = 0;
809 String satInfo = L"";
810 res = gps_status_get_last_satellite(nativeHandle, &satUsedCount, &satViewCount, ×tampSatellite);
813 long timeDiff = abs(timestampPosition - timestampSatellite);
814 res = gps_status_foreach_last_satellites_in_view(nativeHandle, SatelliteInfoUpdated, &satInfo);
815 SysLog(NID_LOC, "Last satellite(foreachResult: %d, inUse: %d, inView: %d, timestamp: %ld, timeDiff: %ld)",
816 res, satUsedCount, satViewCount, timestampSatellite, timeDiff);
819 SysLog(NID_LOC, "Last satellite(info: %ls)", satInfo.GetPointer());
820 pLocationImpl->SetExtraInfo(L"satellite", satInfo);
823 pLocationImpl->SetTimestamp(((long long)timestampPosition) * 1000); // in milliseconds
824 pLocationImpl->SetValidity(true);
825 pLocationImpl->SetExtraInfo(L"location_method", locationMethod);
836 _LocationManager::GetRecentLocationAvailable(void)
838 long long gpsTimestamp = 0;
839 long long wpsTimestamp = 0;
841 Location lastGpsLocation = GetLastKnownLocation(LOCATIONS_METHOD_GPS);
842 if (lastGpsLocation.IsValid())
844 gpsTimestamp = _LocationImpl::GetInstance(lastGpsLocation)->GetTimestampInMs();
847 Location lastWpsLocation = GetLastKnownLocation(LOCATIONS_METHOD_WPS);
848 if (lastWpsLocation.IsValid())
850 wpsTimestamp = _LocationImpl::GetInstance(lastWpsLocation)->GetTimestampInMs();
853 SysLog(NID_LOC, "Compare timestamp(gps: %lld, wps: %lld) for recent location.", gpsTimestamp, wpsTimestamp);
854 if (gpsTimestamp >= wpsTimestamp)
856 return lastGpsLocation;
860 return lastWpsLocation;
865 _LocationManager::UpdateLocRequestInfoList(RequestId reqId, int interval)
867 int count = __pLocRequestInfoList->GetCount();
869 for (int i = 0; i < count; i++)
871 _LocationRequestInfo* pLocRequestInfo = static_cast< _LocationRequestInfo* >(__pLocRequestInfoList->GetAt(i));
872 if (reqId == pLocRequestInfo->GetRequestId())
874 pLocRequestInfo->SetInterval(interval);
875 RestartUpdateTimer();
882 _LocationManager::FindBestLocation(void)
884 switch (__locMethodRequested)
886 case LOC_METHOD_REQUESTED_GPS:
888 SysLog(NID_LOC, "GPS location provider running.");
889 return __gpsHandler.pLocation.get();
893 case LOC_METHOD_REQUESTED_WPS:
895 SysLog(NID_LOC, "WPS location provider running.");
896 return __wpsHandler.pLocation.get();
900 case LOC_METHOD_REQUESTED_ALL:
902 SysLog(NID_LOC, "All the methods are running. Get the best location among all the providers.");
903 long long gpsTimestamp = 0;
904 long long wpsTimestamp = 0;
906 if (__gpsHandler.pLocation->IsValid() && !_LocationImpl::GetInstance(*__gpsHandler.pLocation)->IsDenied())
908 gpsTimestamp = _LocationImpl::GetInstance(*__gpsHandler.pLocation)->GetTimestampInMs();
910 if (__wpsHandler.pLocation->IsValid() && !_LocationImpl::GetInstance(*__wpsHandler.pLocation)->IsDenied())
912 wpsTimestamp = _LocationImpl::GetInstance(*__wpsHandler.pLocation)->GetTimestampInMs();
915 SysLog(NID_LOC, "Compare timestamp(gps: %lld, wps: %lld) of different locations.", gpsTimestamp, wpsTimestamp);
916 if (gpsTimestamp > wpsTimestamp)
918 SysLog(NID_LOC, "GPS time stamp is greater than WPS.");
919 return __gpsHandler.pLocation.get();
921 else if (wpsTimestamp > gpsTimestamp)
923 SysLog(NID_LOC, "WPS time stamp is greater than GPS.");
924 return __wpsHandler.pLocation.get();
926 else if (gpsTimestamp == wpsTimestamp)
928 if (__gpsHandler.pLocation->GetHorizontalAccuracy() <= __wpsHandler.pLocation->GetHorizontalAccuracy())
930 SysLog(NID_LOC, "GPS time stamp is equal to WPS and GPS accuracy is better than WPS.");
931 return __gpsHandler.pLocation.get();
935 SysLog(NID_LOC, "WPS time stamp is equal to GPS but WPS accuracy is better than GPS.");
936 return __wpsHandler.pLocation.get();
942 case LOC_METHOD_REQUESTED_NONE:
945 SysLog(NID_LOC, "Location updates not running.");
949 SysLog(NID_LOC, "Returning null as none of the conditions are satsfied.");
954 _LocationManager::GetLocation(location_method_e nativeLocMethod)
956 const int MAX_VALID_TIME_DIFFERENCE = 2000;
957 double altitude = 0.0;
958 double latitude = 0.0;
959 double longitude = 0.0;
962 int satellitesInViewCount = 0;
963 int satellitesInUseCount = 0;
964 time_t timestampSatellite = 0;
965 String satInfo = L"";
966 long long timeDiff = 0;
967 location_manager_h nativeHandle = null;
968 Location* pLocation = null;
970 switch (nativeLocMethod)
972 case LOCATIONS_METHOD_GPS:
974 nativeHandle = __gpsHandler.handle;
975 pLocation = __gpsHandler.pLocation.get();
979 case LOCATIONS_METHOD_WPS:
981 nativeHandle = __wpsHandler.handle;
982 pLocation = __wpsHandler.pLocation.get();
990 res = location_manager_get_position(nativeHandle, &altitude, &latitude, &longitude, ×tamp);
991 SysTryReturnResult(NID_LOC, res == 0, E_SYSTEM, "[E_SYSTEM] Failed to obtain the natvie location information for the method (%x)", nativeLocMethod);
995 result r = SetLocationInformation(latitude, longitude, altitude, timestamp, nativeLocMethod, pLocation);
998 SysLog(NID_LOC, "Failed to set the location information");
1001 if (nativeLocMethod == LOCATIONS_METHOD_GPS)
1003 res = gps_status_get_satellite(__gpsHandler.handle, &satellitesInUseCount, &satellitesInViewCount, ×tampSatellite);
1005 timeDiff = abs(timestamp - timestampSatellite);
1006 timeDiff = timeDiff * 1000;
1007 SysLog(NID_LOC, "Result (%d), Satellites in Use (%d), Satellites in View (%d), Time stamp (%ld), Time Difference (Loc and Sat) (%ld)",
1008 res, satellitesInUseCount, satellitesInViewCount, timestampSatellite, timeDiff);
1009 if (res == 0 && timeDiff <= MAX_VALID_TIME_DIFFERENCE)
1011 res = gps_status_foreach_satellites_in_view(__gpsHandler.handle, SatelliteInfoUpdated, &satInfo);
1014 SysLog(NID_LOC, "Result of get satellite is (%d) and satelliteInfo string representation is (%ls)", res, satInfo.GetPointer());
1015 _LocationImpl::GetInstance(*__gpsHandler.pLocation.get())->SetExtraInfo(L"satellite", satInfo);
1023 _LocationManager::OnStart(void)
1027 std::unique_ptr< Tizen::Base::Collection::ArrayList, AllElementsDeleter > pLocInfoRequestList(new (std::nothrow) ArrayList());
1028 SysTryReturn(NID_LOC, pLocInfoRequestList != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1029 result r = pLocInfoRequestList->Construct();
1030 SysTryReturn(NID_LOC, r == E_SUCCESS, false, E_SYSTEM, "[E_SYSTEM] Failed to construct the Location Request list.");
1032 std::unique_ptr< Tizen::Base::Collection::ArrayList, AllElementsDeleter > pSyncLocInfoRequestList(new (std::nothrow) ArrayList());
1033 SysTryReturn(NID_LOC, pSyncLocInfoRequestList != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1034 r = pSyncLocInfoRequestList->Construct();
1035 SysTryReturn(NID_LOC, r == E_SUCCESS, false, E_SYSTEM, "[E_SYSTEM] Failed to construct the Sync Location Request list.");
1037 unique_ptr< Tizen::Locations::Location > pGpsLocation(_LocationImpl::GetLocationInstanceN());
1038 SysTryReturn(NID_LOC, pGpsLocation != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1039 unique_ptr< Tizen::Locations::Location > pWpsLocation(_LocationImpl::GetLocationInstanceN());
1040 SysTryReturn(NID_LOC, pWpsLocation != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1042 std::unique_ptr< Tizen::Base::Runtime::Timer > pLocUpdateTimer(new (std::nothrow) Timer());
1043 r = pLocUpdateTimer->Construct(*this);
1044 SysTryReturn(NID_LOC, r == E_SUCCESS, false, E_SYSTEM, "[E_SYSTEM] Failed to construct the location timer.");
1046 res = location_manager_create(LOCATIONS_METHOD_GPS, &__gpsHandler.handle);
1047 SysTryReturn(NID_LOC, res == 0, false, E_SYSTEM, "[E_SYSTEM] Failed to create Native GPS Location provider.");
1048 res = location_manager_set_service_state_changed_cb(__gpsHandler.handle, GpsServiceUpdateCallback, this);
1049 SysTryCatch(NID_LOC, res == 0, , E_SYSTEM, "[E_SYSTEM] Failed to register service callback for Native GPS Location provider.");
1051 res = location_manager_create(LOCATIONS_METHOD_WPS, &__wpsHandler.handle);
1052 SysTryCatch(NID_LOC, res == 0, , E_SYSTEM, "[E_SYSTEM] Failed to create Native WPS Location provider.");
1053 res = location_manager_set_service_state_changed_cb(__wpsHandler.handle, WpsServiceUpdateCallback, this);
1054 SysTryCatch(NID_LOC, res == 0, , E_SYSTEM, "[E_SYSTEM] Failed to register service callback for Native WPS Location provider.");
1056 __pLocRequestInfoList = std::move(pLocInfoRequestList);
1057 __pSyncLocRequestInfoList = std::move(pSyncLocInfoRequestList);
1058 __pLocUpdateTimer = std::move(pLocUpdateTimer);
1059 __gpsHandler.pLocation= std::move(pGpsLocation);
1060 __wpsHandler.pLocation= std::move(pWpsLocation);
1062 __initialized = true;
1063 SysLog(NID_LOC, "All the resources for location manager successfully created.");
1067 if (__gpsHandler.handle)
1069 location_manager_destroy(__gpsHandler.handle);
1072 if (__wpsHandler.handle)
1074 location_manager_destroy(__wpsHandler.handle);
1080 _LocationManager::OnStop(void)
1082 __initialized = false;
1084 if (__gpsHandler.handle)
1086 location_manager_destroy(__gpsHandler.handle);
1088 if (__wpsHandler.handle)
1090 location_manager_destroy(__wpsHandler.handle);
1095 _LocationManager::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
1097 const int ARRAY_LIST_CAPACITY = 1;
1101 case REQ_ID_START_LOC_UPDATES:
1103 SysLog(NID_LOC, "REQ_ID_START_LOC_UPDATES");
1104 SysTryReturnVoidResult(NID_LOC, pArgs, E_INVALID_ARG, "[E_INVALID_ARG] Null argument encountered. Ignored.");
1105 _LocationRequestInfo* pLocRequestInfo = null;
1106 SysTryCatch(NID_LOC, pArgs->GetCount() == ARRAY_LIST_CAPACITY, pArgs->RemoveAll(true), E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1107 pLocRequestInfo = static_cast< _LocationRequestInfo* >(pArgs->GetAt(0));
1108 SysTryCatch(NID_LOC, pLocRequestInfo, , E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1110 AddToLocRequestInfoList(pLocRequestInfo);
1114 case REQ_ID_STOP_LOC_UPDATES:
1116 SysLog(NID_LOC, "REQ_ID_STOP_LOC_UPDATES");
1117 SysTryReturnVoidResult(NID_LOC, pArgs, E_INVALID_ARG, "[E_INVALID_ARG] Null argument encountered. Ignored.");
1118 Integer* pReqId = null;
1119 SysTryCatch(NID_LOC, pArgs->GetCount() == ARRAY_LIST_CAPACITY, pArgs->RemoveAll(true), E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1120 pReqId = static_cast< Integer* >(pArgs->GetAt(0));
1121 SysTryCatch(NID_LOC, pReqId, , E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1123 RemoveFromLocRequestInfoList(static_cast< long >(pReqId->ToInt()));
1127 case REQ_ID_RESTART_LOC_UPDATES:
1129 SysLog(NID_LOC, "REQ_ID_RESTART_LOC_UPDATES.");
1130 RestartLocationUpdates();
1134 case REQ_ID_SUSTAIN_GPS:
1136 SysLog(NID_LOC, "REQ_ID_SUSTAIN_GPS.");
1137 location_manager_stop(__wpsHandler.handle);
1138 __locMethodRequested = LOC_METHOD_REQUESTED_GPS;
1142 case REQ_ID_SUSTAIN_WPS:
1144 SysLog(NID_LOC, "REQ_ID_SUSTAIN_WPS.");
1145 location_manager_stop(__gpsHandler.handle);
1146 __locMethodRequested = LOC_METHOD_REQUESTED_WPS;
1150 case REQ_ID_SYNC_LOC_RETRIEVAL:
1152 SysLog(NID_LOC, "REQ_ID_SYNC_LOC_RETRIEVAL.");
1153 SysTryReturnVoidResult(NID_LOC, pArgs, E_INVALID_ARG, "[E_INVALID_ARG] Null argument encountered. Ignored.");
1154 _SyncLocationRequestInfo* pSyncLocRequestInfo = null;
1155 SysTryCatch(NID_LOC, pArgs->GetCount() == ARRAY_LIST_CAPACITY, , E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1156 pSyncLocRequestInfo = static_cast< _SyncLocationRequestInfo* >(pArgs->GetAt(0));
1157 SysTryCatch(NID_LOC, pSyncLocRequestInfo, , E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1159 AddToSyncLocationRequestInfoList(*pSyncLocRequestInfo);
1164 case REQ_ID_GET_APP_ACCESSIBILITY:
1166 SysLog(NID_LOC, "REQ_ID_GET_APP_ACCESSIBILITY");
1167 SysTryReturnVoidResult(NID_LOC, pArgs, E_INVALID_ARG, "[E_INVALID_ARG] Null argument encountered. Ignored.");
1169 Boolean* pEnabled = static_cast< Boolean* >(pArgs->GetAt(0));
1170 Monitor* pMonitor = static_cast< Monitor* >(pArgs->GetAt(1));
1171 SysAssertf(pEnabled != null && pMonitor != null, "Invalid request has been made on the location manager.");
1172 if (pMonitor != null)
1175 if (pEnabled != null)
1177 pEnabled->value = GetAppAccessibility();
1187 case REQ_ID_GET_LAST_LOCATION:
1189 SysLog(NID_LOC, "REQ_ID_GET_LAST_LOCATION");
1190 SysTryReturnVoidResult(NID_LOC, pArgs, E_INVALID_ARG, "[E_INVALID_ARG] Null argument encountered. Ignored.");
1192 Location* pLocation = static_cast< Location* >(pArgs->GetAt(0));
1193 Monitor* pMonitor = static_cast< Monitor* >(pArgs->GetAt(1));
1194 SysAssertf(pLocation != null && pMonitor != null, "Invalid request has been made on the location manager.");
1195 if (pMonitor != null)
1198 if (pLocation != null)
1200 *pLocation = GetRecentLocationAvailable();
1210 case REQ_ID_UPDATE_INTERVAL:
1212 const int ARRAY_LIST_LENGTH = 2;
1214 SysLog(NID_LOC, "REQ_ID_UPDATE_INTERVAL");
1215 SysTryReturnVoidResult(NID_LOC, pArgs, E_INVALID_ARG, "[E_INVALID_ARG] Null argument encountered. Ignored.");
1216 SysTryCatch(NID_LOC, pArgs->GetCount() == ARRAY_LIST_LENGTH, pArgs->RemoveAll(true), E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1218 Integer* pReqId = null;
1219 pReqId = static_cast< Integer* >(pArgs->GetAt(0));
1220 SysTryCatch(NID_LOC, pReqId, , E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1222 Integer* pInterval = null;
1223 pInterval = static_cast< Integer* >(pArgs->GetAt(1));
1224 SysTryCatch(NID_LOC, pInterval, , E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument encountered. Ignored.");
1226 UpdateLocRequestInfoList(static_cast< long >(pReqId->ToInt()), pInterval->ToInt());
1234 pArgs->RemoveAll(true);
1241 pArgs->RemoveAll(true);
1246 _LocationManager::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
1248 if (__pLocUpdateTimer->Equals(timer))
1251 SysLog(NID_LOC, "LocationManager update timer expired. Timer Tick value is (%d).", __timerTicks);
1252 result r = E_SUCCESS;
1254 switch (__locMethodRequested)
1256 case LOC_METHOD_REQUESTED_GPS:
1258 r = GetLocation(LOCATIONS_METHOD_GPS);
1259 SysTryLog(NID_LOC, r == E_SUCCESS, "[E_SYSTEM] Failed to obtain the GPS location information");
1262 SendUserEvent(REQ_ID_RESTART_LOC_UPDATES, null);
1267 case LOC_METHOD_REQUESTED_WPS:
1269 r = GetLocation(LOCATIONS_METHOD_WPS);
1270 SysTryLog(NID_LOC, r == E_SUCCESS, "[E_SYSTEM] Failed to obtain the WPS location information");
1273 SendUserEvent(REQ_ID_RESTART_LOC_UPDATES, null);
1278 case LOC_METHOD_REQUESTED_ALL:
1280 if (__wpsHandler.serviceState == LOCATIONS_SERVICE_ENABLED)
1282 r = GetLocation(LOCATIONS_METHOD_WPS);
1283 SysTryLog(NID_LOC, r == E_SUCCESS, "[E_SYSTEM] Failed to obtain the WPS location information");
1287 SysLog(NID_LOC, "WPS Location not available at the Native side.");
1290 if (__gpsHandler.serviceState == LOCATIONS_SERVICE_ENABLED)
1292 r = GetLocation(LOCATIONS_METHOD_GPS);
1293 SysTryLog(NID_LOC, r == E_SUCCESS, "[E_SYSTEM] Failed to obtain the GPS location information");
1297 SysLog(NID_LOC, "GPS Location not available at the Native side.");
1302 case LOC_METHOD_REQUESTED_NONE:
1305 SysLog(NID_LOC, "Timer expired when no location update is called.");
1309 __pLocUpdateTimer->Start(__timerInterval * 1000);
1310 SendLocationCallbacks();
1317 _LocationManager::SatelliteInfoUpdated(unsigned int azimuth, unsigned int elevation, unsigned int prn, int snr, bool is_active, void* user_data)
1320 if (user_data == null)
1322 SysLog(NID_LOC, "User data Null. Return.");
1326 String* pSatInfo = static_cast< String* >(user_data);
1327 SysLog(NID_LOC, "GPS Position Satellite Info: SatID (%d), Used (%d), Elevation (%d), Azimuth (%d), Snr (%d).", prn, is_active, elevation, azimuth, snr);
1329 pSatInfo->Append(static_cast< int >(prn));
1330 pSatInfo->Append(", ");
1331 pSatInfo->Append(static_cast< int >(elevation));
1332 pSatInfo->Append(", ");
1333 pSatInfo->Append(static_cast< int >(azimuth));
1334 pSatInfo->Append(", ");
1335 pSatInfo->Append(snr);
1336 pSatInfo->Append(", ");
1337 pSatInfo->Append(is_active);
1338 pSatInfo->Append("; ");
1344 _LocationManager::GpsServiceUpdateCallback(location_service_state_e state, void* user_data)
1346 SysLog(NID_LOC, "Updated State is (%d).", state);
1347 if (user_data == null)
1349 SysLog(NID_LOC, "User data is null. Return");
1353 _LocationManager* pThis = static_cast< _LocationManager* >(user_data);
1354 pThis->__gpsHandler.serviceState = state;
1358 _LocationManager::WpsServiceUpdateCallback(location_service_state_e state, void* user_data)
1360 SysLog(NID_LOC, "Updated State is (%d).", state);
1361 if (user_data == null)
1363 SysLog(NID_LOC, "User data is null. Return");
1367 _LocationManager* pThis = static_cast< _LocationManager* >(user_data);
1368 pThis->__wpsHandler.serviceState = state;
1372 _LocationManager::InitLocationManager(void)
1374 SysLog(NID_LOC, "Creating the first location manager instance.");
1376 unique_ptr< _LocationManager > pLocMgr(new (std::nothrow) _LocationManager());
1377 SysTryReturnVoidResult(NID_LOC, pLocMgr, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1378 result r = pLocMgr->Construct();
1379 SysTryReturnVoidResult(NID_LOC, r == E_SUCCESS, r, "[%s] Falied to construct the Location Manager. Propagating.", GetErrorMessage(r));
1380 r = pLocMgr->Start();
1381 SysTryReturnVoidResult(NID_LOC, r == E_SUCCESS, r, "[%s] Falied to start the Location Manager. Propagating.", GetErrorMessage(r));
1383 __pUniqueInstance = pLocMgr.release();
1384 std::atexit(DestroyLocationManager);
1388 _LocationManager::DestroyLocationManager(void)
1390 delete __pUniqueInstance;
1394 _LocationManager::GetAppAccessibility(void)
1397 static bool appRegistered = false;
1400 static const char COMMAND_REGISTER_APP[] = "ADD_APPLIST";
1401 if (location_manager_send_command(COMMAND_REGISTER_APP) == LOCATIONS_ERROR_NONE)
1404 static const char COMMAND_ENABLE_APP[] = "ACCESSIBILITY:1";
1405 res = location_manager_send_command(COMMAND_ENABLE_APP);
1406 SysTryReturn(NID_LOC, res == 0, false, E_SYSTEM,
1407 "[%s] Failed to register the application.", GetErrorMessage(E_SYSTEM));
1409 appRegistered = true;
1413 location_accessibility_state_e accessState;
1414 res = location_manager_get_accessibility_state(&accessState);
1415 SysTryReturn(NID_LOC, res == 0, false, E_SYSTEM,
1416 "[%s] Failed to get the application location setting.", GetErrorMessage(E_SYSTEM));
1418 return (accessState == LOCATIONS_ACCESS_STATE_ALLOWED) ? true : false;