From 0571d54488b9b39bd546c5f15fdadbe0f0e3641f Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Wed, 18 Nov 2026 09:13:39 +0100 Subject: [PATCH] fix(chat): show messages of a single emoji larger again On Android 15 and newer, messages of a single emoji were shown in the size of any text message. Since emoji2 1.5, EmojiCompat leaves text untouched on these versions unless it is told to replace the system's emoji font, so the check for a single emoji never found one and the 2.5 times larger text size was never used. The check now asks the platform's Unicode data whether the message is one emoji, which does not depend on a loaded emoji font. Below Android 9, where these properties are not public, EmojiCompat is still used. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- .../nextcloud/talk/utils/TextMatchers.java | 30 +++++++ .../nextcloud/talk/utils/TextMatchersTest.kt | 84 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.kt 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(" ")) + } +}