From b8672b04da0b6813895ab718e438f9e31ee2e269 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Tue, 27 Jun 2017 18:51:29 +0200 Subject: [PATCH] Merge EmojiHandler into EmojiManager. (#150) --- .../com/vanniktech/emoji/EmojiButton.java | 2 +- .../com/vanniktech/emoji/EmojiEditText.java | 2 +- .../com/vanniktech/emoji/EmojiHandler.java | 37 ------ .../com/vanniktech/emoji/EmojiManager.java | 26 ++++ .../com/vanniktech/emoji/EmojiTextView.java | 2 +- .../vanniktech/emoji/EmojiHandlerTest.java | 118 ------------------ .../vanniktech/emoji/EmojiManagerTest.java | 93 ++++++++++++++ 7 files changed, 122 insertions(+), 158 deletions(-) delete mode 100644 emoji/src/main/java/com/vanniktech/emoji/EmojiHandler.java delete mode 100644 emoji/src/test/java/com/vanniktech/emoji/EmojiHandlerTest.java diff --git a/emoji/src/main/java/com/vanniktech/emoji/EmojiButton.java b/emoji/src/main/java/com/vanniktech/emoji/EmojiButton.java index cc6079613d..0a28cc46cc 100644 --- a/emoji/src/main/java/com/vanniktech/emoji/EmojiButton.java +++ b/emoji/src/main/java/com/vanniktech/emoji/EmojiButton.java @@ -40,7 +40,7 @@ public EmojiButton(final Context context, final AttributeSet attrs) { @Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) { final CharSequence text = rawText == null ? "" : rawText; final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text); - EmojiHandler.replaceWithImages(getContext(), spannableStringBuilder, emojiSize); + EmojiManager.replaceWithImages(getContext(), spannableStringBuilder, emojiSize); super.setText(spannableStringBuilder, type); } diff --git a/emoji/src/main/java/com/vanniktech/emoji/EmojiEditText.java b/emoji/src/main/java/com/vanniktech/emoji/EmojiEditText.java index f890e512b2..4311b7ebf3 100644 --- a/emoji/src/main/java/com/vanniktech/emoji/EmojiEditText.java +++ b/emoji/src/main/java/com/vanniktech/emoji/EmojiEditText.java @@ -39,7 +39,7 @@ public EmojiEditText(final Context context, final AttributeSet attrs) { } @Override @CallSuper protected void onTextChanged(final CharSequence text, final int start, final int lengthBefore, final int lengthAfter) { - EmojiHandler.replaceWithImages(getContext(), getText(), emojiSize); + EmojiManager.replaceWithImages(getContext(), getText(), emojiSize); } @CallSuper public void backspace() { diff --git a/emoji/src/main/java/com/vanniktech/emoji/EmojiHandler.java b/emoji/src/main/java/com/vanniktech/emoji/EmojiHandler.java deleted file mode 100644 index 7b2782e4e7..0000000000 --- a/emoji/src/main/java/com/vanniktech/emoji/EmojiHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.vanniktech.emoji; - -import android.content.Context; -import android.text.Spannable; -import com.vanniktech.emoji.EmojiManager.EmojiRange; -import java.util.ArrayList; -import java.util.List; - -final class EmojiHandler { - static void replaceWithImages(final Context context, final Spannable text, final int emojiSize) { - final EmojiManager emojiManager = EmojiManager.getInstance(); - final EmojiSpan[] existingSpans = text.getSpans(0, text.length(), EmojiSpan.class); - final List existingSpanPositions = new ArrayList<>(existingSpans.length); - - final int size = existingSpans.length; - //noinspection ForLoopReplaceableByForEach - for (int i = 0; i < size; i++) { - existingSpanPositions.add(text.getSpanStart(existingSpans[i])); - } - - final List findAllEmojis = emojiManager.findAllEmojis(text); - - //noinspection ForLoopReplaceableByForEach - for (int i = 0; i < findAllEmojis.size(); i++) { - final EmojiRange location = findAllEmojis.get(i); - - if (!existingSpanPositions.contains(location.start)) { - text.setSpan(new EmojiSpan(context, location.emoji.getResource(), emojiSize), - location.start, location.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - } - } - } - - private EmojiHandler() { - throw new AssertionError("No instances."); - } -} diff --git a/emoji/src/main/java/com/vanniktech/emoji/EmojiManager.java b/emoji/src/main/java/com/vanniktech/emoji/EmojiManager.java index 45b29f1b30..b0016d4b1e 100644 --- a/emoji/src/main/java/com/vanniktech/emoji/EmojiManager.java +++ b/emoji/src/main/java/com/vanniktech/emoji/EmojiManager.java @@ -1,7 +1,9 @@ package com.vanniktech.emoji; +import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.text.Spannable; import com.vanniktech.emoji.emoji.Emoji; import com.vanniktech.emoji.emoji.EmojiCategory; import java.util.ArrayList; @@ -110,6 +112,30 @@ static void destroy() { INSTANCE.emojiRepetitivePattern = null; } + static void replaceWithImages(final Context context, final Spannable text, final int emojiSize) { + final EmojiManager emojiManager = EmojiManager.getInstance(); + final EmojiSpan[] existingSpans = text.getSpans(0, text.length(), EmojiSpan.class); + final List existingSpanPositions = new ArrayList<>(existingSpans.length); + + final int size = existingSpans.length; + //noinspection ForLoopReplaceableByForEach + for (int i = 0; i < size; i++) { + existingSpanPositions.add(text.getSpanStart(existingSpans[i])); + } + + final List findAllEmojis = emojiManager.findAllEmojis(text); + + //noinspection ForLoopReplaceableByForEach + for (int i = 0; i < findAllEmojis.size(); i++) { + final EmojiRange location = findAllEmojis.get(i); + + if (!existingSpanPositions.contains(location.start)) { + text.setSpan(new EmojiSpan(context, location.emoji.getResource(), emojiSize), + location.start, location.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + } + EmojiCategory[] getCategories() { verifyInstalled(); return categories; // NOPMD diff --git a/emoji/src/main/java/com/vanniktech/emoji/EmojiTextView.java b/emoji/src/main/java/com/vanniktech/emoji/EmojiTextView.java index c57697775d..fa885775fd 100644 --- a/emoji/src/main/java/com/vanniktech/emoji/EmojiTextView.java +++ b/emoji/src/main/java/com/vanniktech/emoji/EmojiTextView.java @@ -40,7 +40,7 @@ public EmojiTextView(final Context context, final AttributeSet attrs) { @Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) { final CharSequence text = rawText == null ? "" : rawText; final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text); - EmojiHandler.replaceWithImages(getContext(), spannableStringBuilder, emojiSize); + EmojiManager.replaceWithImages(getContext(), spannableStringBuilder, emojiSize); super.setText(spannableStringBuilder, type); } diff --git a/emoji/src/test/java/com/vanniktech/emoji/EmojiHandlerTest.java b/emoji/src/test/java/com/vanniktech/emoji/EmojiHandlerTest.java deleted file mode 100644 index ff3e26636a..0000000000 --- a/emoji/src/test/java/com/vanniktech/emoji/EmojiHandlerTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.vanniktech.emoji; - -import android.support.annotation.NonNull; -import android.text.Spannable; -import android.text.SpannableString; -import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker; -import com.vanniktech.emoji.emoji.Emoji; -import com.vanniktech.emoji.emoji.EmojiCategory; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.Config; - -import static org.assertj.core.api.Java6Assertions.assertThat; - -@Config(manifest = Config.NONE) @RunWith(RobolectricTestRunner.class) public class EmojiHandlerTest { - @Before public void setUp() { - EmojiManager.install(new EmojiProvider() { - @NonNull @Override public EmojiCategory[] getCategories() { - return new EmojiCategory[] { new EmojiCategory() { - @NonNull @Override public Emoji[] getEmojis() { - return new Emoji[] { - new Emoji(new int[] { 0x1234 }, R.drawable.emoji_recent), - new Emoji(new int[] { 0x4321 }, R.drawable.emoji_recent), - new Emoji(new int[] { 0x5678 }, R.drawable.emoji_backspace), - new Emoji(new int[] { 0x1234, 0x4321, 0x9999 }, R.drawable.emoji_recent) - }; - } - - @Override public int getIcon() { - return R.drawable.emoji_recent; - } - } }; - } - }); - } - - @Test public void constructorShouldBePrivate() { - PrivateConstructorChecker.forClass(EmojiHandler.class) - .expectedTypeOfException(AssertionError.class) - .expectedExceptionMessage("No instances.") - .check(); - } - - @Test public void simple() { - final Spannable text = new SpannableString(new String(new int[] { 0x1234 }, 0, 1)); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); - } - - @Test public void inString() { - final Spannable text = new SpannableString("test" + new String(new int[] { 0x1234 }, 0, 1) + "abc"); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); - } - - @Test public void multiple() { - final Spannable text = new SpannableString(new String(new int[] { 0x1234 }, 0, 1) + new String(new int[] { 0x5678 }, 0, 1)); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(2); - } - - @Test public void multipleInString() { - final Spannable text = new SpannableString("abc" + new String(new int[] { 0x1234 }, 0, 1) + "cba" + new String(new int[] { 0x5678 }, 0, 1) + "xyz"); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(2); - } - - @Test public void halfPath() { - final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321 }, 0, 1)); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); - } - - @Test public void fullPath() { - final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321, 0x9999 }, 0, 1)); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); - } - - @Test public void takeLongest() { - final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321 }, 0, 1)); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); - } - - @Test public void empty() { - final Spannable text = new SpannableString(""); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(0); - } - - @Test public void noneInString() { - final Spannable text = new SpannableString("abcdefg"); - - EmojiHandler.replaceWithImages(RuntimeEnvironment.application, text, 22); - - assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(0); - } -} diff --git a/emoji/src/test/java/com/vanniktech/emoji/EmojiManagerTest.java b/emoji/src/test/java/com/vanniktech/emoji/EmojiManagerTest.java index 8a7641e373..0485118d5f 100644 --- a/emoji/src/test/java/com/vanniktech/emoji/EmojiManagerTest.java +++ b/emoji/src/test/java/com/vanniktech/emoji/EmojiManagerTest.java @@ -1,6 +1,8 @@ package com.vanniktech.emoji; import android.support.annotation.NonNull; +import android.text.Spannable; +import android.text.SpannableString; import com.vanniktech.emoji.EmojiManager.EmojiRange; import com.vanniktech.emoji.emoji.Emoji; import com.vanniktech.emoji.emoji.EmojiCategory; @@ -12,6 +14,7 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import static org.assertj.core.api.Java6Assertions.assertThat; @@ -156,4 +159,94 @@ public void noProviderInstalled() { assertThat(EmojiManager.getInstance().findAllEmojis("")).isEmpty(); } + + @Test public void simple() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString(new String(new int[] { 0x1234 }, 0, 1)); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); + } + + @Test public void inString() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString("test" + new String(new int[] { 0x1234 }, 0, 1) + "abc"); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); + } + + @Test public void multiple() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString(new String(new int[] { 0x1234 }, 0, 1) + new String(new int[] { 0x5678 }, 0, 1)); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(2); + } + + @Test public void multipleInString() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString("abc" + new String(new int[] { 0x1234 }, 0, 1) + "cba" + new String(new int[] { 0x5678 }, 0, 1) + "xyz"); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(2); + } + + @Test public void halfPath() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321 }, 0, 1)); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); + } + + @Test public void fullPath() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321, 0x9999 }, 0, 1)); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); + } + + @Test public void takeLongest() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString(new String(new int[] { 0x1234, 0x4321 }, 0, 1)); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(1); + } + + @Test public void empty() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString(""); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(0); + } + + @Test public void noneInString() { + EmojiManager.install(provider); + + final Spannable text = new SpannableString("abcdefg"); + + EmojiManager.replaceWithImages(RuntimeEnvironment.application, text, 22); + + assertThat(text.getSpans(0, text.length(), EmojiSpan.class)).hasSize(0); + } }