Fix bug where system emoji setting was ignored in message bubbles.

EmojiTextView. It is our gift. It is our curse.
This commit is contained in:
Greyson Parrelli 2019-03-02 19:29:16 -08:00
parent 003fa1b059
commit 77e971cb9b

View File

@ -83,18 +83,25 @@ public class EmojiTextView extends AppCompatTextView {
previousBufferType = type; previousBufferType = type;
useSystemEmoji = useSystemEmoji(); useSystemEmoji = useSystemEmoji();
if (maxLength <= 0 && (useSystemEmoji || candidates == null || candidates.size() == 0)) { if (useSystemEmoji || candidates == null || candidates.size() == 0) {
super.setText(new SpannableStringBuilder(Optional.fromNullable(text).or("")).append(Optional.fromNullable(overflowText).or("")), BufferType.NORMAL); super.setText(new SpannableStringBuilder(Optional.fromNullable(text).or("")).append(Optional.fromNullable(overflowText).or("")), BufferType.NORMAL);
return;
}
CharSequence emojified = provider.emojify(candidates, text, this); if (getEllipsize() == TextUtils.TruncateAt.END && maxLength > 0) {
super.setText(new SpannableStringBuilder(emojified).append(Optional.fromNullable(overflowText).or("")), BufferType.SPANNABLE); ellipsizeAnyTextForMaxLength();
}
} else {
CharSequence emojified = provider.emojify(candidates, text, this);
super.setText(new SpannableStringBuilder(emojified).append(Optional.fromNullable(overflowText).or("")), BufferType.SPANNABLE);
// Android fails to ellipsize spannable strings. (https://issuetracker.google.com/issues/36991688) // Android fails to ellipsize spannable strings. (https://issuetracker.google.com/issues/36991688)
// We ellipsize them ourselves by manually truncating the appropriate section. // We ellipsize them ourselves by manually truncating the appropriate section.
if (getEllipsize() == TextUtils.TruncateAt.END) { if (getEllipsize() == TextUtils.TruncateAt.END) {
ellipsize(); if (maxLength > 0) {
ellipsizeAnyTextForMaxLength();
} else {
ellipsizeEmojiTextForMaxLines();
}
}
} }
} }
@ -103,21 +110,26 @@ public class EmojiTextView extends AppCompatTextView {
setText(previousText, BufferType.SPANNABLE); setText(previousText, BufferType.SPANNABLE);
} }
private void ellipsize() { private void ellipsizeAnyTextForMaxLength() {
if (maxLength > 0 && getText().length() > maxLength + 1) { if (maxLength > 0 && getText().length() > maxLength + 1) {
SpannableStringBuilder newContent = new SpannableStringBuilder(); SpannableStringBuilder newContent = new SpannableStringBuilder();
newContent.append(getText().subSequence(0, maxLength)).append(ELLIPSIS).append(Optional.fromNullable(overflowText).or("")); newContent.append(getText().subSequence(0, maxLength)).append(ELLIPSIS).append(Optional.fromNullable(overflowText).or(""));
EmojiParser.CandidateList newCandidates = EmojiProvider.getInstance(getContext()).getCandidates(newContent); EmojiParser.CandidateList newCandidates = EmojiProvider.getInstance(getContext()).getCandidates(newContent);
CharSequence emojified = EmojiProvider.getInstance(getContext()).emojify(newCandidates, newContent, this);
super.setText(emojified, BufferType.SPANNABLE); if (useSystemEmoji || newCandidates == null || newCandidates.size() == 0) {
return; super.setText(newContent, BufferType.NORMAL);
} else {
CharSequence emojified = EmojiProvider.getInstance(getContext()).emojify(newCandidates, newContent, this);
super.setText(emojified, BufferType.SPANNABLE);
}
} }
}
private void ellipsizeEmojiTextForMaxLines() {
post(() -> { post(() -> {
if (getLayout() == null) { if (getLayout() == null) {
ellipsize(); ellipsizeEmojiTextForMaxLines();
return; return;
} }