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.os.Looper
|
||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.core.content.ContextCompat.startForegroundService
|
||||||
import com.stevesoltys.seedvault.metadata.MetadataManager
|
import com.stevesoltys.seedvault.metadata.MetadataManager
|
||||||
import com.stevesoltys.seedvault.settings.FlashDrive
|
import com.stevesoltys.seedvault.settings.FlashDrive
|
||||||
import com.stevesoltys.seedvault.settings.SettingsManager
|
import com.stevesoltys.seedvault.settings.SettingsManager
|
||||||
|
import com.stevesoltys.seedvault.storage.StorageBackupService
|
||||||
import com.stevesoltys.seedvault.transport.requestBackup
|
import com.stevesoltys.seedvault.transport.requestBackup
|
||||||
import com.stevesoltys.seedvault.ui.storage.AUTHORITY_STORAGE
|
import com.stevesoltys.seedvault.ui.storage.AUTHORITY_STORAGE
|
||||||
import org.koin.core.context.KoinContextHandler.get
|
import org.koin.core.context.KoinContextHandler.get
|
||||||
|
@ -54,6 +56,12 @@ class UsbIntentReceiver : UsbMonitor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStatusChanged(context: Context, action: String, device: UsbDevice) {
|
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 {
|
Thread {
|
||||||
requestBackup(context)
|
requestBackup(context)
|
||||||
}.start()
|
}.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_APP_BLACKLIST = "backupAppBlacklist"
|
||||||
|
|
||||||
|
private const val PREF_KEY_BACKUP_STORAGE = "backup_storage"
|
||||||
|
|
||||||
class SettingsManager(private val context: Context) {
|
class SettingsManager(private val context: Context) {
|
||||||
|
|
||||||
private val prefs = permitDiskReads {
|
private val prefs = permitDiskReads {
|
||||||
|
@ -138,6 +140,8 @@ class SettingsManager(private val context: Context) {
|
||||||
|
|
||||||
fun isBackupEnabled(packageName: String) = !blacklistedApps.contains(packageName)
|
fun isBackupEnabled(packageName: String) = !blacklistedApps.contains(packageName)
|
||||||
|
|
||||||
|
fun isStorageBackupEnabled() = prefs.getBoolean(PREF_KEY_BACKUP_STORAGE, false)
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
fun onAppBackupStatusChanged(status: AppStatus) {
|
fun onAppBackupStatusChanged(status: AppStatus) {
|
||||||
if (status.enabled) blacklistedApps.remove(status.packageName)
|
if (status.enabled) blacklistedApps.remove(status.packageName)
|
||||||
|
|
|
@ -126,6 +126,13 @@ internal class SettingsViewModel(
|
||||||
networkCallback.registered = true
|
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) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val canDo = settingsManager.canDoBackupNow()
|
val canDo = settingsManager.canDoBackupNow()
|
||||||
mBackupPossible.postValue(canDo)
|
mBackupPossible.postValue(canDo)
|
||||||
|
@ -192,14 +199,16 @@ internal class SettingsViewModel(
|
||||||
return keyManager.hasMainKey()
|
return keyManager.hasMainKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun enableStorageBackup() = BackupJobService.scheduleJob(
|
fun enableStorageBackup() {
|
||||||
context = app,
|
if (settingsManager.getStorage()?.isUsb == false) BackupJobService.scheduleJob(
|
||||||
jobServiceClass = StorageBackupJobService::class.java,
|
context = app,
|
||||||
periodMillis = HOURS.toMillis(24),
|
jobServiceClass = StorageBackupJobService::class.java,
|
||||||
networkType = NETWORK_TYPE_UNMETERED,
|
periodMillis = HOURS.toMillis(24),
|
||||||
deviceIdle = false,
|
networkType = NETWORK_TYPE_UNMETERED,
|
||||||
charging = true
|
deviceIdle = false,
|
||||||
)
|
charging = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun disableStorageBackup() {
|
fun disableStorageBackup() {
|
||||||
BackupJobService.cancelJob(app)
|
BackupJobService.cancelJob(app)
|
||||||
|
|
Loading…
Add table
Reference in a new issue