diff options
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/entities/Conversation.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/entities/Conversation.java | 166 |
1 files changed, 73 insertions, 93 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/entities/Conversation.java b/src/main/java/de/thedevstack/conversationsplus/entities/Conversation.java index 2edd1135..0cce63f4 100644 --- a/src/main/java/de/thedevstack/conversationsplus/entities/Conversation.java +++ b/src/main/java/de/thedevstack/conversationsplus/entities/Conversation.java @@ -16,12 +16,14 @@ import org.json.JSONObject; import java.security.interfaces.DSAPublicKey; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; -import java.util.Iterator; import java.util.List; +import java.util.NavigableSet; +import java.util.TreeSet; import de.thedevstack.conversationsplus.dto.LoadAvatarFor; +import de.thedevstack.conversationsplus.enums.MessageDirection; +import de.thedevstack.conversationsplus.enums.MessageStatus; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.crypto.axolotl.AxolotlService; @@ -66,7 +68,14 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata private Jid nextCounterpart; - protected final ArrayList<Message> messages = new ArrayList<>(); + protected final TreeSet<Message> messages = new TreeSet<>(new Comparator<Message>() { + @Override + public int compare(Message message, Message otherMessage) { + long thisTimeSent = message.getTimeSent(); + long otherTimeSent = otherMessage.getTimeSent(); + return (thisTimeSent < otherTimeSent ? -1 : (thisTimeSent == otherTimeSent ? 0 : 1)); + } + }); protected Account account = null; private transient SessionImpl otrSession; @@ -100,8 +109,8 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata public Message findUnsentMessageWithUuid(String uuid) { synchronized(this.messages) { for (final Message message : this.messages) { - final int s = message.getStatus(); - if ((s == Message.STATUS_UNSEND || s == Message.STATUS_WAITING) && message.getUuid().equals(uuid)) { + if ((MessageUtil.isOutgoingMessage(message) + && (MessageStatus.WAITING == message.getMessageStatus() || MessageStatus.TRANSMITTING == message.getMessageStatus())) && message.getUuid().equals(uuid)) { return message; } } @@ -112,7 +121,7 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata public void findWaitingMessages(OnMessageFound onMessageFound) { synchronized (this.messages) { for(Message message : this.messages) { - if (message.getStatus() == Message.STATUS_WAITING) { + if (MessageUtil.isOutgoingMessage(message) && MessageStatus.WAITING == message.getMessageStatus()) { onMessageFound.onMessageFound(message); } } @@ -193,7 +202,9 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata final int size = messages.size(); final int maxsize = Config.PAGE_SIZE * Config.MAX_NUM_PAGES; if (size > maxsize) { - this.messages.subList(0, size - maxsize).clear(); + for (int i = 0; i < size - maxsize; i++) { + this.messages.pollFirst(); + } } } } @@ -201,7 +212,7 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata public void findUnsentMessagesWithEncryption(int encryptionType, OnMessageFound onMessageFound) { synchronized (this.messages) { for (Message message : this.messages) { - if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_WAITING) + if ((MessageUtil.isOutgoingMessage(message) && (MessageStatus.WAITING == message.getMessageStatus() || MessageStatus.TRANSMITTING == message.getMessageStatus())) && (message.getEncryption() == encryptionType)) { onMessageFound.onMessageFound(message); } @@ -209,23 +220,25 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata } } - public void findUnsentTextMessages(OnMessageFound onMessageFound) { - synchronized (this.messages) { - for (Message message : this.messages) { - if (message.getType() != Message.TYPE_IMAGE - && message.getStatus() == Message.STATUS_UNSEND) { - onMessageFound.onMessageFound(message); - } - } - } - } + public void findOugoingTextMessagesInTransmission(OnMessageFound onMessageFound) { + synchronized (this.messages) { + for (Message message : this.messages) { + if (message.getType() != Message.TYPE_IMAGE // TODO: Check if we should resend everything + && MessageUtil.isOutgoingMessage(message) + && MessageStatus.TRANSMITTING == message.getMessageStatus()) { + onMessageFound.onMessageFound(message); + } + } + } + } public Message findSentMessageWithUuidOrRemoteId(String id) { synchronized (this.messages) { for (Message message : this.messages) { if (id.equals(message.getUuid()) - || (message.getStatus() >= Message.STATUS_SEND - && id.equals(message.getRemoteMsgId()))) { + || (MessageUtil.isOutgoingMessage(message) + && MessageUtil.isMessageTransmittedOrDisplayedOrReceived(message) + && id.equals(message.getRemoteMsgId()))) { return message; } } @@ -233,24 +246,6 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata return null; } - public Message findMessageWithRemoteIdAndCounterpart(String id, Jid counterpart, boolean received, boolean carbon) { - synchronized (this.messages) { - for(int i = this.messages.size() - 1; i >= 0; --i) { - Message message = messages.get(i); - if (counterpart.equals(message.getCounterpart()) - && ((message.getStatus() == Message.STATUS_RECEIVED) == received) - && (carbon == message.isCarbon() || received) ) { - if (id.equals(message.getRemoteMsgId())) { - return message; - } else { - return null; - } - } - } - } - return null; - } - public Message findSentMessageWithUuid(String id) { synchronized (this.messages) { for (Message message : this.messages) { @@ -350,33 +345,42 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata } public boolean isRead() { - return (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); + return (this.messages.size() == 0) || this.messages.last().isRead(); } + /** + * Marks messages as read, starting with the latest message. + * Stops iterating at the first messages that is already read + * @return List of the messages which are newly marked as read + */ public List<Message> markRead() { final List<Message> unread = new ArrayList<>(); synchronized (this.messages) { - for(Message message : this.messages) { + for (Message message : this.messages.descendingSet()) { if (!message.isRead()) { message.markRead(); unread.add(message); - } + } else { + break; + } } } return unread; } public Message getLatestMarkableMessage() { - for (int i = this.messages.size() - 1; i >= 0; --i) { - if (this.messages.get(i).getStatus() <= Message.STATUS_RECEIVED - && this.messages.get(i).markable) { - if (this.messages.get(i).isRead()) { + NavigableSet<Message> descendingSet = this.messages.descendingSet(); + for (Message message : descendingSet) { + if (MessageUtil.isIncomingMessage(message) + && message.markable) { + if (message.isRead()) { return null; } else { - return this.messages.get(i); + return message; } - } + } } + return null; } @@ -386,7 +390,7 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata message.setTime(getCreated()); return message; } else { - Message message = this.messages.get(this.messages.size() - 1); + Message message = this.messages.last(); message.setConversation(this); return message; } @@ -621,9 +625,8 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata private int getMostRecentlyUsedOutgoingEncryption() { synchronized (this.messages) { - for(int i = this.messages.size() -1; i >= 0; --i) { - final Message m = this.messages.get(i); - if (!m.isCarbon() && m.getStatus() != Message.STATUS_RECEIVED) { + for (Message m : this.messages.descendingSet()) { + if (!m.isCarbon() && MessageUtil.isOutgoingMessage(m)) { final int e = m.getEncryption(); if (e == Message.ENCRYPTION_DECRYPTED || e == Message.ENCRYPTION_DECRYPTION_FAILED) { return Message.ENCRYPTION_PGP; @@ -638,10 +641,9 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata private int getMostRecentlyUsedIncomingEncryption() { synchronized (this.messages) { - for(int i = this.messages.size() -1; i >= 0; --i) { - final Message m = this.messages.get(i); - if (m.getStatus() == Message.STATUS_RECEIVED) { - final int e = m.getEncryption(); + for (Message message : this.messages.descendingSet()) { + if (MessageUtil.isIncomingMessage(message)) { + final int e = message.getEncryption(); if (e == Message.ENCRYPTION_DECRYPTED || e == Message.ENCRYPTION_DECRYPTION_FAILED) { return Message.ENCRYPTION_PGP; } else { @@ -734,8 +736,8 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata public boolean hasDuplicateMessage(Message message) { synchronized (this.messages) { - for (int i = this.messages.size() - 1; i >= 0; --i) { - if (this.messages.get(i).equals(message)) { + for (Message m : this.messages.descendingSet()) { + if (m.equals(message)) { return true; } } @@ -745,9 +747,8 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata public Message findSentMessageWithBody(String body) { synchronized (this.messages) { - for (int i = this.messages.size() - 1; i >= 0; --i) { - Message message = this.messages.get(i); - if (message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_SEND) { + for (Message message : this.messages.descendingSet()) { + if (MessageUtil.isMessageSent(message)) { String otherBody = message.getBody(); if (otherBody != null && otherBody.equals(body)) { return message; @@ -760,9 +761,8 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata public long getLastMessageTransmitted() { synchronized (this.messages) { - for(int i = this.messages.size() - 1; i >= 0; --i) { - Message message = this.messages.get(i); - if (message.getStatus() == Message.STATUS_RECEIVED || message.isCarbon()) { + for (Message message : this.messages.descendingSet()) { + if (MessageUtil.isIncomingMessage(message) || message.isCarbon()) { return message.getTimeSent(); } } @@ -880,48 +880,20 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata } } - public void prepend(Message message) { - message.setConversation(this); - synchronized (this.messages) { - this.messages.add(0,message); - } - } - public void addAll(int index, List<Message> messages) { synchronized (this.messages) { - this.messages.addAll(index, messages); + this.messages.addAll(messages); } account.getPgpDecryptionService().addAll(messages); } - public void sort() { - synchronized (this.messages) { - Collections.sort(this.messages, new Comparator<Message>() { - @Override - public int compare(Message left, Message right) { - if (left.getTimeSent() < right.getTimeSent()) { - return -1; - } else if (left.getTimeSent() > right.getTimeSent()) { - return 1; - } else { - return 0; - } - } - }); - for(Message message : this.messages) { - message.untie(); - } - } - } - public int unreadCount() { if (getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL,0) == Long.MAX_VALUE) { return 0; } synchronized (this.messages) { int count = 0; - for(int i = this.messages.size() - 1; i >= 0; --i) { - Message message = this.messages.get(i); + for (Message message : this.messages.descendingSet()) { if (message.isRead()) { return count; } @@ -933,6 +905,14 @@ public class Conversation extends AbstractEntity implements Blockable, LoadAvata } } + public Message getMessageBefore(Message message) { + return this.messages.lower(message); + } + + public Message getMessageAfter(Message message) { + return this.messages.higher(message); + } + public class Smp { public static final int STATUS_NONE = 0; public static final int STATUS_CONTACT_REQUESTED = 1; |