Compare commits

...

1 commit

Author SHA1 Message Date
1743e20fdc
feat: stricted imei checking 2025-03-07 07:07:39 +08:00
2 changed files with 43 additions and 4 deletions

View file

@ -48,9 +48,8 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF
matchingId = view.requireViewById(R.id.profile_download_code)
confirmationCode = view.requireViewById(R.id.profile_download_confirmation_code)
imei = view.requireViewById(R.id.profile_download_imei)
smdp.editText!!.addTextChangedListener {
updateInputCompleteness()
}
smdp.editText!!.addTextChangedListener { updateInputCompleteness() }
imei.editText!!.addTextChangedListener { updateInputCompleteness() }
return view
}
@ -69,7 +68,43 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF
}
private fun updateInputCompleteness() {
inputComplete = Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches()
validate()
val errors = arrayOf(
smdp.error,
imei.error,
)
inputComplete = errors.all { it == null }
refreshButtons()
}
private fun validate() {
smdp.error = smdp.editText!!.text?.let {
when {
it.isEmpty() -> getString(R.string.download_wizard_error_address_required)
it.contains("://") -> getString(R.string.download_wizard_error_cannot_url)
Patterns.DOMAIN_NAME.matcher(it).matches() -> null
else -> getString(R.string.download_wizard_error_address_invalid_format)
}
}
imei.error = imei.editText!!.text?.let {
when {
it.isEmpty() -> null
it.length == 15 && luhnValid(it) -> null
else -> getString(R.string.download_wizard_error_imei_invalid_format)
}
}
}
}
private fun luhnValid(number: CharSequence, mod: Int = 10): Boolean {
if (!number.all(Char::isDigit)) return false
var checksum = 0
for (i in number.length - 1 downTo 0 step 2) {
checksum += number[i] - '0'
}
for (i in number.length - 2 downTo 0 step 2) {
val n: Int = (number[i] - '0') * 2
checksum += if (n > 9) n - 9 else n
}
return checksum % mod == 0
}

View file

@ -98,6 +98,10 @@
<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_file_template">Diagnostics at %s</string>
<string name="download_wizard_error_address_required">Server address is required</string>
<string name="download_wizard_error_cannot_url">Server address not is URL</string>
<string name="download_wizard_error_address_invalid_format">Invalid Server address</string>
<string name="download_wizard_error_imei_invalid_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>