From 0f4a428c0046fde371d9aceab5acced1e52619bf Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Thu, 22 Oct 2020 23:49:36 +0200 Subject: [PATCH] Prevent duplicate numbers in SMS communicator config --- .../general/smsCommunicator/SmsCommunicatorPlugin.kt | 6 +++--- .../utils/textValidator/validators/MultiPhoneValidator.kt | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index c64701ea73..9beccc7ccd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -1004,7 +1004,7 @@ class SmsCommunicatorPlugin @Inject constructor( private fun areMoreNumbers(allowednumbers: String?): Boolean { return allowednumbers?.let { - var countNumbers = 0 + val knownNumbers = HashSet() val substrings = it.split(";").toTypedArray() for (number in substrings) { var cleaned = number.replace(Regex("\\s+"), "") @@ -1012,9 +1012,9 @@ class SmsCommunicatorPlugin @Inject constructor( cleaned = cleaned.replace("+", "") cleaned = cleaned.replace("-", "") if (!cleaned.matches(Regex("[0-9]+"))) continue - countNumbers++ + knownNumbers.add(cleaned) } - countNumbers > 1 + knownNumbers.size > 1 } ?: false } } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/MultiPhoneValidator.kt b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/MultiPhoneValidator.kt index 3baba5240d..a2c6d8f882 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/MultiPhoneValidator.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/MultiPhoneValidator.kt @@ -7,9 +7,13 @@ class MultiPhoneValidator(val _customErrorMessage: String?) : Validator(_customE override fun isValid(editText: EditText): Boolean { val substrings = editText.text.split(";").toTypedArray() + val knownNumbers = HashSet() for (number in substrings) { if (!PatternValidator(_customErrorMessage, Patterns.PHONE).isValid(number)) return false + if (knownNumbers.contains(number)) + return false + knownNumbers.add(number) } return substrings.isNotEmpty() }