diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 9dfe179c1..fe1871eaf 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -3,7 +3,9 @@ package eu.siacs.conversations.services; import android.util.Log; import java.math.BigInteger; +import java.util.ArrayList; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import eu.siacs.conversations.Config; @@ -22,6 +24,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { private final XmppConnectionService mXmppConnectionService; private final HashSet queries = new HashSet(); + private ArrayList pendingQueries = new ArrayList(); public enum PagingOrder { NORMAL, @@ -83,18 +86,41 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { } } + public void executePendingQueries(final Account account) { + List pending = new ArrayList<>(); + synchronized(this.pendingQueries) { + for(Iterator iterator = this.pendingQueries.iterator(); iterator.hasNext();) { + Query query = iterator.next(); + if (query.getAccount() == account) { + pending.add(query); + iterator.remove(); + } + } + } + for(Query query : pending) { + this.execute(query); + } + } + private void execute(final Query query) { - Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": running mam query "+query.toString()); - IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query); - this.mXmppConnectionService.sendIqPacket(query.getAccount(), packet, new OnIqPacketReceived() { + final Account account= query.getAccount(); + if (account.getStatus() == Account.State.ONLINE) { + Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString()); + IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query); + this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE_ERROR) { - Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": error executing mam: "+packet.toString()); + Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString()); finalizeQuery(query); } } }); + } else { + synchronized (this.pendingQueries) { + this.pendingQueries.add(query); + } + } } private void finalizeQuery(Query query) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 9b1ecdeae..b7ca699c2 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -160,6 +160,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa for (Conversation conversation : account.pendingConferenceJoins) { joinMuc(conversation); } + mMessageArchiveService.executePendingQueries(account); mJingleConnectionManager.cancelInTransmission(); List conversations = getConversations(); for (Conversation conversation : conversations) {