Send connectivity backoff

pull/184/head
M66B 5 years ago
parent 060f5b027b
commit bbf074e341

@ -32,7 +32,6 @@ import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.os.Handler; import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -80,6 +79,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
private static final int PI_SEND = 1; private static final int PI_SEND = 1;
private static final int RETRY_MAX = 3; private static final int RETRY_MAX = 3;
private static final int CONNECTIVITY_DELAY = 5000; // milliseconds
@Override @Override
public void onCreate() { public void onCreate() {
@ -174,6 +174,8 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(networkCallback); cm.unregisterNetworkCallback(networkCallback);
handler.removeCallbacks(_checkConnectivity);
owner.stop(); owner.stop();
handling.clear(); handling.clear();
@ -281,52 +283,47 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
}; };
private void checkConnectivity() { private void checkConnectivity() {
if (Looper.myLooper() == Looper.getMainLooper()) handler.postDelayed(_checkConnectivity, CONNECTIVITY_DELAY);
_checkConnectivity();
else
handler.post(new Runnable() {
@Override
public void run() {
_checkConnectivity();
}
});
} }
private void _checkConnectivity() { private Runnable _checkConnectivity = new Runnable() {
Network active = ConnectionHelper.getActiveNetwork(this); @Override
boolean restart = !Objects.equals(lastActive, active); public void run() {
if (restart) { Network active = ConnectionHelper.getActiveNetwork(ServiceSend.this);
lastActive = active; boolean restart = !Objects.equals(lastActive, active);
EntityLog.log(this, "Service send active=" + active); if (restart) {
lastActive = active;
if (lastSuitable) { EntityLog.log(ServiceSend.this, "Service send active=" + active);
EntityLog.log(this, "Service send restart");
lastSuitable = false; if (lastSuitable) {
owner.stop(); EntityLog.log(ServiceSend.this, "Service send restart");
handling.clear(); lastSuitable = false;
owner.stop();
handling.clear();
}
} }
}
boolean suitable = ConnectionHelper.getNetworkState(this).isSuitable(); boolean suitable = ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable();
if (lastSuitable != suitable) { if (lastSuitable != suitable) {
lastSuitable = suitable; lastSuitable = suitable;
EntityLog.log(this, "Service send suitable=" + suitable); EntityLog.log(ServiceSend.this, "Service send suitable=" + suitable);
try { try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build()); nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
if (suitable) if (suitable)
owner.start(); owner.start();
else { else {
owner.stop(); owner.stop();
handling.clear(); handling.clear();
}
} }
} }
} };
private void processOperations(List<TupleOperationEx> ops) { private void processOperations(List<TupleOperationEx> ops) {
try { try {

Loading…
Cancel
Save