add more fault tolerant checks for messages left on server

This commit is contained in:
Daniel Gultsch 2016-02-04 11:55:42 +01:00
parent 17791a703e
commit f88b8c703e
3 changed files with 21 additions and 7 deletions

View File

@ -81,6 +81,7 @@ public class Conversation extends AbstractEntity implements Blockable {
private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE; private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE;
private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE; private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE;
private String mLastReceivedOtrMessageId = null; private String mLastReceivedOtrMessageId = null;
private String mFirstMamReference = null;
public boolean hasMessagesLeftOnServer() { public boolean hasMessagesLeftOnServer() {
return messagesLeftOnServer; return messagesLeftOnServer;
@ -277,6 +278,14 @@ public class Conversation extends AbstractEntity implements Blockable {
} }
} }
public void setFirstMamReference(String reference) {
this.mFirstMamReference = reference;
}
public String getFirstMamReference() {
return this.mFirstMamReference;
}
public interface OnMessageFound { public interface OnMessageFound {
void onMessageFound(final Message message); void onMessageFound(final Message message);
} }

View File

@ -102,6 +102,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
return null; return null;
} }
final Query query = new Query(conversation, start, end,PagingOrder.REVERSE); final Query query = new Query(conversation, start, end,PagingOrder.REVERSE);
query.reference = conversation.getFirstMamReference();
this.queries.add(query); this.queries.add(query);
this.execute(query); this.execute(query);
return query; return query;
@ -141,7 +142,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
} }
} else if (packet.getType() != IqPacket.TYPE.RESULT) { } else if (packet.getType() != IqPacket.TYPE.RESULT) {
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); finalizeQuery(query, true);
} }
} }
}); });
@ -152,14 +153,14 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
} }
} }
private void finalizeQuery(Query query) { private void finalizeQuery(Query query, boolean done) {
synchronized (this.queries) { synchronized (this.queries) {
this.queries.remove(query); this.queries.remove(query);
} }
final Conversation conversation = query.getConversation(); final Conversation conversation = query.getConversation();
if (conversation != null) { if (conversation != null) {
conversation.sort(); conversation.sort();
conversation.setHasMessagesLeftOnServer(query.getMessageCount() > 0); conversation.setHasMessagesLeftOnServer(!done);
} else { } else {
for(Conversation tmp : this.mXmppConnectionService.getConversations()) { for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
if (tmp.getAccount() == query.getAccount()) { if (tmp.getAccount() == query.getAccount()) {
@ -202,9 +203,13 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
Element first = set == null ? null : set.findChild("first"); Element first = set == null ? null : set.findChild("first");
Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first; Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
boolean abort = (query.getStart() == 0 && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES; boolean abort = (query.getStart() == 0 && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
if (query.getConversation() != null) {
query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
}
if (complete || relevant == null || abort) { if (complete || relevant == null || abort) {
this.finalizeQuery(query); final boolean done = (complete || query.getMessageCount() == 0) && query.getStart() == 0;
Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": finished mam after "+query.getTotalCount()+" messages"); this.finalizeQuery(query, done);
Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+" messages. messages left="+Boolean.toString(!done));
if (query.getWith() == null && query.getMessageCount() > 0) { if (query.getWith() == null && query.getMessageCount() > 0) {
mXmppConnectionService.getNotificationService().finishBacklog(true); mXmppConnectionService.getNotificationService().finishBacklog(true);
} }
@ -216,7 +221,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
nextQuery = query.prev(first == null ? null : first.getContent()); nextQuery = query.prev(first == null ? null : first.getContent());
} }
this.execute(nextQuery); this.execute(nextQuery);
this.finalizeQuery(query); this.finalizeQuery(query, false);
synchronized (this.queries) { synchronized (this.queries) {
this.queries.remove(query); this.queries.remove(query);
this.queries.add(nextQuery); this.queries.add(nextQuery);

View File

@ -1230,7 +1230,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
&& account.isOnlineAndConnected()) { && account.isOnlineAndConnected()) {
if ((conversation.getMode() == Conversation.MODE_SINGLE && account.getXmppConnection().getFeatures().mam()) if ((conversation.getMode() == Conversation.MODE_SINGLE && account.getXmppConnection().getFeatures().mam())
|| (conversation.getMode() == Conversation.MODE_MULTI && conversation.getMucOptions().mamSupport())) { || (conversation.getMode() == Conversation.MODE_MULTI && conversation.getMucOptions().mamSupport())) {
MessageArchiveService.Query query = getMessageArchiveService().query(conversation, 0, timestamp - 1); MessageArchiveService.Query query = getMessageArchiveService().query(conversation, 0, timestamp);
if (query != null) { if (query != null) {
query.setCallback(callback); query.setCallback(callback);
} }