Wednesday, 15 December 2021

How Can We Determine If We Can Request Location Permission Upgrade in Android 12?

In Android 12, if an app requests fine location permission via ACCESS_FINE_LOCATION, the user might elect to only grant coarse location permission. Unfortunately, not all APIs work with coarse location permission — in particular, AFAICT, to look up the SSID of the connected WiFi network, we need fine location permission, not coarse location permission.

If you re-request fine location permission, the user will be asked to upgrade your access, but the user might decline. As a result, we need a way to gracefully determine that the user granted us coarse location permission and that we are still eligible to request the upgrade.

This sample project just has a button to trigger a requestPermissions() for ACCESS_FINE_LOCATION, along with four labels to report:

  • Were we granted fine location permission? (checkSelfPermission() for ACCESS_FINE_LOCATION)
  • Were we granted coarse location permission? (checkSelfPermission() for ACCESS_COARSE_LOCATION)
  • Should we prompt the user with rationale for our request, for fine permission? (shouldShowRequestPermissionRationale() for ACCESS_FINE_LOCATION)
  • Should we prompt the user with rationale for our request, for coarse permission? (shouldShowRequestPermissionRationale() for ACCESS_COARSE_LOCATION)

Running this after a fresh install on an Pixel 3a XL running the final beta of Android 12, I get the following results after the listed sequence of actions:

Action Has Fine Permission? Has Coarse Permission? Prompt for Fine? Prompt for Coarse?
launch the app no no no no
click button and grant approximate location while using the app no yes no no
click button again and choose "Keep approximate location" no yes yes no
click button a 3rd time and choose "Keep approximate location" no yes no no

While these all make some sense individually, the problem is that the second and fourth results are the same:

  • I have coarse location access
  • I am not told that I need to show rationale

The problem is that if the user tries clicking the button a 4th time, we're into the "don't ask again" state, and no permission dialog appears. All the previous times, a permission dialog appears, either to initially grant the permission or to grant the upgrade. I would like to be able to detect the 4th or later attempt, so I can show my own dialog that says "sorry, you need to go to Settings and grant the permission manually now". However, other than by tracking the count of requests myself, I do not know how to identify this case distinctly.

So, my question is: is there a way, using some existing permission or location APIs, to determine that I am no longer allowed to re-request ACCESS_FINE_LOCATION, and that further requests will result in immediate failures? In particular, the answer needs to be able to distinguish between the second and fourth rows in that table, so between "the user only granted coarse access" and "the user only granted coarse access, and you tried the upgrade twice, which the user rejected each time".



from How Can We Determine If We Can Request Location Permission Upgrade in Android 12?

No comments:

Post a Comment