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

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

Merge "Add Configuration.getLayoutDirectionFromLocale()"

parents b924ab5b 7194efd3
Loading
Loading
Loading
Loading
+53 −0
Original line number Original line Diff line number Diff line
@@ -269,6 +269,26 @@ public final class Configuration implements Parcelable, Comparable<Configuration
     */
     */
    public int smallestScreenWidthDp;
    public int smallestScreenWidthDp;


    /**
     * @hide
     */
    public static final int LAYOUT_DIRECTION_UNDEFINED = -1;

    /**
     * @hide
     */
    public static final int LAYOUT_DIRECTION_LTR = 0;

    /**
     * @hide
     */
    public static final int LAYOUT_DIRECTION_RTL = 1;

    /**
     * @hide The layout direction associated to the current Locale
     */
    public int layoutDirection;

    /**
    /**
     * @hide Internal book-keeping.
     * @hide Internal book-keeping.
     */
     */
@@ -295,6 +315,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        mnc = o.mnc;
        mnc = o.mnc;
        if (o.locale != null) {
        if (o.locale != null) {
            locale = (Locale) o.locale.clone();
            locale = (Locale) o.locale.clone();
            layoutDirection = o.layoutDirection;
        }
        }
        userSetLocale = o.userSetLocale;
        userSetLocale = o.userSetLocale;
        touchscreen = o.touchscreen;
        touchscreen = o.touchscreen;
@@ -419,6 +440,11 @@ public final class Configuration implements Parcelable, Comparable<Configuration
            case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
            case NAVIGATIONHIDDEN_YES: sb.append("/h"); break;
            default: sb.append("/"); sb.append(navigationHidden); break;
            default: sb.append("/"); sb.append(navigationHidden); break;
        }
        }
        switch (layoutDirection) {
            case LAYOUT_DIRECTION_UNDEFINED: sb.append(" ?layoutdir"); break;
            case LAYOUT_DIRECTION_LTR: sb.append(" ltr"); break;
            case LAYOUT_DIRECTION_RTL: sb.append(" rtl"); break;
        }
        if (seq != 0) {
        if (seq != 0) {
            sb.append(" s.");
            sb.append(" s.");
            sb.append(seq);
            sb.append(seq);
@@ -448,6 +474,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
        screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED;
        smallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
        smallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
        seq = 0;
        seq = 0;
        layoutDirection = LAYOUT_DIRECTION_LTR;
    }
    }


    /** {@hide} */
    /** {@hide} */
@@ -482,6 +509,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
            changed |= ActivityInfo.CONFIG_LOCALE;
            changed |= ActivityInfo.CONFIG_LOCALE;
            locale = delta.locale != null
            locale = delta.locale != null
                    ? (Locale) delta.locale.clone() : null;
                    ? (Locale) delta.locale.clone() : null;
            layoutDirection = getLayoutDirectionFromLocale(locale);
        }
        }
        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
        if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0)))
        {
        {
@@ -563,6 +591,29 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        return changed;
        return changed;
    }
    }


    /**
     * 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 #LAYOUT_DIRECTION_UNDEFINED},
     * {@link #LAYOUT_DIRECTION_LTR} or {@link #LAYOUT_DIRECTION_RTL}.
     *
     * @hide
     */
    public static int getLayoutDirectionFromLocale(Locale locale) {
        if (locale == null || locale.equals(Locale.ROOT)) return LAYOUT_DIRECTION_UNDEFINED;
        // Be careful: this code will need to be changed when vertical scripts will be supported
        // OR if ICU4C is updated to have the "likelySubtags" file
        switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
            case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
                return LAYOUT_DIRECTION_LTR;
            case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
            case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
                return LAYOUT_DIRECTION_RTL;
            default:
                return LAYOUT_DIRECTION_UNDEFINED;
        }
    }

    /**
    /**
     * Return a bit mask of the differences between this Configuration
     * Return a bit mask of the differences between this Configuration
     * object and the given one.  Does not change the values of either.  Any
     * object and the given one.  Does not change the values of either.  Any
@@ -739,6 +790,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        dest.writeInt(screenWidthDp);
        dest.writeInt(screenWidthDp);
        dest.writeInt(screenHeightDp);
        dest.writeInt(screenHeightDp);
        dest.writeInt(smallestScreenWidthDp);
        dest.writeInt(smallestScreenWidthDp);
        dest.writeInt(layoutDirection);
        dest.writeInt(seq);
        dest.writeInt(seq);
    }
    }


@@ -763,6 +815,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration
        screenWidthDp = source.readInt();
        screenWidthDp = source.readInt();
        screenHeightDp = source.readInt();
        screenHeightDp = source.readInt();
        smallestScreenWidthDp = source.readInt();
        smallestScreenWidthDp = source.readInt();
        layoutDirection = source.readInt();
        seq = source.readInt();
        seq = source.readInt();
    }
    }
    
    
+198 −0
Original line number Original line 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.content.res;

import java.util.Locale;

import android.test.AndroidTestCase;
import dalvik.annotation.TestLevel;
import dalvik.annotation.TestTargetNew;

public class ConfigurationTest extends AndroidTestCase {

    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "getLayoutDirectionFromLocale",
        args = {Locale.class}
    )
    public void testGetLayoutDirectionFromLocale() {
        assertEquals(Configuration.LAYOUT_DIRECTION_UNDEFINED,
            Configuration.getLayoutDirectionFromLocale(null));

        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.ENGLISH));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.CANADA));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.CANADA_FRENCH));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.FRANCE));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.FRENCH));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.GERMAN));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.GERMANY));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.ITALIAN));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.ITALY));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.UK));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.US));

        assertEquals(Configuration.LAYOUT_DIRECTION_UNDEFINED,
            Configuration.getLayoutDirectionFromLocale(Locale.ROOT));

        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.CHINA));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.CHINESE));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.JAPAN));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.JAPANESE));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.KOREA));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.KOREAN));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.PRC));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.SIMPLIFIED_CHINESE));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.TAIWAN));
        assertEquals(Configuration.LAYOUT_DIRECTION_LTR,
            Configuration.getLayoutDirectionFromLocale(Locale.TRADITIONAL_CHINESE));

        Locale locale = new Locale("ar");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "AE");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "BH");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "DZ");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "EG");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "IQ");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "JO");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "KW");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "LB");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "LY");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "MA");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "OM");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "QA");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "SA");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "SD");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "SY");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "TN");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ar", "YE");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));

        locale = new Locale("fa");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("fa", "AF");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("fa", "IR");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));

        locale = new Locale("iw");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("iw", "IL");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("he");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("he", "IL");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));

        // The following test will not pass until we are able to take care about the scrip subtag
        // thru having the "likelySubTags" file into ICU4C
//        locale = new Locale("pa_Arab");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));
//        locale = new Locale("pa_Arab", "PK");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));

        locale = new Locale("ps");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));
        locale = new Locale("ps", "AF");
        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
            Configuration.getLayoutDirectionFromLocale(locale));

        // The following test will not work as the localized display name would be "Urdu" with ICU 4.4
        // We will need ICU 4.6 to get the correct localized display name
//        locale = new Locale("ur");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));
//        locale = new Locale("ur", "IN");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));
//        locale = new Locale("ur", "PK");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));

        // The following test will not pass until we are able to take care about the scrip subtag
        // thru having the "likelySubTags" file into ICU4C
//        locale = new Locale("uz_Arab");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));
//        locale = new Locale("uz_Arab", "AF");
//        assertEquals(Configuration.LAYOUT_DIRECTION_RTL,
//            Configuration.getLayoutDirectionFromLocale(locale));
    }
}