Aperture: Fix re-enabling microphone after video has started

Change-Id: I79f47030c0ae4335bd8a687ce07aeb4466411658
diff --git a/app/src/main/java/androidx/camera/video/Recording.kt b/app/src/main/java/androidx/camera/video/Recording.kt
new file mode 100644
index 0000000..391c4fd
--- /dev/null
+++ b/app/src/main/java/androidx/camera/video/Recording.kt
@@ -0,0 +1,36 @@
+/*
+ * SPDX-FileCopyrightText: 2023 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package androidx.camera.video
+
+import androidx.camera.video.internal.audio.AudioSource
+import androidx.camera.video.internal.audio.muted
+
+/**
+ * Get the [Recording]'s [AudioSource]. Returns null if the recording started with muted mic.
+ */
+val Recording.audioSource: AudioSource?
+    get() = (Recording::class.java.getDeclaredField("mRecorder").apply {
+        isAccessible = true
+    }.get(this) as Recorder).mAudioSource
+
+/**
+ * Get the [AudioSource] status
+ * @see Recording.audioSource
+ */
+val Recording.isAudioSourceConfigured
+    get() = audioSource != null
+
+/**
+ * [Recording] muted property. Changing this property will also affect running recordings.
+ * If the [Recording]'s [AudioSource] is null, getting this value will
+ * return true and setting it is a no-op.
+ */
+var Recording.muted
+    get() = audioSource?.muted != false
+    @Suppress("RestrictedApi")
+    set(value) {
+        audioSource?.mute(value)
+    }
diff --git a/app/src/main/java/androidx/camera/video/RecordingExt.kt b/app/src/main/java/androidx/camera/video/RecordingExt.kt
deleted file mode 100644
index 282fb2d..0000000
--- a/app/src/main/java/androidx/camera/video/RecordingExt.kt
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2023 The LineageOS Project
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package androidx.camera.video
-
-import androidx.camera.video.internal.audio.muted
-
-private val Recording.audioSource
-    get() = (Recording::class.java.getDeclaredField("mRecorder").apply {
-        isAccessible = true
-    }.get(this) as Recorder).mAudioSource
-
-var Recording.muted
-    get() = audioSource.muted
-    @Suppress("RestrictedApi")
-    set(value) {
-        audioSource.mute(value)
-    }
diff --git a/app/src/main/java/androidx/camera/video/internal/audio/AudioSourceExt.kt b/app/src/main/java/androidx/camera/video/internal/audio/AudioSource.kt
similarity index 100%
rename from app/src/main/java/androidx/camera/video/internal/audio/AudioSourceExt.kt
rename to app/src/main/java/androidx/camera/video/internal/audio/AudioSource.kt
diff --git a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index e0e67fd..71d4d64 100644
--- a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -58,6 +58,7 @@
 import androidx.camera.video.Quality
 import androidx.camera.video.QualitySelector
 import androidx.camera.video.VideoRecordEvent
+import androidx.camera.video.isAudioSourceConfigured
 import androidx.camera.video.muted
 import androidx.camera.view.CameraController
 import androidx.camera.view.LifecycleCameraController
@@ -219,7 +220,6 @@
     private var videoQuality by nonNullablePropertyDelegate { model.videoQuality }
     private var videoFrameRate by nullablePropertyDelegate { model.videoFrameRate }
     private var videoMicMode by nonNullablePropertyDelegate { model.videoMicMode }
-    private var videoAudioConfig by nonNullablePropertyDelegate { model.videoAudioConfig }
     private var videoRecording by nullablePropertyDelegate { model.videoRecording }
 
     private lateinit var initialCameraFacing: CameraFacing
@@ -239,6 +239,7 @@
         get() = camera.supportedVideoQualities.getOrDefault(
             videoQuality, setOf()
         )
+    private lateinit var videoAudioConfig: AudioConfig
 
     // QR
     private val imageAnalyzer by lazy { QrImageAnalyzer(this) }
@@ -1152,8 +1153,8 @@
             )
         }
 
-        // Observe video AudioConfig
-        model.videoAudioConfig.observe(this) {
+        // Observe video recording
+        model.videoRecording.observe(this) {
             // Update secondary bar buttons
             updateSecondaryBarButtons()
         }
@@ -1909,7 +1910,7 @@
             val cameraState = model.cameraState.value ?: return@runOnUiThread
             val photoCaptureMode = model.photoCaptureMode.value ?: return@runOnUiThread
             val videoQuality = model.videoQuality.value ?: return@runOnUiThread
-            val videoAudioConfig = model.videoAudioConfig.value ?: return@runOnUiThread
+            val videoRecording = model.videoRecording.value
 
             val supportedVideoQualities = camera.supportedVideoQualities
             val supportedVideoFrameRates = supportedVideoQualities.getOrDefault(
@@ -1925,7 +1926,8 @@
                 cameraState == CameraState.IDLE && supportedVideoQualities.size > 1
             videoFrameRateButton.isEnabled =
                 cameraState == CameraState.IDLE && supportedVideoFrameRates.size > 1
-            micButton.isEnabled = cameraState == CameraState.IDLE || videoAudioConfig.audioEnabled
+            micButton.isEnabled =
+                cameraState == CameraState.IDLE || videoRecording?.isAudioSourceConfigured == true
         }
     }
 
@@ -2079,7 +2081,11 @@
      */
     @Suppress("MissingPermission")
     private fun setMicrophoneMode(microphoneMode: Boolean) {
-        videoAudioConfig = AudioConfig.create(microphoneMode)
+        videoAudioConfig = if (microphoneMode) {
+            AudioConfig.create(true)
+        } else {
+            AudioConfig.AUDIO_DISABLED
+        }
         videoRecording?.muted = !microphoneMode
 
         videoMicMode = microphoneMode
diff --git a/app/src/main/java/org/lineageos/aperture/camera/CameraViewModel.kt b/app/src/main/java/org/lineageos/aperture/camera/CameraViewModel.kt
index 4c59823..dfafed6 100644
--- a/app/src/main/java/org/lineageos/aperture/camera/CameraViewModel.kt
+++ b/app/src/main/java/org/lineageos/aperture/camera/CameraViewModel.kt
@@ -7,7 +7,6 @@
 
 import androidx.camera.video.Quality
 import androidx.camera.video.Recording
-import androidx.camera.view.video.AudioConfig
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
 import org.lineageos.aperture.utils.GridMode
@@ -98,11 +97,6 @@
     val videoMicMode = MutableLiveData<Boolean>()
 
     /**
-     * Video [AudioConfig].
-     */
-    val videoAudioConfig = MutableLiveData<AudioConfig>()
-
-    /**
      * Video [Recording].
      */
     val videoRecording = MutableLiveData<Recording?>()