properly ensure RECORD_AUDIO permission is requested during camera initialisation

- since allowBackup is set to true in the manifest,
- it is possible for the app to be launch after a fresh install with mIsInPhotoMode=true in MainActivity
- this commit ensure the audio permission is requested at this point
- if the user denies, then it is initialised in photo mode instead
This commit is contained in:
darthpaul 2022-07-10 18:57:01 +01:00
parent d5e1d61d02
commit e1f292692a
1 changed files with 31 additions and 9 deletions

View File

@ -43,7 +43,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
private var mPreview: MyPreview? = null private var mPreview: MyPreview? = null
private var mPreviewUri: Uri? = null private var mPreviewUri: Uri? = null
private var mIsInPhotoMode = false private var mIsInPhotoMode = true
private var mIsCameraAvailable = false private var mIsCameraAvailable = false
private var mIsHardwareShutterHandled = false private var mIsHardwareShutterHandled = false
private var mCurrVideoRecTimer = 0 private var mCurrVideoRecTimer = 0
@ -154,11 +154,23 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
} }
private fun tryInitCamera() { private fun tryInitCamera() {
handlePermission(PERMISSION_CAMERA) { handlePermission(PERMISSION_CAMERA) { grantedCameraPermission ->
if (it) { if (grantedCameraPermission) {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) { grantedStoragePermission ->
if (it) { if (grantedStoragePermission) {
initializeCamera() if (mIsInPhotoMode) {
initializeCamera()
} else {
handlePermission(PERMISSION_RECORD_AUDIO) { grantedRecordAudioPermission ->
if (grantedRecordAudioPermission) {
initializeCamera()
} else {
toast(R.string.no_audio_permissions)
togglePhotoVideoMode()
tryInitCamera()
}
}
}
} else { } else {
toast(R.string.no_storage_permissions) toast(R.string.no_storage_permissions)
finish() finish()
@ -325,12 +337,16 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
mPreview?.setFlashlightState(FLASH_OFF) mPreview?.setFlashlightState(FLASH_OFF)
hideTimer() hideTimer()
mIsInPhotoMode = !mIsInPhotoMode togglePhotoVideoMode()
config.initPhotoMode = mIsInPhotoMode
checkButtons() checkButtons()
toggleBottomButtons(false) toggleBottomButtons(false)
} }
private fun togglePhotoVideoMode() {
mIsInPhotoMode = !mIsInPhotoMode
config.initPhotoMode = mIsInPhotoMode
}
private fun checkButtons() { private fun checkButtons() {
if (mIsInPhotoMode) { if (mIsInPhotoMode) {
initPhotoMode() initPhotoMode()
@ -456,7 +472,13 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
} }
} }
private fun hasStorageAndCameraPermissions() = hasPermission(PERMISSION_WRITE_STORAGE) && hasPermission(PERMISSION_CAMERA) private fun hasStorageAndCameraPermissions(): Boolean {
return if (mIsInPhotoMode) {
hasPermission(PERMISSION_WRITE_STORAGE) && hasPermission(PERMISSION_CAMERA)
} else {
hasPermission(PERMISSION_WRITE_STORAGE) && hasPermission(PERMISSION_CAMERA) && hasPermission(PERMISSION_RECORD_AUDIO)
}
}
private fun setupOrientationEventListener() { private fun setupOrientationEventListener() {
mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {