dd4d1788e721
fix: CF-1 auto-lock timer + background lock via ProcessLifecycleOwner, nav drops to Lock on lock; VaultLockTimer + 4 timer tests (v2.2.1)
- time
- 2026-06-09 18:09 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- audit fixes vault locking
- parent
- 73682a92c79c
↓ peach-android-dd4d1788.tar.gzatlas at this save
files changed7 files
app/build.gradle+4 −2app/src/main/java/com/peachpasswords/MainActivity.kt+9 −1app/src/main/java/com/peachpasswords/PeachApplication.kt+23 −1app/src/main/java/com/peachpasswords/ui/PeachAppNavHost.kt+26 −0app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+6 −4app/src/main/java/com/peachpasswords/vault/VaultLockTimer.kt+53 −0app/src/test/java/com/peachpasswords/vault/VaultLockTimerTest.kt+87 −0app/build.gradle+4 −2
@@ -16,8 +16,8 @@1616 applicationId "com.peachpasswords"1717 minSdk 261818 targetSdk 3519- versionCode 20200020- versionName "2.2.0"19+ versionCode 20210020+ versionName "2.2.1"2121 2222 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323 vectorDrawables {@@ -83,6 +83,7 @@8383 implementation "androidx.hilt:hilt-navigation-compose:1.2.0"8484 implementation "androidx.lifecycle:lifecycle-runtime-compose:2.8.4"8585 implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.8.4"86+ implementation "androidx.lifecycle:lifecycle-process:2.8.4"8687 implementation "androidx.navigation:navigation-compose:2.8.0"8788 implementation "androidx.security:security-crypto:1.1.0-alpha06"8889 implementation "androidx.compose.material3:material3"@@ -123,4 +124,5 @@123124 androidTestImplementation "androidx.compose.ui:ui-test-junit4"124125 125126 testImplementation "junit:junit:4.13.2"127+ testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1"126128 }app/src/main/java/com/peachpasswords/MainActivity.kt+9 −1
@@ -21,13 +21,21 @@2121 import com.peachpasswords.ui.theme.ACCENT_COLORS2222 import com.peachpasswords.ui.theme.DEFAULT_ACCENT2323 import com.peachpasswords.ui.theme.PeachTheme24+import com.peachpasswords.vault.VaultLockTimer2425 import com.peachpasswords.vault.VaultRepository2526 2627 // FragmentActivity (not ComponentActivity) so androidx BiometricPrompt can attach.2728 @AndroidEntryPoint2829 class MainActivity : FragmentActivity() {2930 @Inject lateinit var vault: VaultRepository31+ @Inject lateinit var lockTimer: VaultLockTimer3032 33+ // Every touch/key event resets the idle auto-lock countdown (CF-1).34+ override fun onUserInteraction() {35+ super.onUserInteraction()36+ lockTimer.poke()37+ }38+3139 override fun onCreate(savedInstanceState: Bundle?) {3240 installSplashScreen()3341 super.onCreate(savedInstanceState)@@ -75,7 +83,7 @@7583 accent = accent,7684 reduceMotion = settings.reduceMotion,7785 ) {78- PeachAppNavHost(startDestination = start)86+ PeachAppNavHost(startDestination = start, isUnlocked = vault.isUnlocked)7987 }8088 }8189 }app/src/main/java/com/peachpasswords/PeachApplication.kt+23 −1
@@ -1,7 +1,29 @@11 package com.peachpasswords22 33 import android.app.Application4+import androidx.lifecycle.DefaultLifecycleObserver5+import androidx.lifecycle.LifecycleOwner6+import androidx.lifecycle.ProcessLifecycleOwner7+import com.peachpasswords.vault.VaultLockTimer8+import com.peachpasswords.vault.VaultRepository49 import dagger.hilt.android.HiltAndroidApp10+import javax.inject.Inject511 612 @HiltAndroidApp7-class PeachApplication : Application()13+class PeachApplication : Application() {14+ @Inject lateinit var vault: VaultRepository15+ @Inject lateinit var lockTimer: VaultLockTimer16+17+ override fun onCreate() {18+ super.onCreate()19+ // Lock the vault the moment the whole app goes to background (CF-1).20+ // ProcessLifecycleOwner is process-wide and debounced, so a rotation or a21+ // brief Activity restart does not trip it — only a genuine backgrounding.22+ ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {23+ override fun onStop(owner: LifecycleOwner) {24+ vault.lock()25+ lockTimer.cancel()26+ }27+ })28+ }29+}app/src/main/java/com/peachpasswords/ui/PeachAppNavHost.kt+26 −0
@@ -6,6 +6,9 @@66 import androidx.compose.animation.slideInVertically77 import androidx.compose.animation.slideOutVertically88 import androidx.compose.runtime.Composable9+import androidx.compose.runtime.LaunchedEffect10+import androidx.compose.runtime.getValue11+import androidx.lifecycle.compose.collectAsStateWithLifecycle912 import androidx.navigation.NavHostController1013 import androidx.navigation.NavType1114 import androidx.navigation.compose.NavHost@@ -36,9 +39,32 @@3639 fun PeachAppNavHost(3740 navController: NavHostController = rememberNavController(),3841 startDestination: PeachNav = PeachNav.Lock,42+ isUnlocked: kotlinx.coroutines.flow.StateFlow<Boolean>? = null,3943 ) {4044 val nav: (PeachNav) -> Unit = { navController.navigate(it.route) }4145 val back: () -> Unit = { navController.popBackStack() }46+47+ // When the vault locks for any reason — manual lock button, idle auto-lock, or48+ // background lock (CF-1/CF-2) — drop to the lock screen and clear history so no49+ // post-unlock screen lingers behind it. Guarded so it never fires on the lock50+ // or onboarding screens (where the vault is legitimately locked).51+ if (isUnlocked != null) {52+ val unlocked by isUnlocked.collectAsStateWithLifecycle()53+ LaunchedEffect(unlocked) {54+ if (!unlocked) {55+ val current = navController.currentDestination?.route56+ if (current != null &&57+ current != PeachNav.Lock.route &&58+ current != PeachNav.Onboarding.route59+ ) {60+ navController.navigate(PeachNav.Lock.route) {61+ popUpTo(0) { inclusive = true }62+ launchSingleTop = true63+ }64+ }65+ }66+ }67+ }4268 4369 NavHost(4470 navController = navController,app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+6 −4
@@ -12,6 +12,7 @@1212 import com.peachpasswords.vault.DuressMatch1313 import com.peachpasswords.vault.UnlockResult1414 import com.peachpasswords.vault.VaultCrypto15+import com.peachpasswords.vault.VaultLockTimer1516 import com.peachpasswords.vault.VaultRepository1617 import dagger.hilt.android.lifecycle.HiltViewModel1718 import dagger.hilt.android.qualifiers.ApplicationContext@@ -44,6 +45,7 @@4445 class LockViewModel @Inject constructor(4546 private val vault: VaultRepository,4647 private val duress: DuressConfigStore,48+ private val lockTimer: VaultLockTimer,4749 @ApplicationContext private val appContext: Context,4850 ) : ViewModel() {4951 private val _state = MutableStateFlow(LockUiState(hasVault = vault.hasVault()))@@ -59,7 +61,7 @@5961 DuressMatch.None -> Unit6062 }6163 when (val r = vault.unlock(password)) {62- is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) }64+ is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true) } }6365 is UnlockResult.WrongPassword -> _state.update {6466 it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)6567 }@@ -77,7 +79,7 @@7779 private suspend fun handleDuress(action: DuressAction) {7880 when (action) {7981 DuressAction.DECOY -> when (vault.unlockDecoy()) {80- is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) }82+ is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true) } }8183 else -> _state.update {8284 it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)8385 }@@ -103,7 +105,7 @@103105 _state.update { it.copy(loading = true, error = null) }104106 viewModelScope.launch {105107 when (val r = vault.createVault(password)) {106- is CreateVaultResult.Success -> _state.update { it.copy(loading = false, unlocked = true, created = true) }108+ is CreateVaultResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true, created = true) } }107109 is CreateVaultResult.Error -> _state.update { it.copy(loading = false, error = r.reason) }108110 }109111 }@@ -141,7 +143,7 @@141143 val r = vault.unlockWithKey(key)142144 VaultCrypto.zero(key)143145 when (r) {144- is UnlockResult.Success -> _state.update { it.copy(unlocked = true) }146+ is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(unlocked = true) } }145147 else -> _state.update { it.copy(error = "biometric unlock failed", shakeNonce = it.shakeNonce + 1) }146148 }147149 }app/src/main/java/com/peachpasswords/vault/VaultLockTimer.kt+53 −0
@@ -0,0 +1,53 @@1+package com.peachpasswords.vault2+3+import androidx.annotation.VisibleForTesting4+import kotlinx.coroutines.CoroutineScope5+import kotlinx.coroutines.Dispatchers6+import kotlinx.coroutines.Job7+import kotlinx.coroutines.SupervisorJob8+import kotlinx.coroutines.delay9+import kotlinx.coroutines.launch10+import javax.inject.Inject11+import javax.inject.Singleton12+13+/**14+ * Idle auto-lock timer. The vault locks [VaultSettings.autoLockMinutes] after the15+ * last user interaction; a null (or non-positive) setting means "never". [poke]16+ * restarts the countdown, so only the final interaction before the idle window17+ * elapses triggers the lock. Reads the live setting from [VaultRepository] each18+ * time so a settings change takes effect on the next interaction without rewiring.19+ */20+@Singleton21+class VaultLockTimer @Inject constructor(22+ private val vault: VaultRepository,23+) {24+ // Test seam: replaced with a TestScope so the countdown can be driven on25+ // virtual time. Defaults to a process-lifetime background scope in production.26+ @VisibleForTesting27+ internal var scope: CoroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob())28+29+ private var timerJob: Job? = null30+31+ /** Reset the idle countdown. No-op (and cancels) when auto-lock is off or locked. */32+ fun poke() {33+ val minutes = vault.settings.value.autoLockMinutes34+ if (minutes == null || minutes <= 0 || !vault.isUnlocked.value) {35+ cancel()36+ return37+ }38+ timerJob?.cancel()39+ timerJob = scope.launch {40+ delay(minutes * 60_000L)41+ vault.lock()42+ }43+ }44+45+ /** Begin the idle countdown after an unlock. */46+ fun start() = poke()47+48+ /** Stop the countdown (manual lock, background lock). */49+ fun cancel() {50+ timerJob?.cancel()51+ timerJob = null52+ }53+}app/src/test/java/com/peachpasswords/vault/VaultLockTimerTest.kt+87 −0
@@ -0,0 +1,87 @@1+package com.peachpasswords.vault2+3+import kotlinx.coroutines.CoroutineScope4+import kotlinx.coroutines.test.StandardTestDispatcher5+import kotlinx.coroutines.test.advanceTimeBy6+import kotlinx.coroutines.test.runCurrent7+import kotlinx.coroutines.test.runTest8+import org.junit.Assert.assertFalse9+import org.junit.Assert.assertTrue10+import org.junit.Test11+12+/** In-memory [VaultStorage] so the timer can drive a real repository off-device. */13+private class LockTimerFakeStorage : VaultStorage {14+ private var slotA: StoredVault? = null15+ private var slotB: String? = null16+ private var ver: Long = 017+ private var settings = VaultSettings()18+ override fun loadSlotA() = slotA19+ override fun hasVault() = slotA != null20+ override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true }21+ override fun writeSlotB(blobBase64: String): Boolean { slotB = blobBase64; return true }22+ private var decoy: StoredVault? = null23+ override fun loadDecoySlot() = decoy24+ override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true }25+ override fun hasDecoySlot() = decoy != null26+ override fun clearDecoySlot() { decoy = null }27+ override fun version() = ver28+ override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver }29+ override fun loadSettings() = settings30+ override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true }31+ override fun clearVault() { slotA = null; slotB = null; ver = 0; settings = VaultSettings() }32+}33+34+/**35+ * Drives [VaultLockTimer] on virtual time. The timer's coroutine scope is replaced36+ * with one backed by the test scheduler, so [advanceTimeBy] moves the idle clock37+ * deterministically without real waiting.38+ */39+class VaultLockTimerTest {40+41+ private suspend fun unlockedRepo(autoLockMinutes: Int?): VaultRepository {42+ val repo = VaultRepository(LockTimerFakeStorage())43+ repo.createVault("correct horse battery staple")44+ repo.updateSettings(VaultSettings(autoLockMinutes = autoLockMinutes))45+ assertTrue(repo.isUnlocked.value)46+ return repo47+ }48+49+ @Test fun timer_locks_after_configured_delay() = runTest {50+ val repo = unlockedRepo(autoLockMinutes = 1)51+ val timer = VaultLockTimer(repo).also { it.scope = CoroutineScope(StandardTestDispatcher(testScheduler)) }52+ timer.start()53+ advanceTimeBy(59_000); runCurrent()54+ assertTrue("still unlocked before the window elapses", repo.isUnlocked.value)55+ advanceTimeBy(2_000); runCurrent()56+ assertFalse("locked once the idle window elapses", repo.isUnlocked.value)57+ }58+59+ @Test fun poke_resets_the_countdown() = runTest {60+ val repo = unlockedRepo(autoLockMinutes = 1)61+ val timer = VaultLockTimer(repo).also { it.scope = CoroutineScope(StandardTestDispatcher(testScheduler)) }62+ timer.start()63+ advanceTimeBy(50_000); runCurrent()64+ timer.poke() // 50s in — restart the 60s window65+ advanceTimeBy(50_000); runCurrent()66+ assertTrue("poke pushed the lock past the original deadline", repo.isUnlocked.value)67+ advanceTimeBy(11_000); runCurrent()68+ assertFalse("locks 60s after the last poke", repo.isUnlocked.value)69+ }70+71+ @Test fun cancel_prevents_firing() = runTest {72+ val repo = unlockedRepo(autoLockMinutes = 1)73+ val timer = VaultLockTimer(repo).also { it.scope = CoroutineScope(StandardTestDispatcher(testScheduler)) }74+ timer.start()75+ timer.cancel()76+ advanceTimeBy(120_000); runCurrent()77+ assertTrue("cancelled timer never locks", repo.isUnlocked.value)78+ }79+80+ @Test fun null_auto_lock_never_fires() = runTest {81+ val repo = unlockedRepo(autoLockMinutes = null)82+ val timer = VaultLockTimer(repo).also { it.scope = CoroutineScope(StandardTestDispatcher(testScheduler)) }83+ timer.start()84+ advanceTimeBy(3_600_000); runCurrent()85+ assertTrue("null autoLockMinutes means never lock", repo.isUnlocked.value)86+ }87+}