forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermissionsExample.js
More file actions
130 lines (118 loc) · 3.31 KB
/
PermissionsExample.js
File metadata and controls
130 lines (118 loc) · 3.31 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
const React = require('react');
const {
PermissionsAndroid,
Picker,
StyleSheet,
Text,
TouchableWithoutFeedback,
View,
} = require('react-native');
const Item = Picker.Item;
class PermissionsExample extends React.Component<{...}, $FlowFixMeState> {
state = {
permission: PermissionsAndroid.PERMISSIONS.CAMERA,
hasPermission: 'Not Checked',
};
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Permission Name:</Text>
<Picker
style={styles.picker}
selectedValue={this.state.permission}
onValueChange={this._onSelectPermission.bind(this)}>
<Item
label={PermissionsAndroid.PERMISSIONS.CAMERA}
value={PermissionsAndroid.PERMISSIONS.CAMERA}
/>
<Item
label={PermissionsAndroid.PERMISSIONS.READ_CALENDAR}
value={PermissionsAndroid.PERMISSIONS.READ_CALENDAR}
/>
<Item
label={PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION}
value={PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION}
/>
</Picker>
<TouchableWithoutFeedback onPress={this._checkPermission}>
<View>
<Text style={[styles.touchable, styles.text]}>
Check Permission
</Text>
</View>
</TouchableWithoutFeedback>
<Text style={styles.text}>
Permission Status: {this.state.hasPermission}
</Text>
<TouchableWithoutFeedback onPress={this._requestPermission}>
<View>
<Text style={[styles.touchable, styles.text]}>
Request Permission
</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
}
_onSelectPermission = (permission: string | number) => {
this.setState({
permission: permission,
});
};
_checkPermission = async () => {
let result = await PermissionsAndroid.check(this.state.permission);
this.setState({
hasPermission:
(result ? 'Granted' : 'Revoked') + ' for ' + this.state.permission,
});
};
_requestPermission = async () => {
let result = await PermissionsAndroid.request(this.state.permission, {
title: 'Permission Explanation',
message:
'The app needs the following permission ' +
this.state.permission +
' because of reasons. Please approve.',
});
this.setState({
hasPermission: result + ' for ' + this.state.permission,
});
};
}
exports.displayName = (undefined: ?string);
exports.framework = 'React';
exports.title = 'PermissionsAndroid';
exports.description = 'Permissions example for API 23+.';
exports.examples = [
{
title: 'Permissions Example',
description:
'Short example of how to use the runtime permissions API introduced in Android M.',
render: (): React.Node => <PermissionsExample />,
},
];
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
text: {
margin: 10,
},
touchable: {
color: '#007AFF',
},
picker: {
flex: 1,
},
});