2015-05-06 13:53:55 -07:00
|
|
|
package org.thoughtcrime.securesms.components.emoji;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-07-28 15:38:38 -07:00
|
|
|
import android.graphics.Paint.FontMetricsInt;
|
2015-05-30 23:52:41 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.support.annotation.NonNull;
|
2015-08-06 10:35:51 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2015-05-14 15:54:07 -07:00
|
|
|
import android.support.v7.widget.AppCompatTextView;
|
2015-07-28 15:38:38 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextUtils.TruncateAt;
|
2015-05-06 13:53:55 -07:00
|
|
|
import android.util.AttributeSet;
|
2015-05-14 15:54:07 -07:00
|
|
|
|
2015-05-30 23:52:41 -07:00
|
|
|
import org.thoughtcrime.securesms.components.emoji.EmojiProvider.EmojiDrawable;
|
2015-07-28 15:38:38 -07:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2015-05-30 23:52:41 -07:00
|
|
|
|
2015-05-14 15:54:07 -07:00
|
|
|
public class EmojiTextView extends AppCompatTextView {
|
2015-07-28 15:38:38 -07:00
|
|
|
private CharSequence source;
|
|
|
|
private boolean needsEllipsizing;
|
2015-05-26 15:10:45 -07:00
|
|
|
|
2015-05-06 13:53:55 -07:00
|
|
|
public EmojiTextView(Context context) {
|
2015-05-30 23:52:41 -07:00
|
|
|
this(context, null);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public EmojiTextView(Context context, AttributeSet attrs) {
|
2015-05-30 23:52:41 -07:00
|
|
|
this(context, attrs, 0);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public EmojiTextView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
2015-07-28 15:38:38 -07:00
|
|
|
}
|
|
|
|
|
2015-08-06 10:35:51 -07:00
|
|
|
@Override public void setText(@Nullable CharSequence text, BufferType type) {
|
2015-07-28 15:38:38 -07:00
|
|
|
source = EmojiProvider.getInstance(getContext()).emojify(text, this);
|
|
|
|
setTextEllipsized(source);
|
|
|
|
}
|
|
|
|
|
2015-08-06 10:35:51 -07:00
|
|
|
public void setTextEllipsized(final @Nullable CharSequence source) {
|
2015-07-28 15:38:38 -07:00
|
|
|
super.setText(needsEllipsizing ? ViewUtil.ellipsize(source, this) : source, BufferType.SPANNABLE);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
2015-05-14 15:54:07 -07:00
|
|
|
|
2015-05-30 23:52:41 -07:00
|
|
|
@Override public void invalidateDrawable(@NonNull Drawable drawable) {
|
|
|
|
if (drawable instanceof EmojiDrawable) invalidate();
|
|
|
|
else super.invalidateDrawable(drawable);
|
2015-05-14 15:54:07 -07:00
|
|
|
}
|
2015-07-28 15:38:38 -07:00
|
|
|
|
|
|
|
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
final int size = MeasureSpec.getSize(widthMeasureSpec);
|
|
|
|
final int mode = MeasureSpec.getMode(widthMeasureSpec);
|
|
|
|
if (getEllipsize() == TruncateAt.END &&
|
|
|
|
!TextUtils.isEmpty(source) &&
|
|
|
|
(mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) &&
|
|
|
|
getPaint().breakText(source, 0, source.length()-1, true, size, null) != source.length())
|
|
|
|
{
|
|
|
|
needsEllipsizing = true;
|
|
|
|
FontMetricsInt font = getPaint().getFontMetricsInt();
|
|
|
|
super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY),
|
|
|
|
MeasureSpec.makeMeasureSpec(Math.abs(font.top - font.bottom), MeasureSpec.EXACTLY));
|
|
|
|
} else {
|
|
|
|
needsEllipsizing = false;
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
if (changed) setTextEllipsized(source);
|
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
|
|
}
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|