Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Handles permissions result according to both [shouldShowSettingsDialog] and
- * fragment.shouldShowRequestPermissionRationale(permission) values.
- * If a user has denied permission permanently, the app shows them a dialog with an option to give permission
- * in Settings. There are following possible states for user denial:
- *
- * - fragment.shouldShowRequestPermissionRationale(permission):
- * Permission was denied for the first time
- *
- * - !fragment.shouldShowRequestPermissionRationale(permission) && shouldShowSettingsDialog:
- * Permission denied and user checked "Don't ask again" (no need to show settings dialog)
- *
- * - !fragment.shouldShowRequestPermissionRationale(permission) && !shouldShowSettingsDialog:
- * Permission permanently denied. Showing settings dialog
- *
- */
- fun handlePermissionsResult(
- fragment: Fragment,
- requestCode: Int,
- permissions: Array<String>,
- grantResults: IntArray,
- permissionGranted: () -> Unit,
- permissionPermanentlyDenied: () -> Unit
- ) {
- if (requestCode == REQUEST_PERMISSION) {
- for (i in grantResults.indices) {
- val permission = permissions[i]
- val result = grantResults[i]
- when (result) {
- PackageManager.PERMISSION_GRANTED -> permissionGranted()
- PackageManager.PERMISSION_DENIED -> {
- if (!fragment.shouldShowRequestPermissionRationale(permission) && !shouldShowSettingsDialog) {
- permissionPermanentlyDenied()
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement