Aperture: Relocate app shortcuts out of resources

Change-Id: I3bc5989cf800c48eb2bb4702b16c2b91964bc0b7
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index afe2188..9e7d0e8 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -38,10 +38,6 @@
                 <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 d6a369a..f43f9b0 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -76,6 +76,7 @@
 import org.lineageos.aperture.utils.CameraState
 import org.lineageos.aperture.utils.GridMode
 import org.lineageos.aperture.utils.PhysicalCamera
+import org.lineageos.aperture.utils.ShortcutsUtils
 import org.lineageos.aperture.utils.StorageUtils
 import org.lineageos.aperture.utils.TimeUtils
 import java.util.concurrent.ExecutorService
@@ -224,15 +225,15 @@
     }
 
     private val shortcutActions = mapOf(
-        "shortcut_selfie" to {
+        ShortcutsUtils.SHORTCUT_ID_SELFIE to {
             sharedPreferences.lastCameraMode = CameraMode.PHOTO
             sharedPreferences.lastCameraFacing = CameraFacing.FRONT
         },
-        "shortcut_video" to {
+        ShortcutsUtils.SHORTCUT_ID_VIDEO to {
             sharedPreferences.lastCameraMode = CameraMode.VIDEO
             sharedPreferences.lastCameraFacing = CameraFacing.BACK
         },
-        "shortcut_qr" to {
+        ShortcutsUtils.SHORTCUT_ID_QR to {
             sharedPreferences.lastCameraMode = CameraMode.QR
         },
     )
@@ -246,6 +247,9 @@
         setContentView(R.layout.activity_main)
         setShowWhenLocked(true)
 
+        // Register shortcuts
+        ShortcutsUtils.registerShortcuts(this)
+
         // Request camera permissions
         if (!allPermissionsGranted()) {
             ActivityCompat.requestPermissions(
diff --git a/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt b/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt
new file mode 100644
index 0000000..c5c6dfb
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/utils/ShortcutsUtils.kt
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2022 The LineageOS Project
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture.utils
+
+import android.content.Context
+import android.content.Intent
+import android.content.pm.ShortcutInfo
+import android.content.pm.ShortcutManager
+import android.graphics.drawable.Icon
+import org.lineageos.aperture.MainActivity
+import org.lineageos.aperture.R
+
+object ShortcutsUtils {
+    const val SHORTCUT_ID_SELFIE = "shortcut_selfie"
+    const val SHORTCUT_ID_VIDEO = "shortcut_video"
+    const val SHORTCUT_ID_QR = "shortcut_qr"
+
+    @androidx.camera.camera2.interop.ExperimentalCamera2Interop
+    @androidx.camera.core.ExperimentalZeroShutterLag
+    @androidx.camera.view.video.ExperimentalVideo
+    fun registerShortcuts(context: Context) {
+        val shortcutManager = context.getSystemService(ShortcutManager::class.java)
+        shortcutManager.dynamicShortcuts = listOf(
+            ShortcutInfo.Builder(context, SHORTCUT_ID_SELFIE)
+                .setShortLabel(context.getString(R.string.shortcut_selfie))
+                .setLongLabel(context.getString(R.string.shortcut_selfie))
+                .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_selfie))
+                .setIntent(
+                    Intent(context, MainActivity::class.java)
+                        .setAction(SHORTCUT_ID_SELFIE)
+                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                )
+                .build(),
+            ShortcutInfo.Builder(context, SHORTCUT_ID_VIDEO)
+                .setShortLabel(context.getString(R.string.shortcut_video))
+                .setLongLabel(context.getString(R.string.shortcut_video))
+                .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_video))
+                .setIntent(
+                    Intent(context, MainActivity::class.java)
+                        .setAction(SHORTCUT_ID_VIDEO)
+                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                )
+                .build(),
+            ShortcutInfo.Builder(context, SHORTCUT_ID_QR)
+                .setShortLabel(context.getString(R.string.shortcut_qr))
+                .setLongLabel(context.getString(R.string.shortcut_qr))
+                .setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut_qr))
+                .setIntent(
+                    Intent(context, MainActivity::class.java)
+                        .setAction(SHORTCUT_ID_QR)
+                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                )
+                .build()
+        )
+    }
+}
diff --git a/app/src/main/res/xml/shortcuts.xml b/app/src/main/res/xml/shortcuts.xml
deleted file mode 100644
index 9e02b7b..0000000
--- a/app/src/main/res/xml/shortcuts.xml
+++ /dev/null
@@ -1,43 +0,0 @@
-<?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>