googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.86k stars 1.46k forks source link

Getting location permissions, any of them #339

Open TWiStErRob opened 2 years ago

TWiStErRob commented 2 years ago

Basic Information

Device type: emulator OS version: 30 EasyPermissions version: 3.0.0

Describe the problem

I have an app that needs location, doesn't matter which permission. Since we have 2 of these, it's quite tricky, and didn't even start considering the background one.... The app works with coarse or fine, but has a better experience with fine.

If I pass this to requestPermissions with a rationale

private static final String[] LOCATION_PERMISSIONS = {
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION
};

I get 2 permission dialogs (fine/coarse selector, and upgrade from coarse to fine) and the rationale dialog, if the user selects the coarse location to begin with. I think this is because EasyPermissions thinks that not all permissions were granted. Is there a way to solve this? I guess I need or instead of and relationship between items in the list?

ahaapaka commented 2 years ago

You can implement EasyPermissions.PermissionCallbacks and then accept either location permission in onPermissionGranted() callback.

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (perms.contains(Manifest.permission.ACCESS_COARSE_LOCATION) ||
                perms.contains(Manifest.permission.ACCESS_FINE_LOCATION)) {
            // App can access device's location
        }
    }