diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index ba46a26e..86fe2f31 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -301,6 +301,12 @@
+
+
+
+
+
+
+
diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/CalendulaApp.kt b/app/src/main/java/de/jeanlucmakiola/calendula/CalendulaApp.kt
index 2a5a2b36..0661c415 100644
--- a/app/src/main/java/de/jeanlucmakiola/calendula/CalendulaApp.kt
+++ b/app/src/main/java/de/jeanlucmakiola/calendula/CalendulaApp.kt
@@ -1,8 +1,10 @@
package de.jeanlucmakiola.calendula
import android.app.Application
+import androidx.work.Configuration
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.android.HiltAndroidApp
+import de.jeanlucmakiola.calendula.data.backup.BackupChangeJob
import de.jeanlucmakiola.calendula.data.backup.BackupScheduler
import de.jeanlucmakiola.calendula.data.calendar.CalendarVisibilityReconciler
import de.jeanlucmakiola.calendula.data.backup.BackupWorker
@@ -27,7 +29,11 @@ import kotlinx.coroutines.launch
* in AndroidManifest.xml. Hilt initializes its component graph here.
*/
@HiltAndroidApp
-class CalendulaApp : Application() {
+class CalendulaApp : Application(), Configuration.Provider {
+
+ /** Keeps WorkManager's JobScheduler ids clear of [BackupChangeJob]'s. */
+ override val workManagerConfiguration: Configuration =
+ Configuration.Builder().setJobSchedulerJobIdRange(0, BackupChangeJob.JOB_ID - 1).build()
override fun onCreate() {
super.onCreate()
@@ -110,12 +116,7 @@ class CalendulaApp : Application() {
val deps = EntryPointAccessors.fromApplication(this, BackupWorker.Deps::class.java)
CoroutineScope(SupervisorJob() + Dispatchers.Default).launch {
val prefs = deps.settingsPrefs()
- BackupScheduler.apply(
- context = this@CalendulaApp,
- enabled = prefs.autoBackupEnabled.first(),
- intervalMinutes = prefs.autoBackupIntervalMinutes.first(),
- hasFolder = prefs.autoBackupFolderUri.first() != null,
- )
+ BackupScheduler.reconcile(this@CalendulaApp, prefs)
}
}
diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt
index fcb2ce0a..51875c6f 100644
--- a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt
+++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt
@@ -11,13 +11,17 @@ import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.net.toUri
+import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
+import androidx.work.WorkInfo
import androidx.work.WorkManager
import androidx.work.WorkerParameters
+import androidx.work.await
+import androidx.work.workDataOf
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
@@ -26,14 +30,22 @@ import de.jeanlucmakiola.calendula.R
import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository
import de.jeanlucmakiola.calendula.data.ics.IcsExporter
import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
+import de.jeanlucmakiola.calendula.domain.backup.BackupFilePlan
+import de.jeanlucmakiola.calendula.domain.backup.BackupTarget
+import de.jeanlucmakiola.calendula.domain.backup.backupFingerprint
import de.jeanlucmakiola.calendula.domain.backup.backupTargets
-import de.jeanlucmakiola.calendula.domain.backup.planBackupFiles
+import de.jeanlucmakiola.calendula.domain.backup.inBackupOrder
import de.jeanlucmakiola.calendula.domain.backup.latestHeldElsewhere
+import de.jeanlucmakiola.calendula.domain.backup.latestFileName
import de.jeanlucmakiola.calendula.domain.backup.latestLedgerKey
+import de.jeanlucmakiola.calendula.domain.backup.needsRefresh
+import de.jeanlucmakiola.calendula.domain.backup.planBackupFiles
import de.jeanlucmakiola.calendula.domain.backup.replacedLatestCopies
+import de.jeanlucmakiola.calendula.domain.backup.withFingerprint
import de.jeanlucmakiola.calendula.domain.backup.withLedgerEntry
import de.jeanlucmakiola.calendula.domain.ics.IcsWriter
import de.jeanlucmakiola.floret.android.hasPermission
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.sync.Mutex
@@ -54,26 +66,51 @@ object BackupScheduler {
private const val WORK_NAME = "auto-backup"
private const val WORK_NAME_NOW = "auto-backup-now"
+ private const val WORK_NAME_CHANGE = "auto-backup-change"
+ internal const val KEY_CHANGE_RUN = "change_run"
+
+ private val reconcileLock = Mutex()
+
+ /** A watch that fired while the option was off: still pending until it finishes, but not re-armed. */
+ private var watchDropped = false
/**
- * Reconcile the scheduled work with the current settings: enqueue a unique
+ * Reconcile the scheduled work with the saved settings: enqueue a unique
* periodic request when backup is on and a folder is set, otherwise cancel
* it. WorkManager persists the request across reboots on its own.
*
* The first run is delayed by one interval so it doesn't follow an immediate
- * [runNow] with a near-identical copy.
+ * [runNow] with a near-identical copy. With backing up after changes on
+ * (#516), the change watch ([BackupChangeJob]) is armed unless it already
+ * waits; it doesn't survive a reboot, and this runs on every process start.
+ * Serialised and not cancellable, so quick toggles settle on the last one.
*/
- fun apply(context: Context, enabled: Boolean, intervalMinutes: Long, hasFolder: Boolean) {
+ suspend fun reconcile(context: Context, prefs: SettingsPrefs) = withContext(NonCancellable + Dispatchers.IO) {
+ reconcileLock.withLock {
+ try {
+ apply(context, prefs)
+ } catch (e: Exception) {
+ Log.w(TAG, "Scheduling the automatic backup failed", e)
+ }
+ }
+ }
+
+ private suspend fun apply(context: Context, prefs: SettingsPrefs) {
val workManager = WorkManager.getInstance(context)
- if (!enabled || !hasFolder) {
+ fun stopWatching() {
+ BackupChangeJob.cancel(context)
+ workManager.cancelUniqueWork(WORK_NAME_CHANGE)
+ }
+ if (!prefs.autoBackupEnabled.first() || prefs.autoBackupFolderUri.first() == null) {
workManager.cancelUniqueWork(WORK_NAME)
// Also cancel any pending immediate run — otherwise a failing run-now
// keeps retrying after the user has turned backup off.
workManager.cancelUniqueWork(WORK_NAME_NOW)
+ stopWatching()
return
}
// WorkManager's own floor is 15 min; the UI floors the user choice at 30.
- val interval = intervalMinutes.coerceAtLeast(15L)
+ val interval = prefs.autoBackupIntervalMinutes.first().coerceAtLeast(15L)
val request = PeriodicWorkRequestBuilder(interval, TimeUnit.MINUTES)
.setInitialDelay(interval, TimeUnit.MINUTES)
.build()
@@ -82,6 +119,44 @@ object BackupScheduler {
ExistingPeriodicWorkPolicy.UPDATE,
request,
)
+ if (!prefs.autoBackupOnChange.first()) {
+ stopWatching()
+ } else if (watchDropped || !BackupChangeJob.isPending(context)) {
+ // Replacing a waiting watch would drop the changes it has seen.
+ BackupChangeJob.schedule(context)
+ watchDropped = false
+ }
+ }
+
+ /**
+ * The watch fired: queue a change run and arm the next watch, unless the
+ * option went off meanwhile. Under [reconcileLock], so it can't re-arm
+ * after a switch-off.
+ */
+ internal suspend fun changed(context: Context, prefs: SettingsPrefs) = withContext(NonCancellable) {
+ reconcileLock.withLock {
+ // Re-armed even when queueing the run fails: a trigger fires only once.
+ var wanted = true
+ try {
+ wanted = prefs.autoBackupEnabled.first() && prefs.autoBackupFolderUri.first() != null &&
+ prefs.autoBackupOnChange.first()
+ if (wanted) queueChangeRun(context)
+ } finally {
+ if (wanted) BackupChangeJob.schedule(context)
+ watchDropped = !wanted
+ }
+ }
+ }
+
+ private suspend fun queueChangeRun(context: Context) {
+ val workManager = WorkManager.getInstance(context)
+ val states = workManager.getWorkInfosForUniqueWorkFlow(WORK_NAME_CHANGE).first().map { it.state }
+ val policy = changeRunPolicy(states) ?: return
+ val run = OneTimeWorkRequestBuilder()
+ .setConstraints(Constraints.Builder().setRequiresBatteryNotLow(true).build())
+ .setInputData(workDataOf(KEY_CHANGE_RUN to true))
+ .build()
+ workManager.enqueueUniqueWork(WORK_NAME_CHANGE, policy, run).await()
}
/**
@@ -96,6 +171,19 @@ object BackupScheduler {
OneTimeWorkRequestBuilder().build(),
)
}
+
+ private const val TAG = "BackupScheduler"
+}
+
+/**
+ * How a fired change watch queues its run (#516), from the change runs' work
+ * [states]: a run that hasn't started yet will see the change anyway (null);
+ * one already going may have read too early, so the new one queues behind it.
+ */
+internal fun changeRunPolicy(states: List): ExistingWorkPolicy? = when {
+ states.any { it == WorkInfo.State.ENQUEUED || it == WorkInfo.State.BLOCKED } -> null
+ WorkInfo.State.RUNNING in states -> ExistingWorkPolicy.APPEND_OR_REPLACE
+ else -> ExistingWorkPolicy.REPLACE
}
/**
@@ -121,51 +209,108 @@ class BackupWorker(
override suspend fun doWork(): Result {
val deps = EntryPointAccessors.fromApplication(applicationContext, Deps::class.java)
- val prefs = deps.settingsPrefs()
- val now = System.currentTimeMillis()
- // The periodic and the immediate run are separate unique works; one at
- // a time, or each would overwrite the other's ledger.
+ val changeRun = inputData.getBoolean(BackupScheduler.KEY_CHANGE_RUN, false)
+ if (!changeRun) return backUp(deps, deps.settingsPrefs(), changeRun = false)
+ // A failed change run would fail the one queued behind it too.
+ try {
+ backUp(deps, deps.settingsPrefs(), changeRun = true)
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ Log.w(TAG, "Backup after a change failed", e)
+ }
+ return Result.success()
+ }
+
+ /**
+ * One run under the shared lock, recorded for the status line. A change
+ * run (#516) refreshes only the stable copies and never retries. The
+ * failure notification comes once per failure streak, when it reaches
+ * [FAILURE_NOTIFY_THRESHOLD], and goes with the next success of any run.
+ */
+ private suspend fun backUp(deps: Deps, prefs: SettingsPrefs, changeRun: Boolean): Result {
+ // One run at a time, or each would overwrite the other's ledger.
val outcome = runLock.withLock {
- // Respect the toggle even for already-queued work, so a lingering
+ val now = System.currentTimeMillis()
+ // Respect the toggles even for already-queued work, so a lingering
// run can't revive itself.
if (!prefs.autoBackupEnabled.first()) return Result.success()
+ if (changeRun && !prefs.autoBackupOnChange.first()) return Result.success()
val folder = prefs.autoBackupFolderUri.first() ?: return Result.failure()
- try {
- runBackup(deps, prefs, folder.toUri())
+ val outcome = try {
+ runBackup(deps, prefs, folder.toUri(), latestOnly = changeRun)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.w(TAG, "Automatic backup failed", e)
RunOutcome.Failed
}
- }
- if (outcome == RunOutcome.NothingSelected) return Result.success()
- val success = outcome == RunOutcome.Written
- prefs.recordAutoBackupRun(success = success, atMillis = now)
- if (!success && prefs.autoBackupStatus.first().consecutiveFailures >= FAILURE_NOTIFY_THRESHOLD) {
- notifyFailure(applicationContext)
+ if (outcome != RunOutcome.NothingToDo) {
+ withContext(NonCancellable) {
+ prefs.recordAutoBackupRun(success = outcome == RunOutcome.Written, atMillis = now)
+ when {
+ outcome == RunOutcome.Written -> clearFailureNotification(applicationContext)
+ prefs.autoBackupStatus.first().consecutiveFailures == FAILURE_NOTIFY_THRESHOLD ->
+ notifyFailure(applicationContext)
+ }
+ }
+ }
+ outcome
}
// Only a run that wrote nothing retries: a retry rewrites every series.
- return if (outcome == RunOutcome.Failed) Result.retry() else Result.success()
+ return if (outcome == RunOutcome.Failed && !changeRun) Result.retry() else Result.success()
}
- private enum class RunOutcome { Written, PartlyFailed, Failed, NothingSelected }
+ private enum class RunOutcome { Written, PartlyFailed, Failed, NothingToDo }
- /** Writes each series and prunes it only once its new file has landed. */
- private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri): RunOutcome {
+ /** How a stable copy's write went: under its own name, under one the provider picked, or not at all. */
+ private enum class LatestWrite { Landed, Renamed, Failed }
+
+ /**
+ * Writes each series and prunes it only once its new file has landed. A
+ * [latestOnly] run (#516) leaves the dated files and their retention to the
+ * schedule and rewrites only the stable copies whose calendars changed.
+ */
+ private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri, latestOnly: Boolean): RunOutcome {
val repository = deps.repository()
val exporter = deps.exporter()
if (!applicationContext.hasPermission(Manifest.permission.READ_CALENDAR)) return RunOutcome.Failed
val all = repository.calendars().first()
val selection = prefs.autoBackupSelection.first()
val combined = prefs.autoBackupCombined.first()
- if (all.none(selection::selects)) return RunOutcome.NothingSelected
+ if (all.none(selection::selects)) return RunOutcome.NothingToDo
prefs.adoptLegacyBackupFile()
val listing = exporter.listFolder(folder)
val ledger = prefs.autoBackupLedger.first()
// A listing still loading can't tell a gone file from a late one.
val existing = if (listing.complete) listing.files.keys else listing.files.keys + ledger.values.flatten()
val targets = backupTargets(all.filter(selection::selects), combined, ledger, existing)
+ if (targets.isEmpty()) return RunOutcome.NothingToDo
+ val stamp = Clock.System.now()
+ val fingerprints = prefs.autoBackupFingerprints.first()
+ suspend fun export(target: BackupTarget): String? = try {
+ val events = repository.exportEvents(target.calendarIds).inBackupOrder()
+ IcsWriter().writeCalendar(events, stamp, target.calendarName)
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ Log.w(TAG, "Reading one series failed", e)
+ null
+ }
+ fun fingerprintOf(target: BackupTarget, text: String) =
+ backupFingerprint(text, latestFileName(target.baseName))
+ // Hashes only serve change runs; without them a stale one could pass later.
+ val trackHashes = prefs.autoBackupOnChange.first()
+ if (!trackHashes && fingerprints.isNotEmpty()) prefs.updateAutoBackupFingerprints(folder.toString()) { emptyMap() }
+ // A change run hashes first (null: unreadable) and leaves the folder alone when nothing moved.
+ val preHashes: Map = if (latestOnly) {
+ targets.associate { target -> target.key to export(target)?.let { fingerprintOf(target, it) } }
+ } else {
+ emptyMap()
+ }
+ if (latestOnly && targets.all { preHashes[it.key].let { hash -> hash != null && hash == fingerprints[it.key] } }) {
+ return RunOutcome.NothingToDo
+ }
val plans = planBackupFiles(
targets = targets,
ledger = ledger,
@@ -173,59 +318,100 @@ class BackupWorker(
at = LocalDateTime.now(),
keep = prefs.autoBackupKeep.first(),
)
- val stamp = Clock.System.now()
val liveKeys = all.map { it.id.toString() }.toSet()
suspend fun record(key: String, files: List) =
prefs.updateAutoBackupLedger(folder.toString()) { withLedgerEntry(it, key, files, liveKeys) }
- var written = 0
- val landedLatest = mutableSetOf()
- for (plan in plans) {
+
+ suspend fun writeDated(plan: BackupFilePlan, bytes: ByteArray): Boolean = try {
+ val name = exporter.writeToFolder(folder, listing, plan.fileName, bytes)
+ record(plan.target.key, plan.prune + plan.keep + name)
+ val undeleted = exporter.deleteFromFolder(listing, plan.prune)
+ record(plan.target.key, plan.prune.filter { it in undeleted } + plan.keep + name)
+ true
+ } catch (e: Exception) {
+ Log.w(TAG, "Backup of one series failed", e)
+ false
+ }
+
+ // An old copy goes only once the new one has landed.
+ suspend fun writeLatest(plan: BackupFilePlan, bytes: ByteArray, fingerprint: String): LatestWrite {
val target = plan.target
- val bytes = try {
- IcsWriter()
- .writeCalendar(repository.exportEvents(target.calendarIds), stamp, target.calendarName)
- .toByteArray(Charsets.UTF_8)
- } catch (e: CancellationException) {
- throw e
- } catch (e: Exception) {
- Log.w(TAG, "Reading one series failed", e)
- continue
- }
- // Not cancellable once a file exists: it must reach the ledger, or
- // nothing would ever prune it.
- withContext(NonCancellable) {
- try {
- val name = exporter.writeToFolder(folder, listing, plan.fileName, bytes)
- record(target.key, plan.prune + plan.keep + name)
- val undeleted = exporter.deleteFromFolder(listing, plan.prune)
- record(target.key, plan.prune.filter { it in undeleted } + plan.keep + name)
- written++
- } catch (e: Exception) {
- Log.w(TAG, "Backup of one series failed", e)
+ val latest = checkNotNull(plan.latestName)
+ var landed: String? = null
+ try {
+ val latestKey = latestLedgerKey(target.key)
+ if (trackHashes && target.key in fingerprints) {
+ prefs.updateAutoBackupFingerprints(folder.toString()) { it - target.key }
}
- // Best-effort: the stable copy never holds up the dated backup,
- // its pruning or the run's status. An old copy goes only once the
- // new one has landed, so a series is never left without one.
- try {
- val latest = plan.latestName ?: return@withContext
- val latestKey = latestLedgerKey(target.key)
- val landed = exporter.writeToFolder(folder, listing, latest, bytes)
- // A provider that renamed it leaves a copy to clean up next
- // run; the intended name stays ours only if it already was.
- val ownsLatest = landed == latest || latest in ledger[latestKey].orEmpty()
- val current = listOfNotNull(landed, latest.takeIf { ownsLatest })
- // A hand-off stays on record only until its new owner claimed it.
- suspend fun recordLatest(stale: List) = prefs.updateAutoBackupLedger(folder.toString()) {
+ landed = exporter.writeToFolder(folder, listing, latest, bytes)
+ // A provider that renamed it leaves a copy to clean up next
+ // run; the intended name stays ours only if it already was.
+ val ownsLatest = landed == latest || latest in ledger[latestKey].orEmpty()
+ val current = listOfNotNull(landed, latest.takeIf { ownsLatest })
+ // A hand-off stays on record only until its new owner claimed it.
+ suspend fun recordLatest(stale: List, landedFingerprint: String? = null) =
+ prefs.updateAutoBackupLedger(
+ folder.toString(),
+ fingerprints = landedFingerprint?.let { hash ->
+ { old -> withFingerprint(old, target.key, hash, liveKeys) }
+ },
+ ) {
val pending = plan.handOff - it.latestHeldElsewhere(target.key)
withLedgerEntry(it, latestKey, stale + pending + current, liveKeys)
}
- recordLatest(plan.staleLatest)
- if (landed == latest) landedLatest += target.key
- val staleLeft = exporter.deleteFromFolder(listing, plan.staleLatest)
- recordLatest(plan.staleLatest.filter { it in staleLeft })
- } catch (e: Exception) {
- Log.w(TAG, "Stable copy of one series failed", e)
+ recordLatest(plan.staleLatest, fingerprint.takeIf { trackHashes })
+ val staleLeft = exporter.deleteFromFolder(listing, plan.staleLatest)
+ recordLatest(plan.staleLatest.filter { it in staleLeft })
+ } catch (e: Exception) {
+ Log.w(TAG, "Stable copy of one series failed", e)
+ }
+ return when (landed) {
+ null -> LatestWrite.Failed
+ latest -> LatestWrite.Landed
+ else -> LatestWrite.Renamed
+ }
+ }
+
+ // A copy blocked by a file of the user's keeps its hash, so a later change run can stop early.
+ suspend fun rememberBlocked(plan: BackupFilePlan, hash: String) {
+ val key = plan.target.key
+ if (plan.latestName == null && trackHashes && fingerprints[key] != hash) {
+ prefs.updateAutoBackupFingerprints(folder.toString()) { withFingerprint(it, key, hash, liveKeys) }
+ }
+ }
+
+ var written = 0
+ var failed = 0
+ val landedLatest = mutableSetOf()
+ for (plan in plans) {
+ val target = plan.target
+ if (latestOnly) {
+ val hash = preHashes[target.key] ?: run {
+ failed++
+ continue
}
+ rememberBlocked(plan, hash)
+ if (!plan.needsRefresh(hash, fingerprints, listing.files.keys, listing.complete)) {
+ if (plan.latestName in listing.files) landedLatest += target.key
+ continue
+ }
+ }
+ // Read again rather than held since the pre-pass: one series' text in memory at a time.
+ val text = export(target) ?: run {
+ failed++
+ continue
+ }
+ val fingerprint = fingerprintOf(target, text)
+ if (!latestOnly) rememberBlocked(plan, fingerprint)
+ val bytes = text.toByteArray(Charsets.UTF_8)
+ // Not cancellable once a file exists: it must reach the ledger.
+ withContext(NonCancellable) {
+ val dated = if (latestOnly) null else writeDated(plan, bytes)
+ val latest = plan.latestName?.let { writeLatest(plan, bytes, fingerprint) }
+ if (latest == LatestWrite.Landed) landedLatest += target.key
+ // A full run's status follows its dated file; the stable copy is best-effort there.
+ val ok = dated ?: (latest != LatestWrite.Failed)
+ if (ok) written++ else failed++
}
}
// A layout switch retires the other layout's copies once every new one is there.
@@ -234,16 +420,23 @@ class BackupWorker(
withContext(NonCancellable) {
val left = exporter.deleteFromFolder(listing, names)
record(latestLedgerKey(key), names.filter { it in left })
+ // Its copy is gone, so its hash mustn't let a change run stop early.
+ prefs.updateAutoBackupFingerprints(folder.toString()) { it - key }
}
}
}
- return when (written) {
- plans.size -> RunOutcome.Written
- 0 -> RunOutcome.Failed
+ return when {
+ failed == 0 && written == 0 -> RunOutcome.NothingToDo
+ failed == 0 -> RunOutcome.Written
+ written == 0 -> RunOutcome.Failed
else -> RunOutcome.PartlyFailed
}
}
+ private fun clearFailureNotification(context: Context) {
+ NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
+ }
+
private fun notifyFailure(context: Context) {
val canPost = Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ||
context.hasPermission(Manifest.permission.POST_NOTIFICATIONS)
diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/BackupChangeJob.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/BackupChangeJob.kt
new file mode 100644
index 00000000..3b56b30d
--- /dev/null
+++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/BackupChangeJob.kt
@@ -0,0 +1,87 @@
+package de.jeanlucmakiola.calendula.data.backup
+
+import android.app.job.JobInfo
+import android.app.job.JobParameters
+import android.app.job.JobScheduler
+import android.app.job.JobService
+import android.content.ComponentName
+import android.content.Context
+import android.provider.CalendarContract
+import android.util.Log
+import dagger.hilt.android.EntryPointAccessors
+import kotlinx.coroutines.CancellationException
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.launch
+import java.util.concurrent.TimeUnit
+
+/**
+ * The watch behind "Also back up after changes" (#516): a JobScheduler job
+ * with a content trigger on the provider's root URI, where it reports every
+ * change, whoever made it (this app, a sync adapter or another app). It waits
+ * for [SETTLE_MINUTES] of quiet, at most [MAX_WAIT_MINUTES], so a burst makes
+ * one run. A trigger fires once; each firing queues a change run and schedules
+ * the next watch under the same id before finishing, which hands over the
+ * changes seen meanwhile, so exactly one watch exists and none are lost.
+ */
+class BackupChangeJob : JobService() {
+
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
+
+ override fun onStartJob(params: JobParameters): Boolean {
+ scope.launch {
+ // Retried when it failed early, so the watch isn't lost; once re-armed, the retry is moot.
+ val failed = try {
+ val deps = EntryPointAccessors.fromApplication(applicationContext, BackupWorker.Deps::class.java)
+ BackupScheduler.changed(applicationContext, deps.settingsPrefs())
+ false
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ Log.w(TAG, "Queueing a backup after a change failed", e)
+ true
+ }
+ jobFinished(params, failed)
+ }
+ return true
+ }
+
+ /** Stopped before it could re-arm: run again rather than leave the provider unwatched. */
+ override fun onStopJob(params: JobParameters): Boolean = true
+
+ override fun onDestroy() {
+ scope.cancel()
+ super.onDestroy()
+ }
+
+ companion object {
+ /** Above the range WorkManager is given (see CalendulaApp). */
+ const val JOB_ID = 0x0516_0001
+ private const val SETTLE_MINUTES = 2L
+ private const val MAX_WAIT_MINUTES = 15L
+ private const val TAG = "BackupChangeJob"
+
+ fun schedule(context: Context) {
+ val job = JobInfo.Builder(JOB_ID, ComponentName(context, BackupChangeJob::class.java))
+ .addTriggerContentUri(
+ JobInfo.TriggerContentUri(
+ CalendarContract.CONTENT_URI,
+ JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS,
+ ),
+ )
+ .setTriggerContentUpdateDelay(TimeUnit.MINUTES.toMillis(SETTLE_MINUTES))
+ .setTriggerContentMaxDelay(TimeUnit.MINUTES.toMillis(MAX_WAIT_MINUTES))
+ .build()
+ if (context.getSystemService(JobScheduler::class.java).schedule(job) != JobScheduler.RESULT_SUCCESS) {
+ Log.w(TAG, "The change watch could not be scheduled")
+ }
+ }
+
+ fun isPending(context: Context): Boolean =
+ context.getSystemService(JobScheduler::class.java).getPendingJob(JOB_ID) != null
+
+ fun cancel(context: Context) = context.getSystemService(JobScheduler::class.java).cancel(JOB_ID)
+ }
+}
diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt
index 0ca2f272..d2f9027c 100644
--- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt
+++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt
@@ -15,6 +15,9 @@ import de.jeanlucmakiola.calendula.domain.backup.normalizeBackupKeep
import de.jeanlucmakiola.calendula.domain.backup.BackupLedger
import de.jeanlucmakiola.calendula.domain.backup.parseBackupLedger
import de.jeanlucmakiola.calendula.domain.backup.persistBackupLedger
+import de.jeanlucmakiola.calendula.domain.backup.BackupFingerprints
+import de.jeanlucmakiola.calendula.domain.backup.parseBackupFingerprints
+import de.jeanlucmakiola.calendula.domain.backup.persistBackupFingerprints
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
import de.jeanlucmakiola.calendula.domain.reminders.ReminderAlertStyle
import de.jeanlucmakiola.calendula.domain.reminders.parseAlertStyles
@@ -221,6 +224,8 @@ class SettingsPrefs @Inject constructor(
val autoBackupCombined: Flow = prefs.snapshot { it.autoBackupCombined }
val autoBackupKeep: Flow = prefs.flow(SettingsKeys.autoBackupKeep)
val autoBackupLedger: Flow = prefs.flow(SettingsKeys.autoBackupLedger)
+ val autoBackupOnChange: Flow = prefs.flow(SettingsKeys.autoBackupOnChange)
+ val autoBackupFingerprints: Flow = prefs.flow(SettingsKeys.autoBackupFingerprints)
val specialDatesEnabled: Flow = prefs.flow(SettingsKeys.specialDatesEnabled)
val specialDatesTypes: Flow> = prefs.snapshot { it.specialDatesTypes }
val specialDatesCalendars: Flow