diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java index 3f1c4dfd8..1e6480cce 100644 --- a/src/de/danoeh/antennapod/service/download/DownloadService.java +++ b/src/de/danoeh/antennapod/service/download/DownloadService.java @@ -309,15 +309,17 @@ public class DownloadService extends Service { private Downloader getDownloader(DownloadStatus status) { if (URLUtil.isHttpUrl(status.getFeedFile().getDownload_url())) { return new HttpDownloader(new DownloaderCallback() { - + @Override public void onDownloadCompleted(final Downloader downloader) { handler.post(new Runnable() { @Override public void run() { - DownloadService.this.onDownloadCompleted(downloader); - }}); + DownloadService.this + .onDownloadCompleted(downloader); + } + }); } }, status); } @@ -329,6 +331,21 @@ public class DownloadService extends Service { @SuppressLint("NewApi") public void onDownloadCompleted(final Downloader downloader) { final AsyncTask handlerTask = new AsyncTask() { + boolean successful; + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + if (!successful) { + queryDownloads(); + } + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + removeDownload(downloader); + } @Override protected Void doInBackground(Void... params) { @@ -337,7 +354,7 @@ public class DownloadService extends Service { downloadsBeingHandled += 1; DownloadStatus status = downloader.getStatus(); status.setCompletionDate(new Date()); - boolean successful = status.isSuccessful(); + successful = status.isSuccessful(); FeedFile download = status.getFeedFile(); if (download != null) { @@ -360,10 +377,6 @@ public class DownloadService extends Service { downloadsBeingHandled -= 1; } } - removeDownload(downloader); - if (!successful) { - queryDownloads(); - } return null; } }; @@ -379,7 +392,9 @@ public class DownloadService extends Service { * DownloadService list. */ private void removeDownload(final Downloader d) { - downloads.remove(d); + if (AppConfig.DEBUG) Log.d(TAG, "Removing downloader: " + d.getStatus().getFeedFile().getDownload_url()); + boolean rc = downloads.remove(d); + if (AppConfig.DEBUG) Log.d(TAG, "Result of downloads.remove: " + rc); DownloadRequester.getInstance().removeDownload( d.getStatus().getFeedFile()); sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); @@ -547,12 +562,12 @@ public class DownloadService extends Service { reason = 0; String reasonDetailed = null; successful = true; - FeedManager manager = FeedManager.getInstance(); - FeedHandler handler = new FeedHandler(); + final FeedManager manager = FeedManager.getInstance(); + FeedHandler feedHandler = new FeedHandler(); feed.setDownloaded(true); try { - feed = handler.parseFeed(feed); + feed = feedHandler.parseFeed(feed); if (AppConfig.DEBUG) Log.d(TAG, feed.getTitle() + " parsed"); if (checkFeedData(feed) == false) { @@ -566,18 +581,29 @@ public class DownloadService extends Service { if (AppConfig.DEBUG) Log.d(TAG, "Feed has image; Downloading...."); savedFeed.getImage().setFeed(savedFeed); - try { - requester.downloadImage(DownloadService.this, - savedFeed.getImage()); - } catch (DownloadRequestException e) { - e.printStackTrace(); - manager.addDownloadStatus(DownloadService.this, - new DownloadStatus(savedFeed.getImage(), - savedFeed.getImage() - .getHumanReadableIdentifier(), - DownloadError.ERROR_REQUEST_ERROR, - false, e.getMessage())); - } + final Feed savedFeedRef = savedFeed; + handler.post(new Runnable() { + + @Override + public void run() { + try { + requester.downloadImage(DownloadService.this, + savedFeedRef.getImage()); + } catch (DownloadRequestException e) { + e.printStackTrace(); + manager.addDownloadStatus( + DownloadService.this, + new DownloadStatus( + savedFeedRef.getImage(), + savedFeedRef + .getImage() + .getHumanReadableIdentifier(), + DownloadError.ERROR_REQUEST_ERROR, + false, e.getMessage())); + } + } + }); + } } catch (SAXException e) { @@ -617,7 +643,14 @@ public class DownloadService extends Service { reasonDetailed)); sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED); downloadsBeingHandled -= 1; - queryDownloads(); + handler.post(new Runnable() { + + @Override + public void run() { + queryDownloads(); + + } + }); } /** Checks if the feed was parsed correctly. */ @@ -694,7 +727,14 @@ public class DownloadService extends Service { "Image has no feed, image might not be saved correctly!"); } downloadsBeingHandled -= 1; - queryDownloads(); + handler.post(new Runnable() { + + @Override + public void run() { + queryDownloads(); + + } + }); } } @@ -750,7 +790,14 @@ public class DownloadService extends Service { } downloadsBeingHandled -= 1; - queryDownloads(); + handler.post(new Runnable() { + + @Override + public void run() { + queryDownloads(); + + } + }); } }