Permissionmanager
is a small wrapper for handling permission requests.
Add jitpack
to your repositories in Project build.gradle
:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
...
}
Add dependency to your app build.gradle
:
implementation 'com.github.grumpyshoe:android-module-permissionmanager:1.2.0'
Get instance of Permissionmanager
:
val permissionManager: PermissionManager = PermissionManagerImpl
To handle the users decision you have to delegate the response of onRequestPermissionsResult
in your activity to the library like this:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
permissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults)
?: super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
Check permission grant state by calling checkPermissions
.
permissionManager.checkPermissions(
activity = this,
permissions = <array-of-permissions>,
onPermissionResult = { permissionResult ->
// handle permission result
})
It's best practise to inform the user about why permission are needed before requesting them so you can define a PermissionRequestExplanation
at permissionRequestPreExecuteExplanation
with information that explains this request.
When adding this parameter a AlertDialog will be shown before requesting permissions.
If the user denies a permission first but another action requests this again because it's needed to do the job, then that's where shouldShowRequestPermissionRationale
takes place.
You can also define which titel should be shown by adding a PermissionRequestExplanation
to the attribute permissionRequestRetryExplanation
.
Last but not least you can define which requestCode should be used on permission requests. The default is 8102.
Example implementation with all available attributes:
permissionManager.checkPermissions(
activity = this,
permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
onPermissionResult = { permissionResult ->
// handle permission result
},
permissionRequestPreExecuteExplanation = PermissionRequestExplanation(
title = "Pre Custom Permission Hint",
message = "The App will request the permissions ..."),
permissionRequestRetryExplanation = PermissionRequestExplanation(
title = "Retry Custom Permission Hint",
message = "You denied the permissions previously but this permissions are needed because ..."),
requestCode = 123)
If you want to customize the button labels that are shown on shouldShowRequestPermissionRationale = true
just add a translation for the following string to your strings.xml
:
- permission_request_explanation_btn_grant_text ("grant" by default)
- permission_request_explanation_btn_deny_text ("deny" by default)
- Please make sure you have added corresponding permissions to yout Manifest, otherwise you get this response in Logcat without anythins noticable on UI-Screen:
PermissionManager: Permissions denied: [...]
Please submit an issue on GitHub.
This project is licensed under the terms of the MIT license. See the LICENSE file.
Android Studio 3.1.4
Build #AI-173.4907809, built on July 23, 2018
JRE: 1.8.0_152-release-1024-b01 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.13.4