summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/danoeh/antennapod')
-rw-r--r--src/de/danoeh/antennapod/service/download/DownloadService.java59
-rw-r--r--src/de/danoeh/antennapod/service/download/Downloader.java6
-rw-r--r--src/de/danoeh/antennapod/storage/DownloadRequester.java26
3 files changed, 61 insertions, 30 deletions
diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java
index 68117b96c..7e165bd9d 100644
--- a/src/de/danoeh/antennapod/service/download/DownloadService.java
+++ b/src/de/danoeh/antennapod/service/download/DownloadService.java
@@ -71,11 +71,15 @@ public class DownloadService extends Service {
* If the DownloadService receives this intent, it will execute
* 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_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 EXTRA_DOWNLOAD_ID = "extra.de.danoeh.antennapod.service.download_id";
/** Extra for ACTION_ENQUEUE_DOWNLOAD intent. */
@@ -133,10 +137,14 @@ public class DownloadService extends Service {
isRunning = true;
completedDownloads = new ArrayList<DownloadStatus>();
downloads = new ArrayList<Downloader>();
- registerReceiver(onDownloadsChanged, new IntentFilter(
- ACTION_NOTIFY_DOWNLOADS_CHANGED));
+
registerReceiver(downloadQueued, new IntentFilter(
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() {
@Override
@@ -188,7 +196,7 @@ public class DownloadService extends Service {
Log.d(TAG, "Service shutting down");
isRunning = false;
mediaplayer.release();
- unregisterReceiver(onDownloadsChanged);
+ unregisterReceiver(cancelDownloadReceiver);
unregisterReceiver(downloadQueued);
downloadObserver.cancel(true);
createReport();
@@ -243,14 +251,46 @@ public class DownloadService extends Service {
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
public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(ACTION_NOTIFY_DOWNLOADS_CHANGED)) {
- queryDownloads();
+ if (intent.getAction().equals(ACTION_CANCEL_DOWNLOAD)) {
+ 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() {
@@ -348,6 +388,7 @@ public class DownloadService extends Service {
private void removeDownload(DownloadStatus status) {
downloads.remove(status);
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 */
void queryDownloads() {
- int numOfDownloads = requester.getNumberOfDownloads();
+ int numOfDownloads = downloads.size();
if (!shutdownInitiated && numOfDownloads == 0) {
shutdownInitiated = true;
initiateShutdown();
diff --git a/src/de/danoeh/antennapod/service/download/Downloader.java b/src/de/danoeh/antennapod/service/download/Downloader.java
index 5871c3ef1..423ecfc06 100644
--- a/src/de/danoeh/antennapod/service/download/Downloader.java
+++ b/src/de/danoeh/antennapod/service/download/Downloader.java
@@ -37,7 +37,7 @@ public abstract class Downloader extends Thread {
});
}
}
-
+
protected void publishProgress() {
status.setUpdateAvailable(true);
}
@@ -49,4 +49,8 @@ public abstract class Downloader extends Thread {
download();
}
+ public DownloadStatus getStatus() {
+ return status;
+ }
+
} \ No newline at end of file
diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java
index 9399de465..687e402e0 100644
--- a/src/de/danoeh/antennapod/storage/DownloadRequester.java
+++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java
@@ -109,28 +109,19 @@ public class DownloadRequester {
/**
* 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)
- Log.d(TAG, "Cancelling download with url " + download_url);
- FeedFile download = downloads.remove(download_url);
- if (download != null) {
- download.setFile_url(null);
- notifyDownloadService(context);
- }
+ Log.d(TAG, "Cancelling download with url " + downloadUrl);
+ Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD);
+ cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, downloadUrl);
}
/** Cancels all running downloads */
public void cancelAllDownloads(Context context) {
if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling all running downloads");
- DownloadManager dm = (DownloadManager) context
- .getSystemService(Context.DOWNLOAD_SERVICE);
- for (FeedFile f : downloads.values()) {
- dm.remove(f.getDownloadId());
- f.setFile_url(null);
- }
- downloads.clear();
- notifyDownloadService(context);
+ context.sendBroadcast(new Intent(
+ DownloadService.ACTION_CANCEL_ALL_DOWNLOADS));
}
/** Returns true if there is at least one Feed in the downloads queue. */
@@ -206,9 +197,4 @@ public class DownloadRequester {
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));
- }
}