MessageAdapter: Display emojis slightly larger in text

This commit is contained in:
Daniel Gultsch 2017-10-28 13:15:31 +02:00
parent aeec76d68b
commit 3205d763cf
3 changed files with 93 additions and 8 deletions

View File

@ -471,6 +471,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 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, XMPP_PATTERN, "xmpp");
Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER); Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
Linkify.addLinks(body, GeoHelper.GEO_URI, "geo"); Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");

View File

@ -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; package eu.siacs.conversations.utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
public class Emoticons { public class Emoticons {
@ -69,25 +99,77 @@ public class Emoticons {
return symbols; 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) { public static boolean isEmoji(String input) {
List<Symbol> symbols = parse(input); List<Symbol> 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) { public static boolean isOnlyEmoji(String input) {
List<Symbol> symbols = parse(input); List<Symbol> symbols = parse(input);
for(Symbol symbol : symbols) { for(Symbol symbol : symbols) {
if (symbol == Symbol.NON_EMOJI) { if (!symbol.isEmoji()) {
return false; return false;
} }
} }
return symbols.size() > 0; return symbols.size() > 0;
} }
private enum Symbol { private static abstract class Symbol {
EMOJI, NON_EMOJI
private final String value;
public Symbol(List<Integer> 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<Integer> codepoints) {
super(codepoints);
}
@Override
boolean isEmoji() {
return true;
}
}
public static class Other extends Symbol {
public Other(List<Integer> codepoints) {
super(codepoints);
}
@Override
boolean isEmoji() {
return false;
}
}
private static class Builder { private static class Builder {
private final List<Integer> codepoints = new ArrayList<>(); private final List<Integer> codepoints = new ArrayList<>();
@ -139,11 +221,11 @@ public class Emoticons {
public Symbol build() { public Symbol build() {
if (codepoints.size() > 0 && SYMBOLIZE.contains(codepoints.get(codepoints.size() - 1))) { 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) { } 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);
} }
} }

View File

@ -312,7 +312,6 @@
\n\nhttps://github.com/WhisperSystems/libaxolotl-java\n(GPLv3) \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/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/jdamcd/android-crop\n(Apache License, Version 2.0)
\n\nhttps://github.com/vdurmont/emoji-java\n(The MIT License)
</string> </string>
<string name="title_pref_quiet_hours">Quiet Hours</string> <string name="title_pref_quiet_hours">Quiet Hours</string>
<string name="title_pref_quiet_hours_start_time">Start time</string> <string name="title_pref_quiet_hours_start_time">Start time</string>