Detect network cellular instead of network metered
Cellular networks may be unmetered, or Wi-Fi networks may be metered
This commit is contained in:
parent
410ebfe98c
commit
c339e27813
|
@ -2,9 +2,12 @@ package de.danoeh.antennapod.core.util;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.Network;
|
||||||
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiInfo;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.Build;
|
||||||
import androidx.core.net.ConnectivityManagerCompat;
|
import androidx.core.net.ConnectivityManagerCompat;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
@ -29,65 +32,65 @@ import okhttp3.Response;
|
||||||
public class NetworkUtils {
|
public class NetworkUtils {
|
||||||
private NetworkUtils(){}
|
private NetworkUtils(){}
|
||||||
|
|
||||||
private static final String TAG = NetworkUtils.class.getSimpleName();
|
private static final String TAG = NetworkUtils.class.getSimpleName();
|
||||||
|
|
||||||
private static Context context;
|
private static Context context;
|
||||||
|
|
||||||
public static void init(Context context) {
|
public static void init(Context context) {
|
||||||
NetworkUtils.context = context;
|
NetworkUtils.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the device is connected to Wi-Fi and the Wi-Fi filter for
|
* 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
|
* 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
|
* network that is on the 'selected networks' list of the Wi-Fi filter for
|
||||||
* automatic downloads and false otherwise.
|
* automatic downloads and false otherwise.
|
||||||
* */
|
* */
|
||||||
public static boolean autodownloadNetworkAvailable() {
|
public static boolean autodownloadNetworkAvailable() {
|
||||||
ConnectivityManager cm = (ConnectivityManager) context
|
ConnectivityManager cm = (ConnectivityManager) context
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
|
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
|
||||||
if (networkInfo != null) {
|
if (networkInfo != null) {
|
||||||
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
|
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||||
Log.d(TAG, "Device is connected to Wi-Fi");
|
Log.d(TAG, "Device is connected to Wi-Fi");
|
||||||
if (networkInfo.isConnected()) {
|
if (networkInfo.isConnected()) {
|
||||||
if (!UserPreferences.isEnableAutodownloadWifiFilter()) {
|
if (!UserPreferences.isEnableAutodownloadWifiFilter()) {
|
||||||
Log.d(TAG, "Auto-dl filter is disabled");
|
Log.d(TAG, "Auto-dl filter is disabled");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
WifiManager wm = (WifiManager) context.getApplicationContext()
|
WifiManager wm = (WifiManager) context.getApplicationContext()
|
||||||
.getSystemService(Context.WIFI_SERVICE);
|
.getSystemService(Context.WIFI_SERVICE);
|
||||||
WifiInfo wifiInfo = wm.getConnectionInfo();
|
WifiInfo wifiInfo = wm.getConnectionInfo();
|
||||||
List<String> selectedNetworks = Arrays
|
List<String> selectedNetworks = Arrays
|
||||||
.asList(UserPreferences
|
.asList(UserPreferences
|
||||||
.getAutodownloadSelectedNetworks());
|
.getAutodownloadSelectedNetworks());
|
||||||
if (selectedNetworks.contains(Integer.toString(wifiInfo
|
if (selectedNetworks.contains(Integer.toString(wifiInfo
|
||||||
.getNetworkId()))) {
|
.getNetworkId()))) {
|
||||||
Log.d(TAG, "Current network is on the selected networks list");
|
Log.d(TAG, "Current network is on the selected networks list");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
|
} else if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
|
||||||
Log.d(TAG, "Device is connected to Ethernet");
|
Log.d(TAG, "Device is connected to Ethernet");
|
||||||
if (networkInfo.isConnected()) {
|
if (networkInfo.isConnected()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!UserPreferences.isAllowMobileAutoDownload()) {
|
if (!UserPreferences.isAllowMobileAutoDownload()) {
|
||||||
Log.d(TAG, "Auto Download not enabled on Mobile");
|
Log.d(TAG, "Auto Download not enabled on Mobile");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (networkInfo.isRoaming()) {
|
if (networkInfo.isRoaming()) {
|
||||||
Log.d(TAG, "Roaming on foreign network");
|
Log.d(TAG, "Roaming on foreign network");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Network for auto-dl is not available");
|
Log.d(TAG, "Network for auto-dl is not available");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean networkAvailable() {
|
public static boolean networkAvailable() {
|
||||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
@ -96,7 +99,7 @@ public class NetworkUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isEpisodeDownloadAllowed() {
|
public static boolean isEpisodeDownloadAllowed() {
|
||||||
return UserPreferences.isAllowMobileEpisodeDownload() || !NetworkUtils.isNetworkMetered();
|
return UserPreferences.isAllowMobileEpisodeDownload() || !NetworkUtils.isNetworkRestricted();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isEpisodeHeadDownloadAllowed() {
|
public static boolean isEpisodeHeadDownloadAllowed() {
|
||||||
|
@ -106,22 +109,53 @@ public class NetworkUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isImageAllowed() {
|
public static boolean isImageAllowed() {
|
||||||
return UserPreferences.isAllowMobileImages() || !NetworkUtils.isNetworkMetered();
|
return UserPreferences.isAllowMobileImages() || !NetworkUtils.isNetworkRestricted();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isStreamingAllowed() {
|
public static boolean isStreamingAllowed() {
|
||||||
return UserPreferences.isAllowMobileStreaming() || !NetworkUtils.isNetworkMetered();
|
return UserPreferences.isAllowMobileStreaming() || !NetworkUtils.isNetworkRestricted();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isFeedRefreshAllowed() {
|
public static boolean isFeedRefreshAllowed() {
|
||||||
return UserPreferences.isAllowMobileFeedRefresh() || !NetworkUtils.isNetworkMetered();
|
return UserPreferences.isAllowMobileFeedRefresh() || !NetworkUtils.isNetworkRestricted();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isNetworkMetered() {
|
public static boolean isNetworkRestricted() {
|
||||||
ConnectivityManager connManager = (ConnectivityManager) context
|
return isNetworkMetered() || isNetworkCellular();
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
}
|
||||||
|
|
||||||
|
private static boolean isNetworkMetered() {
|
||||||
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
return ConnectivityManagerCompat.isActiveNetworkMetered(connManager);
|
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.
|
* 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
|
public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
|
||||||
return Single.create((SingleOnSubscribe<Long>) emitter -> {
|
return Single.create((SingleOnSubscribe<Long>) emitter -> {
|
||||||
if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) {
|
if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) {
|
||||||
emitter.onSuccess(0L);
|
emitter.onSuccess(0L);
|
||||||
|
@ -188,7 +222,7 @@ public class NetworkUtils {
|
||||||
DBWriter.setFeedMedia(media);
|
DBWriter.setFeedMedia(media);
|
||||||
})
|
})
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread());
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue