Modern Apps git

← Modern-Apps.git

photos: secure folder re-lock option (#706)

Commit
f9f897f1dc125556d7ea4d86183bfb3770a6d2fa
Author
vayun-mathur <[email protected]>
When
2026-09-13 18:24 UTC (2 months ago)
Parents
2fed58b

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 de46ac3f90..a26b4bf664 100644--- a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt+++ b/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt@@ -296,6 +296,21 @@ fun Navigation(     val backStack = rememberNavBackStack<Route>(viewerRoute ?: Route.Gallery)     val vaultPhotoDao by secureFolderViewModel.vaultPhotoDao.collectAsState()     val vaultPassword by secureFolderViewModel.vaultPassword.collectAsState()+    val relockOnExit by secureFolderViewModel.relockOnExit.collectAsState()++    // Opt-in re-lock: leaving the Secure Folder (any route other than the+    // folder itself or the vault viewer, which counts as inside) locks the+    // vault so the next open requires unlock again. Off by default, which+    // preserves the current stay-unlocked behaviour.+    val topRoute = backStack.backStack.lastOrNull()+    LaunchedEffect(topRoute, relockOnExit) {+        if (relockOnExit && topRoute != null &&+            topRoute !is Route.SecureFolder && topRoute !is Route.VaultViewer &&+            vaultPhotoDao != null+        ) {+            secureFolderViewModel.lock()+        }+    }      // Indexed once per incoming URI, and `rememberSaveable` rather than the LaunchedEffect key so     // a configuration change does not re-run it.
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 dd0e58dea3..e1dd3fd281 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/SecureFolderPage.kt@@ -6,6 +6,8 @@ import androidx.compose.foundation.background import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth@@ -18,8 +20,13 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import com.vayunmathur.library.ui.EmptyState import com.vayunmathur.library.ui.ExperimentalMaterial3Api+import com.vayunmathur.library.ui.AlertDialog import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.IconSettings+import com.vayunmathur.library.ui.Switch+import com.vayunmathur.library.ui.TextButton+import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Surface import com.vayunmathur.library.ui.Text@@ -27,6 +34,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment@@ -65,9 +73,18 @@ fun SecureFolderPage(     var columnCount by LocalColumnCount.current     val selectedIds by secureFolderViewModel.selectedIds.collectAsState()     val isSelectionMode = selectedIds.isNotEmpty()+    var showRelockSettings by remember { mutableStateOf(false) }+    val relockOnExit by secureFolderViewModel.relockOnExit.collectAsState()      val closeSelection: (() -> Unit)? =         if (isSelectionMode) ({ secureFolderViewModel.clearSelection() }) else null+    if (showRelockSettings) {+        SecureFolderRelockDialog(+            relockOnExit = relockOnExit,+            onCheckedChange = { secureFolderViewModel.setRelockOnExit(it) },+            onDismiss = { showRelockSettings = false },+        )+    }     AppScaffold(         title = if (isSelectionMode) {             stringResource(R.string.items_selected, selectedIds.size)@@ -84,6 +101,9 @@ fun SecureFolderPage(                     IconUnarchive()                 }             } else {+                IconButton(onClick = { showRelockSettings = true }) {+                    IconSettings()+                }                 BackupButtons(                     dbConfigs = listOf("vault-db" to password),                     dbCodec = SqlCipherDbCodec,@@ -136,6 +156,39 @@ fun SecureFolderPage(     } } +@Composable+private fun SecureFolderRelockDialog(+    relockOnExit: Boolean,+    onCheckedChange: (Boolean) -> Unit,+    onDismiss: () -> Unit,+) {+    AlertDialog(+        onDismissRequest = onDismiss,+        title = { Text(stringResource(R.string.secure_folder_relock_title)) },+        text = {+            Column(modifier = Modifier.fillMaxWidth()) {+                Row(+                    modifier = Modifier.fillMaxWidth(),+                    horizontalArrangement = Arrangement.SpaceBetween,+                    verticalAlignment = Alignment.CenterVertically,+                ) {+                    Text(stringResource(R.string.secure_folder_relock_label))+                    Switch(checked = relockOnExit, onCheckedChange = onCheckedChange)+                }+                Text(+                    stringResource(R.string.secure_folder_relock_description),+                    style = MaterialTheme.typography.bodySmall,+                )+            }+        },+        confirmButton = {+            TextButton(onClick = onDismiss) {+                Text(stringResource(UiR.string.done))+            }+        },+    )+}+ @OptIn(ExperimentalFoundationApi::class) @Composable fun VaultPhotoItem(
photos/src/main/java/com/vayunmathur/photos/util/SecureFolderViewModel.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderViewModel.kt b/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderViewModel.ktindex 798cfee274..d36b57ea4c 100644--- a/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderViewModel.kt+++ b/photos/src/main/java/com/vayunmathur/photos/util/SecureFolderViewModel.kt@@ -14,6 +14,7 @@ import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import androidx.core.net.toUri import com.vayunmathur.library.biometric.unlockDatabaseWithBiometrics+import com.vayunmathur.library.util.DataStoreUtils import com.vayunmathur.photos.data.Photo import com.vayunmathur.photos.data.PhotosRepository import com.vayunmathur.photos.data.VaultPhoto@@ -61,6 +62,25 @@ class SecureFolderViewModel(application: Application) : AndroidViewModel(applica      private val sfm: SecureFolderManager by lazy { SecureFolderManager(application) } +    private val dataStore: DataStoreUtils by lazy {+        DataStoreUtils.getInstance(application.applicationContext)+    }++    private val _relockOnExit = MutableStateFlow(+        // Read synchronously at construction; the default (false) preserves the+        // current stay-unlocked behaviour until the user opts into re-locking.+        DataStoreUtils.getInstance(application.applicationContext)+            .getBoolean(RELOCK_ON_EXIT_KEY, false)+    )+    val relockOnExit: StateFlow<Boolean> = _relockOnExit.asStateFlow()++    fun setRelockOnExit(value: Boolean) {+        _relockOnExit.value = value+        viewModelScope.launch {+            dataStore.setBoolean(RELOCK_ON_EXIT_KEY, value)+        }+    }+     /**      * One observable holder per thumbnail path.      *@@ -104,6 +124,23 @@ class SecureFolderViewModel(application: Application) : AndroidViewModel(applica         _vaultPhotoDao.value = dao     } +    /**+     * Re-lock the vault: drop the DAO and password so the next open requires+     * biometric unlock again, and drop decrypted thumbnails. Thumbnails are+     * cleared without recycling (mirrors the LRU eviction policy: the UI may+     * still hold references; the collector reclaims them). No-op safe when+     * already locked.+     */+    fun lock() {+        synchronized(thumbCache) {+            thumbStates.values.forEach { it.value = null }+            thumbCache.clear()+        }+        _selectedIds.value = emptySet()+        _vaultPhotoDao.value = null+        _vaultPassword.value = null+    }+     fun unlock(         activity: FragmentActivity,         onSuccess: (VaultPhotoDao, String) -> Unit = { _, _ -> },@@ -285,6 +322,7 @@ class SecureFolderViewModel(application: Application) : AndroidViewModel(applica      companion object {         private const val TAG = "SecureFolderViewModel"+        const val RELOCK_ON_EXIT_KEY = "secure_folder_relock_on_exit"     } } 
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 f0c3495d4b..1488268d1c 100644--- a/photos/src/main/res/values/strings.xml+++ b/photos/src/main/res/values/strings.xml@@ -103,6 +103,20 @@     <string name="view_panorama">View panorama</string>     <!-- Fallback label when panorama type detected but no specific kind -->     <string name="panorama">Panorama</string>+    <!-- Button to play the embedded clip of a motion photo, replacing the still. Verb phrase -->+    <string name="play_motion_photo">Play motion photo</string>+    <!-- Button to stop motion-photo playback and return to the still. Verb phrase -->+    <string name="stop_motion_photo">Stop motion</string>+    <!-- Viewer metadata badge marking a motion photo (a still with an embedded clip). Keep uppercase -->+    <string name="motion_badge">MOTION</string>+    <!-- Snackbar when the embedded motion-photo clip could not be extracted for playback. Sentence -->+    <string name="motion_photo_failed">Couldn\'t play the motion photo.</string>+    <!-- Secure Folder settings dialog title. Noun phrase -->+    <string name="secure_folder_relock_title">Secure Folder settings</string>+    <!-- Secure Folder settings row label for the re-lock-on-exit toggle. Verb phrase -->+    <string name="secure_folder_relock_label">Lock when leaving</string>+    <!-- Secure Folder settings row description explaining the re-lock toggle. Sentence -->+    <string name="secure_folder_relock_description">When on, the Secure Folder locks every time you leave it and asks for unlock on every open.</string>      <!-- TopAppBar title for photo editor screen. Verb phrase -->     <string name="title_edit_photo">Edit Photo</string>