handle storage backup when USB is used as target
This commit is contained in:
parent
6c633b70c3
commit
5b2b75047e
3 changed files with 29 additions and 8 deletions
|
@ -14,9 +14,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.transport.requestBackup
|
||||
import com.stevesoltys.seedvault.ui.storage.AUTHORITY_STORAGE
|
||||
import org.koin.core.context.KoinContextHandler.get
|
||||
|
@ -54,6 +56,12 @@ class UsbIntentReceiver : UsbMonitor() {
|
|||
}
|
||||
|
||||
override fun onStatusChanged(context: Context, action: String, device: UsbDevice) {
|
||||
if (settingsManager.isStorageBackupEnabled()) {
|
||||
// TODO is it safe to start this at the same time as app backup?
|
||||
val i = Intent(context, StorageBackupService::class.java)
|
||||
startForegroundService(context, i)
|
||||
}
|
||||
|
||||
Thread {
|
||||
requestBackup(context)
|
||||
}.start()
|
||||
|
|
|
@ -30,6 +30,8 @@ private const val PREF_KEY_FLASH_DRIVE_PRODUCT_ID = "flashDriveProductId"
|
|||
|
||||
private const val PREF_KEY_BACKUP_APP_BLACKLIST = "backupAppBlacklist"
|
||||
|
||||
private const val PREF_KEY_BACKUP_STORAGE = "backup_storage"
|
||||
|
||||
class SettingsManager(private val context: Context) {
|
||||
|
||||
private val prefs = permitDiskReads {
|
||||
|
@ -138,6 +140,8 @@ class SettingsManager(private val context: Context) {
|
|||
|
||||
fun isBackupEnabled(packageName: String) = !blacklistedApps.contains(packageName)
|
||||
|
||||
fun isStorageBackupEnabled() = prefs.getBoolean(PREF_KEY_BACKUP_STORAGE, false)
|
||||
|
||||
@UiThread
|
||||
fun onAppBackupStatusChanged(status: AppStatus) {
|
||||
if (status.enabled) blacklistedApps.remove(status.packageName)
|
||||
|
|
|
@ -126,6 +126,13 @@ internal class SettingsViewModel(
|
|||
networkCallback.registered = true
|
||||
}
|
||||
|
||||
if (settingsManager.isStorageBackupEnabled()) {
|
||||
// disable storage backup if new storage is on USB
|
||||
if (storage.isUsb) disableStorageBackup()
|
||||
// enable it, just in case the previous storage was on USB
|
||||
else enableStorageBackup()
|
||||
}
|
||||
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val canDo = settingsManager.canDoBackupNow()
|
||||
mBackupPossible.postValue(canDo)
|
||||
|
@ -192,14 +199,16 @@ internal class SettingsViewModel(
|
|||
return keyManager.hasMainKey()
|
||||
}
|
||||
|
||||
fun enableStorageBackup() = BackupJobService.scheduleJob(
|
||||
context = app,
|
||||
jobServiceClass = StorageBackupJobService::class.java,
|
||||
periodMillis = HOURS.toMillis(24),
|
||||
networkType = NETWORK_TYPE_UNMETERED,
|
||||
deviceIdle = false,
|
||||
charging = true
|
||||
)
|
||||
fun enableStorageBackup() {
|
||||
if (settingsManager.getStorage()?.isUsb == false) BackupJobService.scheduleJob(
|
||||
context = app,
|
||||
jobServiceClass = StorageBackupJobService::class.java,
|
||||
periodMillis = HOURS.toMillis(24),
|
||||
networkType = NETWORK_TYPE_UNMETERED,
|
||||
deviceIdle = false,
|
||||
charging = true
|
||||
)
|
||||
}
|
||||
|
||||
fun disableStorageBackup() {
|
||||
BackupJobService.cancelJob(app)
|
||||
|
|
Loading…
Add table
Reference in a new issue