Add new persistent storage preference

This commit is contained in:
Jakob Nixdorf 2020-05-27 09:11:13 +02:00
parent bf89bca9b4
commit 356815d7e7
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
6 changed files with 61 additions and 10 deletions

View file

@ -27,6 +27,7 @@ import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
@ -141,7 +142,8 @@ public class SettingsActivity extends BaseActivity
if (key.equals(getString(R.string.settings_key_theme)) ||
key.equals(getString(R.string.settings_key_locale)) ||
key.equals(getString(R.string.settings_key_special_features))) {
key.equals(getString(R.string.settings_key_special_features)) ||
key.equals(getString(R.string.settings_key_backup_location))) {
recreate();
} else if(key.equals(getString(R.string.settings_key_encryption))) {
if (settings.getEncryption() != EncryptionType.PASSWORD) {
@ -232,6 +234,15 @@ public class SettingsActivity extends BaseActivity
return false;
}
private void requestBackupAccess() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
startActivityForResult(intent, Constants.INTENT_SETTINGS_BACKUP_LOCATION);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
@ -250,6 +261,13 @@ public class SettingsActivity extends BaseActivity
} else {
Toast.makeText(this, R.string.settings_toast_encryption_auth_failed, Toast.LENGTH_LONG).show();
}
} else if (requestCode == Constants.INTENT_SETTINGS_BACKUP_LOCATION && resultCode == RESULT_OK) {
Uri treeUri = data.getData();
if (treeUri != null) {
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
settings.setBackupLocation(treeUri);
}
} else if (fragment.pgpSigningKey.handleOnActivityResult(requestCode, resultCode, data)) {
// handled by OpenPgpKeyPreference
return;
@ -261,6 +279,7 @@ public class SettingsActivity extends BaseActivity
Settings settings;
ListPreference encryption;
Preference backupLocation;
ListPreference useAutoBackup;
CheckBoxPreference useAndroidSync;
@ -342,6 +361,23 @@ public class SettingsActivity extends BaseActivity
}
});
// Backup location
backupLocation = findPreference(getString(R.string.settings_key_backup_location));
if (settings.isBackupLocationSet()) {
backupLocation.setSummary(R.string.settings_desc_backup_location_set);
} else {
backupLocation.setSummary(R.string.settings_desc_backup_location);
}
backupLocation.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
((SettingsActivity) getActivity()).requestBackupAccess();
return true;
}
});
// OpenPGP
pgpProvider = (OpenPgpAppPreference) findPreference(getString(R.string.settings_key_openpgp_provider));
pgpEncryptionKey = (EditTextPreference) findPreference(getString(R.string.settings_key_openpgp_key_encrypt));

View file

@ -86,6 +86,7 @@ public class Constants {
public final static int INTENT_BACKUP_OPEN_DOCUMENT_CRYPT_OLD = 208;
public static final int INTENT_SETTINGS_AUTHENTICATE = 300;
public static final int INTENT_SETTINGS_BACKUP_LOCATION = 301;
// Intent extras
public final static String EXTRA_AUTH_PASSWORD_KEY = "password_key";

View file

@ -24,6 +24,7 @@ package org.shadowice.flocke.andotp.Utilities;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Base64;
@ -612,4 +613,16 @@ public class Settings {
String doubleTap = getString(R.string.settings_key_tap_double, R.string.settings_default_tap_double);
return Constants.TapMode.valueOf(doubleTap.toUpperCase(Locale.ENGLISH));
}
public void setBackupLocation(Uri uri) {
setString(R.string.settings_key_backup_location, uri.toString());
}
public Uri getBackupLocation() {
return Uri.parse(getString(R.string.settings_key_backup_location, ""));
}
public boolean isBackupLocationSet() {
return !getString(R.string.settings_key_backup_location, "").isEmpty();
}
}

View file

@ -40,8 +40,9 @@
<string name="settings_key_backup_append_date_time" translatable="false">pref_backup_append_date_time</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> <!-- Deprecated -->
<string name="settings_key_backup_location" translatable="false">pref_backup_location</string>
<string name="settings_key_backup_directory" translatable="false">pref_backup_directory</string> <!-- Deprecated -->
<string name="settings_key_backup_password" translatable="false">pref_backup_password</string> <!-- Deprecated -->
<string name="settings_key_backup_password_enc" translatable="false">pref_backup_password_enc</string>
<string name="settings_key_auto_backup_password_enc" translatable="false">pref_backup_auto_password_enc</string>
<string name="settings_key_backup_broadcasts" translatable="false">pref_backup_broadcasts</string>

View file

@ -39,7 +39,7 @@
<string name="settings_title_backup_append_date">Append date to filename</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_location">Default backup location</string>
<string name="settings_title_backup_password">Backup password</string>
<string name="settings_title_auto_backup_password_enc">Automatically backup on new entries</string>
@ -83,8 +83,8 @@
backup filename</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 automatic and broadcast backups
(filenames will depend on the backup type)</string>
<string name="settings_desc_backup_location">Choose a default backup location</string>
<string name="settings_desc_backup_location_set">Backup location has been chosen</string>
<string name="settings_desc_backup_password">Set the password that is used to encrypt the
backups</string>

View file

@ -206,10 +206,10 @@
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" />
<Preference
android:key="@string/settings_key_backup_location"
android:title="@string/settings_title_backup_location"
android:summary="@string/settings_desc_backup_location" />
<org.shadowice.flocke.andotp.Preferences.PasswordEncryptedPreference
android:key="@string/settings_key_backup_password_enc"