Aperture: Fix flash button not being available at app start
* CameraController.cameraInfo is still initializing while we're setting up UI, resulting in hasFlashUnit() returning false
* Get a CameraInfo object from CameraProvider, will be useful in the future once we'll move to camera ID based camera selection
Change-Id: Iae8cfbbdeb063934c756d96ba2708862fc5ed0e6
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index 9a6925e..eca7d49 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -58,6 +58,7 @@
import org.lineageos.aperture.utils.CameraFacing
import org.lineageos.aperture.utils.CameraMode
import org.lineageos.aperture.utils.GridMode
+import org.lineageos.aperture.utils.PhysicalCamera
import org.lineageos.aperture.utils.StorageUtils
import org.lineageos.aperture.utils.TimeUtils
import java.io.FileNotFoundException
@@ -97,6 +98,8 @@
private lateinit var cameraMode: CameraMode
+ private lateinit var camera: PhysicalCamera
+
private var extensionMode = ExtensionMode.NONE
private var extensionModeIndex = 0
private var supportedExtensionModes = listOf(extensionMode)
@@ -406,6 +409,10 @@
// Bind camera controller to lifecycle
cameraController.bindToLifecycle(this)
+ // Get a stable reference to CameraInfo
+ // We can hardcode the first one in the filter as long as we use DEFAULT_*_CAMERA
+ camera = PhysicalCamera(cameraSelector.filter(cameraProvider.availableCameraInfos)[0])
+
// Restore settings that can be set on the fly
setGridMode(sharedPreferences.lastGridMode)
setFlashMode(sharedPreferences.photoFlashMode)
@@ -625,9 +632,9 @@
/**
* Update the flash mode button icon based on the value set in imageCapture
*/
+ @androidx.camera.camera2.interop.ExperimentalCamera2Interop
private fun updateFlashModeIcon() {
- flashButton.isVisible =
- cameraMode == CameraMode.PHOTO && cameraController.cameraInfo?.hasFlashUnit() == true
+ flashButton.isVisible = cameraMode == CameraMode.PHOTO && camera.hasFlashUnit() == true
flashButton.setImageDrawable(
ContextCompat.getDrawable(
this,
@@ -644,6 +651,7 @@
/**
* Set the specified flash mode, saving the value to shared prefs and updating the icon
*/
+ @androidx.camera.camera2.interop.ExperimentalCamera2Interop
private fun setFlashMode(flashMode: Int) {
cameraController.imageCaptureFlashMode = flashMode
updateFlashModeIcon()
@@ -654,6 +662,7 @@
/**
* Cycle flash mode between auto, on and off
*/
+ @androidx.camera.camera2.interop.ExperimentalCamera2Interop
private fun cycleFlashMode() {
setFlashMode(
if (cameraController.imageCaptureFlashMode >= ImageCapture.FLASH_MODE_OFF) ImageCapture.FLASH_MODE_AUTO
diff --git a/app/src/main/java/org/lineageos/aperture/utils/PhysicalCamera.kt b/app/src/main/java/org/lineageos/aperture/utils/PhysicalCamera.kt
index 789d95d..7acf0c5 100644
--- a/app/src/main/java/org/lineageos/aperture/utils/PhysicalCamera.kt
+++ b/app/src/main/java/org/lineageos/aperture/utils/PhysicalCamera.kt
@@ -49,4 +49,11 @@
else -> CameraFacing.UNKNOWN
}
}
+
+ /**
+ * Return if flash is available or not
+ */
+ fun hasFlashUnit(): Boolean {
+ return cameraInfo.hasFlashUnit()
+ }
}