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
1 changed files with 74 additions and 27 deletions

View File

@ -309,15 +309,17 @@ public class DownloadService extends Service {
private Downloader getDownloader(DownloadStatus status) { private Downloader getDownloader(DownloadStatus status) {
if (URLUtil.isHttpUrl(status.getFeedFile().getDownload_url())) { if (URLUtil.isHttpUrl(status.getFeedFile().getDownload_url())) {
return new HttpDownloader(new DownloaderCallback() { return new HttpDownloader(new DownloaderCallback() {
@Override @Override
public void onDownloadCompleted(final Downloader downloader) { public void onDownloadCompleted(final Downloader downloader) {
handler.post(new Runnable() { handler.post(new Runnable() {
@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,18 +581,29 @@ 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);
try { final Feed savedFeedRef = savedFeed;
requester.downloadImage(DownloadService.this, handler.post(new Runnable() {
savedFeed.getImage());
} catch (DownloadRequestException e) { @Override
e.printStackTrace(); public void run() {
manager.addDownloadStatus(DownloadService.this, try {
new DownloadStatus(savedFeed.getImage(), requester.downloadImage(DownloadService.this,
savedFeed.getImage() savedFeedRef.getImage());
.getHumanReadableIdentifier(), } catch (DownloadRequestException e) {
DownloadError.ERROR_REQUEST_ERROR, e.printStackTrace();
false, e.getMessage())); manager.addDownloadStatus(
} DownloadService.this,
new DownloadStatus(
savedFeedRef.getImage(),
savedFeedRef
.getImage()
.getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR,
false, e.getMessage()));
}
}
});
} }
} catch (SAXException e) { } catch (SAXException e) {
@ -617,7 +643,14 @@ public class DownloadService extends Service {
reasonDetailed)); reasonDetailed));
sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED); sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED);
downloadsBeingHandled -= 1; downloadsBeingHandled -= 1;
queryDownloads(); handler.post(new Runnable() {
@Override
public void run() {
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;
queryDownloads(); handler.post(new Runnable() {
@Override
public void run() {
queryDownloads();
}
});
} }
} }
@ -750,7 +790,14 @@ public class DownloadService extends Service {
} }
downloadsBeingHandled -= 1; downloadsBeingHandled -= 1;
queryDownloads(); handler.post(new Runnable() {
@Override
public void run() {
queryDownloads();
}
});
} }
} }