index.android.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 => AsyncStorage.getItem(STORAGE_KEY + p).then(res => !!res)
  25. class ReactNativePermissions {
  26. canOpenSettings() {
  27. return false
  28. }
  29. openSettings() {
  30. return Promise.reject('\'openSettings\' is Depricated on android')
  31. }
  32. getTypes() {
  33. return Object.keys(RNPTypes);
  34. }
  35. check(permission) {
  36. const androidPermission = RNPTypes[permission]
  37. if (!androidPermission) return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on Android`);
  38. const shouldShowRationale = ReactNative.NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale;
  39. return RNPermissions.check(androidPermission)
  40. .then(isAuthorized => {
  41. if (isAuthorized) return 'authorized'
  42. return getDidAskOnce(permission)
  43. .then(didAsk => {
  44. if (didAsk) {
  45. return shouldShowRationale(androidPermission)
  46. .then(shouldShow => shouldShow ? 'denied' : 'restricted')
  47. }
  48. return 'undetermined'
  49. })
  50. })
  51. }
  52. request(permission) {
  53. const androidPermission = RNPTypes[permission]
  54. if (!androidPermission) return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on Android`);
  55. return RNPermissions.request(androidPermission)
  56. .then(res => {
  57. return setDidAskOnce(permission)
  58. .then(() => RESULTS[res])
  59. });
  60. }
  61. checkMultiple(permissions) {
  62. return Promise.all(permissions.map(this.check.bind(this)))
  63. .then(res => res.reduce((pre, cur, i) => {
  64. var name = permissions[i]
  65. pre[name] = cur
  66. return pre
  67. }, {}))
  68. }
  69. }
  70. module.exports = new ReactNativePermissions()