1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-29 11:15:51 +00:00

Prevent crash

This commit is contained in:
M66B 2018-12-02 14:27:47 +01:00
parent 251ff98ea5
commit 78928f3e3d

View file

@ -2278,50 +2278,57 @@ public class ServiceSynchronize extends LifecycleService {
@Override @Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities capabilities) { public void onCapabilitiesChanged(Network network, NetworkCapabilities capabilities) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this); try {
boolean metered = prefs.getBoolean("metered", true); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
boolean metered = prefs.getBoolean("metered", true);
boolean unmetered = (capabilities != null &&
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED));
ConnectivityManager cm = getSystemService(ConnectivityManager.class); if (!started && (metered || unmetered))
NetworkCapabilities nc = cm.getNetworkCapabilities(network); EntityLog.log(ServiceSynchronize.this, "Network " + network + " capabilities " + capabilities);
boolean unmetered = nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
if (!started && (metered || unmetered)) if (!started && suitableNetwork())
EntityLog.log(ServiceSynchronize.this, "Network " + network + " capabilities " + capabilities); queue_reload(true, "connect " + network);
} catch (Throwable ex) {
if (!started && suitableNetwork()) Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
queue_reload(true, "connect " + network); }
} }
@Override @Override
public void onAvailable(Network network) { public void onAvailable(Network network) {
ConnectivityManager cm = getSystemService(ConnectivityManager.class); try {
EntityLog.log(ServiceSynchronize.this, "Available " + network + " " + cm.getNetworkInfo(network)); ConnectivityManager cm = getSystemService(ConnectivityManager.class);
EntityLog.log(ServiceSynchronize.this, "Available " + network + " " + cm.getNetworkInfo(network));
if (!started && suitableNetwork()) if (!started && suitableNetwork())
queue_reload(true, "connect " + network); queue_reload(true, "connect " + network);
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
} }
@Override @Override
public void onLost(Network network) { public void onLost(Network network) {
EntityLog.log(ServiceSynchronize.this, "Lost " + network); try {
EntityLog.log(ServiceSynchronize.this, "Lost " + network);
if (started && !suitableNetwork()) { if (started && !suitableNetwork()) {
lastLost = new Date().getTime(); lastLost = new Date().getTime();
queue_reload(false, "disconnect " + network); queue_reload(false, "disconnect " + network);
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} }
} }
private boolean suitableNetwork() { private boolean suitableNetwork() {
ConnectivityManager cm = getSystemService(ConnectivityManager.class); ConnectivityManager cm = getSystemService(ConnectivityManager.class);
Network network = cm.getActiveNetwork(); boolean unmetered = !cm.isActiveNetworkMetered();
NetworkCapabilities nc = (network == null ? null : cm.getNetworkCapabilities(network));
boolean unmetered = (!cm.isActiveNetworkMetered() ||
(nc != null && nc.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 = (network != null && (metered || unmetered)); boolean suitable = (metered || unmetered);
EntityLog.log(ServiceSynchronize.this, "suitable=" + suitable + " active=" + cm.getActiveNetworkInfo()); EntityLog.log(ServiceSynchronize.this, "suitable=" + suitable + " active=" + cm.getActiveNetworkInfo());
// The connected state is deliberately ignored // The connected state is deliberately ignored