googlesamples / easypermissions

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

Crashing when showing rationale dialog #266

Open aoikonom opened 5 years ago

aoikonom commented 5 years ago

Basic Information

Device type: Nexus 5X on emulator OS version: - EasyPermissions version: 2.0.0

Describe the problem

I am using build tools 28.0.3 for a legacy application that uses Holo themes. When rationale dialog is to be shown (the second time I request permission after the first time it was denied) I get a crash "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity."

The problem lies with RationaleDialogFragmentCompat

Code and logs

  @AfterPermissionGranted(RC_LOCATION)
    private void setMyLocationEnabled() {
        String[] perms = {Manifest.permission.ACCESS_COARSE_LOCATION};
        if (EasyPermissions.hasPermissions(getActivity(), perms)) {
            try {
                if (mMap != null)
                    mMap.setMyLocationEnabled(true);
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        } else {
            EasyPermissions.requestPermissions(this, getString(R.string.permission_rationale_location), RC_LOCATION, perms);
        }
    }
samtstern commented 5 years ago

@aoikonom sorry about the really slow response here, I was out on vacation over the holidays.

So does your Activity implement android.support.v4.app.FragmentActivity but not AppCompatActivity ?

samtstern commented 5 years ago

I was able to reproduce this in a failing test, will send a PR with an update.

samtstern commented 5 years ago

Fixed and released in version 2.0.1

aoikonom commented 5 years ago

The bug seems to have been fixed for calls made from an activity, but not when EasyPermissions.requestPermissions is called from inside a Fragment. The problem seems to lie in the code below

`

public static PermissionHelper<? extends Activity> newInstance(Activity host) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return new LowApiPermissionsHelper<>(host);
    }

    if (host instanceof AppCompatActivity)
        return new AppCompatActivityPermissionsHelper((AppCompatActivity) host);
    else {
        return new ActivityPermissionHelper(host);
    }
}

@NonNull
public static PermissionHelper<Fragment> newInstance(Fragment host) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return new LowApiPermissionsHelper<>(host);
    }

    return new SupportFragmentPermissionHelper(host);
}

`

When called from an activity not derived from AppCompatActivity, ActivityPermissionHelper handles the situation but in the case of a Fragment inside this activity there is no distinction.

samtstern commented 5 years ago

@aoikonom thanks for pointing this out!

aoikonom commented 5 years ago

@aoikonom thanks for pointing this out!

Thanks for the quick response