diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2024-03-11 23:10:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 23:10:09 +0100 |
commit | 2f3f1fd1861f3a76b8473e0734956d7e7a417c0e (patch) | |
tree | af77295f3afdd4367095399c5b1eec742a11c963 /core/src | |
parent | 5c98a33ed2c82fa6b9b45c7234d429d356f17d8c (diff) | |
download | AntennaPod-2f3f1fd1861f3a76b8473e0734956d7e7a417c0e.zip |
Move import/export to its own module (#6986)
Also clean up ImportExportPreferencesFragment a bit.
Diffstat (limited to 'core/src')
14 files changed, 4 insertions, 691 deletions
diff --git a/core/src/main/assets/html-export-favorites-item-template.html b/core/src/main/assets/html-export-favorites-item-template.html deleted file mode 100644 index 83f06a458..000000000 --- a/core/src/main/assets/html-export-favorites-item-template.html +++ /dev/null @@ -1,4 +0,0 @@ -<li><span>
- {FAV_TITLE}<br>
- <a href="{FAV_WEBSITE}">Website</a> • <a href="{FAV_MEDIA}">Media</a>
-</span></li>
diff --git a/core/src/main/assets/html-export-feed-template.html b/core/src/main/assets/html-export-feed-template.html deleted file mode 100644 index 0646d5953..000000000 --- a/core/src/main/assets/html-export-feed-template.html +++ /dev/null @@ -1,7 +0,0 @@ -<img src="{FEED_IMG}" />
-<p>
- {FEED_TITLE}
- <span>
- <a href="{FEED_LINK}">Website</a> • <a href="{FEED_WEBSITE}">Feed</a>
- </span>
-</p>
\ No newline at end of file diff --git a/core/src/main/assets/html-export-template.html b/core/src/main/assets/html-export-template.html deleted file mode 100644 index e4d3ffd31..000000000 --- a/core/src/main/assets/html-export-template.html +++ /dev/null @@ -1,95 +0,0 @@ -<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
-<html>
- <head>
- <title>AntennaPod {TITLE}</title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <style>
- * {
- font-family: "Sarabun", sans-serif;
- font-weight: 300;
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- html {
- background: #0d8eff;
- background-image: linear-gradient(180deg, #0f9cff, #0682ff);
- text-align: center;
- padding: 10px;
- }
- h1 {
- color: #fff;
- font-weight: 300;
- display: inline-block;
- margin-top: 30px;
- margin-bottom: 20px;
- vertical-align: top;
- }
- ul {
- text-align: center;
- }
- 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;
- text-indent: -10000px;
- background: #eee;
- }
- li > div > p {
- width: 100%;
- }
- body > a {
- color: #ffffff;
- display: inline-block;
- margin-top: 10px;
- clear:left;
- }
- ul > li > span {
- width: 100%;
- border-top: 1px solid #eee8e8;
- padding: 5px;
- box-shadow: none;
- text-align: left;
- }
- </style>
- </head>
- <body>
- <img src="https://antennapod.org/assets/img/antennapod-logo.png" />
- <h1>AntennaPod {TITLE}</h1>
- <ul>
- {FEEDS}
- </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/backup/OpmlBackupAgent.java b/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java index 2058d5b2f..a78ae71a3 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java +++ b/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java @@ -10,6 +10,9 @@ import android.util.Log; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.util.download.FeedUpdateManager; +import de.danoeh.antennapod.storage.importexport.OpmlElement; +import de.danoeh.antennapod.storage.importexport.OpmlReader; +import de.danoeh.antennapod.storage.importexport.OpmlWriter; import org.apache.commons.io.IOUtils; import org.xmlpull.v1.XmlPullParserException; @@ -31,9 +34,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import de.danoeh.antennapod.core.export.opml.OpmlElement; -import de.danoeh.antennapod.core.export.opml.OpmlReader; -import de.danoeh.antennapod.core.export.opml.OpmlWriter; import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.core.storage.DBReader; @@ -81,7 +81,7 @@ public class OpmlBackupAgent extends BackupAgentHelper { try { // Write OPML - new OpmlWriter().writeDocument(DBReader.getFeedList(), writer, mContext); + OpmlWriter.writeDocument(DBReader.getFeedList(), writer); // Compare checksum of new and old file to see if we need to perform a backup at all if (digester != null) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/CommonSymbols.java b/core/src/main/java/de/danoeh/antennapod/core/export/CommonSymbols.java deleted file mode 100644 index 2ce340328..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/CommonSymbols.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package de.danoeh.antennapod.core.export; - -public class CommonSymbols { - - public static final String HEAD = "head"; - public static final String BODY = "body"; - public static final String TITLE = "title"; - - public static final String XML_FEATURE_INDENT_OUTPUT = "http://xmlpull.org/v1/doc/features.html#indent-output"; - -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/ExportWriter.java b/core/src/main/java/de/danoeh/antennapod/core/export/ExportWriter.java deleted file mode 100644 index e60609569..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/ExportWriter.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package de.danoeh.antennapod.core.export; - -import android.content.Context; -import java.io.IOException; -import java.io.Writer; -import java.util.List; - -import de.danoeh.antennapod.model.feed.Feed; - -public interface ExportWriter { - - void writeDocument(List<Feed> feeds, Writer writer, Context context) - throws IllegalArgumentException, IllegalStateException, IOException; - - String fileExtension(); - -} 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 deleted file mode 100644 index 649ec815a..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/favorites/FavoritesWriter.java +++ /dev/null @@ -1,122 +0,0 @@ -package de.danoeh.antennapod.core.export.favorites; - -import android.content.Context; -import android.util.Log; - -import de.danoeh.antennapod.model.feed.FeedItemFilter; -import org.apache.commons.io.IOUtils; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Writer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import de.danoeh.antennapod.core.export.ExportWriter; -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.core.storage.DBReader; -import de.danoeh.antennapod.model.feed.SortOrder; - -/** Writes saved favorites to file. */ -public class FavoritesWriter implements ExportWriter { - private static final String TAG = "FavoritesWriter"; - private static final String FAVORITE_TEMPLATE = "html-export-favorites-item-template.html"; - private static final String FEED_TEMPLATE = "html-export-feed-template.html"; - private static final String UTF_8 = "UTF-8"; - - @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("html-export-template.html"); - String template = IOUtils.toString(templateStream, UTF_8); - template = template.replaceAll("\\{TITLE\\}", "Favorites"); - String[] templateParts = template.split("\\{FEEDS\\}"); - - InputStream favTemplateStream = context.getAssets().open(FAVORITE_TEMPLATE); - String favTemplate = IOUtils.toString(favTemplateStream, UTF_8); - - InputStream feedTemplateStream = context.getAssets().open(FEED_TEMPLATE); - String feedTemplate = IOUtils.toString(feedTemplateStream, UTF_8); - - List<FeedItem> allFavorites = DBReader.getEpisodes(0, Integer.MAX_VALUE, - new FeedItemFilter(FeedItemFilter.IS_FAVORITE), SortOrder.DATE_NEW_OLD); - Map<Long, List<FeedItem>> favoriteByFeed = getFeedMap(allFavorites); - - writer.append(templateParts[0]); - - for (Long feedId : favoriteByFeed.keySet()) { - List<FeedItem> favorites = favoriteByFeed.get(feedId); - writer.append("<li><div>\n"); - writeFeed(writer, favorites.get(0).getFeed(), feedTemplate); - - writer.append("<ul>\n"); - for (FeedItem item : favorites) { - writeFavoriteItem(writer, item, favTemplate); - } - writer.append("</ul></div></li>\n"); - } - - writer.append(templateParts[1]); - - Log.d(TAG, "Finished writing document"); - } - - /** - * Group favorite episodes by feed, sorting them by publishing date in descending order. - * - * @param favoritesList {@code List} of all favorite episodes. - * @return A {@code Map} favorite episodes, keyed by feed ID. - */ - 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, String feedTemplate) throws IOException { - String feedInfo = feedTemplate - .replace("{FEED_IMG}", feed.getImageUrl()) - .replace("{FEED_TITLE}", feed.getTitle()) - .replace("{FEED_LINK}", feed.getLink()) - .replace("{FEED_WEBSITE}", feed.getDownload_url()); - - writer.append(feedInfo); - } - - private void writeFavoriteItem(Writer writer, FeedItem item, String favoriteTemplate) throws IOException { - String favItem = favoriteTemplate.replace("{FAV_TITLE}", item.getTitle().trim()); - if (item.getLink() != null) { - favItem = favItem.replace("{FAV_WEBSITE}", item.getLink()); - } else { - favItem = favItem.replace("{FAV_WEBSITE}", ""); - } - if (item.getMedia() != null && item.getMedia().getDownload_url() != null) { - favItem = favItem.replace("{FAV_MEDIA}", item.getMedia().getDownload_url()); - } else { - favItem = favItem.replace("{FAV_MEDIA}", ""); - } - - writer.append(favItem); - } - - @Override - public String fileExtension() { - return "html"; - } -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/html/HtmlWriter.java b/core/src/main/java/de/danoeh/antennapod/core/export/html/HtmlWriter.java deleted file mode 100644 index 8a660600f..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/html/HtmlWriter.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.danoeh.antennapod.core.export.html; - -import android.content.Context; -import android.util.Log; -import de.danoeh.antennapod.core.export.ExportWriter; -import de.danoeh.antennapod.model.feed.Feed; -import java.io.IOException; -import java.io.InputStream; -import java.io.Writer; -import java.util.List; -import org.apache.commons.io.IOUtils; - -/** Writes HTML documents. */ -public class HtmlWriter implements ExportWriter { - private static final String TAG = "HtmlWriter"; - - /** - * Takes a list of feeds and a writer and writes those into an HTML - * document. - */ - @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("html-export-template.html"); - String template = IOUtils.toString(templateStream, "UTF-8"); - template = template.replaceAll("\\{TITLE\\}", "Subscriptions"); - String[] templateParts = template.split("\\{FEEDS\\}"); - - writer.append(templateParts[0]); - for (Feed feed : feeds) { - writer.append("<li><div><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></div></li>\n"); - } - writer.append(templateParts[1]); - Log.d(TAG, "Finished writing document"); - } - - public String fileExtension() { - return "html"; - } - -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlElement.java b/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlElement.java deleted file mode 100644 index e4ba08440..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlElement.java +++ /dev/null @@ -1,48 +0,0 @@ -package de.danoeh.antennapod.core.export.opml; - -/** - * Represents a single feed in an OPML file. - */ -public class OpmlElement { - private String text; - private String xmlUrl; - private String htmlUrl; - private String type; - - public OpmlElement() { - - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public String getXmlUrl() { - return xmlUrl; - } - - public void setXmlUrl(String xmlUrl) { - this.xmlUrl = xmlUrl; - } - - public String getHtmlUrl() { - return htmlUrl; - } - - public void setHtmlUrl(String htmlUrl) { - this.htmlUrl = htmlUrl; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlReader.java b/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlReader.java deleted file mode 100644 index 9bcca5caf..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlReader.java +++ /dev/null @@ -1,92 +0,0 @@ -package de.danoeh.antennapod.core.export.opml; - -import android.util.Log; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; -import org.xmlpull.v1.XmlPullParserFactory; - -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; - -import de.danoeh.antennapod.core.BuildConfig; - -/** - * Reads OPML documents. - */ -public class OpmlReader { - private static final String TAG = "OpmlReader"; - - private boolean isInOpml = false; - - /** - * Reads an Opml document and returns a list of all OPML elements it can - * find - */ - public ArrayList<OpmlElement> readDocument(Reader reader) - throws XmlPullParserException, IOException { - ArrayList<OpmlElement> elementList = new ArrayList<>(); - XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); - factory.setNamespaceAware(true); - XmlPullParser xpp = factory.newPullParser(); - xpp.setInput(reader); - int eventType = xpp.getEventType(); - - while (eventType != XmlPullParser.END_DOCUMENT) { - switch (eventType) { - case XmlPullParser.START_DOCUMENT: - if (BuildConfig.DEBUG) { - Log.d(TAG, "Reached beginning of document"); - } - break; - case XmlPullParser.START_TAG: - if (xpp.getName().equals(OpmlSymbols.OPML)) { - isInOpml = true; - if (BuildConfig.DEBUG) { - Log.d(TAG, "Reached beginning of OPML tree."); - } - } else if (isInOpml && xpp.getName().equals(OpmlSymbols.OUTLINE)) { - if (BuildConfig.DEBUG) { - Log.d(TAG, "Found new Opml element"); - } - OpmlElement element = new OpmlElement(); - - final String title = xpp.getAttributeValue(null, OpmlSymbols.TITLE); - if (title != null) { - Log.i(TAG, "Using title: " + title); - element.setText(title); - } else { - Log.i(TAG, "Title not found, using text"); - element.setText(xpp.getAttributeValue(null, OpmlSymbols.TEXT)); - } - element.setXmlUrl(xpp.getAttributeValue(null, OpmlSymbols.XMLURL)); - element.setHtmlUrl(xpp.getAttributeValue(null, OpmlSymbols.HTMLURL)); - element.setType(xpp.getAttributeValue(null, OpmlSymbols.TYPE)); - if (element.getXmlUrl() != null) { - if (element.getText() == null) { - Log.i(TAG, "Opml element has no text attribute."); - element.setText(element.getXmlUrl()); - } - elementList.add(element); - } else { - if (BuildConfig.DEBUG) { - Log.d(TAG, "Skipping element because of missing xml url"); - } - } - } - break; - default: - break; - } - eventType = xpp.next(); - } - - if (BuildConfig.DEBUG) { - Log.d(TAG, "Parsing finished."); - } - - return elementList; - } - -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlSymbols.java b/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlSymbols.java deleted file mode 100644 index 0cdfd0d87..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlSymbols.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.danoeh.antennapod.core.export.opml; - -import de.danoeh.antennapod.core.export.CommonSymbols; - -/** - * Contains symbols for reading and writing OPML documents. - */ -final class OpmlSymbols extends CommonSymbols { - - public static final String OPML = "opml"; - static final String OUTLINE = "outline"; - static final String TEXT = "text"; - static final String XMLURL = "xmlUrl"; - static final String HTMLURL = "htmlUrl"; - static final String TYPE = "type"; - static final String VERSION = "version"; - static final String DATE_CREATED = "dateCreated"; - - private OpmlSymbols() { - - } - -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlWriter.java b/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlWriter.java deleted file mode 100644 index a44d90557..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/export/opml/OpmlWriter.java +++ /dev/null @@ -1,75 +0,0 @@ -package de.danoeh.antennapod.core.export.opml; - -import android.content.Context; -import android.util.Log; -import android.util.Xml; - -import de.danoeh.antennapod.core.util.DateFormatter; -import org.xmlpull.v1.XmlSerializer; - -import java.io.IOException; -import java.io.Writer; -import java.util.Date; -import java.util.List; - -import de.danoeh.antennapod.core.export.ExportWriter; -import de.danoeh.antennapod.model.feed.Feed; - -/** Writes OPML documents. */ -public class OpmlWriter implements ExportWriter { - - private static final String TAG = "OpmlWriter"; - private static final String ENCODING = "UTF-8"; - private static final String OPML_VERSION = "2.0"; - private static final String OPML_TITLE = "AntennaPod Subscriptions"; - - /** - * Takes a list of feeds and a writer and writes those into an OPML - * document. - */ - @Override - public void writeDocument(List<Feed> feeds, Writer writer, Context context) - throws IllegalArgumentException, IllegalStateException, IOException { - Log.d(TAG, "Starting to write document"); - XmlSerializer xs = Xml.newSerializer(); - xs.setFeature(OpmlSymbols.XML_FEATURE_INDENT_OUTPUT, true); - xs.setOutput(writer); - - xs.startDocument(ENCODING, false); - xs.startTag(null, OpmlSymbols.OPML); - xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION); - - xs.startTag(null, OpmlSymbols.HEAD); - xs.startTag(null, OpmlSymbols.TITLE); - xs.text(OPML_TITLE); - xs.endTag(null, OpmlSymbols.TITLE); - xs.startTag(null, OpmlSymbols.DATE_CREATED); - xs.text(DateFormatter.formatRfc822Date(new Date())); - xs.endTag(null, OpmlSymbols.DATE_CREATED); - xs.endTag(null, OpmlSymbols.HEAD); - - xs.startTag(null, OpmlSymbols.BODY); - for (Feed feed : feeds) { - xs.startTag(null, OpmlSymbols.OUTLINE); - xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle()); - xs.attribute(null, OpmlSymbols.TITLE, feed.getTitle()); - if (feed.getType() != null) { - xs.attribute(null, OpmlSymbols.TYPE, feed.getType()); - } - xs.attribute(null, OpmlSymbols.XMLURL, feed.getDownload_url()); - if (feed.getLink() != null) { - xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink()); - } - xs.endTag(null, OpmlSymbols.OUTLINE); - } - xs.endTag(null, OpmlSymbols.BODY); - xs.endTag(null, OpmlSymbols.OPML); - xs.endDocument(); - Log.d(TAG, "Finished writing document"); - } - - public String fileExtension() { - return "opml"; - } - -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DatabaseExporter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DatabaseExporter.java deleted file mode 100644 index d4a863b8b..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DatabaseExporter.java +++ /dev/null @@ -1,107 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteException; -import android.net.Uri; -import android.os.ParcelFileDescriptor; -import android.text.format.Formatter; -import android.util.Log; -import de.danoeh.antennapod.core.R; -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.channels.FileChannel; - -public class DatabaseExporter { - private static final String TAG = "DatabaseExporter"; - private static final String TEMP_DB_NAME = PodDBAdapter.DATABASE_NAME + "_tmp"; - - public static void exportToDocument(Uri uri, Context context) throws IOException { - ParcelFileDescriptor pfd = null; - FileOutputStream fileOutputStream = null; - try { - pfd = context.getContentResolver().openFileDescriptor(uri, "wt"); - fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); - exportToStream(fileOutputStream, context); - } catch (IOException e) { - Log.e(TAG, Log.getStackTraceString(e)); - throw e; - } finally { - IOUtils.closeQuietly(fileOutputStream); - - if (pfd != null) { - try { - pfd.close(); - } catch (IOException e) { - Log.d(TAG, "Unable to close ParcelFileDescriptor"); - } - } - } - } - - public static void exportToStream(FileOutputStream outFileStream, Context context) throws IOException { - FileChannel src = null; - FileChannel dst = null; - try { - File currentDB = context.getDatabasePath(PodDBAdapter.DATABASE_NAME); - - if (currentDB.exists()) { - src = new FileInputStream(currentDB).getChannel(); - dst = outFileStream.getChannel(); - long srcSize = src.size(); - dst.transferFrom(src, 0, srcSize); - - long newDstSize = dst.size(); - if (newDstSize != srcSize) { - throw new IOException(String.format( - "Unable to write entire database. Expected to write %s, but wrote %s.", - Formatter.formatShortFileSize(context, srcSize), - Formatter.formatShortFileSize(context, newDstSize))); - } - } else { - throw new IOException("Can not access current database"); - } - } catch (IOException e) { - Log.e(TAG, Log.getStackTraceString(e)); - throw e; - } finally { - IOUtils.closeQuietly(src); - IOUtils.closeQuietly(dst); - } - } - - public static void importBackup(Uri inputUri, Context context) throws IOException { - InputStream inputStream = null; - try { - File tempDB = context.getDatabasePath(TEMP_DB_NAME); - inputStream = context.getContentResolver().openInputStream(inputUri); - FileUtils.copyInputStreamToFile(inputStream, tempDB); - - SQLiteDatabase db = SQLiteDatabase.openDatabase(tempDB.getAbsolutePath(), - null, SQLiteDatabase.OPEN_READONLY); - if (db.getVersion() > PodDBAdapter.VERSION) { - throw new IOException(context.getString(R.string.import_no_downgrade)); - } - db.close(); - - File currentDB = context.getDatabasePath(PodDBAdapter.DATABASE_NAME); - boolean success = currentDB.delete(); - if (!success) { - throw new IOException("Unable to delete old database"); - } - FileUtils.moveFile(tempDB, currentDB); - } catch (IOException | SQLiteException e) { - Log.e(TAG, Log.getStackTraceString(e)); - throw e; - } finally { - IOUtils.closeQuietly(inputStream); - } - } -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/DateFormatter.java b/core/src/main/java/de/danoeh/antennapod/core/util/DateFormatter.java index dc7ed4508..c67e13db3 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/DateFormatter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/DateFormatter.java @@ -3,11 +3,9 @@ package de.danoeh.antennapod.core.util; import android.content.Context; import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; -import java.util.Locale; /** * Formats dates. @@ -17,11 +15,6 @@ public class DateFormatter { } - public static String formatRfc822Date(Date date) { - SimpleDateFormat format = new SimpleDateFormat("dd MMM yy HH:mm:ss Z", Locale.US); - return format.format(date); - } - public static String formatAbbrev(final Context context, final Date date) { if (date == null) { return ""; |