Aperture: Match activities from Camera2

Change-Id: I7e74ee1c565efad942e00725b81ffafd01b2ebd0
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9eab7f9..d8e62d0 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -25,34 +25,92 @@
             android:required="false" />
 
         <activity
-            android:name=".MainActivity"
-            android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"
+            android:name=".CameraActivity"
+            android:clearTaskOnLaunch="true"
+            android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden"
             android:exported="true"
             android:launchMode="singleTask"
             android:screenOrientation="nosensor"
-            android:theme="@style/Theme.Aperture.Main">
+            android:taskAffinity=".CameraActivity"
+            android:theme="@style/Theme.Aperture.Camera"
+            android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
+            <intent-filter>
+                <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
 
+        <activity-alias
+            android:name=".CameraLauncher"
+            android:exported="true"
+            android:label="@string/app_name"
+            android:targetActivity=".CameraActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
+                <category android:name="android.intent.category.DEFAULT" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
+        </activity-alias>
 
+        <activity
+            android:name=".CaptureActivity"
+            android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden"
+            android:exported="true"
+            android:screenOrientation="nosensor"
+            android:theme="@style/Theme.Aperture.Camera"
+            android:visibleToInstantApps="true"
+            android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
             <intent-filter>
                 <action android:name="android.media.action.IMAGE_CAPTURE" />
-                <action android:name="android.media.action.IMAGE_CAPTURE_SECURE" />
-                <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
-                <action android:name="android.media.action.STILL_IMAGE_CAMERA_SECURE" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
+        <!-- Video camera and capture use the Camcorder label and icon. -->
+        <activity-alias
+            android:name=".VideoCamera"
+            android:exported="true"
+            android:label="@string/video_camera_label"
+            android:targetActivity=".CaptureActivity">
+            <intent-filter>
                 <action android:name="android.media.action.VIDEO_CAMERA" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+            <intent-filter>
                 <action android:name="android.media.action.VIDEO_CAPTURE" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity-alias>
+
+        <activity
+            android:name=".SecureCameraActivity"
+            android:clearTaskOnLaunch="true"
+            android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden"
+            android:excludeFromRecents="true"
+            android:exported="true"
+            android:taskAffinity=".SecureCameraActivity"
+            android:theme="@style/Theme.Aperture.Camera"
+            android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
+            <intent-filter>
+                <action android:name="android.media.action.STILL_IMAGE_CAMERA_SECURE" />
 
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
+            <intent-filter>
+                <action android:name="android.media.action.IMAGE_CAPTURE_SECURE" />
 
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
         </activity>
 
         <activity
             android:name=".SettingsActivity"
+            android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden"
             android:exported="false"
             android:label="@string/title_activity_settings"
             android:theme="@style/Theme.Aperture.Settings" />
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
similarity index 99%
rename from app/src/main/java/org/lineageos/aperture/MainActivity.kt
rename to app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index 76a7138..fc077ce 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -86,7 +86,7 @@
 @androidx.camera.camera2.interop.ExperimentalCamera2Interop
 @androidx.camera.core.ExperimentalZeroShutterLag
 @androidx.camera.view.video.ExperimentalVideo
