From 475c01cbc64fdcb28fad3a3ae39bd3e829bcb6a1 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Thu, 28 Nov 2024 10:21:05 -0500 Subject: [PATCH] Catch concurrent modification and abort Someone added an attachment while we were sending or what? (cherry picked from commit c4f751e2dd6b0f1bbb56704a3f95e76152989389) --- .../ui/ConversationFragment.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 30d5826d8..def245800 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -1524,22 +1524,27 @@ public class ConversationFragment extends XmppFragment final Runnable next = new Runnable() { @Override public void run() { - final Attachment attachment = i.next(); - if (attachment.getType() == Attachment.Type.LOCATION) { - attachLocationToConversation(conversation, attachment.getUri()); - if (i.hasNext()) runOnUiThread(this); - } else if (attachment.getType() == Attachment.Type.IMAGE) { - Log.d( - Config.LOGTAG, - "ConversationsActivity.commitAttachments() - attaching image to conversations. CHOOSE_IMAGE"); - attachImageToConversation(conversation, attachment.getUri(), attachment.getMime(), i.hasNext() ? this : null); - } else { - Log.d( - Config.LOGTAG, - "ConversationsActivity.commitAttachments() - attaching file to conversations. CHOOSE_FILE/RECORD_VOICE/RECORD_VIDEO"); - attachFileToConversation(conversation, attachment.getUri(), attachment.getMime(), i.hasNext() ? this : null); + try { + final Attachment attachment = i.next(); + if (attachment.getType() == Attachment.Type.LOCATION) { + attachLocationToConversation(conversation, attachment.getUri()); + if (i.hasNext()) runOnUiThread(this); + } else if (attachment.getType() == Attachment.Type.IMAGE) { + Log.d( + Config.LOGTAG, + "ConversationsActivity.commitAttachments() - attaching image to conversations. CHOOSE_IMAGE"); + attachImageToConversation(conversation, attachment.getUri(), attachment.getMime(), i.hasNext() ? this : null); + } else { + Log.d( + Config.LOGTAG, + "ConversationsActivity.commitAttachments() - attaching file to conversations. CHOOSE_FILE/RECORD_VOICE/RECORD_VIDEO"); + attachFileToConversation(conversation, attachment.getUri(), attachment.getMime(), i.hasNext() ? this : null); + } + i.remove(); + } catch (final java.util.ConcurrentModificationException e) { + // Abort, leave any unsent attachments alone for the user to try again + Toast.makeText(activity, "Sometimes went wrong with some attachments. Try again?", Toast.LENGTH_SHORT).show(); } - i.remove(); mediaPreviewAdapter.notifyDataSetChanged(); toggleInputMethod(); }