From 1ab391931dd9b9d83e5f420f6fc94628e920d2f4 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 5 Aug 2017 19:13:29 +0200 Subject: [PATCH] some code cleanup. and avoid scheduling reconnect task for negative interval --- .../services/XmppConnectionService.java | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 981775922..66b3678c3 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/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());