From 5ca7b1080ce58a02a827260692539d84d277e1b7 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 26 Feb 2013 22:56:48 -0800 Subject: [PATCH] Fix for concurrentmodificationexception on race condition. --- .../securesms/service/MmsDownloader.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/org/thoughtcrime/securesms/service/MmsDownloader.java b/src/org/thoughtcrime/securesms/service/MmsDownloader.java index 668831c420..8f54d7ade7 100644 --- a/src/org/thoughtcrime/securesms/service/MmsDownloader.java +++ b/src/org/thoughtcrime/securesms/service/MmsDownloader.java @@ -138,24 +138,28 @@ public class MmsDownloader extends MmscProcessor { } protected void handleConnectivityChange() { - if (!isConnected()) { - if (!isConnectivityPossible() && !pendingMessages.isEmpty()) { - DatabaseFactory.getMmsDatabase(context).markDownloadState(pendingMessages.remove().getMessageId(), MmsDatabase.Types.DOWNLOAD_NO_CONNECTIVITY); - toastHandler.makeToast(context - .getString(R.string.MmsDownloader_no_connectivity_available_for_mms_download_try_again_later)); - Log.w("MmsDownloadService", "Unable to download MMS, please try again later."); - finishConnectivity(); + LinkedList downloadItems = (LinkedList)pendingMessages.clone(); + + if (isConnected()) { + pendingMessages.clear(); + + for (DownloadItem item : downloadItems) { + downloadMms(item); } - return; - } + finishConnectivity(); + } else if (!isConnected() && !isConnectivityPossible()) { + pendingMessages.clear(); - for (DownloadItem item : pendingMessages) { - downloadMms(item); - } + for (DownloadItem item : downloadItems) { + DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_NO_CONNECTIVITY); + } - pendingMessages.clear(); - finishConnectivity(); + toastHandler.makeToast(context + .getString(R.string.MmsDownloader_no_connectivity_available_for_mms_download_try_again_later)); + + finishConnectivity(); + } }