diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 5c7ca310f..18b96b4b5 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -23,15 +23,21 @@
android:versionName="1.7.1">
+
+
+
+
+
+
+ *
+ * This file is part of Loop Habit Tracker.
+ *
+ * Loop Habit Tracker is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
+ *
+ * Loop Habit Tracker is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see .
+ */
+
+package org.isoron.uhabits.receivers;
+
+import android.content.*;
+import android.net.*;
+import android.support.annotation.*;
+
+import org.isoron.uhabits.*;
+import org.isoron.uhabits.sync.*;
+
+import static android.content.Context.*;
+
+public class ConnectivityReceiver extends BroadcastReceiver
+{
+ @Override
+ public void onReceive(@Nullable Context context, @Nullable Intent intent)
+ {
+ if (context == null) return;
+ if (intent == null) return;
+
+ AppComponent component =
+ ((HabitsApplication) context.getApplicationContext()).getComponent();
+
+ NetworkInfo networkInfo =
+ ((ConnectivityManager) context.getSystemService(
+ CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
+
+ boolean isConnected =
+ (networkInfo != null && networkInfo.isConnectedOrConnecting());
+
+ SyncManager syncManager = component.getSyncManager();
+ syncManager.onNetworkStatusChanged(isConnected);
+ }
+}
diff --git a/app/src/main/java/org/isoron/uhabits/sync/SyncManager.java b/app/src/main/java/org/isoron/uhabits/sync/SyncManager.java
index 124af8682..ebfee4be8 100644
--- a/app/src/main/java/org/isoron/uhabits/sync/SyncManager.java
+++ b/app/src/main/java/org/isoron/uhabits/sync/SyncManager.java
@@ -40,6 +40,7 @@ import io.socket.emitter.*;
import static io.socket.client.Socket.*;
+@AppScope
public class SyncManager implements CommandRunner.Listener
{
public static final String EVENT_AUTH = "auth";
@@ -80,15 +81,20 @@ public class SyncManager implements CommandRunner.Listener
@NonNull
private CommandParser commandParser;
+ private boolean isListening;
+
@Inject
public SyncManager(@AppContext @NonNull Context context,
@NonNull Preferences prefs,
@NonNull CommandRunner commandRunner,
@NonNull CommandParser commandParser)
{
+ Log.i("SyncManager", this.toString());
+
this.prefs = prefs;
this.commandRunner = commandRunner;
this.commandParser = commandParser;
+ this.isListening = false;
pendingConfirmation = new LinkedList<>();
pendingEmit = new LinkedList<>(Event.getAll());
@@ -118,19 +124,31 @@ public class SyncManager implements CommandRunner.Listener
if (readyToEmit) emitPending();
}
+ public void onNetworkStatusChanged(boolean isConnected)
+ {
+ if(!isListening) return;
+ if(isConnected) socket.connect();
+ else socket.disconnect();
+ }
+
public void startListening()
{
if (!prefs.isSyncFeatureEnabled()) return;
if (groupKey.isEmpty()) return;
+ if (isListening) return;
+ isListening = true;
socket.connect();
commandRunner.addListener(this);
}
public void stopListening()
{
+ if(!isListening) return;
+
commandRunner.removeListener(this);
socket.close();
+ isListening = false;
}
private void connect(@AppContext @NonNull Context context, String serverURL)
diff --git a/app/src/main/java/org/isoron/uhabits/sync/SyncService.java b/app/src/main/java/org/isoron/uhabits/sync/SyncService.java
index e65bcbc00..3f8d59590 100644
--- a/app/src/main/java/org/isoron/uhabits/sync/SyncService.java
+++ b/app/src/main/java/org/isoron/uhabits/sync/SyncService.java
@@ -21,11 +21,13 @@ package org.isoron.uhabits.sync;
import android.app.*;
import android.content.*;
+import android.net.*;
import android.os.*;
import android.support.v7.app.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.preferences.*;
+import org.isoron.uhabits.receivers.*;
public class SyncService extends Service implements Preferences.Listener
{
@@ -33,6 +35,8 @@ public class SyncService extends Service implements Preferences.Listener
private Preferences prefs;
+ private ConnectivityReceiver connectivityReceiver;
+
public SyncService()
{
}
@@ -59,6 +63,10 @@ public class SyncService extends Service implements Preferences.Listener
startForeground(99999, notification);
+ connectivityReceiver = new ConnectivityReceiver();
+ IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
+ this.registerReceiver(connectivityReceiver, filter);
+
HabitsApplication app = (HabitsApplication) getApplicationContext();
syncManager = app.getComponent().getSyncManager();
syncManager.startListening();
@@ -76,6 +84,7 @@ public class SyncService extends Service implements Preferences.Listener
@Override
public void onDestroy()
{
+ unregisterReceiver(connectivityReceiver);
syncManager.stopListening();
}
}