permissionsdispatcher-ktx
aims to let developers cope with runtime permissions handling in a declarative style without using annotation processing(kapt).
Let's see a minimum example, in which you register a MainActivity
which requires Manifest.permission.CAMERA
.
Add the following line to AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
The library provides constructPermissionsRequest
which you can construct a requester object with the given several callback functions to be called in an appropriate situation.
/**
* @param permissions the permissions [requiresPermission] requires.
* @param onShowRationale the method explains why the permissions are required.
* @param onPermissionDenied the method invoked if the user doesn't grant the permissions.
* @param onNeverAskAgain the method invoked if the user does not deny the permissions with
* "never ask again" option.
* @param requiresPermission the action requires [permissions].
*/
fun FragmentActivity/*(or Fragment)*/.constructPermissionsRequest(
vararg permissions: String,
onShowRationale: ShowRationaleFunc? = null,
onPermissionDenied: Func? = null,
onNeverAskAgain: Func? = null,
requiresPermission: Func): PermissionsRequester
Here you just define showCamera
and basically that's it! With the library you don't need to manually override onRequestPermissionsResult
.
NOTE: Be sure to construct a requester every time an activity is created to capture the callbacks appropriately.
class MainActivity: AppCompatActivity {
// constructPermissionsRequest must be invoked every time an activity is created
private val showCamera = constructPermissionsRequest(Manifest.permission.CAMERA,
onShowRationale = ::onCameraShowRationale,
onPermissionDenied = ::onCameraDenied,
onNeverAskAgain = ::onCameraNeverAskAgain) {
// do something here
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById(R.id.button_camera).setOnClickListener {
showCamera.launch()
}
}
private fun onCameraDenied() {
Toast.makeText(requireContext(), R.string.permission_camera_denied, Toast.LENGTH_SHORT).show()
}
private fun onCameraShowRationale(request: PermissionRequest) {
request.proceed()
}
private fun onCameraNeverAskAgain() {
Toast.makeText(requireContext(), R.string.permission_camera_never_ask_again, Toast.LENGTH_SHORT).show()
}
}
Check out the sample for more details.
Since the location permissions have been one of the most sensitive permission group to deal with, we provide a dedicated method constructLocationPermissionRequest
.
With the method you don't have to think of which API version you can ask ACCESS_BACKGROUND_LOCATION(see the issue for more detail).
/**
* @param onShowRationale the method explains why the permissions are required.
* @param onPermissionDenied the method invoked if the user doesn't grant the permissions.
* @param requiresPermission the action requires [permissions].
*/
fun FragmentActivity/*(or Fragment)*/.constructLocationPermissionRequest(
vararg permissions: LocationPermission,
onShowRationale: ShowRationaleFun? = null,
onPermissionDenied: Fun? = null,
onNeverAskAgain: Fun? = null,
requiresPermission: Fun
): PermissionsRequester
The library also provides constructWriteSettingsPermissionRequest
and
constructSystemAlertWindowPermissionRequest
to support WRITE_SETTINGS
and SYSTEM_ALERT_WINDOW
that
requires exceptional handling.
/**
* @param onShowRationale the method explains why the permissions are required.
* @param onPermissionDenied the method invoked if the user doesn't grant the permissions.
* @param requiresPermission the action requires [permissions].
*/
fun FragmentActivity/*(or Fragment)*/.constructWriteSettingsPermissionRequest(
onShowRationale: ShowRationaleFunc? = null,
onPermissionDenied: Func? = null,
requiresPermission: Func): PermissionsRequester
/**
* @param onShowRationale the method explains why the permissions are required.
* @param onPermissionDenied the method invoked if the user doesn't grant the permissions.
* @param requiresPermission the action requires [permissions].
*/
fun FragmentActivity/*(or Fragment)*/.constructSystemAlertWindowPermissionRequest(
onShowRationale: ShowRationaleFunc? = null,
onPermissionDenied: Func? = null,
requiresPermission: Func): PermissionsRequester
dependencies {
implementation "com.github.permissions-dispatcher:ktx:${latest.version}"
}