Source code
package android.support.v4.text;
import android.os.Build.VERSION;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Locale;
public class TextUtilsCompat {
private static String ARAB_SCRIPT_SUBTAG = "Arab";
private static String HEBR_SCRIPT_SUBTAG = "Hebr";
private static final TextUtilsCompatImpl IMPL;
public static final Locale ROOT = new Locale("", "");
private static class TextUtilsCompatImpl {
private TextUtilsCompatImpl() {
}
@NonNull
public String htmlEncode(@NonNull String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\"':
sb.append(""");
break;
case '&':
sb.append("&");
break;
case '\'':
sb.append("'");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
default:
sb.append(c);
break;
}
}
return sb.toString();
}
public int getLayoutDirectionFromLocale(@Nullable Locale locale) {
if (!(locale == null || locale.equals(TextUtilsCompat.ROOT))) {
String scriptSubtag = ICUCompat.maximizeAndGetScript(locale);
if (scriptSubtag == null) {
return getLayoutDirectionFromFirstChar(locale);
}
if (scriptSubtag.equalsIgnoreCase(TextUtilsCompat.ARAB_SCRIPT_SUBTAG) || scriptSubtag.equalsIgnoreCase(TextUtilsCompat.HEBR_SCRIPT_SUBTAG)) {
return 1;
}
}
return 0;
}
private static int getLayoutDirectionFromFirstChar(@NonNull Locale locale) {
switch (Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
case (byte) 1:
case (byte) 2:
return 1;
default:
return 0;
}
}
}
private static class TextUtilsCompatJellybeanMr1Impl extends TextUtilsCompatImpl {
private TextUtilsCompatJellybeanMr1Impl() {
super();
}
@NonNull
public String htmlEncode(@NonNull String s) {
return TextUtilsCompatJellybeanMr1.htmlEncode(s);
}
public int getLayoutDirectionFromLocale(@Nullable Locale locale) {
return TextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale);
}
}
static {
if (VERSION.SDK_INT >= 17) {
IMPL = new TextUtilsCompatJellybeanMr1Impl();
} else {
IMPL = new TextUtilsCompatImpl();
}
}
@NonNull
public static String htmlEncode(@NonNull String s) {
return IMPL.htmlEncode(s);
}
public static int getLayoutDirectionFromLocale(@Nullable Locale locale) {
return IMPL.getLayoutDirectionFromLocale(locale);
}
}