summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java172
1 files changed, 103 insertions, 69 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java
index a9e46e42c..8cca2f28f 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java
@@ -2,9 +2,12 @@ package de.danoeh.antennapod.core.util;
import android.content.Context;
import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
+import android.os.Build;
import androidx.core.net.ConnectivityManagerCompat;
import android.text.TextUtils;
import android.util.Log;
@@ -29,65 +32,65 @@ import okhttp3.Response;
public class NetworkUtils {
private NetworkUtils(){}
- private static final String TAG = NetworkUtils.class.getSimpleName();
-
- private static Context context;
-
- public static void init(Context context) {
- 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);
- NetworkInfo networkInfo = cm.getActiveNetworkInfo();
- if (networkInfo != null) {
- 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;
- } 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;
- }
- }
- }
- } 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;
- }
+ private static final String TAG = NetworkUtils.class.getSimpleName();
+
+ private static Context context;
+
+ public static void init(Context context) {
+ 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);
+ NetworkInfo networkInfo = cm.getActiveNetworkInfo();
+ if (networkInfo != null) {
+ 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;
+ } 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;
+ }
+ }
+ }
+ } 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() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -96,7 +99,7 @@ public class NetworkUtils {
}
public static boolean isEpisodeDownloadAllowed() {
- return UserPreferences.isAllowMobileEpisodeDownload() || !NetworkUtils.isNetworkMetered();
+ return UserPreferences.isAllowMobileEpisodeDownload() || !NetworkUtils.isNetworkRestricted();
}
public static boolean isEpisodeHeadDownloadAllowed() {
@@ -106,22 +109,53 @@ public class NetworkUtils {
}
public static boolean isImageAllowed() {
- return UserPreferences.isAllowMobileImages() || !NetworkUtils.isNetworkMetered();
+ return UserPreferences.isAllowMobileImages() || !NetworkUtils.isNetworkRestricted();
}
public static boolean isStreamingAllowed() {
- return UserPreferences.isAllowMobileStreaming() || !NetworkUtils.isNetworkMetered();
+ return UserPreferences.isAllowMobileStreaming() || !NetworkUtils.isNetworkRestricted();
}
public static boolean isFeedRefreshAllowed() {
- return UserPreferences.isAllowMobileFeedRefresh() || !NetworkUtils.isNetworkMetered();
+ return UserPreferences.isAllowMobileFeedRefresh() || !NetworkUtils.isNetworkRestricted();
+ }
+
+ public static boolean isNetworkRestricted() {
+ return isNetworkMetered() || isNetworkCellular();
}
- private static boolean isNetworkMetered() {
- ConnectivityManager connManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
+ private static boolean isNetworkMetered() {
+ ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return ConnectivityManagerCompat.isActiveNetworkMetered(connManager);
- }
+ }
+
+ private static boolean isNetworkCellular() {
+ ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+ if (Build.VERSION.SDK_INT >= 23) {
+ Network network = connManager.getActiveNetwork();
+ if (network == null) {
+ return false; // Nothing connected
+ }
+ NetworkInfo info = connManager.getNetworkInfo(network);
+ if (info == null) {
+ return true; // Better be safe than sorry
+ }
+ NetworkCapabilities capabilities = connManager.getNetworkCapabilities(network);
+ if (capabilities == null) {
+ return true; // Better be safe than sorry
+ }
+ return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
+ } else {
+ // if the default network is a VPN,
+ // this method will return the NetworkInfo for one of its underlying networks
+ NetworkInfo info = connManager.getActiveNetworkInfo();
+ if (info == null) {
+ return false; // Nothing connected
+ }
+ //noinspection deprecation
+ return info.getType() == ConnectivityManager.TYPE_MOBILE;
+ }
+ }
/**
* Returns the SSID of the wifi connection, or <code>null</code> if there is no wifi.
@@ -135,7 +169,7 @@ public class NetworkUtils {
return null;
}
- public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
+ public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
return Single.create((SingleOnSubscribe<Long>) emitter -> {
if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) {
emitter.onSuccess(0L);
@@ -188,7 +222,7 @@ public class NetworkUtils {
DBWriter.setFeedMedia(media);
})
.subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread());
+ .observeOn(AndroidSchedulers.mainThread());
}
}