Skip to content

Commit

Permalink
Handle "Never Ask Again" in permissions and add requestMultiplePermis…
Browse files Browse the repository at this point in the history
…sions

Summary:
In order to get featured in the Google Play Store, we had to handle a few specific cases with permissions based on feedback from the editorial team.

First, which was previously possible with this permissions module was bumping the sdk to version 23.

The second is requesting multiple permissions at one time. In order for the camera + upload to work, we needed to request both camera permissions + media storage in one flow.

The last is handling the case where the user checks the "Never Ask Again" box. This will only appear after a user denies a permission once and is then prompted again. The logic for handling this case is taken from here: http://stackoverflow.com/questions/31928868/how-do-we-distinguish-never-asked-from-stop-asking-in-android-ms-runtime-permis/35495372#35495372

We were also seeing a few crashes similar to #10009 due to `onRequestPermissionsResult` being called before `onResume` (http://stackoverflow.com/questions/35205643/why-is-onresume-called-after-onrequestpermissionsresult), so I delaye
Closes facebook/react-native#10221

Differential Revision: D4232551

fbshipit-source-id: fee698d1c48a2d86623cb87996f3d17f4c10a62e
  • Loading branch information
cmcewen authored and Facebook Github Bot committed Nov 25, 2016
1 parent bbb2eaf commit f3ecda8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion UIExplorer/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ android {
defaultConfig {
applicationId "com.facebook.react.uiapp"
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk {
Expand Down
4 changes: 4 additions & 0 deletions UIExplorer/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<!--Just to show permissions example-->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="23" />
Expand Down
35 changes: 21 additions & 14 deletions UIExplorer/js/PermissionsExampleAndroid.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,38 @@ const React = require('react');
const ReactNative = require('react-native');
const {
PermissionsAndroid,
Picker,
StyleSheet,
Text,
TextInput,
TouchableWithoutFeedback,
View,
} = ReactNative;

const Item = Picker.Item;

exports.displayName = (undefined: ?string);
exports.framework = 'React';
exports.title = 'PermissionsAndroid';
exports.description = 'Permissions example for API 23+.';

class PermissionsExample extends React.Component {
state = {
permission: PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
permission: PermissionsAndroid.PERMISSIONS.CAMERA,
hasPermission: 'Not Checked',
};

render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Permission Name:</Text>
<TextInput
autoFocus={true}
autoCorrect={false}
style={styles.singleLine}
onChange={this._updateText}
defaultValue={this.state.permission}
/>
<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>
Expand All @@ -71,22 +74,22 @@ class PermissionsExample extends React.Component {
);
}

_updateText = (event: Object) => {
_onSelectPermission = (permission: string) => {
this.setState({
permission: event.nativeEvent.text,
permission: permission,
});
};

_checkPermission = async () => {
let result = await PermissionsAndroid.checkPermission(this.state.permission);
let result = await PermissionsAndroid.check(this.state.permission);
this.setState({
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' +
this.state.permission,
});
};

_requestPermission = async () => {
let result = await PermissionsAndroid.requestPermission(
let result = await PermissionsAndroid.request(
this.state.permission,
{
title: 'Permission Explanation',
Expand All @@ -95,8 +98,9 @@ class PermissionsExample extends React.Component {
' because of reasons. Please approve.'
},
);

this.setState({
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' +
hasPermission: result + ' for ' +
this.state.permission,
});
};
Expand Down Expand Up @@ -125,4 +129,7 @@ var styles = StyleSheet.create({
touchable: {
color: '#007AFF',
},
picker: {
flex: 1,
}
});

0 comments on commit f3ecda8

Please sign in to comment.