summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorSeigo Nonaka <nona@google.com>2018-02-28 18:21:29 -0800
committerSeigo Nonaka <nona@google.com>2018-03-07 18:30:27 +0000
commite1ffb54167c8ff78855352a324ff8332a33fc805 (patch)
tree22954e17e87f129451f18dc8d7e69c7314ee0acb /core/java
parent1e36211a9633c2f0a87701e0f9bf78291514da03 (diff)
Throw an exception in case of parameter mismatch of precomputed text
If the given precomputed text is not compatible with the TextView, reject the text by throwing IllegalArgumentException. Bug: 73091756 Test: atest CtsWidgetTestCases:EditTextTest CtsWidgetTestCases:TextViewFadingEdgeTest FrameworksCoreTests:TextViewFallbackLineSpacingTest FrameworksCoreTests:TextViewTest FrameworksCoreTests:TypefaceTest CtsGraphicsTestCases:TypefaceTest CtsWidgetTestCases:TextViewTest CtsTextTestCases FrameworksCoreTests:android.text CtsWidgetTestCases:TextViewPrecomputedTextTest Change-Id: I4fbf89a5f1409e8eefdeb9f208f9a3758220fe1a (cherry picked from commit 3a0787af5e1bb1f615c1dd2192a08464ea3776e3)
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/text/PrecomputedText.java20
-rw-r--r--core/java/android/widget/TextView.java24
2 files changed, 42 insertions, 2 deletions
diff --git a/core/java/android/text/PrecomputedText.java b/core/java/android/text/PrecomputedText.java
index c211e2dba3c0..b74019373f57 100644
--- a/core/java/android/text/PrecomputedText.java
+++ b/core/java/android/text/PrecomputedText.java
@@ -211,7 +211,8 @@ public class PrecomputedText implements Spanned {
return mHyphenationFrequency;
}
- private boolean isSameTextMetricsInternal(@NonNull TextPaint paint,
+ /** @hide */
+ public boolean isSameTextMetricsInternal(@NonNull TextPaint paint,
@NonNull TextDirectionHeuristic textDir, @Layout.BreakStrategy int strategy,
@Layout.HyphenationFrequency int frequency) {
return mTextDir == textDir
@@ -247,6 +248,23 @@ public class PrecomputedText implements Spanned {
mPaint.getFontVariationSettings(), mPaint.isElegantTextHeight(), mTextDir,
mBreakStrategy, mHyphenationFrequency);
}
+
+ @Override
+ public String toString() {
+ return "{"
+ + "textSize=" + mPaint.getTextSize()
+ + ", textScaleX=" + mPaint.getTextScaleX()
+ + ", textSkewX=" + mPaint.getTextSkewX()
+ + ", letterSpacing=" + mPaint.getLetterSpacing()
+ + ", textLocale=" + mPaint.getTextLocales()
+ + ", typeface=" + mPaint.getTypeface()
+ + ", variationSettings=" + mPaint.getFontVariationSettings()
+ + ", elegantTextHeight=" + mPaint.isElegantTextHeight()
+ + ", textDir=" + mTextDir
+ + ", breakStrategy=" + mBreakStrategy
+ + ", hyphenationFrequency=" + mHyphenationFrequency
+ + "}";
+ }
};
// The original text.
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 6e40ea85e34a..d358c95ba09b 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -4141,6 +4141,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
*/
public void setTextMetricsParams(@NonNull PrecomputedText.Params params) {
mTextPaint.set(params.getTextPaint());
+ mUserSetTextScaleX = true;
mTextDir = params.getTextDirection();
mBreakStrategy = params.getBreakStrategy();
mHyphenationFrequency = params.getHyphenationFrequency();
@@ -5528,9 +5529,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
* {@link android.text.Editable.Factory} to create final or intermediate
* {@link Editable Editables}.
*
+ * If the passed text is a {@link PrecomputedText} but the parameters used to create the
+ * PrecomputedText mismatches with this TextView, IllegalArgumentException is thrown. To ensure
+ * the parameters match, you can call {@link TextView#setTextMetricsParams} before calling this.
+ *
* @param text text to be displayed
*
* @attr ref android.R.styleable#TextView_text
+ * @throws IllegalArgumentException if the passed text is a {@link PrecomputedText} but the
+ * parameters used to create the PrecomputedText mismatches
+ * with this TextView.
*/
@android.view.RemotableViewMethod
public final void setText(CharSequence text) {
@@ -5644,7 +5652,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
if (imm != null) imm.restartInput(this);
} else if (type == BufferType.SPANNABLE || mMovement != null) {
text = mSpannableFactory.newSpannable(text);
- } else if (!(text instanceof PrecomputedText || text instanceof CharWrapper)) {
+ } else if (text instanceof PrecomputedText) {
+ PrecomputedText precomputed = (PrecomputedText) text;
+ if (mTextDir == null) {
+ mTextDir = getTextDirectionHeuristic();
+ }
+ if (!precomputed.getParams().isSameTextMetricsInternal(
+ getPaint(), mTextDir, mBreakStrategy, mHyphenationFrequency)) {
+ throw new IllegalArgumentException(
+ "PrecomputedText's Parameters don't match the parameters of this TextView."
+ + "Consider using setTextMetricsParams(precomputedText.getParams()) "
+ + "to override the settings of this TextView: "
+ + "PrecomputedText: " + precomputed.getParams()
+ + "TextView: " + getTextMetricsParams());
+ }
+ } else if (!(text instanceof CharWrapper)) {
text = TextUtils.stringOrSpannedString(text);
}