From 3b5e83c74f17a318162f39a2004fe33207de1f10 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Mon, 17 Mar 2014 13:02:37 +0100 Subject: Added authentication support to DownloadRequester and HttpDownloader --- .../antennapod/storage/DownloadRequester.java | 77 +++++++++++++++------- 1 file changed, 52 insertions(+), 25 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 013162f0c..ba112b662 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -1,28 +1,28 @@ package de.danoeh.antennapod.storage; -import java.io.File; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.lang3.StringUtils; - import android.content.Context; import android.content.Intent; import android.util.Log; import android.webkit.URLUtil; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.feed.EventDistributor; -import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedFile; -import de.danoeh.antennapod.feed.FeedImage; -import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.feed.*; import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.download.DownloadRequest; import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.util.FileNameGenerator; import de.danoeh.antennapod.util.URLChecker; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.File; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Sends download requests to the DownloadService. This class should always be used for starting downloads, + * otherwise they won't work correctly. + */ public class DownloadRequester { private static final String TAG = "DownloadRequester"; @@ -45,8 +45,34 @@ public class DownloadRequester { return downloader; } + /** + * Starts a new download with the given DownloadRequest. This method should only + * be used from outside classes if the DownloadRequest was created by the DownloadService to + * ensure that the data is valid. Use downloadFeed(), downloadImage() or downloadMedia() instead. + * + * @param context Context object for starting the DownloadService + * @param request The DownloadRequest. If another DownloadRequest with the same source URL is already stored, this method + * call will return false. + * @return True if the download request was accepted, false otherwise. + */ + public boolean download(Context context, DownloadRequest request) { + if (context == null) throw new IllegalArgumentException("context = null"); + if (request == null) throw new IllegalArgumentException("request = null"); + if (downloads.containsKey(request.getSource())) { + if (AppConfig.DEBUG) Log.i(TAG, "DownloadRequest is already stored."); + return false; + } + downloads.put(request.getSource(), request); + + Intent launchIntent = new Intent(context, DownloadService.class); + launchIntent.putExtra(DownloadService.EXTRA_REQUEST, request); + context.startService(launchIntent); + EventDistributor.getInstance().sendDownloadQueuedBroadcast(); + return true; + } + private void download(Context context, FeedFile item, File dest, - boolean overwriteIfExists) { + boolean overwriteIfExists, String username, String password) { if (!isDownloadingFile(item)) { if (!isFilenameAvailable(dest.toString()) || dest.exists()) { if (AppConfig.DEBUG) @@ -88,14 +114,9 @@ public class DownloadRequester { DownloadRequest request = new DownloadRequest(dest.toString(), item.getDownload_url(), item.getHumanReadableIdentifier(), - item.getId(), item.getTypeAsInt()); - - downloads.put(request.getSource(), request); + item.getId(), item.getTypeAsInt(), username, password); - Intent launchIntent = new Intent(context, DownloadService.class); - launchIntent.putExtra(DownloadService.EXTRA_REQUEST, request); - context.startService(launchIntent); - EventDistributor.getInstance().sendDownloadQueuedBroadcast(); + download(context, request); } else { Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded"); @@ -124,8 +145,11 @@ public class DownloadRequester { public void downloadFeed(Context context, Feed feed) throws DownloadRequestException { if (feedFileValid(feed)) { + String username = (feed.getPreferences() != null) ? feed.getPreferences().getUsername() : null; + String password = (feed.getPreferences() != null) ? feed.getPreferences().getPassword() : null; + download(context, feed, new File(getFeedfilePath(context), - getFeedfileName(feed)), true); + getFeedfileName(feed)), true, username, password); } } @@ -133,7 +157,7 @@ public class DownloadRequester { throws DownloadRequestException { if (feedFileValid(image)) { download(context, image, new File(getImagefilePath(context), - getImagefileName(image)), true); + getImagefileName(image)), true, null, null); } } @@ -142,7 +166,8 @@ public class DownloadRequester { if (feedFileValid(feedmedia)) { download(context, feedmedia, new File(getMediafilePath(context, feedmedia), - getMediafilename(feedmedia)), false); + getMediafilename(feedmedia)), false, null, null + ); } } @@ -278,7 +303,8 @@ public class DownloadRequester { context, MEDIA_DOWNLOADPATH + FileNameGenerator.generateFileName(media.getItem() - .getFeed().getTitle()) + "/"); + .getFeed().getTitle()) + "/" + ); return externalStorage.toString(); } @@ -305,7 +331,8 @@ public class DownloadRequester { } String URLBaseFilename = URLUtil.guessFileName(media.getDownload_url(), - null, media.getMime_type());; + null, media.getMime_type()); + ; if (titleBaseFilename != "") { // Append extension -- cgit v1.2.3 From e8b9e49ee9527f4e533c08cb7ac39d1dc8a19b6b Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 19 Mar 2014 20:27:12 +0100 Subject: Added support for images in itunes:image tags. --- src/de/danoeh/antennapod/storage/DownloadRequester.java | 6 +++--- 1 file changed, 3 insertions(+), 3 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 ba112b662..357f44faa 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -157,7 +157,7 @@ public class DownloadRequester { throws DownloadRequestException { if (feedFileValid(image)) { download(context, image, new File(getImagefilePath(context), - getImagefileName(image)), true, null, null); + getImagefileName(image)), false, null, null); } } @@ -291,8 +291,8 @@ public class DownloadRequester { public String getImagefileName(FeedImage image) { String filename = image.getDownload_url(); - if (image.getFeed() != null && image.getFeed().getTitle() != null) { - filename = image.getFeed().getTitle(); + if (image.getOwner() != null && image.getOwner().getHumanReadableIdentifier() != null) { + filename = image.getOwner().getHumanReadableIdentifier(); } return "image-" + FileNameGenerator.generateFileName(filename); } -- cgit v1.2.3 From b2de7512f4c871e882760bfe8bffa27d10c1c68a Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Fri, 28 Mar 2014 21:16:27 +0100 Subject: Replaced AppConfig.DEBUG with BuildConfig.DEBUG --- .../antennapod/storage/DownloadRequester.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 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 357f44faa..0a1747253 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -4,7 +4,7 @@ import android.content.Context; import android.content.Intent; import android.util.Log; import android.webkit.URLUtil; -import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.feed.*; import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.download.DownloadRequest; @@ -59,7 +59,7 @@ public class DownloadRequester { if (context == null) throw new IllegalArgumentException("context = null"); if (request == null) throw new IllegalArgumentException("request = null"); if (downloads.containsKey(request.getSource())) { - if (AppConfig.DEBUG) Log.i(TAG, "DownloadRequest is already stored."); + if (BuildConfig.DEBUG) Log.i(TAG, "DownloadRequest is already stored."); return false; } downloads.put(request.getSource(), request); @@ -75,11 +75,11 @@ public class DownloadRequester { boolean overwriteIfExists, String username, String password) { if (!isDownloadingFile(item)) { if (!isFilenameAvailable(dest.toString()) || dest.exists()) { - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "Filename already used."); if (isFilenameAvailable(dest.toString()) && overwriteIfExists) { boolean result = dest.delete(); - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "Deleting file. Result: " + result); } else { // find different name @@ -91,12 +91,12 @@ public class DownloadRequester { + i + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(dest.getName()); - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "Testing filename " + newName); newDest = new File(dest.getParent(), newName); if (!newDest.exists() && isFilenameAvailable(newDest.toString())) { - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "File doesn't exist yet. Using " + newName); break; @@ -107,7 +107,7 @@ public class DownloadRequester { } } } - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "Requesting download of url " + item.getDownload_url()); item.setDownload_url(URLChecker.prepareURL(item.getDownload_url())); @@ -131,13 +131,13 @@ public class DownloadRequester { for (String key : downloads.keySet()) { DownloadRequest r = downloads.get(key); if (StringUtils.equals(r.getDestination(), path)) { - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, path + " is already used by another requested download"); return false; } } - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, path + " is available as a download destination"); return true; } @@ -198,7 +198,7 @@ public class DownloadRequester { * Cancels a running download. */ public void cancelDownload(final Context context, final String downloadUrl) { - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "Cancelling download with url " + downloadUrl); Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD); cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, downloadUrl); @@ -209,7 +209,7 @@ public class DownloadRequester { * Cancels all running downloads */ public void cancelAllDownloads(Context context) { - if (AppConfig.DEBUG) + if (BuildConfig.DEBUG) Log.d(TAG, "Cancelling all running downloads"); context.sendBroadcast(new Intent( DownloadService.ACTION_CANCEL_ALL_DOWNLOADS)); -- cgit v1.2.3