Android Permissions Made Easy
Tags: android, kotlin, mobile, programming
Published: 2022-12-12 23:45:09

> 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`

```Kotlin
private val permissionsManager = PermissionManager(this)
```
-   Using the provided DSL, build an appropriate dispatcher
-   where you want to check or grant the permissions, call the manager’s  `checkRequestAndDispatch`  method

```Kotlin
permissionManager.checkRequestAndDispatch(BLUETOOTH_REQUEST_CODE)
```
-   override the Activity’s  `onRequestPermissionsResult`  and add to it the line

```Kotlin
permissionManager.dispatchOnRequestPermissionsResult(requestCode, grantResults)
```

-   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:

```Kotlin
    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 ...  
            }  
        }  
    }
```

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](https://jitpack.io/#lorenzofelletti/permissions), and you can easily add it to your project by adding to your  `settings.gradle`:

```Groovy
    dependencyResolutionManagement {  
        ...  
        repositories {  
            ...  
            maven { url 'https://jitpack.io' }  
        }  
    }
```

And to your module level  `build.gradle`:

```Groovy
    dependencies {  
        ...  
        implementation 'com.github.lorenzofelletti:permissions:0.4.2'  
    } 
```
 

## 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:

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

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 <a href="https://unsplash.com/@markkoenig?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Mark König</a> on <a href="https://unsplash.com/s/photos/permission?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>