Reload on network type change when needed

pull/169/head
M66B 6 years ago
parent 037af96be0
commit 00ef6cd812

@ -75,9 +75,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 actionType action = actionType.NONE; private Integer type = null;
public enum actionType {NONE, AVAILABLE, CHANGED, LOST}
boolean isConnected() { boolean isConnected() {
return (connected != null && connected); return (connected != null && connected);
@ -95,11 +93,16 @@ public class ConnectionHelper {
return (roaming != null && roaming); return (roaming != null && roaming);
} }
Integer getType() {
return type;
}
public void update(NetworkState newState) { public void update(NetworkState newState) {
connected = newState.connected; connected = newState.connected;
unmetered = newState.unmetered; unmetered = newState.unmetered;
suitable = newState.suitable; suitable = newState.suitable;
roaming = newState.roaming; roaming = newState.roaming;
type = newState.type;
} }
@Override @Override
@ -128,10 +131,13 @@ public class ConnectionHelper {
state.unmetered = (isMetered != null && !isMetered); state.unmetered = (isMetered != null && !isMetered);
state.suitable = (isMetered != null && (metered || !isMetered)); state.suitable = (isMetered != null && (metered || !isMetered));
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) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
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 {
@ -166,12 +172,6 @@ public class ConnectionHelper {
return state; return state;
} }
static NetworkState getNetworkState(Context context, NetworkState.actionType action) {
NetworkState state = getNetworkState(context);
state.action = action;
return state;
}
private static Boolean isMetered(Context context) { private static Boolean isMetered(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) if (cm == null)

@ -61,6 +61,7 @@ import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -204,7 +205,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
}); });
Log.i("### observe");
liveAccountNetworkState.observeForever(new Observer<List<TupleAccountNetworkState>>() { liveAccountNetworkState.observeForever(new Observer<List<TupleAccountNetworkState>>() {
boolean running = true; boolean running = true;
private List<TupleAccountNetworkState> accountStates = new ArrayList<>(); private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
@ -241,7 +241,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
int index = accountStates.indexOf(current); int index = accountStates.indexOf(current);
if (index < 0) { if (index < 0) {
if (current.canRun()) { if (current.canRun()) {
Log.i("### new " + current); EntityLog.log(ServiceSynchronize.this, "### new " + current);
start(current, current.accountState.isEnabled(current.enabled)); start(current, current.accountState.isEnabled(current.enabled));
} }
} else { } else {
@ -252,17 +252,21 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (state != null) if (state != null)
state.setNetworkState(current.networkState); state.setNetworkState(current.networkState);
// TODO: reload disconnected account on new network available // Some networks disallow email server connections:
// !"connected".equals(current.accountState.state)) // - reload on network type change when disconnected
if (current.reload || if (current.reload ||
prev.canRun() != current.canRun() || prev.canRun() != current.canRun() ||
!prev.accountState.equals(current.accountState)) { !prev.accountState.equals(current.accountState) ||
Log.i("### changed " + current + (!"connected".equals(current.accountState.state) &&
" reload=" + current.reload + !Objects.equals(prev.networkState.getType(), current.networkState.getType()))) {
" run prev=" + prev.canRun() + if (prev.canRun() || current.canRun())
" run cur=" + current.canRun() + EntityLog.log(ServiceSynchronize.this, "### changed " + current +
" changed=" + !prev.accountState.equals(current.accountState)); " reload=" + current.reload +
" stop=" + prev.canRun() +
" start=" + current.canRun() +
" changed=" + !prev.accountState.equals(current.accountState) +
" state=" + current.accountState.state +
" type=" + prev.networkState.getType() + "/" + current.networkState.getType());
if (prev.canRun()) if (prev.canRun())
stop(prev); stop(prev);
if (current.canRun()) if (current.canRun())
@ -330,7 +334,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i("### start=" + accountNetworkState); Log.i("### start=" + accountNetworkState);
astate.start(); astate.start();
Log.i("### started=" + accountNetworkState); EntityLog.log(ServiceSynchronize.this, "### started=" + accountNetworkState);
} }
}); });
} }
@ -356,7 +360,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i("### stop=" + accountNetworkState); Log.i("### stop=" + accountNetworkState);
state.stop(); state.stop();
state.join(); state.join();
Log.i("### stopped=" + accountNetworkState); EntityLog.log(ServiceSynchronize.this, "### stopped=" + accountNetworkState);
} }
}); });
} }
@ -392,7 +396,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
db.folder().setFolderSyncState(op.folder, null); db.folder().setFolderSyncState(op.folder, null);
stopSelf(); stopSelf();
Log.i("### quited"); EntityLog.log(ServiceSynchronize.this, "### quited");
} }
}); });
} }
@ -582,7 +586,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
String action = (intent == null ? null : intent.getAction()); String action = (intent == null ? null : intent.getAction());
String reason = (intent == null ? null : intent.getStringExtra("reason")); String reason = (intent == null ? null : intent.getStringExtra("reason"));
Log.i("### Service command intent=" + intent + " action=" + action + " reason=" + reason); EntityLog.log(ServiceSynchronize.this, "### Service command " + intent +
" action=" + action + " reason=" + reason);
Log.logExtras(intent); Log.logExtras(intent);
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
@ -1304,7 +1309,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EntityLog.log(ServiceSynchronize.this, "Available network=" + network + EntityLog.log(ServiceSynchronize.this, "Available network=" + network +
" capabilities " + cm.getNetworkCapabilities(network)); " capabilities " + cm.getNetworkCapabilities(network));
updateState(ConnectionHelper.NetworkState.actionType.AVAILABLE); updateState();
} }
@Override @Override
@ -1312,7 +1317,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EntityLog.log(ServiceSynchronize.this, "Changed network=" + network + EntityLog.log(ServiceSynchronize.this, "Changed network=" + network +
" capabilities " + cm.getNetworkCapabilities(network)); " capabilities " + cm.getNetworkCapabilities(network));
updateState(ConnectionHelper.NetworkState.actionType.CHANGED); updateState();
} }
@Override @Override
@ -1322,11 +1327,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityLog.log(ServiceSynchronize.this, "Lost network=" + network + " active=" + active); EntityLog.log(ServiceSynchronize.this, "Lost network=" + network + " active=" + active);
if (active == null) if (active == null)
lastLost = new Date().getTime(); lastLost = new Date().getTime();
updateState(ConnectionHelper.NetworkState.actionType.LOST); updateState();
} }
private void updateState(ConnectionHelper.NetworkState.actionType action) { private void updateState() {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this, action); ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
liveNetworkState.postValue(ns); liveNetworkState.postValue(ns);
if (lastSuitable == null || lastSuitable != ns.isSuitable()) { if (lastSuitable == null || lastSuitable != ns.isSuitable()) {

Loading…
Cancel
Save