|
@@ -2,6 +2,10 @@
|
2
|
2
|
|
3
|
3
|
import { AsyncStorage, NativeModules, PermissionsAndroid } from 'react-native'
|
4
|
4
|
|
|
5
|
+type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
|
|
6
|
+type Rationale = { title: string, message: string }
|
|
7
|
+type Options = string | { type: string, rationale?: Rationale }
|
|
8
|
+
|
5
|
9
|
const permissionTypes = {
|
6
|
10
|
location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
|
7
|
11
|
camera: PermissionsAndroid.PERMISSIONS.CAMERA,
|
|
@@ -23,80 +27,89 @@ const RESULTS = {
|
23
|
27
|
|
24
|
28
|
const STORAGE_KEY = '@RNPermissions:didAskPermission:'
|
25
|
29
|
|
26
|
|
-const setDidAskOnce = permission =>
|
|
30
|
+const setDidAskOnce = (permission: string) =>
|
27
|
31
|
AsyncStorage.setItem(STORAGE_KEY + permission, 'true')
|
28
|
32
|
|
29
|
|
-const getDidAskOnce = permission =>
|
|
33
|
+const getDidAskOnce = (permission: string) =>
|
30
|
34
|
AsyncStorage.getItem(STORAGE_KEY + permission).then(item => !!item)
|
31
|
35
|
|
32
|
36
|
class ReactNativePermissions {
|
33
|
|
- canOpenSettings = () => false
|
34
|
|
- openSettings = () => Promise.reject("'openSettings' is deprecated on android")
|
35
|
|
- getTypes = () => Object.keys(permissionTypes)
|
|
37
|
+ canOpenSettings: () => Promise<boolean> = () => Promise.resolve(false)
|
|
38
|
+
|
|
39
|
+ openSettings: () => Promise<*> = () =>
|
|
40
|
+ Promise.reject(new Error("'openSettings' is deprecated on android"))
|
36
|
41
|
|
37
|
|
- check = permission => {
|
38
|
|
- const androidPermission = permissionTypes[permission]
|
|
42
|
+ getTypes: () => Array<string> = () => Object.keys(permissionTypes)
|
39
|
43
|
|
40
|
|
- if (!androidPermission) {
|
41
|
|
- return Promise.reject(
|
|
44
|
+ check = (permission: string, type?: string): Promise<Status> => {
|
|
45
|
+ if (!permissionTypes[permission]) {
|
|
46
|
+ const error = new Error(
|
42
|
47
|
`ReactNativePermissions: ${
|
43
|
48
|
permission
|
44
|
49
|
} is not a valid permission type on Android`,
|
45
|
50
|
)
|
46
|
|
- }
|
47
|
51
|
|
48
|
|
- return PermissionsAndroid.check(androidPermission).then(isAuthorized => {
|
49
|
|
- if (isAuthorized) {
|
50
|
|
- return 'authorized'
|
51
|
|
- }
|
|
52
|
+ return Promise.reject(error)
|
|
53
|
+ }
|
52
|
54
|
|
53
|
|
- return getDidAskOnce(permission).then(didAsk => {
|
54
|
|
- if (didAsk) {
|
55
|
|
- return NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
|
56
|
|
- androidPermission,
|
57
|
|
- ).then(shouldShow => (shouldShow ? 'denied' : 'restricted'))
|
|
55
|
+ return PermissionsAndroid.check(permissionTypes[permission]).then(
|
|
56
|
+ isAuthorized => {
|
|
57
|
+ if (isAuthorized) {
|
|
58
|
+ return 'authorized'
|
58
|
59
|
}
|
59
|
|
- return 'undetermined'
|
60
|
|
- })
|
61
|
|
- })
|
62
|
|
- }
|
63
|
60
|
|
64
|
|
- request = (permission, options) => {
|
65
|
|
- const androidPermission = permissionTypes[permission]
|
|
61
|
+ return getDidAskOnce(permission).then(didAsk => {
|
|
62
|
+ if (didAsk) {
|
|
63
|
+ return NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
|
|
64
|
+ permissionTypes[permission],
|
|
65
|
+ ).then(shouldShow => (shouldShow ? 'denied' : 'restricted'))
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ return 'undetermined'
|
|
69
|
+ })
|
|
70
|
+ },
|
|
71
|
+ )
|
|
72
|
+ }
|
66
|
73
|
|
67
|
|
- if (!androidPermission) {
|
68
|
|
- return Promise.reject(
|
|
74
|
+ request = (permission: string, options?: Options): Promise<Status> => {
|
|
75
|
+ if (!permissionTypes[permission]) {
|
|
76
|
+ const error = new Error(
|
69
|
77
|
`ReactNativePermissions: ${
|
70
|
78
|
permission
|
71
|
79
|
} is not a valid permission type on Android`,
|
72
|
80
|
)
|
|
81
|
+
|
|
82
|
+ return Promise.reject(error)
|
73
|
83
|
}
|
74
|
84
|
|
75
|
|
- let rationale = null
|
76
|
|
- if (options != null) {
|
|
85
|
+ let rationale
|
|
86
|
+
|
|
87
|
+ if (options && options.rationale) {
|
77
|
88
|
rationale = options.rationale
|
78
|
89
|
}
|
79
|
90
|
|
80
|
|
- return PermissionsAndroid.request(androidPermission, rationale).then(
|
81
|
|
- result => {
|
82
|
|
- // PermissionsAndroid.request() to native module resolves to boolean
|
83
|
|
- // rather than string if running on OS version prior to Android M
|
84
|
|
- if (typeof result === 'boolean') {
|
85
|
|
- return result ? 'authorized' : 'denied'
|
86
|
|
- }
|
|
91
|
+ return PermissionsAndroid.request(
|
|
92
|
+ permissionTypes[permission],
|
|
93
|
+ rationale,
|
|
94
|
+ ).then(result => {
|
|
95
|
+ // PermissionsAndroid.request() to native module resolves to boolean
|
|
96
|
+ // rather than string if running on OS version prior to Android M
|
|
97
|
+ if (typeof result === 'boolean') {
|
|
98
|
+ return result ? 'authorized' : 'denied'
|
|
99
|
+ }
|
87
|
100
|
|
88
|
|
- return setDidAskOnce(permission).then(() => RESULTS[result])
|
89
|
|
- },
|
90
|
|
- )
|
|
101
|
+ return setDidAskOnce(permission).then(() => RESULTS[result])
|
|
102
|
+ })
|
91
|
103
|
}
|
92
|
104
|
|
93
|
|
- checkMultiple = permissions =>
|
94
|
|
- Promise.all(permissions.map(this.check)).then(result =>
|
95
|
|
- result.reduce((acc, value, index) => {
|
96
|
|
- const name = permissions[index]
|
97
|
|
- acc[name] = value
|
98
|
|
- return acc
|
99
|
|
- }, {}),
|
|
105
|
+ checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
|
|
106
|
+ Promise.all(permissions.map(permission => this.check(permission))).then(
|
|
107
|
+ result =>
|
|
108
|
+ result.reduce((acc, value, index) => {
|
|
109
|
+ const name = permissions[index]
|
|
110
|
+ acc[name] = value
|
|
111
|
+ return acc
|
|
112
|
+ }, {}),
|
100
|
113
|
)
|
101
|
114
|
}
|
102
|
115
|
|