Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}
84 changes: 84 additions & 0 deletions app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.kt
Original file line number Diff line number Diff line change
@@ -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(" "))
}
}
Loading