Created Methods for handling completed Downloads

This commit is contained in:
Daniel Oeh 2011-12-23 20:47:48 +01:00
parent c9283f09dc
commit b0b069a20c
5 changed files with 61 additions and 7 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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;
}
}