Merge pull request #4739 from k9mail/fix_pre_api26_crash

Remove usage of java.time API
This commit is contained in:
cketti 2020-05-08 14:57:14 +02:00 committed by GitHub
commit 633fee44d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<MailSyncWorker>(syncInterval)
.setInitialDelay(initialDelay)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, INITIAL_BACKOFF_DELAY)
val mailSyncRequest = PeriodicWorkRequestBuilder<MailSyncWorker>(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
}
}