Aperture: Introduce CameraState enum
This lets us keep track of the state of camera without using multiple
variables.
Change-Id: Idee89d4caad8653c0a14e0f33d22aa1fd81817cc
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index 8e5e06e..e73995d 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -79,6 +79,7 @@
import org.lineageos.aperture.utils.CameraFacing
import org.lineageos.aperture.utils.CameraMode
import org.lineageos.aperture.utils.CameraSoundsUtils
+import org.lineageos.aperture.utils.CameraState
import org.lineageos.aperture.utils.GridMode
import org.lineageos.aperture.utils.PhysicalCamera
import org.lineageos.aperture.utils.StorageUtils
@@ -130,6 +131,8 @@
private val cameraMode: CameraMode
get() = sharedPreferences.lastCameraMode
+ private lateinit var cameraState: CameraState
+
private lateinit var camera: PhysicalCamera
private lateinit var audioConfig: AudioConfig
@@ -141,7 +144,6 @@
get() = sharedPreferences.photoEffect
private lateinit var supportedExtensionModes: List<Int>
- private var isTakingPhoto: Boolean = false
private var tookSomething: Boolean = false
private var viewFinderTouchEvent: MotionEvent? = null
@@ -150,7 +152,6 @@
get() = sharedPreferences.videoQuality
private var recording: Recording? = null
private val recordingLock = Mutex()
- private var recordingPaused = false
private val sharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(this)
@@ -366,10 +367,10 @@
flipCameraButton.setOnClickListener { flipCamera() }
videoRecordingStateButton.setOnClickListener {
- if (recordingPaused) {
- recording?.resume()
- } else {
- recording?.pause()
+ when (cameraState) {
+ CameraState.RECORDING_VIDEO -> recording?.pause()
+ CameraState.RECORDING_VIDEO_PAUSED -> recording?.resume()
+ else -> {}
}
}
@@ -522,11 +523,11 @@
private fun takePhoto() {
// Bail out if a photo is already being taken
- if (isTakingPhoto) {
+ if (cameraState == CameraState.TAKING_PHOTO) {
return
}
- isTakingPhoto = true
+ cameraState = CameraState.TAKING_PHOTO
shutterButton.isEnabled = false
// Create output options object which contains file + metadata
@@ -545,7 +546,7 @@
object : ImageCapture.OnImageSavedCallback {
override fun onError(exc: ImageCaptureException) {
Log.e(LOG_TAG, "Photo capture failed: ${exc.message}", exc)
- isTakingPhoto = false
+ cameraState = CameraState.IDLE
shutterButton.isEnabled = true
}
@@ -560,7 +561,7 @@
sharedPreferences.lastSavedUri = output.savedUri
updateGalleryButton(output.savedUri)
Log.d(LOG_TAG, "Photo capture succeeded: ${output.savedUri}")
- isTakingPhoto = false
+ cameraState = CameraState.IDLE
shutterButton.isEnabled = true
tookSomething = true
}
@@ -575,6 +576,9 @@
return
}
+ // Disallow state changes while we are about to prepare for recording video
+ cameraState = CameraState.PRE_RECORDING_VIDEO
+
// Create output options object which contains file + metadata
val outputOptions = StorageUtils.getVideoMediaStoreOutputOptions(contentResolver, location)
@@ -609,15 +613,15 @@
when (it) {
is VideoRecordEvent.Start -> runOnUiThread {
- recordingPaused = false
+ cameraState = CameraState.RECORDING_VIDEO
startVideoRecordingStateAnimation(VideoRecordingStateAnimation.Init)
}
is VideoRecordEvent.Pause -> runOnUiThread {
- recordingPaused = true
+ cameraState = CameraState.RECORDING_VIDEO_PAUSED
startVideoRecordingStateAnimation(VideoRecordingStateAnimation.ResumeToPause)
}
is VideoRecordEvent.Resume -> runOnUiThread {
- recordingPaused = false
+ cameraState = CameraState.RECORDING_VIDEO
startVideoRecordingStateAnimation(VideoRecordingStateAnimation.PauseToResume)
}
is VideoRecordEvent.Status -> runOnUiThread {
@@ -635,6 +639,7 @@
Log.d(LOG_TAG, "Video capture succeeded: ${it.outputResults.outputUri}")
tookSomething = true
}
+ cameraState = CameraState.IDLE
recording = null
}
}
@@ -644,14 +649,7 @@
/**
* Check if we can reinitialize the camera use cases
*/
- private fun canRestartCamera() = when (cameraMode) {
- // Disallow camera restart if we're taking a photo or if timer is running
- CameraMode.PHOTO -> !isTakingPhoto && !countDownView.isVisible
- // Disallow camera restart if a recording in progress or if timer is running
- CameraMode.VIDEO -> !cameraController.isRecording && !countDownView.isVisible
- // Otherwise, allow camera restart
- else -> true
- }
+ private fun canRestartCamera() = cameraState == CameraState.IDLE && !countDownView.isVisible
/**
* Rebind cameraProvider use cases
@@ -660,7 +658,7 @@
// Unbind previous use cases
cameraController.unbind()
- isTakingPhoto = false
+ cameraState = CameraState.IDLE
// Hide grid until preview is ready
gridView.alpha = 0f
diff --git a/app/src/main/java/org/lineageos/aperture/utils/CameraState.kt b/app/src/main/java/org/lineageos/aperture/utils/CameraState.kt
new file mode 100644
index 0000000..79bc168
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/utils/CameraState.kt
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2022 The LineageOS Project
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture.utils
+
+enum class CameraState {
+ IDLE,
+ TAKING_PHOTO,
+ PRE_RECORDING_VIDEO,
+ RECORDING_VIDEO,
+ RECORDING_VIDEO_PAUSED,
+}