Insight automatic reconnector

This commit is contained in:
Jamorham 2018-01-25 20:55:55 +00:00
parent c8ba4076c6
commit d421ceffa6
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9

View file

@ -28,10 +28,13 @@ public class Connector {
private volatile Status lastStatus = null;
private volatile long lastStatusTime = -1;
private boolean companionAppInstalled = false;
private int serviceReconnects = 0;
private StatusCallback statusCallback = new StatusCallback() {
@Override
public void onStatusChange(Status status) {
synchronized (this) {
log("Status change: " + status);
lastStatus = status;
@ -55,6 +58,15 @@ public class Connector {
@Override
public void onServiceDisconnected() {
log("Disconnected from service");
if (Helpers.ratelimit("insight-automatic-reconnect", 30)) {
log("Scheduling automatic service reconnection");
Helpers.runOnUiThreadDelayed(new Runnable() {
@Override
public void run() {
init();
}
}, 20000);
}
}
};
@ -84,12 +96,12 @@ public class Connector {
}
private static void log(String msg) {
android.util.Log.e("PUMPPUMP", msg);
android.util.Log.e("INSIGHTPUMP", msg);
}
public synchronized void init() {
log("init");
log("Connector::init()");
if (serviceConnector == null) {
companionAppInstalled = isCompanionAppInstalled();
if (companionAppInstalled) {
@ -103,8 +115,16 @@ public class Connector {
}
} else {
if (!serviceConnector.isConnectedToService()) {
log("Trying to reconnect to service");
serviceConnector.connectToService();
if (serviceReconnects > 2) {
serviceConnector = null;
init();
} else {
log("Trying to reconnect to service (" + serviceReconnects + ")");
serviceConnector.connectToService();
serviceReconnects++;
}
} else {
serviceReconnects = 0; // everything ok
}
}
}