index.android.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(
  62. result => {
  63. // PermissionsAndroid.request() to native module resolves to boolean
  64. // rather than string if running on OS version prior to Android M
  65. if (typeof result === 'boolean') {
  66. return result ? 'authorized' : 'denied'
  67. }
  68. return setDidAskOnce(permission).then(() => RESULTS[result])
  69. },
  70. )
  71. }
  72. checkMultiple = permissions =>
  73. Promise.all(permissions.map(this.check)).then(result =>
  74. result.reduce((acc, value, index) => {
  75. const name = permissions[index]
  76. acc[name] = value
  77. return acc
  78. }, {}),
  79. )
  80. }
  81. module.exports = new ReactNativePermissions()