-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Fail to request camera permission in Scan receipt screen #26231
Merged
AndrewGable
merged 8 commits into
Expensify:main
from
tienifr:fix/24160-fail-to-request-camera-permission-in-scan-receipt-screen
Sep 5, 2023
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da87d1b
fix: fail to request camera permission in scan receipt screen
tienifr 72bc75f
fix request permission on android
tienifr d16d979
use predefined constants
tienifr 0d8d9ee
add comment
tienifr 652d8ac
Merge branch 'main' into fix/24160
tienifr fd64c22
fix lint
tienifr 5bc697c
Merge branch 'main' into fix/24160
tienifr 6c0320f
fix lint
tienifr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/pages/iou/ReceiptSelector/CameraPermission/index.android.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {check, PERMISSIONS, request} from 'react-native-permissions'; | ||
|
||
function requestCameraPermission() { | ||
return request(PERMISSIONS.ANDROID.CAMERA); | ||
} | ||
|
||
// Android will never return blocked after a check, you have to request the permission to get the info. | ||
function getCameraPermissionStatus() { | ||
return check(PERMISSIONS.ANDROID.CAMERA); | ||
} | ||
|
||
export {requestCameraPermission, getCameraPermissionStatus}; |
11 changes: 11 additions & 0 deletions
11
src/pages/iou/ReceiptSelector/CameraPermission/index.ios.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {check, PERMISSIONS, request} from 'react-native-permissions'; | ||
|
||
function requestCameraPermission() { | ||
return request(PERMISSIONS.IOS.CAMERA); | ||
} | ||
|
||
function getCameraPermissionStatus() { | ||
return check(PERMISSIONS.IOS.CAMERA); | ||
} | ||
|
||
export {requestCameraPermission, getCameraPermissionStatus}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function requestCameraPermission() {} | ||
|
||
function getCameraPermissionStatus() {} | ||
|
||
export {requestCameraPermission, getCameraPermissionStatus}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import PropTypes from 'prop-types'; | |
import {launchImageLibrary} from 'react-native-image-picker'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import {useIsFocused} from '@react-navigation/native'; | ||
import {RESULTS} from 'react-native-permissions'; | ||
import PressableWithFeedback from '../../../components/Pressable/PressableWithFeedback'; | ||
import Icon from '../../../components/Icon'; | ||
import * as Expensicons from '../../../components/Icon/Expensicons'; | ||
|
@@ -21,6 +22,7 @@ import useLocalize from '../../../hooks/useLocalize'; | |
import ONYXKEYS from '../../../ONYXKEYS'; | ||
import Log from '../../../libs/Log'; | ||
import participantPropTypes from '../../../components/participantPropTypes'; | ||
import * as CameraPermission from './CameraPermission'; | ||
|
||
const propTypes = { | ||
/** React Navigation route */ | ||
|
@@ -99,7 +101,8 @@ function ReceiptSelector(props) { | |
|
||
const camera = useRef(null); | ||
const [flash, setFlash] = useState(false); | ||
const [permissions, setPermissions] = useState('authorized'); | ||
const [permissions, setPermissions] = useState('granted'); | ||
const isAndroidBlockedPermissionRef = useRef(false); | ||
const appState = useRef(AppState.currentState); | ||
|
||
const iouType = lodashGet(props.route, 'params.iouType', ''); | ||
|
@@ -113,7 +116,7 @@ function ReceiptSelector(props) { | |
useEffect(() => { | ||
const subscription = AppState.addEventListener('change', (nextAppState) => { | ||
if (appState.current.match(/inactive|background/) && nextAppState === 'active') { | ||
Camera.getCameraPermissionStatus().then((permissionStatus) => { | ||
CameraPermission.getCameraPermissionStatus().then((permissionStatus) => { | ||
setPermissions(permissionStatus); | ||
}); | ||
} | ||
|
@@ -156,12 +159,14 @@ function ReceiptSelector(props) { | |
}; | ||
|
||
const askForPermissions = () => { | ||
if (permissions === 'not-determined') { | ||
Camera.requestCameraPermission().then((permissionStatus) => { | ||
// Android will never return blocked after a check, you have to request the permission to get the info. | ||
if (permissions === RESULTS.BLOCKED || isAndroidBlockedPermissionRef.current) { | ||
Linking.openSettings(); | ||
} else if (permissions === RESULTS.DENIED) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from #27442. We improved the UX to avoid the permission button does nothing on first click |
||
CameraPermission.requestCameraPermission().then((permissionStatus) => { | ||
setPermissions(permissionStatus); | ||
isAndroidBlockedPermissionRef.current = permissionStatus === RESULTS.BLOCKED; | ||
}); | ||
} else { | ||
Linking.openSettings(); | ||
} | ||
}; | ||
|
||
|
@@ -219,13 +224,13 @@ function ReceiptSelector(props) { | |
}); | ||
}, [flash, iouType, props.iou, props.report, reportID, translate]); | ||
|
||
Camera.getCameraPermissionStatus().then((permissionStatus) => { | ||
CameraPermission.getCameraPermissionStatus().then((permissionStatus) => { | ||
setPermissions(permissionStatus); | ||
}); | ||
|
||
return ( | ||
<View style={styles.flex1}> | ||
{permissions !== CONST.RECEIPT.PERMISSION_AUTHORIZED && ( | ||
{permissions !== RESULTS.GRANTED && ( | ||
<View style={[styles.cameraView, styles.permissionView]}> | ||
<Hand | ||
width={CONST.RECEIPT.HAND_ICON_WIDTH} | ||
|
@@ -248,7 +253,7 @@ function ReceiptSelector(props) { | |
</PressableWithFeedback> | ||
</View> | ||
)} | ||
{permissions === CONST.RECEIPT.PERMISSION_AUTHORIZED && device == null && ( | ||
{permissions === RESULTS.GRANTED && device == null && ( | ||
<View style={[styles.cameraView]}> | ||
<ActivityIndicator | ||
size={CONST.ACTIVITY_INDICATOR_SIZE.LARGE} | ||
|
@@ -257,7 +262,7 @@ function ReceiptSelector(props) { | |
/> | ||
</View> | ||
)} | ||
{permissions === CONST.RECEIPT.PERMISSION_AUTHORIZED && device != null && ( | ||
{permissions === RESULTS.GRANTED && device != null && ( | ||
<Camera | ||
ref={camera} | ||
device={device} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also mentioned in the doc: https://github.com/zoontek/react-native-permissions/blob/a836e114ce3a180b2b23916292c79841a267d828/README.md?plain=1#L670
There's no way we can check for the
BLOCKED
status without requesting the permission first, even with react-native'sPermissionsAndroid
.Context: https://developer.android.com/training/permissions/requesting#workflow_for_requesting_permissions (point 4).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is a hack thus really looking forward to your feedback!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should put this in the description as comments. I think this is fine for the current change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the PR description, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.