diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderBackupPlugin.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderBackupPlugin.kt index bd9a0fba..c3e38da3 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderBackupPlugin.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderBackupPlugin.kt @@ -32,6 +32,7 @@ internal class DocumentsProviderBackupPlugin( // check if storage is already initialized if (storage.isInitialized()) return false + // TODO consider not creating new RestoreSets, but continue working within the existing one. // reset current storage storage.reset(newToken) diff --git a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt index d5673a36..3336cc39 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt @@ -1,11 +1,13 @@ package com.stevesoltys.seedvault.settings +import android.app.backup.RestoreSet import android.content.Context import android.hardware.usb.UsbDevice import android.net.Uri import androidx.annotation.UiThread import androidx.documentfile.provider.DocumentFile import androidx.preference.PreferenceManager +import com.stevesoltys.seedvault.transport.ConfigurableBackupTransport import java.util.concurrent.ConcurrentSkipListSet import java.util.concurrent.atomic.AtomicBoolean @@ -44,7 +46,6 @@ class SettingsManager(context: Context) { .putString(PREF_KEY_STORAGE_NAME, storage.name) .putBoolean(PREF_KEY_STORAGE_IS_USB, storage.isUsb) .apply() - isStorageChanging.set(true) } fun getStorage(): Storage? { @@ -56,7 +57,21 @@ class SettingsManager(context: Context) { return Storage(uri, name, isUsb) } - // TODO find a better solution for this hack abusing the settings manager + /** + * When [ConfigurableBackupTransport.initializeDevice] we try to avoid deleting all stored data, + * as this gets frequently called after network errors by SAF cloud providers. + * + * This method allows us to force a re-initialization of the underlying storage root + * when we change to a new storage provider. + * Currently, this causes us to create a new [RestoreSet]. + * + * As part of the initialization, [getAndResetIsStorageChanging] should get called + * to prevent future calls from causing re-initializations. + */ + fun forceStorageInitialization() { + isStorageChanging.set(true) + } + fun getAndResetIsStorageChanging(): Boolean { return isStorageChanging.getAndSet(false) } 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 35e9d468..b78b5613 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 @@ -24,6 +24,7 @@ internal class BackupStorageViewModel( override fun onLocationSet(uri: Uri) { val isUsb = saveStorage(uri) + settingsManager.forceStorageInitialization() // initialize the new location, will also generate a new backup token val observer = InitializationObserver()