|
46 | 46 | import android.text.style.UnderlineSpan; |
47 | 47 | import android.util.Printer; |
48 | 48 |
|
| 49 | +import android.view.View; |
49 | 50 | import com.android.internal.R; |
50 | 51 | import com.android.internal.util.ArrayUtils; |
| 52 | +import libcore.icu.ICU; |
51 | 53 |
|
52 | 54 | import java.lang.reflect.Array; |
53 | 55 | import java.util.Iterator; |
| 56 | +import java.util.Locale; |
54 | 57 | import java.util.regex.Pattern; |
55 | 58 |
|
56 | 59 | public class TextUtils { |
@@ -1706,10 +1709,64 @@ public static int unpackRangeEndFromLong(long range) { |
1706 | 1709 | return (int) (range & 0x00000000FFFFFFFFL); |
1707 | 1710 | } |
1708 | 1711 |
|
| 1712 | + /** |
| 1713 | + * Return the layout direction for a given Locale |
| 1714 | + * |
| 1715 | + * @param locale the Locale for which we want the layout direction. Can be null. |
| 1716 | + * @return the layout direction. This may be one of: |
| 1717 | + * {@link android.view.View#LAYOUT_DIRECTION_LTR} or |
| 1718 | + * {@link android.view.View#LAYOUT_DIRECTION_RTL}. |
| 1719 | + * |
| 1720 | + * Be careful: this code will need to be updated when vertical scripts will be supported |
| 1721 | + */ |
| 1722 | + public static int getLayoutDirectionFromLocale(Locale locale) { |
| 1723 | + if (locale != null && !locale.equals(Locale.ROOT)) { |
| 1724 | + final String scriptSubtag = ICU.getScript(ICU.addLikelySubtags(locale.toString())); |
| 1725 | + if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale); |
| 1726 | + |
| 1727 | + if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) || |
| 1728 | + scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) { |
| 1729 | + return View.LAYOUT_DIRECTION_RTL; |
| 1730 | + } |
| 1731 | + } |
| 1732 | + |
| 1733 | + return View.LAYOUT_DIRECTION_LTR; |
| 1734 | + } |
| 1735 | + |
| 1736 | + /** |
| 1737 | + * Fallback algorithm to detect the locale direction. Rely on the fist char of the |
| 1738 | + * localized locale name. This will not work if the localized locale name is in English |
| 1739 | + * (this is the case for ICU 4.4 and "Urdu" script) |
| 1740 | + * |
| 1741 | + * @param locale |
| 1742 | + * @return the layout direction. This may be one of: |
| 1743 | + * {@link View#LAYOUT_DIRECTION_LTR} or |
| 1744 | + * {@link View#LAYOUT_DIRECTION_RTL}. |
| 1745 | + * |
| 1746 | + * Be careful: this code will need to be updated when vertical scripts will be supported |
| 1747 | + * |
| 1748 | + * @hide |
| 1749 | + */ |
| 1750 | + private static int getLayoutDirectionFromFirstChar(Locale locale) { |
| 1751 | + switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) { |
| 1752 | + case Character.DIRECTIONALITY_RIGHT_TO_LEFT: |
| 1753 | + case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: |
| 1754 | + return View.LAYOUT_DIRECTION_RTL; |
| 1755 | + |
| 1756 | + case Character.DIRECTIONALITY_LEFT_TO_RIGHT: |
| 1757 | + default: |
| 1758 | + return View.LAYOUT_DIRECTION_LTR; |
| 1759 | + } |
| 1760 | + } |
| 1761 | + |
1709 | 1762 | private static Object sLock = new Object(); |
| 1763 | + |
1710 | 1764 | private static char[] sTemp = null; |
1711 | 1765 |
|
1712 | 1766 | private static String[] EMPTY_STRING_ARRAY = new String[]{}; |
1713 | 1767 |
|
1714 | 1768 | private static final char ZWNBS_CHAR = '\uFEFF'; |
| 1769 | + |
| 1770 | + private static String ARAB_SCRIPT_SUBTAG = "Arab"; |
| 1771 | + private static String HEBR_SCRIPT_SUBTAG = "Hebr"; |
1715 | 1772 | } |
0 commit comments