[ENHANCEMENT] Add permission handling for android 6+

This commit is contained in:
Oscar Franco 2018-01-06 08:54:40 +01:00
parent 7fb27917d2
commit 6749e9e19b
4 changed files with 43 additions and 13 deletions

View File

@ -186,6 +186,8 @@ export default class Example extends React.Component {
onZoomChanged={() => {}} onZoomChanged={() => {}}
defaultTouchToFocus defaultTouchToFocus
mirrorImage={false} mirrorImage={false}
permissionDialogTitle="Sample title"
permissionDialogMessage="Sample dialog message"
/> />
<View style={[styles.overlay, styles.topOverlay]}> <View style={[styles.overlay, styles.topOverlay]}>
<TouchableOpacity <TouchableOpacity

View File

@ -322,6 +322,14 @@ 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. 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 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 change the content of the dialog prompt requesting permissions.
#### `mirrorImage` #### `mirrorImage`
If set to `true`, the image returned will be mirrored. If set to `true`, the image returned will be mirrored.

View File

@ -432,13 +432,9 @@ public class RCTCamera {
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && _cameraInfos.get(RCTCameraModule.RCT_CAMERA_TYPE_FRONT) == null) { if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT && _cameraInfos.get(RCTCameraModule.RCT_CAMERA_TYPE_FRONT) == null) {
_cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_FRONT, new CameraInfoWrapper(info)); _cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_FRONT, new CameraInfoWrapper(info));
_cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_FRONT, i); _cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_FRONT, i);
acquireCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_FRONT);
releaseCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_FRONT);
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK && _cameraInfos.get(RCTCameraModule.RCT_CAMERA_TYPE_BACK) == null) { } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK && _cameraInfos.get(RCTCameraModule.RCT_CAMERA_TYPE_BACK) == null) {
_cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, new CameraInfoWrapper(info)); _cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, new CameraInfoWrapper(info));
_cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, i); _cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, i);
acquireCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_BACK);
releaseCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_BACK);
} }
} }
} }

View File

@ -8,6 +8,7 @@ import {
StyleSheet, StyleSheet,
requireNativeComponent, requireNativeComponent,
ViewPropTypes, ViewPropTypes,
PermissionsAndroid,
} from 'react-native'; } from 'react-native';
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule; const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
@ -91,6 +92,8 @@ export default class Camera extends Component {
playSoundOnCapture: PropTypes.bool, playSoundOnCapture: PropTypes.bool,
torchMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), torchMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
type: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), type: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
permissionDialogTitle: PropTypes.string,
permissionDialogMessage: PropTypes.string
}; };
static defaultProps = { static defaultProps = {
@ -108,6 +111,8 @@ export default class Camera extends Component {
torchMode: CameraManager.TorchMode.off, torchMode: CameraManager.TorchMode.off,
mirrorImage: false, mirrorImage: false,
barCodeTypes: Object.values(CameraManager.BarCodeType), barCodeTypes: Object.values(CameraManager.BarCodeType),
permissionDialogTitle: '',
permissionDialogMessage: ''
}; };
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus; static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
@ -131,16 +136,31 @@ export default class Camera extends Component {
this._addOnBarCodeReadListener(); this._addOnBarCodeReadListener();
let { captureMode } = convertNativeProps({ captureMode: this.props.captureMode }); let { captureMode } = convertNativeProps({ captureMode: this.props.captureMode });
let hasVideoAndAudio = let hasVideoAndAudio = this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video;
this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video;
let check = hasVideoAndAudio if (Platform.OS === 'ios') {
? Camera.checkDeviceAuthorizationStatus
: Camera.checkVideoAuthorizationStatus; let check = hasVideoAndAudio ? Camera.checkDeviceAuthorizationStatus : Camera.checkVideoAuthorizationStatus;
if (check) { if (check) {
const isAuthorized = await check(); const isAuthorized = await check();
this.setState({ isAuthorized }); this.setState({ isAuthorized });
} }
} 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 });
} else {
this.setState({ isAuthorized: true })
}
} }
componentWillUnmount() { componentWillUnmount() {
@ -181,7 +201,11 @@ export default class Camera extends Component {
const style = [styles.base, this.props.style]; const style = [styles.base, this.props.style];
const nativeProps = convertNativeProps(this.props); const nativeProps = convertNativeProps(this.props);
if(this.state.isAuthorized) {
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />; return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
} else {
return null
}
} }
_onBarCodeRead = data => { _onBarCodeRead = data => {