Debian Codegraph @

core/java/com/google/android/util/SmileyParser.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.android.util;
18
19 import android.content.Context;
20 import android.text.Spannable;
21 import android.text.SpannableStringBuilder;
22 import android.text.style.ImageSpan;
23
24 import java.util.ArrayList;
25
26 /**
27 * Parses a text message typed by the user looking for smileys.
28 */
29 public class SmileyParser extends AbstractMessageParser {
30
31 private SmileyResources mRes;
32
33 public SmileyParser(String text, SmileyResources res) {
34 super(text,
35 true, // smilies
36 false, // acronyms
37 false, // formatting
38 false, // urls
39 false, // music
40 false // me text
41 );
42 mRes = res;
43 }
44
45 @Override
46 protected Resources getResources() {
47 return mRes;
48 }
49
50 /**
51 * Retrieves the parsed text as a spannable string object.
52 * @param context the context for fetching smiley resources.
53 * @return the spannable string as CharSequence.
54 */
55 public CharSequence getSpannableString(Context context) {
56 SpannableStringBuilder builder = new SpannableStringBuilder();
57
58 if (getPartCount() == 0) {
59 return "";
60 }
61
62 // should have only one part since we parse smiley only
63 Part part = getPart(0);
64 ArrayList<Token> tokens = part.getTokens();
65 int len = tokens.size();
66 for (int i = 0; i < len; i++) {
67 Token token = tokens.get(i);
68 int start = builder.length();
69 builder.append(token.getRawText());
70 if (token.getType() == AbstractMessageParser.Token.Type.SMILEY) {
71 int resid = mRes.getSmileyRes(token.getRawText());
72 if (resid != -1) {
73 builder.setSpan(new ImageSpan(context, resid),
74 start,
75 builder.length(),
76 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
77 }
78 }
79 }
80 return builder;
81 }
82
83 }