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,10 +249,6 @@ 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() {
@Override
public void run() {
for (Downloader d : downloads) { for (Downloader d : downloads) {
d.interrupt(); d.interrupt();
DownloadRequester.getInstance().removeDownload( DownloadRequester.getInstance().removeDownload(
@ -262,10 +258,7 @@ public class DownloadService extends Service {
Log.d(TAG, "Cancelled all downloads"); Log.d(TAG, "Cancelled all downloads");
} }
downloads.clear(); downloads.clear();
sendBroadcast(new Intent( sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
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,18 +370,11 @@ 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() {
@Override
public void run() {
downloads.remove(d); downloads.remove(d);
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));
} }
});
}
/** /**
* Adds a new DownloadStatus object to the list of completed downloads and * Adds a new DownloadStatus object to the list of completed downloads and

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 */