Add the option to set a static backup directory

Closes #52
This commit is contained in:
Jakob Nixdorf 2017-10-14 14:40:39 +02:00
parent 98061314a6
commit bafeffb239
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
7 changed files with 84 additions and 20 deletions

View file

@ -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) {

View file

@ -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, "");
}

View file

@ -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();
}
}

View file

@ -14,6 +14,8 @@
<string name="settings_key_theme" translatable="false">pref_theme</string>
<string name="settings_key_label_size" translatable="false">pref_label_size</string>
<string name="settings_key_backup_ask" translatable="false">pref_backup_ask</string>
<string name="settings_key_backup_directory" translatable="false">pref_backup_directory</string>
<string name="settings_key_backup_password" translatable="false">pref_backup_password</string>
<string name="settings_key_backup_password_enc" translatable="false">pref_backup_password_enc</string>
<string name="settings_key_openpgp_provider" translatable="false">pref_openpgp_provider</string>

View file

@ -39,6 +39,7 @@
</string>
<!-- Toast messages -->
<string name="backup_toast_mkdir_failed">Failed to create backup directory</string>
<string name="backup_toast_export_success">Export to external storage successful</string>
<string name="backup_toast_export_failed">Export to external storage failed</string>
<string name="backup_toast_import_success">Import from external storage successful</string>

View file

@ -17,6 +17,8 @@
<string name="settings_title_theme">Theme</string>
<string name="settings_title_label_size">Label font size</string>
<string name="settings_title_backup_ask">Ask for filename</string>
<string name="settings_title_backup_directory">Backup directory</string>
<string name="settings_title_backup_password">Backup password</string>
<string name="settings_title_openpgp_provider">Select OpenPGP provider</string>
<string name="settings_title_openpgp_keyid">Select OpenPGP key</string>
@ -28,6 +30,10 @@
revealed manually</string>
<string name="settings_desc_panic">Decide what happens when a Panic Trigger is received</string>
<string name="settings_desc_backup_ask">Ask for the filename every time a backup is created or
restored</string>
<string name="settings_desc_backup_directory">Directory for the backups (filenames will depend
on the backup type)</string>
<string name="settings_desc_backup_password">Set the password that is used to encrypt the
backups</string>

View file

@ -57,6 +57,19 @@
<PreferenceCategory
android:title="@string/settings_category_title_backup">
<CheckBoxPreference
android:key="@string/settings_key_backup_ask"
android:title="@string/settings_title_backup_ask"
android:summary="@string/settings_desc_backup_ask"
android:disableDependentsState="true"
android:defaultValue="true" />
<EditTextPreference
android:key="@string/settings_key_backup_directory"
android:title="@string/settings_title_backup_directory"
android:summary="@string/settings_desc_backup_directory"
android:dependency="@string/settings_key_backup_ask" />
<org.shadowice.flocke.andotp.Preferences.PasswordEncryptedPreference
android:key="@string/settings_key_backup_password_enc"
android:title="@string/settings_title_backup_password"