diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt index 2a57b070..14e34855 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt @@ -88,10 +88,11 @@ class CalendarRepositoryImpl @Inject constructor( override val providerChanges: Flow 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 write( syncCalendars: suspend () -> Collection, @@ -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, 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() diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt index 736b9035..c57db9f7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt @@ -28,10 +28,10 @@ interface SyncRequester { fun adapterAccountTypes(): Set /** - * 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? + fun addressableAccounts(types: Set): Set? /** * 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? = - 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): Set? { + 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 { diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/TestRepositories.kt b/app/src/test/java/de/jeanlucmakiola/calendula/TestRepositories.kt index 0b0db553..c5a602a9 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/TestRepositories.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/TestRepositories.kt @@ -68,8 +68,17 @@ class RecordingSyncRequester( override fun adapterAccountTypes(): Set = adapterTypes ?: setOf("bitfire.at.davdroid", "com.google", "LOCAL") - override fun addressableAccounts(): Set? = - if (hiddenTypes.isEmpty()) null else knownAccounts.filterTo(mutableSetOf()) { it.type !in hiddenTypes } + override fun addressableAccounts(types: Set): Set? { + 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 = emptySet() diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt index 60fdc3af..0a5b7673 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt @@ -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 { 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) + } } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2fed75c5..ff3e213a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -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