Merge pull request #771 from grote/770-backup-start

Unify code to start new backup
This commit is contained in:
Torsten Grote 2024-10-08 18:21:15 -03:00 committed by GitHub
commit a9df9a2d2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 32 deletions

View file

@ -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)
}
}

View file

@ -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<AppStatusResult> = liveData(Dispatchers.Default) {

View file

@ -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())

View file

@ -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