Compare commits

...

1 commit

Author SHA1 Message Date
598c45bd60
feat: stricted imei checking 2025-03-07 06:33:13 +08:00
2 changed files with 34 additions and 4 deletions

View file

@ -48,9 +48,8 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF
matchingId = view.requireViewById(R.id.profile_download_code) matchingId = view.requireViewById(R.id.profile_download_code)
confirmationCode = view.requireViewById(R.id.profile_download_confirmation_code) confirmationCode = view.requireViewById(R.id.profile_download_confirmation_code)
imei = view.requireViewById(R.id.profile_download_imei) imei = view.requireViewById(R.id.profile_download_imei)
smdp.editText!!.addTextChangedListener { smdp.editText!!.addTextChangedListener { updateInputCompleteness() }
updateInputCompleteness() imei.editText!!.addTextChangedListener { updateInputCompleteness() }
}
return view return view
} }
@ -69,7 +68,36 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF
} }
private fun updateInputCompleteness() { private fun updateInputCompleteness() {
inputComplete = Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches() validate()
val errors = arrayOf(
smdp.editText!!.error,
imei.editText!!.error,
)
inputComplete = errors.all { it == null }
refreshButtons() refreshButtons()
} }
private fun validate() {
smdp.editText!!.error = smdp.editText!!.text?.let {
if (Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches()) return@let null
getString(R.string.download_wizard_error_invalid_address_format)
}
imei.editText!!.error = imei.editText!!.text?.let {
if (it.isEmpty() || luhnValid(it)) return@let null
getString(R.string.download_wizard_error_invalid_imei_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,8 @@
<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>