mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-12-01 12:31:45 +00:00
Created Methods for handling completed Downloads
This commit is contained in:
@ -24,16 +24,18 @@ public class FeedHandler {
|
|||||||
public final static String ENC_LEN = "length";
|
public final static String ENC_LEN = "length";
|
||||||
public final static String ENC_TYPE = "type";
|
public final static String ENC_TYPE = "type";
|
||||||
|
|
||||||
public Feed parseFeed(String file) throws ParserConfigurationException, SAXException {
|
public Feed parseFeed(Feed feed) {
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||||
SAXParser saxParser = factory.newSAXParser();
|
SAXParser saxParser = factory.newSAXParser();
|
||||||
RSSHandler handler = new RSSHandler();
|
RSSHandler handler = new RSSHandler(feed);
|
||||||
try {
|
try {
|
||||||
saxParser.parse(new File(file), handler);
|
saxParser.parse(new File(feed.file_url), handler);
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} catch(ParserConfigurationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return handler.feed;
|
return handler.feed;
|
||||||
|
|||||||
@ -42,12 +42,29 @@ public class FeedManager {
|
|||||||
// TODO Check if URL is correct
|
// TODO Check if URL is correct
|
||||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||||
Feed feed = new Feed(url);
|
Feed feed = new Feed(url);
|
||||||
|
feed.download_url = url;
|
||||||
feed.id = adapter.setFeed(feed);
|
feed.id = adapter.setFeed(feed);
|
||||||
|
|
||||||
DownloadRequester req = DownloadRequester.getInstance();
|
DownloadRequester req = DownloadRequester.getInstance();
|
||||||
req.downloadFeed(context, feed);
|
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 */
|
/** Reads the database */
|
||||||
public void loadDBData(Context context) {
|
public void loadDBData(Context context) {
|
||||||
|
|||||||
@ -21,6 +21,11 @@ public class RSSHandler extends DefaultHandler {
|
|||||||
public String active_root_element; // channel or item or image
|
public String active_root_element; // channel or item or image
|
||||||
public String active_sub_element; // Not channel or item
|
public String active_sub_element; // Not channel or item
|
||||||
|
|
||||||
|
public RSSHandler(Feed f) {
|
||||||
|
super();
|
||||||
|
this.feed = f;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void characters(char[] ch, int start, int length)
|
public void characters(char[] ch, int start, int length)
|
||||||
throws SAXException {
|
throws SAXException {
|
||||||
@ -86,7 +91,9 @@ public class RSSHandler extends DefaultHandler {
|
|||||||
public void startElement(String uri, String localName, String qName,
|
public void startElement(String uri, String localName, String qName,
|
||||||
Attributes attributes) throws SAXException {
|
Attributes attributes) throws SAXException {
|
||||||
if (qName.equalsIgnoreCase(FeedHandler.CHANNEL)) {
|
if (qName.equalsIgnoreCase(FeedHandler.CHANNEL)) {
|
||||||
feed = new Feed();
|
if(feed == null) {
|
||||||
|
feed = new Feed();
|
||||||
|
}
|
||||||
active_root_element = qName;
|
active_root_element = qName;
|
||||||
} else if (qName.equalsIgnoreCase(FeedHandler.ITEM)) {
|
} else if (qName.equalsIgnoreCase(FeedHandler.ITEM)) {
|
||||||
currentItem = new FeedItem();
|
currentItem = new FeedItem();
|
||||||
|
|||||||
@ -7,15 +7,18 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
public class DownloadReceiver extends BroadcastReceiver {
|
public class DownloadReceiver extends BroadcastReceiver {
|
||||||
|
private DownloadRequester requester;
|
||||||
|
private FeedManager manager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
|
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);
|
Intent item_intent = requester.getItemIntent(id);
|
||||||
String action = item_intent.getAction();
|
String action = item_intent.getAction();
|
||||||
if(action.equals(DownloadRequester.ACTION_FEED_DOWNLOAD_COMPLETED)) {
|
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)) {
|
} else if(action.equals(DownloadRequester.ACTION_MEDIA_DOWNLOAD_COMPLETED)) {
|
||||||
requester.removeMediaByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
requester.removeMediaByID(item_intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||||
} else if(action.equals(DownloadRequester.ACTION_IMAGE_DOWNLOAD_COMPLETED)) {
|
} else if(action.equals(DownloadRequester.ACTION_IMAGE_DOWNLOAD_COMPLETED)) {
|
||||||
@ -24,4 +27,21 @@ public class DownloadReceiver extends BroadcastReceiver {
|
|||||||
PodcastApp.getInstance().getApplicationContext().sendBroadcast(item_intent);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,7 +63,7 @@ public class DownloadRequester {
|
|||||||
}
|
}
|
||||||
public void downloadFeed(Context context, Feed feed) {
|
public void downloadFeed(Context context, Feed feed) {
|
||||||
download(context, feeds, feed.download_url,
|
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);
|
true, ACTION_FEED_DOWNLOAD_COMPLETED, feed.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,4 +128,12 @@ public class DownloadRequester {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFeedfilePath() {
|
||||||
|
return context.getExternalFilesDir(FEED_DOWNLOADPATH);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFeedfileName(long id) {
|
||||||
|
return "feed-" + id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user