From 3205d763cfe948d009963b7b70925b51850f7b47 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 28 Oct 2017 13:15:31 +0200 Subject: [PATCH] MessageAdapter: Display emojis slightly larger in text --- .../ui/adapter/MessageAdapter.java | 4 + .../siacs/conversations/utils/Emoticons.java | 96 +++++++++++++++++-- src/main/res/values/strings.xml | 1 - 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index d91f6e1e3..2f35d49e0 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -471,6 +471,10 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } + Matcher matcher = Emoticons.generatePattern(body).matcher(body); + while(matcher.find()) { + body.setSpan(new RelativeSizeSpan(1.2f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } Linkify.addLinks(body, XMPP_PATTERN, "xmpp"); Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER); Linkify.addLinks(body, GeoHelper.GEO_URI, "geo"); diff --git a/src/main/java/eu/siacs/conversations/utils/Emoticons.java b/src/main/java/eu/siacs/conversations/utils/Emoticons.java index 3a0420457..dc0433fa7 100644 --- a/src/main/java/eu/siacs/conversations/utils/Emoticons.java +++ b/src/main/java/eu/siacs/conversations/utils/Emoticons.java @@ -1,8 +1,38 @@ +/* + * Copyright (c) 2017, Daniel Gultsch All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + package eu.siacs.conversations.utils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; public class Emoticons { @@ -69,25 +99,77 @@ public class Emoticons { return symbols; } + public static Pattern generatePattern(CharSequence input) { + final StringBuilder pattern = new StringBuilder(); + for(Symbol symbol : parse(input.toString())) { + if (symbol instanceof Emoji) { + if (pattern.length() != 0) { + pattern.append('|'); + } + pattern.append(Pattern.quote(symbol.toString())); + } + } + return Pattern.compile(pattern.toString()); + } + public static boolean isEmoji(String input) { List symbols = parse(input); - return symbols.size() == 1 && symbols.get(0) == Symbol.EMOJI; + return symbols.size() == 1 && symbols.get(0).isEmoji(); } public static boolean isOnlyEmoji(String input) { List symbols = parse(input); for(Symbol symbol : symbols) { - if (symbol == Symbol.NON_EMOJI) { + if (!symbol.isEmoji()) { return false; } } return symbols.size() > 0; } - private enum Symbol { - EMOJI, NON_EMOJI + private static abstract class Symbol { + + private final String value; + + public Symbol(List codepoints) { + StringBuilder builder = new StringBuilder(); + for(Integer codepoint : codepoints) { + builder.appendCodePoint(codepoint); + } + this.value = builder.toString(); + } + + abstract boolean isEmoji(); + + @Override + public String toString() { + return value; + } } + public static class Emoji extends Symbol { + + public Emoji(List codepoints) { + super(codepoints); + } + + @Override + boolean isEmoji() { + return true; + } + } + + public static class Other extends Symbol { + + public Other(List codepoints) { + super(codepoints); + } + + @Override + boolean isEmoji() { + return false; + } + } private static class Builder { private final List codepoints = new ArrayList<>(); @@ -139,11 +221,11 @@ public class Emoticons { public Symbol build() { if (codepoints.size() > 0 && SYMBOLIZE.contains(codepoints.get(codepoints.size() - 1))) { - return Symbol.NON_EMOJI; + return new Other(codepoints); } else if (codepoints.size() > 1 && KEYCAP_COMBINEABLE.contains(codepoints.get(0)) && codepoints.get(codepoints.size() - 1) != COMBINING_ENCLOSING_KEYCAP) { - return Symbol.NON_EMOJI; + return new Other(codepoints); } - return codepoints.size() == 0 ? Symbol.NON_EMOJI : Symbol.EMOJI; + return codepoints.size() == 0 ? new Other(codepoints): new Emoji(codepoints); } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 8a80d4e01..c44a57bff 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -312,7 +312,6 @@ \n\nhttps://github.com/WhisperSystems/libaxolotl-java\n(GPLv3) \n\nhttps://github.com/vinc3m1/RoundedImageView\n(Apache License, Version 2.0) \n\nhttps://github.com/jdamcd/android-crop\n(Apache License, Version 2.0) - \n\nhttps://github.com/vdurmont/emoji-java\n(The MIT License) Quiet Hours Start time