From 9dbd90e65b23d4b5fda883d03d24bedc5127229b Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Thu, 3 Jan 2019 10:46:38 +0100 Subject: winrt: Check access to location service on every call The access to location services can change while the application is running so checking the access only once does not work. If access is not granted set the proper error code. Change-Id: I00d82dd166d049363de1dc9b0ca7015bc494baf5 Reviewed-by: Alex Blasche --- .../winrt/qgeopositioninfosource_winrt.cpp | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/plugins/position/winrt/qgeopositioninfosource_winrt.cpp b/src/plugins/position/winrt/qgeopositioninfosource_winrt.cpp index c4c2c1ca..ac841be7 100644 --- a/src/plugins/position/winrt/qgeopositioninfosource_winrt.cpp +++ b/src/plugins/position/winrt/qgeopositioninfosource_winrt.cpp @@ -314,8 +314,10 @@ bool QGeoPositionInfoSourceWinRT::startHandler() return false; } - if (!requestAccess()) + if (!requestAccess()) { + setError(QGeoPositionInfoSource::AccessError); return false; + } HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([this, d]() { HRESULT hr; @@ -436,6 +438,13 @@ QGeoPositionInfoSource::Error QGeoPositionInfoSourceWinRT::error() const { Q_D(const QGeoPositionInfoSourceWinRT); qCDebug(lcPositioningWinRT) << __FUNCTION__ << d->positionError; + + // If the last encountered error was "Access denied", it is possible that the location service + // has been enabled by now so that we are clear again. + if ((d->positionError == QGeoPositionInfoSource::AccessError + || d->positionError == QGeoPositionInfoSource::UnknownSourceError) && requestAccess()) + return QGeoPositionInfoSource::NoError; + return d->positionError; } @@ -595,21 +604,18 @@ HRESULT QGeoPositionInfoSourceWinRT::onStatusChanged(IGeolocator *, IStatusChang bool QGeoPositionInfoSourceWinRT::requestAccess() const { qCDebug(lcPositioningWinRT) << __FUNCTION__; - static GeolocationAccessStatus accessStatus = GeolocationAccessStatus_Unspecified; + GeolocationAccessStatus accessStatus; static ComPtr statics; - if (accessStatus == GeolocationAccessStatus_Allowed) - return true; - else if (accessStatus == GeolocationAccessStatus_Denied) - return false; - ComPtr> op; HRESULT hr; hr = QEventDispatcherWinRT::runOnXamlThread([&op]() { HRESULT hr; - hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Geolocation_Geolocator).Get(), - IID_PPV_ARGS(&statics)); - RETURN_HR_IF_FAILED("Could not access Geolocation Statics."); + if (!statics) { + hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Geolocation_Geolocator).Get(), + IID_PPV_ARGS(&statics)); + RETURN_HR_IF_FAILED("Could not access Geolocation Statics."); + } hr = statics->RequestAccessAsync(&op); return hr; -- cgit v1.2.3