Repository navigation
fix(android): allow enableCurrentLocation with only ACCESS_COARSE_LOCATION - #188
Conversation
| if (hasLocationPermission()) { | ||
| internalEnableCurrentLocation(call) | ||
| return | ||
| } | ||
|
|
||
| val coarseDeclared = | ||
| PermissionHelper.hasDefinedPermission( | ||
| context, | ||
| Manifest.permission.ACCESS_COARSE_LOCATION | ||
| ) | ||
| val fineDeclared = | ||
| PermissionHelper.hasDefinedPermission( | ||
| context, | ||
| Manifest.permission.ACCESS_FINE_LOCATION | ||
| ) | ||
|
|
||
| if (!coarseDeclared && !fineDeclared) { | ||
| call.reject( | ||
| "location permission was denied: neither ACCESS_COARSE_LOCATION nor ACCESS_FINE_LOCATION is declared in the AndroidManifest.xml" | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| // Only request ACCESS_FINE_LOCATION if the app declared it, otherwise request coarse only. | ||
| val alias = if (fineDeclared) LOCATION else COARSE_LOCATION | ||
| requestPermissionForAlias(alias, call, "enableCurrentLocationCallback") |
There was a problem hiding this comment.
Regarding the behavior change you mention, here's my opinion, but not that of a strong one:
If the FINE location is defined in manifest, we should request it, even if COARSE is already granted. This would make it similar to geolocation plugin. It requests the permissions defined in the manifest, but only requires COARSE granted to work.
Your comment
Android's guidance is that apps "should still work when the user grants only approximate location" and should ask for precise only if a feature "absolutely requires" it. The my-location dot does not.
While I understand it, that sounds like we're making a decision for the app developer.
Or if we want to allow configuring it, we could have an enableHighAccuracy, but I don't think it's needed.
Furthermore, having this behavior change should maybe be marked as a breaking change for the plugin (even if not a huge one), which is okay, but seems unnecessary to me.
It's just my opinion, if you feel strongly with the current approach, I'm not opposed to it.
There was a problem hiding this comment.
I don't have a strong opinion about this and what you said makes a lot of sense, even more when considering that's the approach we took for the Geolocation plugin.
I'll make that change now, thank you!
There was a problem hiding this comment.
@OS-pedrogustavobilro I updated it, can you check again?
…ATION The `location` permission alias only contained ACCESS_FINE_LOCATION, so `enableCurrentLocation` was rejected when an app declared only ACCESS_COARSE_LOCATION, and when the user chose approximate location on Android 12+, even though Google Maps only needs one of the two. - `location` alias now covers ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION, and a new `coarseLocation` alias covers ACCESS_COARSE_LOCATION (same approach as the Geolocation plugin) - the current location is enabled when coarse location is granted (precise location also grants coarse) - ACCESS_FINE_LOCATION is only requested when the app declares it - reject with a clear message if the app declares neither permission - README: ACCESS_FINE_LOCATION is now optional; document the Google Play policy for sensitive location permissions This lets apps that don't need precise location avoid declaring ACCESS_FINE_LOCATION (and the Play Console declaration it requires). Refs: RMET-5644
c264051 to
c1c583e
Compare
OS-pedrogustavobilro
left a comment
There was a problem hiding this comment.
Tested here, it's working well. Leaving a non-blocking comment.
There was a problem hiding this comment.
There's no breaking changes here, so this could technically go in main branch.
But because the main goal here is for Google Play policies that are only in effect for API level 37 (Capacitor 9), I don't mind this staying in next.
There was a problem hiding this comment.
main goal here is for Google Play policies that are only in effect for API level 37 (Capacitor 9)
Yeah that was my reasoning. I'll keep it in next and if anything if we for some reason want it in main (or Capacitor 8 once it's in 8.x) in the future we can always cherry pick
Jira: RMET-5644
Problem
The plugin's
locationpermission alias only containedACCESS_FINE_LOCATION, so on Android:ACCESS_COARSE_LOCATIONcould never useenableCurrentLocation(rejected withMissing the following permissions in AndroidManifest.xml: android.permission.ACCESS_FINE_LOCATION), andlocation permission was denied, even in apps that declare both permissions.Google Maps only needs one of the two permissions to show the current location. This matters because Google Play is making
ACCESS_FINE_LOCATIONa sensitive permission that requires a Play Console declaration, so apps that don't need precise location should be able to declare onlyACCESS_COARSE_LOCATION.Changes
locationalias now coversACCESS_COARSE_LOCATION+ACCESS_FINE_LOCATION, and a newcoarseLocationalias coversACCESS_COARSE_LOCATION(same approach as@capacitor/geolocation).enableCurrentLocationrequests the location permissions the app declares in its manifest, and only needs coarse location to be granted to work (precise location also grants coarse):ACCESS_FINE_LOCATIONdeclared: thelocationalias (coarse + fine) is requested whenever it is not fully granted, even if coarse is already granted. On Android 12+ this shows the "approximate to precise" upgrade prompt, like@capacitor/geolocation. If the user keeps approximate, the call still succeeds with approximate accuracy.ACCESS_COARSE_LOCATIONdeclared: only coarse is requested.enableCurrentLocationrejects immediately with a clear message.ACCESS_FINE_LOCATIONis now optional,ACCESS_COARSE_LOCATIONis enough to show the current location, the request behaviour above is described, and the Google Play policy for sensitive location permissions is documented.The plugin's own
AndroidManifest.xmlis unchanged: apps keep declaring the location permissions they need.Behaviour change
For apps that declare
ACCESS_FINE_LOCATION, users who pick Approximate no longer getlocation permission was denied: the call now succeeds with approximate accuracy (previously the call only worked after the user upgraded to precise on a later call). Users who pick Precise are unaffected.Testing
Manual tests with the example app (Config Maps > Show current location), all on clean installs. No Maps API key was set, so map tiles were blank; the my-location button and results were checked.
next)Also:
./gradlew clean build test(includes Android lint),eslint,prettier --check, SwiftLint (pre-commit) all pass.Notes