Aperture: Add camera mode switch

Change-Id: I2349a36f68c3b389c65e309aba5775f68af3191d
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index 7a8f7d8..0cd3dd0 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -38,6 +38,8 @@
 class MainActivity : AppCompatActivity() {
     private lateinit var viewBinding: ActivityMainBinding
 
+    private lateinit var cameraMode: CameraMode
+
     private var imageCapture: ImageCapture? = null
 
     private var videoCapture: VideoCapture<Recorder>? = null
@@ -63,6 +65,9 @@
 
         viewBinding.flashButton.setOnClickListener { cycleFlashMode() }
 
+        viewBinding.photoModeButton.setOnClickListener { changeCameraMode(CameraMode.PHOTO) }
+        viewBinding.videoModeButton.setOnClickListener { changeCameraMode(CameraMode.VIDEO) }
+
         // Set up the listeners for take photo and video capture buttons
         viewBinding.imageCaptureButton.setOnClickListener { takePhoto() }
         viewBinding.videoCaptureButton.setOnClickListener { captureVideo() }
@@ -226,7 +231,7 @@
                 }
 
             // Initialize the use case we want
-            val cameraMode = SharedPreferencesUtils.getLastCameraMode(sharedPreferences)
+            cameraMode = SharedPreferencesUtils.getLastCameraMode(sharedPreferences)
             if (cameraMode == CameraMode.PHOTO) {
                 imageCapture = ImageCapture.Builder()
                     .setCaptureMode(SharedPreferencesUtils.getPhotoCaptureMode(sharedPreferences))
@@ -245,6 +250,7 @@
             }
 
             // Update icons from last state
+            updateCameraModeButtons()
             updateFlashModeIcon()
 
             // Select back camera as a default
