Merge pull request #4443 from ByteHamster/network-metered-detection
Detect network cellular instead of network metered
This commit is contained in:
commit
bba5f224e6
|
@ -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;
|
||||
|
@ -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,23 +109,54 @@ 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);
|
||||
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.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue