Made sure that cancelled/failed downloads are handled correctly

This commit is contained in:
daniel oeh 2012-08-18 00:47:52 +02:00
parent 026efe29c3
commit bcbc162412
2 changed files with 21 additions and 31 deletions

View File

@ -249,23 +249,16 @@ public class DownloadService extends Service {
} }
} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) { } else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
handler.post(new Runnable() { for (Downloader d : downloads) {
d.interrupt();
@Override DownloadRequester.getInstance().removeDownload(
public void run() { d.getStatus().getFeedFile());
for (Downloader d : downloads) { d.getStatus().getFeedFile().setFile_url(null);
d.interrupt(); if (AppConfig.DEBUG)
DownloadRequester.getInstance().removeDownload( Log.d(TAG, "Cancelled all downloads");
d.getStatus().getFeedFile()); }
d.getStatus().getFeedFile().setFile_url(null); downloads.clear();
if (AppConfig.DEBUG) sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
Log.d(TAG, "Cancelled all downloads");
}
downloads.clear();
sendBroadcast(new Intent(
ACTION_DOWNLOADS_CONTENT_CHANGED));
}
});
} }
queryDownloads(); queryDownloads();
@ -327,7 +320,7 @@ public class DownloadService extends Service {
@SuppressLint("NewApi") @SuppressLint("NewApi")
public void onDownloadCompleted(final Downloader downloader) { public void onDownloadCompleted(final Downloader downloader) {
AsyncTask<Void, Void, Void> handler = new AsyncTask<Void, Void, Void>() { final AsyncTask<Void, Void, Void> handlerTask = new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
@ -359,13 +352,16 @@ public class DownloadService extends Service {
} }
} }
removeDownload(downloader); removeDownload(downloader);
if (!successful) {
queryDownloads();
}
return null; return null;
} }
}; };
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
handler.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); handlerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else { } else {
handler.execute(); handlerTask.execute();
} }
} }
@ -374,17 +370,10 @@ public class DownloadService extends Service {
* DownloadService list. * DownloadService list.
*/ */
private void removeDownload(final Downloader d) { private void removeDownload(final Downloader d) {
handler.post(new Runnable() { downloads.remove(d);
DownloadRequester.getInstance().removeDownload(
@Override d.getStatus().getFeedFile());
public void run() { sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
downloads.remove(d);
DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile());
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
});
} }
/** /**

View File

@ -109,6 +109,7 @@ public class DownloadRequester {
Log.d(TAG, "Cancelling download with url " + downloadUrl); Log.d(TAG, "Cancelling download with url " + downloadUrl);
Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD); Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD);
cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, downloadUrl); cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, downloadUrl);
context.sendBroadcast(cancelIntent);
} }
/** Cancels all running downloads */ /** Cancels all running downloads */