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 Status lastStatus = null;
private volatile long lastStatusTime = -1; private volatile long lastStatusTime = -1;
private boolean companionAppInstalled = false; private boolean companionAppInstalled = false;
private int serviceReconnects = 0;
private StatusCallback statusCallback = new StatusCallback() { private StatusCallback statusCallback = new StatusCallback() {
@Override @Override
public void onStatusChange(Status status) { public void onStatusChange(Status status) {
synchronized (this) { synchronized (this) {
log("Status change: " + status); log("Status change: " + status);
lastStatus = status; lastStatus = status;
@ -55,6 +58,15 @@ public class Connector {
@Override @Override
public void onServiceDisconnected() { public void onServiceDisconnected() {
log("Disconnected from service"); 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) { private static void log(String msg) {
android.util.Log.e("PUMPPUMP", msg); android.util.Log.e("INSIGHTPUMP", msg);
} }
public synchronized void init() { public synchronized void init() {
log("init"); log("Connector::init()");
if (serviceConnector == null) { if (serviceConnector == null) {
companionAppInstalled = isCompanionAppInstalled(); companionAppInstalled = isCompanionAppInstalled();
if (companionAppInstalled) { if (companionAppInstalled) {
@ -103,8 +115,16 @@ public class Connector {
} }
} else { } else {
if (!serviceConnector.isConnectedToService()) { if (!serviceConnector.isConnectedToService()) {
log("Trying to reconnect to service"); if (serviceReconnects > 2) {
serviceConnector.connectToService(); serviceConnector = null;
init();
} else {
log("Trying to reconnect to service (" + serviceReconnects + ")");
serviceConnector.connectToService();
serviceReconnects++;
}
} else {
serviceReconnects = 0; // everything ok
} }
} }
} }