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 {
|
fun canDoBackupNow(): Boolean {
|
||||||
val storage = getStorage() ?: return false
|
val storage = getStorage() ?: return false
|
||||||
val systemContext = context.getStorageContext { storage.isUsb }
|
val systemContext = context.getStorageContext { storage.isUsb }
|
||||||
return !storage.isUnavailableUsb(systemContext) && !storage.isUnavailableNetwork(context)
|
return !storage.isUnavailableUsb(systemContext) &&
|
||||||
|
!storage.isUnavailableNetwork(context, useMeteredNetwork)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun backupApks(): Boolean {
|
fun backupApks(): Boolean {
|
||||||
|
@ -208,15 +209,16 @@ data class Storage(
|
||||||
* Returns true if this is storage that requires network access,
|
* Returns true if this is storage that requires network access,
|
||||||
* but it isn't available right now.
|
* but it isn't available right now.
|
||||||
*/
|
*/
|
||||||
fun isUnavailableNetwork(context: Context): Boolean {
|
fun isUnavailableNetwork(context: Context, allowMetered: Boolean): Boolean {
|
||||||
return requiresNetwork && !hasUnmeteredInternet(context)
|
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 cm = context.getSystemService(ConnectivityManager::class.java) ?: return false
|
||||||
val isMetered = cm.isActiveNetworkMetered
|
val isMetered = cm.isActiveNetworkMetered
|
||||||
val capabilities = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false
|
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
|
// back off if storage is removable and not available right now
|
||||||
storage.isUnavailableUsb(context) -> longBackoff
|
storage.isUnavailableUsb(context) -> longBackoff
|
||||||
// back off if storage is on network, but we have no access
|
// 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
|
// otherwise no back off
|
||||||
else -> 0L
|
else -> 0L
|
||||||
}
|
}
|
||||||
|
|
|
@ -294,6 +294,7 @@ internal class BackupCoordinatorTest : BackupTest() {
|
||||||
} just Runs
|
} just Runs
|
||||||
coEvery { full.cancelFullBackup(token, metadata.salt, false) } just Runs
|
coEvery { full.cancelFullBackup(token, metadata.salt, false) } just Runs
|
||||||
every { settingsManager.getStorage() } returns storage
|
every { settingsManager.getStorage() } returns storage
|
||||||
|
every { settingsManager.useMeteredNetwork } returns false
|
||||||
every { metadataOutputStream.close() } just Runs
|
every { metadataOutputStream.close() } just Runs
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
|
@ -343,6 +344,7 @@ internal class BackupCoordinatorTest : BackupTest() {
|
||||||
} just Runs
|
} just Runs
|
||||||
coEvery { full.cancelFullBackup(token, metadata.salt, false) } just Runs
|
coEvery { full.cancelFullBackup(token, metadata.salt, false) } just Runs
|
||||||
every { settingsManager.getStorage() } returns storage
|
every { settingsManager.getStorage() } returns storage
|
||||||
|
every { settingsManager.useMeteredNetwork } returns false
|
||||||
every { metadataOutputStream.close() } just Runs
|
every { metadataOutputStream.close() } just Runs
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
|
|
Loading…
Reference in a new issue