From 5cfaae4aae744574f0fcc0876cdc5473bc1a344d Mon Sep 17 00:00:00 2001 From: James Cook Date: Thu, 28 May 2015 15:52:44 -0700 Subject: [PATCH] assist: Fix reported colors/styles for TextView/Switch Changes to the data provided to AssistStructure: * Text foreground color is correct even if the view has not yet been painted. * Text background color is now always 1 (TEXT_COLOR_UNDEFINED) for a TextView, as it has no separate concept of background color. * Switch now reports the text size/color/style of the label text (usually user visible) rather than the on/off text on the button itself (usually hidden in Material, and not usually revelant when visible). Bug: 21080375 Change-Id: I7e15f68d89510a76cab76031c2c8ca6ca3f32435 --- api/current.txt | 3 +- api/system-current.txt | 3 +- core/java/android/app/AssistStructure.java | 30 +------------------ core/java/android/view/ViewStructure.java | 9 +----- .../android/webkit/ViewAssistStructure.java | 7 +---- core/java/android/widget/Switch.java | 4 ++- core/java/android/widget/TextView.java | 29 +++++++++++++++++- 7 files changed, 36 insertions(+), 49 deletions(-) diff --git a/api/current.txt b/api/current.txt index 41acac5e18951..a1ed4bec32039 100644 --- a/api/current.txt +++ b/api/current.txt @@ -36911,8 +36911,7 @@ package android.view { method public abstract void setStylusButtonPressable(boolean); method public abstract void setText(java.lang.CharSequence); method public abstract void setText(java.lang.CharSequence, int, int); - method public abstract void setTextPaint(android.text.TextPaint); - method public abstract void setTextStyle(int, int, int, int); + method public abstract void setTextStyle(float, int, int, int); method public abstract void setVisibility(int); } diff --git a/api/system-current.txt b/api/system-current.txt index 0f6bd2a80dfd2..cd349d8ad9913 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -39170,8 +39170,7 @@ package android.view { method public abstract void setStylusButtonPressable(boolean); method public abstract void setText(java.lang.CharSequence); method public abstract void setText(java.lang.CharSequence, int, int); - method public abstract void setTextPaint(android.text.TextPaint); - method public abstract void setTextStyle(int, int, int, int); + method public abstract void setTextStyle(float, int, int, int); method public abstract void setVisibility(int); } diff --git a/core/java/android/app/AssistStructure.java b/core/java/android/app/AssistStructure.java index b703b0e50b1de..6c605b2a3512a 100644 --- a/core/java/android/app/AssistStructure.java +++ b/core/java/android/app/AssistStructure.java @@ -607,35 +607,7 @@ final public class AssistStructure implements Parcelable { } @Override - public void setTextPaint(TextPaint paint) { - ViewNodeText t = getNodeText(); - t.mTextColor = paint.getColor(); - t.mTextBackgroundColor = paint.bgColor; - t.mTextSize = paint.getTextSize(); - t.mTextStyle = 0; - Typeface tf = paint.getTypeface(); - if (tf != null) { - if (tf.isBold()) { - t.mTextStyle |= ViewNode.TEXT_STYLE_BOLD; - } - if (tf.isItalic()) { - t.mTextStyle |= ViewNode.TEXT_STYLE_ITALIC; - } - } - int pflags = paint.getFlags(); - if ((pflags& Paint.FAKE_BOLD_TEXT_FLAG) != 0) { - t.mTextStyle |= ViewNode.TEXT_STYLE_BOLD; - } - if ((pflags& Paint.UNDERLINE_TEXT_FLAG) != 0) { - t.mTextStyle |= ViewNode.TEXT_STYLE_UNDERLINE; - } - if ((pflags& Paint.STRIKE_THRU_TEXT_FLAG) != 0) { - t.mTextStyle |= ViewNode.TEXT_STYLE_STRIKE_THRU; - } - } - - @Override - public void setTextStyle(int size, int fgColor, int bgColor, int style) { + public void setTextStyle(float size, int fgColor, int bgColor, int style) { ViewNodeText t = getNodeText(); t.mTextColor = fgColor; t.mTextBackgroundColor = bgColor; diff --git a/core/java/android/view/ViewStructure.java b/core/java/android/view/ViewStructure.java index 5c8b0230ab39e..886547ac42e44 100644 --- a/core/java/android/view/ViewStructure.java +++ b/core/java/android/view/ViewStructure.java @@ -144,13 +144,6 @@ public abstract class ViewStructure { */ public abstract void setText(CharSequence text, int selectionStart, int selectionEnd); - /** - * Set default global style of the text previously set with - * {@link #setText}, derived from the given TextPaint object. Size, foreground color, - * background color, and style information will be extracted from the paint. - */ - public abstract void setTextPaint(TextPaint paint); - /** * Explicitly set default global style information for text that was previously set with * {@link #setText}. @@ -160,7 +153,7 @@ public abstract class ViewStructure { * @param bgColor The background color, packed as 0xAARRGGBB. * @param style Style flags, as defined by {@link android.app.AssistStructure.ViewNode}. */ - public abstract void setTextStyle(int size, int fgColor, int bgColor, int style); + public abstract void setTextStyle(float size, int fgColor, int bgColor, int style); /** * Set optional hint text associated with this view; this is for example the text that is diff --git a/core/java/android/webkit/ViewAssistStructure.java b/core/java/android/webkit/ViewAssistStructure.java index bbaceeee1c480..afa5ab817f1b1 100644 --- a/core/java/android/webkit/ViewAssistStructure.java +++ b/core/java/android/webkit/ViewAssistStructure.java @@ -132,12 +132,7 @@ public class ViewAssistStructure extends android.view.ViewAssistStructure { } @Override - public void setTextPaint(TextPaint paint) { - mV.setTextPaint(paint); - } - - @Override - public void setTextStyle(int size, int fgColor, int bgColor, int style) { + public void setTextStyle(float size, int fgColor, int bgColor, int style) { mV.setTextStyle(size, fgColor, bgColor, style); } diff --git a/core/java/android/widget/Switch.java b/core/java/android/widget/Switch.java index f42959ffe28f7..49226cd0fff65 100644 --- a/core/java/android/widget/Switch.java +++ b/core/java/android/widget/Switch.java @@ -1374,7 +1374,9 @@ public class Switch extends CompoundButton { newText.append(oldText).append(' ').append(switchText); structure.setText(newText); } - structure.setTextPaint(mTextPaint); + // The style of the label text is provided via the base TextView class. This is more + // relevant than the style of the (optional) on/off text on the switch button itself, + // so ignore the size/color/style stored this.mTextPaint. } } diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index a93e7efe9f193..1ea68d587896c 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -25,6 +25,7 @@ import android.annotation.StringRes; import android.annotation.StyleRes; import android.annotation.XmlRes; import android.app.Activity; +import android.app.AssistStructure; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -8785,7 +8786,33 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener final boolean isPassword = hasPasswordTransformationMethod(); if (!isPassword) { structure.setText(getText(), getSelectionStart(), getSelectionEnd()); - structure.setTextPaint(mTextPaint); + + // Extract style information that applies to the TextView as a whole. + int style = 0; + int typefaceStyle = getTypefaceStyle(); + if ((typefaceStyle & Typeface.BOLD) != 0) { + style |= AssistStructure.ViewNode.TEXT_STYLE_BOLD; + } + if ((typefaceStyle & Typeface.ITALIC) != 0) { + style |= AssistStructure.ViewNode.TEXT_STYLE_ITALIC; + } + + // Global styles can also be set via TextView.setPaintFlags(). + int paintFlags = mTextPaint.getFlags(); + if ((paintFlags & Paint.FAKE_BOLD_TEXT_FLAG) != 0) { + style |= AssistStructure.ViewNode.TEXT_STYLE_BOLD; + } + if ((paintFlags & Paint.UNDERLINE_TEXT_FLAG) != 0) { + style |= AssistStructure.ViewNode.TEXT_STYLE_UNDERLINE; + } + if ((paintFlags & Paint.STRIKE_THRU_TEXT_FLAG) != 0) { + style |= AssistStructure.ViewNode.TEXT_STYLE_STRIKE_THRU; + } + + // TextView does not have its own text background color. A background is either part + // of the View (and can be any drawable) or a BackgroundColorSpan inside the text. + structure.setTextStyle(getTextSize(), getCurrentTextColor(), + AssistStructure.ViewNode.TEXT_COLOR_UNDEFINED /* bgColor */, style); } structure.setHint(getHint()); }