Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.grumpyshoe:android-module-permissionmanager:1.2.0'
}
dependencies {
implementation("com.github.grumpyshoe:android-module-permissionmanager:1.2.0")
}
<dependency>
<groupId>com.github.grumpyshoe</groupId>
<artifactId>android-module-permissionmanager</artifactId>
<version>1.2.0</version>
</dependency>
libraryDependencies += "com.github.grumpyshoe" % "android-module-permissionmanager" % "1.2.0"
:dependencies [[com.github.grumpyshoe/android-module-permissionmanager "1.2.0"]]
# Permissionmanager
Permissionmanager
is a small wrapper for handling permission requests.
## Installation
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
:
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