Make auto backup work with the new storage access

This commit is contained in:
Jakob Nixdorf 2020-05-28 06:42:04 +02:00
parent 34ac28189c
commit bd2f359628
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
2 changed files with 23 additions and 14 deletions

View file

@ -1,10 +1,7 @@
package org.shadowice.flocke.andotp.Utilities; package org.shadowice.flocke.andotp.Utilities;
import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import androidx.core.content.ContextCompat;
import org.shadowice.flocke.andotp.Database.Entry; import org.shadowice.flocke.andotp.Database.Entry;
@ -43,11 +40,8 @@ public class BackupHelper {
public static Constants.BackupType autoBackupType(Context context) { public static Constants.BackupType autoBackupType(Context context) {
Settings settings = new Settings(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; return Constants.BackupType.UNAVAILABLE;
} }

View file

@ -31,6 +31,7 @@ import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
import androidx.documentfile.provider.DocumentFile;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable; import android.text.Editable;
import android.text.InputType; import android.text.InputType;
@ -173,16 +174,30 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
if(auto_backup) { if(auto_backup) {
Constants.BackupType backupType = BackupHelper.autoBackupType(context); Constants.BackupType backupType = BackupHelper.autoBackupType(context);
if (backupType == Constants.BackupType.ENCRYPTED) { 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(); if (backupLocation != null) {
SecretKey encryptionKey = EncryptionHelper.generateSymmetricKey(keyMaterial); // 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 (cryptBackupFile == null)
if (success) { cryptBackupFile = backupLocation.createFile(Constants.BACKUP_MIMETYPE_CRYPT, BackupHelper.backupFilename(context, Constants.BackupType.ENCRYPTED));
Toast.makeText(context, R.string.backup_toast_export_success, Toast.LENGTH_LONG).show();
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 { } 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();
} }
} }
} }