Fix bug #5175022 Easy mode in MeasuredText.setPara() is no more used

- use the real TextDirectionHeuristics constants
- make more formal that LayoutDirection should be resolved BEFORE TextDirection
- reset TextDirection if LayoutDirection is recomputed
- remove non used TextDirectionHeuristic.isRtl(CharSequence...)

Change-Id: I1d53a7e3ee324cfd27785bb4d715d93de8d9ff4d
diff --git a/core/java/android/text/BoringLayout.java b/core/java/android/text/BoringLayout.java
index 5a244f1..77cd71e 100644
--- a/core/java/android/text/BoringLayout.java
+++ b/core/java/android/text/BoringLayout.java
@@ -280,7 +280,7 @@
                 }
             }
 
-            if (textDir.isRtl(temp, 0, n)) {
+            if (textDir != null && textDir.isRtl(temp, 0, n)) {
                boring = false;
                break outer;
             }
diff --git a/core/java/android/text/TextDirectionHeuristic.java b/core/java/android/text/TextDirectionHeuristic.java
index 130f879..0bf64e4 100644
--- a/core/java/android/text/TextDirectionHeuristic.java
+++ b/core/java/android/text/TextDirectionHeuristic.java
@@ -1,4 +1,18 @@
-// Copyright 2011 Google Inc. All Rights Reserved.
+/*
+ * 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.text;
 
@@ -8,6 +22,5 @@
  * @hide
  */
 public interface TextDirectionHeuristic {
-    /** @hide */ boolean isRtl(CharSequence text, int start, int end);
     /** @hide */ boolean isRtl(char[] text, int start, int count);
 }
diff --git a/core/java/android/text/TextDirectionHeuristics.java b/core/java/android/text/TextDirectionHeuristics.java
index 5f9ffc5..5ed2df4 100644
--- a/core/java/android/text/TextDirectionHeuristics.java
+++ b/core/java/android/text/TextDirectionHeuristics.java
@@ -1,4 +1,18 @@
-// Copyright 2011 Google Inc. All Rights Reserved.
+/*
+ * 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.text;
 
@@ -36,12 +50,12 @@
         new TextDirectionHeuristicInternal(FirstStrong.INSTANCE, true);
 
     /**
-     * If the text contains any strong right to left non-format character, determines
-     * that the direction is right to left, falling back to left to right if it
+     * If the text contains any strong left to right non-format character, determines
+     * that the direction is left to right, falling back to left to right if it
      * finds none.
      */
-    public static final TextDirectionHeuristic ANYRTL_LTR =
-        new TextDirectionHeuristicInternal(AnyStrong.INSTANCE_RTL, false);
+    public static final TextDirectionHeuristic ANYLTR_LTR =
+        new TextDirectionHeuristicInternal(AnyStrong.INSTANCE_LTR, false);
 
     /**
      * If the text contains any strong left to right non-format character, determines
@@ -52,6 +66,22 @@
         new TextDirectionHeuristicInternal(AnyStrong.INSTANCE_LTR, true);
 
     /**
+     * If the text contains any strong right to left non-format character, determines
+     * that the direction is right to left, falling back to left to right if it
+     * finds none.
+     */
+    public static final TextDirectionHeuristic ANYRTL_LTR =
+        new TextDirectionHeuristicInternal(AnyStrong.INSTANCE_RTL, false);
+
+    /**
+     * If the text contains any strong right to left non-format character, determines
+     * that the direction is right to left, falling back to right to left if it
+     * finds none.
+     */
+    public static final TextDirectionHeuristic ANYRTL_RTL =
+        new TextDirectionHeuristicInternal(AnyStrong.INSTANCE_RTL, true);
+
+    /**
      * Examines only the strong directional non-format characters, and if either
      * left to right or right to left characters are 60% or more of this total,
      * determines that the direction follows the majority of characters.  Falls
@@ -92,19 +122,6 @@
         abstract protected boolean defaultIsRtl();
 
         @Override
-        public boolean isRtl(CharSequence text, int start, int end) {
-            if (text == null || start < 0 || end < start || text.length() < end) {
-                throw new IllegalArgumentException();
-            }
-            if (mAlgorithm == null) {
-                return defaultIsRtl();
-            }
-            text = text.subSequence(start, end);
-            char[] chars = text.toString().toCharArray();
-            return doCheck(chars, 0, chars.length);
-        }
-
-        @Override
         public boolean isRtl(char[] chars, int start, int count) {
             if (chars == null || start < 0 || count < 0 || chars.length - count < start) {
                 throw new IllegalArgumentException();
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 54bd637..03f5826 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -9159,6 +9159,7 @@
             mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
         }
         jumpDrawablesToCurrentState();
+        // Order is important here: LayoutDirection should be resolved before Padding and TextDirection
         resolveLayoutDirectionIfNeeded();
         resolvePadding();
         resolveTextDirection();
@@ -9179,6 +9180,10 @@
         // Clear any previous layout direction resolution
         mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
 
+        // Reset also TextDirection as a change into LayoutDirection may impact the selected
+        // TextDirectionHeuristic
+        resetResolvedTextDirection();
+
         // Set resolved depending on layout direction
         switch (getLayoutDirection()) {
             case LAYOUT_DIRECTION_INHERIT:
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 1ab1a87..f1832c9 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -10699,42 +10699,29 @@
         return mInBatchEditControllers;
     }
 
-    private class TextViewDirectionHeuristic extends TextDirectionHeuristicImpl {
-        private TextViewDirectionHeuristic(TextDirectionAlgorithm algorithm) {
-            super(algorithm);
-        }
-        @Override
-        protected boolean defaultIsRtl() {
-            return getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL;
-        }
-    }
-
-    /**
-     * Resolve the text direction.
-     *
-     * Text direction of paragraphs in a TextView is determined using a heuristic. If the correct
-     * text direction cannot be determined by the heuristic, the view's resolved layout direction
-     * determines the direction.
-     *
-     * This heuristic and result is applied individually to each paragraph in a TextView, based on
-     * the text and style content of that paragraph. Paragraph text styles can also be used to force
-     * a particular direction.
-     */
     @Override
     protected void resolveTextDirection() {
+        // Always need to resolve layout direction first
+        final boolean defaultIsRtl = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
+
+        // Then resolve text direction on the parent
         super.resolveTextDirection();
 
+        // Now, we can select the heuristic
         int textDir = getResolvedTextDirection();
         switch (textDir) {
             default:
             case TEXT_DIRECTION_FIRST_STRONG:
-                mTextDir = new TextViewDirectionHeuristic(FirstStrong.INSTANCE);
+                mTextDir = (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL :
+                        TextDirectionHeuristics.FIRSTSTRONG_LTR);
                 break;
             case TEXT_DIRECTION_ANY_RTL:
-                mTextDir = new TextViewDirectionHeuristic(AnyStrong.INSTANCE_RTL);
+                mTextDir = (defaultIsRtl ? TextDirectionHeuristics.ANYRTL_RTL:
+                        TextDirectionHeuristics.ANYRTL_LTR);
                 break;
             case TEXT_DIRECTION_CHAR_COUNT:
-                mTextDir = new TextViewDirectionHeuristic(CharCount.INSTANCE_DEFAULT);
+                mTextDir = (defaultIsRtl ? TextDirectionHeuristics.CHARCOUNT_RTL:
+                        TextDirectionHeuristics.CHARCOUNT_LTR);
                 break;
             case TEXT_DIRECTION_LTR:
                 mTextDir = TextDirectionHeuristics.LTR;
@@ -10743,7 +10730,6 @@
                 mTextDir = TextDirectionHeuristics.RTL;
                 break;
         }
-
     }
 
     /**