Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. /**
  2. * Handles permissions result according to both [shouldShowSettingsDialog] and
  3. * fragment.shouldShowRequestPermissionRationale(permission) values.
  4. * If a user has denied permission permanently, the app shows them a dialog with an option to give permission
  5. * in Settings. There are following possible states for user denial:
  6. *
  7. * - fragment.shouldShowRequestPermissionRationale(permission):
  8. * Permission was denied for the first time
  9. *
  10. * - !fragment.shouldShowRequestPermissionRationale(permission) && shouldShowSettingsDialog:
  11. * Permission denied and user checked "Don't ask again" (no need to show settings dialog)
  12. *
  13. * - !fragment.shouldShowRequestPermissionRationale(permission) && !shouldShowSettingsDialog:
  14. * Permission permanently denied. Showing settings dialog
  15. *
  16. */
  17. fun handlePermissionsResult(
  18. fragment: Fragment,
  19. requestCode: Int,
  20. permissions: Array<String>,
  21. grantResults: IntArray,
  22. permissionGranted: () -> Unit,
  23. permissionPermanentlyDenied: () -> Unit
  24. ) {
  25. if (requestCode == REQUEST_PERMISSION) {
  26. for (i in grantResults.indices) {
  27. val permission = permissions[i]
  28. val result = grantResults[i]
  29. when (result) {
  30. PackageManager.PERMISSION_GRANTED -> permissionGranted()
  31. PackageManager.PERMISSION_DENIED -> {
  32. if (!fragment.shouldShowRequestPermissionRationale(permission) && !shouldShowSettingsDialog) {
  33. permissionPermanentlyDenied()
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement