Allow backups in metered network, if user wants that
This commit is contained in:
parent
4eaa806636
commit
19bfc41d95
3 changed files with 13 additions and 6 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue