diff options
| author | 2011-06-09 11:05:25 -0700 | |
|---|---|---|
| committer | 2011-06-09 16:00:28 -0700 | |
| commit | 7194efd394ce0b38a74bab87206adfd9ff823742 (patch) | |
| tree | a6e3d90cb82a5891258ac07d5ed531093c211908 | |
| parent | c8307c885f8462f2baf9f06c9916d1521f7fdcfb (diff) | |
Add Configuration.getLayoutDirectionFromLocale()
- returns LAYOUT_DIRECTION_UNDEFINED / LAYOUT_DIRECTION_LTR / LAYOUT_DIRECTION_RTL depending on the Locale
- add unit tests
Change-Id: I4372734eb011cbf6270f39ba815e696b04f2352f
| -rw-r--r-- | core/java/android/content/res/Configuration.java | 53 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/content/res/ConfigurationTest.java | 198 |
2 files changed, 251 insertions, 0 deletions
diff --git a/core/java/android/content/res/Configuration.java b/core/java/android/content/res/Configuration.java index 6409aac441e7..f51d46e93f4f 100644 --- a/core/java/android/content/res/Configuration.java +++ b/core/java/android/content/res/Configuration.java @@ -270,6 +270,26 @@ public final class Configuration implements Parcelable, Comparable<Configuration 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. */ public int seq; @@ -295,6 +315,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration mnc = o.mnc; if (o.locale != null) { locale = (Locale) o.locale.clone(); + layoutDirection = o.layoutDirection; } userSetLocale = o.userSetLocale; touchscreen = o.touchscreen; @@ -419,6 +440,11 @@ public final class Configuration implements Parcelable, Comparable<Configuration case NAVIGATIONHIDDEN_YES: sb.append("/h"); 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) { sb.append(" s."); sb.append(seq); @@ -448,6 +474,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration screenHeightDp = SCREEN_HEIGHT_DP_UNDEFINED; smallestScreenWidthDp = SMALLEST_SCREEN_WIDTH_DP_UNDEFINED; seq = 0; + layoutDirection = LAYOUT_DIRECTION_LTR; } /** {@hide} */ @@ -482,6 +509,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration changed |= ActivityInfo.CONFIG_LOCALE; locale = delta.locale != null ? (Locale) delta.locale.clone() : null; + layoutDirection = getLayoutDirectionFromLocale(locale); } if (delta.userSetLocale && (!userSetLocale || ((changed & ActivityInfo.CONFIG_LOCALE) != 0))) { @@ -564,6 +592,29 @@ public final class Configuration implements Parcelable, Comparable<Configuration } /** + * 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 * object and the given one. Does not change the values of either. Any * undefined fields in <var>delta</var> are ignored. @@ -739,6 +790,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration dest.writeInt(screenWidthDp); dest.writeInt(screenHeightDp); dest.writeInt(smallestScreenWidthDp); + dest.writeInt(layoutDirection); dest.writeInt(seq); } @@ -763,6 +815,7 @@ public final class Configuration implements Parcelable, Comparable<Configuration screenWidthDp = source.readInt(); screenHeightDp = source.readInt(); smallestScreenWidthDp = source.readInt(); + layoutDirection = source.readInt(); seq = source.readInt(); } diff --git a/core/tests/coretests/src/android/content/res/ConfigurationTest.java b/core/tests/coretests/src/android/content/res/ConfigurationTest.java new file mode 100644 index 000000000000..f1f745ea9be9 --- /dev/null +++ b/core/tests/coretests/src/android/content/res/ConfigurationTest.java @@ -0,0 +1,198 @@ +/* + * 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)); + } +} |