Aperture: Save last _saved_ photo/video Uri

Will be used next time

Signed-off-by: Luca Stefani <luca.stefani.ge1@gmail.com>
Change-Id: I787bca6de393594bb75f145a5e807ca1d244efa2
diff --git a/app/src/main/java/org/lineageos/aperture/MainActivity.kt b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
index 1872891..594ac9c 100644
--- a/app/src/main/java/org/lineageos/aperture/MainActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/MainActivity.kt
@@ -156,7 +156,11 @@
         galleryButton.setOnClickListener {
             val intent = Intent().apply {
                 action = MediaStore.ACTION_REVIEW
-                type = "image/*"
+                sharedPreferences.getLastSavedUri()?.let {
+                    data = it
+                } ?: run {
+                    type = "image/*"
+                }
                 flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
             }
             startActivity(intent)
@@ -231,6 +235,7 @@
                     }
                     galleryButton.setOnClickListener { startActivity(intent) }
                     val msg = "Photo capture succeeded: ${output.savedUri}"
+                    sharedPreferences.setLastSavedUri(output.savedUri)
                     Log.d(LOG_TAG, msg)
                     isTakingPhoto = false
                     shutterButton.isEnabled = true
@@ -264,6 +269,7 @@
                     }
                     galleryButton.setOnClickListener { startActivity(intent) }
                     val msg = "Video capture succeeded: ${output.savedUri}"
+                    sharedPreferences.setLastSavedUri(output.savedUri)
                     Log.d(LOG_TAG, msg)
                 }
 
diff --git a/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt b/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt
index 7233549..47d6f3b 100644
--- a/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt
+++ b/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt
@@ -1,6 +1,7 @@
 package org.lineageos.aperture
 
 import android.content.SharedPreferences
+import android.net.Uri
 import androidx.camera.core.ImageCapture
 import androidx.camera.extensions.ExtensionMode
 import androidx.camera.video.Quality
@@ -185,3 +186,17 @@
         else -> Quality.HIGHEST
     }
 }
+
+// Last saved URI
+private const val LAST_SAVED_URI_KEY = "saved_uri"
+
+internal fun SharedPreferences.getLastSavedUri(): Uri? {
+    val raw = getString(LAST_SAVED_URI_KEY, null) ?: return null
+    return Uri.parse(raw)
+}
+
+internal fun SharedPreferences.setLastSavedUri(uri: Uri?) {
+    edit {
+        putString(LAST_SAVED_URI_KEY, uri.toString())
+    }
+}
\ No newline at end of file