-class MainActivity : AppCompatActivity() {
+open class CameraActivity : AppCompatActivity() {
     private val aspectRatioButton by lazy { findViewById<ToggleButton>(R.id.aspectRatioButton) }
     private val cameraModeHighlight by lazy { findViewById<MaterialButton>(R.id.cameraModeHighlight) }
     private val countDownView by lazy { findViewById<CountDownView>(R.id.countDownView) }
@@ -181,12 +181,12 @@
     private var location: Location? = null
     private val locationListener = object : LocationListener {
         override fun onLocationChanged(location: Location) {
-            val mainActivity = this@MainActivity
-            mainActivity.location = mainActivity.location?.let {
+            val cameraActivity = this@CameraActivity
+            cameraActivity.location = cameraActivity.location?.let {
                 if (it.accuracy >= location.accuracy) {
                     location
                 } else {
-                    mainActivity.location
+                    cameraActivity.location
                 }
             } ?: location
         }
@@ -251,7 +251,7 @@
 
         hideStatusBars()
 
-        setContentView(R.layout.activity_main)
+        setContentView(R.layout.activity_camera)
         setShowWhenLocked(true)
 
         // Register shortcuts
@@ -566,7 +566,7 @@
         val outputOptions = StorageUtils.getPhotoMediaStoreOutputOptions(
             contentResolver,
             ImageCapture.Metadata().apply {
-                location = this@MainActivity.location
+                location = this@CameraActivity.location
             }
         )
 
diff --git a/app/src/main/java/org/lineageos/aperture/CaptureActivity.kt b/app/src/main/java/org/lineageos/aperture/CaptureActivity.kt
new file mode 100644
index 0000000..debd6b1
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/CaptureActivity.kt
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) 2022 The LineageOS Project
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture
+
+// Use a different activity for capture intents, so it can have a different
+// task affinity from others. This makes sure the regular camera activity is not
+// reused for IMAGE_CAPTURE or VIDEO_CAPTURE intents from other activities.
+@androidx.camera.camera2.interop.ExperimentalCamera2Interop
+class CaptureActivity : CameraActivity() {
+}
diff --git a/app/src/main/java/org/lineageos/aperture/SecureCameraActivity.kt b/app/src/main/java/org/lineageos/aperture/SecureCameraActivity.kt
new file mode 100644
index 0000000..5ce6697
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/SecureCameraActivity.kt
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) 2022 The LineageOS Project
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture
+
+// Use a different activity for secure camera only. So it can have a different
+// task affinity from others. This makes sure non-secure camera activity is not
+// started in secure lock screen.
+@androidx.camera.camera2.interop.ExperimentalCamera2Interop
+class SecureCameraActivity : CameraActivity() {
+}
diff --git a/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt b/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
index 8b39813..6d74892 100644
--- a/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
@@ -68,7 +68,7 @@
                     Preference.OnPreferenceChangeListener { _, newValue ->
                         if (newValue as Boolean) {
                             requestLocationPermissions.launch(
-                                MainActivity.REQUIRED_PERMISSIONS_LOCATION
+                                CameraActivity.REQUIRED_PERMISSIONS_LOCATION
                             )
                         }
                         true
@@ -79,7 +79,7 @@
 
         @SuppressLint("UnsafeOptInUsageError")
         private fun allLocationPermissionsGranted() =
-            MainActivity.REQUIRED_PERMISSIONS_LOCATION.all {
+            CameraActivity.REQUIRED_PERMISSIONS_LOCATION.all {
                 ContextCompat.checkSelfPermission(
                     requireContext(), it
                 ) == PackageManager.PERMISSION_GRANTED
diff --git a/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt b/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt
index c5c6dfb..88f3ea0 100644
--- a/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt
+++ b/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt
@@ -11,7 +11,7 @@
 import android.content.pm.ShortcutInfo
 import android.content.pm.ShortcutManager
 import android.graphics.drawable.Icon
-import org.lineageos.aperture.MainActivity
+import org.lineageos.aperture.CameraActivity
 import org.lineageos.aperture.R
 
 object ShortcutsUtils {
@@ -30,7 +30,7 @@
                 .setLongLabel(context.getString(R.string.shortcut_selfie))
                 .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_selfie))
                 .setIntent(
-                    Intent(context, MainActivity::class.java)
+                    Intent(context, CameraActivity::class.java)
                         .setAction(SHORTCUT_ID_SELFIE)
                         .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                 )
@@ -40,7 +40,7 @@
                 .setLongLabel(context.getString(R.string.shortcut_video))
                 .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_video))
                 .setIntent(
-                    Intent(context, MainActivity::class.java)
+                    Intent(context, CameraActivity::class.java)
                         .setAction(SHORTCUT_ID_VIDEO)
                         .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                 )
@@ -50,7 +50,7 @@
                 .setLongLabel(context.getString(R.string.shortcut_qr))
                 .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_qr))
                 .setIntent(
-                    Intent(context, MainActivity::class.java)
+                    Intent(context, CameraActivity::class.java)
                         .setAction(SHORTCUT_ID_QR)
                         .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                 )
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_camera.xml
similarity index 99%
rename from app/src/main/res/layout/activity_main.xml
rename to app/src/main/res/layout/activity_camera.xml
index 4dee24b..90d3103 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_camera.xml
@@ -5,7 +5,7 @@
     android:id="@+id/mainLayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    tools:context=".MainActivity">
+    tools:context=".CameraActivity">
 
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/secondaryBarLayout"
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index a5d0436..63d87b7 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,5 +1,7 @@
 <resources>
+    <!-- App/Activities -->
     <string name="app_name">Camera</string>
+    <string name="video_camera_label">Camcorder</string>
 
     <!-- Button descriptions -->
     <string name="aspect_ratio_button_description">Aspect ratio</string>
diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml
index 3733931..0806477 100644
--- a/app/src/main/res/values/themes.xml
+++ b/app/src/main/res/values/themes.xml
@@ -19,7 +19,7 @@
     </style>
 
     <!-- Main activity theme. -->
-    <style name="Theme.Aperture.Main" parent="Theme.Aperture">
+    <style name="Theme.Aperture.Camera" parent="Theme.Aperture">
         <item name="android:windowBackground">?attr/colorOnPrimary</item>
         <item name="windowActionBar">false</item>
         <item name="windowNoTitle">true</item>