am 20586fa0: Merge "Fix bug #7173351 API REVIEW: android.util.LocaleUtil" into jb-mr1-dev

* commit '20586fa0353f63453766140b32a4778793ce2b43':
  Fix bug #7173351 API REVIEW: android.util.LocaleUtil
This commit is contained in:
Fabrice Di Meglio
2012-09-18 14:54:23 -07:00
committed by Android Git Automerger
7 changed files with 65 additions and 96 deletions

View File

@@ -22125,6 +22125,7 @@ package android.text {
method public static java.lang.CharSequence expandTemplate(java.lang.CharSequence, java.lang.CharSequence...);
method public static int getCapsMode(java.lang.CharSequence, int, int);
method public static void getChars(java.lang.CharSequence, int, int, char[], int);
method public static int getLayoutDirectionFromLocale(java.util.Locale);
method public static int getOffsetAfter(java.lang.CharSequence, int);
method public static int getOffsetBefore(java.lang.CharSequence, int);
method public static java.lang.CharSequence getReverse(java.lang.CharSequence, int, int);
@@ -23220,10 +23221,6 @@ package android.util {
method public android.util.JsonWriter value(java.lang.Number) throws java.io.IOException;
}
public class LocaleUtil {
method public static int getLayoutDirectionFromLocale(java.util.Locale);
}
public final class Log {
method public static int d(java.lang.String, java.lang.String);
method public static int d(java.lang.String, java.lang.String, java.lang.Throwable);

View File

@@ -19,7 +19,7 @@ package android.content.res;
import android.content.pm.ActivityInfo;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.LocaleUtil;
import android.text.TextUtils;
import android.view.View;
import java.util.Locale;
@@ -1169,7 +1169,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
public void setLayoutDirection(Locale locale) {
// There is a "1" difference between the configuration values for
// layout direction and View constants for layout direction, just add "1".
final int layoutDirection = 1 + LocaleUtil.getLayoutDirectionFromLocale(locale);
final int layoutDirection = 1 + TextUtils.getLayoutDirectionFromLocale(locale);
screenLayout = (screenLayout&~SCREENLAYOUT_LAYOUTDIR_MASK)|
(layoutDirection << SCREENLAYOUT_LAYOUTDIR_SHIFT);
}

View File

@@ -17,7 +17,6 @@
package android.text;
import android.util.LocaleUtil;
import android.view.View;
/**
@@ -242,7 +241,7 @@ public class TextDirectionHeuristics {
@Override
protected boolean defaultIsRtl() {
final int dir = LocaleUtil.getLayoutDirectionFromLocale(java.util.Locale.getDefault());
final int dir = TextUtils.getLayoutDirectionFromLocale(java.util.Locale.getDefault());
return (dir == View.LAYOUT_DIRECTION_RTL);
}

View File

@@ -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";
}

View File

@@ -1,84 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.util;
import java.util.Locale;
import android.view.View;
import libcore.icu.ICU;
/**
* Various utilities for Locales
*
*/
public class LocaleUtil {
private LocaleUtil() { /* cannot be instantiated */ }
private static String ARAB_SCRIPT_SUBTAG = "Arab";
private static String HEBR_SCRIPT_SUBTAG = "Hebr";
/**
* 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 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
*/
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;
}
}
}

View File

@@ -48,9 +48,9 @@ import android.os.Parcelable;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.FloatProperty;
import android.util.LocaleUtil;
import android.util.Log;
import android.util.Pool;
import android.util.Poolable;
@@ -11655,7 +11655,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
* @return true if the Locale uses an RTL script.
*/
protected static boolean isLayoutDirectionRtl(Locale locale) {
return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
return (LAYOUT_DIRECTION_RTL == TextUtils.getLayoutDirectionFromLocale(locale));
}
/**

View File

@@ -21,8 +21,8 @@ import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.LocaleUtil;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
@@ -200,7 +200,7 @@ public class ListPopupWindow {
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
// Set the default layout direction to match the default locale one
final Locale locale = mContext.getResources().getConfiguration().locale;
mLayoutDirection = LocaleUtil.getLayoutDirectionFromLocale(locale);
mLayoutDirection = TextUtils.getLayoutDirectionFromLocale(locale);
}
/**