Backup restore success dialog

This commit is contained in:
MrLetsplay 2023-09-27 16:04:27 +02:00
parent 2e88d021bb
commit 9265a2f0ff
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 45 additions and 25 deletions

View File

@ -2,7 +2,6 @@ package com.cringe_studios.cringe_authenticator;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -330,7 +329,7 @@ public class MainActivity extends BaseActivity {
lockOnStop = true;
callback.accept(uri);
};
pickBackupFileLoad.launch(new String[]{"application/json"});
pickBackupFileLoad.launch(new String[]{"application/json", "*/*"});
}
@Override
@ -342,7 +341,6 @@ public class MainActivity extends BaseActivity {
@Override
protected void onStop() {
super.onStop();
Log.d("STOP", lockOnStop+"");
if(lockOnStop) OTPDatabase.unloadDatabase();
}

View File

@ -267,27 +267,32 @@ public class SettingsFragment extends NamedFragment {
}
private void loadBackup(Uri uri) {
OTPDatabase.promptLoadDatabase(requireActivity(), () -> {
if(SettingsUtil.isDatabaseEncrypted(requireContext())) {
try {
SecretKey key = OTPDatabase.getLoadedKey();
CryptoParameters parameters = SettingsUtil.getCryptoParameters(requireContext());
loadBackup(uri, key, parameters);
} catch (CryptoException ignored) { // Load with password
} catch (BackupException | OTPDatabaseException e) {
DialogUtil.showErrorDialog(requireContext(), "Failed to load backup", e);
return;
DialogUtil.showYesNo(requireContext(), R.string.load_backup_title, R.string.backup_load_message, () -> {
OTPDatabase.promptLoadDatabase(requireActivity(), () -> {
if(SettingsUtil.isDatabaseEncrypted(requireContext())) {
try {
SecretKey key = OTPDatabase.getLoadedKey();
CryptoParameters parameters = SettingsUtil.getCryptoParameters(requireContext());
loadBackup(uri, key, parameters);
return;
} catch (CryptoException ignored) { // Load with password
} catch (BackupException | OTPDatabaseException e) {
DialogUtil.showErrorDialog(requireContext(), "Failed to load backup", e);
return;
}
}
}
DialogUtil.showInputPasswordDialog(requireContext(), password -> {
try {
CryptoParameters parameters = BackupUtil.loadParametersFromBackup(requireContext(), uri);
SecretKey key = Crypto.generateKey(parameters, password);
loadBackup(uri, key, parameters);
} catch (BackupException | OTPDatabaseException | CryptoException e2) {
DialogUtil.showErrorDialog(requireContext(), "Failed to load backup", e2);
}
DialogUtil.showInputPasswordDialog(requireContext(), password -> {
try {
CryptoParameters parameters = BackupUtil.loadParametersFromBackup(requireContext(), uri);
SecretKey key = Crypto.generateKey(parameters, password);
loadBackup(uri, key, parameters);
} catch (CryptoException e) {
DialogUtil.showErrorDialog(requireContext(), "Failed to load backup. Make sure the password is valid", e);
} catch (BackupException | OTPDatabaseException e) {
DialogUtil.showErrorDialog(requireContext(), "Failed to load backup", e);
}
}, null);
}, null);
}, null);
}
@ -302,6 +307,8 @@ public class SettingsFragment extends NamedFragment {
SettingsUtil.restoreGroups(requireContext(), data.getGroups());
OTPDatabase.setLoadedDatabase(db);
OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext()));
DialogUtil.showBackupLoadedDialog(requireContext(), data);
} catch (OTPDatabaseException | CryptoException e) {
OTPDatabase.setLoadedDatabase(oldDatabase);
throw new RuntimeException(e);

View File

@ -12,6 +12,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.core.util.Consumer;
import com.cringe_studios.cringe_authenticator.R;
import com.cringe_studios.cringe_authenticator.backup.BackupData;
import com.cringe_studios.cringe_authenticator.databinding.DialogCreateGroupBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeHotpBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeTotpBinding;
@ -277,7 +278,7 @@ public class DialogUtil {
AlertDialog dialog = new StyledDialogBuilder(context)
.setTitle(R.string.set_password)
.setView(binding.getRoot())
.setPositiveButton("Ok", (d, which) -> {})
.setPositiveButton(R.string.ok, (d, which) -> {})
.setNegativeButton(R.string.cancel, (d, which) -> { if(onCancel != null) onCancel.run(); })
.setOnCancelListener(d -> { if(onCancel != null) onCancel.run(); })
.create();
@ -310,8 +311,8 @@ public class DialogUtil {
AlertDialog dialog = new StyledDialogBuilder(context)
.setTitle("Input Password")
.setView(binding.inputPassword)
.setPositiveButton("Ok", (d, which) -> {})
.setView(binding.getRoot())
.setPositiveButton(R.string.ok, (d, which) -> {})
.setNegativeButton(R.string.cancel, (d, which) -> { if(onCancel != null) onCancel.run(); })
.setOnCancelListener(d -> { if(onCancel != null) onCancel.run(); })
.create();
@ -371,4 +372,14 @@ public class DialogUtil {
showYesNoCancel(context, title, message, R.string.yes, R.string.no, R.string.cancel, yes, no, cancel);
}
public static void showBackupLoadedDialog(Context context, BackupData data) {
AlertDialog dialog = new StyledDialogBuilder(context)
.setTitle("Backup loaded")
.setMessage(String.format("Successfully loaded %s group(s) from the backup", data.getGroups().length))
.setPositiveButton(R.string.ok, (d, which) -> {})
.create();
dialog.show();
}
}

View File

@ -96,4 +96,6 @@
<string name="enter_password">Passwort eingeben</string>
<string name="disable_encryption_title">Verschlüsselung deaktivieren</string>
<string name="disable_encryption_message">Willst du wirklich die Verschlüsselung deaktivieren?</string>
<string name="load_backup_title">Backup laden</string>
<string name="backup_load_message">Willst du dieses Backup laden?\n\nDadurch werden ALLE Daten der App gelöscht und mit denen aus dem Backup ersetzt!</string>
</resources>

View File

@ -97,4 +97,6 @@
<string name="enter_password">Enter Password</string>
<string name="disable_encryption_title">Disable encryption</string>
<string name="disable_encryption_message">Do you really want to disable encryption?</string>
<string name="load_backup_title">Load backup</string>
<string name="backup_load_message">Do you want to load this backup?\n\nThis will delete ALL of the current data in the app and replace it with the data from the backup!</string>
</resources>