An Improved Permission Manager for Android

Permission Manager

The purpose of a permission is to protect the privacy of an Android user. Android apps must request permission to access sensitive user data (such as contacts and SMS), as well as certain system features (such as camera and internet). A central design point of the Android security architecture is that no app, by default, has permission to perform any operations that would adversely impact other apps, the operating system, or the user. If an app needs to use resources or information outside of its own sandbox, the app has to request the appropriate permission.

Ever since its introduction in Android Marshmallow, developers had to check and manage permission before enabling certain features or modules of the application. These permissions are unpredictable as the user can grant or deny permissions at any time. They can also prevent an application from requesting permissions permanently.

In this article I’m going to introduce you to an open sourced Android PermissionManager library that can request, manage and handle user permission choices appropriately. Using libraries like these can save a lot of your development time.

Overview

Android Simple Permission Manager

Simple Permission Manager is an open-sourced lightweight android library to request and manage permissions at runtime on android versions M and above. The library has been created with one thing in mind, Make Permission Management Easy for Developers. Use can leave Android Runtime Permission to SimplePermissionManager and focus on creating your application. Some of the features and advantages of using SimplePermissionManager as opposed to writing these boilerplate code yourself are

  • Requests a Single permission.
  • Request Group of permissions together.
  • If permissions are denied, a Custom Alert Dialog will be shown explaining why the permission is needed, before requesting a second time.
  • If permissions are permanently denied, automatically open Application Info Page in settings to manually grant requests.
  • Developer need not keep track of permission statuses, SimplePermissionManager will do it for you.
  • No need to override onRequestPermissionsResult() method and check permission status. SimplePermissionManager will provide you with a PermissionResponse that denotes whether a Permission has been granted, denied or permanently denied.
  • Irrespective of the permission status, you can make permission requests and SimplePermissionManager will take care of requesting permission, showing alert dialog, and opening settings.
  • Simple and easy Builder to create PermissionManager

Screenshots

Integrate In Your Application

Integrating the SimplePermissionManager library into you app is extremely easy. A few changes in the build gradle and your all ready to use Simple Preference Manager. Make the following changes.

  • Add the JitPack repository to your build file. Add it in your project / root build.gradle at the end of repositories
allprojects {
  repositories {
    ...
    maven { url "https://jitpack.io" }
  }
}
  • Add the Simple PermissionManager dependency to your module / app build.gradle in the dependencies section
dependencies {
        implementation 'com.github.a-anand-91119:SimplePermissionManager:<latest-version>'
}

Creating a new PermissionManager

  • Build an instance of PermissionManager
    • Provide the activity context using withContext()
    • Add a PermissionResponseListener using addPermissionResponseListener()
    • Optional – pass a view that can be used by Snackbar to find parent. Providing this will show a snackbar with option to open application info page in case of permanently denied permissions
// Create a PermissionManager with all the features
// This PermissionManager will display a toast and open settings page for permanently denied permissions
PermissionManager permissionManager = PermissionManagerBuilder.withContext(this)
                .addPermissionResponseListener(new PermissionResponseListener() {
                    @Override
                    public void singlePermissionResponse(PermissionResponse permissionResponse) {
                    	// Single permission requests responses
                    }

                    @Override
                    public void multiplePermissionResponse(List<PermissionResponse> permissionResponses) {
                    	// Group permission requests responses
                    }
                }).build();
// For showing a SnackBar instead of Toast and an action to open settings, 
// provide the view that the snackbar can use to find parent
PermissionMananger permissionManager = PermissionManagerBuilder.withContext(this)
                .addPermissionResponseListener(new PermissionResponseListener() {
                    @Override
                    public void singlePermissionResponse(PermissionResponse permissionResponse) {
                        // Single permission requests responses
                    }

                    @Override
                    public void multiplePermissionResponse(List<PermissionResponse> permissionResponses) {
                        // Group permission requests responses
                    }

                }).enableSnackbarForSettings(findViewById(R.id.layout_container))
                .build();
  • Check whether permissions has been granted
// To check whether a permission has been granted or not use hasPermission()
boolean status = permissionManager.hasPermission(Manifest.permission.CALL_PHONE);


// To check the status of multiple permissions, pass the permissions as argument to hasPermissions()
List<PermissionResponse> permissionResponses = permissionManager.hasPermissions(Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.READ_CONTACTS);

Requesting a Single Permission

  • Request for permission using requestPermission() method. PermissionManager displays a Custom Alert Dialog before requesting for permissions that have been denied previously. The message can be customized using the overloaded method.
// To request for a permission use the requestPermission()
// The user response will be available in singlePermissionResponse() callback of PermissionResponseListener
// A default message will be displayed before requesting previously denied permissions
permissionManager.requestPermission(Manifest.permission.CAMERA);
// You can use use the overloaded message to display a custom message in alert dialog
permissionManager.requestPermission(Manifest.permission.CAMERA, "Reason Why My App Needs This Permission");

Requesting Group of Permissions

  • Request for multiple permissions together using the requestPermissions() method. Like Single Permissions, if at least one of the permissions in this group has been denied previously, then the Custom Alert will be displayed whose message can be customized using the overloaded method
// To request a group of permissions, use the method requestPermissions()
permissionManager.requestPermissions(Manifest.permission.CAMERA, 
                Manifest.permission.RECORD_AUDIO, 
                Manifest.permission.READ_CONTACTS);
// To display custom message use requestPermissions() method accepting a message and String[] of permissions
permissionManager.requestPermissions("Reason Why Group Of Permissions Are Needed For My App",
                new String[]{Manifest.permission.CAMERA, 
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.READ_CONTACTS});

Understanding Permission Manager Responses

All the requested permission statuses will be returned as a PermissionResponse Object using the attached PermissionResponseListener. The permission response listener has two methods one to receive the responses of Single Permission Requests and the other for Group Permission Requests.

  • public void singlePermissionResponse(PermissionResponse permissionResponse)
  • public void multiplePermissionResponse(List permissionResponses)

The PermissionResponse Object contains two attributes

  • String permission
  • int permissionStatus

The possible values for permission status are what they mean are

  • PermissionManager.PERMISSION_GRANTED (0) : User has granted the requested permission
  • PermissionManager.PERMISSION_DENIED (1) : User has denied the requested permission
  • PermissionManager.PERMISSION_PERMANENTLY_DENIED (-3) : User has denied the permission by choosing “Don’t Ask Again”.

Contribute

You can also contribute to this library to add new features or fix any bugs that you find. Create a pull request with the changes and it will be merged after reviewing. You can check out some of my other open source libraries

If you have any doubt’s or questions, please feel free to ask in the comment section and I’ll be happy to help.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top