Skip to content

Commit

Permalink
Dictionary backup to correct folder
Browse files Browse the repository at this point in the history
  • Loading branch information
usharik committed Mar 16, 2018
1 parent 3c1d6f1 commit de836c1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
20 changes: 16 additions & 4 deletions app/src/main/java/com/usharik/seznamslovnik/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,25 @@ private Observable<String> handleActions(Action action) {
return Observable.empty();

case BackupDictionaryAction.BACKUP_DICTIONARY_ACTION: {
databaseManager.backup();
return Observable.just("Dictionary backup completed");
String message;
try {
databaseManager.backup();
message = "Dictionary backup completed";
} catch (Exception ex) {
message = ex.getLocalizedMessage();
}
return Observable.just(message);
}

case RestoreDictionaryAction.RESTORE_DICTIONARY_ACTION: {
databaseManager.restore();
return Observable.just("Dictionary restore completed");
String message;
try {
databaseManager.restore();
message = "Dictionary restore completed";
} catch (Exception ex) {
message = ex.getLocalizedMessage();
}
return Observable.just(message);
}

default:
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/usharik/seznamslovnik/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public boolean onOptionsItemSelected(MenuItem item){
item.setChecked(!getViewModel().isOfflineMode());
getViewModel().setOfflineMode(item.isChecked());
updateTitle();
if (!getViewModel().isOfflineMode()) {
getViewModel().onTextChanged(binding.input.getText(), 0, 0, 0);
}
return true;
case R.id.backup:
if (isExternalStoragePermitted()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -16,6 +15,8 @@

public class DatabaseManager {

private static final String BACKUP_FOLDER = "/Seznam-Slovnik/";

private Context context;
private AppDatabase instance;

Expand All @@ -37,34 +38,42 @@ public void close() {
instance.close();
}

public void backup() {
public void backup() throws IOException {
close();
String sourcePath = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/";
String destPath = Environment.getExternalStorageDirectory() + "/Download/";
File folder = new File(Environment.getExternalStorageDirectory() + BACKUP_FOLDER);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (!success) {
throw new RuntimeException("Can't create folder for backup");
}
String sourcePath = getDatabasePath();
String destPath = Environment.getExternalStorageDirectory() + BACKUP_FOLDER;
copyFile(sourcePath, AppDatabase.DB_NAME, destPath, AppDatabase.DB_NAME);
}

public void restore() {
public void restore() throws IOException {
close();
String sourcePath = Environment.getExternalStorageDirectory() + "/Download/";
String destPath = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/";
String sourcePath = Environment.getExternalStorageDirectory() + BACKUP_FOLDER;
String destPath = getDatabasePath();
copyFile(sourcePath, AppDatabase.DB_NAME, destPath, AppDatabase.DB_NAME);
}

private String getDatabasePath() {
return Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/";
}

private void copyFile(String sourcePath,
String sourceFileName,
String destPath,
String detFileName){
String sourceFileName,
String destPath,
String detFileName) throws IOException {
File sourceFile = new File(sourcePath, sourceFileName);
File destFile = new File(destPath, detFileName);
try {
FileChannel source = new FileInputStream(sourceFile).getChannel();
FileChannel destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
Log.e(getClass().getName(), e.getLocalizedMessage());
}
FileChannel source = new FileInputStream(sourceFile).getChannel();
FileChannel destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
android:hint="@string/word_to_translate"
android:inputType="text"
android:imeOptions="actionDone"
android:selectAllOnFocus="true"
android:onTextChanged="@{(s, start, before, count) -> viewModel.onTextChanged(s, start, before, count)}"
android:text="@={viewModel.word}"
app:layout_constraintEnd_toStartOf="@+id/btTo"
Expand All @@ -72,7 +73,6 @@
app:layout_constraintTop_toBottomOf="@+id/input"
app:layout_constraintVertical_bias="0.51" />


</android.support.constraint.ConstraintLayout>

</layout>

0 comments on commit de836c1

Please sign in to comment.