-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.android.js
80 lines (66 loc) · 1.64 KB
/
index.android.js
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
/**
* @providesModule react-native-torch
* @flow
*/
import { NativeModules, PermissionsAndroid, Alert } from 'react-native';
const { Torch } = NativeModules;
/* Private fucntion to create a dialog with an OK button
This is because the RN rationale dialog has no buttons, so isn't obvious to dismiss
NOTE: We always show this dialog, if cameraPermission not present */
async function showRationaleDialog(title: string, message: string): Promise<*> {
let done;
const result = new Promise(resolve => {
done = resolve;
});
Alert.alert(
title,
message,
[
{
text: 'OK',
onPress: () => done()
}
],
{ cancelable: false }
);
return result;
}
async function requestCameraPermission(
title: string,
message: string
): Promise<boolean> {
try {
const hasCameraPermission = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.CAMERA
);
if (hasCameraPermission) {
return true;
}
await showRationaleDialog(title, message);
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
}
return false;
} catch (err) {
return false;
}
}
async function switchState(newState: boolean): Promise<boolean> {
let done;
let failure;
const result = new Promise((resolve, reject) => {
done = resolve;
failure = reject;
});
Torch.switchState(newState, done, failure);
return result;
}
const TorchWithPermissionCheck = {
...Torch,
switchState,
requestCameraPermission
};
export default TorchWithPermissionCheck;