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
diff --git a/api/current.txt b/api/current.txt
index 41acac5..a1ed4be 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -36911,8 +36911,7 @@
     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 0f6bd2a..cd349d8 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -39170,8 +39170,7 @@
     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 b703b0e..6c605b2 100644
--- a/core/java/android/app/AssistStructure.java
+++ b/core/java/android/app/AssistStructure.java
@@ -607,35 +607,7 @@
         }
 
         @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 5c8b023..886547a 100644
--- a/core/java/android/view/ViewStructure.java
+++ b/core/java/android/view/ViewStructure.java
@@ -145,13 +145,6 @@
     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 @@
      * @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 bbaceee..afa5ab8 100644
--- a/core/java/android/webkit/ViewAssistStructure.java
+++ b/core/java/android/webkit/ViewAssistStructure.java
@@ -132,12 +132,7 @@
     }
 
     @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 f42959f..49226cd0 100644
--- a/core/java/android/widget/Switch.java
+++ b/core/java/android/widget/Switch.java
@@ -1374,7 +1374,9 @@
                 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 a93e7ef..1ea68d5 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -25,6 +25,7 @@
 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 @@
         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());
     }