Skip to content

Add a setting for how long location history is kept - #321

Open
lazzurs wants to merge 1 commit into
parawanderer:mainfrom
lazzurs:feat/location-history-retention
Open

lazzurs wants to merge 1 commit into
parawanderer:mainfrom
lazzurs:feat/location-history-retention

Conversation

@lazzurs

@lazzurs lazzurs commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Adds a setting for how long location history is kept, the retention half of audit finding S-04. Until now nothing in the app ever deleted a LocationReport, so a phone held every position of every tag since install. #315 lets people delete a tag's history or everything; this lets them keep a rolling window instead.

What it does

  • Settings → Tags → Keep location history: Forever, 1 year, 6 months, 3 months or 1 month. The subtitle shows the current choice.
  • Forever is the default, and it changes nothing: until somebody picks a period, the app doesn't even open the database for this at start. Nobody loses history to a setting they never saw.
  • Choosing a shorter period asks first ("Delete older history?", which mentions that imported history counts too and suggests Export History for a copy). Delete saves the period, then deletes what's now too old. A longer period, or going back to Forever, is only saved, with no question, since there's nothing new to delete.
  • After that the app prunes each time it starts, on Schedulers.io(), in OpenAirTagApplication (main process only, after the :adiprobe/:macadi early return). That covers the background scan's process too. I chose app start over "after history is persisted" because the write paths are in BeaconRepository, which Delete a removed tag's keys, and offer to erase its history #315 changes heavily, and a once-per-start delete is cheap and enough for a period measured in months.

What's kept, and why

  • Each tag's newest report, however old. That row is the tag's last known position: the map marker, "last updated" and Navigate all read it. Without this, a tag that hasn't been heard for longer than the period would show "No last location known", which looks like the app lost it. The delete compares each row against its own tag's newest timestamp (correlated subquery, served by the (beacon_id, timestamp) index).
  • DailyHistoryFetchRecord (the per-day "already fetched" markers). They hold no position, and removing one makes the History screen ask Apple for that day again. The shortest choice is a month, so a deleted day is always far older than anything Apple still has.
  • Periods are stored in days, and each is the longest the named period can be on a calendar (366, 184, 92, 31), so "1 month" never keeps less than a month. A stored value that isn't one of these, for example from a later version after a downgrade, rounds up to the next choice that keeps at least as much, never down.

Sequencing

The save is a DataStore write, and updateDataAsync starts when called, so save.andThen(prune) would start the delete regardless. It uses RxFlows.onlyAfter from #317: the delete is only created once the save has completed. If the save fails, nothing is deleted, the old period is restored on screen and the existing "Error updating settings" message is shown. If the delete fails, the new period stays and a toast says the app will try again at the next start.

