diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardDetailsFragment.kt b/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardDetailsFragment.kt index 5fa8002..cd758c3 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardDetailsFragment.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardDetailsFragment.kt @@ -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,36 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF } 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() } + + 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 } \ No newline at end of file diff --git a/app-common/src/main/res/values/strings.xml b/app-common/src/main/res/values/strings.xml index a45ce1f..94f7f7a 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -98,6 +98,8 @@ Last APDU exception: Save Diagnostics at %s + Invalid SM-DP+ address + Invalid IMEI format Logs have been saved to the selected path. Would you like to share the log through another app?