Fixed suitable network check

pull/146/head
M66B 6 years ago
parent 75d4dc2a9d
commit 3dd522f6b8

@ -36,6 +36,7 @@ import android.media.RingtoneManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -2366,13 +2367,16 @@ public class ServiceSynchronize extends LifecycleService {
private boolean suitableNetwork() { private boolean suitableNetwork() {
ConnectivityManager cm = getSystemService(ConnectivityManager.class); ConnectivityManager cm = getSystemService(ConnectivityManager.class);
boolean unmetered = !cm.isActiveNetworkMetered(); Network active = cm.getActiveNetwork();
NetworkCapabilities caps = (active == null ? null : cm.getNetworkCapabilities(active));
NetworkInfo ni = (active == null ? null : cm.getNetworkInfo(active));
boolean unmetered = (caps != null && caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
boolean metered = prefs.getBoolean("metered", true); boolean metered = prefs.getBoolean("metered", true);
boolean suitable = (metered || unmetered); boolean suitable = (active != null && (metered || unmetered));
EntityLog.log(ServiceSynchronize.this, "suitable=" + suitable + " active=" + cm.getActiveNetworkInfo()); EntityLog.log(ServiceSynchronize.this, "suitable=" + suitable + " active=" + ni);
// The connected state is deliberately ignored // The connected state is deliberately ignored
return suitable; return suitable;

Loading…
Cancel
Save