Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling Permissions in a single class in android

Tags:

java

android

For handling permissions in android M+, I want to write a single class, namely PermissionHandler class, to handle all the permission-related work so that I can easily use the same class in any project without making changes to the calling activity by calling only the constructor:

           new PermissionHandler(CallingActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE, new PermissionHandler.PermissionGranted() {
                @Override
                public void onPermissionGranted() {
                    doWhatever();
                }
            });

My PermissionHandler is:

public class PermissionHandler implements ActivityCompat.OnRequestPermissionsResultCallback{
.
.
.
public PermissionHandler(AppCompatActivity callingActivity, String permission, PermissionGranted permissionGranted) {
        this.permission = permission;
        this.permissionGranted = permissionGranted;
        this.callingActivity= callingActivity;
        askForPermission();

}

private void askForPermission() {

            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
                    showAlertDialog();
                } else {
                    ActivityCompat.requestPermissions(callingActivity,permissionsArray, PERMISSION_REQUEST);
                }
            } else {
                permissionGranted.onPermissionGranted();
            }
}

    @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                permissionGranted.onPermissionGranted();
            } else {
                onPermissionIsNotGranted();
            }
            break;
        }
    }
}

My problem here is that onRequestPermissionsResult which is supposed to be invoked when ActivityCompat.requestPermissions(callingActivity,permissionsArray, PERMISSION_REQUEST) is called is never invoked. What I found out is that this is due to android calling callingActivity.onRequestPermissionsResult which does not exist in the callingActivity and is passed to PermissionHandler. I also considered using Reflection and Proxies to resolve this issue at runtime, but no success.

like image 985
Ali Nem Avatar asked May 17 '16 08:05

Ali Nem


People also ask

How does Android handle multiple permissions?

In case one or more permissions are not granted, ActivityCompat. requestPermissions() will request permissions and the control goes to onRequestPermissionsResult() callback method. You should check the value of shouldShowRequestPermissionRationale() flag in onRequestPermissionsResult() callback method.

What are two types of permissions in Android?

Android categorizes permissions into different types, including install-time permissions, runtime permissions, and special permissions.

How do I set permissions in Android programmatically?

You need to declare the permissions in the manifest file first before you request for them programmatically. refer this for more information. declaring the permission in the manifest file is static type declaration by which your os know what kind of permission your app might require.


1 Answers

As far as i searched i found that its a bug in android. You can see the live issue here. onRequestPermissionsResult() will not be called in any other class then the activity it is called by.

For more details refer this question : onRequestPermissionsResult not being called in dialog fragment. this user having the same problem as yours.

Creating a Common class for requesting permissions is a really good attempt but my friend, we need to find an alternative for handling onRequestPermissionsResult()

like image 110
Janki Gadhiya Avatar answered Sep 22 '22 22:09

Janki Gadhiya