android: Report an error for invalid integer values

Previously we'd just ignore the invalid values without notifying the
user.
This commit is contained in:
Tobias Brunner 2016-11-02 16:33:29 +01:00
parent cefbf2bf9b
commit ef2ad9db1c
1 changed files with 27 additions and 4 deletions

View File

@ -496,14 +496,12 @@ public class VpnProfileDetailActivity extends AppCompatActivity
showCertificateAlert();
valid = false;
}
Integer mtu = getInteger(mMTU);
if (mtu != null && (mtu < MTU_MIN || mtu > MTU_MAX))
if (!validateInteger(mMTU, MTU_MIN, MTU_MAX))
{
mMTUWrap.setError(String.format(getString(R.string.alert_text_out_of_range), MTU_MIN, MTU_MAX));
valid = false;
}
Integer port = getInteger(mPort);
if (port != null && (port < 1 || port > 65535))
if (!validateInteger(mPort, 1, 65535))
{
mPortWrap.setError(String.format(getString(R.string.alert_text_out_of_range), 1, 65535));
valid = false;
@ -633,6 +631,31 @@ public class VpnProfileDetailActivity extends AppCompatActivity
}
}
/**
* Check that the value in the given text box is a valid integer in the given range
*
* @param view text box (numeric entry assumed)
* @param min minimum value (inclusive)
* @param max maximum value (inclusive)
*/
private boolean validateInteger(EditText view, Integer min, Integer max)
{
String value = view.getText().toString().trim();
try
{
if (value.isEmpty())
{
return true;
}
Integer val = Integer.valueOf(value);
return min <= val && val <= max;
}
catch (NumberFormatException e)
{
return false;
}
}
private class SelectUserCertOnClickListener implements OnClickListener, KeyChainAliasCallback
{
@Override