Code

  • UserSettings.historyRetentionDays + HISTORY_RETENTION_DAYS int key in user_settings, read and written like LEFT_BEHIND_AFTER_SECONDS (0 for "forever", since an int key can't hold null).
  • LocationReportDao.deleteOlderThanExceptNewest(cutoff).
  • HistoryPruner (interface) and LocationHistoryRetention (the real one), behind AppDependencies.historyPruner so the Settings test can choose a period without deleting the history on whatever phone runs the suite.
  • ui/settings/HistoryRetentionRow, in the same shape as UpdateSettingsRow. It shares SettingsActivity's UserSettings object, because the screen saves the whole object on other changes and a second copy would write the old period back.
  • No schema change: no new table or column, so no migration (AGENTS rule 1).
  • Ten new strings in all ten locales via scripts/add_strings.py, --check passes. German and Dutch use the informal register as the rest of those files do. I placed them next to the left-behind strings rather than at the end of each file, so they don't collide with Delete a removed tag's keys, and offer to erase its history #315's additions there.

Tests

  • db/repo/model/KeepingLocationHistoryTest (JVM): unset, zero and negative mean forever; each choice resolves to itself; unknown values round up, never down; cutoff maths, including "no cutoff" for forever.
  • ui/settings/HistoryRetentionRowTest (JVM): which changes ask before deleting (any period from Forever, any shorter period) and which don't (longer, Forever); every choice has its own label, in order.
  • db/repo/PruningOldLocationHistoryTest (in-memory Room, fixed clock): Forever deletes nothing; older reports go and newer stay; a tag keeps its newest however old; a tag with one old report keeps it; each tag is judged on its own newest and the map query still returns both; a report exactly at the cutoff stays; local and Google reports follow the same rule; fetch markers are untouched.
  • ui/settings/HistoryRetentionSettingTest (Espresso, fake pruner, DeviceStateGuard): default subtitle is Forever; choosing a month asks; Cancel saves and deletes nothing; confirming deletes with 31 days, and the fake reads the stored period when it's called, so it fails if the delete started before the save landed; back to Forever and a longer period save without asking or deleting; a shorter period from a year asks again; a failed delete keeps the new period.

Verified

  • Local: compile (app and androidTest), :app:testDebugUnitTest (587 tests, 0 failures, 1 skipped), add_strings.py --check, check_java_imports.py. :app:lintDebug totals are identical to main (no new findings).
  • Room compiled and validated the new DELETE query at build time.

Not verified

  • I have no KVM here, so the two instrumented test classes have only run in PR CI, not locally or on a phone. I haven't looked at the row or the dialogs on a real device.
  • The app-start prune isn't covered by a test of its own: the Application starts before any test can set a period. The delete it calls is the one PruningOldLocationHistoryTest covers, and with the default it returns before touching the database.
  • No VACUUM after pruning, so deleted rows can stay in the database file's free pages until SQLite reuses them. Vacuuming on every start would rewrite the whole file each time. If Delete a removed tag's keys, and offer to erase its history #315 merges, its compaction could be run after an explicit change in Settings; I've left that out so this PR stands alone.
  • How long the first prune of a very large history takes hasn't been measured. It runs off the main thread either way.

Relationship

Part of #318 (the retention half of "Encrypt the key columns … and add a location-history retention setting"; encryption at rest will get its own issue first). parawanderer agreed to this follow-up on #315.

Branched from main and independent of #313, #315 and #320. #315 is still open and also touches SettingsActivity, activity_settings.xml, AppDependencies and all ten strings.xml. I kept this PR's edits away from #315's hunks in those files, and git merge-tree merges it cleanly with #315, #313 and #320 in either order.

🤖 Generated with Claude Code

Settings -> Tags -> Keep location history: forever (the default), 1
year, 6 months, 3 months or 1 month. Until now the app never deleted a
location report, and with the default it still doesn't: nothing is
read from the database at start unless a period has been chosen.

Choosing a shorter period asks first, then saves it and deletes older
reports. The save goes first through RxFlows.onlyAfter, so a failed
save deletes nothing (updateDataAsync starts writing when called,
so store.andThen(prune) would not wait for it). A longer
period or forever is only saved. After that the app prunes on each
start, off the main thread.

Each tag's newest report is kept however old, since it is the tag's
last known position on the map, not history. The per-day fetch
markers are kept too, so the History screen doesn't ask Apple for a
day it has deleted; the shortest period, a month, is well past what
Apple keeps.

The pruner sits behind AppDependencies so the Settings test can drive
the row without deleting a phone's real history.

Tests: KeepingLocationHistoryTest and HistoryRetentionRowTest (JVM),
PruningOldLocationHistoryTest (in-memory Room) and
HistoryRetentionSettingTest (Espresso, fake pruner). Strings added to
all ten locales with add_strings.py.

Part of parawanderer#318.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@parawanderer

Copy link
Copy Markdown
Owner

Same flake as #320, at 00:24 UTC: DebugModeReachesEveryScreenTest reporting "no history row was built at all". Nothing to do with this PR. Rerun is queued.

#322 replaces the seeded times with ones anchored to local midnight, so after it merges a rebase here ends the problem for good.

Drafted by Claude Code, reviewed by @parawanderer

This branch was previously deployed

1 inactive deployment
Android Build — 8e606fe8 Deployed Nov 24, 2026 by lazzurs via Instrumented tests (emulator) #430
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants