Only enable background sync for active services

pull/1061/head
Alex Baker 4 years ago
parent ddd66c0ec8
commit 3fbbd95395

@ -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<CaldavAccount>

@ -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()

@ -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)

@ -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<out SyncWork>) {
private suspend fun scheduleBackgroundSync(
tag: String, c: Class<out SyncWork>, 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)
}

@ -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
}

Loading…
Cancel
Save