permissions.android.js 3.8KB

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