Merge pull request #4443 from ByteHamster/network-metered-detection

Detect network cellular instead of network metered
This commit is contained in:
H. Lehmann 2020-09-29 13:34:26 +02:00 committed by GitHub
commit bba5f224e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 100 additions and 66 deletions

View File

@ -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.
*/