diff --git a/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java index 5a047cad33a..d7e50873f88 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java +++ b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java @@ -9,14 +9,25 @@ */ package com.nextcloud.talk.utils; +import android.icu.lang.UCharacter; +import android.icu.lang.UProperty; +import android.icu.text.BreakIterator; +import android.os.Build; import android.text.Spanned; import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; import androidx.emoji2.text.EmojiCompat; import androidx.emoji2.text.EmojiSpan; public final class TextMatchers { + private static final char EMOJI_PRESENTATION_SELECTOR = '\uFE0F'; + + /** + * Whether {@code text} is a single emoji, e.g. a face, a flag, an emoji with a skin tone or a sequence joined into one + * emoji, surrounded by nothing but whitespace. + */ public static boolean isMessageWithSingleEmoticonOnly(@Nullable final String text) { if (text == null) { return false; @@ -27,6 +38,10 @@ public static boolean isMessageWithSingleEmoticonOnly(@Nullable final String tex return false; } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + return isSingleEmoji(trimmed); + } + final EmojiCompat emojiCompat = EmojiCompat.get(); if (emojiCompat.getLoadState() != EmojiCompat.LOAD_STATE_SUCCEEDED) { return false; @@ -43,4 +58,19 @@ public static boolean isMessageWithSingleEmoticonOnly(@Nullable final String tex && spanned.getSpanStart(spans[0]) == 0 && spanned.getSpanEnd(spans[0]) == trimmed.length(); } + + @RequiresApi(Build.VERSION_CODES.P) + private static boolean isSingleEmoji(final String text) { + final BreakIterator graphemes = BreakIterator.getCharacterInstance(); + graphemes.setText(text); + graphemes.first(); + if (graphemes.next() != text.length()) { + return false; + } + + final int first = text.codePointAt(0); + return UCharacter.hasBinaryProperty(first, UProperty.EMOJI_PRESENTATION) + || UCharacter.hasBinaryProperty(first, UProperty.EMOJI) + && text.indexOf(EMOJI_PRESENTATION_SELECTOR) >= 0; + } } diff --git a/app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.kt b/app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.kt new file mode 100644 index 00000000000..c57082fea88 --- /dev/null +++ b/app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.kt @@ -0,0 +1,84 @@ +/* + * Nextcloud Talk - Android Client + * + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package com.nextcloud.talk.utils + +import android.app.Application +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(application = Application::class, sdk = [35]) +class TextMatchersTest { + + @Test + fun `a single emoji is detected`() { + assertTrue(TextMatchers.isMessageWithSingleEmoticonOnly("😀")) + } + + @Test + fun `a single emoji surrounded by whitespace is detected`() { + assertTrue(TextMatchers.isMessageWithSingleEmoticonOnly(" 😀\n")) + } + + @Test + fun `an emoji with a skin tone is detected`() { + assertTrue(TextMatchers.isMessageWithSingleEmoticonOnly("👍🏽")) + } + + @Test + fun `an emoji drawn as emoji by its presentation selector is detected`() { + assertTrue(TextMatchers.isMessageWithSingleEmoticonOnly("❤️")) + } + + @Test + fun `a flag is detected`() { + assertTrue(TextMatchers.isMessageWithSingleEmoticonOnly("🇩🇪")) + } + + @Test + fun `emoji joined into one are detected`() { + assertTrue( + TextMatchers.isMessageWithSingleEmoticonOnly( + "👨‍👩‍👧" + ) + ) + } + + @Test + fun `a keycap is detected`() { + assertTrue(TextMatchers.isMessageWithSingleEmoticonOnly("1️⃣")) + } + + @Test + fun `text with an emoji is not a single emoji`() { + assertFalse(TextMatchers.isMessageWithSingleEmoticonOnly("hi 😀")) + } + + @Test + fun `two emoji are not a single emoji`() { + assertFalse(TextMatchers.isMessageWithSingleEmoticonOnly("😀😀")) + } + + @Test + fun `a digit is not a single emoji`() { + assertFalse(TextMatchers.isMessageWithSingleEmoticonOnly("1")) + } + + @Test + fun `a letter is not a single emoji`() { + assertFalse(TextMatchers.isMessageWithSingleEmoticonOnly("a")) + } + + @Test + fun `blank text is not a single emoji`() { + assertFalse(TextMatchers.isMessageWithSingleEmoticonOnly(" ")) + } +}