/ Tool

PowerPermission makes handling runtime permissions extremely easy

PowerPermission makes handling runtime permissions extremely easy

PowerPermission

PowerPermission is a library to simplify process of demanding RuntimePermission.

You find an example application in this Repo or downloading directly here.
Here are some different points compare with other libraries:

  • support asking permissions in AppCompatActivity and Fragment(include ChildFragment)
  • support custom rational view after user refuse permission
  • support ability to choose permissions to display rational views
  • support different interface (RxJava2,RxJava3,Coroutines,LiveData)

example

How to Download

Basic

implementation "com.qifan.powerpermission:powerpermission:1.0.0"

Other API Support

implementation "com.qifan.powerpermission:powerpermission-rxjava2:1.0.0"
implementation "com.qifan.powerpermission:powerpermission-rxjava3:1.0.0"
implementation "com.qifan.powerpermission:powerpermission-coroutines:1.0.0"
implementation "com.qifan.powerpermission:powerpermission-livedata:1.0.0"

OverView

Package Name Role Usage
powerpermission Basic core package implementation "com.qifan.powerpermission:powerpermission:1.0.0"
powerpermission-rxjava2 Support RxJava2 implementation "com.qifan.powerpermission:powerpermission-rxjava2:1.0.0"
powerpermission-rxjava3 Support RxJava3 implementation "com.qifan.powerpermission:powerpermission-rxjava3:1.0.0"
powerpermission-coroutines Support Kotlin Coroutine implementation "com.qifan.powerpermission:powerpermission-coroutines:1.0.0"
powerpermission-livedata Support Android LiveData implementation "com.qifan.powerpermission:powerpermission-livedata:1.0.0"

How to use

Simple Usage

Singleton

  PowerPermission.init()
            .requestPermissions(
                context = [email protected],
                permissions = *arrayOf(
                    Manifest.permission.CAMERA
                )
            ) { permissionResult ->
                when {
                    permissionResult.hasAllGranted() -> {
                        doPermissionAllGrantedWork(permissionResult.granted())
                    }
                    permissionResult.hasRational() -> {
                        doPermissionReasonWork(permissionResult.rational())
                    }
                    permissionResult.hasPermanentDenied() -> {
                        doPermissionPermanentWork(permissionResult.permanentDenied())
                    }
                }
            }

Extension

  • Require in Activity
 askPermissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CALENDAR
        ) { permissionResult ->
            //do whatever you want
        }
  • Require in Fragment
 askPermissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CALENDAR
        ) { permissionResult ->
            //do whatever you want
        }
  • Require in ChildFragment
 askPermissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CALENDAR
        ) { permissionResult ->
            //do whatever you want
        }

RxJava/RxKotlin

Basic Usage

 simpleRequestButton.setOnClickListener {
            askPermissionsRx(Manifest.permission.CAMERA)
}

Use with RxBinding

 rxBindingRequestButton.clicks()
            .throttleFirst(1L, TimeUnit.SECONDS)
            .flatMap {
                askPermissionsRx(Manifest.permission.CAMERA)
            }

Coroutine

    simpleRequestButton.setOnClickListener {
            launch {
                val result = awaitAskPermissions(
                    Manifest.permission.CAMERA,
                    rationaleDelegate = dialogRationaleDelegate
                )
                //do something for this permission result
               // handlePermissionRequest(result)
            }
        }

Livedata

simpleRequestButton.setOnClickListener {
            observeAskPermissions(
                Manifest.permission.CAMERA,
                rationaleDelegate = dialogRationaleDelegate
            ).observe(this, Observer{ result->
               //do something for this permission result
               // handlePermissionRequest(result)
            })
        }

Rationale Interface

Custom View

In PowerPermission it have a interface which called RationaleDelegate, it should be used as bridge to create your proper
delegation class to implement this interface.
It provide two basic public declaration functions,

  • displayRationale
//aims to display a view to explain reason to user why request permissions
    fun displayRationale(
        vararg permission: Permission,
        message: String,
        actionCallback: RationaleActionCallback
    )
  • onDismissView
//aims to simply disappear view and do some clean work etc.
 fun onDismissView()

PS: PowerPermission has already provided a class called DialogRationaleDelegate.kt to display a classic dialog view you can take a look at it and find more inspiration.

Choose those permissions are rational

  • RationaleData
    It's a data class used as choose which permission or permissions to display rationale view.
    And what kind of message will be displayed in your rational view.
    example:
RationaleData(
            rationalPermission = Manifest.permission.CAMERA,
            message = message
        )
RationaleData(
            rationalPermissions = listOf(Manifest.permission.CAMERA),
            message = message
        )

GitHub

Comments