2016-06-15 16:42:51 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-06-15 16:42:51 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-06-15 16:42:51 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2018-08-08 17:39:16 +00:00
|
|
|
* @flow
|
2016-06-15 16:42:51 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-06-15 16:42:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {
|
2016-08-15 12:52:04 +00:00
|
|
|
PermissionsAndroid,
|
2016-11-25 06:36:23 +00:00
|
|
|
Picker,
|
2016-06-15 16:42:51 +00:00
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TouchableWithoutFeedback,
|
|
|
|
View,
|
|
|
|
} = ReactNative;
|
|
|
|
|
2016-11-25 06:36:23 +00:00
|
|
|
const Item = Picker.Item;
|
|
|
|
|
2016-06-15 16:42:51 +00:00
|
|
|
exports.displayName = (undefined: ?string);
|
|
|
|
exports.framework = 'React';
|
2016-08-15 12:52:04 +00:00
|
|
|
exports.title = 'PermissionsAndroid';
|
2016-06-15 16:42:51 +00:00
|
|
|
exports.description = 'Permissions example for API 23+.';
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class PermissionsExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
2016-11-25 06:36:23 +00:00
|
|
|
permission: PermissionsAndroid.PERMISSIONS.CAMERA,
|
2016-07-26 08:00:02 +00:00
|
|
|
hasPermission: 'Not Checked',
|
|
|
|
};
|
2016-06-15 16:42:51 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2016-06-15 16:42:51 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Text style={styles.text}>Permission Name:</Text>
|
2016-11-25 06:36:23 +00:00
|
|
|
<Picker
|
|
|
|
style={styles.picker}
|
|
|
|
selectedValue={this.state.permission}
|
|
|
|
onValueChange={this._onSelectPermission.bind(this)}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<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}
|
|
|
|
/>
|
2016-11-25 06:36:23 +00:00
|
|
|
</Picker>
|
2016-06-15 16:42:51 +00:00
|
|
|
<TouchableWithoutFeedback onPress={this._checkPermission}>
|
|
|
|
<View>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Text style={[styles.touchable, styles.text]}>
|
|
|
|
Check Permission
|
|
|
|
</Text>
|
2016-06-15 16:42:51 +00:00
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Text style={styles.text}>
|
|
|
|
Permission Status: {this.state.hasPermission}
|
|
|
|
</Text>
|
2016-08-15 12:52:04 +00:00
|
|
|
<TouchableWithoutFeedback onPress={this._requestPermission}>
|
2016-06-15 16:42:51 +00:00
|
|
|
<View>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Text style={[styles.touchable, styles.text]}>
|
|
|
|
Request Permission
|
|
|
|
</Text>
|
2016-06-15 16:42:51 +00:00
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-06-15 16:42:51 +00:00
|
|
|
|
2016-11-25 06:36:23 +00:00
|
|
|
_onSelectPermission = (permission: string) => {
|
2016-06-15 16:42:51 +00:00
|
|
|
this.setState({
|
2016-11-25 06:36:23 +00:00
|
|
|
permission: permission,
|
2016-06-15 16:42:51 +00:00
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-06-15 16:42:51 +00:00
|
|
|
|
2016-08-15 12:52:04 +00:00
|
|
|
_checkPermission = async () => {
|
2016-11-25 06:36:23 +00:00
|
|
|
let result = await PermissionsAndroid.check(this.state.permission);
|
2016-08-15 12:52:04 +00:00
|
|
|
this.setState({
|
2018-05-11 20:32:37 +00:00
|
|
|
hasPermission:
|
|
|
|
(result ? 'Granted' : 'Revoked') + ' for ' + this.state.permission,
|
2016-08-15 12:52:04 +00:00
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-06-15 16:42:51 +00:00
|
|
|
|
2016-08-15 12:52:04 +00:00
|
|
|
_requestPermission = async () => {
|
2018-05-11 20:32:37 +00:00
|
|
|
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.',
|
|
|
|
});
|
2016-11-25 06:36:23 +00:00
|
|
|
|
2016-08-15 12:52:04 +00:00
|
|
|
this.setState({
|
2018-05-11 20:32:37 +00:00
|
|
|
hasPermission: result + ' for ' + this.state.permission,
|
2016-08-15 12:52:04 +00:00
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
}
|
2016-06-15 16:42:51 +00:00
|
|
|
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'Permissions Example',
|
2018-05-11 20:32:37 +00:00
|
|
|
description:
|
|
|
|
'Short example of how to use the runtime permissions API introduced in Android M.',
|
2016-06-15 16:42:51 +00:00
|
|
|
render: () => <PermissionsExample />,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: 'white',
|
|
|
|
},
|
|
|
|
singleLine: {
|
|
|
|
fontSize: 16,
|
|
|
|
padding: 4,
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
margin: 10,
|
|
|
|
},
|
|
|
|
touchable: {
|
|
|
|
color: '#007AFF',
|
|
|
|
},
|
2016-11-25 06:36:23 +00:00
|
|
|
picker: {
|
|
|
|
flex: 1,
|
2018-05-11 20:32:37 +00:00
|
|
|
},
|
2016-06-15 16:42:51 +00:00
|
|
|
});
|