Skip to content

Commit

Permalink
add: launchCamera.js and launchCamera.android.js
Browse files Browse the repository at this point in the history
When an user installs E.cash on Android they've automatically granted camera permission
since it's included in the manifest, but if they ever revoke the Permission we
need to check and request it again as the image-picker library does not handle this
  • Loading branch information
kidroca committed Jun 10, 2021
1 parent 2fe0b2b commit dc447db
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/AttachmentPicker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import React, {Component} from 'react';
import {Alert, Linking, View} from 'react-native';
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
import {launchImageLibrary} from 'react-native-image-picker';
import RNDocumentPicker from 'react-native-document-picker';
import basePropTypes from './AttachmentPickerPropTypes';
import styles from '../../styles/styles';
Expand All @@ -13,6 +13,7 @@ import {Camera, Gallery, Paperclip} from '../Icon/Expensicons';
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import compose from '../../libs/compose';
import launchCamera from './launchCamera';

const propTypes = {
...basePropTypes,
Expand Down
32 changes: 32 additions & 0 deletions src/components/AttachmentPicker/launchCamera.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {PermissionsAndroid} from 'react-native';
import {launchCamera} from 'react-native-image-picker';

/**
* Launching the camera for Android involves checking for permissions
* And only then starting the camera
* If the user deny permission the callback will be called with an error response
* in the same format as the error returned by react-native-image-picker
* @param {CameraOptions} options
* @param {function} callback - callback called with the result
*/
export default function launchCameraAndroid(options, callback) {
// Checks current camera permissions and prompts the user in case they aren't granted
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA)
.then((permission) => {
if (permission !== PermissionsAndroid.RESULTS.GRANTED) {
const error = new Error('User did not grant permissions');
error.errorCode = 'permission';
throw error;
}

launchCamera(options, callback);
})
.catch((error) => {
/* Intercept the permission error as well as any other errors and call the callback
* follow the same pattern expected for image picker results */
callback({
errorMessage: error.message,
errorCode: error.errorCode || 'others',
});
});
}
3 changes: 3 additions & 0 deletions src/components/AttachmentPicker/launchCamera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {launchCamera} from 'react-native-image-picker';

export default launchCamera;

0 comments on commit dc447db

Please sign in to comment.