From f8a496a5f1e225585d0ecaec68fb81e8cffa3083 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Sun, 14 Dec 2014 23:23:32 +0100 Subject: run mam queries only when online --- .../services/MessageArchiveService.java | 34 +++++++++++++++++++--- .../services/XmppConnectionService.java | 1 + 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 9dfe179c..fe1871ea 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 9b1ecdea..b7ca699c 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) { -- cgit v1.2.3