diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-06-16 01:22:46 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-06-16 01:22:46 +0200 |
commit | 30017a316e1a514e2441f45ab1545442fe94c0b3 (patch) | |
tree | fec694a07dc4f70f82184bbe14fce51891771477 /src/de | |
parent | 7fc0e73ea7bcf21f843a0d94426e8df515182271 (diff) | |
download | AntennaPod-30017a316e1a514e2441f45ab1545442fe94c0b3.zip |
Implemented Feed-discovery in OnlineFeedView
Diffstat (limited to 'src/de')
3 files changed, 108 insertions, 10 deletions
diff --git a/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java b/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java index 322c32741..2c6d75cd8 100644 --- a/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java +++ b/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java @@ -9,6 +9,7 @@ import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; +import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.RelativeLayout; @@ -29,13 +30,16 @@ import de.danoeh.antennapod.util.DownloadError; import de.danoeh.antennapod.util.FileNameGenerator; import de.danoeh.antennapod.util.StorageUtils; import de.danoeh.antennapod.util.URLChecker; +import de.danoeh.antennapod.util.syndication.FeedDiscoverer; import org.apache.commons.lang3.StringUtils; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Map; /** @@ -127,6 +131,13 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { } } + private void resetIntent(String url, String title) { + Intent intent = new Intent(); + intent.putExtra(ARG_FEEDURL, url); + intent.putExtra(ARG_TITLE, title); + setIntent(intent); + } + private void onDownloadCompleted(final Downloader downloader) { runOnUiThread(new Runnable() { @@ -244,8 +255,15 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { e.printStackTrace(); reasonDetailed = e.getMessage(); } catch (UnsupportedFeedtypeException e) { - e.printStackTrace(); - reasonDetailed = e.getMessage(); + if (BuildConfig.DEBUG) Log.d(TAG, "Unsupported feed type detected"); + if (StringUtils.equalsIgnoreCase("html", e.getRootElement())) { + if (showFeedDiscoveryDialog(new File(feed.getFile_url()), feed.getDownload_url())) { + return; + } + } else { + e.printStackTrace(); + reasonDetailed = e.getMessage(); + } } finally { boolean rc = new File(feed.getFile_url()).delete(); if (BuildConfig.DEBUG) @@ -326,9 +344,67 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { finish(); } }); + builder.show(); } } + private boolean showFeedDiscoveryDialog(File feedFile, String baseUrl) { + FeedDiscoverer fd = new FeedDiscoverer(); + final Map<String, String> urlsMap; + try { + urlsMap = fd.findLinks(feedFile, baseUrl); + if (urlsMap == null || urlsMap.isEmpty()) { + return false; + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + runOnUiThread(new Runnable() { + @Override + public void run() { + if (isPaused || isFinishing()) { + return; + } + + final List<String> titles = new ArrayList<String>(); + final List<String> urls = new ArrayList<String>(); + + urls.addAll(urlsMap.keySet()); + for (String url : urls) { + titles.add(urlsMap.get(url)); + } + + final ArrayAdapter<String> adapter = new ArrayAdapter<String>(OnlineFeedViewActivity.this, R.layout.ellipsize_start_listitem, R.id.txtvTitle, titles); + DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String selectedUrl = urls.get(which); + dialog.dismiss(); + resetIntent(selectedUrl, titles.get(which)); + startFeedDownload(selectedUrl, null, null); + } + }; + + AlertDialog.Builder ab = new AlertDialog.Builder(OnlineFeedViewActivity.this) + .setTitle(R.string.feeds_label) + .setCancelable(true) + .setOnCancelListener(new OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + finish(); + } + }) + .setAdapter(adapter, onClickListener); + ab.show(); + } + }); + + + return true; + } + private class FeedViewAuthenticationDialog extends AuthenticationDialog { private String feedUrl; @@ -349,5 +425,4 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { startFeedDownload(feedUrl, username, password); } } - } diff --git a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java index 5ed9ff2b0..2496e112a 100644 --- a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java +++ b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java @@ -4,6 +4,8 @@ import android.util.Log; import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.feed.Feed; import org.apache.commons.io.input.XmlStreamReader; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; @@ -64,7 +66,7 @@ public class TypeGetter { } else { if (BuildConfig.DEBUG) Log.d(TAG, "Type is invalid"); - throw new UnsupportedFeedtypeException(Type.INVALID); + throw new UnsupportedFeedtypeException(Type.INVALID, tag); } } else { eventType = xpp.next(); @@ -73,7 +75,19 @@ public class TypeGetter { } catch (XmlPullParserException e) { e.printStackTrace(); - } catch (IOException e) { + // XML document might actually be a HTML document -> try to parse as HTML + String rootElement = null; + try { + if (Jsoup.parse(new File(feed.getFile_url()), null) != null) { + rootElement = "html"; + } + } catch (IOException e1) { + e1.printStackTrace(); + } finally { + throw new UnsupportedFeedtypeException(Type.INVALID, rootElement); + } + + } catch (IOException e) { e.printStackTrace(); } } diff --git a/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java b/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java index 67fbc9cc9..605dad2fb 100644 --- a/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java +++ b/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java @@ -5,18 +5,27 @@ import de.danoeh.antennapod.syndication.handler.TypeGetter.Type; public class UnsupportedFeedtypeException extends Exception { private static final long serialVersionUID = 9105878964928170669L; private TypeGetter.Type type; + private String rootElement; public UnsupportedFeedtypeException(Type type) { super(); this.type = type; - } - - public TypeGetter.Type getType() { + + public UnsupportedFeedtypeException(Type type, String rootElement) { + this.type = type; + this.rootElement = rootElement; + } + + public TypeGetter.Type getType() { return type; } - - @Override + + public String getRootElement() { + return rootElement; + } + + @Override public String getMessage() { if (type == TypeGetter.Type.INVALID) { return "Invalid type"; |