When selecting USB storage, do a backup right away

This is because USB drives are rarely plugged in,
so we should use every chance we get to do a backup.
This commit is contained in:
Torsten Grote 2019-09-19 16:36:40 -03:00
parent 26f23e95fe
commit cc2bb4a651
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
4 changed files with 21 additions and 6 deletions

View file

@ -86,9 +86,9 @@ class SettingsManager(context: Context) {
/** /**
* Sets the last backup time to "now". * Sets the last backup time to "now".
*/ */
fun saveNewBackupTime(millis: Long = Date().time) { fun saveNewBackupTime() {
prefs.edit() prefs.edit()
.putLong(PREF_KEY_BACKUP_TIME, millis) .putLong(PREF_KEY_BACKUP_TIME, Date().time)
.apply() .apply()
} }

View file

@ -10,6 +10,7 @@ import androidx.annotation.WorkerThread
import com.stevesoltys.backup.Backup import com.stevesoltys.backup.Backup
import com.stevesoltys.backup.R import com.stevesoltys.backup.R
import com.stevesoltys.backup.transport.TRANSPORT_ID import com.stevesoltys.backup.transport.TRANSPORT_ID
import com.stevesoltys.backup.transport.requestBackup
private val TAG = BackupStorageViewModel::class.java.simpleName private val TAG = BackupStorageViewModel::class.java.simpleName
@ -18,7 +19,7 @@ internal class BackupStorageViewModel(private val app: Application) : StorageVie
override val isRestoreOperation = false override val isRestoreOperation = false
override fun onLocationSet(uri: Uri) { override fun onLocationSet(uri: Uri) {
saveStorage(uri) val isUsb = saveStorage(uri)
// use a new backup token // use a new backup token
settingsManager.getAndSaveNewBackupToken() settingsManager.getAndSaveNewBackupToken()
@ -26,6 +27,11 @@ internal class BackupStorageViewModel(private val app: Application) : StorageVie
// initialize the new location // initialize the new location
val observer = InitializationObserver() val observer = InitializationObserver()
Backup.backupManager.initializeTransportsForUser(UserHandle.myUserId(), arrayOf(TRANSPORT_ID), observer) 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 @WorkerThread

View file

@ -31,6 +31,7 @@ class StorageActivity : BackupActivity() {
} else { } else {
ViewModelProviders.of(this).get(BackupStorageViewModel::class.java) ViewModelProviders.of(this).get(BackupStorageViewModel::class.java)
} }
viewModel.isSetupWizard = isSetupWizard()
viewModel.locationSet.observeEvent(this, LiveEventHandler { viewModel.locationSet.observeEvent(this, LiveEventHandler {
showFragment(StorageCheckFragment.newInstance(getCheckFragmentTitle()), true) showFragment(StorageCheckFragment.newInstance(getCheckFragmentTitle()), true)

View file

@ -40,6 +40,7 @@ internal abstract class StorageViewModel(private val app: Application) : Android
private val storageRootFetcher by lazy { StorageRootFetcher(app) } private val storageRootFetcher by lazy { StorageRootFetcher(app) }
private var storageRoot: StorageRoot? = null private var storageRoot: StorageRoot? = null
internal var isSetupWizard: Boolean = false
abstract val isRestoreOperation: Boolean abstract val isRestoreOperation: Boolean
companion object { companion object {
@ -79,9 +80,12 @@ internal abstract class StorageViewModel(private val app: Application) : Android
onLocationSet(uri) onLocationSet(uri)
} }
abstract fun onLocationSet(uri: Uri) /**
* Saves the storage behind the given [Uri] (and saved [storageRoot]).
protected fun saveStorage(uri: Uri) { *
* @return true if the storage is a USB flash drive, false otherwise.
*/
protected fun saveStorage(uri: Uri): Boolean {
// store backup storage location in settings // store backup storage location in settings
val root = storageRoot ?: throw IllegalStateException() val root = storageRoot ?: throw IllegalStateException()
val name = if (root.isInternal()) { 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)) app.stopService(Intent(app, ConfigurableBackupTransportService::class.java))
Log.d(TAG, "New storage location saved: $uri") Log.d(TAG, "New storage location saved: $uri")
return storage.isUsb
} }
private fun saveUsbDevice(): Boolean { private fun saveUsbDevice(): Boolean {
@ -123,6 +129,8 @@ internal abstract class StorageViewModel(private val app: Application) : Android
return false return false
} }
abstract fun onLocationSet(uri: Uri)
override fun onCleared() { override fun onCleared() {
storageRootFetcher.setRemovableStorageListener(null) storageRootFetcher.setRemovableStorageListener(null)
super.onCleared() super.onCleared()