Loading core/java/android/text/TextUtils.java +7 −42 Original line number Original line Diff line number Diff line Loading @@ -18,6 +18,7 @@ package android.text; import android.annotation.Nullable; import android.annotation.Nullable; import android.content.res.Resources; import android.content.res.Resources; import android.icu.util.ULocale; import android.os.Parcel; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable; import android.os.SystemProperties; import android.os.SystemProperties; Loading Loading @@ -1754,47 +1755,14 @@ public class TextUtils { * Be careful: this code will need to be updated when vertical scripts will be supported * Be careful: this code will need to be updated when vertical scripts will be supported */ */ public static int getLayoutDirectionFromLocale(Locale locale) { public static int getLayoutDirectionFromLocale(Locale locale) { if (locale != null && !locale.equals(Locale.ROOT)) { return ((locale != null && !locale.equals(Locale.ROOT) final String scriptSubtag = ICU.addLikelySubtags(locale).getScript(); && ULocale.forLocale(locale).isRightToLeft()) if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale); // If forcing into RTL layout mode, return RTL as default || SystemProperties.getBoolean(Settings.Global.DEVELOPMENT_FORCE_RTL, false)) if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) || scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) { return View.LAYOUT_DIRECTION_RTL; } } // If forcing into RTL layout mode, return RTL as default, else LTR return SystemProperties.getBoolean(Settings.Global.DEVELOPMENT_FORCE_RTL, false) ? View.LAYOUT_DIRECTION_RTL ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR; : View.LAYOUT_DIRECTION_LTR; } } /** * 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 View#LAYOUT_DIRECTION_LTR} or * {@link View#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical scripts will be supported * * @hide */ private static int getLayoutDirectionFromFirstChar(Locale locale) { switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) { case Character.DIRECTIONALITY_RIGHT_TO_LEFT: case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: return View.LAYOUT_DIRECTION_RTL; case Character.DIRECTIONALITY_LEFT_TO_RIGHT: default: return View.LAYOUT_DIRECTION_LTR; } } /** /** * Return localized string representing the given number of selected items. * Return localized string representing the given number of selected items. * * Loading @@ -1811,7 +1779,4 @@ public class TextUtils { private static String[] EMPTY_STRING_ARRAY = new String[]{}; private static String[] EMPTY_STRING_ARRAY = new String[]{}; private static final char ZWNBS_CHAR = '\uFEFF'; private static final char ZWNBS_CHAR = '\uFEFF'; private static String ARAB_SCRIPT_SUBTAG = "Arab"; private static String HEBR_SCRIPT_SUBTAG = "Hebr"; } } core/tests/coretests/src/android/text/TextUtilsTest.java +44 −0 Original line number Original line Diff line number Diff line Loading @@ -25,9 +25,11 @@ import android.test.suitebuilder.annotation.SmallTest; import android.text.style.StyleSpan; import android.text.style.StyleSpan; import android.text.util.Rfc822Token; import android.text.util.Rfc822Token; import android.text.util.Rfc822Tokenizer; import android.text.util.Rfc822Tokenizer; import android.view.View; import java.util.ArrayList; import java.util.ArrayList; import java.util.List; import java.util.List; import java.util.Locale; import junit.framework.TestCase; import junit.framework.TestCase; Loading Loading @@ -519,4 +521,46 @@ public class TextUtilsTest extends TestCase { return 0; return 0; } } } } @SmallTest public void testGetLayoutDirectionFromLocale() { assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(null)); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.ROOT)); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("en"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("en-US"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-AZ"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-Latn"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("en-EG"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("ar-Latn"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("ar"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("fa"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("he"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("iw"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("ur"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("dv"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-Arab"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-IR"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("fa-US"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("tr-Arab"))); } } } Loading
core/java/android/text/TextUtils.java +7 −42 Original line number Original line Diff line number Diff line Loading @@ -18,6 +18,7 @@ package android.text; import android.annotation.Nullable; import android.annotation.Nullable; import android.content.res.Resources; import android.content.res.Resources; import android.icu.util.ULocale; import android.os.Parcel; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable; import android.os.SystemProperties; import android.os.SystemProperties; Loading Loading @@ -1754,47 +1755,14 @@ public class TextUtils { * Be careful: this code will need to be updated when vertical scripts will be supported * Be careful: this code will need to be updated when vertical scripts will be supported */ */ public static int getLayoutDirectionFromLocale(Locale locale) { public static int getLayoutDirectionFromLocale(Locale locale) { if (locale != null && !locale.equals(Locale.ROOT)) { return ((locale != null && !locale.equals(Locale.ROOT) final String scriptSubtag = ICU.addLikelySubtags(locale).getScript(); && ULocale.forLocale(locale).isRightToLeft()) if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale); // If forcing into RTL layout mode, return RTL as default || SystemProperties.getBoolean(Settings.Global.DEVELOPMENT_FORCE_RTL, false)) if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) || scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) { return View.LAYOUT_DIRECTION_RTL; } } // If forcing into RTL layout mode, return RTL as default, else LTR return SystemProperties.getBoolean(Settings.Global.DEVELOPMENT_FORCE_RTL, false) ? View.LAYOUT_DIRECTION_RTL ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR; : View.LAYOUT_DIRECTION_LTR; } } /** * 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 View#LAYOUT_DIRECTION_LTR} or * {@link View#LAYOUT_DIRECTION_RTL}. * * Be careful: this code will need to be updated when vertical scripts will be supported * * @hide */ private static int getLayoutDirectionFromFirstChar(Locale locale) { switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) { case Character.DIRECTIONALITY_RIGHT_TO_LEFT: case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: return View.LAYOUT_DIRECTION_RTL; case Character.DIRECTIONALITY_LEFT_TO_RIGHT: default: return View.LAYOUT_DIRECTION_LTR; } } /** /** * Return localized string representing the given number of selected items. * Return localized string representing the given number of selected items. * * Loading @@ -1811,7 +1779,4 @@ public class TextUtils { private static String[] EMPTY_STRING_ARRAY = new String[]{}; private static String[] EMPTY_STRING_ARRAY = new String[]{}; private static final char ZWNBS_CHAR = '\uFEFF'; private static final char ZWNBS_CHAR = '\uFEFF'; private static String ARAB_SCRIPT_SUBTAG = "Arab"; private static String HEBR_SCRIPT_SUBTAG = "Hebr"; } }
core/tests/coretests/src/android/text/TextUtilsTest.java +44 −0 Original line number Original line Diff line number Diff line Loading @@ -25,9 +25,11 @@ import android.test.suitebuilder.annotation.SmallTest; import android.text.style.StyleSpan; import android.text.style.StyleSpan; import android.text.util.Rfc822Token; import android.text.util.Rfc822Token; import android.text.util.Rfc822Tokenizer; import android.text.util.Rfc822Tokenizer; import android.view.View; import java.util.ArrayList; import java.util.ArrayList; import java.util.List; import java.util.List; import java.util.Locale; import junit.framework.TestCase; import junit.framework.TestCase; Loading Loading @@ -519,4 +521,46 @@ public class TextUtilsTest extends TestCase { return 0; return 0; } } } } @SmallTest public void testGetLayoutDirectionFromLocale() { assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(null)); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.ROOT)); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("en"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("en-US"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-AZ"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-Latn"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("en-EG"))); assertEquals(View.LAYOUT_DIRECTION_LTR, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("ar-Latn"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("ar"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("fa"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("he"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("iw"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("ur"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("dv"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-Arab"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("az-IR"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("fa-US"))); assertEquals(View.LAYOUT_DIRECTION_RTL, TextUtils.getLayoutDirectionFromLocale(Locale.forLanguageTag("tr-Arab"))); } } }