Fixes #569 - Validate expected Integer input for the Digits and Period fields, and expected Long input for the Counter field

This commit is contained in:
Joshua Soberg 2020-05-29 09:33:53 -04:00
parent eaa9123b43
commit 7c283980ff

View file

@ -278,27 +278,36 @@ public class ManualEntryDialog {
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
if ((TextUtils.isEmpty(labelInput.getText()) && TextUtils.isEmpty(issuerInput.getText())) || if ((TextUtils.isEmpty(labelInput.getText()) && TextUtils.isEmpty(issuerInput.getText())) ||
(TextUtils.isEmpty(secretInput.getText()) && isNewEntry) || (TextUtils.isEmpty(secretInput.getText()) && isNewEntry) ||
TextUtils.isEmpty(digitsInput.getText()) || !isNonZeroIntegerInput(digitsInput)) {
Integer.parseInt(digitsInput.getText().toString()) == 0) {
positiveButton.setEnabled(false); positiveButton.setEnabled(false);
} else { } else {
Entry.OTPType type = (Entry.OTPType) typeInput.getSelectedItem(); Entry.OTPType type = (Entry.OTPType) typeInput.getSelectedItem();
if (type == Entry.OTPType.HOTP) { if (type == Entry.OTPType.HOTP) {
if (TextUtils.isEmpty(counterInput.getText())) { positiveButton.setEnabled(isZeroOrPositiveLongInput(counterInput));
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
} else if (type == Entry.OTPType.TOTP || type == Entry.OTPType.STEAM) { } else if (type == Entry.OTPType.TOTP || type == Entry.OTPType.STEAM) {
if (TextUtils.isEmpty(periodInput.getText()) || Integer.parseInt(periodInput.getText().toString()) == 0) { positiveButton.setEnabled(isNonZeroIntegerInput(periodInput));
positiveButton.setEnabled(false);
} else { } else {
positiveButton.setEnabled(true); positiveButton.setEnabled(true);
} }
} else {
positiveButton.setEnabled(true);
} }
} }
private boolean isNonZeroIntegerInput(EditText editText) {
try {
Editable text = editText.getText();
return !TextUtils.isEmpty(text) && (Integer.parseInt(text.toString()) != 0);
} catch (NumberFormatException e) {
return false;
}
}
private boolean isZeroOrPositiveLongInput(EditText editText) {
try {
Editable text = editText.getText();
return !TextUtils.isEmpty(text) && (Long.parseLong(text.toString()) >= 0);
} catch (NumberFormatException e) {
return false;
}
} }
}; };