index.android.js 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // @flow
  2. import { AsyncStorage, NativeModules, PermissionsAndroid } from 'react-native'
  3. const permissionTypes = {
  4. location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  5. camera: PermissionsAndroid.PERMISSIONS.CAMERA,
  6. microphone: PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
  7. contacts: PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
  8. event: PermissionsAndroid.PERMISSIONS.READ_CALENDAR,
  9. storage: PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
  10. photo: PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
  11. callPhone: PermissionsAndroid.PERMISSIONS.CALL_PHONE,
  12. readSms: PermissionsAndroid.PERMISSIONS.READ_SMS,
  13. receiveSms: PermissionsAndroid.PERMISSIONS.RECEIVE_SMS,
  14. }
  15. const RESULTS = {
  16. [PermissionsAndroid.RESULTS.GRANTED]: 'authorized',
  17. [PermissionsAndroid.RESULTS.DENIED]: 'denied',
  18. [PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN]: 'restricted',
  19. }
  20. const STORAGE_KEY = '@RNPermissions:didAskPermission:'
  21. const setDidAskOnce = permission =>
  22. AsyncStorage.setItem(STORAGE_KEY + permission, 'true')
  23. const getDidAskOnce = permission =>
  24. AsyncStorage.getItem(STORAGE_KEY + permission).then(item => !!item)
  25. class ReactNativePermissions {
  26. canOpenSettings = () => false
  27. openSettings = () => Promise.reject("'openSettings' is deprecated on android")
  28. getTypes = () => Object.keys(permissionTypes)
  29. check = permission => {
  30. const androidPermission = permissionTypes[permission]
  31. if (!androidPermission) {
  32. return Promise.reject(
  33. `ReactNativePermissions: ${
  34. permission
  35. } is not a valid permission type on Android`,
  36. )
  37. }
  38. return PermissionsAndroid.check(androidPermission).then(isAuthorized => {
  39. if (isAuthorized) {
  40. return 'authorized'
  41. }
  42. return getDidAskOnce(permission).then(didAsk => {
  43. if (didAsk) {
  44. return NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
  45. androidPermission,
  46. ).then(shouldShow => (shouldShow ? 'denied' : 'restricted'))
  47. }
  48. return 'undetermined'
  49. })
  50. })
  51. }
  52. request = (permission, { rationale }) => {
  53. const androidPermission = permissionTypes[permission]
  54. if (!androidPermission) {
  55. return Promise.reject(
  56. `ReactNativePermissions: ${
  57. permission
  58. } is not a valid permission type on Android`,
  59. )
  60. }
  61. return PermissionsAndroid.request(androidPermission, rationale).then(result => {
  62. // PermissionsAndroid.request() to native module resolves to boolean
  63. // rather than string if running on OS version prior to Android M
  64. if (typeof result === 'boolean') {
  65. return result ? 'authorized' : 'denied'
  66. }
  67. return setDidAskOnce(permission).then(() => RESULTS[result])
  68. })
  69. }
  70. checkMultiple = permissions =>
  71. Promise.all(permissions.map(this.check)).then(result =>
  72. result.reduce((acc, value, index) => {
  73. const name = permissions[index]
  74. acc[name] = value
  75. return acc
  76. }, {}),
  77. )
  78. }
  79. module.exports = new ReactNativePermissions()