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..007e4c0 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,43 @@ 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.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 } \ 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..13d765f 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -98,6 +98,10 @@ Last APDU exception: Save Diagnostics at %s + Server address is required + Server address not is URL + Invalid Server address + Invalid IMEI format Logs have been saved to the selected path. Would you like to share the log through another app?