diff --git a/app/src/androidTest/kotlin/at/bitfire/davdroid/settings/AccountSettingsTest.kt b/app/src/androidTest/kotlin/at/bitfire/davdroid/settings/AccountSettingsTest.kt index 803f1ecc1..a9d98b686 100644 --- a/app/src/androidTest/kotlin/at/bitfire/davdroid/settings/AccountSettingsTest.kt +++ b/app/src/androidTest/kotlin/at/bitfire/davdroid/settings/AccountSettingsTest.kt @@ -6,6 +6,7 @@ package at.bitfire.davdroid.settings import android.accounts.AccountManager import android.content.Context +import at.bitfire.davdroid.TestUtils import at.bitfire.davdroid.sync.account.TestAccountAuthenticator import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.testing.HiltAndroidRule @@ -32,6 +33,7 @@ class AccountSettingsTest { @Before fun setUp() { hiltRule.inject() + TestUtils.setUpWorkManager(context) } @@ -50,7 +52,7 @@ class AccountSettingsTest { accountSettingsFactory.create(account, abortOnMissingMigration = true) val accountManager = AccountManager.get(context) - val version = accountManager.getUserData(account, AccountSettings.KEY_SETTINGS_VERSION).toIntOrNull() + val version = accountManager.getUserData(account, AccountSettings.KEY_SETTINGS_VERSION).toInt() assertEquals(AccountSettings.CURRENT_VERSION, version) } } diff --git a/app/src/main/kotlin/at/bitfire/davdroid/settings/AccountSettings.kt b/app/src/main/kotlin/at/bitfire/davdroid/settings/AccountSettings.kt index cf3942aa2..b60336526 100644 --- a/app/src/main/kotlin/at/bitfire/davdroid/settings/AccountSettings.kt +++ b/app/src/main/kotlin/at/bitfire/davdroid/settings/AccountSettings.kt @@ -155,9 +155,9 @@ class AccountSettings @AssistedInject constructor( * * @param dataType data type of the sync interval to set * @param seconds sync interval in seconds; _null_ for no periodic sync - * @param enableAutomaticSync whether to enable automatic sync using [AutomaticSyncManager.enableAutomaticSync] + * @param updateAutomaticSync whether to update automatic synchronization afterwards */ - fun setSyncInterval(dataType: SyncDataType, seconds: Long?, enableAutomaticSync: Boolean = true) { + fun setSyncInterval(dataType: SyncDataType, seconds: Long?, updateAutomaticSync: Boolean = true) { val key = when (dataType) { SyncDataType.CONTACTS -> KEY_SYNC_INTERVAL_ADDRESSBOOKS SyncDataType.EVENTS -> KEY_SYNC_INTERVAL_CALENDARS @@ -166,8 +166,8 @@ class AccountSettings @AssistedInject constructor( val newValue = if (seconds == null) SYNC_INTERVAL_MANUALLY else seconds accountManager.setAndVerifyUserData(account, key, newValue.toString()) - if (enableAutomaticSync) - automaticSyncManager.enableAutomaticSync(account, dataType, seconds, getSyncWifiOnly()) + if (updateAutomaticSync) + automaticSyncManager.updateAutomaticSync(account, dataType) } fun getSyncWifiOnly() = diff --git a/app/src/main/kotlin/at/bitfire/davdroid/sync/AutomaticSyncManager.kt b/app/src/main/kotlin/at/bitfire/davdroid/sync/AutomaticSyncManager.kt index d335b3791..a23be876d 100644 --- a/app/src/main/kotlin/at/bitfire/davdroid/sync/AutomaticSyncManager.kt +++ b/app/src/main/kotlin/at/bitfire/davdroid/sync/AutomaticSyncManager.kt @@ -39,7 +39,7 @@ class AutomaticSyncManager @Inject constructor( /** * Disable automatic synchronization for the given account and data type. */ - fun disableAutomaticSync(account: Account, dataType: SyncDataType) { + private fun disableAutomaticSync(account: Account, dataType: SyncDataType) { workerManager.disablePeriodic(account, dataType) for (authority in dataType.possibleAuthorities(context)) @@ -54,19 +54,18 @@ class AutomaticSyncManager @Inject constructor( * * @param account the account to synchronize * @param dataType the data type to synchronize - * @param seconds interval in seconds, or `null` to disable periodic sync (only sync on local data changes) - * @param wifiOnly whether to synchronize only on Wi-Fi (default value takes the account setting) */ - fun enableAutomaticSync( + private fun enableAutomaticSync( account: Account, - dataType: SyncDataType, - seconds: Long?, - wifiOnly: Boolean = accountSettingsFactory.create(account).getSyncWifiOnly() + dataType: SyncDataType ) { - if (seconds != null) + val accountSettings = accountSettingsFactory.create(account) + val syncInterval = accountSettings.getSyncInterval(dataType) + if (syncInterval != null) { // update sync workers (needs already updated sync interval in AccountSettings) - workerManager.enablePeriodic(account, dataType, seconds, wifiOnly) - else + val wifiOnly = accountSettings.getSyncWifiOnly() + workerManager.enablePeriodic(account, dataType, syncInterval, wifiOnly) + } else workerManager.disablePeriodic(account, dataType) // also enable/disable content-triggered syncs @@ -76,7 +75,7 @@ class AutomaticSyncManager @Inject constructor( SyncDataType.EVENTS -> CalendarContract.AUTHORITY SyncDataType.TASKS -> tasksAppManager.get().currentProvider()?.authority } - if (authority != null && seconds != null) { + if (authority != null && syncInterval != null) { // enable authority, but completely disable all other possible authorities (for instance, tasks apps which are not the current task app) syncFramework.enableSyncOnContentChange(account, authority) for (disableAuthority in possibleAuthorities - authority) @@ -113,11 +112,9 @@ class AutomaticSyncManager @Inject constructor( } val hasService = serviceRepository.getByAccountAndType(account.name, serviceType) != null - if (hasService) { - val accountSettings = accountSettingsFactory.create(account) - val syncInterval = accountSettings.getSyncInterval(dataType) - enableAutomaticSync(account, dataType, syncInterval) - } else + if (hasService) + enableAutomaticSync(account, dataType) + else disableAutomaticSync(account, dataType) }