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:
Torsten Grote 2024-02-22 13:36:43 -03:00
parent 8a870d8942
commit e7e489e091
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
5 changed files with 13 additions and 8 deletions

View file

@ -63,7 +63,7 @@ class UsbIntentReceiver : UsbMonitor() {
i.putExtra(EXTRA_START_APP_BACKUP, true) i.putExtra(EXTRA_START_APP_BACKUP, true)
startForegroundService(context, i) startForegroundService(context, i)
} else { } else {
AppBackupWorker.scheduleNow(context) AppBackupWorker.scheduleNow(context, reschedule = false)
} }
} }

View file

@ -193,7 +193,8 @@ internal class SettingsViewModel(
i.putExtra(EXTRA_START_APP_BACKUP, true) i.putExtra(EXTRA_START_APP_BACKUP, true)
startForegroundService(app, i) startForegroundService(app, i)
} else { } else {
AppBackupWorker.scheduleNow(app) val isUsb = settingsManager.getStorage()?.isUsb ?: false
AppBackupWorker.scheduleNow(app, reschedule = !isUsb)
} }
} }
} }

View file

@ -1,6 +1,7 @@
package com.stevesoltys.seedvault.storage package com.stevesoltys.seedvault.storage
import android.content.Intent import android.content.Intent
import com.stevesoltys.seedvault.settings.SettingsManager
import com.stevesoltys.seedvault.worker.AppBackupWorker import com.stevesoltys.seedvault.worker.AppBackupWorker
import org.calyxos.backup.storage.api.BackupObserver import org.calyxos.backup.storage.api.BackupObserver
import org.calyxos.backup.storage.api.RestoreObserver import org.calyxos.backup.storage.api.RestoreObserver
@ -33,6 +34,7 @@ internal class StorageBackupService : BackupService() {
} }
override val storageBackup: StorageBackup by inject() override val storageBackup: StorageBackup by inject()
private val settingsManager: SettingsManager by inject()
// use lazy delegate because context isn't available during construction time // use lazy delegate because context isn't available during construction time
override val backupObserver: BackupObserver by lazy { override val backupObserver: BackupObserver by lazy {
@ -41,7 +43,8 @@ internal class StorageBackupService : BackupService() {
override fun onBackupFinished(intent: Intent, success: Boolean) { override fun onBackupFinished(intent: Intent, success: Boolean) {
if (intent.getBooleanExtra(EXTRA_START_APP_BACKUP, false)) { if (intent.getBooleanExtra(EXTRA_START_APP_BACKUP, false)) {
AppBackupWorker.scheduleNow(applicationContext) val isUsb = settingsManager.getStorage()?.isUsb ?: false
AppBackupWorker.scheduleNow(applicationContext, reschedule = !isUsb)
} }
} }
} }

View file

@ -106,7 +106,8 @@ internal class BackupStorageViewModel(
// notify the UI that the location has been set // notify the UI that the location has been set
mLocationChecked.postEvent(LocationResult()) mLocationChecked.postEvent(LocationResult())
if (requestBackup) { if (requestBackup) {
AppBackupWorker.scheduleNow(app) val isUsb = settingsManager.getStorage()?.isUsb ?: false
AppBackupWorker.scheduleNow(app, reschedule = !isUsb)
} }
} else { } else {
// notify the UI that the location was invalid // notify the UI that the location was invalid

View file

@ -36,7 +36,7 @@ class AppBackupWorker(
companion object { companion object {
private val TAG = AppBackupWorker::class.simpleName private val TAG = AppBackupWorker::class.simpleName
internal const val UNIQUE_WORK_NAME = "com.stevesoltys.seedvault.APP_BACKUP" 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) { fun schedule(context: Context, existingWorkPolicy: ExistingPeriodicWorkPolicy = UPDATE) {
val constraints = Constraints.Builder() val constraints = Constraints.Builder()
@ -56,10 +56,10 @@ class AppBackupWorker(
workManager.enqueueUniquePeriodicWork(UNIQUE_WORK_NAME, existingWorkPolicy, workRequest) workManager.enqueueUniquePeriodicWork(UNIQUE_WORK_NAME, existingWorkPolicy, workRequest)
} }
fun scheduleNow(context: Context) { fun scheduleNow(context: Context, reschedule: Boolean) {
val workRequest = OneTimeWorkRequestBuilder<AppBackupWorker>() val workRequest = OneTimeWorkRequestBuilder<AppBackupWorker>()
.setExpedited(RUN_AS_NON_EXPEDITED_WORK_REQUEST) .setExpedited(RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.addTag(TAG_NOW) .apply { if (reschedule) addTag(TAG_RESCHEDULE) }
.build() .build()
val workManager = WorkManager.getInstance(context) val workManager = WorkManager.getInstance(context)
Log.i(TAG, "Asking to do app backup now...") Log.i(TAG, "Asking to do app backup now...")
@ -93,7 +93,7 @@ class AppBackupWorker(
} finally { } finally {
// schedule next backup, because the old one gets lost // schedule next backup, because the old one gets lost
// when scheduling a OneTimeWorkRequest with the same unique name via scheduleNow() // 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 // needs to use CANCEL_AND_REENQUEUE otherwise it doesn't get scheduled
schedule(applicationContext, CANCEL_AND_REENQUEUE) schedule(applicationContext, CANCEL_AND_REENQUEUE)
} }