Skip to content

Commit

Permalink
AutomaticSyncWorker: only provide updateAutomaticSync() as public method
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Dec 26, 2024
1 parent 226464f commit e114511
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,6 +33,7 @@ class AccountSettingsTest {
@Before
fun setUp() {
hiltRule.inject()
TestUtils.setUpWorkManager(context)
}


Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit e114511

Please sign in to comment.