Compare commits
1 commit
598c45bd60
...
48b7f0bfef
Author | SHA1 | Date | |
---|---|---|---|
48b7f0bfef |
2 changed files with 15 additions and 25 deletions
|
@ -68,36 +68,28 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateInputCompleteness() {
|
private fun updateInputCompleteness() {
|
||||||
validate()
|
inputComplete = runCatching(::validate).isSuccess
|
||||||
val errors = arrayOf(
|
|
||||||
smdp.editText!!.error,
|
|
||||||
imei.editText!!.error,
|
|
||||||
)
|
|
||||||
inputComplete = errors.all { it == null }
|
|
||||||
refreshButtons()
|
refreshButtons()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun validate() {
|
private fun validate() {
|
||||||
smdp.editText!!.error = smdp.editText!!.text?.let {
|
check(Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches()) {
|
||||||
if (Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches()) return@let null
|
"Invalid SM-DP+ address"
|
||||||
getString(R.string.download_wizard_error_invalid_address_format)
|
|
||||||
}
|
}
|
||||||
imei.editText!!.error = imei.editText!!.text?.let {
|
check(imei.editText!!.text.let { it.isEmpty() || isValidIMEI(it) }) {
|
||||||
if (it.isEmpty() || luhnValid(it)) return@let null
|
"Invalid IMEI"
|
||||||
getString(R.string.download_wizard_error_invalid_imei_format)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun luhnValid(number: CharSequence, mod: Int = 10): Boolean {
|
private fun isValidIMEI(input: CharSequence): Boolean {
|
||||||
if (!number.all(Char::isDigit)) return false
|
if (input.length != 15 || !input.all(Char::isDigit)) return false
|
||||||
var checksum = 0
|
|
||||||
for (i in number.length - 1 downTo 0 step 2) {
|
fun sumOfDigits(input: Int): Int {
|
||||||
checksum += number[i] - '0'
|
if (input % 2 == 0) return input
|
||||||
|
return (input * 2).toString().map(Char::digitToInt).sum()
|
||||||
}
|
}
|
||||||
for (i in number.length - 2 downTo 0 step 2) {
|
|
||||||
val n: Int = (number[i] - '0') * 2
|
val sum = input.dropLast(1).map(Char::digitToInt).sumOf(::sumOfDigits)
|
||||||
checksum += if (n > 9) n - 9 else n
|
return (sum * 9) % 10 == input.last().digitToInt()
|
||||||
}
|
|
||||||
return checksum % mod == 0
|
|
||||||
}
|
}
|
|
@ -98,8 +98,6 @@
|
||||||
<string name="download_wizard_diagnostics_last_apdu_exception">Last APDU exception:</string>
|
<string name="download_wizard_diagnostics_last_apdu_exception">Last APDU exception:</string>
|
||||||
<string name="download_wizard_diagnostics_save">Save</string>
|
<string name="download_wizard_diagnostics_save">Save</string>
|
||||||
<string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string>
|
<string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string>
|
||||||
<string name="download_wizard_error_invalid_address_format">Invalid SM-DP+ address</string>
|
|
||||||
<string name="download_wizard_error_invalid_imei_format">Invalid IMEI format</string>
|
|
||||||
|
|
||||||
<string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string>
|
<string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue