1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-24 16:53:37 +00:00

Delegate network switching

This commit is contained in:
M66B 2020-10-27 20:49:20 +01:00
parent 3b8574b2ca
commit 80a00b157d
3 changed files with 48 additions and 38 deletions

View file

@ -88,7 +88,7 @@ public class ConnectionHelper {
private Boolean suitable = null;
private Boolean unmetered = null;
private Boolean roaming = null;
private Integer type = null;
private Network active;
boolean isConnected() {
return (connected != null && connected);
@ -106,16 +106,16 @@ public class ConnectionHelper {
return (roaming != null && roaming);
}
Integer getType() {
return type;
Network getActive() {
return active;
}
public void update(NetworkState newState) {
connected = newState.connected;
unmetered = newState.unmetered;
suitable = newState.suitable;
unmetered = newState.unmetered;
roaming = newState.roaming;
type = newState.type;
active = newState.active;
}
@Override
@ -143,14 +143,13 @@ public class ConnectionHelper {
state.connected = (isMetered != null);
state.unmetered = (isMetered != null && !isMetered);
state.suitable = (isMetered != null && (metered || !isMetered));
state.active = getActiveNetwork(context);
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
if (ani != null)
state.type = ani.getType();
if (state.connected && !roaming) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
if (ani != null)
state.roaming = ani.isRoaming();
} else {

View file

@ -1523,7 +1523,6 @@ public class Log {
size += write(os, "Suitable=" + state.isSuitable() + "\r\n");
size += write(os, "Unmetered=" + state.isUnmetered() + "\r\n");
size += write(os, "Roaming=" + state.isRoaming() + "\r\n");
size += write(os, "Type=" + state.getType() + "\r\n\r\n");
}
db.attachment().setDownloaded(attachment.id, size);

View file

@ -246,7 +246,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" ops=" + current.accountState.operations +
" tbd=" + current.accountState.tbd +
" state=" + current.accountState.state +
" type=" + current.networkState.getType());
" active=" + current.networkState.getActive());
event = true;
start(current, current.accountState.isEnabled(current.enabled), false);
}
@ -271,9 +271,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
// - reload on network type change when disconnected
if (reload ||
prev.canRun() != current.canRun() ||
!prev.accountState.equals(current.accountState) ||
(!"connected".equals(current.accountState.state) &&
!Objects.equals(prev.networkState.getType(), current.networkState.getType()))) {
!prev.accountState.equals(current.accountState)) {
if (prev.canRun() || current.canRun())
EntityLog.log(ServiceSynchronize.this, "### changed " + current +
" reload=" + reload +
@ -290,7 +288,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" ops=" + current.accountState.operations +
" tbd=" + current.accountState.tbd +
" state=" + current.accountState.state +
" type=" + prev.networkState.getType() + "/" + current.networkState.getType());
" active=" + prev.networkState.getActive() + "/" + current.networkState.getActive());
if (prev.canRun()) {
event = true;
stop(prev);
@ -299,6 +297,15 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
event = true;
start(current, current.accountState.isEnabled(current.enabled) || sync, force);
}
} else {
if (state != null) {
Network p = prev.networkState.getActive();
if (p != null && !p.equals(current.networkState.getActive())) {
EntityLog.log(ServiceSynchronize.this, "### changed " + current +
" active=" + prev.networkState.getActive() + "/" + current.networkState.getActive());
state.error(new OperationCanceledException("Active network changed"));
}
}
}
}
@ -1948,11 +1955,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
*/
if (Objects.equals(lastActive, network)) {
EntityLog.log(ServiceSynchronize.this, "Lost active network=" + network);
lastLost = new Date().getTime();
}
updateNetworkState(network, null);
}
};
@ -1974,32 +1976,42 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
};
private void updateNetworkState(Network network, NetworkCapabilities capabilities) {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
liveNetworkState.postValue(ns);
Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this);
if (lastSuitable == null || lastSuitable != ns.isSuitable()) {
lastSuitable = ns.isSuitable();
EntityLog.log(ServiceSynchronize.this,
"Updated network=" + network +
" capabilities " + capabilities +
" suitable=" + lastSuitable);
if (Objects.equals(network, active)) {
if (BuildConfig.DEBUG)
EntityLog.log(ServiceSynchronize.this, "Updating active network state");
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
liveNetworkState.postValue(ns);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
boolean background_service = prefs.getBoolean("background_service", false);
if (!background_service)
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
} catch (Throwable ex) {
Log.w(ex);
}
if (lastSuitable == null || lastSuitable != ns.isSuitable()) {
lastSuitable = ns.isSuitable();
EntityLog.log(ServiceSynchronize.this,
"Updated network=" + network +
" capabilities " + capabilities +
" suitable=" + lastSuitable);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
boolean background_service = prefs.getBoolean("background_service", false);
if (!background_service)
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
} catch (Throwable ex) {
Log.w(ex);
}
}
}
Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this);
if (!Objects.equals(lastActive, active)) {
if (lastActive != null)
lastLost = new Date().getTime();
lastActive = active;
EntityLog.log(ServiceSynchronize.this, "New active network=" + active);
reload(ServiceSynchronize.this, -1L, false, "Network changed active=" + active);
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
liveNetworkState.postValue(ns);
}
}