Skip to content

Commit

Permalink
Convert smileys to emojis instead of using images. Fixes pocmo#179.
Browse files Browse the repository at this point in the history
  • Loading branch information
pocmo committed Oct 9, 2015
1 parent 44208da commit 38611c4
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 142 deletions.
20 changes: 7 additions & 13 deletions app/src/main/java/org/yaaic/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@
*/
package org.yaaic.model;

import java.util.Date;

import org.yaaic.utils.MircColors;
import org.yaaic.utils.Smilies;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.util.Linkify;
import android.view.View;
import android.widget.TextView;

import org.yaaic.utils.Emojis;
import org.yaaic.utils.MircColors;

import java.util.Date;

/**
* A channel or server message
*
Expand Down Expand Up @@ -239,6 +235,8 @@ public SpannableString render(Context context)
canvas = new SpannableString(prefix + timestamp + nick);
SpannableString renderedText;

String text = settings.showGraphicalSmilies() ? Emojis.convert(this.text) : this.text;

if (settings.showMircColors()) {
renderedText = MircColors.toSpannable(text);
} else {
Expand All @@ -247,10 +245,6 @@ public SpannableString render(Context context)
);
}

if (settings.showGraphicalSmilies()) {
renderedText = Smilies.toSpannable(renderedText, context);
}

canvas = new SpannableString(TextUtils.concat(canvas, renderedText));

if (hasSender()) {
Expand Down
89 changes: 89 additions & 0 deletions app/src/main/java/org/yaaic/utils/Emojis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009-2015 Sebastian Kaspari
This file is part of Yaaic.
Yaaic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Yaaic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/
package org.yaaic.utils;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Emojis {
private static final HashMap<String, String> mappings = new HashMap<>();
private static Pattern pattern;

static {
mappings.put(":)", "\uD83D\uDE03");
mappings.put(":-)", "\uD83D\uDE03");
mappings.put(":(", "\uD83D\uDE1E");
mappings.put(":-(", "\uD83D\uDE1E");
mappings.put(";)", "\uD83D\uDE09");
mappings.put(";-)", "\uD83D\uDE09");
mappings.put(":p", "\uD83D\uDE1B");
mappings.put(":-p", "\uD83D\uDE1B");
mappings.put(":P", "\uD83D\uDE1B");
mappings.put(":-P", "\uD83D\uDE1B");
mappings.put(":D", "\uD83D\uDE04");
mappings.put(":-D", "\uD83D\uDE04");
mappings.put(":[", "\uD83D\uDE12");
mappings.put(":-[", "\uD83D\uDE12");
mappings.put(":\\", "\uD83D\uDE14");
mappings.put(":-\\", "\uD83D\uDE14");
mappings.put(":o", "\uD83D\uDE2E");
mappings.put(":-o", "\uD83D\uDE2E");
mappings.put(":O", "\uD83D\uDE32");
mappings.put(":-O", "\uD83D\uDE32");
mappings.put(":*", "\uD83D\uDE18");
mappings.put(":-*", "\uD83D\uDE18");
mappings.put("8)", "\uD83D\uDE0E");
mappings.put("8-)", "\uD83D\uDE0E");
mappings.put(":'(", "\uD83D\uDE22");
mappings.put(":'-(", "\uD83D\uDE22");
mappings.put(":X", "\uD83D\uDE2F");
mappings.put(":-X", "\uD83D\uDE2F");

StringBuilder regex = new StringBuilder("(");

for (String emoji : mappings.keySet()) {
regex.append(Pattern.quote(emoji));
regex.append("|");
}

regex.deleteCharAt(regex.length() - 1);
regex.append(")");

pattern = Pattern.compile(regex.toString());
}

/**
* Replace text smileys like :) with Emojis.
*/
public static String convert(String text) {
Matcher matcher = pattern.matcher(text);
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
matcher.appendReplacement(buffer, mappings.get(matcher.group(1)));
}

matcher.appendTail(buffer);

return buffer.toString();
}
}
129 changes: 0 additions & 129 deletions app/src/main/java/org/yaaic/utils/Smilies.java

This file was deleted.

Binary file removed app/src/main/res/drawable-mdpi/smiley_cool.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_cry.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_embarassed.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_foot_in_mouth.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_frown.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_innocent.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_kiss.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_laughing.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_money_mouth.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_sealed.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_smile.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_surprised.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_tongue_out.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_undecided.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_wink.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_wtf.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/smiley_yell.png
Binary file not shown.

0 comments on commit 38611c4

Please sign in to comment.