mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-02-02 03:36:48 +01:00
Don't connect to metered wifi networks
This commit is contained in:
parent
345aad4148
commit
b1c4a89f9b
@ -22,7 +22,7 @@ public class ConnectivityActionReceiver extends BroadcastReceiver {
|
|||||||
Log.d(TAG, "Received intent");
|
Log.d(TAG, "Received intent");
|
||||||
|
|
||||||
ClientConfig.initialize(context);
|
ClientConfig.initialize(context);
|
||||||
if (NetworkUtils.autodownloadNetworkAvailable()) {
|
if (NetworkUtils.isAutoDownloadAllowed()) {
|
||||||
Log.d(TAG, "auto-dl network available, starting auto-download");
|
Log.d(TAG, "auto-dl network available, starting auto-download");
|
||||||
DBTasks.autodownloadUndownloadedItems(context);
|
DBTasks.autodownloadUndownloadedItems(context);
|
||||||
} else { // if new network is Wi-Fi, finish ongoing downloads,
|
} else { // if new network is Wi-Fi, finish ongoing downloads,
|
||||||
|
@ -35,7 +35,7 @@ public class AutomaticDownloadAlgorithm {
|
|||||||
return () -> {
|
return () -> {
|
||||||
|
|
||||||
// true if we should auto download based on network status
|
// true if we should auto download based on network status
|
||||||
boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable()
|
boolean networkShouldAutoDl = NetworkUtils.isAutoDownloadAllowed()
|
||||||
&& UserPreferences.isEnableAutodownload();
|
&& UserPreferences.isEnableAutodownload();
|
||||||
|
|
||||||
// true if we should auto download based on power status
|
// true if we should auto download based on power status
|
||||||
|
@ -40,56 +40,23 @@ public class NetworkUtils {
|
|||||||
NetworkUtils.context = context;
|
NetworkUtils.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static boolean isAutoDownloadAllowed() {
|
||||||
* Returns true if the device is connected to Wi-Fi and the Wi-Fi filter for
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
* 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);
|
|
||||||
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
|
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
|
||||||
if (networkInfo != null) {
|
if (networkInfo == null) {
|
||||||
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
|
return false;
|
||||||
Log.d(TAG, "Device is connected to Wi-Fi");
|
}
|
||||||
if (networkInfo.isConnected()) {
|
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||||
if (!UserPreferences.isEnableAutodownloadWifiFilter()) {
|
if (UserPreferences.isEnableAutodownloadWifiFilter()) {
|
||||||
Log.d(TAG, "Auto-dl filter is disabled");
|
return isInAllowedWifiNetwork();
|
||||||
return true;
|
} else {
|
||||||
} else {
|
return !isNetworkMetered();
|
||||||
WifiManager wm = (WifiManager) context.getApplicationContext()
|
}
|
||||||
.getSystemService(Context.WIFI_SERVICE);
|
} else if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
|
||||||
WifiInfo wifiInfo = wm.getConnectionInfo();
|
return true;
|
||||||
List<String> selectedNetworks = Arrays
|
} else {
|
||||||
.asList(UserPreferences
|
return UserPreferences.isAllowMobileAutoDownload() || !NetworkUtils.isNetworkRestricted();
|
||||||
.getAutodownloadSelectedNetworks());
|
|
||||||
if (selectedNetworks.contains(Integer.toString(wifiInfo
|
|
||||||
.getNetworkId()))) {
|
|
||||||
Log.d(TAG, "Current network is on the selected networks list");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
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() {
|
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.
|
* Returns the SSID of the wifi connection, or <code>null</code> if there is no wifi.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user