From 0c0b4455c0236383ffbc98cd2b6169abb8cd7239 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Tue, 23 Mar 2021 07:02:06 +0100 Subject: [PATCH] Use background tasks for automatic backups That way we work around the NetworkOnMainThreadException that certain storage providers (e.g. Nextcloud) cause when called directly from the broadcast receiver --- .../flocke/andotp/Utilities/BackupHelper.java | 11 +------ .../andotp/View/EntriesCardAdapter.java | 31 ++++++++----------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/BackupHelper.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/BackupHelper.java index f6377303..18c0bbfb 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/BackupHelper.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/BackupHelper.java @@ -5,12 +5,10 @@ import android.net.Uri; import androidx.documentfile.provider.DocumentFile; -import org.shadowice.flocke.andotp.Database.Entry; import org.shadowice.flocke.andotp.R; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import javax.crypto.SecretKey; @@ -101,16 +99,9 @@ public class BackupHelper { return Constants.BackupType.UNAVAILABLE; } - public static boolean backupToFile(Context context, Uri uri, String password, SecretKey encryptionKey) { - ArrayList entries = DatabaseHelper.loadDatabase(context, encryptionKey); - String plain = DatabaseHelper.entriesToString(entries); - - return backupToFile(context, uri, password, plain); - } - public static boolean backupToFile(Context context, Uri uri, String password, String plain) { - boolean success = true; + boolean success; try { int iter = EncryptionHelper.generateRandomIterations(); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java index a7ff0aed..52ce4ff9 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java @@ -63,10 +63,11 @@ import org.shadowice.flocke.andotp.Database.Entry; import org.shadowice.flocke.andotp.Database.EntryList; import org.shadowice.flocke.andotp.Dialogs.ManualEntryDialog; import org.shadowice.flocke.andotp.R; +import org.shadowice.flocke.andotp.Tasks.EncryptedBackupTask; +import org.shadowice.flocke.andotp.Tasks.GenericBackupTask; import org.shadowice.flocke.andotp.Utilities.BackupHelper; import org.shadowice.flocke.andotp.Utilities.Constants; import org.shadowice.flocke.andotp.Utilities.DatabaseHelper; -import org.shadowice.flocke.andotp.Utilities.EncryptionHelper; import org.shadowice.flocke.andotp.Utilities.EntryThumbnail; import org.shadowice.flocke.andotp.Utilities.Settings; import org.shadowice.flocke.andotp.Utilities.Tools; @@ -189,25 +190,19 @@ public class EntriesCardAdapter extends RecyclerView.Adapter public void saveEntries(boolean auto_backup) { DatabaseHelper.saveDatabase(context, entries.getEntries(), encryptionKey); - if(auto_backup) { - Constants.BackupType backupType = BackupHelper.autoBackupType(context); - if (backupType == Constants.BackupType.ENCRYPTED) { - BackupHelper.BackupFile cryptBackupFile = BackupHelper.backupFile(context, settings.getBackupLocation(), Constants.BackupType.ENCRYPTED); + if(auto_backup && BackupHelper.autoBackupType(context) == Constants.BackupType.ENCRYPTED) { + EncryptedBackupTask task = new EncryptedBackupTask(context, entries.getEntries(), settings.getBackupPasswordEnc(), null); + task.setCallback(this::handleTaskResult); - if (cryptBackupFile.file != null) { - byte[] keyMaterial = encryptionKey.getEncoded(); - SecretKey encryptionKey = EncryptionHelper.generateSymmetricKey(keyMaterial); + task.execute(); + } + } - boolean success = BackupHelper.backupToFile(context, cryptBackupFile.file.getUri(), settings.getBackupPasswordEnc(), encryptionKey); - if (success) { - Toast.makeText(context, R.string.backup_toast_export_success, Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(context, R.string.backup_toast_export_failed, Toast.LENGTH_LONG).show(); - } - } else { - Toast.makeText(context, cryptBackupFile.errorMessage, Toast.LENGTH_LONG).show(); - } - } + private void handleTaskResult(GenericBackupTask.BackupTaskResult result) { + if (result.success) { + Toast.makeText(context, R.string.backup_toast_export_success, Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(context, result.messageId, Toast.LENGTH_LONG).show(); } }