mirror of
https://github.com/status-im/react-native-camera.git
synced 2025-02-24 01:38:18 +00:00
Merge pull request #1076 from react-native-community/android-6-permissions
Add permission handling for android 6+
This commit is contained in:
commit
4e237549e3
@ -186,6 +186,8 @@ export default class Example extends React.Component {
|
||||
onZoomChanged={() => {}}
|
||||
defaultTouchToFocus
|
||||
mirrorImage={false}
|
||||
permissionDialogTitle="Sample title"
|
||||
permissionDialogMessage="Sample dialog message"
|
||||
/>
|
||||
<View style={[styles.overlay, styles.topOverlay]}>
|
||||
<TouchableOpacity
|
||||
|
20
README.md
20
README.md
@ -322,6 +322,26 @@ from javascript.
|
||||
|
||||
If set to `true`, the device will not sleep while the camera preview is visible. This mimics the behavior of the default camera app, which keeps the device awake while open.
|
||||
|
||||
#### `Android` `permissionDialogTitle`
|
||||
|
||||
Starting on android M individual permissions must be granted for certain services, the camera is one of them, you can use this to change the title of the dialog prompt requesting permissions.
|
||||
|
||||
#### `Android` `permissionDialogMessage`
|
||||
|
||||
Starting on android M individual permissions must be granted for certain services, the camera is one of them, you can use this to change the content of the dialog prompt requesting permissions.
|
||||
|
||||
#### `notAuthorizedView`
|
||||
|
||||
By default a `Camera not authorized` message will be displayed when access to the camera has been denied, if set displays the passed react element instead of the default one.
|
||||
|
||||
#### `pendingAuthorizationView`
|
||||
|
||||
By default a <ActivityIndicator> will be displayed while the component is waiting for the user to grant/deny access to the camera, if set displays the passed react element instead of the default one.
|
||||
|
||||
#### `pendingAuthorizationView`
|
||||
|
||||
|
||||
|
||||
#### `mirrorImage`
|
||||
|
||||
If set to `true`, the image returned will be mirrored.
|
||||
|
73
src/index.js
73
src/index.js
@ -8,6 +8,10 @@ import {
|
||||
StyleSheet,
|
||||
requireNativeComponent,
|
||||
ViewPropTypes,
|
||||
PermissionsAndroid,
|
||||
ActivityIndicator,
|
||||
View,
|
||||
Text,
|
||||
} from 'react-native';
|
||||
|
||||
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
|
||||
@ -91,6 +95,10 @@ export default class Camera extends Component {
|
||||
playSoundOnCapture: PropTypes.bool,
|
||||
torchMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
||||
type: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
||||
permissionDialogTitle: PropTypes.string,
|
||||
permissionDialogMessage: PropTypes.string,
|
||||
notAuthorizedView: PropTypes.element,
|
||||
pendingAuthorizationView: PropTypes.element,
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
@ -108,6 +116,31 @@ export default class Camera extends Component {
|
||||
torchMode: CameraManager.TorchMode.off,
|
||||
mirrorImage: false,
|
||||
barCodeTypes: Object.values(CameraManager.BarCodeType),
|
||||
permissionDialogTitle: '',
|
||||
permissionDialogMessage: '',
|
||||
notAuthorizedView: (
|
||||
<View style={{
|
||||
flex: 1,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
}}>
|
||||
<Text style={{
|
||||
textAlign: 'center',
|
||||
fontSize: 16,
|
||||
}}>
|
||||
Camera not authorized
|
||||
</Text>
|
||||
</View>
|
||||
),
|
||||
pendingAuthorizationView: (
|
||||
<View style={{
|
||||
flex: 1,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
}}>
|
||||
<ActivityIndicator size="small"/>
|
||||
</View>
|
||||
),
|
||||
};
|
||||
|
||||
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
|
||||
@ -123,6 +156,7 @@ export default class Camera extends Component {
|
||||
super();
|
||||
this.state = {
|
||||
isAuthorized: false,
|
||||
isAuthorizationChecked: false,
|
||||
isRecording: false,
|
||||
};
|
||||
}
|
||||
@ -131,16 +165,31 @@ export default class Camera extends Component {
|
||||
this._addOnBarCodeReadListener();
|
||||
|
||||
let { captureMode } = convertNativeProps({ captureMode: this.props.captureMode });
|
||||
let hasVideoAndAudio =
|
||||
this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video;
|
||||
let check = hasVideoAndAudio
|
||||
? Camera.checkDeviceAuthorizationStatus
|
||||
: Camera.checkVideoAuthorizationStatus;
|
||||
let hasVideoAndAudio = this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video;
|
||||
|
||||
if (Platform.OS === 'ios') {
|
||||
|
||||
let check = hasVideoAndAudio ? Camera.checkDeviceAuthorizationStatus : Camera.checkVideoAuthorizationStatus;
|
||||
|
||||
if (check) {
|
||||
const isAuthorized = await check();
|
||||
this.setState({ isAuthorized });
|
||||
if (check) {
|
||||
const isAuthorized = await check();
|
||||
this.setState({ isAuthorized, isAuthorizationChecked: true });
|
||||
}
|
||||
|
||||
} else if (Platform.OS === 'android') {
|
||||
|
||||
const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA, {
|
||||
title: this.props.permissionDialogTitle,
|
||||
message: this.props.permissionDialogMessage,
|
||||
}
|
||||
);
|
||||
|
||||
this.setState({ isAuthorized: granted === PermissionsAndroid.RESULTS.GRANTED, isAuthorizationChecked: true });
|
||||
} else {
|
||||
|
||||
this.setState({ isAuthorized: true, isAuthorizationChecked: true })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
@ -181,7 +230,13 @@ export default class Camera extends Component {
|
||||
const style = [styles.base, this.props.style];
|
||||
const nativeProps = convertNativeProps(this.props);
|
||||
|
||||
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
|
||||
if(this.state.isAuthorized) {
|
||||
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
|
||||
} else if (!this.state.isAuthorizationChecked) {
|
||||
return this.props.pendingAuthorizationView
|
||||
} else {
|
||||
return this.props.notAuthorizedView
|
||||
}
|
||||
}
|
||||
|
||||
_onBarCodeRead = data => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user