Aperture: Allow muting microphone during ongoing recording

Change-Id: I64c0c974f7aa391f8316fdcf5a42b34a285cb162
diff --git a/app/src/main/java/androidx/camera/video/RecordingExt.kt b/app/src/main/java/androidx/camera/video/RecordingExt.kt
new file mode 100644
index 0000000..444ce72
--- /dev/null
+++ b/app/src/main/java/androidx/camera/video/RecordingExt.kt
@@ -0,0 +1,19 @@
+/*
+ * 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
+    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/AudioSourceExt.kt
new file mode 100644
index 0000000..a0d6178
--- /dev/null
+++ b/app/src/main/java/androidx/camera/video/internal/audio/AudioSourceExt.kt
@@ -0,0 +1,9 @@
+/*
+ * SPDX-FileCopyrightText: 2023 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package androidx.camera.video.internal.audio
+
+val AudioSource.muted
+    get() = mMuted
diff --git a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index a818693..9bdbe85 100644
--- a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -49,6 +49,7 @@
 import androidx.camera.video.Quality
 import androidx.camera.video.Recording
 import androidx.camera.video.VideoRecordEvent
+import androidx.camera.video.muted
 import androidx.camera.view.CameraController
 import androidx.camera.view.LifecycleCameraController
 import androidx.camera.view.PreviewView
@@ -1288,7 +1289,7 @@
             // Torch mode can be toggled at any time
             flashButton.isEnabled =
                 cameraMode != CameraMode.PHOTO || cameraState == CameraState.IDLE
-            micButton.isEnabled = cameraState == CameraState.IDLE
+            micButton.isEnabled = cameraState == CameraState.IDLE || audioConfig.audioEnabled
             settingsButton.isEnabled = cameraState == CameraState.IDLE
         }
     }
@@ -1545,7 +1546,7 @@
     private fun updateMicrophoneModeIcon() {
         micButton.isVisible = cameraMode == CameraMode.VIDEO
 
-        audioConfig.audioEnabled.let {
+        sharedPreferences.lastMicMode.let {
             micButton.setCompoundDrawablesWithIntrinsicBounds(
                 0,
                 if (it) R.drawable.ic_mic_on else R.drawable.ic_mic_off,
@@ -1560,7 +1561,7 @@
      * Toggles microphone during video recording
      */
     private fun toggleMicrophoneMode() {
-        setMicrophoneMode(!audioConfig.audioEnabled)
+        setMicrophoneMode(!sharedPreferences.lastMicMode)
     }
 
     /**
@@ -1569,9 +1570,9 @@
     @SuppressLint("MissingPermission")
     private fun setMicrophoneMode(microphoneMode: Boolean) {
         audioConfig = AudioConfig.create(microphoneMode)
-        updateMicrophoneModeIcon()
-
+        recording?.muted = !microphoneMode
         sharedPreferences.lastMicMode = microphoneMode
+        updateMicrophoneModeIcon()
     }
 
     /**