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