From 380154724b513ff7649d93f03d67b0e481f0db02 Mon Sep 17 00:00:00 2001 From: Roozbeh Pournader Date: Tue, 4 Apr 2017 18:27:10 -0700 Subject: [PATCH] Force direction of phone number fields to (practically) LTR Previously, phone number fields defaulted to locale direction, which created problems for apps that were not aware of LTR requirements for phone numbers. Now, we look at the direction of the digits for the locale, and use that to determine the direction of the edit field. (For practically all major RTL locales, that direction is LTR.) Test: Manual Bug: 33643035 Change-Id: I17c70d8462bd403ea6866057971105f1f5772ba3 --- core/java/android/widget/TextView.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 7d9253b4db0a5..58da92f6bd19e 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -57,6 +57,7 @@ import android.graphics.RectF; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.graphics.fonts.FontVariationAxis; +import android.icu.text.DecimalFormatSymbols; import android.os.AsyncTask; import android.os.Bundle; import android.os.LocaleList; @@ -11058,6 +11059,26 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return TextDirectionHeuristics.LTR; } + if (mEditor != null + && (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) + == EditorInfo.TYPE_CLASS_PHONE) { + // Phone numbers must be in the direction of the locale's digits. Most locales have LTR + // digits, but some locales, such as those written in the Adlam or N'Ko scripts, have + // RTL digits. + final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(getTextLocale()); + final String zero = symbols.getDigitStrings()[0]; + // In case the zero digit is multi-codepoint, just use the first codepoint to determine + // direction. + final int firstCodepoint = zero.codePointAt(0); + final byte digitDirection = Character.getDirectionality(firstCodepoint); + if (digitDirection == Character.DIRECTIONALITY_RIGHT_TO_LEFT + || digitDirection == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) { + return TextDirectionHeuristics.RTL; + } else { + return TextDirectionHeuristics.LTR; + } + } + // Always need to resolve layout direction first final boolean defaultIsRtl = (getLayoutDirection() == LAYOUT_DIRECTION_RTL);