FileDocCategorySizeDatePackage
SmileyParser.javaAPI DocAndroid 1.5 API5907Wed May 06 22:42:46 BST 2009com.android.mms.util

SmileyParser

public class SmileyParser extends Object
A class for annotating a CharSequence with spans to convert textual emoticons to graphical ones.

Fields Summary
private static SmileyParser
sInstance
private final android.content.Context
mContext
private final String[]
mSmileyTexts
private final Pattern
mPattern
private final HashMap
mSmileyToRes
public static final int[]
DEFAULT_SMILEY_RES_IDS
public static final int
DEFAULT_SMILEY_TEXTS
public static final int
DEFAULT_SMILEY_NAMES
Constructors Summary
private SmileyParser(android.content.Context context)

        mContext = context;
        mSmileyTexts = mContext.getResources().getStringArray(DEFAULT_SMILEY_TEXTS);
        mSmileyToRes = buildSmileyToRes();
        mPattern = buildPattern();
    
Methods Summary
public java.lang.CharSequenceaddSmileySpans(java.lang.CharSequence text)
Adds ImageSpans to a CharSequence that replace textual emoticons such as :-) with a graphical version.

param
text A CharSequence possibly containing emoticons
return
A CharSequence annotated with ImageSpans covering any recognized emoticons.

        SpannableStringBuilder builder = new SpannableStringBuilder(text);

        Matcher matcher = mPattern.matcher(text);
        while (matcher.find()) {
            int resId = mSmileyToRes.get(matcher.group());
            builder.setSpan(new ImageSpan(mContext, resId),
                            matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        return builder;
    
private java.util.regex.PatternbuildPattern()
Builds the regular expression we use to find smileys in {@link #addSmileySpans}.

        // Set the StringBuilder capacity with the assumption that the average
        // smiley is 3 characters long.
        StringBuilder patternString = new StringBuilder(mSmileyTexts.length * 3);

        // Build a regex that looks like (:-)|:-(|...), but escaping the smilies
        // properly so they will be interpreted literally by the regex matcher.
        patternString.append('(");
        for (String s : mSmileyTexts) {
            patternString.append(Pattern.quote(s));
            patternString.append('|");
        }
        // Replace the extra '|' with a ')'
        patternString.replace(patternString.length() - 1, patternString.length(), ")");
        
        return Pattern.compile(patternString.toString());
    
private java.util.HashMapbuildSmileyToRes()
Builds the hashtable we use for mapping the string version of a smiley (e.g. ":-)") to a resource ID for the icon version.


                                
        
        if (DEFAULT_SMILEY_RES_IDS.length != mSmileyTexts.length) {
            // Throw an exception if someone updated DEFAULT_SMILEY_RES_IDS
            // and failed to update arrays.xml
            throw new IllegalStateException("Smiley resource ID/text mismatch");
        }

        HashMap<String, Integer> smileyToRes =
                            new HashMap<String, Integer>(mSmileyTexts.length);
        for (int i = 0; i < mSmileyTexts.length; i++) {
            smileyToRes.put(mSmileyTexts[i], DEFAULT_SMILEY_RES_IDS[i]);
        }
        
        return smileyToRes;
    
public static com.android.mms.util.SmileyParsergetInstance()

 return sInstance; 
public static voidinit(android.content.Context context)

        sInstance = new SmileyParser(context);