summaryrefslogtreecommitdiff
path: root/src/com/android/im/app/Markup.java
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2008-10-21 07:00:00 -0700
committerThe Android Open Source Project <initial-contribution@android.com>2008-10-21 07:00:00 -0700
commit4879f9aa9a3ba8ea89fe31f8d765c4908d4fd53a (patch)
tree96412e1299b497c31f773d4cf2e0094ed07ca715 /src/com/android/im/app/Markup.java
downloadIM-4879f9aa9a3ba8ea89fe31f8d765c4908d4fd53a.tar.gz
Diffstat (limited to 'src/com/android/im/app/Markup.java')
-rw-r--r--src/com/android/im/app/Markup.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/com/android/im/app/Markup.java b/src/com/android/im/app/Markup.java
new file mode 100644
index 0000000..b4f07b0
--- /dev/null
+++ b/src/com/android/im/app/Markup.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2008 Esmertec AG.
+ * Copyright (C) 2008 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.
+ */
+
+package com.android.im.app;
+
+import com.android.im.plugin.BrandingResourceIDs;
+
+import android.graphics.drawable.Drawable;
+import android.text.Spannable;
+import android.text.SpannableString;
+import android.text.style.ImageSpan;
+import android.text.util.Linkify;
+
+public class Markup {
+ private BrandingResources mRes;
+ private IntTrie mSmileys;
+
+ public Markup(BrandingResources res) {
+ mRes = res;
+ mSmileys = new IntTrie(
+ res.getStringArray(BrandingResourceIDs.STRING_ARRAY_SMILEY_TEXTS), res.getSmileyIcons());
+ }
+
+ public final CharSequence markup(CharSequence text) {
+ SpannableString result;
+
+ if (text instanceof SpannableString) {
+ result = (SpannableString) text;
+ } else {
+ result = new SpannableString(text);
+ }
+
+ Linkify.addLinks(result, Linkify.ALL);
+ applyEmoticons(result);
+
+ return result;
+ }
+
+ public final CharSequence applyEmoticons(CharSequence text) {
+ int offset = 0;
+ final int len = text.length();
+ SpannableString result = null;
+
+ while (offset < len) {
+ int index = offset;
+ IntTrie.Node n = mSmileys.getNode(text.charAt(index++));
+ int candidate = 0;
+ int lastMatchEnd = -1;
+
+ // Search the trie until we stop matching
+ while (n != null) {
+ // Record the value and position of the longest match
+ if (n.mValue != 0) {
+ candidate = n.mValue;
+ lastMatchEnd = index;
+ }
+
+ // Let's not run off the end of the input
+ if (index >= len) {
+ break;
+ }
+
+ n = n.getNode(text.charAt(index++));
+ }
+
+ // If we matched a smiley, apply its image over the text
+ if (candidate != 0) {
+ // Lazy-convert the result text to a SpannableString if we have to
+ if (result == null) {
+ if (text instanceof SpannableString) {
+ result = (SpannableString) text;
+ } else {
+ result = new SpannableString(text);
+ text = result;
+ }
+ }
+ Drawable smiley = mRes.getSmileyIcon(candidate);
+ smiley.setBounds(0, 0, smiley.getIntrinsicWidth(), smiley.getIntrinsicHeight());
+ result.setSpan(new ImageSpan(smiley),
+ offset, lastMatchEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+
+ candidate = 0;
+ }
+
+ // if there was a match, start searching for the next one after it
+ // if no match, start at the next character
+ if (lastMatchEnd != -1) {
+ offset = lastMatchEnd;
+ lastMatchEnd = -1;
+ } else {
+ offset++;
+ }
+ }
+
+ // If there were no modifications, return the original string
+ if (result == null) {
+ return text;
+ }
+
+ return result;
+ }
+}