-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPermissions.ts
149 lines (136 loc) · 4.57 KB
/
Permissions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { isIOS } from "@nativescript/core";
import {
coalesceExpirations,
coalesceStatuses,
coalesceCanAskAgain,
coalesceGranted,
} from 'expo-permissions/build/CoalescedPermissions';
import { PermissionResponse as UMPermissionResponse } from 'unimodules-permissions-interface';
import Permissions from './ExpoPermissions';
import {
PermissionResponse,
PermissionType,
PermissionMap,
PermissionStatus,
PermissionExpiration,
PermissionInfo,
} from 'expo-permissions/build/Permissions.types';
export {
PermissionStatus,
PermissionResponse,
PermissionExpiration,
PermissionMap,
PermissionInfo,
PermissionType,
};
export const CAMERA = 'camera';
export const CAMERA_ROLL = 'cameraRoll';
export const AUDIO_RECORDING = 'audioRecording';
export const LOCATION = 'location';
export const USER_FACING_NOTIFICATIONS = 'userFacingNotifications';
export const NOTIFICATIONS = 'notifications';
export const CONTACTS = 'contacts';
export const CALENDAR = 'calendar';
export const REMINDERS = 'reminders';
export const SYSTEM_BRIGHTNESS = 'systemBrightness';
export const MOTION = 'motion';
// Map corresponding permission to correct package
const PERMISSION_MODULE_MAPPING = {
[CAMERA]: 'expo-camera',
[CAMERA_ROLL]: 'expo-media-library',
[AUDIO_RECORDING]: 'expo-av',
[LOCATION]: 'expo-location',
[USER_FACING_NOTIFICATIONS]: 'expo-notifications',
[NOTIFICATIONS]: 'expo-notifications',
[CONTACTS]: 'expo-contacts',
[CALENDAR]: 'expo-calendar',
[REMINDERS]: 'expo-calendar',
[SYSTEM_BRIGHTNESS]: 'expo-brightness',
[MOTION]: 'expo-sensors',
};
/**
* @see ../node_modules/expo-permissions/ios/EXPermissions/EXPermissions.m parsePermissionFromRequester() method
*/
export async function getAsync(...types: PermissionType[]): Promise<PermissionResponse> {
if (isIOS) {
return await _handleMultiPermissionsRequestIOSAsync(types, Permissions.getAsync);
}
return await _handlePermissionsRequestAsync(types, Permissions.getAsync);
}
export async function askAsync(...types: PermissionType[]): Promise<PermissionResponse> {
if (isIOS) {
return await _handleMultiPermissionsRequestIOSAsync(types, Permissions.askAsync);
}
return await _handlePermissionsRequestAsync(types, Permissions.askAsync);
}
async function _handleSinglePermissionRequestIOSAsync(
type: PermissionType,
handlePermission: (type: PermissionType) => Promise<PermissionInfo>
): Promise<PermissionInfo> {
if (type === 'motion') {
return {
status: PermissionStatus.GRANTED,
expires: 'never',
granted: true,
canAskAgain: true,
};
}
try {
return await handlePermission(type);
} catch (error) {
// We recognize the permission's library, so we inform the user to link that library to request the permission.
if (error.code === 'E_PERMISSIONS_UNKNOWN' && PERMISSION_MODULE_MAPPING[type]) {
const library = PERMISSION_MODULE_MAPPING[type];
error.message = `${error.message}, please install and link the package ${PERMISSION_MODULE_MAPPING[type]}, see more at https://github.com/expo/expo/tree/master/packages/${library}`;
}
throw error;
}
}
async function _handleMultiPermissionsRequestIOSAsync(
types: PermissionType[],
handlePermission: (type: PermissionType) => Promise<PermissionInfo>
): Promise<PermissionResponse> {
if (!types.length) {
throw new Error('At least one permission type must be specified');
}
const permissions = {};
for (const type of types) {
permissions[type] = await _handleSinglePermissionRequestIOSAsync(type, handlePermission);
}
return {
status: coalesceStatuses(permissions),
expires: coalesceExpirations(permissions),
canAskAgain: coalesceCanAskAgain(permissions),
granted: coalesceGranted(permissions),
permissions,
};
}
async function _handlePermissionsRequestAsync(
types: PermissionType[],
handlePermissions: (types: PermissionType[]) => Promise<PermissionMap>
): Promise<PermissionResponse> {
if (!types.length) {
throw new Error('At least one permission type must be specified');
}
if (types.length === 1 && types[0] === 'motion') {
const approvedPermission = {
status: PermissionStatus.GRANTED,
expires: 'never',
granted: true,
canAskAgain: true,
};
return {
...approvedPermission,
// @ts-ignore
permissions: { motion: approvedPermission },
};
}
const permissions = await handlePermissions(types);
return {
status: coalesceStatuses(permissions),
expires: coalesceExpirations(permissions),
canAskAgain: coalesceCanAskAgain(permissions),
granted: coalesceGranted(permissions),
permissions,
};
}