Showing dialog when import succeeds

This commit is contained in:
ByteHamster 2017-10-21 20:08:11 +02:00
parent 4cb4a0a0cb
commit 2e5407370f
2 changed files with 30 additions and 25 deletions

View File

@ -1,5 +1,6 @@
package de.danoeh.antennapod.activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
@ -9,6 +10,8 @@ import android.os.Bundle;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.design.widget.Snackbar;
import android.support.v4.content.IntentCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import de.danoeh.antennapod.R;
@ -74,20 +77,10 @@ public class ImportExportActivity extends AppCompatActivity {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
if (requestCode == READ_REQUEST_CODE_DOCUMENT && resultCode == RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
copyInputStreamToFile(inputStream, currentDB);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
writeDatabase(uri);
}
} else if (requestCode == WRITE_REQUEST_CODE_DOCUMENT && resultCode == RESULT_OK) {
if (resultData != null) {
@ -97,23 +90,34 @@ public class ImportExportActivity extends AppCompatActivity {
} else if(requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
try {
File backupDB = new File(getPath(getBaseContext(), uri));
if (backupDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} catch (Exception e) {
e.printStackTrace();
}
writeDatabase(uri);
}
}
}
private void writeDatabase(Uri inputUri) {
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
try {
InputStream inputStream = getContentResolver().openInputStream(inputUri);
copyInputStreamToFile(inputStream, currentDB);
inputStream.close();
AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this);
d.setMessage(R.string.import_ok);
d.setCancelable(false);
d.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);
});
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) {
try {
OutputStream out = new FileOutputStream(file);
@ -179,7 +183,7 @@ public class ImportExportActivity extends AppCompatActivity {
}
}
void writeBackup(FileOutputStream outFileStream) {
private void writeBackup(FileOutputStream outFileStream) {
try {
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);

View File

@ -668,6 +668,7 @@
<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>
<string name="import_ok">Success.\n\nPlease press OK to restart AntennaPod</string>
<!-- Casting -->
<string name="cast_media_route_menu_title">Play on&#8230;</string>