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;
|
||||||
|
@ -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,23 +109,54 @@ 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isNetworkRestricted() {
|
||||||
|
return isNetworkMetered() || isNetworkCellular();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isNetworkMetered() {
|
private static boolean isNetworkMetered() {
|
||||||
ConnectivityManager connManager = (ConnectivityManager) context
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
.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.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue