Completed downloader-objects were removed too late

This commit is contained in:
daniel oeh
2012-12-21 22:10:32 +01:00
parent 63c86505a3
commit 90b7fd012d

View File

@ -316,8 +316,10 @@ public class DownloadService extends Service {
@Override @Override
public void run() { public void run() {
DownloadService.this.onDownloadCompleted(downloader); DownloadService.this
}}); .onDownloadCompleted(downloader);
}
});
} }
}, status); }, status);
} }
@ -329,6 +331,21 @@ public class DownloadService extends Service {
@SuppressLint("NewApi") @SuppressLint("NewApi")
public void onDownloadCompleted(final Downloader downloader) { public void onDownloadCompleted(final Downloader downloader) {
final AsyncTask<Void, Void, Void> handlerTask = new AsyncTask<Void, Void, Void>() { final AsyncTask<Void, Void, Void> handlerTask = new AsyncTask<Void, Void, Void>() {
boolean successful;
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (!successful) {
queryDownloads();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
removeDownload(downloader);
}
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
@ -337,7 +354,7 @@ public class DownloadService extends Service {
downloadsBeingHandled += 1; downloadsBeingHandled += 1;
DownloadStatus status = downloader.getStatus(); DownloadStatus status = downloader.getStatus();
status.setCompletionDate(new Date()); status.setCompletionDate(new Date());
boolean successful = status.isSuccessful(); successful = status.isSuccessful();
FeedFile download = status.getFeedFile(); FeedFile download = status.getFeedFile();
if (download != null) { if (download != null) {
@ -360,10 +377,6 @@ public class DownloadService extends Service {
downloadsBeingHandled -= 1; downloadsBeingHandled -= 1;
} }
} }
removeDownload(downloader);
if (!successful) {
queryDownloads();
}
return null; return null;
} }
}; };
@ -379,7 +392,9 @@ public class DownloadService extends Service {
* DownloadService list. * DownloadService list.
*/ */
private void removeDownload(final Downloader d) { 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( DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile()); d.getStatus().getFeedFile());
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
@ -547,12 +562,12 @@ public class DownloadService extends Service {
reason = 0; reason = 0;
String reasonDetailed = null; String reasonDetailed = null;
successful = true; successful = true;
FeedManager manager = FeedManager.getInstance(); final FeedManager manager = FeedManager.getInstance();
FeedHandler handler = new FeedHandler(); FeedHandler feedHandler = new FeedHandler();
feed.setDownloaded(true); feed.setDownloaded(true);
try { try {
feed = handler.parseFeed(feed); feed = feedHandler.parseFeed(feed);
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, feed.getTitle() + " parsed"); Log.d(TAG, feed.getTitle() + " parsed");
if (checkFeedData(feed) == false) { if (checkFeedData(feed) == false) {
@ -566,19 +581,30 @@ public class DownloadService extends Service {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Feed has image; Downloading...."); Log.d(TAG, "Feed has image; Downloading....");
savedFeed.getImage().setFeed(savedFeed); savedFeed.getImage().setFeed(savedFeed);
final Feed savedFeedRef = savedFeed;
handler.post(new Runnable() {
@Override
public void run() {
try { try {
requester.downloadImage(DownloadService.this, requester.downloadImage(DownloadService.this,
savedFeed.getImage()); savedFeedRef.getImage());
} catch (DownloadRequestException e) { } catch (DownloadRequestException e) {
e.printStackTrace(); e.printStackTrace();
manager.addDownloadStatus(DownloadService.this, manager.addDownloadStatus(
new DownloadStatus(savedFeed.getImage(), DownloadService.this,
savedFeed.getImage() new DownloadStatus(
savedFeedRef.getImage(),
savedFeedRef
.getImage()
.getHumanReadableIdentifier(), .getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR, DownloadError.ERROR_REQUEST_ERROR,
false, e.getMessage())); false, e.getMessage()));
} }
} }
});
}
} catch (SAXException e) { } catch (SAXException e) {
successful = false; successful = false;
@ -617,7 +643,14 @@ public class DownloadService extends Service {
reasonDetailed)); reasonDetailed));
sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED); sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED);
downloadsBeingHandled -= 1; downloadsBeingHandled -= 1;
handler.post(new Runnable() {
@Override
public void run() {
queryDownloads(); queryDownloads();
}
});
} }
/** Checks if the feed was parsed correctly. */ /** 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!"); "Image has no feed, image might not be saved correctly!");
} }
downloadsBeingHandled -= 1; downloadsBeingHandled -= 1;
handler.post(new Runnable() {
@Override
public void run() {
queryDownloads(); queryDownloads();
}
});
} }
} }
@ -750,7 +790,14 @@ public class DownloadService extends Service {
} }
downloadsBeingHandled -= 1; downloadsBeingHandled -= 1;
handler.post(new Runnable() {
@Override
public void run() {
queryDownloads(); queryDownloads();
}
});
} }
} }