feat: register a bulwark account with android's contacts and calendar sync #34

Android syncs the contacts and calendars of other services through
accounts and sync adapters. This adds the Android side of device sync:
a "Bulwark" account type (`${applicationId}.account`, set in
build.gradle), an authenticator that sends Settings → Add account to
the app instead of asking for credentials, and one sync adapter each
for the contacts and the calendar provider.

An adapter does no syncing itself. It starts the BulwarkDeviceSync
headless JS task from the main thread, through the helper the push job
uses, waits for the task's report and hands it to SyncManager as the
SyncResult. The engine that will write the report runs in JS, so the
existing client stays the only owner of token refresh, mTLS and
session discovery; for now the task only reports that device sync is
off. Nothing in the app creates an account yet, so the feature stays
unreachable until its settings land. Debug builds get an adb hook for
the device tests (DeviceSyncDebugReceiver, shell only).

Hard errors are reported without progress: SyncManager retries a
failed sync at once when it made progress, which would loop an auth
failure.

Checked on the API 34 and API 32 emulators with the debug build, from
logcat and `dumpsys content`:
- app in the foreground: report after 319-334 ms
- app in the background: 320-327 ms
- process killed: SyncManager starts it for the sync service, React
  Native starts headless, and the report arrives after 16.2 s (API 34)
  and 8.5 s (API 32), most of it the debug bundle download from Metro
- an io report is retried with back-off and an auth report is not
  retried; too many deletions shows the system's "Attempted to delete
  too many Contacts." notification
- with Metro unreachable the adapter gives up after 2 s instead of
  waiting for its deadline
- `gradlew :app:testDebugUnitTest`: 10 JVM tests for the report mapping
This commit is contained in:
Linus Rath
2026-09-27 01:53:40 +02:00
parent 1f7acaf08a
commit ed2525e5cb
20 changed files with 1434 additions and 1 deletions
@@ -0,0 +1,84 @@
package com.anonymous.bulwarkmobile.sync
import org.junit.Assert.assertEquals
import org.junit.Test
class SyncReportsTest {
private fun report(outcome: String, extra: String = "") = """
{"v":1,"runId":"r","authority":"com.android.contacts","outcome":"$outcome",
"startedAt":0,"durationMs":5,"conflicts":0,"itemErrors":[],
"stats":{"downloaded":{"created":3,"updated":2,"deleted":1},
"uploaded":{"created":1,"updated":4,"deleted":2},"entries":13,"skipped":1}$extra}
""".trimIndent()
@Test
fun `a run without a report is a soft error`() {
assertEquals(SyncOutcome(ioErrors = 1), SyncReports.outcome(null, "task finished"))
}
@Test
fun `an unreadable report is a soft error`() {
assertEquals(SyncOutcome(ioErrors = 1), SyncReports.outcome("{not json", "reported"))
}
@Test
fun `ok sums both directions into the framework's stats`() {
assertEquals(
SyncOutcome(inserts = 4, updates = 6, deletes = 3, entries = 13, skipped = 1),
SyncReports.outcome(report("ok"), "reported"),
)
}
@Test
fun `io keeps the progress so the framework retries at once`() {
val outcome = SyncReports.outcome(report("io"), "reported")
assertEquals(1L, outcome.ioErrors)
assertEquals(4L, outcome.inserts)
}
@Test
fun `auth is hard and reports no progress, or the framework would retry it at once`() {
assertEquals(
SyncOutcome(authErrors = 1, entries = 13, skipped = 1),
SyncReports.outcome(report("auth"), "reported"),
)
}
@Test
fun `permission, unsupported and safety aborts are database errors without progress`() {
for (outcome in listOf("permission", "unsupported", "safetyAbort")) {
assertEquals(
outcome,
SyncOutcome(databaseError = true, entries = 13, skipped = 1),
SyncReports.outcome(report(outcome), "reported"),
)
}
}
@Test
fun `too many deletions reports the pending deletions for the system notification`() {
assertEquals(
SyncOutcome(tooManyDeletions = true, deletes = 42, entries = 13, skipped = 1),
SyncReports.outcome(report("tooManyDeletions", ""","tooManyDeletions":{"count":42,"threshold":20}"""), "reported"),
)
}
@Test
fun `delayUntil and moreRecordsToGet reach the framework`() {
val outcome = SyncReports.outcome(report("ok", ""","delayUntil":1790000000,"moreRecordsToGet":true"""), "reported")
assertEquals(1790000000L, outcome.delayUntil)
assertEquals(true, outcome.fullSyncRequested)
}
@Test
fun `cancelled and disabled runs are not errors`() {
for (outcome in listOf("cancelled", "disabled")) {
assertEquals(0L, SyncReports.outcome(report(outcome), "reported").ioErrors)
}
}
@Test
fun `an outcome this build does not know is retried`() {
assertEquals(1L, SyncReports.outcome(report("somethingNew"), "reported").ioErrors)
}
}