Skip to content

Commit

Permalink
Move code to get the list of backups from BackupPreferenceFragment to…
Browse files Browse the repository at this point in the history
… BackupManager
  • Loading branch information
rivaldi8 committed Jan 21, 2018
1 parent bf19595 commit 366f1a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public void onConnectionFailed(ConnectionResult connectionResult) {
*/
private void restoreBackup() {
Log.i("Settings", "Opening GnuCash XML backups for restore");
String bookUID = BooksDbAdapter.getInstance().getActiveBookUID();
final String bookUID = BooksDbAdapter.getInstance().getActiveBookUID();

final String defaultBackupFile = BackupManager.getBookBackupFileUri(bookUID);
if (defaultBackupFile != null){
Expand All @@ -392,8 +392,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
}

//If no default location was set, look in the internal SD card location
File[] backupFiles = new File(BackupManager.getBackupFolderPath(bookUID)).listFiles();
if (backupFiles == null || backupFiles.length == 0){
if (BackupManager.getBackupList(bookUID).isEmpty()){
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getActivity())
.setTitle(R.string.title_no_backups_found)
.setMessage(R.string.msg_no_backups_to_restore_from)
Expand All @@ -407,14 +406,10 @@ public void onClick(DialogInterface dialog, int which) {
return;
}

Arrays.sort(backupFiles);
List<File> backupFilesList = Arrays.asList(backupFiles);
Collections.reverse(backupFilesList);
final File[] sortedBackupFiles = (File[]) backupFilesList.toArray();

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.select_dialog_singlechoice);
final DateFormat dateFormatter = SimpleDateFormat.getDateTimeInstance();
for (File backupFile : sortedBackupFiles) {
for (File backupFile : BackupManager.getBackupList(bookUID)) {
long time = Exporter.getExportTime(backupFile.getName());
if (time > 0)
arrayAdapter.add(dateFormatter.format(new Date(time)));
Expand All @@ -434,7 +429,7 @@ public void onClick(DialogInterface dialog, int which) {
restoreDialogBuilder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File backupFile = sortedBackupFiles[which];
File backupFile = BackupManager.getBackupList(bookUID).get(which);
new ImportAsyncTask(getActivity()).execute(Uri.fromFile(backupFile));
}
});
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/org/gnucash/android/util/BackupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.crashlytics.android.Crashlytics;

Expand All @@ -23,6 +24,8 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.zip.GZIPOutputStream;

Expand Down Expand Up @@ -123,7 +126,7 @@ private static String getBackupFilePath(String bookUID){
*
* @return Absolute path to backup folder for the book
*/
public static String getBackupFolderPath(String bookUID){
private static String getBackupFolderPath(String bookUID){
String baseFolderPath = GnuCashApplication.getAppContext()
.getExternalFilesDir(null)
.getAbsolutePath();
Expand All @@ -144,4 +147,12 @@ public static String getBookBackupFileUri(String bookUID){
SharedPreferences sharedPreferences = PreferenceActivity.getBookSharedPreferences(bookUID);
return sharedPreferences.getString(KEY_BACKUP_FILE, null);
}

public static List<File> getBackupList(String bookUID) {
File[] backupFiles = new File(getBackupFolderPath(bookUID)).listFiles();
Arrays.sort(backupFiles);
List<File> backupFilesList = Arrays.asList(backupFiles);
Collections.reverse(backupFilesList);
return backupFilesList;
}
}
23 changes: 21 additions & 2 deletions app/src/test/java/org/gnucash/android/util/BackupManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,30 @@ public void backupAllBooks() throws Exception {
BackupManager.backupAllBooks();

for (String bookUID : mBooksDbAdapter.getAllBookUIDs()) {
File backupFolder = new File(BackupManager.getBackupFolderPath(bookUID));
assertThat(backupFolder.list().length).isEqualTo(1);
assertThat(BackupManager.getBackupList(bookUID).size()).isEqualTo(1);
}
}

@Test
public void getBackupList() throws Exception {
String bookUID = createNewBookWithDefaultAccounts();
BookUtils.activateBook(bookUID);

BackupManager.backupActiveBook();
Thread.sleep(1000); // FIXME: Use Mockito to get a different date in Exporter.buildExportFilename
BackupManager.backupActiveBook();

assertThat(BackupManager.getBackupList(bookUID).size()).isEqualTo(2);
}

@Test
public void whenNoBackupsHaveBeenDone_shouldReturnEmptyBackupList() {
String bookUID = createNewBookWithDefaultAccounts();
BookUtils.activateBook(bookUID);

assertThat(BackupManager.getBackupList(bookUID)).isEmpty();
}

/**
* Creates a new database with default accounts
* @return The book UID for the new database
Expand Down

0 comments on commit 366f1a4

Please sign in to comment.