code clean up in processAccountState()

This commit is contained in:
Daniel Gultsch 2023-10-19 08:57:28 +02:00
parent 928db01ae8
commit a162d72c2a
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -906,79 +906,75 @@ public class XmppConnectionService extends Service {
} }
private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) { private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) {
boolean pingNow = false; if (!account.getStatus().isAttemptReconnect()) {
if (account.getStatus().isAttemptReconnect()) { return false;
if (!hasInternetConnection()) { }
account.setStatus(Account.State.NO_INTERNET); if (!hasInternetConnection()) {
if (statusListener != null) { account.setStatus(Account.State.NO_INTERNET);
statusListener.onStatusChanged(account); statusListener.onStatusChanged(account);
} } else {
} else { if (account.getStatus() == Account.State.NO_INTERNET) {
if (account.getStatus() == Account.State.NO_INTERNET) { account.setStatus(Account.State.OFFLINE);
account.setStatus(Account.State.OFFLINE); statusListener.onStatusChanged(account);
if (statusListener != null) { }
statusListener.onStatusChanged(account); if (account.getStatus() == Account.State.ONLINE) {
} synchronized (mLowPingTimeoutMode) {
} long lastReceived = account.getXmppConnection().getLastPacketReceived();
if (account.getStatus() == Account.State.ONLINE) { long lastSent = account.getXmppConnection().getLastPingSent();
synchronized (mLowPingTimeoutMode) { long pingInterval = isUiAction ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
long lastReceived = account.getXmppConnection().getLastPacketReceived(); long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
long lastSent = account.getXmppConnection().getLastPingSent(); int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().asBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000;
long pingInterval = isUiAction ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000; long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime();
long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime(); if (lastSent > lastReceived) {
int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().asBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000; if (pingTimeoutIn < 0) {
long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime(); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ping timeout");
if (lastSent > lastReceived) { this.reconnectAccount(account, true, interactive);
if (pingTimeoutIn < 0) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ping timeout");
this.reconnectAccount(account, true, interactive);
} else {
int secs = (int) (pingTimeoutIn / 1000);
this.scheduleWakeUpCall(secs, account.getUuid().hashCode());
}
} else { } else {
pingCandidates.add(account); int secs = (int) (pingTimeoutIn / 1000);
if (isAccountPushed) { this.scheduleWakeUpCall(secs, account.getUuid().hashCode());
pingNow = true; }
if (mLowPingTimeoutMode.add(account.getJid().asBareJid())) { } else {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": entering low ping timeout mode"); pingCandidates.add(account);
} if (isAccountPushed) {
} else if (msToNextPing <= 0) { if (mLowPingTimeoutMode.add(account.getJid().asBareJid())) {
pingNow = true; Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": entering low ping timeout mode");
} else { }
this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode()); return true;
if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) { } else if (msToNextPing <= 0) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": leaving low ping timeout mode"); return true;
} } else {
this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode());
if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": leaving low ping timeout mode");
} }
} }
} }
} else if (account.getStatus() == Account.State.OFFLINE) { }
} else if (account.getStatus() == Account.State.OFFLINE) {
reconnectAccount(account, true, interactive);
} else if (account.getStatus() == Account.State.CONNECTING) {
long secondsSinceLastConnect = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastConnect()) / 1000;
long secondsSinceLastDisco = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastDiscoStarted()) / 1000;
long discoTimeout = Config.CONNECT_DISCO_TIMEOUT - secondsSinceLastDisco;
long timeout = Config.CONNECT_TIMEOUT - secondsSinceLastConnect;
if (timeout < 0) {
Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting (secondsSinceLast=" + secondsSinceLastConnect + ")");
account.getXmppConnection().resetAttemptCount(false);
reconnectAccount(account, true, interactive); reconnectAccount(account, true, interactive);
} else if (account.getStatus() == Account.State.CONNECTING) { } else if (discoTimeout < 0) {
long secondsSinceLastConnect = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastConnect()) / 1000; account.getXmppConnection().sendDiscoTimeout();
long secondsSinceLastDisco = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastDiscoStarted()) / 1000; scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
long discoTimeout = Config.CONNECT_DISCO_TIMEOUT - secondsSinceLastDisco;
long timeout = Config.CONNECT_TIMEOUT - secondsSinceLastConnect;
if (timeout < 0) {
Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting (secondsSinceLast=" + secondsSinceLastConnect + ")");
account.getXmppConnection().resetAttemptCount(false);
reconnectAccount(account, true, interactive);
} else if (discoTimeout < 0) {
account.getXmppConnection().sendDiscoTimeout();
scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
} else {
scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
}
} else { } else {
final boolean aggressive = hasJingleRtpConnection(account); scheduleWakeUpCall((int) Math.min(timeout, discoTimeout), account.getUuid().hashCode());
if (account.getXmppConnection().getTimeToNextAttempt(aggressive) <= 0) { }
reconnectAccount(account, true, interactive); } else {
} final boolean aggressive = hasJingleRtpConnection(account);
if (account.getXmppConnection().getTimeToNextAttempt(aggressive) <= 0) {
reconnectAccount(account, true, interactive);
} }
} }
} }
return pingNow; return false;
} }
private void toggleSoftDisabled(final boolean softDisabled) { private void toggleSoftDisabled(final boolean softDisabled) {