mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-29 15:59:39 +08:00
Add JS library for requesting Android M Permissions
Summary:
Explain the **motivation** for making this change. What existing problem does the pull request solve?
The Android permissions native module was open sourced recently (b7352b4667) but it is currently undocumented and requires directly interfacing with the native module.
This provides a JS wrapper to make it easier to use the permissions module and documents it.
This could be cleaner if the native code used Promise blocks instead of callbacks, but I didn't want to change the native code without a thumbs up since I'm guessing this is used in one of facebook's apps. Happy to do that if it makes sense
I also tried to make the `PERMISSIONS` object a class property - it works in the actual code but not in the documentation (think it's a jsdocs problem), so decided to initialize in the constructor.
**Test plan (required)**
If the API looks good, I will change the UIExplorer example to use this.
cc andreicoman11
Closes https://github.com/facebook/react-native/pull/9292
Differential Revision: D3716303
Pulled By: andreicoman11
fbshipit-source-id: cd40b8757fdf70ea8faecfb58caa00e99a99789e
This commit is contained in:
committed by
Facebook Github Bot 1
parent
1a683fa5e8
commit
0fb2ccfcc3
@@ -26,23 +26,22 @@
|
||||
const React = require('react');
|
||||
const ReactNative = require('react-native');
|
||||
const {
|
||||
PermissionsAndroid,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TextInput,
|
||||
TouchableWithoutFeedback,
|
||||
View,
|
||||
} = ReactNative;
|
||||
const DialogManager = require('NativeModules').DialogManagerAndroid;
|
||||
const Permissions = require('NativeModules').AndroidPermissions;
|
||||
|
||||
exports.displayName = (undefined: ?string);
|
||||
exports.framework = 'React';
|
||||
exports.title = '<Permissions>';
|
||||
exports.title = 'PermissionsAndroid';
|
||||
exports.description = 'Permissions example for API 23+.';
|
||||
|
||||
class PermissionsExample extends React.Component {
|
||||
state = {
|
||||
permission: 'android.permission.WRITE_EXTERNAL_STORAGE',
|
||||
permission: PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
||||
hasPermission: 'Not Checked',
|
||||
};
|
||||
|
||||
@@ -63,7 +62,7 @@ class PermissionsExample extends React.Component {
|
||||
</View>
|
||||
</TouchableWithoutFeedback>
|
||||
<Text style={styles.text}>Permission Status: {this.state.hasPermission}</Text>
|
||||
<TouchableWithoutFeedback onPress={this._shouldExplainPermission}>
|
||||
<TouchableWithoutFeedback onPress={this._requestPermission}>
|
||||
<View>
|
||||
<Text style={[styles.touchable, styles.text]}>Request Permission</Text>
|
||||
</View>
|
||||
@@ -78,51 +77,28 @@ class PermissionsExample extends React.Component {
|
||||
});
|
||||
};
|
||||
|
||||
_checkPermission = () => {
|
||||
Permissions.checkPermission(
|
||||
this.state.permission,
|
||||
(permission: string, result: boolean) => {
|
||||
this.setState({
|
||||
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' + permission,
|
||||
});
|
||||
},
|
||||
this._showError);
|
||||
_checkPermission = async () => {
|
||||
let result = await PermissionsAndroid.checkPermission(this.state.permission);
|
||||
this.setState({
|
||||
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' +
|
||||
this.state.permission,
|
||||
});
|
||||
};
|
||||
|
||||
_shouldExplainPermission = () => {
|
||||
Permissions.shouldShowRequestPermissionRationale(
|
||||
_requestPermission = async () => {
|
||||
let result = await PermissionsAndroid.requestPermission(
|
||||
this.state.permission,
|
||||
(permission: string, shouldShow: boolean) => {
|
||||
if (shouldShow) {
|
||||
DialogManager.showAlert(
|
||||
{
|
||||
title: 'Permission Explanation',
|
||||
message:
|
||||
'The app needs the following permission ' + this.state.permission +
|
||||
' because of reasons. Please approve.'
|
||||
},
|
||||
this._showError,
|
||||
this._requestPermission);
|
||||
} else {
|
||||
this._requestPermission();
|
||||
}
|
||||
{
|
||||
title: 'Permission Explanation',
|
||||
message:
|
||||
'The app needs the following permission ' + this.state.permission +
|
||||
' because of reasons. Please approve.'
|
||||
},
|
||||
this._showError);
|
||||
};
|
||||
|
||||
_requestPermission = () => {
|
||||
Permissions.requestPermission(
|
||||
this.state.permission,
|
||||
(permission: string, result: boolean) => {
|
||||
this.setState({
|
||||
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' + permission,
|
||||
});
|
||||
},
|
||||
this._showError);
|
||||
};
|
||||
|
||||
_showError = () => {
|
||||
DialogManager.showAlert({message: 'Error'}, {}, {});
|
||||
);
|
||||
this.setState({
|
||||
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' +
|
||||
this.state.permission,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -150,4 +126,3 @@ var styles = StyleSheet.create({
|
||||
color: '#007AFF',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user