index.android.js 2.9KB

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