From 17a44369b6b2e03465b12902940e128079537388 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 21 Oct 2026 19:41:46 +0200 Subject: [PATCH] refactor(timers): settle timer vibration by copying the alarm default Telling a fresh install from an updated one (package install times, the boot id, a string-encoded tri-state) was more machinery than the default is worth. Timer vibration is a plain boolean again, defaulting to on like the alarm default, and a startup copy writes the alarm default into it once, so no install's timers change on update. The copy runs in its own coroutine, off the engines' boot pass. Fresh installs start with timer vibration on, matching alarms; the switch is one tap away in Settings > Timers. Refs #22 --- CHANGELOG.md | 5 +- .../de/jeanlucmakiola/clockula/ClockulaApp.kt | 22 +--- .../clockula/data/prefs/ClockPrefs.kt | 13 +-- .../clockula/data/prefs/SettingsPrefs.kt | 42 ++----- .../clockula/domain/ClockDefaults.kt | 6 +- .../clockula/data/prefs/ClockPrefsTest.kt | 7 +- .../clockula/data/prefs/SettingsPrefsTest.kt | 107 ++---------------- docs/ARCHITECTURE.md | 22 ++-- 8 files changed, 43 insertions(+), 181 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e278004..c974f57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,9 +40,8 @@ The first release of Clockula: alarms, timers, a stopwatch and a world clock. to the clock. - Timers have their own vibration switch and an optional gradual volume, both apart from the alarms'. A kitchen timer can just beep while alarms still - vibrate; a silent timer still vibrates. New installs start with timer - vibration off; an updated install keeps vibrating its timers if it did - before. + vibrate; a silent timer still vibrates. Timer vibration starts out matching + the alarm vibration setting, so nothing changes until you change it. - **Stopwatch** with laps, the fastest and slowest marked, and Lap and Pause in the notification. It keeps counting while the app is closed. - **World clock.** An analog face for your own time, plus cities from the diff --git a/app/src/main/java/de/jeanlucmakiola/clockula/ClockulaApp.kt b/app/src/main/java/de/jeanlucmakiola/clockula/ClockulaApp.kt index 30c4d1f..4b949c6 100644 --- a/app/src/main/java/de/jeanlucmakiola/clockula/ClockulaApp.kt +++ b/app/src/main/java/de/jeanlucmakiola/clockula/ClockulaApp.kt @@ -72,7 +72,6 @@ class ClockulaApp : Application() { // a cold start caused by the fire broadcast must not interleave its // read-modify-write over `alarm_states` with the fire's own. scope.launch { - settleTimerVibrate() engine.onBootCompleted() // After the alarm engine's pass, and idempotent within a boot: the // boot-id gate makes the second repair a no-op, so neither engine @@ -91,29 +90,18 @@ class ClockulaApp : Application() { // Never completes on its own โ€” it is the widgets' one refresh trigger // for as long as the process lives (M10, docs/PLAN.md ยง9). scope.launch { widgetSync.run() } + + scope.launch { settleTimerVibrate() } } - /** - * Ahead of the engines' pass, because the boot gate writes the boot id, and - * its absence is half of how a fresh install is told apart. Never - * in the way of that pass: a failure here leaves the setting unsettled, - * which reads as the old behaviour (#22). - */ + /** Off the engines' pass: a preference copy has no business delaying an alarm. */ private suspend fun settleTimerVibrate() { try { - settings.settleTimerVibrate(freshInstall = ::isFreshInstall) + settings.settleTimerVibrate() } catch (cancelled: CancellationException) { throw cancelled } catch (_: Exception) { - // Unsettled reads as the old behaviour. The next launch tries again, - // and by then the boot id is there, so even a fresh install settles - // on the alarm default: vibrating, the safe side to err on. + // Until it lands timers vibrate, the default; the next launch tries again. } } - - /** Never updated since it was installed. Unknown reads as not fresh: the safe side. */ - private fun isFreshInstall(): Boolean = runCatching { - val info = packageManager.getPackageInfo(packageName, 0) - info.firstInstallTime == info.lastUpdateTime - }.getOrDefault(false) } diff --git a/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefs.kt b/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefs.kt index 118a782..070d2d1 100644 --- a/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefs.kt @@ -29,15 +29,12 @@ object ClockPrefs { val timerRingtoneUri: Pref = nullableStringPref("default_timer_ringtone_uri").blankAsAbsent() /** - * Null โ€” absent, or not a boolean โ€” means "never settled", and reads as - * [vibrate]: what every timer vibrated by before the setting existed (#22). - * `SettingsPrefs.settleTimerVibrate` writes it once at startup, off on a - * fresh install. Stored as text because the kit has no nullable boolean. + * The timers' own vibration (#22). Until it existed a timer vibrated + * exactly when [vibrate] said so; `SettingsPrefs.settleTimerVibrate` copies + * that over once, so nobody's timers change on update. The default is + * [vibrate]'s, for the moment before that lands. */ - val timerVibrate: Pref = nullableStringPref("default_timer_vibrate").map( - decode = { it?.toBooleanStrictOrNull() }, - encode = { it?.toString() }, - ) + val timerVibrate: Pref = booleanPref("default_timer_vibrate", true) val timerVolumeRampSeconds: Pref = intPref("default_timer_volume_ramp_seconds", 0).clampedTo(ClockLimits.VOLUME_RAMP_SECONDS) diff --git a/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefs.kt index b4eea11..f5bb996 100644 --- a/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefs.kt @@ -1,7 +1,6 @@ package de.jeanlucmakiola.clockula.data.prefs import androidx.datastore.preferences.core.booleanPreferencesKey -import androidx.datastore.preferences.core.stringPreferencesKey import de.jeanlucmakiola.clockula.domain.ClockDefaults import de.jeanlucmakiola.clockula.domain.DismissChallenge import de.jeanlucmakiola.clockula.domain.backup.BackupSettings @@ -77,8 +76,7 @@ class SettingsPrefs @Inject constructor(private val store: PrefStore) { partial.copy( alarmRingtoneUri = alarmRingtoneUri, timerRingtoneUri = timerRingtoneUri, - // Unsettled follows the alarm default, which is what it did before #22. - timerVibrate = timerVibrate ?: partial.vibrate, + timerVibrate = timerVibrate, timerVolumeRampSeconds = timerVolumeRampSeconds, homeZoneId = homeZoneId, ) @@ -124,39 +122,17 @@ class SettingsPrefs @Inject constructor(private val store: PrefStore) { suspend fun setTimerVibrate(enabled: Boolean) = store.set(ClockPrefs.timerVibrate, enabled) /** - * Gives the timers' vibration a value of its own, once (#22). Until then a - * timer vibrated exactly when the alarm default said so, and an install - * from before keeps that: the alarm default is copied, so changing it - * later no longer drags the timers along. Only a fresh install starts with - * it off: a [freshInstall] package *and* a store without a boot id, which - * the engines write on every launch after this runs. Either signal alone - * mistakes something for fresh (an Auto Backup restore brings the old - * device's boot id; a store replaced after corruption is not a fresh - * package), and that mistake would silently stop someone's timers buzzing. - * The boot id rather than "empty" because the UI may write the store first. - * - * Best-effort by design: until it lands, and if it never does, the read - * side's fallback keeps the old behaviour, so a failed write costs nothing. - * Once settled it is one read; [freshInstall] is asked only when needed. + * Gives the timers' vibration a value of its own, once (#22): the alarm + * default, which is what every timer vibrated by until then. From here on + * the two move apart. A no-op once the key exists. */ - suspend fun settleTimerVibrate(freshInstall: () -> Boolean) { - if (store.get(ClockPrefs.timerVibrate) != null) return - // Asked outside the transaction: a binder call has no business holding - // the store's write lock. - val freshPackage = freshInstall() - // One transaction, so nothing written meanwhile can make the snapshot - // it decides on stale. Raw keys for the reads: the kit reads a Pref - // only from a store, not from a transaction's preferences. + suspend fun settleTimerVibrate() { store.edit { prefs -> - val stored = prefs[stringPreferencesKey(ClockPrefs.timerVibrate.name)] - // Settled meanwhile; a garbled value falls through and is replaced. - if (stored?.toBooleanStrictOrNull() != null) return@edit - val neverRan = stringPreferencesKey(SystemPrefs.lastBootId.name) !in prefs - prefs[ClockPrefs.timerVibrate] = if (neverRan && freshPackage) { - false - } else { + // Raw keys for the reads: the kit reads a Pref only from a store, not + // from a transaction's preferences. Both are plain boolean prefs. + if (booleanPreferencesKey(ClockPrefs.timerVibrate.name) in prefs) return@edit + prefs[ClockPrefs.timerVibrate] = prefs[booleanPreferencesKey(ClockPrefs.vibrate.name)] ?: ClockPrefs.vibrate.default - } } } diff --git a/app/src/main/java/de/jeanlucmakiola/clockula/domain/ClockDefaults.kt b/app/src/main/java/de/jeanlucmakiola/clockula/domain/ClockDefaults.kt index ef8fc73..f2152d5 100644 --- a/app/src/main/java/de/jeanlucmakiola/clockula/domain/ClockDefaults.kt +++ b/app/src/main/java/de/jeanlucmakiola/clockula/domain/ClockDefaults.kt @@ -6,8 +6,8 @@ package de.jeanlucmakiola.clockula.domain * * [vibrate] and [volumeRampSeconds] are the alarms'; timers have their own * [timerVibrate] and [timerVolumeRampSeconds], because a kitchen timer that - * should just beep is not an alarm that should wake someone. Their defaults - * are a fresh install's: no vibration, no ramp. + * should just beep is not an alarm that should wake someone. They start as + * timers have always rung: vibrating like the alarms' default, unramped. */ data class ClockDefaults( val snoozeMinutes: Int = 10, @@ -16,7 +16,7 @@ data class ClockDefaults( val volumeRampSeconds: Int = 15, val alarmRingtoneUri: String? = null, val timerRingtoneUri: String? = null, - val timerVibrate: Boolean = false, + val timerVibrate: Boolean = true, val timerVolumeRampSeconds: Int = 0, val dismissChallenge: DismissChallenge = DismissChallenge.NONE, val homeZoneId: String? = null, diff --git a/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefsTest.kt index fc833b0..aa495dd 100644 --- a/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/ClockPrefsTest.kt @@ -37,8 +37,6 @@ class ClockPrefsTest { @Test fun `a fresh install reads the documented defaults`(@TempDir tempDir: Path) = runTest { val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - // As ClockulaApp's startup pass does on a fresh install. - prefs.settleTimerVibrate(freshInstall = { true }) assertThat(prefs.defaults.first()).isEqualTo(ClockDefaults()) } @@ -53,7 +51,7 @@ class ClockPrefsTest { prefs.setVolumeRampSeconds(0) prefs.setAlarmRingtoneUri("content://a") prefs.setTimerRingtoneUri("content://t") - prefs.setTimerVibrate(true) + prefs.setTimerVibrate(false) prefs.setTimerVolumeRampSeconds(25) prefs.setDismissChallenge(DismissChallenge.HOLD) prefs.setHomeZoneId("Europe/Berlin") @@ -66,7 +64,7 @@ class ClockPrefsTest { volumeRampSeconds = 0, alarmRingtoneUri = "content://a", timerRingtoneUri = "content://t", - timerVibrate = true, + timerVibrate = false, timerVolumeRampSeconds = 25, dismissChallenge = DismissChallenge.HOLD, homeZoneId = "Europe/Berlin", @@ -77,7 +75,6 @@ class ClockPrefsTest { @Test fun `the defaults do not re-emit when the same value is written again`(@TempDir tempDir: Path) = runTest { val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - prefs.settleTimerVibrate(freshInstall = { true }) prefs.defaults.test { assertThat(awaitItem()).isEqualTo(ClockDefaults()) diff --git a/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefsTest.kt index c44f501..948b50b 100644 --- a/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefsTest.kt @@ -78,7 +78,7 @@ class SettingsPrefsTest { snoozeMinutes = 99, volumeRampSeconds = 99, alarmRingtoneUri = " ", - timerVibrate = true, + timerVibrate = false, timerVolumeRampSeconds = 99, ), timerPresets = listOf(5.minutes, 1.minutes, 5.minutes), @@ -90,7 +90,7 @@ class SettingsPrefsTest { assertThat(prefs.currentDefaults().snoozeMinutes).isEqualTo(60) assertThat(prefs.currentDefaults().volumeRampSeconds).isEqualTo(60) assertThat(prefs.currentDefaults().alarmRingtoneUri).isNull() - assertThat(prefs.currentDefaults().timerVibrate).isTrue() + assertThat(prefs.currentDefaults().timerVibrate).isFalse() assertThat(prefs.currentDefaults().timerVolumeRampSeconds).isEqualTo(60) assertThat(prefs.timerPresets.first()).containsExactly(1.minutes, 5.minutes).inOrder() } @@ -170,33 +170,11 @@ class SettingsPrefsTest { } @Test - fun `an unsettled timer vibration follows the alarm default, as timers did before`( - @TempDir tempDir: Path, - ) = runTest { - val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - - prefs.setVibrate(false) - assertThat(prefs.currentDefaults().timerVibrate).isFalse() - prefs.setVibrate(true) - assertThat(prefs.currentDefaults().timerVibrate).isTrue() - } - - @Test - fun `a fresh install settles timer vibration off`(@TempDir tempDir: Path) = runTest { - val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - - prefs.settleTimerVibrate(freshInstall = { true }) - - assertThat(prefs.currentDefaults().timerVibrate).isFalse() - assertThat(prefs.currentDefaults().vibrate).isTrue() - } - - @Test - fun `an older install settles timer vibration from the alarm default`(@TempDir tempDir: Path) = runTest { + fun `settling copies the alarm default, as timers did before`(@TempDir tempDir: Path) = runTest { val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) prefs.setVibrate(false) - prefs.settleTimerVibrate(freshInstall = { false }) + prefs.settleTimerVibrate() // Settled, so the two now move apart. prefs.setVibrate(true) @@ -204,92 +182,23 @@ class SettingsPrefsTest { } @Test - fun `an older install that never touched alarm vibration keeps vibrating its timers`( + fun `an install that never touched alarm vibration keeps vibrating its timers`( @TempDir tempDir: Path, ) = runTest { val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - prefs.setSnoozeMinutes(7) - prefs.settleTimerVibrate(freshInstall = { false }) + prefs.settleTimerVibrate() prefs.setVibrate(false) assertThat(prefs.currentDefaults().timerVibrate).isTrue() } - @Test - fun `an updated install with an empty store is not taken for fresh`(@TempDir tempDir: Path) = runTest { - // A store replaced after corruption, or never written: empty, but not new. - val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - - prefs.settleTimerVibrate(freshInstall = { false }) - - assertThat(prefs.currentDefaults().timerVibrate).isTrue() - } - - @Test - fun `a new package with a restored store is not taken for fresh`(@TempDir tempDir: Path) = runTest { - // Auto Backup restored the old device's store, boot id and all. - val store = PrefStore(newDataStore(tempDir)) - val prefs = SettingsPrefs(store) - store.set(SystemPrefs.lastBootId, "old-device-boot") - - prefs.settleTimerVibrate(freshInstall = { true }) - - assertThat(prefs.currentDefaults().timerVibrate).isTrue() - } - - @Test - fun `a fresh install is still fresh when the UI wrote the store first`(@TempDir tempDir: Path) = runTest { - val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - prefs.markNotificationPermissionAsked() - - prefs.settleTimerVibrate(freshInstall = { true }) - - assertThat(prefs.currentDefaults().timerVibrate).isFalse() - } - - @Test - fun `once settled, the package is not asked again`(@TempDir tempDir: Path) = runTest { - val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - prefs.settleTimerVibrate(freshInstall = { true }) - - prefs.settleTimerVibrate(freshInstall = { error("asked again") }) - - assertThat(prefs.currentDefaults().timerVibrate).isFalse() - } - @Test fun `settling never overwrites a choice`(@TempDir tempDir: Path) = runTest { val prefs = SettingsPrefs(PrefStore(newDataStore(tempDir))) - prefs.setTimerVibrate(true) + prefs.setTimerVibrate(false) - prefs.settleTimerVibrate(freshInstall = { true }) - - assertThat(prefs.currentDefaults().timerVibrate).isTrue() - } - - @Test - fun `a garbled timer vibration reads as unsettled`(@TempDir tempDir: Path) = runTest { - val dataStore = newDataStore(tempDir) - val prefs = SettingsPrefs(PrefStore(dataStore)) - dataStore.updateData { stored -> - stored.toMutablePreferences().apply { this[stringPreferencesKey("default_timer_vibrate")] = "maybe" } - } - - assertThat(prefs.currentDefaults().timerVibrate).isTrue() - } - - @Test - fun `a garbled timer vibration is settled like an absent one`(@TempDir tempDir: Path) = runTest { - val dataStore = newDataStore(tempDir) - val prefs = SettingsPrefs(PrefStore(dataStore)) - prefs.setVibrate(false) - dataStore.updateData { stored -> - stored.toMutablePreferences().apply { this[stringPreferencesKey("default_timer_vibrate")] = "maybe" } - } - - prefs.settleTimerVibrate(freshInstall = { false }) - prefs.setVibrate(true) + prefs.settleTimerVibrate() assertThat(prefs.currentDefaults().timerVibrate).isFalse() } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 7f5db0a..b28a77e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -1908,19 +1908,15 @@ stays flat. `VolumeRamp.levelAt(_, 0s)` already returns `1f`, so Vibration is `ClockDefaults.timerVibrate`, never the alarms' `vibrate`: an alarm that should buzz the bed and a kitchen timer that should only beep are -different questions. A fresh install has it **off**. An install from before -the setting existed had its timers vibrate exactly when the alarm default did, -and keeps that. The key is nullable: absent reads as the alarm default, and -`SettingsPrefs.settleTimerVibrate` writes a value of its own once, at the head -of `ClockulaApp`'s startup pass (before the boot gate writes the store). Only a -package never updated since install *and* a store without `last_boot_id` -(the engines write it on every launch, after the settle) count as fresh and -get `false`; anything else (an update, an Auto Backup restore, which brings the -old boot id, a store replaced after corruption) gets the alarm default copied. -The boot id rather than an empty store, because the UI may write first. It is deliberately not a -DataStore migration: a migration's write runs inside the store's -initialisation, and a failed one would fail every read, the alarm path's -included. A failed settle only leaves the fallback in place. +different questions. Until #22 a timer vibrated exactly when the alarm +default did, so nobody's timers change on update: `SettingsPrefs.settleTimerVibrate` +copies the alarm default into the new key once, from its own coroutine at +startup (never ahead of the engines' pass), and from then on the two move +apart. The key's own default is `true`, the alarm default's, for the moment +before that lands; a failed copy is tried again on the next launch. Fresh +installs therefore start with timer vibration on, like alarms; telling a fresh +install from an updated one reliably was more machinery than the default is +worth, and the switch is one tap away. A ring restarted mid-session (handed to a second expired timer, back after an alarm held it, a recreated service) picks its ramp up where the session is,