Only reschedule next app backup when not on USB storage
Currently, after a manual run, we need to schedule the background backups again, because the scheduling gets lost. However, we need to be careful not to do that when the backup destination is on removable storage. Then we don't want to run.
This commit is contained in:
parent
8a870d8942
commit
e7e489e091
5 changed files with 13 additions and 8 deletions
|
@ -63,7 +63,7 @@ class UsbIntentReceiver : UsbMonitor() {
|
|||
i.putExtra(EXTRA_START_APP_BACKUP, true)
|
||||
startForegroundService(context, i)
|
||||
} else {
|
||||
AppBackupWorker.scheduleNow(context)
|
||||
AppBackupWorker.scheduleNow(context, reschedule = false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -193,7 +193,8 @@ internal class SettingsViewModel(
|
|||
i.putExtra(EXTRA_START_APP_BACKUP, true)
|
||||
startForegroundService(app, i)
|
||||
} else {
|
||||
AppBackupWorker.scheduleNow(app)
|
||||
val isUsb = settingsManager.getStorage()?.isUsb ?: false
|
||||
AppBackupWorker.scheduleNow(app, reschedule = !isUsb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.stevesoltys.seedvault.storage
|
||||
|
||||
import android.content.Intent
|
||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||
import com.stevesoltys.seedvault.worker.AppBackupWorker
|
||||
import org.calyxos.backup.storage.api.BackupObserver
|
||||
import org.calyxos.backup.storage.api.RestoreObserver
|
||||
|
@ -33,6 +34,7 @@ internal class StorageBackupService : BackupService() {
|
|||
}
|
||||
|
||||
override val storageBackup: StorageBackup by inject()
|
||||
private val settingsManager: SettingsManager by inject()
|
||||
|
||||
// use lazy delegate because context isn't available during construction time
|
||||
override val backupObserver: BackupObserver by lazy {
|
||||
|
@ -41,7 +43,8 @@ internal class StorageBackupService : BackupService() {
|
|||
|
||||
override fun onBackupFinished(intent: Intent, success: Boolean) {
|
||||
if (intent.getBooleanExtra(EXTRA_START_APP_BACKUP, false)) {
|
||||
AppBackupWorker.scheduleNow(applicationContext)
|
||||
val isUsb = settingsManager.getStorage()?.isUsb ?: false
|
||||
AppBackupWorker.scheduleNow(applicationContext, reschedule = !isUsb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,7 +106,8 @@ internal class BackupStorageViewModel(
|
|||
// notify the UI that the location has been set
|
||||
mLocationChecked.postEvent(LocationResult())
|
||||
if (requestBackup) {
|
||||
AppBackupWorker.scheduleNow(app)
|
||||
val isUsb = settingsManager.getStorage()?.isUsb ?: false
|
||||
AppBackupWorker.scheduleNow(app, reschedule = !isUsb)
|
||||
}
|
||||
} else {
|
||||
// notify the UI that the location was invalid
|
||||
|
|
|
@ -36,7 +36,7 @@ class AppBackupWorker(
|
|||
companion object {
|
||||
private val TAG = AppBackupWorker::class.simpleName
|
||||
internal const val UNIQUE_WORK_NAME = "com.stevesoltys.seedvault.APP_BACKUP"
|
||||
private const val TAG_NOW = "com.stevesoltys.seedvault.TAG_NOW"
|
||||
private const val TAG_RESCHEDULE = "com.stevesoltys.seedvault.TAG_RESCHEDULE"
|
||||
|
||||
fun schedule(context: Context, existingWorkPolicy: ExistingPeriodicWorkPolicy = UPDATE) {
|
||||
val constraints = Constraints.Builder()
|
||||
|
@ -56,10 +56,10 @@ class AppBackupWorker(
|
|||
workManager.enqueueUniquePeriodicWork(UNIQUE_WORK_NAME, existingWorkPolicy, workRequest)
|
||||
}
|
||||
|
||||
fun scheduleNow(context: Context) {
|
||||
fun scheduleNow(context: Context, reschedule: Boolean) {
|
||||
val workRequest = OneTimeWorkRequestBuilder<AppBackupWorker>()
|
||||
.setExpedited(RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
||||
.addTag(TAG_NOW)
|
||||
.apply { if (reschedule) addTag(TAG_RESCHEDULE) }
|
||||
.build()
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
Log.i(TAG, "Asking to do app backup now...")
|
||||
|
@ -93,7 +93,7 @@ class AppBackupWorker(
|
|||
} finally {
|
||||
// schedule next backup, because the old one gets lost
|
||||
// when scheduling a OneTimeWorkRequest with the same unique name via scheduleNow()
|
||||
if (tags.contains(TAG_NOW) && backupRequester.isBackupEnabled) {
|
||||
if (tags.contains(TAG_RESCHEDULE) && backupRequester.isBackupEnabled) {
|
||||
// needs to use CANCEL_AND_REENQUEUE otherwise it doesn't get scheduled
|
||||
schedule(applicationContext, CANCEL_AND_REENQUEUE)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue