mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Detect network changes
This commit is contained in:
@@ -23,15 +23,21 @@
|
|||||||
android:versionName="1.7.1">
|
android:versionName="1.7.1">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||||
|
|
||||||
<uses-permission
|
<uses-permission
|
||||||
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="18"/>
|
android:maxSdkVersion="18"/>
|
||||||
|
|
||||||
<uses-permission
|
<uses-permission
|
||||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="18"/>
|
android:maxSdkVersion="18"/>
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".HabitsApplication"
|
android:name=".HabitsApplication"
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,6 +40,7 @@ import io.socket.emitter.*;
|
|||||||
|
|
||||||
import static io.socket.client.Socket.*;
|
import static io.socket.client.Socket.*;
|
||||||
|
|
||||||
|
@AppScope
|
||||||
public class SyncManager implements CommandRunner.Listener
|
public class SyncManager implements CommandRunner.Listener
|
||||||
{
|
{
|
||||||
public static final String EVENT_AUTH = "auth";
|
public static final String EVENT_AUTH = "auth";
|
||||||
@@ -80,15 +81,20 @@ public class SyncManager implements CommandRunner.Listener
|
|||||||
@NonNull
|
@NonNull
|
||||||
private CommandParser commandParser;
|
private CommandParser commandParser;
|
||||||
|
|
||||||
|
private boolean isListening;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public SyncManager(@AppContext @NonNull Context context,
|
public SyncManager(@AppContext @NonNull Context context,
|
||||||
@NonNull Preferences prefs,
|
@NonNull Preferences prefs,
|
||||||
@NonNull CommandRunner commandRunner,
|
@NonNull CommandRunner commandRunner,
|
||||||
@NonNull CommandParser commandParser)
|
@NonNull CommandParser commandParser)
|
||||||
{
|
{
|
||||||
|
Log.i("SyncManager", this.toString());
|
||||||
|
|
||||||
this.prefs = prefs;
|
this.prefs = prefs;
|
||||||
this.commandRunner = commandRunner;
|
this.commandRunner = commandRunner;
|
||||||
this.commandParser = commandParser;
|
this.commandParser = commandParser;
|
||||||
|
this.isListening = false;
|
||||||
|
|
||||||
pendingConfirmation = new LinkedList<>();
|
pendingConfirmation = new LinkedList<>();
|
||||||
pendingEmit = new LinkedList<>(Event.getAll());
|
pendingEmit = new LinkedList<>(Event.getAll());
|
||||||
@@ -118,19 +124,31 @@ public class SyncManager implements CommandRunner.Listener
|
|||||||
if (readyToEmit) emitPending();
|
if (readyToEmit) emitPending();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onNetworkStatusChanged(boolean isConnected)
|
||||||
|
{
|
||||||
|
if(!isListening) return;
|
||||||
|
if(isConnected) socket.connect();
|
||||||
|
else socket.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
public void startListening()
|
public void startListening()
|
||||||
{
|
{
|
||||||
if (!prefs.isSyncFeatureEnabled()) return;
|
if (!prefs.isSyncFeatureEnabled()) return;
|
||||||
if (groupKey.isEmpty()) return;
|
if (groupKey.isEmpty()) return;
|
||||||
|
if (isListening) return;
|
||||||
|
|
||||||
|
isListening = true;
|
||||||
socket.connect();
|
socket.connect();
|
||||||
commandRunner.addListener(this);
|
commandRunner.addListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopListening()
|
public void stopListening()
|
||||||
{
|
{
|
||||||
|
if(!isListening) return;
|
||||||
|
|
||||||
commandRunner.removeListener(this);
|
commandRunner.removeListener(this);
|
||||||
socket.close();
|
socket.close();
|
||||||
|
isListening = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connect(@AppContext @NonNull Context context, String serverURL)
|
private void connect(@AppContext @NonNull Context context, String serverURL)
|
||||||
|
|||||||
@@ -21,11 +21,13 @@ package org.isoron.uhabits.sync;
|
|||||||
|
|
||||||
import android.app.*;
|
import android.app.*;
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
|
import android.net.*;
|
||||||
import android.os.*;
|
import android.os.*;
|
||||||
import android.support.v7.app.*;
|
import android.support.v7.app.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.preferences.*;
|
import org.isoron.uhabits.preferences.*;
|
||||||
|
import org.isoron.uhabits.receivers.*;
|
||||||
|
|
||||||
public class SyncService extends Service implements Preferences.Listener
|
public class SyncService extends Service implements Preferences.Listener
|
||||||
{
|
{
|
||||||
@@ -33,6 +35,8 @@ public class SyncService extends Service implements Preferences.Listener
|
|||||||
|
|
||||||
private Preferences prefs;
|
private Preferences prefs;
|
||||||
|
|
||||||
|
private ConnectivityReceiver connectivityReceiver;
|
||||||
|
|
||||||
public SyncService()
|
public SyncService()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -59,6 +63,10 @@ public class SyncService extends Service implements Preferences.Listener
|
|||||||
|
|
||||||
startForeground(99999, notification);
|
startForeground(99999, notification);
|
||||||
|
|
||||||
|
connectivityReceiver = new ConnectivityReceiver();
|
||||||
|
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||||
|
this.registerReceiver(connectivityReceiver, filter);
|
||||||
|
|
||||||
HabitsApplication app = (HabitsApplication) getApplicationContext();
|
HabitsApplication app = (HabitsApplication) getApplicationContext();
|
||||||
syncManager = app.getComponent().getSyncManager();
|
syncManager = app.getComponent().getSyncManager();
|
||||||
syncManager.startListening();
|
syncManager.startListening();
|
||||||
@@ -76,6 +84,7 @@ public class SyncService extends Service implements Preferences.Listener
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy()
|
public void onDestroy()
|
||||||
{
|
{
|
||||||
|
unregisterReceiver(connectivityReceiver);
|
||||||
syncManager.stopListening();
|
syncManager.stopListening();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user