max out emoji enlargment at 42 emojis

This commit is contained in:
Daniel Gultsch 2018-05-09 09:07:26 +02:00
parent 13d6885883
commit ffe610f961
2 changed files with 34 additions and 10 deletions

View file

@ -203,7 +203,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
return 4; return 4;
} }
public int getItemViewType(Message message) { private int getItemViewType(Message message) {
if (message.getType() == Message.TYPE_STATUS) { if (message.getType() == Message.TYPE_STATUS) {
if (DATE_SEPARATOR_BODY.equals(message.getBody())) { if (DATE_SEPARATOR_BODY.equals(message.getBody())) {
return DATE_SEPARATOR; return DATE_SEPARATOR;
@ -222,7 +222,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
return this.getItemViewType(getItem(position)); return this.getItemViewType(getItem(position));
} }
public int getMessageTextColor(boolean onDark, boolean primary) { private int getMessageTextColor(boolean onDark, boolean primary) {
if (onDark) { if (onDark) {
return ContextCompat.getColor(activity, primary ? R.color.white : R.color.white70); return ContextCompat.getColor(activity, primary ? R.color.white : R.color.white70);
} else { } else {
@ -390,7 +390,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.messageBody.setText(EmojiWrapper.transform(span)); viewHolder.messageBody.setText(EmojiWrapper.transform(span));
} }
private int applyQuoteSpan(SpannableStringBuilder body, int start, int end, boolean darkBackground) { private void applyQuoteSpan(SpannableStringBuilder body, int start, int end, boolean darkBackground) {
if (start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) { if (start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) {
body.insert(start++, "\n"); body.insert(start++, "\n");
body.setSpan(new DividerSpan(false), start - 2, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new DividerSpan(false), start - 2, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
@ -404,7 +404,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
: ContextCompat.getColor(activity, R.color.green700_desaturated); : ContextCompat.getColor(activity, R.color.green700_desaturated);
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
body.setSpan(new QuoteSpan(color, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new QuoteSpan(color, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return 0;
} }
/** /**
@ -536,7 +535,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
} }
} }
} }
Matcher matcher = Emoticons.generatePattern(body).matcher(body); Matcher matcher = Emoticons.getEmojiPattern(body).matcher(body);
while (matcher.find()) { while (matcher.find()) {
if (matcher.start() < matcher.end()) { if (matcher.start() < matcher.end()) {
body.setSpan(new RelativeSizeSpan(1.2f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new RelativeSizeSpan(1.2f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

View file

@ -29,8 +29,12 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.utils;
import android.util.LruCache;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -71,12 +75,16 @@ public class Emoticons {
ENCLOSED_IDEOGRAPHIC_SUPPLEMENT, ENCLOSED_IDEOGRAPHIC_SUPPLEMENT,
MISC_TECHNICAL); MISC_TECHNICAL);
private static final int MAX_EMOIJS = 42;
private static final int ZWJ = 0x200D; private static final int ZWJ = 0x200D;
private static final int VARIATION_16 = 0xFE0F; private static final int VARIATION_16 = 0xFE0F;
private static final int COMBINING_ENCLOSING_KEYCAP = 0x20E3; private static final int COMBINING_ENCLOSING_KEYCAP = 0x20E3;
private static final int BLACK_FLAG = 0x1F3F4; private static final int BLACK_FLAG = 0x1F3F4;
private static final UnicodeRange FITZPATRICK = new UnicodeRange(0x1F3FB,0x1F3FF); private static final UnicodeRange FITZPATRICK = new UnicodeRange(0x1F3FB,0x1F3FF);
private static final LruCache<CharSequence,Pattern> CACHE = new LruCache<>(256);
private static List<Symbol> parse(String input) { private static List<Symbol> parse(String input) {
List<Symbol> symbols = new ArrayList<>(); List<Symbol> symbols = new ArrayList<>();
Builder builder = new Builder(); Builder builder = new Builder();
@ -99,16 +107,33 @@ public class Emoticons {
return symbols; return symbols;
} }
public static Pattern generatePattern(CharSequence input) { public static Pattern getEmojiPattern(CharSequence input) {
final StringBuilder pattern = new StringBuilder(); Pattern pattern = CACHE.get(input);
if (pattern == null) {
pattern = generatePattern(input);
CACHE.put(input, pattern);
}
return pattern;
}
private static Pattern generatePattern(CharSequence input) {
final HashSet<String> emojis = new HashSet<>();
int i = 0;
for(Symbol symbol : parse(input.toString())) { for(Symbol symbol : parse(input.toString())) {
if (symbol instanceof Emoji) { if (symbol instanceof Emoji) {
if (pattern.length() != 0) { emojis.add(symbol.toString());
pattern.append('|'); if (++i >= MAX_EMOIJS) {
return Pattern.compile("");
} }
pattern.append(Pattern.quote(symbol.toString()));
} }
} }
final StringBuilder pattern = new StringBuilder();
for(String emoji : emojis) {
if (pattern.length() != 0) {
pattern.append('|');
}
pattern.append(Pattern.quote(emoji));
}
return Pattern.compile(pattern.toString()); return Pattern.compile(pattern.toString());
} }