From b8f97a7072d37e6aab26238a5620991b0b8a14ce Mon Sep 17 00:00:00 2001 From: lookshe Date: Tue, 29 Mar 2016 20:58:44 +0200 Subject: Fixes FS#174: Displaying of ASCII emoticons differs --- .../emojicon/EmojiconAbstractSpan.java | 36 ++++++++++++++++++++++ .../ankushsachdeva/emojicon/EmojiconHandler.java | 11 ++++--- .../ankushsachdeva/emojicon/EmojiconImageSpan.java | 6 ++-- .../ankushsachdeva/emojicon/EmojiconSpan.java | 24 ++------------- .../ankushsachdeva/emojicon/EmojiconTextView.java | 8 +++-- 5 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconAbstractSpan.java (limited to 'libs/emojicon') diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconAbstractSpan.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconAbstractSpan.java new file mode 100644 index 00000000..1ee4599a --- /dev/null +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconAbstractSpan.java @@ -0,0 +1,36 @@ +package github.ankushsachdeva.emojicon; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.text.style.DynamicDrawableSpan; + +/** + * Created by lookshe on 29.03.16. + */ +public abstract class EmojiconAbstractSpan extends DynamicDrawableSpan { + private final Context mContext; + private final int mResourceId; + private final int mSize; + private Drawable mDrawable; + + public EmojiconAbstractSpan(Context context, int resourceId, int size) { + super(); + mContext = context; + mResourceId = resourceId; + mSize = size; + } + + @Override + public Drawable getDrawable() { + if (mDrawable == null) { + try { + mDrawable = mContext.getResources().getDrawable(mResourceId); + int size = mSize; + mDrawable.setBounds(0, 0, size, size); + } catch (Exception e) { + // swallow + } + } + return mDrawable; + } +} diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java index f611a263..b6804b6d 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java @@ -1445,7 +1445,7 @@ public final class EmojiconHandler { map.put(Pattern.compile(smile), resource); } - private static boolean getSmiledText(Context context, Spannable spannable) { + private static boolean getSmiledText(Context context, Spannable spannable, int size) { // remove spans throughout all text EmojiconImageSpan[] oldSpans = spannable.getSpans(0, spannable.length(), EmojiconImageSpan.class); for (int i = 0; i < oldSpans.length; i++) { @@ -1495,7 +1495,7 @@ public final class EmojiconHandler { } } if (set) { - spannable.setSpan(new EmojiconImageSpan(context, entry.getValue()), + spannable.setSpan(new EmojiconImageSpan(context, entry.getValue(), size), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); hasChanges = true; @@ -1514,9 +1514,10 @@ public final class EmojiconHandler { * @param index * @param length */ - public static void addEmojis(Context context, Spannable text, int emojiSize, int index, int length) { + public static boolean addEmojis(Context context, Spannable text, int emojiSize, int index, int length) { + boolean hasChanges = false; if (isParseEmoticons()) { - getSmiledText(context, text); + hasChanges = getSmiledText(context, text, emojiSize); } int textLength = text.length(); int textLengthToProcessMax = textLength - index; @@ -1633,7 +1634,9 @@ public final class EmojiconHandler { if (icon > 0) { text.setSpan(new EmojiconSpan(context, icon, emojiSize), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + hasChanges = true; } } + return hasChanges; } } diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconImageSpan.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconImageSpan.java index 8ad8fbb5..17ed779f 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconImageSpan.java +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconImageSpan.java @@ -6,9 +6,9 @@ import android.text.style.ImageSpan; /** * Created by lookshe on 16.08.2015. */ -public class EmojiconImageSpan extends ImageSpan { +public class EmojiconImageSpan extends EmojiconAbstractSpan { - EmojiconImageSpan(Context context, int resourceId) { - super(context, resourceId); + EmojiconImageSpan(Context context, int resourceId, int size) { + super(context, resourceId, size); } } diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java index 1afeea95..0fb43139 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java @@ -23,29 +23,9 @@ import android.text.style.DynamicDrawableSpan; /** * @author Hieu Rocker (rockerhieu@gmail.com) */ -class EmojiconSpan extends DynamicDrawableSpan { - private final Context mContext; - private final int mResourceId; - private final int mSize; - private Drawable mDrawable; +class EmojiconSpan extends EmojiconAbstractSpan { public EmojiconSpan(Context context, int resourceId, int size) { - super(); - mContext = context; - mResourceId = resourceId; - mSize = size; - } - - public Drawable getDrawable() { - if (mDrawable == null) { - try { - mDrawable = mContext.getResources().getDrawable(mResourceId); - int size = mSize; - mDrawable.setBounds(0, 0, size, size); - } catch (Exception e) { - // swallow - } - } - return mDrawable; + super(context, resourceId, size); } } \ No newline at end of file diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java index 5822e478..4f63c30a 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java @@ -63,8 +63,12 @@ public class EmojiconTextView extends TextView { @Override public void setText(CharSequence text, BufferType type) { SpannableStringBuilder builder = new SpannableStringBuilder(text); - EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mTextStart, mTextLength); - super.setText(builder, type); + boolean hasChanges = EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mTextStart, mTextLength); + if (hasChanges) { + super.setText(builder, BufferType.SPANNABLE); + } else { + super.setText(text, type); + } } /** -- cgit v1.2.3