From 8bda54d48c185a1ce7787080fe6223d47377d55e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 3 Jan 2015 00:47:22 +0100 Subject: [PATCH] code cleanup --- .../conversations/entities/Conversation.java | 20 +++++++++++-------- .../services/MessageArchiveService.java | 9 +++++++-- .../services/XmppConnectionService.java | 4 +++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index a88984fb1..470bd2902 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -76,6 +76,16 @@ public class Conversation extends AbstractEntity implements Blockable { private Bookmark bookmark; + private boolean messagesLeftOnServer = true; + + public boolean hasMessagesLeftOnServer() { + return messagesLeftOnServer; + } + + public void setHasMessagesLeftOnServer(boolean value) { + this.messagesLeftOnServer = value; + } + public Message findUnsentMessageWithUuid(String uuid) { synchronized(this.messages) { for (final Message message : this.messages) { @@ -227,13 +237,10 @@ public class Conversation extends AbstractEntity implements Blockable { } public boolean isRead() { - return (this.messages == null) || (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); + return (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); } public void markRead() { - if (this.messages == null) { - return; - } for (int i = this.messages.size() - 1; i >= 0; --i) { if (messages.get(i).isRead()) { break; @@ -243,9 +250,6 @@ public class Conversation extends AbstractEntity implements Blockable { } public Message getLatestMarkableMessage() { - if (this.messages == null) { - return null; - } for (int i = this.messages.size() - 1; i >= 0; --i) { if (this.messages.get(i).getStatus() <= Message.STATUS_RECEIVED && this.messages.get(i).markable) { @@ -260,7 +264,7 @@ public class Conversation extends AbstractEntity implements Blockable { } public Message getLatestMessage() { - if ((this.messages == null) || (this.messages.size() == 0)) { + if (this.messages.size() == 0) { Message message = new Message(this, "", Message.ENCRYPTION_NONE); message.setTime(getCreated()); return message; diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java index 3fef5703c..821112433 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java +++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java @@ -24,7 +24,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { private final XmppConnectionService mXmppConnectionService; private final HashSet queries = new HashSet(); - private ArrayList pendingQueries = new ArrayList(); + private final ArrayList pendingQueries = new ArrayList(); public enum PagingOrder { NORMAL, @@ -134,6 +134,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { if (conversation.setLastMessageTransmitted(query.getEnd())) { this.mXmppConnectionService.databaseBackend.updateConversation(conversation); } + conversation.setHasMessagesLeftOnServer(query.getMessageCount() > 0); if (query.hasCallback()) { query.callback(); } else { @@ -300,7 +301,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { public void callback() { if (this.callback != null) { this.callback.onMoreMessagesLoaded(messageCount,conversation); - if (messageCount==0) { + if (messageCount == 0) { this.callback.informUser(R.string.no_more_history_on_server); } } @@ -330,6 +331,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded { return this.totalCount; } + public int getMessageCount() { + return this.messageCount; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index bdc517830..ec904fbb2 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -971,7 +971,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (messages.size() > 0) { conversation.addAll(0, messages); callback.onMoreMessagesLoaded(messages.size(), conversation); - } else if (account.getStatus() == Account.State.ONLINE && account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) { + } else if (conversation.hasMessagesLeftOnServer() + && account.isOnlineAndConnected() + && account.getXmppConnection().getFeatures().mam()) { MessageArchiveService.Query query = getMessageArchiveService().query(conversation,0,timestamp - 1); if (query != null) { query.setCallback(callback);