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

@ -316,8 +316,10 @@ public class DownloadService extends Service {
@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<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
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,19 +581,30 @@ public class DownloadService extends Service {
if (AppConfig.DEBUG)
Log.d(TAG, "Feed has image; Downloading....");
savedFeed.getImage().setFeed(savedFeed);
final Feed savedFeedRef = savedFeed;
handler.post(new Runnable() {
@Override
public void run() {
try {
requester.downloadImage(DownloadService.this,
savedFeed.getImage());
savedFeedRef.getImage());
} catch (DownloadRequestException e) {
e.printStackTrace();
manager.addDownloadStatus(DownloadService.this,
new DownloadStatus(savedFeed.getImage(),
savedFeed.getImage()
manager.addDownloadStatus(
DownloadService.this,
new DownloadStatus(
savedFeedRef.getImage(),
savedFeedRef
.getImage()
.getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR,
false, e.getMessage()));
}
}
});
}
} catch (SAXException e) {
successful = false;
@ -617,7 +643,14 @@ public class DownloadService extends Service {
reasonDetailed));
sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED);
downloadsBeingHandled -= 1;
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;
handler.post(new Runnable() {
@Override
public void run() {
queryDownloads();
}
});
}
}
@ -750,7 +790,14 @@ public class DownloadService extends Service {
}
downloadsBeingHandled -= 1;
handler.post(new Runnable() {
@Override
public void run() {
queryDownloads();
}
});
}
}