/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ packagecom.tao.test; importandroid.content.Context; importandroid.text.Spannable; importandroid.text.SpannableStringBuilder; importandroid.text.style.ImageSpan; //import com.android.mms.R; importjava.util.HashMap; importjava.util.regex.Matcher; importjava.util.regex.Pattern; /** * A class for annotating a CharSequence with spans to convert textual emoticons * to graphical ones. */ publicclassSmileyParser{ // Singleton stuff privatestaticSmileyParser sInstance; publicstaticSmileyParsergetInstance(){returnsInstance;} publicstaticvoidinit(Contextcontext){ sInstance=newSmileyParser(context); } privatefinalContextmContext; privatefinalString[]mSmileyTexts; privatefinalPatternmPattern; privatefinalHashMap<String,Integer>mSmileyToRes; privateSmileyParser(Contextcontext){ mContext=context; mSmileyTexts=mContext.getResources().getStringArray(DEFAULT_SMILEY_TEXTS); mSmileyToRes=buildSmileyToRes(); mPattern=buildPattern(); } staticclassSmileys{ privatestaticfinalint[]sIconIds={ R.drawable.emo_im_happy, R.drawable.emo_im_sad, R.drawable.emo_im_winking, R.drawable.emo_im_tongue_sticking_out, R.drawable.emo_im_surprised, R.drawable.emo_im_kissing, R.drawable.emo_im_yelling, R.drawable.emo_im_cool, R.drawable.emo_im_money_mouth, R.drawable.emo_im_foot_in_mouth, R.drawable.emo_im_embarrassed, R.drawable.emo_im_angel, R.drawable.emo_im_undecided, R.drawable.emo_im_crying, R.drawable.emo_im_lips_are_sealed, R.drawable.emo_im_laughing, R.drawable.emo_im_wtf }; publicstaticintHAPPY=0; publicstaticintSAD=1; publicstaticintWINKING=2; publicstaticintTONGUE_STICKING_OUT=3; publicstaticintSURPRISED=4; publicstaticintKISSING=5; publicstaticintYELLING=6; publicstaticintCOOL=7; publicstaticintMONEY_MOUTH=8; publicstaticintFOOT_IN_MOUTH=9; publicstaticintEMBARRASSED=10; publicstaticintANGEL=11; publicstaticintUNDECIDED=12; publicstaticintCRYING=13; publicstaticintLIPS_ARE_SEALED=14; publicstaticintLAUGHING=15; publicstaticintWTF=16; publicstaticintgetSmileyResource(intwhich){ returnsIconIds[which]; } } // NOTE: if you change anything about this array, you must make the corresponding change // to the string arrays: default_smiley_texts and default_smiley_names in res/values/arrays.xml publicstaticfinalint[]DEFAULT_SMILEY_RES_IDS={ Smileys.getSmileyResource(Smileys.HAPPY),// 0 Smileys.getSmileyResource(Smileys.SAD),// 1 Smileys.getSmileyResource(Smileys.WINKING),// 2 Smileys.getSmileyResource(Smileys.TONGUE_STICKING_OUT),// 3 Smileys.getSmileyResource(Smileys.SURPRISED),// 4 Smileys.getSmileyResource(Smileys.KISSING),// 5 Smileys.getSmileyResource(Smileys.YELLING),// 6 Smileys.getSmileyResource(Smileys.COOL),// 7 Smileys.getSmileyResource(Smileys.MONEY_MOUTH),// 8 Smileys.getSmileyResource(Smileys.FOOT_IN_MOUTH),// 9 Smileys.getSmileyResource(Smileys.EMBARRASSED),// 10 Smileys.getSmileyResource(Smileys.ANGEL),// 11 Smileys.getSmileyResource(Smileys.UNDECIDED),// 12 Smileys.getSmileyResource(Smileys.CRYING),// 13 Smileys.getSmileyResource(Smileys.LIPS_ARE_SEALED),// 14 Smileys.getSmileyResource(Smileys.LAUGHING),// 15 Smileys.getSmileyResource(Smileys.WTF),// 16 }; publicstaticfinalintDEFAULT_SMILEY_TEXTS=R.array.default_smiley_texts; publicstaticfinalintDEFAULT_SMILEY_NAMES=R.array.default_smiley_names; /** * Builds the hashtable we use for mapping the string version * of a smiley (e.g. ":-)") to a resource ID for the icon version. */ privateHashMap<String,Integer>buildSmileyToRes(){ if(DEFAULT_SMILEY_RES_IDS.length!=mSmileyTexts.length){ // Throw an exception if someone updated DEFAULT_SMILEY_RES_IDS // and failed to update arrays.xml thrownewIllegalStateException("Smiley resource ID/text mismatch"); } HashMap<String,Integer>smileyToRes= newHashMap<String,Integer>(mSmileyTexts.length); for(inti=0;i<mSmileyTexts.length;i++){ smileyToRes.put(mSmileyTexts[i],DEFAULT_SMILEY_RES_IDS[i]); } returnsmileyToRes; } /** * Builds the regular expression we use to find smileys in {@link #addSmileySpans}. */ privatePatternbuildPattern(){ // Set the StringBuilder capacity with the assumption that the average // smiley is 3 characters long. StringBuilderpatternString=newStringBuilder(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(Strings:mSmileyTexts){ patternString.append(Pattern.quote(s)); patternString.append('|'); } // Replace the extra '|' with a ')' patternString.replace(patternString.length()-1,patternString.length(),")"); returnPattern.compile(patternString.toString()); } /** * 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. */ publicCharSequenceaddSmileySpans(CharSequencetext){ SpannableStringBuilder builder=newSpannableStringBuilder(text); Matchermatcher=mPattern.matcher(text); while(matcher.find()){ intresId=mSmileyToRes.get(matcher.group()); builder.setSpan(newImageSpan(mContext,resId), matcher.start(),matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } returnbuilder; } } |