summaryrefslogtreecommitdiff
path: root/core/java/android/text/TextUtils.java
diff options
context:
space:
mode:
authorFabrice Di Meglio <fdimeglio@google.com>2012-09-18 12:55:32 -0700
committerFabrice Di Meglio <fdimeglio@google.com>2012-09-18 12:55:32 -0700
commitd3d9f3f1004dfee2649a26cfe8dba948cd364904 (patch)
tree089a87a8ecaf116fddfff6af95aadbb54e6203da /core/java/android/text/TextUtils.java
parent5e900e3a17a87e91a0d190bc1c96efc440eb026a (diff)
Fix bug #7173351 API REVIEW: android.util.LocaleUtil
Change-Id: I08fd491eff714059e9ec874fadebe7eb556c34d5
Diffstat (limited to 'core/java/android/text/TextUtils.java')
-rw-r--r--core/java/android/text/TextUtils.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java
index 987062ab08a2..1508d10ea6af 100644
--- a/core/java/android/text/TextUtils.java
+++ b/core/java/android/text/TextUtils.java
@@ -46,11 +46,14 @@ import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.Printer;
+import android.view.View;
import com.android.internal.R;
import com.android.internal.util.ArrayUtils;
+import libcore.icu.ICU;
import java.lang.reflect.Array;
import java.util.Iterator;
+import java.util.Locale;
import java.util.regex.Pattern;
public class TextUtils {
@@ -1706,10 +1709,64 @@ public class TextUtils {
return (int) (range & 0x00000000FFFFFFFFL);
}
+ /**
+ * 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 android.view.View#LAYOUT_DIRECTION_LTR} or
+ * {@link android.view.View#LAYOUT_DIRECTION_RTL}.
+ *
+ * Be careful: this code will need to be updated when vertical scripts will be supported
+ */
+ public static int getLayoutDirectionFromLocale(Locale locale) {
+ if (locale != null && !locale.equals(Locale.ROOT)) {
+ final String scriptSubtag = ICU.getScript(ICU.addLikelySubtags(locale.toString()));
+ if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);
+
+ if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||
+ scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
+ return View.LAYOUT_DIRECTION_RTL;
+ }
+ }
+
+ return 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;
+ }
+ }
+
private static Object sLock = new Object();
+
private static char[] sTemp = null;
private static String[] EMPTY_STRING_ARRAY = new String[]{};
private static final char ZWNBS_CHAR = '\uFEFF';
+
+ private static String ARAB_SCRIPT_SUBTAG = "Arab";
+ private static String HEBR_SCRIPT_SUBTAG = "Hebr";
}