Improve some things

This commit is contained in:
MrLetsplay 2023-09-17 21:04:28 +02:00
parent 94059ad623
commit 3981e525eb
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 46 additions and 37 deletions

View File

@ -93,14 +93,11 @@ public class GroupFragment extends NamedFragment {
break; break;
case 2: case 2:
DialogUtil.showChooseGroupDialog(requireContext(), group -> { DialogUtil.showChooseGroupDialog(requireContext(), group -> {
if(OTPDatabase.getLoadedDatabase() == null) { OTPDatabase.promptLoadDatabase(requireContext(), () -> {
// TODO: prompt user
return;
}
OTPDatabase.getLoadedDatabase().addOTP(group, data); OTPDatabase.getLoadedDatabase().addOTP(group, data);
// TODO: save // TODO: save
otpListAdapter.remove(data); otpListAdapter.remove(data);
}, () -> DialogUtil.showErrorDialog(requireContext(), "Failed to add OTP"));
saveOTPs(); saveOTPs();
}, null); }, null);
break; break;

View File

@ -107,12 +107,12 @@ public class OTPData implements Serializable {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
OTPData otpData = (OTPData) o; OTPData otpData = (OTPData) o;
return digits == otpData.digits && period == otpData.period && counter == otpData.counter && checksum == otpData.checksum && Objects.equals(name, otpData.name) && Objects.equals(issuer, otpData.issuer) && type == otpData.type && Objects.equals(secret, otpData.secret) && algorithm == otpData.algorithm && Objects.equals(otp, otpData.otp); return digits == otpData.digits && period == otpData.period && counter == otpData.counter && checksum == otpData.checksum && Objects.equals(name, otpData.name) && Objects.equals(issuer, otpData.issuer) && type == otpData.type && Objects.equals(secret, otpData.secret) && algorithm == otpData.algorithm;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(name, issuer, type, secret, algorithm, digits, period, counter, checksum, otp); return Objects.hash(name, issuer, type, secret, algorithm, digits, period, counter, checksum);
} }
} }

View File

