From 878492088d078e881c832c550e570e56fb146a98 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Tue, 18 Jun 2024 11:10:10 -0300
Subject: [PATCH 1/2] Don't disable framework scheduling when not current
 transport

---
 app/src/main/java/com/stevesoltys/seedvault/App.kt | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/com/stevesoltys/seedvault/App.kt b/app/src/main/java/com/stevesoltys/seedvault/App.kt
index 4391067f..053af85f 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/App.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/App.kt
@@ -33,6 +33,7 @@ import com.stevesoltys.seedvault.settings.AppListRetriever
 import com.stevesoltys.seedvault.settings.SettingsManager
 import com.stevesoltys.seedvault.settings.SettingsViewModel
 import com.stevesoltys.seedvault.storage.storageModule
+import com.stevesoltys.seedvault.transport.TRANSPORT_ID
 import com.stevesoltys.seedvault.transport.backup.backupModule
 import com.stevesoltys.seedvault.transport.restore.restoreModule
 import com.stevesoltys.seedvault.ui.files.FileSelectionViewModel
@@ -185,12 +186,14 @@ open class App : Application() {
             return
         }
 
-        backupManager.setFrameworkSchedulingEnabledForUser(UserHandle.myUserId(), false)
-        if (backupManager.isBackupEnabled && !pluginManager.isOnRemovableDrive) {
-            AppBackupWorker.schedule(applicationContext, settingsManager, UPDATE)
+        if (backupManager.currentTransport == TRANSPORT_ID) {
+            backupManager.setFrameworkSchedulingEnabledForUser(UserHandle.myUserId(), false)
+            if (backupManager.isBackupEnabled && !pluginManager.isOnRemovableDrive) {
+                AppBackupWorker.schedule(applicationContext, settingsManager, UPDATE)
+            }
+            // cancel old D2D worker
+            WorkManager.getInstance(this).cancelUniqueWork("APP_BACKUP")
         }
-        // cancel old D2D worker
-        WorkManager.getInstance(this).cancelUniqueWork("APP_BACKUP")
     }
 
     private fun isFrameworkSchedulingEnabled(): Boolean = Settings.Secure.getInt(

From 484767bf16b0463f9b802f105f9d4a6f202412c3 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Tue, 18 Jun 2024 11:15:50 -0300
Subject: [PATCH 2/2] Disable framework scheduling when scheduling backup,
 because another transport may have re-enabled it

---
 .../com/stevesoltys/seedvault/settings/SettingsViewModel.kt    | 3 +++
 .../stevesoltys/seedvault/ui/storage/BackupStorageViewModel.kt | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt
index f3a71fa0..cf1fd610 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsViewModel.kt
@@ -18,6 +18,7 @@ import android.net.NetworkRequest
 import android.net.Uri
 import android.os.BadParcelableException
 import android.os.Process.myUid
+import android.os.UserHandle
 import android.provider.Settings
 import android.util.Log
 import android.widget.Toast
@@ -308,6 +309,8 @@ internal class SettingsViewModel(
     }
 
     fun scheduleAppBackup(existingWorkPolicy: ExistingPeriodicWorkPolicy) {
+        // disable framework scheduling, because another transport may have enabled it
+        backupManager.setFrameworkSchedulingEnabledForUser(UserHandle.myUserId(), false)
         if (!pluginManager.isOnRemovableDrive && backupManager.isBackupEnabled) {
             AppBackupWorker.schedule(app, settingsManager, existingWorkPolicy)
         }
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 37d0ef7e..8e71d9cb 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
@@ -8,6 +8,7 @@ package com.stevesoltys.seedvault.ui.storage
 import android.app.Application
 import android.app.backup.IBackupManager
 import android.app.job.JobInfo
+import android.os.UserHandle
 import android.util.Log
 import androidx.lifecycle.viewModelScope
 import androidx.work.ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE
@@ -100,6 +101,8 @@ internal class BackupStorageViewModel(
 
     private fun scheduleBackupWorkers() {
         val storage = storagePluginManager.storageProperties ?: error("no storage available")
+        // disable framework scheduling, because another transport may have enabled it
+        backupManager.setFrameworkSchedulingEnabledForUser(UserHandle.myUserId(), false)
         if (!storage.isUsb) {
             if (backupManager.isBackupEnabled) {
                 AppBackupWorker.schedule(app, settingsManager, CANCEL_AND_REENQUEUE)