diff options
author | ByteHamster <info@bytehamster.com> | 2017-10-16 00:06:17 +0200 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2017-12-24 13:11:57 +0100 |
commit | 4cb4a0a0cbb65bc0db58ce24cc665f38a06ee208 (patch) | |
tree | d2cac06794778df8794a7c0c70878980d475f8ca /app/src/main | |
parent | af33e4c37bdad3151013553dbeafc3bc1e8043b0 (diff) | |
download | AntennaPod-4cb4a0a0cbb65bc0db58ce24cc665f38a06ee208.zip |
Supporting export using documents store API
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java | 117 | ||||
-rw-r--r-- | app/src/main/res/layout/import_export_activity.xml | 21 |
2 files changed, 90 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> |