diff --git a/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/view/EmojiFilterTest.kt b/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/view/EmojiFilterTest.kt new file mode 100644 index 00000000000..5c48ee8ea1a --- /dev/null +++ b/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/view/EmojiFilterTest.kt @@ -0,0 +1,52 @@ +package de.rki.coronawarnapp.ui.view + +import android.text.SpannableString +import io.kotest.matchers.shouldBe +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class EmojiFilterTest { + + private val testData = mapOf( + "Pneumonoultramicroscopicsilicovolcanoconiosis" to null, // English + "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft" to null, // German + "Muvaffakiyetsizleştiricileştiriveremeyebileceklerimizdenmişsinizcesine" to null, // Turkish + "Рентгеноэлектрокардиографический" to null, // Russian + "speciallægepraksisplanlægningsstabiliseringsperiode" to null, // Danish + "Непротивоконституционствувателствувайте" to null, // Bulgarian + "Dziewięćsetdziewięćdziesięciodziewięcionarodowościowego" to null, // Polish + "Pneumoultramicroscopicossilicovulcanoconiótico" to null, // Portuguese + "paraskevidékatriaphobie" to null, // French + "peruspalveluliikelaitoskuntayhtymä" to null, // Finnish + "Arquitectónicamente" to null, // Spanish + "znajneprekryštalizovávateľnejšievajúcimi" to null, // Slovak + // Swedish + "Spårvagnsaktiebolagsskensmutsskjutarefackföreningspersonalbeklädnadsmagasinsförråd-sförvaltarens" to null, + // Hungarian + "legösszetettebbszóhosszúságvilágrekorddöntéskényszerneurózistünetegyüttesmegnyilvá-nulásfejleszthetőségvizsgálataitokként" to null, + "nebeprisikiškiakopūstlapiaujančiuosiuose" to null, // Lithuanian + "וכשבהשתעשעויותיהם" to null, // Hebrew + "ηλεκτροεγκεφαλογραφήματος" to null, // Greek + "prijestolonasljednikovičičinima" to null, // Croatian + "Sünnipäevanädalalõpupeopärastlõunaväsimatus" to null, // Estonian + "أفاستسقيناكموها" to null, // Arabic, + "✌️✌️✌️✌️✌️✌️✌️✌️✌️️" to "", // Emoji, + "☕☕☕☕☕☕☕☕☕" to "", // Emoji, + ) + + @Test + fun filter() { + testData.forEach { (input, output) -> + EmojiFilter().filter( + input, + 0, + input.length, + SpannableString(input), + 0, + input.length + ) shouldBe output + } + } +} diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/coronatest/rat/profile/create/RATProfileCreateFragment.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/coronatest/rat/profile/create/RATProfileCreateFragment.kt index 836b2be107d..204d625ce2a 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/coronatest/rat/profile/create/RATProfileCreateFragment.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/coronatest/rat/profile/create/RATProfileCreateFragment.kt @@ -8,6 +8,7 @@ import com.google.android.material.datepicker.MaterialDatePicker import de.rki.coronawarnapp.R import de.rki.coronawarnapp.contactdiary.util.hideKeyboard import de.rki.coronawarnapp.databinding.RatProfileCreateFragmentBinding +import de.rki.coronawarnapp.ui.view.addEmojiFilter import de.rki.coronawarnapp.util.TimeAndDateExtensions.toDayFormat import de.rki.coronawarnapp.util.di.AutoInject import de.rki.coronawarnapp.util.ui.doNavigate @@ -36,8 +37,8 @@ class RATProfileCreateFragment : Fragment(R.layout.rat_profile_create_fragment), } // Full name - firstNameInputEdit.doAfterTextChanged { viewModel.firstNameChanged(it.toString()) } - lastNameInputEdit.doAfterTextChanged { viewModel.lastNameChanged(it.toString()) } + firstNameInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.firstNameChanged(it.toString()) } + lastNameInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.lastNameChanged(it.toString()) } // Birth date birthDateInputEdit.setOnClickListener { openDatePicker() } @@ -48,15 +49,15 @@ class RATProfileCreateFragment : Fragment(R.layout.rat_profile_create_fragment), } // Address - streetInputEdit.doAfterTextChanged { viewModel.streetChanged(it.toString()) } + streetInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.streetChanged(it.toString()) } + cityInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.cityChanged(it.toString()) } zipCodeInputEdit.doAfterTextChanged { viewModel.zipCodeChanged(it.toString()) } - cityInputEdit.doAfterTextChanged { viewModel.cityChanged(it.toString()) } // Phone phoneInputEdit.doAfterTextChanged { viewModel.phoneChanged(it.toString()) } // E-mail - emailInputEdit.doAfterTextChanged { viewModel.emailChanged(it.toString()) } + emailInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.emailChanged(it.toString()) } viewModel.profile.observe(viewLifecycleOwner) { profileSaveButton.isEnabled = it?.isValid == true diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/view/EmojiFilter.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/view/EmojiFilter.kt new file mode 100644 index 00000000000..17cf20682fb --- /dev/null +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/view/EmojiFilter.kt @@ -0,0 +1,32 @@ +package de.rki.coronawarnapp.ui.view + +import android.text.InputFilter +import android.text.Spanned +import com.google.android.material.textfield.TextInputEditText +import java.lang.Character.OTHER_SYMBOL +import java.lang.Character.SURROGATE + +/** + * Adds [InputFilter] of emojis ,returns same [TextInputEditText] + */ +fun TextInputEditText.addEmojiFilter(): TextInputEditText { + filters += EmojiFilter() + return this +} + +class EmojiFilter : InputFilter { + override fun filter( + source: CharSequence, + sStart: Int, + sEnd: Int, + destination: Spanned, + dStart: Int, + dEnd: Int + ): CharSequence? { + for (index in sStart until sEnd) { + val type = Character.getType(source[index]).toByte() + if (type in listOf(SURROGATE, OTHER_SYMBOL)) return "" + } + return null + } +}