diff options
author | malockin <malockin@gmail.com> | 2020-05-03 14:12:40 +0300 |
---|---|---|
committer | malockin <malockin@gmail.com> | 2020-05-03 14:12:40 +0300 |
commit | f634198794ee3e15fc02f677da1642c7df0f5271 (patch) | |
tree | 1095321ab24c506c2ad5be884fb808e5d19de563 /core/src | |
parent | c33669478a388489bcdbe114f8b7fa07ebcaa5af (diff) | |
download | AntennaPod-f634198794ee3e15fc02f677da1642c7df0f5271.zip |
Initial favorites export commit
Diffstat (limited to 'core/src')
4 files changed, 222 insertions, 0 deletions
diff --git a/core/src/main/assets/favorites-export-template.html b/core/src/main/assets/favorites-export-template.html new file mode 100644 index 000000000..ace80591b --- /dev/null +++ b/core/src/main/assets/favorites-export-template.html @@ -0,0 +1,82 @@ +<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
+<html>
+ <head>
+ <title>AntennaPod Favorites</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <style>
+ * {
+ font-family: 'Lato', sans-serif;
+ font-weight: 300;
+ margin: 0;
+ padding: 0;
+ box-sizing: border-box;
+ }
+ html {
+ background: #3498db;
+ text-align: center;
+ padding: 10px;
+ }
+ h1 {
+ color: #fff;
+ font-weight: 300;
+ display: inline-block;
+ margin-top: 40px;
+ margin-bottom: 20px;
+ vertical-align: top;
+ }
+ li {
+ width: 100%;
+ max-width: 500px;
+ display: block;
+ display: inline-flex;
+ padding: 10px;
+ }
+ li > div {
+ background: #fefefe;
+ padding: 10px;
+ display: inline-block;
+ width: 100%;
+ box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
+ text-align: left;
+ }
+ li span {
+ margin-top: 10px;
+ display: block;
+ }
+ a {
+ text-decoration: none;
+ }
+ a:hover {
+ text-decoration: underline;
+ }
+ span a {
+ color: #3498db;
+ }
+ img {
+ width: 100px;
+ height: 100px;
+ margin-right: 10px;
+ }
+ li > div > img {
+ float: left;
+ }
+ li > div > p {
+ width: 100%;
+ }
+ body > a {
+ color: #ffffff;
+ display: inline-block;
+ margin-top: 10px;
+ clear:left;
+ }
+ </style>
+ </head>
+ <body>
+ <img src="https://antennapod.org/assets/img/antennapod-logo.png" />
+ <h1>AntennaPod Favorites</h1>
+ <ul>
+ {FAVORITES}
+ </ul>
+ <a href="https://play.google.com/store/apps/details?id=de.danoeh.antennapod" target="_blank">Get AntennaPod</a>
+ </body>
+</html>
diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java b/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java new file mode 100644 index 000000000..a6686a8a4 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java @@ -0,0 +1,131 @@ +package de.danoeh.antennapod.core.export.favorites; + +import android.content.Context; +import android.util.Log; + +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Writer; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import de.danoeh.antennapod.core.export.ExportWriter; +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.storage.DBReader; + +/** Writes saved favorites to file */ +public class FavoritesWriter implements ExportWriter { + private static final String TAG = "FavoritesWriter"; + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + private static final int PAGE_LIMIT = 100; + + @Override + public void writeDocument(List<Feed> feeds, Writer writer, Context context) throws IllegalArgumentException, IllegalStateException, IOException { + Log.d(TAG, "Starting to write document"); + + InputStream templateStream = context.getAssets().open("favorites-export-template.html"); + String template = IOUtils.toString(templateStream, "UTF-8"); + String[] templateParts = template.split("\\{FAVORITES\\}"); + + Map<Long, List<FeedItem>> favoriteByFeed = getFeedMap(getFavorites(feeds)); + + writer.append(templateParts[0]); + writer.append("<ul>"); + + for (Long feedId : favoriteByFeed.keySet()) { + List<FeedItem> favorites = favoriteByFeed.get(feedId); + + writer.append("<li><div>"); + writeFeed(writer, favorites.get(0).getFeed()); + + writer.append("<ul>"); + for (FeedItem item : favorites) { + writeFavoriteItem(writer, item); + } + writer.append("</ul></div></li>\n"); + } + + writer.append("</ul>"); + + writer.append(templateParts[1]); + + Log.d(TAG, "Finished writing document"); + } + + private List<FeedItem> getFavorites(List<Feed> feeds) { + int page = 0; + + List<FeedItem> favoritesPage = DBReader.getFavoriteItemsList(page, PAGE_LIMIT); + List<FeedItem> favoritesList = new ArrayList<>(); + + while (!favoritesPage.isEmpty()) { + favoritesList.addAll(favoritesPage); + + // save a DB call if there are no more items to fetch + if (favoritesPage.size() < PAGE_LIMIT) { + break; + } + + ++page; + + favoritesPage = DBReader.getFavoriteItemsList(page * PAGE_LIMIT, PAGE_LIMIT); + } + + Collections.sort(favoritesList, (lhs, rhs) -> lhs.getPubDate().compareTo(rhs.getPubDate())); + + return favoritesList; + } + + private Map<Long, List<FeedItem>> getFeedMap(List<FeedItem> favoritesList) { + Map<Long, List<FeedItem>> feedMap = new TreeMap<>(); + + for (FeedItem item : favoritesList) { + List<FeedItem> feedEpisodes = feedMap.get(item.getFeedId()); + + if (feedEpisodes == null) { + feedEpisodes = new ArrayList<>(); + feedMap.put(item.getFeedId(), feedEpisodes); + } + + feedEpisodes.add(item); + } + + return feedMap; + } + + private void writeFeed(Writer writer, Feed feed) throws IOException { + writer.append("<img src=\""); + writer.append(feed.getImageUrl()); + writer.append("\" /><p>"); + writer.append(feed.getTitle()); + writer.append(" <span><a href=\""); + writer.append(feed.getLink()); + writer.append("\">Website</a> • <a href=\""); + writer.append(feed.getDownload_url()); + writer.append("\">Feed</a></span></p>"); + } + + private void writeFavoriteItem(Writer writer, FeedItem item) throws IOException { + writer.append("<li><span>["); + writer.append(DATE_FORMAT.format(item.getPubDate())); + writer.append("] <a href=\""); + writer.append(item.getLink()); + writer.append("\">"); + writer.append(item.getTitle().trim()); + writer.append("</a>"); + writer.append("</span></li>\n"); + } + + @Override + public String fileExtension() { + return "html"; + } +} diff --git a/core/src/main/res/values-ar/strings.xml b/core/src/main/res/values-ar/strings.xml index 0e29db095..cf9fde1b6 100644 --- a/core/src/main/res/values-ar/strings.xml +++ b/core/src/main/res/values-ar/strings.xml @@ -135,6 +135,12 @@ <string name="exporting_label">جار التصدير ...</string> <string name="export_error_label">حدث خطأ أثناء التصدير</string> <string name="export_success_title">تم التصدير بنجاح</string> + + <!--Favorites import and export--> + <string name="favorites">المفضلات</string> + <string name="favorites_export_label">تصدير المفضلات</string> + <string name="favorites_export_summary">تصدير المفضلات الى ملف</string> + <!--Sleep timer--> <string name="enter_time_here_label">أدخل التوقيت</string> <string name="timer_vibration_label">تشغيل الهزاز</string> diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 7b011159b..d4bc20974 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -576,6 +576,9 @@ <string name="import_select_file">Select file to import</string> <string name="import_ok">Import successful.\n\nPlease press OK to restart AntennaPod</string> <string name="import_no_downgrade">This database was exported with a newer version of AntennaPod. Your current installation does not yet know how to handle this file.</string> + <string name="favorites">Favorites</string> + <string name="favorites_export_label">Favorites Export</string> + <string name="favorites_export_summary">Export saved favorites to file</string> <!-- Sleep timer --> <string name="set_sleeptimer_label">Set sleep timer</string> |