An easy to use permissions management library written in Kotlin.
The library is deployed on JitPack here.
To add it to your project, add to settings.gradle
:
dependencyResolutionManagement {
...
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And to your module level build.gradle:
dependencies {
...
implementation 'com.github.lorenzofelletti:permissions:0.4.2'
}
To use the library in your project, just:
- Declare the permissions your app will use in your application's Manifest
- Example: add to the app
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- Example: add to the app
- Create a
PermissionManager
in yourActivity
- Example: in your
MainActivity
private val permissionManager = PermissionManager(this)
- Example: in your
- Decide a request code for each set of permissions that you will require simultaneously (each request code is a positive integer of your choice, but do not use the same code for different set of permissions)
- Example: add to your
MainActivity
companion object { private const val POSITION_REQUEST_CODE = 1 private val POSITION_REQUIRED_PERMISSIONS = arrayOf( android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION ) }
- Example: add to your
- Build the RequestResultsDispatcher
- Example: in the
MainActivity
'sonCreate
function add
permissionManager.buildRequestResultsDispatcher { withRequestCode(POSITION_REQUEST_CODE) { checkPermissions(POSITION_REQUIRED_PERMISSIONS) doOnGranted { Log.d(TAG, "Location permission granted") } doOnDenied { Log.d(TAG, "Location permission denied") } } }
- Example: in the
- Call
permissionManager.checkRequestAndDispatch
where you want to check for a set of permissions (and ask them if not granted)- Example: in your Activity, where you want to check (and request) permissions add
permissionManager checkRequestAndDispatch POSITION_REQUEST_CODE
- Override
onRequestPermissionsResult
and callPermissionsUtilities.dispatchOnRequestPermissionsResult
in it- Example: inside your
MainActivity
override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>, grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) permissionManager.dispatchOnRequestPermissionsResult(requestCode, grantResults) }
- Example: inside your
All kinds of contributions are welcome (bug reports, feature requests, pull requests, etc.). Suggestions and improvements on documentation, tests, code quality, translations, etc. are also welcome.