diff --git a/app/src/main/java/org/tasks/data/CaldavDao.kt b/app/src/main/java/org/tasks/data/CaldavDao.kt index d83f44a00..5d417a737 100644 --- a/app/src/main/java/org/tasks/data/CaldavDao.kt +++ b/app/src/main/java/org/tasks/data/CaldavDao.kt @@ -30,9 +30,6 @@ abstract class CaldavDao { @Query("SELECT * FROM caldav_accounts WHERE cda_uuid = :uuid LIMIT 1") abstract suspend fun getAccountByUuid(uuid: String): CaldavAccount? - @Query("SELECT COUNT(*) FROM caldav_accounts WHERE cda_account_type != 2") - abstract suspend fun accountCount(): Int - @Query("SELECT * FROM caldav_accounts WHERE cda_account_type = :type") abstract suspend fun getAccounts(type: Int): List diff --git a/app/src/main/java/org/tasks/jobs/SyncWork.kt b/app/src/main/java/org/tasks/jobs/SyncWork.kt index c4c5af5e7..dcbcc89d9 100644 --- a/app/src/main/java/org/tasks/jobs/SyncWork.kt +++ b/app/src/main/java/org/tasks/jobs/SyncWork.kt @@ -12,8 +12,9 @@ abstract class SyncWork constructor( workerParams: WorkerParameters, firebase: Firebase, private val localBroadcastManager: LocalBroadcastManager, - private val preferences: Preferences) : BaseWorker(context, workerParams, firebase) { - + private val preferences: Preferences +) : BaseWorker(context, workerParams, firebase) { + final override suspend fun run(): Result { if (!enabled()) { return Result.failure() diff --git a/app/src/main/java/org/tasks/jobs/WorkManager.kt b/app/src/main/java/org/tasks/jobs/WorkManager.kt index a61847a6f..2fbc988cc 100644 --- a/app/src/main/java/org/tasks/jobs/WorkManager.kt +++ b/app/src/main/java/org/tasks/jobs/WorkManager.kt @@ -22,9 +22,7 @@ interface WorkManager { suspend fun updateBackgroundSync() suspend fun updateBackgroundSync( - forceAccountPresent: Boolean?, - forceBackgroundEnabled: Boolean?, - forceOnlyOnUnmetered: Boolean?) + forceBackgroundEnabled: Boolean?, forceOnlyOnUnmetered: Boolean?) suspend fun scheduleRefresh(time: Long) diff --git a/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt b/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt index fc0da1332..b948adb1b 100644 --- a/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt +++ b/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt @@ -12,6 +12,8 @@ import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.data.Task import org.tasks.BuildConfig import org.tasks.R +import org.tasks.data.CaldavAccount.Companion.TYPE_CALDAV +import org.tasks.data.CaldavAccount.Companion.TYPE_ETESYNC import org.tasks.data.CaldavDao import org.tasks.data.GoogleTaskListDao import org.tasks.data.Place @@ -119,40 +121,43 @@ class WorkManagerImpl constructor( } override suspend fun updateBackgroundSync() { - updateBackgroundSync(null, null, null) + updateBackgroundSync(null, null) } @SuppressLint("CheckResult") override suspend fun updateBackgroundSync( - forceAccountPresent: Boolean?, - forceBackgroundEnabled: Boolean?, - forceOnlyOnUnmetered: Boolean?) { - val backgroundEnabled = forceBackgroundEnabled + forceBackgroundEnabled: Boolean?, forceOnlyOnUnmetered: Boolean?) { + val enabled = forceBackgroundEnabled ?: preferences.getBoolean(R.string.p_background_sync, true) - val onlyOnWifi = forceOnlyOnUnmetered + val unmetered = forceOnlyOnUnmetered ?: preferences.getBoolean(R.string.p_background_sync_unmetered_only, false) - val accountsPresent = forceAccountPresent == true - || googleTaskListDao.accountCount() > 0 - || caldavDao.accountCount() > 0 - scheduleBackgroundSync(backgroundEnabled && accountsPresent, onlyOnWifi) - } - private suspend fun scheduleBackgroundSync(enabled: Boolean, onlyOnUnmetered: Boolean) { - Timber.d("background sync enabled: %s, onlyOnUnmetered: %s", enabled, onlyOnUnmetered) - scheduleBackgroundSync(enabled, onlyOnUnmetered, TAG_BACKGROUND_SYNC_GOOGLE_TASKS, SyncGoogleTasksWork::class.java) - scheduleBackgroundSync(enabled, onlyOnUnmetered, TAG_BACKGROUND_SYNC_CALDAV, SyncCaldavWork::class.java) - scheduleBackgroundSync(enabled, onlyOnUnmetered, TAG_BACKGROUND_SYNC_ETESYNC, SyncEteSyncWork::class.java) + scheduleBackgroundSync( + TAG_BACKGROUND_SYNC_GOOGLE_TASKS, + SyncGoogleTasksWork::class.java, + enabled && googleTaskListDao.accountCount() > 0, + unmetered) + scheduleBackgroundSync( + TAG_BACKGROUND_SYNC_CALDAV, + SyncCaldavWork::class.java, + enabled && caldavDao.getAccounts(TYPE_CALDAV).isNotEmpty(), + unmetered) + scheduleBackgroundSync( + TAG_BACKGROUND_SYNC_ETESYNC, + SyncEteSyncWork::class.java, + enabled && caldavDao.getAccounts(TYPE_ETESYNC).isNotEmpty(), + unmetered) } - private suspend fun scheduleBackgroundSync(enabled: Boolean, onlyOnUnmetered: Boolean, tag: String, c: Class) { + private suspend fun scheduleBackgroundSync( + tag: String, c: Class, enabled: Boolean, unmetered: Boolean? = null) { + Timber.d("scheduleBackgroundSync($tag, $c, enabled = $enabled, unmetered = $unmetered)") throttle.run { if (enabled) { + val builder = PeriodicWorkRequest.Builder(c, 1, TimeUnit.HOURS) + unmetered?.let { builder.setConstraints(getNetworkConstraints(it)) } workManager.enqueueUniquePeriodicWork( - tag, - ExistingPeriodicWorkPolicy.KEEP, - PeriodicWorkRequest.Builder(c, 1, TimeUnit.HOURS) - .setConstraints(getNetworkConstraints(onlyOnUnmetered)) - .build()) + tag, ExistingPeriodicWorkPolicy.KEEP, builder.build()) } else { workManager.cancelUniqueWork(tag) } diff --git a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt index 00e5fc69d..92193ebd6 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Synchronization.kt @@ -52,14 +52,14 @@ class Synchronization : InjectingPreferenceFragment() { findPreference(R.string.p_background_sync_unmetered_only) .setOnPreferenceChangeListener { _: Preference?, o: Any? -> lifecycleScope.launch { - workManager.updateBackgroundSync(null, null, o as Boolean?) + workManager.updateBackgroundSync(null, o as Boolean?) } true } findPreference(R.string.p_background_sync) .setOnPreferenceChangeListener { _: Preference?, o: Any? -> lifecycleScope.launch { - workManager.updateBackgroundSync(null, o as Boolean?, null) + workManager.updateBackgroundSync(o as Boolean?, null) } true }