Fix backup on boot logic and improve readability

This commit is contained in:
Naveen 2023-03-25 12:31:21 +05:30
parent eb1c5157af
commit 0481b20fab
2 changed files with 26 additions and 6 deletions

View File

@ -192,7 +192,7 @@ fun Context.getAutomaticBackupIntent(): PendingIntent {
fun Context.scheduleNextAutomaticBackup() {
if (config.autoBackup) {
val backupAtMillis = DateTime.now().withHourOfDay(6).plusDays(AUTO_BACKUP_INTERVAL_IN_DAYS).millis
val backupAtMillis = getNextAutoBackupTime().millis
val pendingIntent = getAutomaticBackupIntent()
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
try {
@ -209,11 +209,14 @@ fun Context.cancelScheduledAutomaticBackup() {
}
fun Context.checkAndBackupEventsOnBoot() {
val now = getNowSeconds()
val intervalInSeconds = AUTO_BACKUP_INTERVAL_IN_DAYS * DAY
if (config.autoBackup && config.lastAutoBackupTime !in (now - intervalInSeconds)..now) {
// device was probably off at the scheduled time so backup now
backupEventsAndTasks()
if (config.autoBackup) {
val previousRealBackupTime = config.lastAutoBackupTime
val previousScheduledBackupTime = getPreviousAutoBackupTime().seconds()
val missedPreviousBackup = previousRealBackupTime < previousScheduledBackupTime
if (missedPreviousBackup) {
// device was probably off at the scheduled time so backup now
backupEventsAndTasks()
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.pro.helpers
import com.simplemobiletools.calendar.pro.activities.EventActivity
import com.simplemobiletools.calendar.pro.activities.TaskActivity
import com.simplemobiletools.commons.helpers.MONTH_SECONDS
import org.joda.time.DateTime
import java.util.*
const val STORED_LOCALLY_ONLY = 0
@ -275,3 +276,19 @@ fun getActivityToOpen(isTask: Boolean) = if (isTask) {
fun generateImportId(): String {
return UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis().toString()
}
// 6 am is the hardcoded automatic backup time, intervals shorter than 1 day are not yet supported.
fun getNextAutoBackupTime(): DateTime {
val now = DateTime.now()
val sixHour = now.withHourOfDay(6)
return if (now.millis < sixHour.millis) {
sixHour
} else {
sixHour.plusDays(AUTO_BACKUP_INTERVAL_IN_DAYS)
}
}
fun getPreviousAutoBackupTime(): DateTime {
val nextBackupTime = getNextAutoBackupTime()
return nextBackupTime.minusDays(AUTO_BACKUP_INTERVAL_IN_DAYS)
}