Aperture: Improve vendor extensions handling

* Make getSupportedModes a ExtensionsManager method
* Always set supportedExtensionModes, we can query them regardless of the camera mode
* Restore extensionModeIndex as well (fixes wrong icon on app reopening)

Change-Id: I6ffca1c56f4ab23b5c2cb8f31d1e5d82bb1f9c3b
diff --git a/app/src/main/java/org/lineageos/aperture/ExtensionModeExt.kt b/app/src/main/java/org/lineageos/aperture/ExtensionModeExt.kt
new file mode 100644
index 0000000..3b35916
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/ExtensionModeExt.kt
@@ -0,0 +1,10 @@
+package org.lineageos.aperture
+
+import androidx.camera.core.CameraSelector
+import androidx.camera.extensions.ExtensionMode
+import androidx.camera.extensions.ExtensionsManager
+
+val extensionModes = IntProgression.fromClosedRange(ExtensionMode.NONE, ExtensionMode.AUTO, 1)
+
+internal fun ExtensionsManager.getSupportedModes(cameraSelector: CameraSelector) =
+    extensionModes.filter { isExtensionAvailable(cameraSelector, it) }
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index 8a41d2e..ed9995f 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -41,7 +41,6 @@
 import org.lineageos.aperture.ui.GridView
 import org.lineageos.aperture.utils.CameraFacing
 import org.lineageos.aperture.utils.CameraMode
-import org.lineageos.aperture.utils.ExtensionModeExt
 import org.lineageos.aperture.utils.GridMode
 import org.lineageos.aperture.utils.StorageUtils
 import org.lineageos.aperture.utils.TimeUtils
@@ -318,8 +317,14 @@
             else -> CameraSelector.DEFAULT_BACK_CAMERA
         }
 
+        // Get the supported vendor extensions for the given camera selector
+        supportedExtensionModes = extensionsManager.getSupportedModes(cameraSelector)
+
         // Get the user selected effect
         extensionMode = sharedPreferences.getPhotoEffect()
+        if (!supportedExtensionModes.contains(extensionMode))
+            extensionMode = ExtensionMode.NONE
+        extensionModeIndex = supportedExtensionModes.indexOf(extensionMode)
 
         // Initialize the use case we want
         cameraMode = sharedPreferences.getLastCameraMode()
@@ -330,9 +335,6 @@
 
         // Only photo mode supports vendor extensions for now
         if (cameraMode == CameraMode.PHOTO) {
-            // Fetch the supported extensions
-            supportedExtensionModes =
-                ExtensionModeExt.getSupportedModes(extensionsManager, cameraSelector)
             // Select the extension
             if (supportedExtensionModes.contains(extensionMode)) {
                 cameraSelector = extensionsManager.getExtensionEnabledCameraSelector(
diff --git a/app/src/main/java/org/lineageos/aperture/utils/ExtensionModeExt.kt b/app/src/main/java/org/lineageos/aperture/utils/ExtensionModeExt.kt
deleted file mode 100644
index 77f622e..0000000
--- a/app/src/main/java/org/lineageos/aperture/utils/ExtensionModeExt.kt
+++ /dev/null
@@ -1,18 +0,0 @@
-package org.lineageos.aperture.utils
-
-import androidx.camera.core.CameraSelector
-import androidx.camera.extensions.ExtensionMode
-import androidx.camera.extensions.ExtensionsManager
-
-object ExtensionModeExt {
-    private val extensionModes =
-        IntProgression.fromClosedRange(ExtensionMode.NONE, ExtensionMode.AUTO, 1)
-
-    fun getSupportedModes(
-        extensionsManager: ExtensionsManager,
-        cameraSelector: CameraSelector
-    ) = extensionModes.filter {
-        extensionsManager.isExtensionAvailable(cameraSelector, it)
-    }
-}
-