From bd2f359628fb54eea2d9410a3314de8c917f2b21 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Thu, 28 May 2020 06:42:04 +0200 Subject: [PATCH] Make auto backup work with the new storage access --- .../flocke/andotp/Utilities/BackupHelper.java | 8 +---- .../andotp/View/EntriesCardAdapter.java | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 14 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 2c101b04..a344b9c7 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 @@ -1,10 +1,7 @@ package org.shadowice.flocke.andotp.Utilities; -import android.Manifest; import android.content.Context; -import android.content.pm.PackageManager; import android.net.Uri; -import androidx.core.content.ContextCompat; import org.shadowice.flocke.andotp.Database.Entry; @@ -43,11 +40,8 @@ public class BackupHelper { public static Constants.BackupType autoBackupType(Context context) { Settings settings = new Settings(context); - if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - return Constants.BackupType.UNAVAILABLE; - } - if(!Tools.mkdir(settings.getBackupDir())) { + if(!settings.isBackupLocationSet()) { return Constants.BackupType.UNAVAILABLE; } 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 7ffe3893..76bcfb74 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 @@ -31,6 +31,7 @@ import android.net.Uri; import android.os.Handler; import androidx.annotation.NonNull; import androidx.appcompat.widget.PopupMenu; +import androidx.documentfile.provider.DocumentFile; import androidx.recyclerview.widget.RecyclerView; import android.text.Editable; import android.text.InputType; @@ -173,16 +174,30 @@ public class EntriesCardAdapter extends RecyclerView.Adapter if(auto_backup) { Constants.BackupType backupType = BackupHelper.autoBackupType(context); if (backupType == Constants.BackupType.ENCRYPTED) { - Uri backupFilename = Tools.buildUri(settings.getBackupDir(), BackupHelper.backupFilename(context, Constants.BackupType.ENCRYPTED)); + DocumentFile backupLocation = DocumentFile.fromTreeUri(context, settings.getBackupLocation()); - byte[] keyMaterial = encryptionKey.getEncoded(); - SecretKey encryptionKey = EncryptionHelper.generateSymmetricKey(keyMaterial); + if (backupLocation != null) { + // Try to find an existing file to overwrite + DocumentFile cryptBackupFile = backupLocation.findFile(BackupHelper.backupFilename(context, Constants.BackupType.ENCRYPTED)); - boolean success = BackupHelper.backupToFile(context, backupFilename, settings.getBackupPasswordEnc(), encryptionKey); - if (success) { - Toast.makeText(context, R.string.backup_toast_export_success, Toast.LENGTH_LONG).show(); + if (cryptBackupFile == null) + cryptBackupFile = backupLocation.createFile(Constants.BACKUP_MIMETYPE_CRYPT, BackupHelper.backupFilename(context, Constants.BackupType.ENCRYPTED)); + + if (cryptBackupFile != null) { + byte[] keyMaterial = encryptionKey.getEncoded(); + SecretKey encryptionKey = EncryptionHelper.generateSymmetricKey(keyMaterial); + + boolean success = BackupHelper.backupToFile(context, cryptBackupFile.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, R.string.backup_toast_file_creation_failed, Toast.LENGTH_LONG).show(); + } } else { - Toast.makeText(context, R.string.backup_toast_export_failed, Toast.LENGTH_LONG).show(); + Toast.makeText(context, R.string.backup_toast_location_access_failed, Toast.LENGTH_LONG).show(); } } }