Aperture: Add shortcuts

Signed-off-by: Luca Stefani <luca.stefani.ge1@gmail.com>
Change-Id: I9819b0f15f96e56c0758e67db276d9ff0e78a31d
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9e7d0e8..afe2188 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -38,6 +38,10 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
 
+            <meta-data
+                android:name="android.app.shortcuts"
+                android:resource="@xml/shortcuts" />
+
             <intent-filter>
                 <action android:name="android.media.action.IMAGE_CAPTURE" />
                 <action android:name="android.media.action.IMAGE_CAPTURE_SECURE" />
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index 290565a..04a9486 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -223,6 +223,20 @@
         PauseToResume(R.drawable.avd_video_recording_resume),
     }
 
+    private val shortcutActions = mapOf(
+        "shortcut_selfie" to {
+            sharedPreferences.lastCameraMode = CameraMode.PHOTO
+            sharedPreferences.lastCameraFacing = CameraFacing.FRONT
+        },
+        "shortcut_video" to {
+            sharedPreferences.lastCameraMode = CameraMode.VIDEO
+            sharedPreferences.lastCameraFacing = CameraFacing.BACK
+        },
+        "shortcut_qr" to {
+            sharedPreferences.lastCameraMode = CameraMode.QR
+        },
+    )
+
     @SuppressLint("ClickableViewAccessibility")
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -376,6 +390,11 @@
             }
         }
 
+        // Handle shortcut intent
+        intent.action?.let {
+            shortcutActions[it]?.invoke()
+        }
+
         // Initialize shutter drawable
         when (cameraMode) {
             CameraMode.PHOTO -> startShutterAnimation(ShutterAnimation.InitPhoto)
diff --git a/app/src/main/res/drawable/ic_shortcut_qr.xml b/app/src/main/res/drawable/ic_shortcut_qr.xml
new file mode 100644
index 0000000..c585d82
--- /dev/null
+++ b/app/src/main/res/drawable/ic_shortcut_qr.xml
@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="48dp"
+    android:height="48dp"
+    android:viewportWidth="48"
+    android:viewportHeight="48">
+    <path
+        android:fillColor="@color/black"
+        android:pathData="M4,12.65V4H12.65V7H7V12.65ZM4,44V35.35H7V41H12.65V44ZM35.35,44V41H41V35.35H44V44ZM41,12.65V7H35.35V4H44V12.65ZM35.4,35.45H38.55V38.6H35.4ZM35.4,29.15H38.55V32.3H35.4ZM32.25,32.3H35.4V35.45H32.25ZM29.1,35.45H32.25V38.6H29.1ZM25.95,32.3H29.1V35.45H25.95ZM32.25,26H35.4V29.15H32.25ZM29.1,29.15H32.25V32.3H29.1ZM25.95,26H29.1V29.15H25.95ZM38.55,9.4V22H25.95V9.4ZM22,26V38.6H9.4V26ZM22,9.4V22H9.4V9.4ZM19.5,36.1V28.5H11.9V36.1ZM19.5,19.5V11.9H11.9V19.5ZM36.05,19.5V11.9H28.45V19.5Z" />
+</vector>
diff --git a/app/src/main/res/drawable/ic_shortcut_selfie.xml b/app/src/main/res/drawable/ic_shortcut_selfie.xml
new file mode 100644
index 0000000..4667024
--- /dev/null
+++ b/app/src/main/res/drawable/ic_shortcut_selfie.xml
@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+    <path
+        android:fillColor="@color/black"
+        android:pathData="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z" />
+</vector>
diff --git a/app/src/main/res/drawable/ic_shortcut_video.xml b/app/src/main/res/drawable/ic_shortcut_video.xml
new file mode 100644
index 0000000..fed0e16
--- /dev/null
+++ b/app/src/main/res/drawable/ic_shortcut_video.xml
@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+    <path
+        android:fillColor="@color/black"
+        android:pathData="M17,10.5V7A1,1 0 0,0 16,6H4A1,1 0 0,0 3,7V17A1,1 0 0,0 4,18H16A1,1 0 0,0 17,17V13.5L21,17.5V6.5L17,10.5Z" />
+</vector>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 2740b6e..f877276 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -60,4 +60,9 @@
     <string name="photo_capture_mode_maximize_quality">Maximize quality</string>
     <string name="photo_capture_mode_minimize_latency">Minimize latency</string>
     <string name="photo_capture_mode_zsl">Zero shutter lag (experimental)</string>
+
+    <!-- Shortcuts -->
+    <string name="shortcut_selfie">Take a selfie</string>
+    <string name="shortcut_video">Take a video</string>
+    <string name="shortcut_qr">Scan a barcode</string>
 </resources>
\ No newline at end of file
diff --git a/app/src/main/res/xml/shortcuts.xml b/app/src/main/res/xml/shortcuts.xml
new file mode 100644
index 0000000..9e02b7b
--- /dev/null
+++ b/app/src/main/res/xml/shortcuts.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     SPDX-FileCopyrightText: 2022 The LineageOS Project
+     SPDX-License-Identifier: Apache-2.0
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
+    <shortcut
+        android:enabled="true"
+        android:icon="@drawable/ic_shortcut_selfie"
+        android:shortcutDisabledMessage="@string/shortcut_selfie"
+        android:shortcutId="selfie"
+        android:shortcutLongLabel="@string/shortcut_selfie"
+        android:shortcutShortLabel="@string/shortcut_selfie">
+        <intent
+            android:action="shortcut_selfie"
+            android:targetClass="org.lineageos.aperture.MainActivity"
+            android:targetPackage="org.lineageos.aperture" />
+    </shortcut>
+    <shortcut
+        android:enabled="true"
+        android:icon="@drawable/ic_shortcut_video"
+        android:shortcutDisabledMessage="@string/shortcut_video"
+        android:shortcutId="video"
+        android:shortcutLongLabel="@string/shortcut_video"
+        android:shortcutShortLabel="@string/shortcut_video">
+        <intent
+            android:action="shortcut_video"
+            android:targetClass="org.lineageos.aperture.MainActivity"
+            android:targetPackage="org.lineageos.aperture" />
+    </shortcut>
+    <shortcut
+        android:enabled="true"
+        android:icon="@drawable/ic_shortcut_qr"
+        android:shortcutDisabledMessage="@string/shortcut_qr"
+        android:shortcutId="qr"
+        android:shortcutLongLabel="@string/shortcut_qr"
+        android:shortcutShortLabel="@string/shortcut_qr">
+        <intent
+            android:action="shortcut_qr"
+            android:targetClass="org.lineageos.aperture.MainActivity"
+            android:targetPackage="org.lineageos.aperture" />
+    </shortcut>
+</shortcuts>