@@ -265,6 +271,26 @@
     }
 
     /**
+     * Change the current camera mode and restarts the stream
+     */
+    @androidx.camera.core.ExperimentalZeroShutterLag
+    private fun changeCameraMode(cameraMode: CameraMode) {
+        if (cameraMode == this.cameraMode)
+            return
+
+        SharedPreferencesUtils.setLastCameraMode(getSharedPreferences(), cameraMode)
+        startCamera()
+    }
+
+    /**
+     * Update the camera mode buttons reflecting the current mode
+     */
+    private fun updateCameraModeButtons() {
+        viewBinding.photoModeButton.isEnabled = cameraMode != CameraMode.PHOTO
+        viewBinding.videoModeButton.isEnabled = cameraMode != CameraMode.VIDEO
+    }
+
+    /**
      * Update the flash mode button icon based on the value set in imageCapture
      */
     private fun updateFlashModeIcon() {
diff --git a/app/src/main/res/color/camera_mode_selector_icon.xml b/app/src/main/res/color/camera_mode_selector_icon.xml
new file mode 100644
index 0000000..ff55303
--- /dev/null
+++ b/app/src/main/res/color/camera_mode_selector_icon.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:color="?attr/colorPrimary" android:state_enabled="true" />
+    <item android:color="?attr/colorOnPrimary" android:state_enabled="false" />
+    <item android:color="?attr/colorPrimary" />
+</selector>
\ No newline at end of file
diff --git a/app/src/main/res/drawable/camera_mode_button.xml b/app/src/main/res/drawable/camera_mode_button.xml
new file mode 100644
index 0000000..8378770
--- /dev/null
+++ b/app/src/main/res/drawable/camera_mode_button.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<selector
+    xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <item android:state_enabled="true" >
+        <shape>
+            <corners
+                android:radius="25dp" />
+            <padding
+                android:left="7dp"
+                android:top="7dp"
+                android:right="7dp"
+                android:bottom="7dp" />
+        </shape>
+    </item>
+
+    <item android:state_enabled="false" >
+        <shape>
+            <gradient
+                android:startColor="?attr/colorPrimary"
+                android:endColor="?attr/colorPrimary"
+                android:angle="270" />
+            <corners
+                android:radius="25dp" />
+            <padding
+                android:left="7dp"
+                android:top="7dp"
+                android:right="7dp"
+                android:bottom="7dp" />
+        </shape>
+    </item>
+
+    <item>
+        <shape>
+            <corners
+                android:radius="25dp" />
+            <padding
+                android:left="7dp"
+                android:top="7dp"
+                android:right="7dp"
+                android:bottom="7dp" />
+        </shape>
+    </item>
+</selector>
\ No newline at end of file
diff --git a/app/src/main/res/drawable/ic_photo.xml b/app/src/main/res/drawable/ic_photo.xml
new file mode 100644
index 0000000..43a4fd5
--- /dev/null
+++ b/app/src/main/res/drawable/ic_photo.xml
@@ -0,0 +1,6 @@
+<vector android:height="24dp" android:tint="#000000"
+    android:viewportHeight="24" android:viewportWidth="24"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="@android:color/white" android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
+    <path android:fillColor="@android:color/white" android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
+</vector>
diff --git a/app/src/main/res/drawable/ic_video.xml b/app/src/main/res/drawable/ic_video.xml
new file mode 100644
index 0000000..34b26df
--- /dev/null
+++ b/app/src/main/res/drawable/ic_video.xml
@@ -0,0 +1,5 @@
+<vector android:height="24dp" android:tint="#000000"
+    android:viewportHeight="24" android:viewportWidth="24"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="@android:color/white" android:pathData="M17,10.5V7c0,-0.55 -0.45,-1 -1,-1H4c-0.55,0 -1,0.45 -1,1v10c0,0.55 0.45,1 1,1h12c0.55,0 1,-0.45 1,-1v-3.5l4,4v-11l-4,4z"/>
+</vector>
diff --git a/app/src/main/res/drawable/layout_camera_mode.xml b/app/src/main/res/drawable/layout_camera_mode.xml
new file mode 100644
index 0000000..cead44f
--- /dev/null
+++ b/app/src/main/res/drawable/layout_camera_mode.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid
+        android:color="#525252"/>
+    <corners
+        android:radius="50dp"/>
+    <padding
+        android:left="0dp"
+        android:top="0dp"
+        android:right="0dp"
+        android:bottom="0dp" />
+</shape>
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index df0d67b..10124d7 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -164,18 +164,64 @@
             app:layout_constraintStart_toEndOf="@+id/shutterButton"
             app:layout_constraintTop_toTopOf="parent" />
 
-        <ImageButton
-            android:id="@+id/flipCameraButton"
-            style="@style/ApertureBottomBarButton"
-            android:layout_width="50dp"
-            android:layout_height="50dp"
-            android:contentDescription="@string/flip_camera_button_description"
-            android:scaleType="fitXY"
-            android:src="@drawable/ic_flip_camera"
+        <androidx.constraintlayout.widget.ConstraintLayout
+            android:layout_width="0dp"
+            android:layout_height="0dp"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toStartOf="@+id/shutterButton"
             app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintTop_toTopOf="parent" />
+            app:layout_constraintTop_toTopOf="parent">
+
+            <ImageButton
+                android:id="@+id/flipCameraButton"
+                style="@style/ApertureBottomBarButton"
+                android:layout_width="50dp"
+                android:layout_height="50dp"
+                android:contentDescription="@string/flip_camera_button_description"
+                android:scaleType="fitXY"
+                android:src="@drawable/ic_flip_camera"
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintHorizontal_bias="0.5"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />
+
+            <androidx.constraintlayout.widget.ConstraintLayout
+                android:id="@+id/constraintLayout"
+                android:layout_width="100dp"
+                android:layout_height="50dp"
+                android:background="@drawable/layout_camera_mode"
+                app:layout_constraintBottom_toTopOf="@+id/flipCameraButton"
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintHorizontal_bias="0.5"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toTopOf="parent">
+
+                <ImageButton
+                    android:id="@+id/photoModeButton"
+                    style="@style/ApertureCameraModeButton"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    app:layout_constraintBottom_toBottomOf="parent"
+                    app:layout_constraintEnd_toStartOf="@+id/videoModeButton"
+                    app:layout_constraintHorizontal_bias="0.5"
+                    app:layout_constraintStart_toStartOf="parent"
+                    app:layout_constraintTop_toTopOf="parent"
+                    app:srcCompat="@drawable/ic_photo" />
+
+                <ImageButton
+                    android:id="@+id/videoModeButton"
+                    style="@style/ApertureCameraModeButton"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    app:layout_constraintBottom_toBottomOf="parent"
+                    app:layout_constraintEnd_toEndOf="parent"
+                    app:layout_constraintHorizontal_bias="0.5"
+                    app:layout_constraintStart_toEndOf="@+id/photoModeButton"
+                    app:layout_constraintTop_toTopOf="parent"
+                    app:srcCompat="@drawable/ic_video" />
+            </androidx.constraintlayout.widget.ConstraintLayout>
+        </androidx.constraintlayout.widget.ConstraintLayout>
     </androidx.constraintlayout.widget.ConstraintLayout>
 
 
diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml
index d9d31fd..3416b74 100644
--- a/app/src/main/res/values/themes.xml
+++ b/app/src/main/res/values/themes.xml
@@ -29,4 +29,10 @@
         <item name="android:padding">0dp</item>
         <item name="android:scaleType">fitXY</item>
     </style>
+
+    <!-- Camera mode selector button theme -->
+    <style name="ApertureCameraModeButton">
+        <item name="tint">@color/camera_mode_selector_icon</item>
+        <item name="android:background">@drawable/camera_mode_button</item>
+    </style>
 </resources>
\ No newline at end of file