Downloads can now be cancelled

This commit is contained in:
daniel oeh 2012-08-16 19:30:19 +02:00
parent e8894926ff
commit 697c00e0eb
3 changed files with 61 additions and 30 deletions

View File

@ -71,8 +71,12 @@ public class DownloadService extends Service {
* If the DownloadService receives this intent, it will execute * If the DownloadService receives this intent, it will execute
* queryDownloads() * queryDownloads()
*/ */
public static final String ACTION_NOTIFY_DOWNLOADS_CHANGED = "action.de.danoeh.antennapod.service.notifyDownloadsChanged";
public static final String ACTION_ENQUEUE_DOWNLOAD = "action.de.danoeh.antennapod.service.enqueueDownload"; public static final String ACTION_ENQUEUE_DOWNLOAD = "action.de.danoeh.antennapod.service.enqueueDownload";
public static final String ACTION_CANCEL_DOWNLOAD = "action.de.danoeh.antennapod.service.cancelDownload";
public static final String ACTION_CANCEL_ALL_DOWNLOADS = "action.de.danoeh.antennapod.service.cancelAllDownloads";
/** Extra for ACTION_CANCEL_DOWNLOAD */
public static final String EXTRA_DOWNLOAD_URL = "downloadUrl";
public static final String ACTION_DOWNLOAD_HANDLED = "action.de.danoeh.antennapod.service.download_handled"; public static final String ACTION_DOWNLOAD_HANDLED = "action.de.danoeh.antennapod.service.download_handled";
@ -133,10 +137,14 @@ public class DownloadService extends Service {
isRunning = true; isRunning = true;
completedDownloads = new ArrayList<DownloadStatus>(); completedDownloads = new ArrayList<DownloadStatus>();
downloads = new ArrayList<Downloader>(); downloads = new ArrayList<Downloader>();
registerReceiver(onDownloadsChanged, new IntentFilter(
ACTION_NOTIFY_DOWNLOADS_CHANGED));
registerReceiver(downloadQueued, new IntentFilter( registerReceiver(downloadQueued, new IntentFilter(
ACTION_ENQUEUE_DOWNLOAD)); ACTION_ENQUEUE_DOWNLOAD));
IntentFilter cancelDownloadReceiverFilter = new IntentFilter();
cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_ALL_DOWNLOADS);
cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD);
registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter);
syncExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { syncExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override @Override
@ -188,7 +196,7 @@ public class DownloadService extends Service {
Log.d(TAG, "Service shutting down"); Log.d(TAG, "Service shutting down");
isRunning = false; isRunning = false;
mediaplayer.release(); mediaplayer.release();
unregisterReceiver(onDownloadsChanged); unregisterReceiver(cancelDownloadReceiver);
unregisterReceiver(downloadQueued); unregisterReceiver(downloadQueued);
downloadObserver.cancel(true); downloadObserver.cancel(true);
createReport(); createReport();
@ -243,14 +251,46 @@ public class DownloadService extends Service {
Log.d(TAG, "Notification set up"); Log.d(TAG, "Notification set up");
} }
private BroadcastReceiver onDownloadsChanged = new BroadcastReceiver() { private Downloader getDownloader(String downloadUrl) {
for (Downloader downloader : downloads) {
if (downloader.getStatus().getFeedFile().getDownload_url()
.equals(downloadUrl)) {
return downloader;
}
}
return null;
}
private BroadcastReceiver cancelDownloadReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_NOTIFY_DOWNLOADS_CHANGED)) { if (intent.getAction().equals(ACTION_CANCEL_DOWNLOAD)) {
queryDownloads(); String url = intent.getStringExtra(EXTRA_DOWNLOAD_URL);
if (url == null) {
throw new IllegalArgumentException(
"ACTION_CANCEL_DOWNLOAD intent needs download url extra");
}
if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling download with url " + url);
Downloader d = getDownloader(url);
if (d != null) {
d.interrupt();
removeDownload(d.getStatus());
} else {
Log.e(TAG, "Could not cancel download with url " + url);
}
} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
for (Downloader d : downloads) {
d.interrupt();
removeDownload(d.getStatus());
if (AppConfig.DEBUG)
Log.d(TAG, "Cancelled all downloads");
} }
} }
}
}; };
private BroadcastReceiver downloadQueued = new BroadcastReceiver() { private BroadcastReceiver downloadQueued = new BroadcastReceiver() {
@ -348,6 +388,7 @@ public class DownloadService extends Service {
private void removeDownload(DownloadStatus status) { private void removeDownload(DownloadStatus status) {
downloads.remove(status); downloads.remove(status);
DownloadRequester.getInstance().removeDownload(status.getFeedFile()); DownloadRequester.getInstance().removeDownload(status.getFeedFile());
status.getFeedFile().setFile_url(null);
} }
/** /**
@ -444,7 +485,7 @@ public class DownloadService extends Service {
/** Check if there's something else to download, otherwise stop */ /** Check if there's something else to download, otherwise stop */
void queryDownloads() { void queryDownloads() {
int numOfDownloads = requester.getNumberOfDownloads(); int numOfDownloads = downloads.size();
if (!shutdownInitiated && numOfDownloads == 0) { if (!shutdownInitiated && numOfDownloads == 0) {
shutdownInitiated = true; shutdownInitiated = true;
initiateShutdown(); initiateShutdown();

View File

@ -49,4 +49,8 @@ public abstract class Downloader extends Thread {
download(); download();
} }
public DownloadStatus getStatus() {
return status;
}
} }

View File

@ -109,28 +109,19 @@ public class DownloadRequester {
/** /**
* Cancels a running download. * Cancels a running download.
* */ * */
public void cancelDownload(final Context context, final String download_url) { public void cancelDownload(final Context context, final String downloadUrl) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling download with url " + download_url); Log.d(TAG, "Cancelling download with url " + downloadUrl);
FeedFile download = downloads.remove(download_url); Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD);
if (download != null) { cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, downloadUrl);
download.setFile_url(null);
notifyDownloadService(context);
}
} }
/** Cancels all running downloads */ /** Cancels all running downloads */
public void cancelAllDownloads(Context context) { public void cancelAllDownloads(Context context) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling all running downloads"); Log.d(TAG, "Cancelling all running downloads");
DownloadManager dm = (DownloadManager) context context.sendBroadcast(new Intent(
.getSystemService(Context.DOWNLOAD_SERVICE); DownloadService.ACTION_CANCEL_ALL_DOWNLOADS));
for (FeedFile f : downloads.values()) {
dm.remove(f.getDownloadId());
f.setFile_url(null);
}
downloads.clear();
notifyDownloadService(context);
} }
/** Returns true if there is at least one Feed in the downloads queue. */ /** Returns true if there is at least one Feed in the downloads queue. */
@ -206,9 +197,4 @@ public class DownloadRequester {
media.getMime_type()); media.getMime_type());
} }
/** Notifies the DownloadService to check if there are any Downloads left */
public void notifyDownloadService(Context context) {
context.sendBroadcast(new Intent(
DownloadService.ACTION_NOTIFY_DOWNLOADS_CHANGED));
}
} }