From b0b069a20c808319331fe550854eee45614d2797 Mon Sep 17 00:00:00 2001 From: Daniel Oeh Date: Fri, 23 Dec 2011 20:47:48 +0100 Subject: [PATCH] Created Methods for handling completed Downloads --- src/de/podfetcher/feed/FeedHandler.java | 8 ++++--- src/de/podfetcher/feed/FeedManager.java | 17 +++++++++++++ src/de/podfetcher/feed/RSSHandler.java | 9 ++++++- .../podfetcher/storage/DownloadReceiver.java | 24 +++++++++++++++++-- .../podfetcher/storage/DownloadRequester.java | 10 +++++++- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/de/podfetcher/feed/FeedHandler.java b/src/de/podfetcher/feed/FeedHandler.java index 304799c7a..385bd0139 100644 --- a/src/de/podfetcher/feed/FeedHandler.java +++ b/src/de/podfetcher/feed/FeedHandler.java @@ -24,16 +24,18 @@ public class FeedHandler { public final static String ENC_LEN = "length"; public final static String ENC_TYPE = "type"; - public Feed parseFeed(String file) throws ParserConfigurationException, SAXException { + public Feed parseFeed(Feed feed) { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); - RSSHandler handler = new RSSHandler(); + RSSHandler handler = new RSSHandler(feed); try { - saxParser.parse(new File(file), handler); + saxParser.parse(new File(feed.file_url), handler); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); + } catch(ParserConfigurationException e) { + e.printStackTrace(); } return handler.feed; diff --git a/src/de/podfetcher/feed/FeedManager.java b/src/de/podfetcher/feed/FeedManager.java index fde532694..7a5579055 100644 --- a/src/de/podfetcher/feed/FeedManager.java +++ b/src/de/podfetcher/feed/FeedManager.java @@ -42,12 +42,29 @@ public class FeedManager { // TODO Check if URL is correct PodDBAdapter adapter = new PodDBAdapter(context); Feed feed = new Feed(url); + feed.download_url = url; feed.id = adapter.setFeed(feed); DownloadRequester req = DownloadRequester.getInstance(); req.downloadFeed(context, feed); } + + /** Updates Information of an existing Feed */ + public void setFeed(Context context, Feed feed) { + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.setFeed(feed); + } + + /** Get a Feed by its id */ + public Feed getFeed(long id) { + for(Feed f : feeds) { + if(f.id == id) { + return f; + } + } + return null; + } /** Reads the database */ public void loadDBData(Context context) { diff --git a/src/de/podfetcher/feed/RSSHandler.java b/src/de/podfetcher/feed/RSSHandler.java index 1176107b9..7456026c8 100644 --- a/src/de/podfetcher/feed/RSSHandler.java +++ b/src/de/podfetcher/feed/RSSHandler.java @@ -21,6 +21,11 @@ public class RSSHandler extends DefaultHandler { public String active_root_element; // channel or item or image public String active_sub_element; // Not channel or item + public RSSHandler(Feed f) { + super(); + this.feed = f; + } + @Override public void characters(char[] ch, int start, int length) throws SAXException { @@ -86,7 +91,9 @@ public class RSSHandler extends DefaultHandler { public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase(FeedHandler.CHANNEL)) { - feed = new Feed(); + if(feed == null) { + feed = new Feed(); + } active_root_element = qName; } else if (qName.equalsIgnoreCase(FeedHandler.ITEM)) { currentItem = new FeedItem(); diff --git a/src/de/podfetcher/storage/DownloadReceiver.java b/src/de/podfetcher/storage/DownloadReceiver.java index 1d06b51f7..872f539ed 100644 --- a/src/de/podfetcher/storage/DownloadReceiver.java +++ b/src/de/podfetcher/storage/DownloadReceiver.java @@ -7,15 +7,18 @@ import android.content.Context; import android.content.Intent; public class DownloadReceiver extends BroadcastReceiver { + private DownloadRequester requester; + private FeedManager manager; @Override public void onReceive(Context context, Intent intent) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); - DownloadRequester requester = DownloadRequester.getInstance(); + requester = DownloadRequester.getInstance(); + manager = FeedManager.getInstance(); Intent item_intent = requester.getItemIntent(id); String action = item_intent.getAction(); if(action.equals(DownloadRequester.ACTION_FEED_DOWNLOAD_COMPLETED)) { - requester.removeFeedByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1)); + handleCompletedFeedDownload(context, intent); } else if(action.equals(DownloadRequester.ACTION_MEDIA_DOWNLOAD_COMPLETED)) { requester.removeMediaByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1)); } else if(action.equals(DownloadRequester.ACTION_IMAGE_DOWNLOAD_COMPLETED)) { @@ -24,4 +27,21 @@ public class DownloadReceiver extends BroadcastReceiver { PodcastApp.getInstance().getApplicationContext().sendBroadcast(item_intent); } + /** Is called whenever a Feed is Downloaded */ + private void handleCompletedFeedDownload(Context context, Intent intent) { + RSSHandler handler = new RSSHandler(); + + requester.removeFeedByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1)); + // Get Feed Information + Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1)); + feed.file_url = DownloadRequester.getFeedfilePath() + DownloadRequester.getFeedfileName(feed.id); + feed = handler.parseFeed(feed); + // Download Feed Image if provided + if(feed.image != null) { + requester.downloadImage(context, feed.image); + } + // Update Information in Database + manager.setFeed(feed); + } + } diff --git a/src/de/podfetcher/storage/DownloadRequester.java b/src/de/podfetcher/storage/DownloadRequester.java index 6161e7ac3..407555ee0 100644 --- a/src/de/podfetcher/storage/DownloadRequester.java +++ b/src/de/podfetcher/storage/DownloadRequester.java @@ -63,7 +63,7 @@ public class DownloadRequester { } public void downloadFeed(Context context, Feed feed) { download(context, feeds, feed.download_url, - new File(context.getExternalFilesDir(FEED_DOWNLOADPATH), "feed-" + feed.id), + new File(getFeedfilePath(id), getFeedfileName(id)), true, ACTION_FEED_DOWNLOAD_COMPLETED, feed.id); } @@ -128,4 +128,12 @@ public class DownloadRequester { } return null; } + + public String getFeedfilePath() { + return context.getExternalFilesDir(FEED_DOWNLOADPATH); + } + + public String getFeedfileName(long id) { + return "feed-" + id; + } }