Improve some things
This commit is contained in:
parent
94059ad623
commit
3981e525eb
@ -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
|
OTPDatabase.getLoadedDatabase().addOTP(group, data);
|
||||||
return;
|
// TODO: save
|
||||||
}
|
otpListAdapter.remove(data);
|
||||||
|
}, () -> DialogUtil.showErrorDialog(requireContext(), "Failed to add OTP"));
|
||||||
OTPDatabase.getLoadedDatabase().addOTP(group, data);
|
|
||||||
// TODO: save
|
|
||||||
otpListAdapter.remove(data);
|
|
||||||
saveOTPs();
|
saveOTPs();
|
||||||
}, null);
|
}, null);
|
||||||
break;
|
break;
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
try {
|
||||||
|
OTPDatabase.saveDatabase(this, SettingsUtil.getCryptoParameters(this));
|
||||||
OTPDatabase.getLoadedDatabase().addOTP(group, d);
|
} catch (OTPDatabaseException | CryptoException e) {
|
||||||
try {
|
DialogUtil.showErrorDialog(this, e.toString());
|
||||||
OTPDatabase.saveDatabase(this, SettingsUtil.getCryptoParameters(this));
|
}
|
||||||
} catch (OTPDatabaseException | CryptoException e) {
|
}, () -> finishAffinity());
|
||||||
DialogUtil.showErrorDialog(this, e.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
@ -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,24 +267,30 @@ 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) -> {})
|
||||||
if(passwordField.getText().length() == 0) {
|
.setNegativeButton(R.string.cancel, (d, which) -> { if(onCancel != null) onCancel.run(); })
|
||||||
DialogUtil.showErrorDialog(context, "You need to enter a password");
|
.setOnCancelListener(d -> { if(onCancel != null) onCancel.run(); })
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback.accept(passwordField.getText().toString());
|
|
||||||
})
|
|
||||||
.setNegativeButton(R.string.cancel, (d, which) -> { if(onDismiss != null) onDismiss.run(); })
|
|
||||||
.setOnCancelListener(d -> { if(onDismiss != null) onDismiss.run(); })
|
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
|
dialog.setOnShowListener(d -> {
|
||||||
|
Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
||||||
|
okButton.setOnClickListener(v -> {
|
||||||
|
if(passwordField.getText().length() == 0) {
|
||||||
|
DialogUtil.showErrorDialog(context, "You need to enter a password");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dialog.dismiss();
|
||||||
|
callback.accept(passwordField.getText().toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
dialog.show();
|
dialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user