@ -65,16 +65,14 @@ public class URIHandlerActivity extends AppCompatActivity {
private void importCodes(OTPData... data) { private void importCodes(OTPData... data) {
DialogUtil.showChooseGroupDialog(this, group -> { DialogUtil.showChooseGroupDialog(this, group -> {
for(OTPData d : data) { for(OTPData d : data) {
if(OTPDatabase.getLoadedDatabase() == null) { OTPDatabase.promptLoadDatabase(this, () -> {
// TODO: prompt user
}
OTPDatabase.getLoadedDatabase().addOTP(group, d); OTPDatabase.getLoadedDatabase().addOTP(group, d);
try { try {
OTPDatabase.saveDatabase(this, SettingsUtil.getCryptoParameters(this)); OTPDatabase.saveDatabase(this, SettingsUtil.getCryptoParameters(this));
} catch (OTPDatabaseException | CryptoException e) { } catch (OTPDatabaseException | CryptoException e) {
DialogUtil.showErrorDialog(this, e.toString()); DialogUtil.showErrorDialog(this, e.toString());
} }
}, () -> finishAffinity());
} }
Toast.makeText(this, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.uri_handler_code_added, Toast.LENGTH_SHORT).show();
}, this::finishAndRemoveTask); }, this::finishAndRemoveTask);

View File

@ -47,14 +47,19 @@ public class DialogUtil {
dialog.show(); dialog.show();
} }
public static void showErrorDialog(Context context, String errorMessage) { public static void showErrorDialog(Context context, String errorMessage, Runnable closed) {
new StyledDialogBuilder(context) new StyledDialogBuilder(context)
.setTitle(R.string.failed_title) .setTitle(R.string.failed_title)
.setMessage(errorMessage) .setMessage(errorMessage)
.setPositiveButton(R.string.ok, (d, which) -> {}) .setPositiveButton(R.string.ok, (d, which) -> {})
.setOnDismissListener(d -> { if(closed != null) closed.run(); })
.show(); .show();
} }
public static void showErrorDialog(Context context, String errorMessage) {
showErrorDialog(context, errorMessage, null);
}
public static void showTOTPDialog(LayoutInflater inflater, OTPData initialData, Consumer<OTPData> callback, Runnable back, boolean view) { public static void showTOTPDialog(LayoutInflater inflater, OTPData initialData, Consumer<OTPData> callback, Runnable back, boolean view) {
Context context = inflater.getContext(); Context context = inflater.getContext();
DialogInputCodeTotpBinding binding = DialogInputCodeTotpBinding.inflate(inflater); DialogInputCodeTotpBinding binding = DialogInputCodeTotpBinding.inflate(inflater);
@ -262,23 +267,29 @@ public class DialogUtil {
dialog.show(); dialog.show();
} }
public static void showInputPasswordDialog(Context context, Consumer<String> callback, Runnable onDismiss) { public static void showInputPasswordDialog(Context context, Consumer<String> callback, Runnable onCancel) {
EditText passwordField = new EditText(context); EditText passwordField = new EditText(context);
passwordField.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); passwordField.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
AlertDialog dialog = new StyledDialogBuilder(context) AlertDialog dialog = new StyledDialogBuilder(context)
.setTitle("Input Password") .setTitle("Input Password")
.setView(passwordField) // TODO: better layout .setView(passwordField) // TODO: better layout
.setPositiveButton("Ok", (d, which) -> { .setPositiveButton("Ok", (d, which) -> {})
.setNegativeButton(R.string.cancel, (d, which) -> { if(onCancel != null) onCancel.run(); })
.setOnCancelListener(d -> { if(onCancel != null) onCancel.run(); })
.create();
dialog.setOnShowListener(d -> {
Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
okButton.setOnClickListener(v -> {
if(passwordField.getText().length() == 0) { if(passwordField.getText().length() == 0) {
DialogUtil.showErrorDialog(context, "You need to enter a password"); DialogUtil.showErrorDialog(context, "You need to enter a password");
return; return;
} }
dialog.dismiss();
callback.accept(passwordField.getText().toString()); callback.accept(passwordField.getText().toString());
}) });
.setNegativeButton(R.string.cancel, (d, which) -> { if(onDismiss != null) onDismiss.run(); }) });
.setOnCancelListener(d -> { if(onDismiss != null) onDismiss.run(); })
.create();
dialog.show(); dialog.show();
} }

View File

@ -2,6 +2,8 @@ package com.cringe_studios.cringe_authenticator.util;
import android.content.Context; import android.content.Context;
import androidx.core.util.Consumer;
import com.cringe_studios.cringe_authenticator.crypto.Crypto; import com.cringe_studios.cringe_authenticator.crypto.Crypto;
import com.cringe_studios.cringe_authenticator.crypto.CryptoException; import com.cringe_studios.cringe_authenticator.crypto.CryptoException;
import com.cringe_studios.cringe_authenticator.crypto.CryptoParameters; import com.cringe_studios.cringe_authenticator.crypto.CryptoParameters;
@ -69,19 +71,20 @@ public class OTPDatabase {
loadDatabase(ctx, null); loadDatabase(ctx, null);
if(success != null) success.run(); if(success != null) success.run();
} catch (OTPDatabaseException | CryptoException e) { } catch (OTPDatabaseException | CryptoException e) {
throw new RuntimeException(e); // TODO DialogUtil.showErrorDialog(ctx, "Failed to load database: " + e, failure);
} }
return; return;
} }
DialogUtil.showErrorDialog(ctx, "LOAD DB!"); // TODO: implement
DialogUtil.showInputPasswordDialog(ctx, password -> { DialogUtil.showInputPasswordDialog(ctx, password -> {
try { try {
SecretKey key = Crypto.generateKey(SettingsUtil.getCryptoParameters(ctx), password); SecretKey key = Crypto.generateKey(SettingsUtil.getCryptoParameters(ctx), password);
loadDatabase(ctx, key); loadDatabase(ctx, key);
if(success != null) success.run(); if (success != null) success.run();
} catch (CryptoException | OTPDatabaseException e) { }catch(CryptoException e) {
failure.run(); // TODO: show error DialogUtil.showErrorDialog(ctx, "Failed to load database: Invalid password or database corrupted", failure);
} catch (OTPDatabaseException e) {
DialogUtil.showErrorDialog(ctx, "Failed to load database: " + e, failure);
} }
}, failure); }, failure);
} }