RuntimePermissionsExtended

DEPRECATED

RuntimePermissionsExtended

Android Weekly

This project provides Kotlin extension functions that make permission handling easier and more concise. These extensions provide same implementation for permission handling in both Activities and Fragments.

Usage

All system permissions are annotated in AppPermission class.

AppPermission class preview:

sealed class AppPermission(val permissionName: String,
                         val requestCode: Int,
                         val deniedMessageId: Int,
                         val explanationMessageId: Int) {
    
    object CAMERA:  AppPermission(permissionName = Manifest.permission.CAMERA, 
    requestCode = 1, 
    deniedMessageId = R.string.permission_camera_denied, 
    explanationMessageId = R.string.permission_camera_explanation)
    /** 
    * other permissions
    **/
}

AppPermission class properties:

Note: If you want to use certain permission, you can do that by annotating AppPermision.{PERMISSION NAME}

Step 0: Prepare AndroidManifest

Add the following line to AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />

Step 1: Request permission by calling handlePermission function

In Activity or Fragment where you want to perform operation that needs system permission use handlePermission function as in example bellow:

Example - requesting camera permission:

fun captureCameraImage(){
        handlePermission(AppPermission.CAMERA_PERMISSION,
            onGranted = {
                 /** Permission is granted and we can use camera*inte */
                 openCameraPreviewScreen()
            },
            onDenied = {
                /** Permission is not granted - we should request permission **/      
                requestPermission(it)
            },
            onExplanationNeeded = {
                /** Additional explanation for permission usage needed **/
                snackbarWithAction(it.explanationMessageId) {
                    requestPermission(it)
                }
            })
}

handlePermission function signature:

fun handlePermission(permission: AppPermission, 
                     onGranted: (AppPermission) -> Unit, 
                     onDenied: (AppPermission) -> Unit,
                     onExplanationNeeded: (AppPermission) -> Unit)

handlePermission function parameters:

Note: In every permission request callback block you can access instance of requested AppPermission via Kotlin keyword ‘‘it’’ (implicit name of a single parameter).

Step 2: Handle onRequestPermissionResult callback

In Activity or Fragment where you need CAMERA permission you should override onRequestPermissionsResult function and delegate its parameters to onRequestPermissionsResultReceived extension functions as in example bellow:

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResultReceived(requestCode, permissions, grantResults,
                onPermissionGranted = {
                    /** start camera preview **/
                     openCameraPreviewScreen()
                },
                onPermissionDenied = {
                    /** show message that permission is denied**/
                    snackbarWithoutAction(it.deniedMessageId)
                }
        )
    }

onRequestPermissionsResultReceived function signature:

fun onRequestPermissionsResultReceived(requestCode: Int, permissions: Array<out String>,
                                       grantResults: IntArray,
                                       onPermissionGranted: (AppPermission) -> Unit, 
                                       onPermissionDenied: (AppPermission) -> Unit)

onRequestPermissionsResultReceived function parameters:

Important Note: Currently, these permission extensions are only able to handle one permission request at the time. Requesting two or more permissions in single permission request is yet to be implemented.

Licence

MIT License

Copyright (c) 2017 Nebojsa Vuksic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.