2011-03-27 13:53:05 -04:00
|
|
|
|
/*
|
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
|
|
|
|
|
Copyright 2009-2011 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/>.
|
|
|
|
|
*/
|
2011-03-17 23:08:21 -04:00
|
|
|
|
package org.yaaic.utils;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
import org.yaaic.R;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.text.Spannable;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2011-03-27 14:02:06 -04:00
|
|
|
|
/**
|
|
|
|
|
* Class for handling graphical smilies in text messages.
|
|
|
|
|
*
|
2011-04-12 18:10:47 -04:00
|
|
|
|
* @author Liato
|
2011-03-27 14:02:06 -04:00
|
|
|
|
*/
|
|
|
|
|
public abstract class Smilies
|
|
|
|
|
{
|
|
|
|
|
private static final HashMap<String, Integer> mappings = new HashMap<String, Integer>();
|
2011-03-17 23:08:21 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts all smilies in a string to graphical smilies.
|
|
|
|
|
*
|
|
|
|
|
* @param text A string with smilies.
|
|
|
|
|
* @return A SpannableString with graphical smilies.
|
|
|
|
|
*/
|
2011-03-27 14:02:06 -04:00
|
|
|
|
public static SpannableString toSpannable(SpannableString text, Context context)
|
|
|
|
|
{
|
2011-03-17 23:08:21 -04:00
|
|
|
|
mappings.put(">:o", R.drawable.smiley_yell);
|
|
|
|
|
mappings.put(">:-o", R.drawable.smiley_yell);
|
|
|
|
|
mappings.put("O:)", R.drawable.smiley_innocent);
|
|
|
|
|
mappings.put("O:-)", R.drawable.smiley_innocent);
|
|
|
|
|
mappings.put(":)", R.drawable.smiley_smile);
|
|
|
|
|
mappings.put(":-)", R.drawable.smiley_smile);
|
|
|
|
|
mappings.put(":(", R.drawable.smiley_frown);
|
|
|
|
|
mappings.put(":-(", R.drawable.smiley_frown);
|
|
|
|
|
mappings.put(";)", R.drawable.smiley_wink);
|
|
|
|
|
mappings.put(";-)", R.drawable.smiley_wink);
|
|
|
|
|
mappings.put(":p", R.drawable.smiley_tongue_out);
|
|
|
|
|
mappings.put(":-p", R.drawable.smiley_tongue_out);
|
|
|
|
|
mappings.put(":P", R.drawable.smiley_tongue_out);
|
|
|
|
|
mappings.put(":-P", R.drawable.smiley_tongue_out);
|
|
|
|
|
mappings.put(":D", R.drawable.smiley_laughing);
|
|
|
|
|
mappings.put(":-D", R.drawable.smiley_laughing);
|
|
|
|
|
mappings.put(":[", R.drawable.smiley_embarassed);
|
|
|
|
|
mappings.put(":-[", R.drawable.smiley_embarassed);
|
|
|
|
|
mappings.put(":\\", R.drawable.smiley_undecided);
|
|
|
|
|
mappings.put(":-\\", R.drawable.smiley_undecided);
|
|
|
|
|
mappings.put(":o", R.drawable.smiley_surprised);
|
|
|
|
|
mappings.put(":-o", R.drawable.smiley_surprised);
|
|
|
|
|
mappings.put(":O", R.drawable.smiley_surprised);
|
|
|
|
|
mappings.put(":-O", R.drawable.smiley_surprised);
|
|
|
|
|
mappings.put(":*", R.drawable.smiley_kiss);
|
|
|
|
|
mappings.put(":-*", R.drawable.smiley_kiss);
|
|
|
|
|
mappings.put("8)", R.drawable.smiley_cool);
|
|
|
|
|
mappings.put("8-)", R.drawable.smiley_cool);
|
|
|
|
|
mappings.put(":$", R.drawable.smiley_money_mouth);
|
|
|
|
|
mappings.put(":-$", R.drawable.smiley_money_mouth);
|
|
|
|
|
mappings.put(":!", R.drawable.smiley_foot_in_mouth);
|
|
|
|
|
mappings.put(":-!", R.drawable.smiley_foot_in_mouth);
|
|
|
|
|
mappings.put(":'(", R.drawable.smiley_cry);
|
|
|
|
|
mappings.put(":'-(", R.drawable.smiley_cry);
|
2011-03-27 13:53:05 -04:00
|
|
|
|
mappings.put(":<3A>(", R.drawable.smiley_cry);
|
|
|
|
|
mappings.put(":<3A>-(", R.drawable.smiley_cry);
|
2011-03-17 23:08:21 -04:00
|
|
|
|
mappings.put(":X", R.drawable.smiley_sealed);
|
|
|
|
|
mappings.put(":-X", R.drawable.smiley_sealed);
|
|
|
|
|
|
|
|
|
|
StringBuilder regex = new StringBuilder("(");
|
|
|
|
|
String[] smilies = mappings.keySet().toArray(new String[mappings.size()]);
|
2011-03-27 14:02:06 -04:00
|
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
|
for (int i = 0; i < smilies.length; i++) {
|
|
|
|
|
regex.append(Pattern.quote(smilies[i]));
|
|
|
|
|
regex.append("|");
|
|
|
|
|
}
|
2011-03-27 14:02:06 -04:00
|
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
|
regex.deleteCharAt(regex.length()-1);
|
|
|
|
|
regex.append(")");
|
|
|
|
|
Pattern smiliematcher = Pattern.compile(regex.toString());
|
|
|
|
|
Matcher m = smiliematcher.matcher(text);
|
2011-03-27 14:02:06 -04:00
|
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
|
while (m.find()) {
|
|
|
|
|
Log.d("Smilies", "SID: "+mappings.get(m.group(1)).intValue());
|
|
|
|
|
Log.d("Smilies", "OID: "+R.drawable.smiley_smile);
|
|
|
|
|
Drawable smilie = context.getResources().getDrawable(mappings.get(m.group(1)).intValue());
|
|
|
|
|
smilie.setBounds(0, 0, smilie.getIntrinsicWidth(), smilie.getIntrinsicHeight());
|
|
|
|
|
ImageSpan span = new ImageSpan(smilie, ImageSpan.ALIGN_BOTTOM);
|
|
|
|
|
text.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
}
|
2011-03-27 14:02:06 -04:00
|
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts all smilies in a string to graphical smilies.
|
|
|
|
|
*
|
|
|
|
|
* @param text A string with smilies.
|
|
|
|
|
* @return A SpannableString with graphical smilies.
|
|
|
|
|
*/
|
2011-03-27 14:02:06 -04:00
|
|
|
|
public static SpannableString toSpannable(String text, Context context)
|
|
|
|
|
{
|
2011-03-17 23:08:21 -04:00
|
|
|
|
return toSpannable(new SpannableString(text), context);
|
|
|
|
|
}
|
2011-03-27 14:02:06 -04:00
|
|
|
|
}
|