Cleanup import/export

This commit is contained in:
ByteHamster 2017-12-24 13:11:41 +01:00
parent 2e5407370f
commit b665792d29
1 changed files with 88 additions and 130 deletions

View File

@ -1,9 +1,7 @@
package de.danoeh.antennapod.activity; package de.danoeh.antennapod.activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -30,12 +28,8 @@ import java.nio.channels.FileChannel;
* Displays the 'import/export' screen * Displays the 'import/export' screen
*/ */
public class ImportExportActivity extends AppCompatActivity { public class ImportExportActivity extends AppCompatActivity {
private static final int READ_REQUEST_CODE = 42; private static final int REQUEST_CODE_RESTORE = 43;
private static final int READ_REQUEST_CODE_DOCUMENT = 43; private static final int REQUEST_CODE_BACKUP_DOCUMENT = 44;
private static final int WRITE_REQUEST_CODE_DOCUMENT = 44;
private static final String TAG = ImportExportActivity.class.getSimpleName();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -63,45 +57,64 @@ public class ImportExportActivity extends AppCompatActivity {
} }
} }
private void backup() {
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, REQUEST_CODE_BACKUP_DOCUMENT);
} else {
try {
File sd = Environment.getExternalStorageDirectory();
File backupDB = new File(sd, "AntennaPodBackup.db");
writeBackupTo(new FileOutputStream(backupDB));
} catch (Exception e) {
e.printStackTrace();
Snackbar.make(findViewById(R.id.import_export_layout), e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
}
}
private void restore() { private void restore() {
if(Build.VERSION.SDK_INT >= 19) { if(Build.VERSION.SDK_INT >= 19) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*"); intent.setType("*/*");
startActivityForResult(intent, READ_REQUEST_CODE_DOCUMENT); startActivityForResult(intent, REQUEST_CODE_RESTORE);
} else { } else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.import_select_file)), READ_REQUEST_CODE); startActivityForResult(Intent.createChooser(intent,
getString(R.string.import_select_file)), REQUEST_CODE_RESTORE);
} }
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) { public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == READ_REQUEST_CODE_DOCUMENT && resultCode == RESULT_OK) { if (requestCode == REQUEST_CODE_RESTORE && resultCode == RESULT_OK && resultData != null) {
if (resultData != null) {
Uri uri = resultData.getData(); Uri uri = resultData.getData();
writeDatabase(uri); restoreFrom(uri);
} } else if (requestCode == REQUEST_CODE_BACKUP_DOCUMENT && resultCode == RESULT_OK && resultData != null) {
} else if (requestCode == WRITE_REQUEST_CODE_DOCUMENT && resultCode == RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData(); Uri uri = resultData.getData();
writeBackupDocument(uri); backupToDocument(uri);
}
} else if(requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
writeDatabase(uri);
}
} }
} }
private void writeDatabase(Uri inputUri) { private void restoreFrom(Uri inputUri) {
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME); File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
try { try {
InputStream inputStream = getContentResolver().openInputStream(inputUri); InputStream inputStream = getContentResolver().openInputStream(inputUri);
copyInputStreamToFile(inputStream, currentDB); copyInputStreamToFile(inputStream, currentDB);
inputStream.close(); inputStream.close();
displayImportSuccessDialog();
} catch (Exception e) {
e.printStackTrace();
Snackbar.make(findViewById(R.id.import_export_layout), e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
}
private void displayImportSuccessDialog() {
AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this); AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this);
d.setMessage(R.string.import_ok); d.setMessage(R.string.import_ok);
d.setCancelable(false); d.setCancelable(false);
@ -112,10 +125,6 @@ public class ImportExportActivity extends AppCompatActivity {
startActivity(mainIntent); startActivity(mainIntent);
}); });
d.show(); d.show();
} catch (Exception e) {
e.printStackTrace();
Snackbar.make(findViewById(R.id.import_export_layout), e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
} }
private void copyInputStreamToFile(InputStream in, File file) { private void copyInputStreamToFile(InputStream in, File file) {
@ -132,58 +141,25 @@ public class ImportExportActivity extends AppCompatActivity {
} }
} }
private static String getPath(Context context, Uri uri) { private void backupToDocument(Uri uri) {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try { try {
cursor = context.getContentResolver().query(uri, projection, null, null, null); ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "w");
int column_index = cursor.getColumnIndexOrThrow("_data"); FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
if (cursor.moveToFirst()) { writeBackupTo(fileOutputStream);
return cursor.getString(column_index); fileOutputStream.close();
} pfd.close();
} catch (Exception e) {
// Eat it
} finally {
cursor.close();
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null; Snackbar.make(findViewById(R.id.import_export_layout),
} R.string.export_ok, Snackbar.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
private void backup() {
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), Snackbar.make(findViewById(R.id.import_export_layout),
"Can not write SD", Snackbar.LENGTH_SHORT).show(); "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();
}
}
} }
private void writeBackup(FileOutputStream outFileStream) { private void writeBackupTo(FileOutputStream outFileStream) {
try { try {
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME); File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
@ -207,22 +183,4 @@ public class ImportExportActivity extends AppCompatActivity {
} }
} }
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();
}
}
} }