Permalink
Browse files

Do not try to reconnect while network is disconnected

This avoids error messages and periodic reconnect attempts while we lack
a network link. Once we gain a network again, we will be called anyway,
allowing to reestablish the link.

Signed-off-by: Jan Kiszka <jan.kiszka@web.de>
  • Loading branch information...
1 parent 76f6ca0 commit 6c067c97a6dfa6c56254a71d428d35d40ade9b53 @jan-kiszka jan-kiszka committed Apr 17, 2012
Showing with 30 additions and 2 deletions.
  1. +30 −2 src/org/yaxim/androidclient/service/XMPPService.java
@@ -16,6 +16,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteCallbackList;
@@ -190,7 +192,8 @@ public void unregisterRosterCallback(IXMPPRosterCallback callback)
public int getConnectionState() throws RemoteException {
if (mSmackable != null && mSmackable.isAuthenticated()) {
return ConnectionState.AUTHENTICATED;
- } else if (mConnectionDemanded.get()) {
+ } else if (mConnectionDemanded.get() &&
+ networkConnectedOrConnecting()) {
return ConnectionState.CONNECTING;
} else {
return ConnectionState.OFFLINE;
@@ -379,12 +382,37 @@ private void broadcastConnectionStatus(boolean isConnected, boolean willReconnec
mRosterCallbacks.finishBroadcast();
}
+ private NetworkInfo getNetworkInfo() {
+ Context ctx = getApplicationContext();
+ ConnectivityManager connMgr =
+ (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
+ return connMgr.getActiveNetworkInfo();
+ }
+ private boolean networkConnected() {
+ NetworkInfo info = getNetworkInfo();
+
+ return info != null && info.isConnected();
+ }
+ private boolean networkConnectedOrConnecting() {
+ NetworkInfo info = getNetworkInfo();
+
+ return info != null && info.isConnectedOrConnecting();
+ }
+
private void connectionFailed(String reason) {
logInfo("connectionFailed: " + reason);
mLastConnectionError = reason;
mIsConnected.set(false);
broadcastConnectionStatus(false, mConnectionDemanded.get());
- if (mConnectionDemanded.get()) {
+ if (!networkConnected()) {
+ mLastConnectionError = null;
+ mReconnectInfo = "";
+ updateServiceNotification();
+ if (mSmackable != null) {
+ mSmackable.unRegisterCallback();
+ mSmackable = null;
+ }
+ } else if (mConnectionDemanded.get()) {
mReconnectInfo = getString(R.string.conn_reconnect, mReconnectTimeout);
updateServiceNotification();
logInfo("connectionFailed(): registering reconnect in " + mReconnectTimeout + "s");

0 comments on commit 6c067c9

Please sign in to comment.