diff --git a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt index 3db9b9d66..989a42ee0 100644 --- a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt +++ b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt @@ -10,7 +10,7 @@ import androidx.work.workDataOf import com.fsck.k9.Account import com.fsck.k9.Clock import com.fsck.k9.K9 -import java.time.Duration +import java.util.concurrent.TimeUnit import timber.log.Timber class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clock) { @@ -24,9 +24,9 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo fun scheduleMailSync(account: Account) { if (isNeverSyncInBackground()) return - getSyncIntervalIfEnabled(account)?.let { syncInterval -> + getSyncIntervalIfEnabled(account)?.let { syncIntervalMinutes -> Timber.v("Scheduling mail sync worker for %s", account.description) - Timber.v(" sync interval: %d minutes", syncInterval.toMinutes()) + Timber.v(" sync interval: %d minutes", syncIntervalMinutes) val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) @@ -36,14 +36,14 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo val lastSyncTime = account.lastSyncTime Timber.v(" last sync time: %tc", lastSyncTime) - val initialDelay = calculateInitialDelay(lastSyncTime, syncInterval) - Timber.v(" initial delay: %d minutes", initialDelay.toMinutes()) + val initialDelay = calculateInitialDelay(lastSyncTime, syncIntervalMinutes) + Timber.v(" initial delay: %d ms", initialDelay) val data = workDataOf(MailSyncWorker.EXTRA_ACCOUNT_UUID to account.uuid) - val mailSyncRequest = PeriodicWorkRequestBuilder(syncInterval) - .setInitialDelay(initialDelay) - .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, INITIAL_BACKOFF_DELAY) + val mailSyncRequest = PeriodicWorkRequestBuilder(syncIntervalMinutes, TimeUnit.MINUTES) + .setInitialDelay(initialDelay, TimeUnit.MILLISECONDS) + .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, INITIAL_BACKOFF_DELAY_MINUTES, TimeUnit.MINUTES) .setConstraints(constraints) .setInputData(data) .addTag(MAIL_SYNC_TAG) @@ -56,23 +56,23 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo private fun isNeverSyncInBackground() = K9.backgroundOps == K9.BACKGROUND_OPS.NEVER - private fun getSyncIntervalIfEnabled(account: Account): Duration? { + private fun getSyncIntervalIfEnabled(account: Account): Long? { val intervalMinutes = account.automaticCheckIntervalMinutes if (intervalMinutes <= Account.INTERVAL_MINUTES_NEVER) { return null } - return Duration.ofMinutes(intervalMinutes.toLong()) + return intervalMinutes.toLong() } - private fun calculateInitialDelay(lastSyncTime: Long, syncInterval: Duration): Duration { + private fun calculateInitialDelay(lastSyncTime: Long, syncIntervalMinutes: Long): Long { val now = clock.time - val nextSyncTime = lastSyncTime + syncInterval.toMillis() + val nextSyncTime = lastSyncTime + (syncIntervalMinutes * 60L * 1000L) return if (lastSyncTime > now || nextSyncTime <= now) { - Duration.ZERO + 0L } else { - Duration.ofMillis(nextSyncTime - now) + nextSyncTime - now } } @@ -82,6 +82,6 @@ class MailSyncWorkerManager(private val workManager: WorkManager, val clock: Clo companion object { const val MAIL_SYNC_TAG = "MailSync" - private val INITIAL_BACKOFF_DELAY = Duration.ofMinutes(5) + private const val INITIAL_BACKOFF_DELAY_MINUTES = 5L } }