Skip to content

Commit

Permalink
Request location permissions only once needed
Browse files Browse the repository at this point in the history
(i.e. when project is loaded, not at the startup when position provider is created)
  • Loading branch information
wonder-sk committed Jun 10, 2024
1 parent 9be9459 commit c1b96cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 53 deletions.
5 changes: 0 additions & 5 deletions app/android/src/uk/co/lutraconsulting/MMAndroidPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -159,16 +157,13 @@ public boolean start() {
return false;
}

Log.e("CPP", "[java] here 1");

if (mUseFused) {
LocationRequest locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000).build();

mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.getMainLooper());
}
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()));
Expand Down
45 changes: 11 additions & 34 deletions app/position/providers/androidpositionprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<jboolean>( "start", "()Z" );

__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] start updates res: %d", res );
Expand Down
18 changes: 4 additions & 14 deletions app/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ) {
Expand Down

1 comment on commit c1b96cd

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 24.6.631911 just submitted!

Please sign in to comment.