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 0a5d3cc7..e596f64a 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 @@ -305,18 +305,40 @@ public class BackupActivity extends BaseActivity { /* Generic functions for all backup/restore options */ private void showOpenFileSelector(int intentId) { - Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); - intent.addCategory(Intent.CATEGORY_OPENABLE); - intent.setType("*/*"); - startActivityForResult(intent, intentId); + if (settings.getBackupAsk()) { + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType("*/*"); + startActivityForResult(intent, intentId); + } else { + if (intentId == INTENT_OPEN_DOCUMENT_PLAIN) + doRestorePlain(Tools.buildUri(settings.getBackupDir(), DEFAULT_BACKUP_FILENAME_PLAIN)); + else if (intentId == INTENT_OPEN_DOCUMENT_CRYPT) + doRestoreCrypt(Tools.buildUri(settings.getBackupDir(), DEFAULT_BACKUP_FILENAME_CRYPT)); + else if (intentId == INTENT_OPEN_DOCUMENT_PGP) + restoreEncryptedWithPGP(Tools.buildUri(settings.getBackupDir(), DEFAULT_BACKUP_FILENAME_PGP), null); + } } private void showSaveFileSelector(String mimeType, String fileName, int intentId) { - Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); - intent.addCategory(Intent.CATEGORY_OPENABLE); - intent.setType(mimeType); - intent.putExtra(Intent.EXTRA_TITLE, fileName); - startActivityForResult(intent, intentId); + if (settings.getBackupAsk()) { + Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType(mimeType); + intent.putExtra(Intent.EXTRA_TITLE, fileName); + startActivityForResult(intent, intentId); + } else { + if (Tools.mkdir(settings.getBackupDir())) { + if (intentId == INTENT_SAVE_DOCUMENT_PLAIN) + doBackupPlain(Tools.buildUri(settings.getBackupDir(), DEFAULT_BACKUP_FILENAME_PLAIN)); + else if (intentId == INTENT_SAVE_DOCUMENT_CRYPT) + doBackupCrypt(Tools.buildUri(settings.getBackupDir(), DEFAULT_BACKUP_FILENAME_CRYPT)); + else if (intentId == INTENT_SAVE_DOCUMENT_PGP) + backupEncryptedWithPGP(Tools.buildUri(settings.getBackupDir(), DEFAULT_BACKUP_FILENAME_PGP), null); + } else { + Toast.makeText(this, R.string.backup_toast_mkdir_failed, Toast.LENGTH_LONG).show(); + } + } } private void openFileWithPermissions(int intentId, int requestId) { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java index cf71d6f5..200f3284 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Settings.java @@ -24,6 +24,7 @@ package org.shadowice.flocke.andotp.Utilities; import android.content.Context; import android.content.SharedPreferences; +import android.os.Environment; import android.preference.PreferenceManager; import android.util.Base64; @@ -31,6 +32,7 @@ import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.shadowice.flocke.andotp.R; +import java.io.File; import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.util.Collections; @@ -39,6 +41,8 @@ import java.util.Set; import static org.shadowice.flocke.andotp.Preferences.PasswordEncryptedPreference.KEY_ALIAS; public class Settings { + private static final String DEFAULT_BACKUP_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "andOTP"; + private Context context; private SharedPreferences settings; @@ -54,9 +58,17 @@ public class Settings { this.context = context; this.settings = PreferenceManager.getDefaultSharedPreferences(context); + setupDeviceDependedDefaults(); migrateDeprecatedSettings(); } + private void setupDeviceDependedDefaults() { + if (! settings.contains(getResString(R.string.settings_key_backup_directory)) + || settings.getString(getResString(R.string.settings_key_backup_directory), "").isEmpty()) { + setString(R.string.settings_key_backup_directory, DEFAULT_BACKUP_FOLDER); + } + } + private void migrateDeprecatedSettings() { if (settings.contains(getResString(R.string.settings_key_auth_password))) { String plainPassword = getAuthPassword(); @@ -211,6 +223,14 @@ public class Settings { setString(R.string.settings_key_sort_mode, value.toString()); } + public boolean getBackupAsk() { + return getBoolean(R.string.settings_key_backup_ask, true); + } + + public String getBackupDir() { + return getString(R.string.settings_key_backup_directory, DEFAULT_BACKUP_FOLDER); + } + public String getBackupPassword() { return getString(R.string.settings_key_backup_password, ""); } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java index 404d3bf3..ef434d77 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Tools.java @@ -23,16 +23,15 @@ package org.shadowice.flocke.andotp.Utilities; import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.ColorFilter; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; +import android.net.Uri; import android.os.Environment; -import java.util.List; +import java.io.File; public class Tools { /* Checks if external storage is available for read and write */ @@ -47,14 +46,6 @@ public class Tools { return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); } - /* Check is there is a handler for an Intent */ - public static boolean isIntentAvailable(Context context, String action) { - final PackageManager packageManager = context.getPackageManager(); - final Intent intent = new Intent(action); - List resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); - return resolveInfo.size() > 0; - } - /* Get a color based on the current theme */ public static int getThemeColor(Context context, int colorAttr) { Resources.Theme theme = context.getTheme(); @@ -70,4 +61,13 @@ public class Tools { public static ColorFilter getThemeColorFilter(Context context, int colorAttr) { return new PorterDuffColorFilter(getThemeColor(context, colorAttr), PorterDuff.Mode.SRC_IN); } + + public static Uri buildUri(String base, String name) { + return Uri.fromFile(new File(base, name)); + } + + public static boolean mkdir(String path) { + File dir = new File(path); + return dir.exists() || dir.mkdirs(); + } } diff --git a/app/src/main/res/values/settings.xml b/app/src/main/res/values/settings.xml index d37a7137..5a558ea9 100644 --- a/app/src/main/res/values/settings.xml +++ b/app/src/main/res/values/settings.xml @@ -14,6 +14,8 @@ pref_theme pref_label_size + pref_backup_ask + pref_backup_directory pref_backup_password pref_backup_password_enc pref_openpgp_provider diff --git a/app/src/main/res/values/strings_backup.xml b/app/src/main/res/values/strings_backup.xml index d7f5560d..0d8d24c4 100644 --- a/app/src/main/res/values/strings_backup.xml +++ b/app/src/main/res/values/strings_backup.xml @@ -39,6 +39,7 @@ + Failed to create backup directory Export to external storage successful Export to external storage failed Import from external storage successful diff --git a/app/src/main/res/values/strings_settings.xml b/app/src/main/res/values/strings_settings.xml index 23cd88f6..b34cd8eb 100644 --- a/app/src/main/res/values/strings_settings.xml +++ b/app/src/main/res/values/strings_settings.xml @@ -17,6 +17,8 @@ Theme Label font size + Ask for filename + Backup directory Backup password Select OpenPGP provider Select OpenPGP key @@ -28,6 +30,10 @@ revealed manually Decide what happens when a Panic Trigger is received + Ask for the filename every time a backup is created or + restored + Directory for the backups (filenames will depend + on the backup type) Set the password that is used to encrypt the backups diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 89dbc5af..d8d5047e 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -57,6 +57,19 @@ + + + +