From 19bfc41d95144375dd52ab926c3baade2f4f24ed Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 23 Feb 2024 11:35:26 -0300 Subject: [PATCH] Allow backups in metered network, if user wants that --- .../seedvault/settings/SettingsManager.kt | 12 +++++++----- .../seedvault/transport/backup/BackupCoordinator.kt | 5 ++++- .../transport/backup/BackupCoordinatorTest.kt | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt index 5990dcb1..786f5781 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/settings/SettingsManager.kt @@ -146,7 +146,8 @@ class SettingsManager(private val context: Context) { fun canDoBackupNow(): Boolean { val storage = getStorage() ?: return false val systemContext = context.getStorageContext { storage.isUsb } - return !storage.isUnavailableUsb(systemContext) && !storage.isUnavailableNetwork(context) + return !storage.isUnavailableUsb(systemContext) && + !storage.isUnavailableNetwork(context, useMeteredNetwork) } fun backupApks(): Boolean { @@ -208,15 +209,16 @@ data class Storage( * Returns true if this is storage that requires network access, * but it isn't available right now. */ - fun isUnavailableNetwork(context: Context): Boolean { - return requiresNetwork && !hasUnmeteredInternet(context) + fun isUnavailableNetwork(context: Context, allowMetered: Boolean): Boolean { + return requiresNetwork && !hasUnmeteredInternet(context, allowMetered) } - private fun hasUnmeteredInternet(context: Context): Boolean { + private fun hasUnmeteredInternet(context: Context, allowMetered: Boolean): Boolean { val cm = context.getSystemService(ConnectivityManager::class.java) ?: return false val isMetered = cm.isActiveNetworkMetered val capabilities = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false - return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && !isMetered + return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && + (allowMetered || !isMetered) } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt index 1dfbbce0..e9275bf7 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt @@ -422,7 +422,10 @@ internal class BackupCoordinator( // back off if storage is removable and not available right now storage.isUnavailableUsb(context) -> longBackoff // back off if storage is on network, but we have no access - storage.isUnavailableNetwork(context) -> HOURS.toMillis(1) + storage.isUnavailableNetwork( + context = context, + allowMetered = settingsManager.useMeteredNetwork, + ) -> HOURS.toMillis(1) // otherwise no back off else -> 0L } diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt index a883aaa4..2fed54ab 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt @@ -294,6 +294,7 @@ internal class BackupCoordinatorTest : BackupTest() { } just Runs coEvery { full.cancelFullBackup(token, metadata.salt, false) } just Runs every { settingsManager.getStorage() } returns storage + every { settingsManager.useMeteredNetwork } returns false every { metadataOutputStream.close() } just Runs assertEquals( @@ -343,6 +344,7 @@ internal class BackupCoordinatorTest : BackupTest() { } just Runs coEvery { full.cancelFullBackup(token, metadata.salt, false) } just Runs every { settingsManager.getStorage() } returns storage + every { settingsManager.useMeteredNetwork } returns false every { metadataOutputStream.close() } just Runs assertEquals(