From 30017a316e1a514e2441f45ab1545442fe94c0b3 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Mon, 16 Jun 2014 01:22:46 +0200 Subject: Implemented Feed-discovery in OnlineFeedView --- .../activity/OnlineFeedViewActivity.java | 81 +++++++++++++++++++++- .../antennapod/syndication/handler/TypeGetter.java | 18 ++++- .../handler/UnsupportedFeedtypeException.java | 19 +++-- 3 files changed, 108 insertions(+), 10 deletions(-) (limited to 'src/de') 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 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 titles = new ArrayList(); + final List urls = new ArrayList(); + + urls.addAll(urlsMap.keySet()); + for (String url : urls) { + titles.add(urlsMap.get(url)); + } + + final ArrayAdapter adapter = new ArrayAdapter(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"; -- cgit v1.2.3