From 8ecbe95e1654ba3e31408c39e57f80dcf6ead3cb Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 27 Jan 2020 10:28:41 +0100 Subject: Moved database import/export to settings --- .../antennapod/core/storage/DatabaseExporter.java | 95 ++++++++++++++++++++++ core/src/main/res/values/strings.xml | 4 +- 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/de/danoeh/antennapod/core/storage/DatabaseExporter.java (limited to 'core/src/main') 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 new file mode 100644 index 000000000..af3d1206c --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DatabaseExporter.java @@ -0,0 +1,95 @@ +package de.danoeh.antennapod.core.storage; + +import android.content.Context; +import android.net.Uri; +import android.os.ParcelFileDescriptor; +import android.util.Log; +import de.danoeh.antennapod.core.R; +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; +import java.util.Arrays; + +public class DatabaseExporter { + private static final String TAG = "DatabaseExporter"; + private static final byte[] SQLITE3_MAGIC = "SQLite format 3\0".getBytes(); + + public static boolean validateDB(Uri inputUri, Context context) throws IOException { + try (InputStream inputStream = context.getContentResolver().openInputStream(inputUri)) { + byte[] magicBuf = new byte[SQLITE3_MAGIC.length]; + if (inputStream.read(magicBuf) == magicBuf.length) { + return Arrays.equals(SQLITE3_MAGIC, magicBuf); + } + } + return false; + } + + public static void exportToDocument(Uri uri, Context context) throws IOException { + ParcelFileDescriptor pfd = null; + FileOutputStream fileOutputStream = null; + try { + pfd = context.getContentResolver().openFileDescriptor(uri, "w"); + 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(); + dst.transferFrom(src, 0, src.size()); + } 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 { + if (!validateDB(inputUri, context)) { + throw new IOException(context.getString(R.string.import_bad_file)); + } + + File currentDB = context.getDatabasePath(PodDBAdapter.DATABASE_NAME); + inputStream = context.getContentResolver().openInputStream(inputUri); + FileUtils.copyInputStreamToFile(inputStream, currentDB); + } catch (IOException e) { + Log.e(TAG, Log.getStackTraceString(e)); + throw e; + } finally { + IOUtils.closeQuietly(inputStream); + } + } +} diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 26ed87537..554e1489f 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -566,7 +566,9 @@ From local filesystem OPML export HTML export - Exporting… + Database export + Database import + Please wait… Export error Export successful The exported file was written to:\n\n%1$s -- cgit v1.2.3