feat(android): auto-backup triggers — MediaStore observer, app-open run, configurable interval #1463
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/auto-backup-triggers"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
On a Pixel 2, new camera photos were only backed up when the user pressed „Jetzt sichern" or the fixed 6 h periodic run happened to fire. There was no trigger for new media.
The three triggers
All three wake the SAME pipeline — the engine diffs MediaStore against Room state on every run and stays the source of truth; a trigger only needs to enqueue an expedited one-off (
BackupScheduler.enqueueNow, whose REPLACE semantics and RUNNING guard are untouched).1. MediaStore ContentObserver (process-lived) — new
MediaStoreChangeObserverin the sync package registers on the external images + video collections (notify-for-descendants, so per-item camera URIs are seen; both URIs are valid at minSdk 25).onChangeis CHEAP: no MediaStore query, it feeds a 10 s trailing debounce (a camera burst fires one notification per frame — the window is anchored at the FIRST frame, so latency is capped at burst-start + 10 s and the whole burst collapses into ONE enqueue), then calls the gatedenqueueNow. Registered fromAndroidSyncController.activatewhen a session exists AND backup is unpaused; unregistered indeactivate(logout), plus defensively on re-activate.Platform constraint (documented in KDoc): Android gives third-party apps no push channel for new media — the insert broadcast was deprecated years ago, and WorkManager cannot watch content URIs on its own cadence. A ContentObserver is the only in-process signal, and process death silently removes it. The observer is therefore best-effort in addition to the guarantees — the periodic run and the app-open run are what a user can rely on.
2. Auto-run on app open —
AppContainer.bootstrap()(runs once per process fromApplication.onCreate) performs one gatedenqueueNowafterAndroidSyncController.activatesucceeds. This covers exactly the windows the observer cannot see: while the process was dead.3. Configurable periodic interval —
SettingsStore.backupIntervalMinutes(default 360 = the historic 6 h). Deviation from the brief, explicitly flagged: the brief asked forbackupIntervalHours: Longand a 15-minute UI option; a hours-only Long cannot express 15 minutes. The setting is stored in minutes (backupIntervalMinutes) — same unit as WorkManager's own floor — so both requirements hold.BackupScheduler.schedulePeriodic(context, requiresCharging, intervalMinutes)clamps to WorkManager's 15-min periodic floor (pureclampPeriodicIntervalMinutes, JVM-tested: a corrupt stored value degrades to the fastest legal cadence, never crashes scheduling);ExistingPeriodicWorkPolicy.UPDATEis kept, so a cadence change reschedules cleanly without cancelling an in-flight run. The interval threads throughBackupControl.schedulePeriodic(intervalMinutes)(interface +WorkManagerBackupControl+ test fake),AndroidSyncController.activate(a changed interval survives startup/re-login instead of silently reverting to 6 h), andBackupStatusViewModel.setBackupIntervalMinutes(mirrorssetRequiresCharging: persist → reschedule if not paused → UI state). The backup settings screen gains a filter-chip row — 15 Min. / 1 / 6 / 12 / 24 Std. — copied from the existing design language (label + hint + horizontally scrollable chip line; no visual redesign). Strings added in all three locales (de base, en, da).Shared gate
Both automatic triggers share one pure predicate,
isAutoBackupTriggerAllowed(dependenciesConfigured, backupPaused): no wake whenSyncRuntime.dependencies == null(the worker would failnot_configuredand flip the last-run card into a fake failure), no wake while paused (pausing must silence every automatic path; the observer additionally re-checks at fire time, since registration and fire can straddle a pause/logout). The manual „Jetzt sichern" press deliberately bypasses this gate — unchanged behavior.Scope guards
BackupWorkerresult mapping,enqueueNowREPLACE semantics, RUNNING guard: untouched.Tests (915 app tests pre-rebase / 925 post-rebase incl. #1462, 0 failed;
:core:testgreen;assembleDebuggreen)AutoBackupTriggerTest— gate truth table (all four combinations).MediaStoreChangeObserverTest— pure debounce window math (boundary at exactly the delay) + observer state machine on a fake clock: first-notification anchoring, fire-exactly-once per burst, burst frames don't extend the window, straggler opens a fresh window. Uses a handler-less test seam (plain JVM tests cannot touchLooper); the Handler self-timer path is exercised on-device.BackupRetryBackoffTest— extended with the interval clamp table.BackupStatusViewModelTest— interval persists, reschedules with the chosen cadence, mirrors into UI state; paused path persists without rescheduling; initial state reads the stored cadence; unpause reschedules with the stored cadence, not a hardcoded default.EncryptedSettingsStoreRobolectricTest— default = 6 h; persists across store instances.Version
0.10.19 → 0.10.20 +
apps/android/CHANGELOG.mdentry (repo convention).Device context
Pixel 2 / LineageOS — the reporting device. The observer + app-open triggers remove the "shooting → hours-long wait" gap; with the interval settable down to 15 min, the periodic run itself becomes a tighter backstop for the process-death windows.