diff --git a/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.kt b/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.kt index 66efce96..eadf7d28 100644 --- a/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.kt +++ b/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.kt @@ -86,9 +86,9 @@ class SettingsManager(context: Context) { /** * Sets the last backup time to "now". */ - fun saveNewBackupTime(millis: Long = Date().time) { + fun saveNewBackupTime() { prefs.edit() - .putLong(PREF_KEY_BACKUP_TIME, millis) + .putLong(PREF_KEY_BACKUP_TIME, Date().time) .apply() } diff --git a/app/src/main/java/com/stevesoltys/backup/ui/storage/BackupStorageViewModel.kt b/app/src/main/java/com/stevesoltys/backup/ui/storage/BackupStorageViewModel.kt index 02a1be4e..9f771ca1 100644 --- a/app/src/main/java/com/stevesoltys/backup/ui/storage/BackupStorageViewModel.kt +++ b/app/src/main/java/com/stevesoltys/backup/ui/storage/BackupStorageViewModel.kt @@ -10,6 +10,7 @@ import androidx.annotation.WorkerThread import com.stevesoltys.backup.Backup import com.stevesoltys.backup.R import com.stevesoltys.backup.transport.TRANSPORT_ID +import com.stevesoltys.backup.transport.requestBackup private val TAG = BackupStorageViewModel::class.java.simpleName @@ -18,7 +19,7 @@ internal class BackupStorageViewModel(private val app: Application) : StorageVie override val isRestoreOperation = false override fun onLocationSet(uri: Uri) { - saveStorage(uri) + val isUsb = saveStorage(uri) // use a new backup token settingsManager.getAndSaveNewBackupToken() @@ -26,6 +27,11 @@ internal class BackupStorageViewModel(private val app: Application) : StorageVie // initialize the new location val observer = InitializationObserver() Backup.backupManager.initializeTransportsForUser(UserHandle.myUserId(), arrayOf(TRANSPORT_ID), observer) + + // if storage is on USB and this is not SetupWizard, do a backup right away + if (isUsb && !isSetupWizard) Thread { + requestBackup(app) + }.start() } @WorkerThread diff --git a/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageActivity.kt b/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageActivity.kt index e4e50af0..b5981491 100644 --- a/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageActivity.kt +++ b/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageActivity.kt @@ -31,6 +31,7 @@ class StorageActivity : BackupActivity() { } else { ViewModelProviders.of(this).get(BackupStorageViewModel::class.java) } + viewModel.isSetupWizard = isSetupWizard() viewModel.locationSet.observeEvent(this, LiveEventHandler { showFragment(StorageCheckFragment.newInstance(getCheckFragmentTitle()), true) diff --git a/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageViewModel.kt b/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageViewModel.kt index 596c6ffa..2b01336c 100644 --- a/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageViewModel.kt +++ b/app/src/main/java/com/stevesoltys/backup/ui/storage/StorageViewModel.kt @@ -40,6 +40,7 @@ internal abstract class StorageViewModel(private val app: Application) : Android private val storageRootFetcher by lazy { StorageRootFetcher(app) } private var storageRoot: StorageRoot? = null + internal var isSetupWizard: Boolean = false abstract val isRestoreOperation: Boolean companion object { @@ -79,9 +80,12 @@ internal abstract class StorageViewModel(private val app: Application) : Android onLocationSet(uri) } - abstract fun onLocationSet(uri: Uri) - - protected fun saveStorage(uri: Uri) { + /** + * Saves the storage behind the given [Uri] (and saved [storageRoot]). + * + * @return true if the storage is a USB flash drive, false otherwise. + */ + protected fun saveStorage(uri: Uri): Boolean { // store backup storage location in settings val root = storageRoot ?: throw IllegalStateException() val name = if (root.isInternal()) { @@ -109,6 +113,8 @@ internal abstract class StorageViewModel(private val app: Application) : Android app.stopService(Intent(app, ConfigurableBackupTransportService::class.java)) Log.d(TAG, "New storage location saved: $uri") + + return storage.isUsb } private fun saveUsbDevice(): Boolean { @@ -123,6 +129,8 @@ internal abstract class StorageViewModel(private val app: Application) : Android return false } + abstract fun onLocationSet(uri: Uri) + override fun onCleared() { storageRootFetcher.setRemovableStorageListener(null) super.onCleared()