avoid race conditions when downloading files or decrypting pgp messages and waiting for sm catchup

This commit is contained in:
Daniel Gultsch 2017-04-30 16:19:39 +02:00
parent 15e5ccd1f4
commit 84baa3ae68
3 changed files with 23 additions and 11 deletions

View file

@ -624,9 +624,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} else if (notify) { } else if (notify) {
if (query != null && query.isCatchup()) { if (query != null && query.isCatchup()) {
mXmppConnectionService.getNotificationService().pushFromBacklog(message); mXmppConnectionService.getNotificationService().pushFromBacklog(message);
} else if (account.getXmppConnection().isWaitingForSmCatchup()) {
account.getXmppConnection().incrementSmCatchupMessageCounter();
mXmppConnectionService.getNotificationService().pushFromBacklog(message);
} else { } else {
mXmppConnectionService.getNotificationService().push(message); mXmppConnectionService.getNotificationService().push(message);
} }
@ -700,7 +697,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
if (displayed != null) { if (displayed != null) {
if (packet.fromAccount(account)) { if (packet.fromAccount(account)) {
Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid()); Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
if (conversation != null) { if (conversation != null && (query == null || query.isCatchup())) {
mXmppConnectionService.markRead(conversation); mXmppConnectionService.markRead(conversation);
} }
} else { } else {

View file

@ -46,6 +46,7 @@ import eu.siacs.conversations.ui.SettingsActivity;
import eu.siacs.conversations.ui.TimePreference; import eu.siacs.conversations.ui.TimePreference;
import eu.siacs.conversations.utils.GeoHelper; import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.XmppConnection;
public class NotificationService { public class NotificationService {
@ -170,6 +171,18 @@ public class NotificationService {
} }
public void push(final Message message) { public void push(final Message message) {
synchronized (message.getConversation().getAccount()) {
final XmppConnection connection = message.getConversation().getAccount().getXmppConnection();
if (connection.isWaitingForSmCatchup()) {
connection.incrementSmCatchupMessageCounter();
pushFromBacklog(message);
} else {
pushNow(message);
}
}
}
private void pushNow(final Message message) {
mXmppConnectionService.updateUnreadCountBadge(); mXmppConnectionService.updateUnreadCountBadge();
if (!notify(message)) { if (!notify(message)) {
Log.d(Config.LOGTAG,message.getConversation().getAccount().getJid().toBareJid()+": suppressing notification because turned off"); Log.d(Config.LOGTAG,message.getConversation().getAccount().getJid().toBareJid()+": suppressing notification because turned off");

View file

@ -96,7 +96,7 @@ public class XmppConnection implements Runnable {
private static final int PACKET_IQ = 0; private static final int PACKET_IQ = 0;
private static final int PACKET_MESSAGE = 1; private static final int PACKET_MESSAGE = 1;
private static final int PACKET_PRESENCE = 2; private static final int PACKET_PRESENCE = 2;
protected Account account; protected final Account account;
private final WakeLock wakeLock; private final WakeLock wakeLock;
private Socket socket; private Socket socket;
private XmlReader tagReader; private XmlReader tagReader;
@ -133,7 +133,7 @@ public class XmppConnection implements Runnable {
private OnBindListener bindListener = null; private OnBindListener bindListener = null;
private final ArrayList<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners = new ArrayList<>(); private final ArrayList<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners = new ArrayList<>();
private OnMessageAcknowledged acknowledgedListener = null; private OnMessageAcknowledged acknowledgedListener = null;
private XmppConnectionService mXmppConnectionService = null; private final XmppConnectionService mXmppConnectionService;
private SaslMechanism saslMechanism; private SaslMechanism saslMechanism;
@ -626,6 +626,7 @@ public class XmppConnection implements Runnable {
final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion); final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
tagWriter.writeStanzaAsync(ack); tagWriter.writeStanzaAsync(ack);
} else if (nextTag.isStart("a")) { } else if (nextTag.isStart("a")) {
synchronized (account) {
if (mWaitingForSmCatchup.compareAndSet(true, false)) { if (mWaitingForSmCatchup.compareAndSet(true, false)) {
int count = mSmCatchupMessageCounter.get(); int count = mSmCatchupMessageCounter.get();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": SM catchup complete (" + count + ")"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": SM catchup complete (" + count + ")");
@ -633,6 +634,7 @@ public class XmppConnection implements Runnable {
mXmppConnectionService.getNotificationService().finishBacklog(true, account); mXmppConnectionService.getNotificationService().finishBacklog(true, account);
} }
} }
}
final Element ack = tagReader.readElement(nextTag); final Element ack = tagReader.readElement(nextTag);
lastPacketReceived = SystemClock.elapsedRealtime(); lastPacketReceived = SystemClock.elapsedRealtime();
try { try {