diff --git a/Example/Example.js b/Example/Example.js index 88820de..8470596 100644 --- a/Example/Example.js +++ b/Example/Example.js @@ -186,6 +186,8 @@ export default class Example extends React.Component { onZoomChanged={() => {}} defaultTouchToFocus mirrorImage={false} + permissionDialogTitle="Sample title" + permissionDialogMessage="Sample dialog message" /> 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. diff --git a/src/index.js b/src/index.js index 4c3fcc7..7000596 100644 --- a/src/index.js +++ b/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: ( + + + Camera not authorized + + + ), + pendingAuthorizationView: ( + + + + ), }; 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 ; + if(this.state.isAuthorized) { + return ; + } else if (!this.state.isAuthorizationChecked) { + return this.props.pendingAuthorizationView + } else { + return this.props.notAuthorizedView + } } _onBarCodeRead = data => {