Allow backups in metered network, if user wants that

This commit is contained in:
Torsten Grote 2024-02-23 11:35:26 -03:00
parent 4eaa806636
commit 19bfc41d95
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
3 changed files with 13 additions and 6 deletions

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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(