diff --git a/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt b/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt index ff5208b0..a14cd0c6 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt @@ -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) } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt index 73f0bb3d..2c421e14 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt @@ -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) } } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/storage/Services.kt b/app/src/main/java/com/stevesoltys/seedvault/storage/Services.kt index 59b7278e..5a9096d3 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/storage/Services.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/storage/Services.kt @@ -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) } } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/ui/storage/BackupStorageViewModel.kt b/app/src/main/java/com/stevesoltys/seedvault/ui/storage/BackupStorageViewModel.kt index 01cc817f..141992ea 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/ui/storage/BackupStorageViewModel.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/ui/storage/BackupStorageViewModel.kt @@ -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 diff --git a/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt b/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt index 32c44a24..31587fd7 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt @@ -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() .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) }