From dbf16830fa8fc40624943135bcbe1b8aa7be6835 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Thu, 19 Jul 2012 14:36:40 +0200 Subject: Downloadrequester will now no longer queue downloads which are already running --- .../antennapod/storage/DownloadRequester.java | 58 ++++++++++++---------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'src/de/danoeh/antennapod/storage/DownloadRequester.java') diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 508df0a8c..e5a980f22 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -58,33 +58,38 @@ public class DownloadRequester {// TODO handle externalstorage missing @SuppressLint("NewApi") private long download(Context context, FeedFile item, File dest) { - if (dest.exists()) { - Log.d(TAG, "File already exists. Deleting !"); - dest.delete(); - } - Log.d(TAG, "Requesting download of url " + item.getDownload_url()); - downloads.add(item); - DownloadManager.Request request = new DownloadManager.Request( - Uri.parse(item.getDownload_url())).setDestinationUri(Uri - .fromFile(dest)); - Log.d(TAG, "Version is " + currentApi); - if (currentApi >= 11) { - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); - } else { - request.setVisibleInDownloadsUi(false); - request.setShowRunningNotification(false); - } + if (!isDownloadingFile(item)) { + if (dest.exists()) { + Log.d(TAG, "File already exists. Deleting !"); + dest.delete(); + } + Log.d(TAG, "Requesting download of url " + item.getDownload_url()); + downloads.add(item); + DownloadManager.Request request = new DownloadManager.Request( + Uri.parse(item.getDownload_url())).setDestinationUri(Uri + .fromFile(dest)); + Log.d(TAG, "Version is " + currentApi); + if (currentApi >= 11) { + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); + } else { + request.setVisibleInDownloadsUi(false); + request.setShowRunningNotification(false); + } - // TODO Set Allowed Network Types - DownloadManager manager = (DownloadManager) context - .getSystemService(Context.DOWNLOAD_SERVICE); + // TODO Set Allowed Network Types + DownloadManager manager = (DownloadManager) context + .getSystemService(Context.DOWNLOAD_SERVICE); - long downloadId = manager.enqueue(request); - item.setDownloadId(downloadId); - item.setFile_url(dest.toString()); - context.startService(new Intent(context, DownloadService.class)); - context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); - return downloadId; + long downloadId = manager.enqueue(request); + item.setDownloadId(downloadId); + item.setFile_url(dest.toString()); + context.startService(new Intent(context, DownloadService.class)); + context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); + return downloadId; + } else { + Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded"); + return 0; + } } public long downloadFeed(Context context, Feed feed) { @@ -203,7 +208,8 @@ public class DownloadRequester {// TODO handle externalstorage missing public String getMediafilePath(Context context, FeedMedia media) { File externalStorage = context.getExternalFilesDir(MEDIA_DOWNLOADPATH - + NumberGenerator.generateLong(media.getItem().getFeed().getTitle()) + "/"); + + NumberGenerator.generateLong(media.getItem().getFeed() + .getTitle()) + "/"); return externalStorage.toString(); } -- cgit v1.2.3