CoroutinesPermission
Android library Android Library for easy permission management
Install
Gradle
- Add the JitPack repository to your project level build.gradle file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Add the dependency to your app level build.gradle file
implementation 'com.github.wooooooak:CoroutinesPermission:version'
The latest version is 1.0.2. Checkout here
Since this library uses coroutine, it is recommended to add the following library for convenience.
implementation "androidx.lifecycle:lifecycle-runtime-ktx:library_version"
Usage
We will use kotlin DSL for request permission.
yourCoroutineScope.launch {
val result = CoroutinesPermissionManager.requestPermission(activity) {
permissionList = listOf(Manifest.permission.CAMERA, or something..)
Rationale {
title = "Alert"
message = "In order to use the app, camera permission is required."
}
...
}
when (result) {
is PermissionResult.Granted -> {}
is PermissionResult.Denied -> {}
}
}
You can set the following items.
-
permissionList (required) : List of permission to acquire.
-
rationaleTitle (option) : Title for Rationale dialog.
-
rationaleMessage (option) : Message for Rationale dialog.
-
rationaleConfirmText (option) : Confirm button Text for Rationale dialog.
-
rationaleCancelText (option) : Cancel button Text for Rationale dialog.
Rationale example
Result
Result is an Sealed Class.
sealed class PermissionResult {
object Granted : PermissionResult()
class Denied(
val deniedList: List,
val permanentlyDeniedList: List
) : PermissionResult()
override fun toString(): String {
return when (this) {
is Granted -> "All Permission granted"
is Denied -> "Some Permission denied : \ndenied - $deniedList, \npermanentlyDeniedList-$permanentlyDeniedList"
}
}
}
Therefore, if the permission acquisition fails, it is possible to know which permission was not obtained and which permission was permanently denied.
License
Copyright 2020 wooooooak
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.