DEV Community

Cover image for Android Permissions Made Easy
Lorenzo Felletti
Lorenzo Felletti

Posted on • Updated on • Originally published at Medium

Android Permissions Made Easy

One library to rule them all

Handling the permission requests in Android is not as straightforward as I thought it should be, especially for beginners. Thus, I decided to write a small library to help in managing them.

The library is very simple to use:

  • choose a request code for each group of permissions you are going to ask altogether, and define an array containing those permissions (for example, if the application has a button enabling Bluetooth, and a view showing the user location on a map, you would create a BLUETOOTH_REQUEST_CODE = 1 and a LOCATION_REQUEST_CODE = 2, and define two arrays BLUETOOTH_PERMISSIONS = arrayOf(...), and LOCATION_PERMISSIONS = arrayOf(...))
  • create in the Activity that you want to request the permissions a PermissionManager
private val permissionsManager = PermissionManager(this)
Enter fullscreen mode Exit fullscreen mode
  • Using the provided DSL, build an appropriate dispatcher
  • where you want to check or grant the permissions, call the manager’s checkRequestAndDispatch method
permissionManager.checkRequestAndDispatch(BLUETOOTH_REQUEST_CODE)
Enter fullscreen mode Exit fullscreen mode
  • override the Activity’s onRequestPermissionsResult and add to it the line
permissionManager.dispatchOnRequestPermissionsResult(requestCode, grantResults)
Enter fullscreen mode Exit fullscreen mode
  • enjoy!

The Dispatcher’s DSL

The DSL to build the dispatcher has a few functions that helps to create the dispatcher in a human-readable way. The following is an example:

    permissionManager.buildRequestResultsDispatcher {  
        withRequestCode(BLUETOOTH_REQUEST_CODE) {  
            checkPermissions(BLUETOOTH_PERMISSIONS)  
            doOnGranted {  
                Log.d(TAG, "Bluetooth permission granted")  
                // something ...  
            }  
            doOnDenied {  
                Log.d(TAG, "Bluetooth permission denied")  
                // something else ...  
            }  
        }  
        withRequestCode(LOCATION_REQUEST_CODE) {  
            checkPermissions(LOCATION_PERMISSIONS)  
            doOnGranted {  
                Log.d(TAG, "Location permission granted")  
                // something ...  
            }  
            doOnDenied {  
                Log.d(TAG, "Location permission denied")  
                // something else ...  
            }  
        }  
    }
Enter fullscreen mode Exit fullscreen mode

Inside buildRequestResultsDispatcher, you can define as many as you want withRequestCode(requestCode) blocks, each containing the permissions to check (checkPermissions) and the behavior to dispatch in case all the permissions are granted (doOnGranted), or some were not (doOnDenied).

The checkRequestAndDispatch Method

This method does two things:

  • it checks if the permissions were already granted, and if so it dispatches the same action associated in the dispatcher to the doOnGranted for the given request code
  • otherwise, it asks for the permissions that are not granted; and it would be the overridden onRequestPermissionsResult to dispatch the appropriate action after the permission request.

How To Use The Library

The library is deployed on Jitpack, and you can easily add it to your project by adding to your settings.gradle:

    dependencyResolutionManagement {  
        ...  
        repositories {  
            ...  
            maven { url 'https://jitpack.io' }  
        }  
    }
Enter fullscreen mode Exit fullscreen mode

And to your module level build.gradle:

    dependencies {  
        ...  
        implementation 'com.github.lorenzofelletti:permissions:0.4.2'  
    } 
Enter fullscreen mode Exit fullscreen mode

The showRationaleDialog Function

It is recommended that you also manage the shouldShowRequestPermissionRationale, so since version 0.3.0 it is possible to do it. Two DSL functions are defined to do this:

  • rationale — suitable for the cases when you need the maximum configurability, but you should manage “manually” some things, such as calling permissionManager.checkRequestAndDispatch again if needed with the comingFromRationale flag set to true
  • showRationaleDialog — suitable in all the cases when what you want is to show a dialog with a message. In that case, everything is already set up to function properly, you don’t have to do anything but choosing the message.

An example of the latter is:

    permissionManager.buildRequestResultsDispatcher {  
        withRequestCode(LOCATION_REQUEST_CODE) {  
            checkPermissions(LOCATION_PERMISSIONS)  
            showRationaleDialog("Location permissions are needed for this feature.")  
            doOnGranted {  
                // something ...  
            }  
            doOnDenied {  
                // something else ...  
            }  
        }  
    }
Enter fullscreen mode Exit fullscreen mode

The library is still very young and new features may be added in the future, as well as the existing features may be modified.


Please let me know your opinion on this simple library and if you want any feature added to it.


Cover image by Mark König on Unsplash

Top comments (0)