Merge pull request #5505 from ByteHamster/metered-network

Don't use metered networks for auto download
This commit is contained in:
ByteHamster 2021-10-30 20:46:09 +02:00 committed by GitHub
commit 9541d9459f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 50 deletions

View File

@ -22,7 +22,7 @@ public class ConnectivityActionReceiver extends BroadcastReceiver {
Log.d(TAG, "Received intent");
ClientConfig.initialize(context);
if (NetworkUtils.autodownloadNetworkAvailable()) {
if (NetworkUtils.isAutoDownloadAllowed()) {
Log.d(TAG, "auto-dl network available, starting auto-download");
DBTasks.autodownloadUndownloadedItems(context);
} else { // if new network is Wi-Fi, finish ongoing downloads,

View File

@ -35,7 +35,7 @@ public class AutomaticDownloadAlgorithm {
return () -> {
// true if we should auto download based on network status
boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable()
boolean networkShouldAutoDl = NetworkUtils.isAutoDownloadAllowed()
&& UserPreferences.isEnableAutodownload();
// true if we should auto download based on power status

View File

@ -40,56 +40,23 @@ public class NetworkUtils {
NetworkUtils.context = context;
}
/**
* Returns true if the device is connected to Wi-Fi and the Wi-Fi filter for
* automatic downloads is disabled or the device is connected to a Wi-Fi
* network that is on the 'selected networks' list of the Wi-Fi filter for
* automatic downloads and false otherwise.
* */
public static boolean autodownloadNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
public static boolean isAutoDownloadAllowed() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null) {
if (networkInfo == null) {
return false;
}
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
Log.d(TAG, "Device is connected to Wi-Fi");
if (networkInfo.isConnected()) {
if (!UserPreferences.isEnableAutodownloadWifiFilter()) {
Log.d(TAG, "Auto-dl filter is disabled");
return true;
if (UserPreferences.isEnableAutodownloadWifiFilter()) {
return isInAllowedWifiNetwork();
} else {
WifiManager wm = (WifiManager) context.getApplicationContext()
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wm.getConnectionInfo();
List<String> selectedNetworks = Arrays
.asList(UserPreferences
.getAutodownloadSelectedNetworks());
if (selectedNetworks.contains(Integer.toString(wifiInfo
.getNetworkId()))) {
Log.d(TAG, "Current network is on the selected networks list");
return true;
}
}
return !isNetworkMetered();
}
} else if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
Log.d(TAG, "Device is connected to Ethernet");
if (networkInfo.isConnected()) {
return true;
}
} else {
if (!UserPreferences.isAllowMobileAutoDownload()) {
Log.d(TAG, "Auto Download not enabled on Mobile");
return false;
return UserPreferences.isAllowMobileAutoDownload() || !NetworkUtils.isNetworkRestricted();
}
if (networkInfo.isRoaming()) {
Log.d(TAG, "Roaming on foreign network");
return false;
}
return true;
}
}
Log.d(TAG, "Network for auto-dl is not available");
return false;
}
public static boolean networkAvailable() {
@ -157,6 +124,12 @@ public class NetworkUtils {
}
}
private static boolean isInAllowedWifiNetwork() {
WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<String> selectedNetworks = Arrays.asList(UserPreferences.getAutodownloadSelectedNetworks());
return selectedNetworks.contains(Integer.toString(wm.getConnectionInfo().getNetworkId()));
}
/**
* Returns the SSID of the wifi connection, or <code>null</code> if there is no wifi.
*/