From d6572979cc6f0986bf7c139d582dd12e9aef8da0 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 7 Oct 2024 09:47:33 -0300 Subject: [PATCH] Unify code to start new backup to ensure that files backup and app backup will always run if they should --- .../seedvault/UsbIntentReceiver.kt | 16 ++-------- .../seedvault/settings/SettingsViewModel.kt | 18 ++---------- .../ui/storage/BackupStorageViewModel.kt | 5 ++-- .../seedvault/worker/BackupRequester.kt | 29 +++++++++++++++++++ 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt b/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt index c6f0c088..0cfcfcde 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/UsbIntentReceiver.kt @@ -20,14 +20,11 @@ import android.os.Handler import android.os.Looper import android.provider.DocumentsContract import android.util.Log -import androidx.core.content.ContextCompat.startForegroundService import com.stevesoltys.seedvault.metadata.MetadataManager import com.stevesoltys.seedvault.settings.FlashDrive import com.stevesoltys.seedvault.settings.SettingsManager -import com.stevesoltys.seedvault.storage.StorageBackupService -import com.stevesoltys.seedvault.storage.StorageBackupService.Companion.EXTRA_START_APP_BACKUP import com.stevesoltys.seedvault.ui.storage.AUTHORITY_STORAGE -import com.stevesoltys.seedvault.worker.AppBackupWorker +import com.stevesoltys.seedvault.worker.BackupRequester.Companion.requestFilesAndAppBackup import org.koin.core.context.GlobalContext.get import java.util.Date @@ -64,16 +61,7 @@ class UsbIntentReceiver : UsbMonitor() { } override fun onStatusChanged(context: Context, action: String, device: UsbDevice) { - if (settingsManager.isStorageBackupEnabled()) { - val i = Intent(context, StorageBackupService::class.java) - // this starts an app backup afterwards - i.putExtra(EXTRA_START_APP_BACKUP, true) - startForegroundService(context, i) - } else if (backupManager.isBackupEnabled) { - AppBackupWorker.scheduleNow(context, reschedule = false) - } else { - Log.d(TAG, "Neither files nor app backup enabled, do nothing.") - } + requestFilesAndAppBackup(context, settingsManager, backupManager) } } 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 2c1de79d..e5ccd77e 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt @@ -9,7 +9,6 @@ import android.app.Application import android.app.backup.IBackupManager import android.app.job.JobInfo.NETWORK_TYPE_NONE import android.app.job.JobInfo.NETWORK_TYPE_UNMETERED -import android.content.Intent import android.database.ContentObserver import android.net.ConnectivityManager import android.net.Network @@ -24,7 +23,6 @@ import android.util.Log import android.widget.Toast import android.widget.Toast.LENGTH_LONG import androidx.annotation.UiThread -import androidx.core.content.ContextCompat.startForegroundService import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.liveData @@ -42,14 +40,13 @@ import com.stevesoltys.seedvault.metadata.MetadataManager import com.stevesoltys.seedvault.permitDiskReads import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.storage.StorageBackupJobService -import com.stevesoltys.seedvault.storage.StorageBackupService -import com.stevesoltys.seedvault.storage.StorageBackupService.Companion.EXTRA_START_APP_BACKUP import com.stevesoltys.seedvault.transport.backup.BackupInitializer import com.stevesoltys.seedvault.ui.LiveEvent import com.stevesoltys.seedvault.ui.MutableLiveEvent import com.stevesoltys.seedvault.ui.RequireProvisioningViewModel import com.stevesoltys.seedvault.worker.AppBackupWorker import com.stevesoltys.seedvault.worker.AppBackupWorker.Companion.UNIQUE_WORK_NAME +import com.stevesoltys.seedvault.worker.BackupRequester.Companion.requestFilesAndAppBackup import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow @@ -224,17 +221,8 @@ internal class SettingsViewModel( } internal fun backupNow() { - viewModelScope.launch(Dispatchers.IO) { - val isAppBackupEnabled = backupManager.isBackupEnabled - if (settingsManager.isStorageBackupEnabled()) { - val i = Intent(app, StorageBackupService::class.java) - // this starts an app backup afterwards (if enabled) - i.putExtra(EXTRA_START_APP_BACKUP, isAppBackupEnabled) - startForegroundService(app, i) - } else if (isAppBackupEnabled) { - AppBackupWorker.scheduleNow(app, reschedule = !backendManager.isOnRemovableDrive) - } - } + val reschedule = !backendManager.isOnRemovableDrive + requestFilesAndAppBackup(app, settingsManager, backupManager, reschedule) } private fun getAppStatusResult(): LiveData = liveData(Dispatchers.Default) { 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 bacd0518..b3cf1cc4 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 @@ -21,6 +21,7 @@ import com.stevesoltys.seedvault.settings.SettingsManager import com.stevesoltys.seedvault.storage.StorageBackupJobService import com.stevesoltys.seedvault.transport.backup.BackupInitializer import com.stevesoltys.seedvault.worker.AppBackupWorker +import com.stevesoltys.seedvault.worker.BackupRequester.Companion.requestFilesAndAppBackup import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.calyxos.backup.storage.api.StorageBackup @@ -70,8 +71,6 @@ internal class BackupStorageViewModel( viewModelScope.launch(Dispatchers.IO) { try { // remove old storage snapshots and clear cache - // TODO For SAF, this also does create all 255 chunk folders which takes time - // pass a flag to getCurrentBackupSnapshots() to not create missing folders? storageBackup.init() // initialize the new location (if backups are enabled) if (backupManager.isBackupEnabled) { @@ -83,7 +82,7 @@ internal class BackupStorageViewModel( val requestBackup = isUsb && !isSetupWizard if (requestBackup) { Log.i(TAG, "Requesting a backup now, because we use USB storage") - AppBackupWorker.scheduleNow(app, reschedule = false) + requestFilesAndAppBackup(app, settingsManager, backupManager) } // notify the UI that the location has been set mLocationChecked.postEvent(LocationResult()) diff --git a/app/src/main/java/com/stevesoltys/seedvault/worker/BackupRequester.kt b/app/src/main/java/com/stevesoltys/seedvault/worker/BackupRequester.kt index 02d4cc3b..f7a7ce95 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/worker/BackupRequester.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/worker/BackupRequester.kt @@ -8,9 +8,15 @@ package com.stevesoltys.seedvault.worker import android.app.backup.BackupManager import android.app.backup.IBackupManager import android.content.Context +import android.content.Intent import android.os.RemoteException import android.util.Log +import androidx.annotation.UiThread import androidx.annotation.WorkerThread +import androidx.core.content.ContextCompat.startForegroundService +import com.stevesoltys.seedvault.settings.SettingsManager +import com.stevesoltys.seedvault.storage.StorageBackupService +import com.stevesoltys.seedvault.storage.StorageBackupService.Companion.EXTRA_START_APP_BACKUP import com.stevesoltys.seedvault.BackupMonitor import com.stevesoltys.seedvault.transport.backup.PackageService import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager @@ -35,6 +41,29 @@ internal class BackupRequester( val packageService: PackageService, ) : KoinComponent { + companion object { + @UiThread + fun requestFilesAndAppBackup( + context: Context, + settingsManager: SettingsManager, + backupManager: IBackupManager, + reschedule: Boolean = false, + ) { + val appBackupEnabled = backupManager.isBackupEnabled + // TODO eventually, we could think about running files and app backup simultaneously + if (settingsManager.isStorageBackupEnabled()) { + val i = Intent(context, StorageBackupService::class.java) + // this starts an app backup afterwards + i.putExtra(EXTRA_START_APP_BACKUP, appBackupEnabled) + startForegroundService(context, i) + } else if (appBackupEnabled) { + AppBackupWorker.scheduleNow(context, reschedule) + } else { + Log.d(TAG, "Neither files nor app backup enabled, do nothing.") + } + } + } + val isBackupEnabled: Boolean get() = backupManager.isBackupEnabled private val packages = packageService.eligiblePackages