dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

Location Permission is always Denied after denying it once #17361

Closed gktval closed 9 months ago

gktval commented 1 year ago

Description

Several months ago, while testing, I chose to deny permission to my app when asking for LocationWhenInUse. Since then, whenever I reinstall the app and need to ask for permissions again, this permission is always denied. The screen to ask for permissions never pops up. Instead, I must go into the settings for the app on the device and manually allow access to Location. I have only noticed this affecting Android.

I feel like after clearing the cache or reinstalling the app, that this permission should be reset.

Steps to Reproduce

  1. Request for LocationWhenInUse permission
  2. Deny the app permission
  3. Uninstall the app to clear all caches
  4. Reinstall the app
  5. Request for LocationWhenInUse permission
  6. Notice that the permission is still denied.

Relevant Code: BasePermission gpsPermission = new LocationWhenInUse(); var permissionStatus = await permission.CheckStatusAsync(); Console.WriteLine(permissionStatus);

Link to public reproduction project repository

NA

Version with bug

7.0.92

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

7.0.92

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No

Relevant log output

No response

drasticactions commented 1 year ago

I don't believe that's an MAUI issue: I believe that's what happens for Android applications in general and is not specific to MAUI. If you created an Android app in straight .NET or Java and denied the permissions, you would need to reset them through the settings app. Unless I'm mistaken, IMO, we shouldn't do something special for MAUI UI apps specifically in this regard.

@jonathanpeppers Is this correct?

jonathanpeppers commented 1 year ago

The code looks like it just calls Android to request every time:

https://github.com/dotnet/maui/blob/aad88d6f01a9e244df189a2031d6b1df7be4a019/src/Essentials/src/Permissions/Permissions.android.cs#L72-L91

@gktval if you want to validate Essential's behavior, you could try calling the Android APIs directly:

If you get the same behavior, calling directly (not using Essentials), then it's likely "just how Android works".

You might look into adb shell pm, as a another way to debug:

adb shell pm [grant|revoke] com.my.app android.permission.ACCESS_FINE_LOCATION

There are some examples in adb shell pm help output:

  grant [--user USER_ID] PACKAGE PERMISSION
  revoke [--user USER_ID] PACKAGE PERMISSION
    These commands either grant or revoke permissions to apps.  The permissions
    must be declared as used in the app's manifest, be runtime permissions
    (protection level dangerous), and the app targeting SDK greater than Lollipop MR1.

  set-permission-flags [--user USER_ID] PACKAGE PERMISSION [FLAGS..]
  clear-permission-flags [--user USER_ID] PACKAGE PERMISSION [FLAGS..]
    These commands either set or clear permission flags on apps.  The permissions
    must be declared as used in the app's manifest, be runtime permissions
    (protection level dangerous), and the app targeting SDK greater than Lollipop MR1.
    The flags must be one or more of [review-required, revoked-compat, revoke-when-requested, user-fixed, user-set]
gktval commented 1 year ago

Thanks. I tried calling adb shell pm clear-permission-flags PACKAGE_NAME PERMISSION_NAME user-set user-fixed as shown here: https://developer.android.com/about/versions/11/privacy/permissions

Coarse and Fine location do have the USER_SET flag. After calling the command above, it removes the USER_SET flag. But if I reinstall, the flag gets set again.

jonathanpeppers commented 1 year ago

I think it's just how Android works:

Permissions that have been denied once by the user are flagged by USER_SET. Permissions that have been denied permanently by selecting Deny twice are flagged by USER_FIXED.

During testing you might want to reset these flags to ensure that testers aren't surprised when the request dialog isn't shown. To do this, use the command:

adb shell pm clear-permission-flags PACKAGE_NAME PERMISSION_NAME user-set user-fixed

jfversluis commented 9 months ago

Default Android behavior, there is not much we can do here on our side unfortunately.