From 26e432d25f2ba199ae8b762fc68da8463389dd9b Mon Sep 17 00:00:00 2001 From: Fabrice Di Meglio Date: Fri, 10 Jun 2011 14:19:18 -0700 Subject: [PATCH] Make View respect LAYOUT_DIRECTION_LOCALE - update also unit tests for taking care of the locale direction - code formatting on the layout test files Change-Id: I4037eac3c572de9abb0178f36ca03803cc2c1522 --- core/java/android/view/View.java | 31 ++- tests/BiDiTests/Android | 0 tests/BiDiTests/AndroidManifest.xml | 28 ++ tests/BiDiTests/res/layout/basic.xml | 42 +-- tests/BiDiTests/res/layout/canvas.xml | 22 +- .../res/layout/frame_layout_locale.xml | 88 ++++++ .../BiDiTests/res/layout/frame_layout_ltr.xml | 46 ++-- .../BiDiTests/res/layout/frame_layout_rtl.xml | 46 ++-- .../res/layout/linear_layout_locale.xml | 257 +++++++++++++++++ .../res/layout/linear_layout_ltr.xml | 219 +++++++++------ .../res/layout/linear_layout_rtl.xml | 219 +++++++++------ tests/BiDiTests/res/layout/main.xml | 32 +-- .../res/layout/relative_layout_2_locale.xml | 183 +++++++++++++ .../res/layout/relative_layout_2_ltr.xml | 183 +++++++++++++ .../res/layout/relative_layout_2_rtl.xml | 183 +++++++++++++ .../res/layout/relative_layout_ltr.xml | 46 ++-- .../res/layout/relative_layout_ltr_2.xml | 155 ----------- .../res/layout/relative_layout_rtl.xml | 46 ++-- .../res/layout/relative_layout_rtl_2.xml | 155 ----------- .../res/layout/table_layout_locale.xml | 258 ++++++++++++++++++ .../BiDiTests/res/layout/table_layout_ltr.xml | 137 ++++++---- .../BiDiTests/res/layout/table_layout_rtl.xml | 137 ++++++---- .../com/android/bidi/BiDiTestActivity.java | 20 ++ .../BiDiTestFrameLayoutLocaleActivity.java | 31 +++ .../BiDiTestLinearLayoutLocaleActivity.java | 30 ++ ...BiDiTestRelativeLayoutLocaleActivity2.java | 31 +++ .../BiDiTestRelativeLayoutLtrActivity2.java | 2 +- .../BiDiTestRelativeLayoutRtlActivity2.java | 2 +- .../BiDiTestTableLayoutLocaleActivity.java | 31 +++ 29 files changed, 1939 insertions(+), 721 deletions(-) create mode 100644 tests/BiDiTests/Android create mode 100644 tests/BiDiTests/res/layout/frame_layout_locale.xml create mode 100644 tests/BiDiTests/res/layout/linear_layout_locale.xml create mode 100644 tests/BiDiTests/res/layout/relative_layout_2_locale.xml create mode 100644 tests/BiDiTests/res/layout/relative_layout_2_ltr.xml create mode 100644 tests/BiDiTests/res/layout/relative_layout_2_rtl.xml delete mode 100644 tests/BiDiTests/res/layout/relative_layout_ltr_2.xml delete mode 100644 tests/BiDiTests/res/layout/relative_layout_rtl_2.xml create mode 100644 tests/BiDiTests/res/layout/table_layout_locale.xml create mode 100644 tests/BiDiTests/src/com/android/bidi/BiDiTestFrameLayoutLocaleActivity.java create mode 100644 tests/BiDiTests/src/com/android/bidi/BiDiTestLinearLayoutLocaleActivity.java create mode 100644 tests/BiDiTests/src/com/android/bidi/BiDiTestRelativeLayoutLocaleActivity2.java create mode 100644 tests/BiDiTests/src/com/android/bidi/BiDiTestTableLayoutLocaleActivity.java diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 30ac3f77bf78d..441cdc14db8a2 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -76,6 +76,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Locale; import java.util.WeakHashMap; import java.util.concurrent.CopyOnWriteArrayList; @@ -8665,7 +8666,7 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit /** * Resolving the layout direction. LTR is set initially. - * We are supposing here that the parent directionality will be resolved before its children + * We are supposing here that the parent directionality will be resolved before its children. */ private void resolveLayoutDirection() { mPrivateFlags2 &= ~RESOLVED_LAYOUT_RTL; @@ -8680,6 +8681,34 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit case LAYOUT_DIRECTION_RTL: mPrivateFlags2 |= RESOLVED_LAYOUT_RTL; break; + case LAYOUT_DIRECTION_LOCALE: + if(isLayoutDirectionRtl(Locale.getDefault())) { + mPrivateFlags2 |= RESOLVED_LAYOUT_RTL; + } + break; + default: + // Nothing to do, LTR by default + } + } + + /** + * Check if a Locale is corresponding to a RTL script. + * + * @param locale Locale to check + * @return true if a Locale is corresponding to a RTL script. + */ + private static boolean isLayoutDirectionRtl(Locale locale) { + if (locale == null || locale.equals(Locale.ROOT)) return false; + // 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 false; + case Character.DIRECTIONALITY_RIGHT_TO_LEFT: + case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: + return true; + default: + return false; } } diff --git a/tests/BiDiTests/Android b/tests/BiDiTests/Android new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tests/BiDiTests/AndroidManifest.xml b/tests/BiDiTests/AndroidManifest.xml index ad27a62bc1f00..135c5dde0b589 100644 --- a/tests/BiDiTests/AndroidManifest.xml +++ b/tests/BiDiTests/AndroidManifest.xml @@ -57,6 +57,13 @@ + + + + + + @@ -71,6 +78,13 @@ + + + + + + @@ -99,6 +113,13 @@ + + + + + + @@ -113,6 +134,13 @@ + + + + + + diff --git a/tests/BiDiTests/res/layout/basic.xml b/tests/BiDiTests/res/layout/basic.xml index f254e3c4d40fe..d5f5ba7ff72c5 100644 --- a/tests/BiDiTests/res/layout/basic.xml +++ b/tests/BiDiTests/res/layout/basic.xml @@ -15,34 +15,34 @@ --> + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"> + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="wrap_content"> -