|
@@ -1,91 +1,100 @@
|
1
|
|
-'use strict';
|
|
1
|
+'use strict'
|
2
|
2
|
|
3
|
3
|
const ReactNative = require('react-native')
|
4
|
|
-const RNPermissions = ReactNative.PermissionsAndroid;
|
|
4
|
+const RNPermissions = ReactNative.PermissionsAndroid
|
5
|
5
|
const AsyncStorage = ReactNative.AsyncStorage
|
6
|
6
|
|
7
|
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,
|
|
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,
|
18
|
18
|
}
|
19
|
19
|
|
20
|
20
|
const RESULTS = {
|
21
|
|
- [ RNPermissions.RESULTS.GRANTED ]: 'authorized',
|
22
|
|
- [ RNPermissions.RESULTS.DENIED ]: 'denied',
|
23
|
|
- [ RNPermissions.RESULTS.NEVER_ASK_AGAIN ]: 'restricted',
|
|
21
|
+ [RNPermissions.RESULTS.GRANTED]: 'authorized',
|
|
22
|
+ [RNPermissions.RESULTS.DENIED]: 'denied',
|
|
23
|
+ [RNPermissions.RESULTS.NEVER_ASK_AGAIN]: 'restricted',
|
24
|
24
|
}
|
25
|
25
|
|
26
|
26
|
const STORAGE_KEY = '@RNPermissions:didAskPermission:'
|
27
|
27
|
|
28
|
28
|
const setDidAskOnce = p => AsyncStorage.setItem(STORAGE_KEY + p, 'true')
|
29
|
|
-const getDidAskOnce = p => AsyncStorage.getItem(STORAGE_KEY + p).then(res => !!res)
|
|
29
|
+const getDidAskOnce = p =>
|
|
30
|
+ AsyncStorage.getItem(STORAGE_KEY + p).then(res => !!res)
|
30
|
31
|
|
31
|
32
|
class ReactNativePermissions {
|
32
|
|
- canOpenSettings() {
|
33
|
|
- return false
|
34
|
|
- }
|
35
|
|
-
|
36
|
|
- openSettings() {
|
37
|
|
- return Promise.reject('\'openSettings\' is Depricated on android')
|
38
|
|
- }
|
39
|
|
-
|
40
|
|
- getTypes() {
|
41
|
|
- return Object.keys(RNPTypes);
|
42
|
|
- }
|
43
|
|
-
|
44
|
|
- check(permission) {
|
45
|
|
- const androidPermission = RNPTypes[permission]
|
46
|
|
- if (!androidPermission) return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on Android`);
|
47
|
|
-
|
48
|
|
- const shouldShowRationale = ReactNative.NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale;
|
49
|
|
-
|
50
|
|
- return RNPermissions.check(androidPermission)
|
51
|
|
- .then(isAuthorized => {
|
52
|
|
- if (isAuthorized) return 'authorized'
|
53
|
|
-
|
54
|
|
- return getDidAskOnce(permission)
|
55
|
|
- .then(didAsk => {
|
56
|
|
- if (didAsk) {
|
57
|
|
- return shouldShowRationale(androidPermission)
|
58
|
|
- .then(shouldShow => shouldShow ? 'denied' : 'restricted')
|
59
|
|
- }
|
60
|
|
- return 'undetermined'
|
61
|
|
- })
|
62
|
|
- })
|
63
|
|
- }
|
64
|
|
-
|
65
|
|
-
|
66
|
|
- request(permission) {
|
67
|
|
- const androidPermission = RNPTypes[permission]
|
68
|
|
- if (!androidPermission) return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type on Android`);
|
69
|
|
-
|
70
|
|
-
|
71
|
|
- return RNPermissions.request(androidPermission)
|
72
|
|
- .then(res => {
|
73
|
|
- // RNPermissions.request() to native module resolves to boolean
|
74
|
|
- // rather than string if running on OS version prior to Android M
|
75
|
|
- if (typeof res === 'boolean') return res ? 'authorized' : 'denied';
|
76
|
|
- return setDidAskOnce(permission)
|
77
|
|
- .then(() => RESULTS[res])
|
78
|
|
- });
|
79
|
|
- }
|
80
|
|
-
|
81
|
|
- checkMultiple(permissions) {
|
82
|
|
- return Promise.all(permissions.map(this.check.bind(this)))
|
83
|
|
- .then(res => res.reduce((pre, cur, i) => {
|
84
|
|
- var name = permissions[i]
|
85
|
|
- pre[name] = cur
|
86
|
|
- return pre
|
87
|
|
- }, {}))
|
88
|
|
- }
|
|
33
|
+ canOpenSettings() {
|
|
34
|
+ return false
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ openSettings() {
|
|
38
|
+ return Promise.reject("'openSettings' is Depricated on android")
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ getTypes() {
|
|
42
|
+ return Object.keys(RNPTypes)
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ check(permission) {
|
|
46
|
+ const androidPermission = RNPTypes[permission]
|
|
47
|
+ if (!androidPermission)
|
|
48
|
+ return Promise.reject(
|
|
49
|
+ `ReactNativePermissions: ${
|
|
50
|
+ permission
|
|
51
|
+ } is not a valid permission type on Android`,
|
|
52
|
+ )
|
|
53
|
+
|
|
54
|
+ const shouldShowRationale =
|
|
55
|
+ ReactNative.NativeModules.PermissionsAndroid
|
|
56
|
+ .shouldShowRequestPermissionRationale
|
|
57
|
+
|
|
58
|
+ return RNPermissions.check(androidPermission).then(isAuthorized => {
|
|
59
|
+ if (isAuthorized) return 'authorized'
|
|
60
|
+
|
|
61
|
+ return getDidAskOnce(permission).then(didAsk => {
|
|
62
|
+ if (didAsk) {
|
|
63
|
+ return shouldShowRationale(androidPermission).then(
|
|
64
|
+ shouldShow => (shouldShow ? 'denied' : 'restricted'),
|
|
65
|
+ )
|
|
66
|
+ }
|
|
67
|
+ return 'undetermined'
|
|
68
|
+ })
|
|
69
|
+ })
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ request(permission) {
|
|
73
|
+ const androidPermission = RNPTypes[permission]
|
|
74
|
+ if (!androidPermission)
|
|
75
|
+ return Promise.reject(
|
|
76
|
+ `ReactNativePermissions: ${
|
|
77
|
+ permission
|
|
78
|
+ } is not a valid permission type on Android`,
|
|
79
|
+ )
|
|
80
|
+
|
|
81
|
+ return RNPermissions.request(androidPermission).then(res => {
|
|
82
|
+ // RNPermissions.request() to native module resolves to boolean
|
|
83
|
+ // 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])
|
|
86
|
+ })
|
|
87
|
+ }
|
|
88
|
+
|
|
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
|
|
95
|
+ }, {}),
|
|
96
|
+ )
|
|
97
|
+ }
|
89
|
98
|
}
|
90
|
99
|
|
91
|
100
|
module.exports = new ReactNativePermissions()
|