Implement infinite backups

This commit is contained in:
r3dh3ck 2024-07-23 06:08:52 +00:00
parent 2050d29236
commit fc8cdc6502
5 changed files with 35 additions and 12 deletions

View file

@ -33,6 +33,7 @@ public class Preferences {
public static final int AUTO_LOCK_ON_BACK_BUTTON = 1 << 1;
public static final int AUTO_LOCK_ON_MINIMIZE = 1 << 2;
public static final int AUTO_LOCK_ON_DEVICE_LOCK = 1 << 3;
public static final int BACKUPS_VERSIONS_INFINITE = -1;
public static final int[] AUTO_LOCK_SETTINGS = {
AUTO_LOCK_ON_BACK_BUTTON,

View file

@ -318,25 +318,32 @@ public class Dialogs {
}
public static void showBackupVersionsPickerDialog(Context context, int currentVersionCount, NumberInputListener listener) {
final int max = 30;
String[] numbers = new String[max / 5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Integer.toString(i * 5 + 5);
String infinite = context.getString(R.string.pref_backups_versions_infinite);
String[] values = {"5", "10", "15", "20", "25", "30", infinite};
int[] numbers = {5, 10, 15, 20, 25, 30, Preferences.BACKUPS_VERSIONS_INFINITE};
int selectedIndex;
if (currentVersionCount == Preferences.BACKUPS_VERSIONS_INFINITE) {
selectedIndex = numbers.length - 1;
} else {
selectedIndex = currentVersionCount / 5 - 1;
}
View view = LayoutInflater.from(context).inflate(R.layout.dialog_number_picker, null);
NumberPicker numberPicker = view.findViewById(R.id.numberPicker);
numberPicker.setDisplayedValues(numbers);
numberPicker.setMaxValue(numbers.length - 1);
numberPicker.setDisplayedValues(values);
numberPicker.setMaxValue(values.length - 1);
numberPicker.setMinValue(0);
numberPicker.setValue(currentVersionCount / 5 - 1);
numberPicker.setValue(selectedIndex);
numberPicker.setWrapSelectorWheel(false);
AlertDialog dialog = new MaterialAlertDialogBuilder(context)
.setTitle(R.string.set_number)
.setView(view)
.setPositiveButton(android.R.string.ok, (dialog1, which) ->
listener.onNumberInputResult(numberPicker.getValue()))
.setPositiveButton(android.R.string.ok, (dialog1, which) -> {
int index = numberPicker.getValue();
int number = numbers[index];
listener.onNumberInputResult(number);
})
.create();
showSecureDialog(dialog);

View file

@ -131,12 +131,11 @@ public class BackupsPreferencesFragment extends PreferencesFragment {
});
_backupsVersionsPreference = requirePreference("pref_backups_versions");
_backupsVersionsPreference.setSummary(getResources().getQuantityString(R.plurals.pref_backups_versions_summary, _prefs.getBackupsVersionCount(), _prefs.getBackupsVersionCount()));
updateBackupsVersionsSummary();
_backupsVersionsPreference.setOnPreferenceClickListener(preference -> {
Dialogs.showBackupVersionsPickerDialog(requireContext(), _prefs.getBackupsVersionCount(), number -> {
number = number * 5 + 5;
_prefs.setBackupsVersionCount(number);
_backupsVersionsPreference.setSummary(getResources().getQuantityString(R.plurals.pref_backups_versions_summary, _prefs.getBackupsVersionCount(), _prefs.getBackupsVersionCount()));
updateBackupsVersionsSummary();
});
return false;
});
@ -242,4 +241,14 @@ public class BackupsPreferencesFragment extends PreferencesFragment {
Dialogs.showErrorDialog(requireContext(), R.string.backup_error, e);
}
}
private void updateBackupsVersionsSummary() {
int count = _prefs.getBackupsVersionCount();
if (count == Preferences.BACKUPS_VERSIONS_INFINITE) {
_backupsVersionsPreference.setSummary(R.string.pref_backups_versions_infinite_summary);
} else {
String summary = getResources().getQuantityString(R.plurals.pref_backups_versions_summary, count, count);
_backupsVersionsPreference.setSummary(summary);
}
}
}

View file

@ -118,6 +118,10 @@ public class VaultBackupManager {
}
private void enforceVersioning(DocumentFile dir, int versionsToKeep) {
if (versionsToKeep < 0) {
return;
}
Log.i(TAG, String.format("Scanning directory %s for backup files", Uri.decode(dir.getUri().toString())));
List<BackupFile> files = new ArrayList<>();

View file

@ -66,10 +66,12 @@
<string name="pref_backups_trigger_title">Trigger backup</string>
<string name="pref_backups_trigger_summary">Manually trigger a backup</string>
<string name="pref_backups_versions_title">Number of versions to keep</string>
<string name="pref_backups_versions_infinite">\u221E</string>
<plurals name="pref_backups_versions_summary">
<item quantity="one">Keep %1$d version of the backup</item>
<item quantity="other">Keep %1$d versions of the backup</item>
</plurals>
<string name="pref_backups_versions_infinite_summary">Keep an infinite number of versions of the backup</string>
<string name="pref_import_app_title">Import from app</string>
<string name="pref_import_app_summary">Import tokens from an app (requires root access)</string>
<string name="pref_export_title">Export</string>