diff --git a/app/android/src/uk/co/lutraconsulting/MMAndroidPosition.java b/app/android/src/uk/co/lutraconsulting/MMAndroidPosition.java index 16049eaa5..f2731f7aa 100644 --- a/app/android/src/uk/co/lutraconsulting/MMAndroidPosition.java +++ b/app/android/src/uk/co/lutraconsulting/MMAndroidPosition.java @@ -138,8 +138,6 @@ public boolean start() { if (mIsStarted) return false; - Log.e("CPP", "[java] here 0"); - if (mUseFused && !mFusedAvailable) { mErrorMessage = "FUSED_NOT_AVAILABLE"; Log.e("CPP", "[java] FUSED_NOT_AVAILABLE"); @@ -159,8 +157,6 @@ public boolean start() { return false; } - Log.e("CPP", "[java] here 1"); - if (mUseFused) { LocationRequest locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000).build(); @@ -168,7 +164,6 @@ public boolean start() { } else { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 0.F, mLocationManagerCallback, Looper.getMainLooper()); - Log.e("CPP", "[java] here 2"); } mLocationManager.registerGnssStatusCallback(mGnssStatusCallback, new Handler(Looper.getMainLooper())); diff --git a/app/position/providers/androidpositionprovider.cpp b/app/position/providers/androidpositionprovider.cpp index 4bd52d362..277776d14 100644 --- a/app/position/providers/androidpositionprovider.cpp +++ b/app/position/providers/androidpositionprovider.cpp @@ -169,40 +169,6 @@ AndroidPositionProvider::AndroidPositionProvider( bool fused, QObject *parent ) mAndroidPos = QJniObject::callStaticObjectMethod( "uk/co/lutraconsulting/MMAndroidPosition", "createWithJniCallback", "(Landroid/content/Context;ZI)Luk/co/lutraconsulting/MMAndroidPosition;", context, mFused, mInstanceId ); - // Request permissions if needed - - QLocationPermission perm; - perm.setAccuracy( QLocationPermission::Precise ); - if ( qApp->checkPermission( perm ) != Qt::PermissionStatus::Granted ) - { - // if user previously completely denied location, the permissions request dialog - // may not even show up and we get denied response again. - __android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] going to request permissions" ); - qApp->requestPermission( perm, [this]( const QPermission & p ) - { - if ( p.status() == Qt::PermissionStatus::Granted ) - { - __android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] permissions granted!" ); - this->startUpdates(); - } - else - { - __android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] permissions denied :-(" ); - // User may have granted permission just for approximate location, - // so it would be good to detect that and warn about that: approximate - // location has intentionally only ~2km accuracy - too low for any data collection - QLocationPermission permApprox; - permApprox.setAccuracy( QLocationPermission::Approximate ); - if ( qApp->checkPermission( permApprox ) == Qt::PermissionStatus::Granted ) - this->setState( tr( "Approximate location only!" ), State::NoConnection ); - else - this->setState( tr( "No location permissions" ), State::NoConnection ); - } - } ); - return; - } - - // TODO: this should not be needed? AndroidPositionProvider::startUpdates(); } @@ -238,6 +204,17 @@ void AndroidPositionProvider::startUpdates() { __android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] start updates" ); + // permissions are currently being requested in main.qml, so here + // we only check that we have the permissions we need. + QLocationPermission perm; + perm.setAccuracy( QLocationPermission::Precise ); + if ( qApp->checkPermission( perm ) != Qt::PermissionStatus::Granted ) + { + __android_log_print( ANDROID_LOG_ERROR, "CPP", "[c++] no location permissions - not starting!" ); + setState( tr( "No location permissions" ), State::NoConnection ); + return; + } + jboolean res = mAndroidPos.callMethod( "start", "()Z" ); __android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] start updates res: %d", res ); diff --git a/app/qml/main.qml b/app/qml/main.qml index 8b794b4fb..822e7bd41 100644 --- a/app/qml/main.qml +++ b/app/qml/main.qml @@ -244,20 +244,6 @@ ApplicationWindow { LocationPermission { id: locationPermission accuracy: LocationPermission.Precise - - function requestPermissionAsync() { - if ( locationPermission.status === Qt.Granted ) { - return true; - } - else if ( locationPermission.status === Qt.Undetermined ) { - locationPermission.request() - } - else if ( locationPermission.status === Qt.Denied ) { - __inputUtils.log("Permissions", "Location permission is denied") - __notificationModel.addInfo( qsTr( "Location permission is required to show your location on map. Please enable it in system settings." ) ); - } - return false; - } } MMToolbar { @@ -950,6 +936,10 @@ ApplicationWindow { // check location permission if ( locationPermission.status === Qt.Undetermined ) { + // This is the place where we actually request permissions. + // When the system's request permissions dialog get closed, + // we get a notification that our application is active again, + // and PositionKit::appStateChanged() will try to start updates. locationPermission.request(); } else if ( locationPermission.status === Qt.Denied ) {