some code cleanup. and avoid scheduling reconnect task for negative interval

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 59 
1 file changed, 31 insertions(+), 28 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -358,32 +358,29 @@ public class XmppConnectionService extends Service {
 				}
 				account.pendingConferenceJoins.clear();
 				scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode());
-			} else {
-				if (account.getStatus() == Account.State.OFFLINE || account.getStatus() == Account.State.DISABLED) {
-					resetSendingToWaiting(account);
-					if (!account.isOptionSet(Account.OPTION_DISABLED)) {
-						synchronized (mLowPingTimeoutMode) {
-							if (mLowPingTimeoutMode.contains(account.getJid().toBareJid())) {
-								Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": went into offline state during low ping mode. reconnecting now");
-								reconnectAccount(account, true, false);
-							} else {
-								int timeToReconnect = mRandom.nextInt(10) + 2;
-								scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
-							}
-						}
-					}
-				} else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
-					databaseBackend.updateAccount(account);
+			} else if (account.getStatus() == Account.State.OFFLINE || account.getStatus() == Account.State.DISABLED) {
+				resetSendingToWaiting(account);
+				if (!account.isOptionSet(Account.OPTION_DISABLED) && isInLowPingTimeoutMode(account)) {
+					Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": went into offline state during low ping mode. reconnecting now");
 					reconnectAccount(account, true, false);
-				} else if ((account.getStatus() != Account.State.CONNECTING)
-						&& (account.getStatus() != Account.State.NO_INTERNET)) {
-					resetSendingToWaiting(account);
-					if (connection != null) {
-						int next = connection.getTimeToNextAttempt();
-						Log.d(Config.LOGTAG, account.getJid().toBareJid()
-								+ ": error connecting account. try again in "
-								+ next + "s for the "
-								+ (connection.getAttempt() + 1) + " time");
+				} else {
+					int timeToReconnect = mRandom.nextInt(10) + 2;
+					scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
+				}
+			} else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
+				databaseBackend.updateAccount(account);
+				reconnectAccount(account, true, false);
+			} else if (account.getStatus() != Account.State.CONNECTING && account.getStatus() != Account.State.NO_INTERNET) {
+				resetSendingToWaiting(account);
+				if (connection != null) {
+					final int next = connection.getTimeToNextAttempt();
+					final boolean lowPingTimeoutMode = isInLowPingTimeoutMode(account);
+					if (next <= 0) {
+						Log.d(Config.LOGTAG, account.getJid().toBareJid()+": error connecting account. reconnecting now. lowPingTimeout="+Boolean.toString(lowPingTimeoutMode));
+						reconnectAccount(account, true, false);
+					} else {
+						final int attempt = connection.getAttempt() + 1;
+						Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": error connecting account. try again in " + next + "s for the " + attempt+ " time. lowPingTimeout="+Boolean.toString(lowPingTimeoutMode));
 						scheduleWakeUpCall(next, account.getUuid().hashCode());
 					}
 				}
@@ -391,6 +388,13 @@ public class XmppConnectionService extends Service {
 			getNotificationService().updateErrorNotification();
 		}
 	};
+
+	private boolean isInLowPingTimeoutMode(Account account) {
+		synchronized (mLowPingTimeoutMode) {
+			return mLowPingTimeoutMode.contains(account.getJid().toBareJid());
+		}
+	}
+
 	private OpenPgpServiceConnection pgpServiceConnection;
 	private PgpEngine mPgpEngine = null;
 	private WakeLock wakeLock;
@@ -529,8 +533,7 @@ public class XmppConnectionService extends Service {
 						if (!progressTracker.contains(p) && p != 100 && p != 0) {
 							progressTracker.add(p);
 							if (informableCallback != null) {
-
-								informableCallback.inform(getString(R.string.transcoding_video_progress, p));
+								informableCallback.inform(getString(R.string.transcoding_video_progress, String.valueOf(p)));
 							}
 						}
 					}
@@ -722,7 +725,7 @@ public class XmppConnectionService extends Service {
 			}
 			if (pingNow) {
 				for (Account account : pingCandidates) {
-					final boolean lowTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid());
+					final boolean lowTimeout = isInLowPingTimeoutMode(account);
 					account.getXmppConnection().sendPing();
 					Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action=" + action + ",lowTimeout=" + Boolean.toString(lowTimeout) + ")");
 					scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());