summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java117
-rw-r--r--app/src/main/res/layout/import_export_activity.xml21
-rw-r--r--core/src/main/res/values/strings.xml2
3 files changed, 92 insertions, 48 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java
index 5cd562d45..adaf5fe50 100644
--- a/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java
+++ b/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java
@@ -2,48 +2,34 @@ package de.danoeh.antennapod.activity;
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
-import android.support.v7.app.AlertDialog;
+import android.os.ParcelFileDescriptor;
+import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
-import android.util.Log;
-import android.view.Menu;
-import android.view.MenuInflater;
import android.view.MenuItem;
-import android.view.View;
-import android.widget.AdapterView;
-import android.widget.ListView;
-import android.widget.ProgressBar;
-import android.widget.RadioButton;
-import android.widget.TextView;
import de.danoeh.antennapod.R;
-import de.danoeh.antennapod.adapter.StatisticsListAdapter;
import de.danoeh.antennapod.core.preferences.UserPreferences;
-import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
-import de.danoeh.antennapod.core.util.Converter;
-import rx.Observable;
-import rx.Subscription;
-import rx.android.schedulers.AndroidSchedulers;
-import rx.schedulers.Schedulers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
/**
- * Displays the 'statistics' screen
+ * Displays the 'import/export' screen
*/
public class ImportExportActivity extends AppCompatActivity {
- private static final int READ_REQUEST_CODE = 41;
- private static final int READ_REQUEST_CODE_DOCUMENT = 42;
+ private static final int READ_REQUEST_CODE = 42;
+ private static final int READ_REQUEST_CODE_DOCUMENT = 43;
+ private static final int WRITE_REQUEST_CODE_DOCUMENT = 44;
private static final String TAG = ImportExportActivity.class.getSimpleName();
@@ -55,9 +41,8 @@ public class ImportExportActivity extends AppCompatActivity {
getSupportActionBar().setDisplayShowHomeEnabled(true);
setContentView(R.layout.import_export_activity);
-
- //backup();
- //restore();
+ findViewById(R.id.button_export).setOnClickListener(view -> backup());
+ findViewById(R.id.button_import).setOnClickListener(view -> restore());
}
@Override
@@ -83,7 +68,7 @@ public class ImportExportActivity extends AppCompatActivity {
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
- startActivityForResult(Intent.createChooser(intent, "Select a File to import"), READ_REQUEST_CODE);
+ startActivityForResult(Intent.createChooser(intent, getString(R.string.import_select_file)), READ_REQUEST_CODE);
}
}
@@ -104,6 +89,11 @@ public class ImportExportActivity extends AppCompatActivity {
}
}
+ } else if (requestCode == WRITE_REQUEST_CODE_DOCUMENT && resultCode == RESULT_OK) {
+ if (resultData != null) {
+ Uri uri = resultData.getData();
+ writeBackupDocument(uri);
+ }
} else if(requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
@@ -151,6 +141,8 @@ public class ImportExportActivity extends AppCompatActivity {
}
} catch (Exception e) {
// Eat it
+ } finally {
+ cursor.close();
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
@@ -161,23 +153,72 @@ public class ImportExportActivity extends AppCompatActivity {
}
private void backup() {
- try {
- File sd = Environment.getExternalStorageDirectory();
-
- if (sd.canWrite()) {
- File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
- File backupDB = new File(sd, "AntennaPodBackup.db");
-
- if (currentDB.exists()) {
- FileChannel src = new FileInputStream(currentDB).getChannel();
- FileChannel dst = new FileOutputStream(backupDB).getChannel();
- dst.transferFrom(src, 0, src.size());
- src.close();
- dst.close();
+ if (Build.VERSION.SDK_INT >= 19) {
+ Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT)
+ .addCategory(Intent.CATEGORY_OPENABLE)
+ .setType("application/x-sqlite3")
+ .putExtra(Intent.EXTRA_TITLE, "AntennaPodBackup.db");
+
+ startActivityForResult(intent, WRITE_REQUEST_CODE_DOCUMENT);
+ } else {
+ try {
+ File sd = Environment.getExternalStorageDirectory();
+
+ if (sd.canWrite()) {
+ File backupDB = new File(sd, "AntennaPodBackup.db");
+ writeBackup(new FileOutputStream(backupDB));
+ } else {
+ Snackbar.make(findViewById(R.id.import_export_layout),
+ "Can not write SD", Snackbar.LENGTH_SHORT).show();
}
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ Snackbar.make(findViewById(R.id.import_export_layout), e.getMessage(), Snackbar.LENGTH_SHORT).show();
+ }
+ }
+ }
+
+ void writeBackup(FileOutputStream outFileStream) {
+ try {
+ File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
+
+ if (currentDB.exists()) {
+ FileChannel src = new FileInputStream(currentDB).getChannel();
+ FileChannel dst = outFileStream.getChannel();
+ dst.transferFrom(src, 0, src.size());
+ src.close();
+ dst.close();
+
+ Snackbar.make(findViewById(R.id.import_export_layout),
+ R.string.export_ok, Snackbar.LENGTH_SHORT).show();
+ } else {
+ Snackbar.make(findViewById(R.id.import_export_layout),
+ "Can not access current database", Snackbar.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
+
+ Snackbar.make(findViewById(R.id.import_export_layout), e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
}
+
+ private void writeBackupDocument(Uri uri) {
+ try {
+ ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "w");
+ FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
+ writeBackup(fileOutputStream);
+ fileOutputStream.close();
+ pfd.close();
+
+ Snackbar.make(findViewById(R.id.import_export_layout),
+ R.string.export_ok, Snackbar.LENGTH_SHORT).show();
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ Snackbar.make(findViewById(R.id.import_export_layout),
+ "Can not write SD", Snackbar.LENGTH_SHORT).show();
+ }
+ }
+
}
diff --git a/app/src/main/res/layout/import_export_activity.xml b/app/src/main/res/layout/import_export_activity.xml
index 91e798ead..6614a8710 100644
--- a/app/src/main/res/layout/import_export_activity.xml
+++ b/app/src/main/res/layout/import_export_activity.xml
@@ -3,8 +3,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
- android:paddingTop="8dp"
- android:paddingBottom="8dp">
+ android:id="@+id/import_export_layout"
+ android:padding="8dp">
<TextView
android:layout_width="match_parent"
@@ -13,15 +13,16 @@
android:gravity="center_horizontal"/>
<Button
- android:text="@string/label_export"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/button_export"/>
+ android:text="@string/label_export"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:id="@+id/button_export"
+ android:layout_marginTop="24dp"/>
<Button
- android:text="@string/label_import"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/button_import"/>
+ android:text="@string/label_import"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:id="@+id/button_import"/>
</LinearLayout>
diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml
index 545eea228..4f79914ee 100644
--- a/core/src/main/res/values/strings.xml
+++ b/core/src/main/res/values/strings.xml
@@ -666,6 +666,8 @@
<string name="import_export_warning">This experimental function can be used to transfer your subscriptions and played episodes to another device.\n\nExported databases can only be imported when using the same version of AntennaPod. Otherwise, this function will lead to unexpected behavior.\n\nAfter importing, episodes might be displayed as downloaded even though they are not. Just press the play button of the episodes to make AntennaPod detect this.</string>
<string name="label_import">Import</string>
<string name="label_export">Export</string>
+ <string name="import_select_file">Select file to import</string>
+ <string name="export_ok">Export successful. The database was written to the sd card.</string>
<!-- Casting -->
<string name="cast_media_route_menu_title">Play on&#8230;</string>