Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling requestPermissions twice on custom kernel calls onRequestPermissionsResult before user response

I have a project in which I ask for 2 permissions on startup. In debugging, it works as intended until it makes the second call to requestPermissions. As soon as it does, the first one fires the onRequestPermissionsResult with an empty grantResults array.

This worked fine until just today. The only thing I can think of is the updates I have done today. I updated to Android SDK Platform Tools 23.1, and updated my Mac to El Capitan.

Can anyone help me fix whatever is up?

Code follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blank_layout);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int hasCameraPermissions = checkSelfPermission(android.Manifest.permission.CAMERA);
        if (hasCameraPermissions != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{android.Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_CODE);
        }
        int hasStoragePermissions = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (hasStoragePermissions != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION_CODE);
        }
        if (hasCameraPermissions == PackageManager.PERMISSION_GRANTED && hasStoragePermissions == PackageManager.PERMISSION_GRANTED) {
            startCameraIntent();
        }
    } else {
        startCameraIntent();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CAMERA_PERMISSION_CODE:
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, R.string.camera_denied, Toast.LENGTH_LONG).show();
                quitAfter5();
            }
            break;
        case REQUEST_STORAGE_PERMISSION_CODE:
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, R.string.storage_denied, Toast.LENGTH_LONG).show();
                quitAfter5();
            }
            break;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int hasCameraPermissions = checkSelfPermission(android.Manifest.permission.CAMERA);
        int hasStoragePermissions = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (hasCameraPermissions == PackageManager.PERMISSION_GRANTED && hasStoragePermissions == PackageManager.PERMISSION_GRANTED) {
            startCameraIntent();
        }
    }
}

EDIT:: The change that caused this to crash was the fact that I had installed the custom kernel ElementalX on my Nexus 5 a few days earlier. I confirmed the issue just yesterday on my brother's Nexus 6 using the same kernel. I am going to shoot the kernel developer an email to see what he says about it.

As in the answer though, the code above really is the incorrect way to use the requestPermissions method anyway. So everyone just do it the right way and it will work for all devices :)

like image 846
RayfenWindspear Avatar asked Dec 17 '15 00:12

RayfenWindspear


1 Answers

The best-case scenario that I would expect from two consecutive calls to requestPermissions() is for two consecutive-yet-separate dialogs to appear. I would not expect the behavior that you are seeing. I have done similar multiple-requestPermissions() calls, in the form of not handling configuration changes properly, and both editions of the dialog appeared.

All else being equal, you're better off just requesting all the permissions in requestPermissions(), so there is a single dialog. That will better fit user expectations, and I think that it would simplify your code a pinch.

like image 138
CommonsWare Avatar answered Sep 30 '22 17:09

CommonsWare