KPermissions

Additional

Language
Kotlin
Version
1.0.0 (Jan 13, 2018)
Created
Jan 6, 2018
Updated
Jan 23, 2018
Owner
Giorgio Antonioli (Fondesa)
Contributor
Giorgio Antonioli (Fondesa)
1
Activity
Badge
Generate
Download
Source code
APK file

Commercial

KPermissions

An Android library totally written in Kotlin that helps to request runtime permissions. This library is compatible also below Android M where runtime permissions doesn't exist, so you haven't to handle them separately.

Usage

To discover all the APIs of this library, check the wiki. It contains some useful notes and advanced features not explained in the README.

Basic usage

You can create a PermissionRequest either from an Activity or a Fragment using the extension method permissionsBuilder():

// Creates the request with the permissions you would like to request.
val request = permissionsBuilder(Manifest.permission.CAMERA, Manifest.permission.SEND_SMS).build()
// Send the request when you want.
request.send() 

To be notified about permissions' events, you can attach some listeners in one of the following ways (you can also combine them in the way you prefer most).

1. DSL

request.listeners {
    
    onAccepted { permissions ->
        // Notified when the permissions are accepted.
    }

    onDenied { permissions ->
        // Notified when the permissions are denied.
    }
    
    onPermanentlyDenied { permissions ->
        // Notified when the permissions are permanently denied.
    }
    
    onShouldShowRationale { permissions, nonce ->
        // Notified when the permissions should show a rationale.
        // The nonce can be used to request the permissions again.
    }
}

2. Builder's extensions

request.onAccepted { permissions ->
    // Notified when the permissions are accepted.
}.onDenied { permissions ->
    // Notified when the permissions are denied.
}.onPermanentlyDenied { permissions ->
    // Notified when the permissions are permanently denied.
}.onShouldShowRationale { permissions, nonce ->
    // Notified when the permissions should show a rationale.
    // The nonce can be used to request the permissions again.
}

3. Normal listeners

// It must implement [PermissionListener.AcceptedListener].
request.acceptedListener(this)
// It must implement [PermissionListener.DeniedListener].
request.deniedListener(this)
// It must implement [PermissionListener.PermanentlyDeniedListener].
request.permanentlyDeniedListener(this)
// It must implement [PermissionListener.RationaleListener].
request.rationaleListener(this)

Compatibility

Android SDK: KPermissions requires a minimum API level of 14 (the same of the latest support libraries).

Integration

You can download a jar from GitHub's releases page or grab it from jcenter() or mavenCentral().

Gradle

dependencies {
    compile 'com.github.fondesa:kpermissions:1.0.0'
}

Maven

<dependency>
  <groupId>com.github.fondesa</groupId>
  <artifactId>kpermissions</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

Contributing

Feel free to contribute to this project following the contributing guidelines.