diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java
index 00c4a2aa..9a330656 100644
--- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java
+++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/BackupActivity.java
@@ -31,6 +31,8 @@ import android.content.IntentSender;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
+import androidx.documentfile.provider.DocumentFile;
+
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
@@ -317,15 +319,40 @@ public class BackupActivity extends BaseActivity {
intent.putExtra(Intent.EXTRA_TITLE, BackupHelper.backupFilename(this, backupType));
startActivityForResult(intent, intentId);
} else {
- if (Tools.mkdir(settings.getBackupDir())) {
- if (intentId == Constants.INTENT_BACKUP_SAVE_DOCUMENT_PLAIN)
- doBackupPlain(Tools.buildUri(settings.getBackupDir(), BackupHelper.backupFilename(this, Constants.BackupType.PLAIN_TEXT)));
- else if (intentId == Constants.INTENT_BACKUP_SAVE_DOCUMENT_CRYPT)
- doBackupCrypt(Tools.buildUri(settings.getBackupDir(), BackupHelper.backupFilename(this, Constants.BackupType.ENCRYPTED)));
- else if (intentId == Constants.INTENT_BACKUP_SAVE_DOCUMENT_PGP)
- backupEncryptedWithPGP(Tools.buildUri(settings.getBackupDir(), BackupHelper.backupFilename(this, Constants.BackupType.OPEN_PGP)), null);
+ if (settings.isBackupLocationSet()) {
+ DocumentFile backupLocation = DocumentFile.fromTreeUri(this, settings.getBackupLocation());
+
+ if (backupLocation != null) {
+ if (intentId == Constants.INTENT_BACKUP_SAVE_DOCUMENT_PLAIN) {
+ DocumentFile plainBackupFile = backupLocation.createFile(Constants.BACKUP_MIMETYPE_PLAIN, BackupHelper.backupFilename(this, Constants.BackupType.PLAIN_TEXT));
+
+ if (plainBackupFile != null) {
+ doBackupPlain(plainBackupFile.getUri());
+ } else {
+ Toast.makeText(this, R.string.backup_toast_file_creation_failed, Toast.LENGTH_LONG).show();
+ }
+ } else if (intentId == Constants.INTENT_BACKUP_SAVE_DOCUMENT_CRYPT) {
+ DocumentFile cryptBackupFile = backupLocation.createFile(Constants.BACKUP_MIMETYPE_CRYPT, BackupHelper.backupFilename(this, Constants.BackupType.ENCRYPTED));
+
+ if (cryptBackupFile != null) {
+ doBackupCrypt(cryptBackupFile.getUri());
+ } else {
+ Toast.makeText(this, R.string.backup_toast_file_creation_failed, Toast.LENGTH_LONG).show();
+ }
+ } else if (intentId == Constants.INTENT_BACKUP_SAVE_DOCUMENT_PGP) {
+ DocumentFile pgpBackupFile = backupLocation.createFile(Constants.BACKUP_MIMETYPE_PGP, BackupHelper.backupFilename(this, Constants.BackupType.OPEN_PGP));
+
+ if (pgpBackupFile != null) {
+ backupEncryptedWithPGP(pgpBackupFile.getUri(), null);
+ } else {
+ Toast.makeText(this, R.string.backup_toast_file_creation_failed, Toast.LENGTH_LONG).show();
+ }
+ }
+ } else {
+ Toast.makeText(this, R.string.backup_toast_location_access_failed, Toast.LENGTH_LONG).show();
+ }
} else {
- Toast.makeText(this, R.string.backup_toast_mkdir_failed, Toast.LENGTH_LONG).show();
+ Toast.makeText(this, R.string.backup_toast_no_location, Toast.LENGTH_LONG).show();
}
}
}
diff --git a/app/src/main/res/values/strings_backup.xml b/app/src/main/res/values/strings_backup.xml
index c0d15043..82119c89 100644
--- a/app/src/main/res/values/strings_backup.xml
+++ b/app/src/main/res/values/strings_backup.xml
@@ -80,6 +80,10 @@
automatic backup was successful
+ Please set a default backup location in the settings!
+ Failed to access backup location!
+ Failed to create backup file!
+
Failed to create backup directory
Export to external storage successful
Export to external storage failed%