Delegate network switching

pull/187/head
M66B 5 years ago
parent 3b8574b2ca
commit 80a00b157d

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

@ -1523,7 +1523,6 @@ public class Log {
size += write(os, "Suitable=" + state.isSuitable() + "\r\n"); size += write(os, "Suitable=" + state.isSuitable() + "\r\n");
size += write(os, "Unmetered=" + state.isUnmetered() + "\r\n"); size += write(os, "Unmetered=" + state.isUnmetered() + "\r\n");
size += write(os, "Roaming=" + state.isRoaming() + "\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); db.attachment().setDownloaded(attachment.id, size);

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

Loading…
Cancel
Save