From 7d3b8d69993ba3c253b76c6c0b17d9fabd978593 Mon Sep 17 00:00:00 2001 From: Sriram Ramasubramanian Date: Mon, 10 Jun 2013 11:17:34 -0700 Subject: [PATCH] Bug 880393: FadedTextView for about:home. [r=mfinkle] --- mobile/android/base/GeckoViewsFactory.java | 2 + mobile/android/base/Makefile.in | 1 + mobile/android/base/home/FadedTextView.java | 67 +++++++++++++++++++ .../resources/layout/two_line_page_row.xml | 13 ++-- .../base/resources/values-v16/styles.xml | 2 +- .../android/base/resources/values/attrs.xml | 4 ++ .../android/base/resources/values/styles.xml | 2 +- 7 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 mobile/android/base/home/FadedTextView.java diff --git a/mobile/android/base/GeckoViewsFactory.java b/mobile/android/base/GeckoViewsFactory.java index 3db52cf3235c..cf11ed82ae7c 100644 --- a/mobile/android/base/GeckoViewsFactory.java +++ b/mobile/android/base/GeckoViewsFactory.java @@ -6,6 +6,7 @@ package org.mozilla.gecko; import org.mozilla.gecko.gfx.LayerView; import org.mozilla.gecko.home.BookmarkFolderView; +import org.mozilla.gecko.home.FadedTextView; import org.mozilla.gecko.home.TwoLinePageRow; import org.mozilla.gecko.menu.MenuItemDefault; import org.mozilla.gecko.widget.AboutHomeView; @@ -90,6 +91,7 @@ public final class GeckoViewsFactory implements LayoutInflater.Factory { mFactoryMap.put("TextView", GeckoTextView.class.getConstructor(arg1Class, arg2Class)); mFactoryMap.put("FaviconView", FaviconView.class.getConstructor(arg1Class, arg2Class)); mFactoryMap.put("home.BookmarkFolderView", BookmarkFolderView.class.getConstructor(arg1Class, arg2Class)); + mFactoryMap.put("home.FadedTextView", FadedTextView.class.getConstructor(arg1Class, arg2Class)); mFactoryMap.put("home.TwoLinePageRow", TwoLinePageRow.class.getConstructor(arg1Class, arg2Class)); } catch (NoSuchMethodException nsme) { Log.e(LOGTAG, "Unable to initialize views factory", nsme); diff --git a/mobile/android/base/Makefile.in b/mobile/android/base/Makefile.in index 63c5de03dc2d..2a8612bc6221 100644 --- a/mobile/android/base/Makefile.in +++ b/mobile/android/base/Makefile.in @@ -221,6 +221,7 @@ FENNEC_JAVA_FILES = \ home/BookmarkFolderView.java \ home/HomePager.java \ home/HomePagerTabStrip.java \ + home/FadedTextView.java \ home/TwoLinePageRow.java \ menu/GeckoMenu.java \ menu/GeckoMenuInflater.java \ diff --git a/mobile/android/base/home/FadedTextView.java b/mobile/android/base/home/FadedTextView.java new file mode 100644 index 000000000000..dee9f5163d10 --- /dev/null +++ b/mobile/android/base/home/FadedTextView.java @@ -0,0 +1,67 @@ +/* -*- 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.home; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.LinearGradient; +import android.graphics.Shader; +import android.util.AttributeSet; +import android.widget.TextView; + +import org.mozilla.gecko.R; + +/** + * FadedTextView fades the ends of the text by fadeWidth amount, + * if the text is too long and requires an ellipsis. + */ +public class FadedTextView extends TextView { + + // Width of the fade effect from end of the view. + private int mFadeWidth; + + public FadedTextView(Context context) { + this(context, null); + } + + public FadedTextView(Context context, AttributeSet attrs) { + this(context, attrs, android.R.attr.textViewStyle); + } + + public FadedTextView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView); + mFadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0); + a.recycle(); + } + + /** + * {@inheritDoc} + */ + @Override + public void onDraw(Canvas canvas) { + int width = getMeasuredWidth(); + + // Layout doesn't return a proper width for getWidth(). + // Instead check the width of the first line, as we've restricted to just one line. + if (getLayout().getLineWidth(0) > width) { + int color = getCurrentTextColor(); + float stop = ((float) (width - mFadeWidth) / (float) width); + LinearGradient gradient = new LinearGradient(0, 0, width, 0, + new int[] { color, color, 0x0 }, + new float[] { 0, stop, 1.0f }, + Shader.TileMode.CLAMP); + getPaint().setShader(gradient); + } else { + getPaint().setShader(null); + } + + // Do a default draw. + super.onDraw(canvas); + } +} diff --git a/mobile/android/base/resources/layout/two_line_page_row.xml b/mobile/android/base/resources/layout/two_line_page_row.xml index 2c9f5731e6f1..54dd80e92416 100644 --- a/mobile/android/base/resources/layout/two_line_page_row.xml +++ b/mobile/android/base/resources/layout/two_line_page_row.xml @@ -3,7 +3,8 @@ - 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/. --> - + - + @style/TextAppearance.Widget.TwoLinePageRow.Title sans-serif-light true - middle + none diff --git a/mobile/android/base/resources/values/attrs.xml b/mobile/android/base/resources/values/attrs.xml index b72665054dfb..cf203cd5be32 100644 --- a/mobile/android/base/resources/values/attrs.xml +++ b/mobile/android/base/resources/values/attrs.xml @@ -168,6 +168,10 @@ + + + + diff --git a/mobile/android/base/resources/values/styles.xml b/mobile/android/base/resources/values/styles.xml index 3ac4c59809da..22b870490caa 100644 --- a/mobile/android/base/resources/values/styles.xml +++ b/mobile/android/base/resources/values/styles.xml @@ -76,7 +76,7 @@