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) {
if ((TextUtils.isEmpty(labelInput.getText()) && TextUtils.isEmpty(issuerInput.getText())) ||
(TextUtils.isEmpty(secretInput.getText()) && isNewEntry) ||
TextUtils.isEmpty(digitsInput.getText()) ||
Integer.parseInt(digitsInput.getText().toString()) == 0) {
!isNonZeroIntegerInput(digitsInput)) {
positiveButton.setEnabled(false);
} else {
Entry.OTPType type = (Entry.OTPType) typeInput.getSelectedItem();
if (type == Entry.OTPType.HOTP) {
if (TextUtils.isEmpty(counterInput.getText())) {
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
positiveButton.setEnabled(isZeroOrPositiveLongInput(counterInput));
} else if (type == Entry.OTPType.TOTP || type == Entry.OTPType.STEAM) {
if (TextUtils.isEmpty(periodInput.getText()) || Integer.parseInt(periodInput.getText().toString()) == 0) {
positiveButton.setEnabled(false);
positiveButton.setEnabled(isNonZeroIntegerInput(periodInput));
} else {
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;
}
}
};