Bug 1332258 - Reset text fading gradient to null when text direction changed, r=sebastian draft
authormaliu <max@mxli.us>
Thu, 19 Jan 2017 15:09:14 +0800
changeset 463700 4a2dfc118cb2e3a67e1bef0f65180ee61a2f849b
parent 462769 80eac484366ad881c6a10bf81e8d9b8f7a676c75
child 542755 35c8ff020baadae68507cf65c9f93f08241e66de
push id42153
push userbmo:max@mxli.us
push dateThu, 19 Jan 2017 17:16:23 +0000
reviewerssebastian
bugs1332258
milestone53.0a1
Bug 1332258 - Reset text fading gradient to null when text direction changed, r=sebastian MozReview-Commit-ID: EaUUO8aegzj
mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
mobile/android/base/java/org/mozilla/gecko/widget/FadedSingleColorTextView.java
--- a/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
@@ -1,21 +1,23 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 package org.mozilla.gecko.util;
 
+import android.annotation.TargetApi;
 import android.content.res.TypedArray;
 import android.os.Build;
 import android.support.v4.text.TextUtilsCompat;
 import android.support.v4.view.MarginLayoutParamsCompat;
 import android.support.v4.view.ViewCompat;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.TextView;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.R;
 
 import java.util.Locale;
 
 public class ViewUtil {
 
@@ -67,9 +69,30 @@ public class ViewUtil {
                 ViewCompat.setLayoutDirection(view, ViewCompat.LAYOUT_DIRECTION_RTL);
                 break;
             case ViewCompat.LAYOUT_DIRECTION_LTR:
             default:
                 ViewCompat.setLayoutDirection(view, ViewCompat.LAYOUT_DIRECTION_LTR);
                 break;
         }
     }
+
+    /**
+     * RTL compatibility wrapper to force set TextDirection for JB mr1 and above
+     *
+     * @param textView
+     * @param isRtl
+     */
+    public static void setTextDirectionRtlCompat(TextView textView, boolean isRtl) {
+        if (AppConstants.Versions.feature17Plus) {
+            setTextDirectionRtlCompat17(textView, isRtl);
+        }
+    }
+
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
+    private static void setTextDirectionRtlCompat17(TextView textView, boolean isRtl) {
+        if (isRtl) {
+            textView.setTextDirection(View.TEXT_DIRECTION_RTL);
+        } else {
+            textView.setTextDirection(View.TEXT_DIRECTION_LTR);
+        }
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/widget/FadedSingleColorTextView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/FadedSingleColorTextView.java
@@ -5,19 +5,22 @@
 
 package org.mozilla.gecko.widget;
 
 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.LinearGradient;
 import android.graphics.Shader;
 import android.support.v4.text.BidiFormatter;
+import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.View;
 
+import org.mozilla.gecko.util.ViewUtil;
+
 /**
  * Fades the end of the text by gecko:fadeWidth amount,
  * if the text is too long and requires an ellipsis.
  *
  * This implementation is an improvement over Android's built-in fadingEdge
  * and the fastest of Fennec's implementations. However, it only works for
  * text of one color. It works by applying a linear gradient directly to the text.
  */
@@ -44,20 +47,24 @@ public class FadedSingleColorTextView ex
         }
 
         getPaint().setShader(needsEllipsis ? mTextGradient : null);
     }
 
     @Override
     public void setText(CharSequence text, BufferType type) {
         super.setText(text, type);
-        mIsTextDirectionRtl = BidiFormatter.getInstance().isRtl((String) text);
-        if (mIsTextDirectionRtl) {
-            setTextDirection(TEXT_DIRECTION_RTL);
+        final boolean previousTextDirectionRtl = mIsTextDirectionRtl;
+        if (!TextUtils.isEmpty(text)) {
+            mIsTextDirectionRtl = BidiFormatter.getInstance().isRtl((String) text);
         }
+        if (mIsTextDirectionRtl != previousTextDirectionRtl) {
+            mTextGradient = null;
+        }
+        ViewUtil.setTextDirectionRtlCompat(this, mIsTextDirectionRtl);
     }
 
     @Override
     public void onDraw(Canvas canvas) {
         updateGradientShader();
         super.onDraw(canvas);
     }