fix(#303): nudge after failed writes again, per-type account visibility

This commit is contained in:
makiolaj committed 2026-11-13 19:04:07 +01:00
1 parent 1688b206a5
commit 8c31e0f6ee
5 files changed
+52 -20

No files matched your search

@@ -88,10 +88,11 @@ class CalendarRepositoryImpl @Inject constructor(
override val providerChanges: Flow<Unit> get() = ticks
/**
* Run a provider write on [io], then re-query whether or not it threw. Once
* it succeeded, the accounts behind [syncCalendars] (read before the write,
* which may delete the row) get one sync nudge each (#303) — once per write,
* so a multi-row save or a whole import asks once per account.
* Run a provider write on [io], then re-query whether or not it threw. The
* accounts behind [syncCalendars] (read before the write, which may delete
* the row) then get one sync nudge each (#303), also after a failure, which
* may have landed part of a multi-row write. Once per write, so a multi-row
* save or a whole import asks once per account.
*/
private suspend fun <T> write(
syncCalendars: suspend () -> Collection<Long?>,
@@ -99,10 +100,11 @@ class CalendarRepositoryImpl @Inject constructor(
): T = withContext(io) {
val accounts = syncAccountsOf(syncCalendars)
try {
block().also { requestSyncs(accounts, userInitiated = false) }
block()
} finally {
refresh()
writes.tryEmit(Unit)
requestSyncs(accounts, userInitiated = false)
}
}
@@ -147,7 +149,7 @@ class CalendarRepositoryImpl @Inject constructor(
private fun requestSyncs(accounts: Set<SyncAccount>, userInitiated: Boolean): Int {
if (accounts.isEmpty()) return 0
val addressable = try {
syncRequester.addressableAccounts()
syncRequester.addressableAccounts(accounts.mapTo(mutableSetOf()) { it.type })
} catch (e: Exception) {
Log.w(TAG, "Account visibility unreadable", e)
emptySet()
@@ -28,10 +28,10 @@ interface SyncRequester {
fun adapterAccountTypes(): Set<String>
/**
* The accounts a request may name, or null when any may. From Android 13 a
* request for an account the app can't see is dropped without a word.
* The accounts of [types] a request may name, or null when any may. From
* Android 13 a request for an account the app can't see is dropped without a word.
*/
fun addressableAccounts(): Set<SyncAccount>?
fun addressableAccounts(types: Set<String>): Set<SyncAccount>?
/**
* Schedule a sync of [account], or of every calendar account when null; may
@@ -51,12 +51,13 @@ class AndroidSyncRequester @Inject constructor(
.filter { it.authority == CalendarContract.AUTHORITY }
.mapTo(mutableSetOf()) { it.accountType }
override fun addressableAccounts(): Set<SyncAccount>? =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
null
} else {
AccountManager.get(context).accounts.mapTo(mutableSetOf()) { SyncAccount(it.name, it.type) }
override fun addressableAccounts(types: Set<String>): Set<SyncAccount>? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return null
val manager = AccountManager.get(context)
return types.flatMapTo(mutableSetOf()) { type ->
manager.getAccountsByType(type).map { SyncAccount(it.name, it.type) }
}
}
override fun requestSync(account: SyncAccount?, userInitiated: Boolean) {
val extras = Bundle().apply {
@@ -68,8 +68,17 @@ class RecordingSyncRequester(
override fun adapterAccountTypes(): Set<String> =
adapterTypes ?: setOf("bitfire.at.davdroid", "com.google", "LOCAL")
override fun addressableAccounts(): Set<SyncAccount>? =
if (hiddenTypes.isEmpty()) null else knownAccounts.filterTo(mutableSetOf()) { it.type !in hiddenTypes }
override fun addressableAccounts(types: Set<String>): Set<SyncAccount>? {
visibilityError?.let { throw it }
return if (hiddenTypes.isEmpty()) {
null
} else {
knownAccounts.filterTo(mutableSetOf()) { it.type in types && it.type !in hiddenTypes }
}
}
/** Thrown by [addressableAccounts] when set. */
var visibilityError: Exception? = null
/** The accounts on the "device", for [addressableAccounts]. */
var knownAccounts: Set<SyncAccount> = emptySet()
@@ -124,14 +124,14 @@ class SyncNudgeTest {
}
@Test
fun `a failed write asks for no sync`(@TempDir dir: Path) = runTest {
fun `a failed write still nudges, since part of it may have landed`(@TempDir dir: Path) = runTest {
val sync = RecordingSyncRequester()
val source = fake().apply { writeError = IllegalStateException("boom") }
val repo = testRepository(source, dir, Dispatchers.Unconfined, syncRequester = sync)
assertThrows<IllegalStateException> { repo.deleteEvent(10L) }
assertThat(sync.requests).isEmpty()
assertThat(sync.requests).containsExactly(auto(dav))
}
@Test
@@ -240,4 +240,24 @@ class SyncNudgeTest {
repo.createEvent(form(1L))
assertThat(sync.requests).containsExactly(auto(dav))
}
@Test
fun `unreadable account visibility falls back to the account-less request`(@TempDir dir: Path) = runTest {
val sync = RecordingSyncRequester(hiddenTypes = setOf("other"))
sync.visibilityError = SecurityException("no")
val repo = testRepository(fake(), dir, Dispatchers.Unconfined, syncRequester = sync)
repo.createEvent(form(1L))
assertThat(sync.requests).containsExactly(Request(null, false))
}
@Test
fun `a refused account-less request reports a failure`(@TempDir dir: Path) = runTest {
val sync = RecordingSyncRequester(accepts = false, hiddenTypes = setOf(dav.type))
sync.knownAccounts = setOf(dav, google)
val repo = testRepository(fake(), dir, Dispatchers.Unconfined, syncRequester = sync)
assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.Failed)
}
}
+2 -2
View File
@@ -11,8 +11,8 @@ the package list (recurring writes, save conflicts, reminder delivery).
no caching layer, no sync code. Reads query the provider; writes go
straight back to it. Sync is DAVx5's / Google's / the system's job.
The app only *asks* for one through `ContentResolver.requestSync`: an
expedited nudge for the account after each of its own successful writes to
a synced calendar (#303, once per write, so a multi-row save or an import asks once
expedited nudge for the account after each of its own writes to a synced
calendar (#303, once per write, so a multi-row save or an import asks once
per account, and never `MANUAL`, which would override master-sync-off), and
a manual request from the drawer's "Sync now" (#240), which like the
system's own "Sync now" also runs for accounts whose auto-sync is off. Both