Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20586fa0 authored by Fabrice Di Meglio's avatar Fabrice Di Meglio Committed by Android (Google) Code Review
Browse files

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

parents 642258a2 d3d9f3f1
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -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);
+2 −2
Original line number Diff line number Diff line
@@ -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);
    }
+1 −2
Original line number Diff line number Diff line
@@ -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);
        }

+57 −0
Original line number Diff line number Diff line
@@ -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";
}
+0 −84
Original line number Diff line number Diff line
/*
 * 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;
        }
    }
}
Loading