From 6749e9e19be917847df8ae7042dba75f25086123 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sat, 6 Jan 2018 08:54:40 +0100 Subject: [PATCH 1/3] [ENHANCEMENT] Add permission handling for android 6+ --- Example/Example.js | 2 + README.md | 8 ++++ .../com/lwansbrough/RCTCamera/RCTCamera.java | 4 -- src/index.js | 42 +++++++++++++++---- 4 files changed, 43 insertions(+), 13 deletions(-) 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" /> ; + if(this.state.isAuthorized) { + return ; + } else { + return null + } } _onBarCodeRead = data => { From 59ce880b7e420309e44652b6337871e2dbe0c497 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sat, 6 Jan 2018 21:30:49 +0100 Subject: [PATCH 2/3] [ENHANCEMENT] Restore accidental change to RCTCamera module and implement custom views for checking permissions and permission denied states --- .../com/lwansbrough/RCTCamera/RCTCamera.java | 4 ++ src/index.js | 43 ++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java index cbad030..e433f9d 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java @@ -432,9 +432,13 @@ public class RCTCamera { 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)); _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) { _cameraInfos.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, new CameraInfoWrapper(info)); _cameraTypeToIndex.put(RCTCameraModule.RCT_CAMERA_TYPE_BACK, i); + acquireCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_BACK); + releaseCameraInstance(RCTCameraModule.RCT_CAMERA_TYPE_BACK); } } } diff --git a/src/index.js b/src/index.js index 59d7fad..7000596 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,9 @@ import { requireNativeComponent, ViewPropTypes, PermissionsAndroid, + ActivityIndicator, + View, + Text, } from 'react-native'; const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule; @@ -93,7 +96,9 @@ export default class Camera extends Component { torchMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), type: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), permissionDialogTitle: PropTypes.string, - permissionDialogMessage: PropTypes.string + permissionDialogMessage: PropTypes.string, + notAuthorizedView: PropTypes.element, + pendingAuthorizationView: PropTypes.element, }; static defaultProps = { @@ -112,7 +117,30 @@ export default class Camera extends Component { mirrorImage: false, barCodeTypes: Object.values(CameraManager.BarCodeType), permissionDialogTitle: '', - permissionDialogMessage: '' + permissionDialogMessage: '', + notAuthorizedView: ( + + + Camera not authorized + + + ), + pendingAuthorizationView: ( + + + + ), }; static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus; @@ -128,6 +156,7 @@ export default class Camera extends Component { super(); this.state = { isAuthorized: false, + isAuthorizationChecked: false, isRecording: false, }; } @@ -144,7 +173,7 @@ export default class Camera extends Component { if (check) { const isAuthorized = await check(); - this.setState({ isAuthorized }); + this.setState({ isAuthorized, isAuthorizationChecked: true }); } } else if (Platform.OS === 'android') { @@ -155,10 +184,10 @@ export default class Camera extends Component { } ); - this.setState({ isAuthorized: granted === PermissionsAndroid.RESULTS.GRANTED }); + this.setState({ isAuthorized: granted === PermissionsAndroid.RESULTS.GRANTED, isAuthorizationChecked: true }); } else { - this.setState({ isAuthorized: true }) + this.setState({ isAuthorized: true, isAuthorizationChecked: true }) } } @@ -203,8 +232,10 @@ export default class Camera extends Component { if(this.state.isAuthorized) { return ; + } else if (!this.state.isAuthorizationChecked) { + return this.props.pendingAuthorizationView } else { - return null + return this.props.notAuthorizedView } } From 7610ddd8e88626bd61cf789674529b57a96662a6 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 7 Jan 2018 08:55:26 +0100 Subject: [PATCH 3/3] [ENHANCEMENT] Add new properties to README file --- README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 23a17f7..40d34f0 100644 --- a/README.md +++ b/README.md @@ -324,11 +324,23 @@ If set to `true`, the device will not sleep while the camera preview is visible. #### `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. +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 change the content of the dialog prompt requesting permissions. +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 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`