Skip to content

Commit 20586fa

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Fix bug #7173351 API REVIEW: android.util.LocaleUtil" into jb-mr1-dev
2 parents 642258a + d3d9f3f commit 20586fa

File tree

7 files changed

+65
-96
lines changed

7 files changed

+65
-96
lines changed

api/current.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22125,6 +22125,7 @@ package android.text {
2212522125
method public static java.lang.CharSequence expandTemplate(java.lang.CharSequence, java.lang.CharSequence...);
2212622126
method public static int getCapsMode(java.lang.CharSequence, int, int);
2212722127
method public static void getChars(java.lang.CharSequence, int, int, char[], int);
22128+
method public static int getLayoutDirectionFromLocale(java.util.Locale);
2212822129
method public static int getOffsetAfter(java.lang.CharSequence, int);
2212922130
method public static int getOffsetBefore(java.lang.CharSequence, int);
2213022131
method public static java.lang.CharSequence getReverse(java.lang.CharSequence, int, int);
@@ -23220,10 +23221,6 @@ package android.util {
2322023221
method public android.util.JsonWriter value(java.lang.Number) throws java.io.IOException;
2322123222
}
2322223223

23223-
public class LocaleUtil {
23224-
method public static int getLayoutDirectionFromLocale(java.util.Locale);
23225-
}
23226-
2322723224
public final class Log {
2322823225
method public static int d(java.lang.String, java.lang.String);
2322923226
method public static int d(java.lang.String, java.lang.String, java.lang.Throwable);

core/java/android/content/res/Configuration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import android.content.pm.ActivityInfo;
2020
import android.os.Parcel;
2121
import android.os.Parcelable;
22-
import android.util.LocaleUtil;
22+
import android.text.TextUtils;
2323
import android.view.View;
2424

2525
import java.util.Locale;
@@ -1169,7 +1169,7 @@ public int getLayoutDirection() {
11691169
public void setLayoutDirection(Locale locale) {
11701170
// There is a "1" difference between the configuration values for
11711171
// layout direction and View constants for layout direction, just add "1".
1172-
final int layoutDirection = 1 + LocaleUtil.getLayoutDirectionFromLocale(locale);
1172+
final int layoutDirection = 1 + TextUtils.getLayoutDirectionFromLocale(locale);
11731173
screenLayout = (screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK)|
11741174
(layoutDirection << SCREENLAYOUT_LAYOUTDIR_SHIFT);
11751175
}

core/java/android/text/TextDirectionHeuristics.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package android.text;
1818

1919

20-
import android.util.LocaleUtil;
2120
import android.view.View;
2221

2322
/**
@@ -242,7 +241,7 @@ public TextDirectionHeuristicLocale() {
242241

243242
@Override
244243
protected boolean defaultIsRtl() {
245-
final int dir = LocaleUtil.getLayoutDirectionFromLocale(java.util.Locale.getDefault());
244+
final int dir = TextUtils.getLayoutDirectionFromLocale(java.util.Locale.getDefault());
246245
return (dir == View.LAYOUT_DIRECTION_RTL);
247246
}
248247

core/java/android/text/TextUtils.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@
4646
import android.text.style.UnderlineSpan;
4747
import android.util.Printer;
4848

49+
import android.view.View;
4950
import com.android.internal.R;
5051
import com.android.internal.util.ArrayUtils;
52+
import libcore.icu.ICU;
5153

5254
import java.lang.reflect.Array;
5355
import java.util.Iterator;
56+
import java.util.Locale;
5457
import java.util.regex.Pattern;
5558

5659
public class TextUtils {
@@ -1706,10 +1709,64 @@ public static int unpackRangeEndFromLong(long range) {
17061709
return (int) (range & 0x00000000FFFFFFFFL);
17071710
}
17081711

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+
17091762
private static Object sLock = new Object();
1763+
17101764
private static char[] sTemp = null;
17111765

17121766
private static String[] EMPTY_STRING_ARRAY = new String[]{};
17131767

17141768
private static final char ZWNBS_CHAR = '\uFEFF';
1769+
1770+
private static String ARAB_SCRIPT_SUBTAG = "Arab";
1771+
private static String HEBR_SCRIPT_SUBTAG = "Hebr";
17151772
}

core/java/android/util/LocaleUtil.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

core/java/android/view/View.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
import android.os.RemoteException;
4949
import android.os.SystemClock;
5050
import android.os.SystemProperties;
51+
import android.text.TextUtils;
5152
import android.util.AttributeSet;
5253
import android.util.FloatProperty;
53-
import android.util.LocaleUtil;
5454
import android.util.Log;
5555
import android.util.Pool;
5656
import android.util.Poolable;
@@ -11655,7 +11655,7 @@ public void onResolvedLayoutDirectionReset() {
1165511655
* @return true if the Locale uses an RTL script.
1165611656
*/
1165711657
protected static boolean isLayoutDirectionRtl(Locale locale) {
11658-
return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
11658+
return (LAYOUT_DIRECTION_RTL == TextUtils.getLayoutDirectionFromLocale(locale));
1165911659
}
1166011660

1166111661
/**

core/java/android/widget/ListPopupWindow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import android.graphics.Rect;
2222
import android.graphics.drawable.Drawable;
2323
import android.os.Handler;
24+
import android.text.TextUtils;
2425
import android.util.AttributeSet;
25-
import android.util.LocaleUtil;
2626
import android.util.Log;
2727
import android.view.KeyEvent;
2828
import android.view.MotionEvent;
@@ -200,7 +200,7 @@ public ListPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, in
200200
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
201201
// Set the default layout direction to match the default locale one
202202
final Locale locale = mContext.getResources().getConfiguration().locale;
203-
mLayoutDirection = LocaleUtil.getLayoutDirectionFromLocale(locale);
203+
mLayoutDirection = TextUtils.getLayoutDirectionFromLocale(locale);
204204
}
205205

206206
/**

0 commit comments

Comments
 (0)