diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
commit | f013e1afd1e68af5e3b868c26a653bbfb39538f8 (patch) | |
tree | 7ad6c8fd9c7b55f4b4017171dec1cb760bbd26bf /core/java/android/text/TextUtils.java | |
parent | e70cfafe580c6f2994c4827cd8a534aabf3eb05c (diff) | |
download | frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.zip frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.gz frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.bz2 |
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'core/java/android/text/TextUtils.java')
-rw-r--r-- | core/java/android/text/TextUtils.java | 159 |
1 files changed, 156 insertions, 3 deletions
diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java index e791aaf..64356d5 100644 --- a/core/java/android/text/TextUtils.java +++ b/core/java/android/text/TextUtils.java @@ -22,6 +22,7 @@ import android.content.res.ColorStateList; import android.content.res.Resources; import android.os.Parcel; import android.os.Parcelable; +import android.text.method.TextKeyListener.Capitalize; import android.text.style.AbsoluteSizeSpan; import android.text.style.AlignmentSpan; import android.text.style.BackgroundColorSpan; @@ -42,13 +43,14 @@ import android.text.style.TextAppearanceSpan; import android.text.style.TypefaceSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; +import android.util.Printer; + import com.android.internal.util.ArrayUtils; import java.util.regex.Pattern; import java.util.Iterator; -public class TextUtils -{ +public class TextUtils { private TextUtils() { /* cannot be instantiated */ } private static String[] EMPTY_STRING_ARRAY = new String[]{}; @@ -827,6 +829,30 @@ public class TextUtils }; /** + * Debugging tool to print the spans in a CharSequence. The output will + * be printed one span per line. If the CharSequence is not a Spanned, + * then the entire string will be printed on a single line. + */ + public static void dumpSpans(CharSequence cs, Printer printer, String prefix) { + if (cs instanceof Spanned) { + Spanned sp = (Spanned) cs; + Object[] os = sp.getSpans(0, cs.length(), Object.class); + + for (int i = 0; i < os.length; i++) { + Object o = os[i]; + printer.println(prefix + cs.subSequence(sp.getSpanStart(o), + sp.getSpanEnd(o)) + ": " + + Integer.toHexString(System.identityHashCode(o)) + + " " + o.getClass().getCanonicalName() + + " (" + sp.getSpanStart(o) + "-" + sp.getSpanEnd(o) + + ") fl=#" + sp.getSpanFlags(o)); + } + } else { + printer.println(prefix + cs + ": (no spans)"); + } + } + + /** * Return a new CharSequence in which each of the source strings is * replaced by the corresponding element of the destinations. */ @@ -1021,6 +1047,7 @@ public class TextUtils START, MIDDLE, END, + MARQUEE, } public interface EllipsizeCallback { @@ -1460,7 +1487,7 @@ public class TextUtils case '&': sb.append("&"); //$NON-NLS-1$ break; - case '\\': + case '\'': sb.append("'"); //$NON-NLS-1$ break; case '"': @@ -1565,6 +1592,132 @@ public class TextUtils return true; } + /** + * Capitalization mode for {@link #getCapsMode}: capitalize all + * characters. This value is explicitly defined to be the same as + * {@link InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}. + */ + public static final int CAP_MODE_CHARACTERS + = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; + + /** + * Capitalization mode for {@link #getCapsMode}: capitalize the first + * character of all words. This value is explicitly defined to be the same as + * {@link InputType#TYPE_TEXT_FLAG_CAP_WORDS}. + */ + public static final int CAP_MODE_WORDS + = InputType.TYPE_TEXT_FLAG_CAP_WORDS; + + /** + * Capitalization mode for {@link #getCapsMode}: capitalize the first + * character of each sentence. This value is explicitly defined to be the same as + * {@link InputType#TYPE_TEXT_FLAG_CAP_SENTENCES}. + */ + public static final int CAP_MODE_SENTENCES + = InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; + + /** + * Determine what caps mode should be in effect at the current offset in + * the text. Only the mode bits set in <var>reqModes</var> will be + * checked. Note that the caps mode flags here are explicitly defined + * to match those in {@link InputType}. + * + * @param cs The text that should be checked for caps modes. + * @param off Location in the text at which to check. + * @param reqModes The modes to be checked: may be any combination of + * {@link #CAP_MODE_CHARACTERS}, {@link #CAP_MODE_WORDS}, and + * {@link #CAP_MODE_SENTENCES}. + * + * @return Returns the actual capitalization modes that can be in effect + * at the current position, which is any combination of + * {@link #CAP_MODE_CHARACTERS}, {@link #CAP_MODE_WORDS}, and + * {@link #CAP_MODE_SENTENCES}. + */ + public static int getCapsMode(CharSequence cs, int off, int reqModes) { + int i; + char c; + int mode = 0; + + if ((reqModes&CAP_MODE_CHARACTERS) != 0) { + mode |= CAP_MODE_CHARACTERS; + } + if ((reqModes&(CAP_MODE_WORDS|CAP_MODE_SENTENCES)) == 0) { + return mode; + } + + // Back over allowed opening punctuation. + + for (i = off; i > 0; i--) { + c = cs.charAt(i - 1); + + if (c != '"' && c != '\'' && + Character.getType(c) != Character.START_PUNCTUATION) { + break; + } + } + + // Start of paragraph, with optional whitespace. + + int j = i; + while (j > 0 && ((c = cs.charAt(j - 1)) == ' ' || c == '\t')) { + j--; + } + if (j == 0 || cs.charAt(j - 1) == '\n') { + return mode | CAP_MODE_WORDS; + } + + // Or start of word if we are that style. + + if ((reqModes&CAP_MODE_SENTENCES) == 0) { + if (i != j) mode |= CAP_MODE_WORDS; + return mode; + } + + // There must be a space if not the start of paragraph. + + if (i == j) { + return mode; + } + + // Back over allowed closing punctuation. + + for (; j > 0; j--) { + c = cs.charAt(j - 1); + + if (c != '"' && c != '\'' && + Character.getType(c) != Character.END_PUNCTUATION) { + break; + } + } + + if (j > 0) { + c = cs.charAt(j - 1); + + if (c == '.' || c == '?' || c == '!') { + // Do not capitalize if the word ends with a period but + // also contains a period, in which case it is an abbreviation. + + if (c == '.') { + for (int k = j - 2; k >= 0; k--) { + c = cs.charAt(k); + + if (c == '.') { + return mode; + } + + if (!Character.isLetter(c)) { + break; + } + } + } + + return mode | CAP_MODE_SENTENCES; + } + } + + return mode; + } + private static Object sLock = new Object(); private static char[] sTemp = null; } |