FileDocCategorySizeDatePackage
TextUtilsCompat.javaAPI DocAndroid 5.1 API4424Thu Mar 12 22:22:56 GMT 2015android.support.v4.text

TextUtilsCompat

public class TextUtilsCompat extends Object

Fields Summary
public static final Locale
ROOT
private static String
ARAB_SCRIPT_SUBTAG
private static String
HEBR_SCRIPT_SUBTAG
Constructors Summary
Methods Summary
private static intgetLayoutDirectionFromFirstChar(java.util.Locale locale)
Fallback algorithm to detect the locale direction. Rely on the fist char of the localized locale name. This will not work if the localized locale name is in English (this is the case for ICU 4.4 and "Urdu" script)

param
locale
return
the layout direction. This may be one of: {@link ViewCompat#LAYOUT_DIRECTION_LTR} or {@link ViewCompat#LAYOUT_DIRECTION_RTL}. Be careful: this code will need to be updated when vertical scripts will be supported

        switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
            case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
            case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
                return ViewCompat.LAYOUT_DIRECTION_RTL;

            case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
            default:
                return ViewCompat.LAYOUT_DIRECTION_LTR;
        }
    
public static intgetLayoutDirectionFromLocale(java.util.Locale locale)
Return the layout direction for a given Locale

param
locale the Locale for which we want the layout direction. Can be null.
return
the layout direction. This may be one of: {@link ViewCompat#LAYOUT_DIRECTION_LTR} or {@link ViewCompat#LAYOUT_DIRECTION_RTL}. Be careful: this code will need to be updated when vertical scripts will be supported

        if (locale != null && !locale.equals(ROOT)) {
            final String scriptSubtag = ICUCompat.getScript(
                    ICUCompat.addLikelySubtags(locale.toString()));
            if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);

            if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||
                    scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
                return ViewCompat.LAYOUT_DIRECTION_RTL;
            }
        }

        return ViewCompat.LAYOUT_DIRECTION_LTR;
    
public static java.lang.StringhtmlEncode(java.lang.String s)
Html-encode the string.

param
s the string to be encoded
return
the encoded string

        StringBuilder sb = new StringBuilder();
        char c;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            switch (c) {
                case '<":
                    sb.append("<"); //$NON-NLS-1$
                    break;
                case '>":
                    sb.append(">"); //$NON-NLS-1$
                    break;
                case '&":
                    sb.append("&"); //$NON-NLS-1$
                    break;
                case '\'":
                    //http://www.w3.org/TR/xhtml1
                    // The named character reference ' (the apostrophe, U+0027) was introduced in
                    // XML 1.0 but does not appear in HTML. Authors should therefore use ' instead
                    // of ' to work as expected in HTML 4 user agents.
                    sb.append("'"); //$NON-NLS-1$
                    break;
                case '"":
                    sb.append("""); //$NON-NLS-1$
                    break;
                default:
                    sb.append(c);
            }
        }
        return sb.toString();