photos: view secure folder items (#707)
- Commit
2fed58b219622486e9ebcf92126abcd9c2ef5b45- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-13 18:17 UTC (2 months ago)
- Parents
16bc8c2
Diff
photos/src/main/java/com/vayunmathur/photos/MainActivity.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt b/photos/src/main/java/com/vayunmathur/photos/MainActivity.ktindex a8316696d2..de46ac3f90 100644--- a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt+++ b/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt@@ -81,6 +81,7 @@ import com.vayunmathur.photos.ui.PeoplePage import com.vayunmathur.photos.ui.PhotoPage import com.vayunmathur.photos.ui.SecureFolderPage import com.vayunmathur.photos.ui.TrashPage+import com.vayunmathur.photos.ui.VaultViewerPage import com.vayunmathur.photos.ui.WallpaperPage import com.vayunmathur.photos.util.GalleryViewModel import com.vayunmathur.photos.util.GalleryViewModelFactory@@ -247,6 +248,9 @@ sealed interface Route: NavKey { @Serializable data object SecureFolder: Route + @Serializable+ data class VaultViewer(val vaultId: Long): Route+ @Serializable data class Wallpaper(val id: Long, val uri: String? = null) : Route }@@ -349,6 +353,15 @@ fun Navigation( entry<Route.SecureFolder>(metadata = SiblingPage()) { SecureFolderEntry(backStack, secureFolderViewModel, vaultPhotoDao != null, vaultPassword) }++ entry<Route.VaultViewer>(metadata = ListDetailPage()) {+ val password = vaultPassword+ if (password != null) {+ VaultViewerPage(backStack, it.vaultId, password, secureFolderViewModel)+ } else {+ SecureFolderEntry(backStack, secureFolderViewModel, false, null)+ }+ } } }
photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.kt b/photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.ktindex ead9fa73ac..dd0e58dea3 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.kt@@ -118,13 +118,17 @@ fun SecureFolderPage( isSelected = photo.id in selectedIds, isSelectionMode = isSelectionMode, secureFolderViewModel = secureFolderViewModel,- ) {- if (isSelectionMode) {- secureFolderViewModel.toggleSelection(photo.id)- } else {+ onClick = {+ if (isSelectionMode) {+ secureFolderViewModel.toggleSelection(photo.id)+ } else {+ backStack.add(Route.VaultViewer(photo.id))+ }+ },+ onLongClick = { secureFolderViewModel.addSelection(photo.id)- }- }+ },+ ) } } }@@ -141,6 +145,7 @@ fun VaultPhotoItem( isSelectionMode: Boolean, secureFolderViewModel: SecureFolderViewModel, onClick: () -> Unit,+ onLongClick: () -> Unit = onClick, ) { val bitmap by remember(photo.thumbnailPath) { secureFolderViewModel.thumbnailState(photo.thumbnailPath)@@ -161,7 +166,7 @@ fun VaultPhotoItem( .clip(RoundedCornerShape(8.dp)) .combinedClickable( onClick = onClick,- onLongClick = onClick+ onLongClick = onLongClick ), contentAlignment = Alignment.Center ) {
photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerPage.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerPage.kt b/photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerPage.ktnew file mode 100644index 0000000000..3aec849055--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerPage.kt@@ -0,0 +1,92 @@+package com.vayunmathur.photos.ui++import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.runtime.Composable+import androidx.compose.runtime.DisposableEffect+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.produceState+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import com.vayunmathur.library.ui.LoadingIndicator+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.util.AppMessages+import com.vayunmathur.library.util.NavBackStack+import com.vayunmathur.photos.R+import com.vayunmathur.photos.Route+import com.vayunmathur.photos.util.SecureFolderManager+import com.vayunmathur.photos.util.SecureFolderViewModel+import java.io.File+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext++/**+ * Binder for the full-screen vault viewer. Collects the [VaultPhoto] by id,+ * decrypts it to the app cache off the main thread, and renders+ * [VaultViewerScreen] once ready. The plaintext cache file is deleted when the+ * viewer leaves the composition.+ */+@Composable+fun VaultViewerPage(+ backStack: NavBackStack<Route>,+ vaultId: Long,+ password: String,+ secureFolderViewModel: SecureFolderViewModel,+) {+ val context = LocalContext.current+ val photos by secureFolderViewModel.photos.collectAsState()+ val photo = photos.firstOrNull { it.id == vaultId }++ val decryptFailedMessage = stringResource(R.string.vault_viewer_decrypt_failed)++ // Null = still decrypting; non-null but file missing/failed handled below.+ val decryptedFile by produceState<File?>(initialValue = null, photo, password) {+ value = if (photo == null) {+ null+ } else {+ withContext(Dispatchers.IO) {+ SecureFolderManager(context).decryptToCacheFile(+ photo.path,+ password,+ context.cacheDir,+ )+ }+ }+ if (photo != null && value == null) {+ AppMessages.show(decryptFailedMessage)+ }+ }++ // Plaintext must not outlive the viewer: delete the decrypted file (and any+ // stale ones from a killed process) on dispose.+ DisposableEffect(photo?.path) {+ onDispose {+ decryptedFile?.delete()+ SecureFolderManager(context).clearViewerCache(context.cacheDir)+ }+ }++ if (photo == null) {+ Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {+ Text(stringResource(R.string.vault_viewer_not_found))+ }+ return+ }++ if (decryptedFile == null) {+ Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {+ LoadingIndicator()+ }+ return+ }++ VaultViewerScreen(+ fileName = photo.name,+ file = decryptedFile,+ isVideo = photo.videoDuration != null,+ onBack = { backStack.pop() },+ )+}
photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerScreen.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerScreen.kt b/photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerScreen.ktnew file mode 100644index 0000000000..6b067595c3--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/ui/VaultViewerScreen.kt@@ -0,0 +1,125 @@+package com.vayunmathur.photos.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.layout.ContentScale+import androidx.compose.ui.layout.onGloballyPositioned+import androidx.compose.ui.text.style.TextOverflow+import androidx.compose.ui.unit.IntSize+import androidx.compose.ui.unit.dp+import androidx.core.net.toUri+import com.vayunmathur.library.image.ImageRequest+import com.vayunmathur.library.image.compose.AsyncImage+import com.vayunmathur.library.ui.FadeVisibility+import com.vayunmathur.library.ui.IconBack+import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Scaffold+import com.vayunmathur.library.ui.Text+import java.io.File++/**+ * Full-screen black viewer for one decrypted vault item, mirroring PhotoPage's+ * immersive pattern. Stateless: the caller supplies the decrypted [file] (or+ * null while decryption is still running).+ *+ * Vault rows carry no mime column and GIFs are indistinguishable from stills at+ * rest, so every non-video item renders through [AsyncImage] regardless of+ * format; videos (videoDuration != null) play via [VideoPlayer].+ */+@Composable+fun VaultViewerScreen(+ fileName: String,+ file: File?,+ isVideo: Boolean,+ onBack: () -> Unit,+) {+ var isChromeVisible by remember { mutableStateOf(true) }+ var zoom by remember { mutableStateOf(ZoomState()) }+ // Live reader, not a snapshot: gesture handlers read the latest zoom without+ // recomposing this screen on every pinch frame.+ val zoomState = rememberUpdatedState(zoom)+ var containerSize by remember { mutableStateOf(IntSize.Zero) }++ // RAW SCAFFOLD EXCEPTION: bar-less full-screen black media viewer (no top/bottom+ // bar); chrome is a floating overlay inside the viewer. AppScaffold always renders a+ // top app bar, which would break the immersive viewer.+ Scaffold(containerColor = Color.Black) { paddingValues ->+ Box(+ modifier = Modifier+ .fillMaxSize()+ .padding(paddingValues)+ .background(Color.Black)+ .onGloballyPositioned { containerSize = it.size }+ .then(+ photoZoomGestures(+ currentZoom = zoomState,+ containerSize = { containerSize },+ onZoomUpdate = { zoom = it },+ onToggleMetadata = { isChromeVisible = !isChromeVisible },+ )+ )+ ) {+ if (file != null) {+ val zoomModifier = Modifier.graphicsLayer {+ scaleX = zoom.scale+ scaleY = zoom.scale+ translationX = zoom.offset.x+ translationY = zoom.offset.y+ }+ if (isVideo) {+ VideoPlayer(+ modifier = Modifier.fillMaxSize().then(zoomModifier),+ uri = file.toUri(),+ isMetadataVisible = isChromeVisible,+ isSettledPage = true,+ )+ } else {+ AsyncImage(+ model = ImageRequest.Builder().data(file).build(),+ contentDescription = fileName,+ modifier = Modifier.fillMaxSize().then(zoomModifier),+ contentScale = ContentScale.Fit,+ )+ }+ }++ FadeVisibility(+ visible = isChromeVisible,+ modifier = Modifier.align(Alignment.TopStart).fillMaxWidth()+ ) {+ Box(+ modifier = Modifier+ .fillMaxWidth()+ .background(Color.Black.copy(alpha = 0.5f))+ .padding(horizontal = 4.dp, vertical = 8.dp)+ ) {+ IconButton(onClick = onBack) {+ IconBack(tint = Color.White)+ }+ Text(+ text = fileName,+ color = Color.White,+ style = MaterialTheme.typography.titleMedium,+ maxLines = 1,+ overflow = TextOverflow.Ellipsis,+ modifier = Modifier.align(Alignment.Center)+ )+ }+ }+ }+ }+}
photos/src/main/java/com/vayunmathur/photos/util/SecureFolderManager.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderManager.kt b/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderManager.ktindex 3a4ab72b02..08a9b62161 100644--- a/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderManager.kt+++ b/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderManager.kt@@ -201,6 +201,42 @@ class SecureFolderManager(val context: Context) { } } + /**+ * Decrypt the vault file at [path] into the app cache so it can be viewed+ * full-screen without touching the MediaStore. The output lives under+ * [cacheDir]/vault_view/ and is plaintext while it exists, so callers must+ * serve it via FileProvider (never a file:// Uri to another app) and delete+ * it when the viewer closes (see [clearViewerCache]).+ * Returns null when the file is missing or decryption fails.+ */+ fun decryptToCacheFile(path: String, password: String, cacheDir: File): File? {+ val inputFile = File(path)+ if (!inputFile.exists()) return null+ val viewerDir = File(cacheDir, "vault_view")+ if (!viewerDir.exists() && !viewerDir.mkdirs()) return null+ val outputFile = File(viewerDir, inputFile.nameWithoutExtension)+ return try {+ val key = getSecretKey(password)+ FileInputStream(inputFile).use { fis ->+ FileOutputStream(outputFile).use { fos ->+ if (!decryptToOutputStream(fis, fos, key)) {+ outputFile.delete()+ return null+ }+ }+ }+ outputFile+ } catch (e: Exception) {+ outputFile.delete()+ null+ }+ }++ /** Delete plaintext files left under [cacheDir]/vault_view (see [decryptToCacheFile]). */+ fun clearViewerCache(cacheDir: File) {+ File(cacheDir, "vault_view").listFiles()?.forEach { runCatching { it.delete() } }+ }+ fun decryptAndRestore(vaultPhoto: VaultPhoto, password: String): Uri? { val inputFile = File(vaultPhoto.path) if (!inputFile.exists()) return null
photos/src/main/res/values/strings.xml
diff --git a/photos/src/main/res/values/strings.xml b/photos/src/main/res/values/strings.xmlindex f9a622365b..f0c3495d4b 100644--- a/photos/src/main/res/values/strings.xml+++ b/photos/src/main/res/values/strings.xml@@ -46,6 +46,10 @@ <!-- Generic selection count badge in TopAppBar of Gallery, SecureFolder, Trash. %1$d = number selected --> <string name="items_selected"><xliff:g id="count" example="5">%1$d</xliff:g> selected</string>+ <!-- Vault viewer error when the encrypted file cannot be decrypted to the cache. Sentence -->+ <string name="vault_viewer_decrypt_failed">Couldn\'t open this item</string>+ <!-- Vault viewer message when the item no longer exists in the vault. Sentence -->+ <string name="vault_viewer_not_found">This item is no longer in the Secure Folder</string> <!-- Gallery grouping header: month + year. %1$s = month name e.g. January, %2$d = year e.g. 2024 --> <string name="month_year_format"><xliff:g id="month" example="January">%1$s</xliff:g> <xliff:g id="year" example="2024">%2$d</xliff:g></string> <!-- Search bar placeholder hint. Noun phrase with ellipsis -->