From fd28b32d5638d49eb382fdad7b39672ab6013be9 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 22 Jul 2018 22:42:32 +0200 Subject: [PATCH] call onMessageFound outside sync block --- .../conversations/entities/Conversation.java | 43 ++++++++++--------- .../services/XmppConnectionService.java | 16 +------ 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index ed3a443ef..e03f24693 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -151,34 +151,45 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl } public void findWaitingMessages(OnMessageFound onMessageFound) { + final ArrayList results = new ArrayList<>(); synchronized (this.messages) { for (Message message : this.messages) { if (message.getStatus() == Message.STATUS_WAITING) { - onMessageFound.onMessageFound(message); + results.add(message); } } } + for(Message result : results) { + onMessageFound.onMessageFound(result); + } } public void findUnreadMessages(OnMessageFound onMessageFound) { + final ArrayList results = new ArrayList<>(); synchronized (this.messages) { for (Message message : this.messages) { if (!message.isRead()) { - onMessageFound.onMessageFound(message); + results.add(message); } } } + for(Message result : results) { + onMessageFound.onMessageFound(result); + } } public void findMessagesWithFiles(final OnMessageFound onMessageFound) { + final ArrayList results = new ArrayList<>(); synchronized (this.messages) { - for (final Message message : this.messages) { - if ((message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) - && message.getEncryption() != Message.ENCRYPTION_PGP) { - onMessageFound.onMessageFound(message); + for (final Message m : this.messages) { + if (m.isFileOrImage() && m.getEncryption() != Message.ENCRYPTION_PGP) { + results.add(m); } } } + for(Message result : results) { + onMessageFound.onMessageFound(result); + } } public Message findMessageWithFileAndUuid(final String uuid) { @@ -242,25 +253,17 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl } } - public void findUnsentMessagesWithEncryption(int encryptionType, OnMessageFound onMessageFound) { + public void findUnsentTextMessages(OnMessageFound onMessageFound) { + final ArrayList results = new ArrayList<>(); synchronized (this.messages) { for (Message message : this.messages) { - if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_WAITING) - && (message.getEncryption() == encryptionType)) { - onMessageFound.onMessageFound(message); + if (message.getType() != Message.TYPE_IMAGE && message.getStatus() == Message.STATUS_UNSEND) { + results.add(message); } } } - } - - 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); - } - } + for(Message result : results) { + onMessageFound.onMessageFound(result); } } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 23899ff0d..a6ac97eeb 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1307,13 +1307,7 @@ public class XmppConnectionService extends Service { } private void sendUnsentMessages(final Conversation conversation) { - conversation.findWaitingMessages(new Conversation.OnMessageFound() { - - @Override - public void onMessageFound(Message message) { - resendMessage(message, true); - } - }); + conversation.findWaitingMessages(message -> resendMessage(message, true)); } public void resendMessage(final Message message, final boolean delay) { @@ -3136,13 +3130,7 @@ public class XmppConnectionService extends Service { public void resetSendingToWaiting(Account account) { for (Conversation conversation : getConversations()) { if (conversation.getAccount() == account) { - conversation.findUnsentTextMessages(new Conversation.OnMessageFound() { - - @Override - public void onMessageFound(Message message) { - markMessage(message, Message.STATUS_WAITING); - } - }); + conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING)); } } }