From 92feabbb5d2cf1362812a5c71bc27d9b8820093f Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 23 Sep 2012 16:40:44 +0200 Subject: DownloadRequester now throws an exception if request is invalid --- .../storage/DownloadRequestException.java | 25 +++++++ .../antennapod/storage/DownloadRequester.java | 82 ++++++++++++++++------ 2 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 src/de/danoeh/antennapod/storage/DownloadRequestException.java (limited to 'src/de/danoeh/antennapod/storage') diff --git a/src/de/danoeh/antennapod/storage/DownloadRequestException.java b/src/de/danoeh/antennapod/storage/DownloadRequestException.java new file mode 100644 index 000000000..0ef766e58 --- /dev/null +++ b/src/de/danoeh/antennapod/storage/DownloadRequestException.java @@ -0,0 +1,25 @@ +package de.danoeh.antennapod.storage; + +/** + * Thrown by the DownloadRequester if a download request contains invalid data + * or something went wrong while processing the request. + */ +public class DownloadRequestException extends Exception { + + public DownloadRequestException() { + super(); + } + + public DownloadRequestException(String detailMessage, Throwable throwable) { + super(detailMessage, throwable); + } + + public DownloadRequestException(String detailMessage) { + super(detailMessage); + } + + public DownloadRequestException(Throwable throwable) { + super(throwable); + } + +} diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 13c8f58d0..928b923fd 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -60,7 +60,7 @@ public class DownloadRequester { DownloadService.Request request = new DownloadService.Request( item.getFile_url(), item.getDownload_url()); - + if (!DownloadService.isRunning) { Intent launchIntent = new Intent(context, DownloadService.class); launchIntent.putExtra(DownloadService.EXTRA_REQUEST, request); @@ -78,20 +78,44 @@ public class DownloadRequester { } } - public void downloadFeed(Context context, Feed feed) { - download(context, feed, new File(getFeedfilePath(context), - getFeedfileName(feed))); + public void downloadFeed(Context context, Feed feed) + throws DownloadRequestException { + if (feedFileValid(feed)) { + download(context, feed, new File(getFeedfilePath(context), + getFeedfileName(feed))); + } + } + + public void downloadImage(Context context, FeedImage image) + throws DownloadRequestException { + if (feedFileValid(image)) { + download(context, image, new File(getImagefilePath(context), + getImagefileName(image))); + } } - public void downloadImage(Context context, FeedImage image) { - download(context, image, new File(getImagefilePath(context), - getImagefileName(image))); + public void downloadMedia(Context context, FeedMedia feedmedia) throws DownloadRequestException { + if (feedFileValid(feedmedia)) { + download(context, feedmedia, + new File(getMediafilePath(context, feedmedia), + getMediafilename(feedmedia))); + } } - public void downloadMedia(Context context, FeedMedia feedmedia) { - download(context, feedmedia, - new File(getMediafilePath(context, feedmedia), - getMediafilename(feedmedia))); + /** + * Throws a DownloadRequestException if the feedfile or the download url of + * the feedfile is null. + * + * @throws DownloadRequestException + */ + private boolean feedFileValid(FeedFile f) throws DownloadRequestException { + if (f == null) { + throw new DownloadRequestException("Feedfile was null"); + } else if (f.getDownload_url() == null) { + throw new DownloadRequestException("File has no download URL"); + } else { + return true; + } } /** @@ -158,7 +182,8 @@ public class DownloadRequester { /** Remove an object from the downloads-list of the requester. */ public void removeDownload(FeedFile f) { if (downloads.remove(f.getDownload_url()) == null) { - Log.e(TAG, "Could not remove object with url " + f.getDownload_url()); + Log.e(TAG, + "Could not remove object with url " + f.getDownload_url()); } } @@ -167,29 +192,46 @@ public class DownloadRequester { return downloads.size(); } - public String getFeedfilePath(Context context) { - return context.getExternalFilesDir(FEED_DOWNLOADPATH).toString() + "/"; + public String getFeedfilePath(Context context) + throws DownloadRequestException { + return getExternalFilesDirOrThrowException(context, FEED_DOWNLOADPATH) + .toString() + "/"; } public String getFeedfileName(Feed feed) { return "feed-" + NumberGenerator.generateLong(feed.getDownload_url()); } - public String getImagefilePath(Context context) { - return context.getExternalFilesDir(IMAGE_DOWNLOADPATH).toString() + "/"; + public String getImagefilePath(Context context) + throws DownloadRequestException { + return getExternalFilesDirOrThrowException(context, IMAGE_DOWNLOADPATH) + .toString() + "/"; } public String getImagefileName(FeedImage image) { return "image-" + NumberGenerator.generateLong(image.getDownload_url()); } - public String getMediafilePath(Context context, FeedMedia media) { - File externalStorage = context.getExternalFilesDir(MEDIA_DOWNLOADPATH - + NumberGenerator.generateLong(media.getItem().getFeed() - .getTitle()) + "/"); + public String getMediafilePath(Context context, FeedMedia media) + throws DownloadRequestException { + File externalStorage = getExternalFilesDirOrThrowException( + context, + MEDIA_DOWNLOADPATH + + NumberGenerator.generateLong(media.getItem() + .getFeed().getTitle()) + "/"); return externalStorage.toString(); } + private File getExternalFilesDirOrThrowException(Context context, + String type) throws DownloadRequestException { + File result = context.getExternalFilesDir(type); + if (result == null) { + throw new DownloadRequestException( + "Failed to access external storage"); + } + return result; + } + public String getMediafilename(FeedMedia media) { return URLUtil.guessFileName(media.getDownload_url(), null, media.getMime_type()); -- cgit v1.2.3