Aperture: Avoid binding camera without permissions

Change-Id: I66ebe1242b13b17716a17340c211f599ebf7f055
diff --git a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index 5dbe499..60f910f 100644
--- a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -346,6 +346,25 @@
         }
     }
 
+    private val mainPermissionsRequestOnStartLauncher = registerForActivityResult(
+        ActivityResultContracts.RequestMultiplePermissions()
+    ) {
+        if (it.isNotEmpty()) {
+            if (!permissionsUtils.mainPermissionsGranted()) {
+                Toast.makeText(
+                    this, getString(R.string.app_permissions_toast), Toast.LENGTH_SHORT
+                ).show()
+                finish()
+                return@registerForActivityResult
+            }
+
+            // This is a good time to ask the user for location permissions
+            if (sharedPreferences.saveLocation == null) {
+                locationPermissionsDialog.show()
+            }
+        }
+    }
+
     private val mainPermissionsRequestLauncher = registerForActivityResult(
         ActivityResultContracts.RequestMultiplePermissions()
     ) {
@@ -355,14 +374,13 @@
                     this, getString(R.string.app_permissions_toast), Toast.LENGTH_SHORT
                 ).show()
                 finish()
+                return@registerForActivityResult
             }
 
-            // This is a good time to ask the user for location permissions
-            if (sharedPreferences.saveLocation == null) {
-                locationPermissionsDialog.show()
-            }
+            bindCameraUseCases()
         }
     }
+
     private val locationPermissionsRequestLauncher = registerForActivityResult(
         ActivityResultContracts.RequestMultiplePermissions()
     ) {
@@ -1129,7 +1147,7 @@
 
         // Request camera permissions
         if (!permissionsUtils.mainPermissionsGranted()) {
-            mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
+            mainPermissionsRequestOnStartLauncher.launch(PermissionsUtils.mainPermissions)
         } else if (sharedPreferences.saveLocation == null) {
             locationPermissionsDialog.show()
         }
@@ -1138,11 +1156,6 @@
     override fun onResume() {
         super.onResume()
 
-        // Re-request camera permissions in case the user revoked them on app runtime
-        if (!permissionsUtils.mainPermissionsGranted()) {
-            mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
-        }
-
         // Set bright screen
         setBrightScreen(sharedPreferences.brightScreen)
 
@@ -1166,8 +1179,13 @@
         // Start observing battery status
         registerReceiver(batteryBroadcastReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
 
-        // Re-bind the use cases
-        bindCameraUseCases()
+        // Re-request camera permissions in case the user revoked them on app runtime
+        if (!permissionsUtils.mainPermissionsGranted()) {
+            mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
+        } else {
+            // If we already have the permission, re-bind the use cases
+            bindCameraUseCases()
+        }
     }
 
     override fun onPause() {