From 91645e30c4f1a0335256f2da11a71e727dde318c Mon Sep 17 00:00:00 2001 From: Theresa Date: Thu, 11 Oct 2018 18:51:17 +0000 Subject: [PATCH] Introduce ChromeBulletSpan and use on incognito NTP, sad tab Move the existing SadTabBulletSpan to a new ui/widget/../ChromeBulletSpan.java and re-use for the material design incognito NTP. This fixes a text wrapping issues caused by the bullet previously being included in the text string. BUG=740619,740619 Change-Id: I172ee3e388962efc90d0defe01525828491883cf Reviewed-on: https://chromium-review.googlesource.com/c/1277376 Reviewed-by: Ted Choc Commit-Queue: Theresa Cr-Commit-Position: refs/heads/master@{#598870} --- chrome/android/java/res/values/dimens.xml | 2 - .../ntp/IncognitoNewTabPageViewMD.java | 31 ++++---------- .../chromium/chrome/browser/tab/SadTab.java | 26 +----------- ui/android/BUILD.gn | 1 + ui/android/java/res/values/dimens.xml | 3 ++ .../chromium/ui/widget/ChromeBulletSpan.java | 40 +++++++++++++++++++ 6 files changed, 53 insertions(+), 50 deletions(-) create mode 100644 ui/android/java/src/org/chromium/ui/widget/ChromeBulletSpan.java diff --git a/chrome/android/java/res/values/dimens.xml b/chrome/android/java/res/values/dimens.xml index 604f4a0ca85b39..8c561513df0f17 100644 --- a/chrome/android/java/res/values/dimens.xml +++ b/chrome/android/java/res/values/dimens.xml @@ -502,8 +502,6 @@ 10dp 10dp 16dp - 12dp - 1dp -20dp 56dp diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/IncognitoNewTabPageViewMD.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/IncognitoNewTabPageViewMD.java index dcc72bb6bd8e73..325d6ace94a364 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/IncognitoNewTabPageViewMD.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/IncognitoNewTabPageViewMD.java @@ -8,14 +8,10 @@ import android.content.Context; import android.content.res.Configuration; -import android.graphics.Canvas; -import android.graphics.Paint; import android.support.annotation.IdRes; import android.support.annotation.StringRes; -import android.text.Layout; import android.text.SpannableString; import android.text.method.LinkMovementMethod; -import android.text.style.BulletSpan; import android.text.style.ForegroundColorSpan; import android.util.AttributeSet; import android.view.Gravity; @@ -28,6 +24,7 @@ import org.chromium.chrome.R; import org.chromium.ui.text.NoUnderlineClickableSpan; import org.chromium.ui.text.SpanApplier; +import org.chromium.ui.widget.ChromeBulletSpan; /** * The Material Design New Tab Page for use in the Incognito profile. This is an extension @@ -50,19 +47,6 @@ public class IncognitoNewTabPageViewMD extends IncognitoNewTabPageView { private static final int CONTENT_WIDTH_DP = 600; private static final int WIDE_LAYOUT_THRESHOLD_DP = 720; - private static class IncognitoBulletSpan extends BulletSpan { - public IncognitoBulletSpan() { - super(0 /* gapWidth */); - } - - @Override - public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, - int bottom, CharSequence text, int start, int end, boolean first, Layout l) { - // Do not draw the standard bullet point. We will include the Unicode bullet point - // symbol in the text instead. - } - } - /** Default constructor needed to inflate via XML. */ public IncognitoNewTabPageViewMD(Context context, AttributeSet attrs) { super(context, attrs); @@ -132,12 +116,11 @@ private void populateBulletpoints(@IdRes int element, @StringRes int content) { // Format the bulletpoints: // - Disambiguate the
  • spans for SpanApplier. - // - Add the bulletpoint symbols (Unicode BULLET U+2022) // - Remove leading whitespace (caused by formatting in the .grdp file) // - Remove the trailing newline after the last bulletpoint. - text = text.replaceFirst(" +
  • ([^<]*)
  • ", " \u2022 $1"); - text = text.replaceFirst(" +
  • ([^<]*)
  • ", " \u2022 $1"); - text = text.replaceFirst(" +
  • ([^<]*)
  • \n", " \u2022 $1"); + text = text.replaceFirst(" +
  • ([^<]*)
  • ", "$1"); + text = text.replaceFirst(" +
  • ([^<]*)
  • ", "$1"); + text = text.replaceFirst(" +
  • ([^<]*)
  • \n", "$1"); // Remove the
      tags which serve no purpose here, including the whitespace around // them. @@ -147,9 +130,9 @@ private void populateBulletpoints(@IdRes int element, @StringRes int content) { new SpanApplier.SpanInfo("", "", new ForegroundColorSpan(ApiCompatibilityUtils.getColor( mContext.getResources(), R.color.incognito_emphasis))), - new SpanApplier.SpanInfo("", "", new IncognitoBulletSpan()), - new SpanApplier.SpanInfo("", "", new IncognitoBulletSpan()), - new SpanApplier.SpanInfo("", "", new IncognitoBulletSpan()))); + new SpanApplier.SpanInfo("", "", new ChromeBulletSpan(mContext)), + new SpanApplier.SpanInfo("", "", new ChromeBulletSpan(mContext)), + new SpanApplier.SpanInfo("", "", new ChromeBulletSpan(mContext)))); } /** Adjusts the paddings, margins, and the orientation of bulletpoints. */ diff --git a/chrome/android/java/src/org/chromium/chrome/browser/tab/SadTab.java b/chrome/android/java/src/org/chromium/chrome/browser/tab/SadTab.java index 0505a075f03db3..66e15e7c943b7e 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/tab/SadTab.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/tab/SadTab.java @@ -6,13 +6,9 @@ import android.app.Activity; import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.text.Layout; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.method.LinkMovementMethod; -import android.text.style.BulletSpan; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -33,6 +29,7 @@ import org.chromium.ui.text.NoUnderlineClickableSpan; import org.chromium.ui.text.SpanApplier; import org.chromium.ui.text.SpanApplier.SpanInfo; +import org.chromium.ui.widget.ChromeBulletSpan; /** * Represent the sad tab displayed in place of a crashed renderer. Instantiated on the first @@ -279,7 +276,7 @@ private static void intializeSuggestionsViews( */ private static SpannableString generateBulletedString(Context context, int stringResId) { SpannableString bullet = new SpannableString(context.getString(stringResId)); - bullet.setSpan(new SadTabBulletSpan(context), 0, bullet.length(), 0); + bullet.setSpan(new ChromeBulletSpan(context), 0, bullet.length(), 0); return bullet; } @@ -298,25 +295,6 @@ private static void recordEvent(boolean sendFeedbackView, int event) { } } - private static class SadTabBulletSpan extends BulletSpan { - private int mXOffset; - - public SadTabBulletSpan(Context context) { - super(context.getResources().getDimensionPixelSize(R.dimen.sad_tab_bullet_gap)); - mXOffset = context.getResources().getDimensionPixelSize( - R.dimen.sad_tab_bullet_leading_offset); - } - - @Override - public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, - int bottom, CharSequence text, int start, int end, boolean first, Layout l) { - // Android cuts off the bullet points. Adjust the x-position so that the bullets aren't - // cut off. - super.drawLeadingMargin( - c, p, x + mXOffset, dir, top, baseline, bottom, text, start, end, first, l); - } - } - // Bare minimum set up so |isShowing| returns true. @VisibleForTesting public static void initForTesting(Tab tab, SadTab sadTab) { diff --git a/ui/android/BUILD.gn b/ui/android/BUILD.gn index bc04112d81f462..b641523e27c2e6 100644 --- a/ui/android/BUILD.gn +++ b/ui/android/BUILD.gn @@ -310,6 +310,7 @@ android_library("ui_full_java") { "java/src/org/chromium/ui/widget/AnchoredPopupWindow.java", "java/src/org/chromium/ui/widget/ButtonCompat.java", "java/src/org/chromium/ui/widget/CheckableImageView.java", + "java/src/org/chromium/ui/widget/ChromeBulletSpan.java", "java/src/org/chromium/ui/widget/OptimizedFrameLayout.java", "java/src/org/chromium/ui/widget/RectProvider.java", "java/src/org/chromium/ui/widget/RoundedCornerImageView.java", diff --git a/ui/android/java/res/values/dimens.xml b/ui/android/java/res/values/dimens.xml index e1768773f3bc25..aeded178b12493 100644 --- a/ui/android/java/res/values/dimens.xml +++ b/ui/android/java/res/values/dimens.xml @@ -23,6 +23,9 @@ 4dp + 12dp + 1dp + 1dp diff --git a/ui/android/java/src/org/chromium/ui/widget/ChromeBulletSpan.java b/ui/android/java/src/org/chromium/ui/widget/ChromeBulletSpan.java new file mode 100644 index 00000000000000..f9c8ded3d457b4 --- /dev/null +++ b/ui/android/java/src/org/chromium/ui/widget/ChromeBulletSpan.java @@ -0,0 +1,40 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.ui.widget; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.text.Layout; +import android.text.style.BulletSpan; + +import org.chromium.ui.R; + +/** + * A wrapper around Android's BulletSpan that provides default styling and adjusts the bullet + * positioning to prevent it from being cut off. + */ +public class ChromeBulletSpan extends BulletSpan { + private int mXOffset; + + /** + * Construts a new ChromeBuletSpan. + * @param context The context of the containing view, used to retrieve dimensions. + */ + public ChromeBulletSpan(Context context) { + super(context.getResources().getDimensionPixelSize(R.dimen.chrome_bullet_gap)); + mXOffset = + context.getResources().getDimensionPixelSize(R.dimen.chrome_bullet_leading_offset); + } + + @Override + public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, + int bottom, CharSequence text, int start, int end, boolean first, Layout l) { + // Android cuts off the bullet points. Adjust the x-position so that the bullets aren't + // cut off. + super.drawLeadingMargin( + c, p, x + mXOffset, dir, top, baseline, bottom, text, start, end, first, l); + } +}