synchronzie on xmpp service around all state changes

This commit is contained in:
Daniel Gultsch 2016-11-18 13:14:26 +01:00
parent 1ed2445c1d
commit 0303c28ad9
1 changed files with 119 additions and 112 deletions

View File

@ -298,7 +298,7 @@ public class XmppConnectionService extends Service {
mOnAccountUpdate.onAccountUpdate();
}
if (account.getStatus() == Account.State.ONLINE) {
synchronized (mLowPingTimeoutMode) {
synchronized (XmppConnectionService.this) {
if (mLowPingTimeoutMode.remove(account.getJid().toBareJid())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": leaving low ping timeout mode");
}
@ -335,10 +335,11 @@ 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) {
} else {
synchronized (XmppConnectionService.this) {
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);
@ -347,7 +348,6 @@ public class XmppConnectionService extends Service {
scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
}
}
}
} else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
databaseBackend.updateAccount(account);
reconnectAccount(account, true, false);
@ -363,6 +363,8 @@ public class XmppConnectionService extends Service {
scheduleWakeUpCall(next, account.getUuid().hashCode());
}
}
}
}
getNotificationService().updateErrorNotification();
}
};
@ -611,12 +613,37 @@ public class XmppConnectionService extends Service {
break;
}
}
synchronized (this) {
this.wakeLock.acquire();
boolean pingNow = false;
HashSet<Account> pingCandidates = new HashSet<>();
for (Account account : accounts) {
pingNow |= processAccountState(account,
interactive,
"ui".equals(action),
CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash),
pingCandidates);
}
if (pingNow) {
for (Account account : pingCandidates) {
final boolean lowTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid());
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());
}
}
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (final RuntimeException ignored) {
}
}
}
return START_STICKY;
}
private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) {
boolean pingNow = false;
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
if (!hasInternetConnection()) {
account.setStatus(Account.State.NO_INTERNET);
@ -634,7 +661,7 @@ public class XmppConnectionService extends Service {
synchronized (mLowPingTimeoutMode) {
long lastReceived = account.getXmppConnection().getLastPacketReceived();
long lastSent = account.getXmppConnection().getLastPingSent();
long pingInterval = "ui".equals(action) ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
long pingInterval = isUiAction ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000;
long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime();
@ -648,7 +675,7 @@ public class XmppConnectionService extends Service {
}
} else {
pingCandidates.add(account);
if (CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash)) {
if (isAccountPushed) {
pingNow = true;
if (mLowPingTimeoutMode.add(account.getJid().toBareJid())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": entering low ping timeout mode");
@ -686,28 +713,8 @@ public class XmppConnectionService extends Service {
}
}
}
if (mOnAccountUpdate != null) {
mOnAccountUpdate.onAccountUpdate();
}
}
}
if (pingNow) {
for (Account account : pingCandidates) {
synchronized (mLowPingTimeoutMode) {
final boolean lowTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid());
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());
}
}
}
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (final RuntimeException ignored) {
}
}
return START_STICKY;
return pingNow;
}
public boolean isDataSaverDisabled() {