1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-08-13 16:53:50 -04:00

Add support for graphical smilies in chat.

This commit is contained in:
liato 2011-03-18 04:08:21 +01:00 committed by Sebastian Kaspari
parent 92a1e93bc1
commit ac1a07894f
23 changed files with 140 additions and 7 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 799 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 798 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 792 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 762 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 830 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 813 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 775 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 792 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 754 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 757 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 744 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 759 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 748 B

View File

@ -41,4 +41,7 @@
<string name="key_mirc_colors">mirc_colors</string>
<string name="default_mirc_colors">true</string>
<string name="key_graphical_smilies">graphical_smilies</string>
<string name="default_graphical_smilies">false</string>
</resources>

View File

@ -198,4 +198,6 @@
<string name="settings_noticeserverwindow_desc">Show notices in server window</string>
<string name="settings_mirc_colors_title">Show message colors</string>
<string name="settings_mirc_colors_desc">Show mIRC colors in messages</string>
<string name="settings_graphical_smilies_title">Show graphical smilies</string>
<string name="settings_graphical_smilies_desc">Text smilies will be displayed as images in chat</string>
</resources>

View File

@ -79,7 +79,12 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
android:title="@string/settings_mirc_colors_title"
android:summary="@string/settings_mirc_colors_desc"
android:key="@string/key_mirc_colors"
android:defaultValue="@string/default_mirc_colors" />
android:defaultValue="@string/default_mirc_colors" />
<CheckBoxPreference
android:title="@string/settings_graphical_smilies_title"
android:summary="@string/settings_graphical_smilies_desc"
android:key="@string/key_graphical_smilies"
android:defaultValue="@string/default_graphical_smilies" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/settings_highlight">

View File

@ -23,6 +23,7 @@ package org.yaaic.model;
import java.util.Date;
import org.yaaic.utils.MircColors;
import org.yaaic.utils.Smilies;
import android.content.Context;
import android.graphics.Typeface;
@ -228,14 +229,15 @@ public class Message
String prefix = hasIcon() && settings.showIcons() ? " " : "";
String nick = hasSender() ? "<" + sender + "> " : "";
String timestamp = settings.showTimestamp() ? renderTimeStamp(settings.use24hFormat()) : "";
canvas = new SpannableString(prefix + timestamp + nick);
SpannableString renderedText = new SpannableString(text);
if (settings.showMircColors()) {
canvas = new SpannableString(prefix + timestamp + nick);
canvas = new SpannableString(TextUtils.concat(canvas, MircColors.toSpannable(text)));
renderedText = MircColors.toSpannable(text);
}
else {
canvas = new SpannableString(prefix + timestamp + nick + MircColors.removeStyleAndColors(text));
if (settings.showGraphicalSmilies()) {
renderedText = Smilies.toSpannable(renderedText, context);
}
canvas = new SpannableString(TextUtils.concat(canvas, renderedText));
if (hasSender()) {
int start = (prefix + timestamp).length() + 1;
int end = start + sender.length();

View File

@ -234,4 +234,17 @@ public class Settings
Boolean.parseBoolean(resources.getString(R.string.default_mirc_colors))
);
}
/**
* Render messages with graphical smilies.
*
* @return True if text smilies should be rendered as graphical smilies, false otherwise.
*/
public boolean showGraphicalSmilies()
{
return preferences.getBoolean(
resources.getString(R.string.key_graphical_smilies),
Boolean.parseBoolean(resources.getString(R.string.default_graphical_smilies))
);
}
}

View File

@ -54,7 +54,7 @@ public class MircColors {
* @param text A string with mIRC color codes.
* @return A SpannableString with all the styles applied.
*/
public static SpannableString toSpannable(String text) {
public static SpannableString toSpannable(SpannableString text) {
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
replaceControlCodes(boldPattern.matcher(ssb), ssb, new StyleSpan(Typeface.BOLD));
replaceControlCodes(underlinePattern.matcher(ssb), ssb, new UnderlineSpan());
@ -96,6 +96,17 @@ public class MircColors {
return new SpannableString(removeStyleAndColors(ssb));
}
/**
* Converts a string with mIRC style and color codes to a SpannableString with
* all the style and color codes applied.
*
* @param text A string with mIRC color codes.
* @return A SpannableString with all the styles applied.
*/
public static SpannableString toSpannable(String text) {
return toSpannable(new SpannableString(text));
}
private static void replaceControlCodes(Matcher m, SpannableStringBuilder ssb, CharacterStyle style) {
ArrayList<Integer> toremove = new ArrayList<Integer>();
while (m.find()) {

View File

@ -0,0 +1,97 @@
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;
public class Smilies {
public static final HashMap<String, Integer> mappings = new HashMap<String, Integer>();
private Smilies() {}
/**
* Converts all smilies in a string to graphical smilies.
*
* @param text A string with smilies.
* @return A SpannableString with graphical smilies.
*/
public static SpannableString toSpannable(SpannableString text, Context context) {
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);
mappings.put(":´(", R.drawable.smiley_cry);
mappings.put(":´-(", R.drawable.smiley_cry);
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()]);
for (int i = 0; i < smilies.length; i++) {
regex.append(Pattern.quote(smilies[i]));
regex.append("|");
}
regex.deleteCharAt(regex.length()-1);
regex.append(")");
Pattern smiliematcher = Pattern.compile(regex.toString());
Matcher m = smiliematcher.matcher(text);
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);
}
return text;
}
/**
* Converts all smilies in a string to graphical smilies.
*
* @param text A string with smilies.
* @return A SpannableString with graphical smilies.
*/
public static SpannableString toSpannable(String text, Context context) {
return toSpannable(new SpannableString(text), context);
}
}