Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9fe208fe authored by Andrei Stingaceanu's avatar Andrei Stingaceanu
Browse files

AutoSize TextView (part 8) - APIs for predefined sizes

* getter/setter for predefined sizes
* reads and configures from XML at construction time
* fix for an ugly bug where the sizes were missing an
  entry in certain cases, e.g: min = 10; max = 20;
  step = 2 would have produced [10, 12, 14, 16, 18]
  instead of [10, 12, 14, 16, 18, 20]
* fix using getHeight()/getWidth() instead of
  untrusted getMeasuredHeight()/getMeasuredWidth()
  and move the auto-sizing triggering to
  onLayout() instead of onMeasure() (while manually
  testing discovered missing or extra pixels and
  sometimes resizing being skipped - it's all fixed
  now)
* fix using deceiving getTotalPaddingBottom()/...Top()
  and replaced with getExtendedPaddingBottom()/..Top()
  (getTotal... was removing the whitespace height but
  auto-size needs to know about it so it can fill the
  space)

Bug: 32221168
Test: attached in the same topic
      run cts-dev -m CtsWidgetTestCases -t \
      android.widget.cts.TextViewTest

Change-Id: Id5a31d0d32b2b4082af45b4bd65af8cb85bdc92e
parent 155c3a88
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -50383,6 +50383,7 @@ package android.widget {
    method public int getAutoSizeMaxTextSize();
    method public int getAutoSizeMinTextSize();
    method public int getAutoSizeStepGranularity();
    method public int[] getAutoSizeTextPresetSizes();
    method public int getAutoSizeTextType();
    method public int getBreakStrategy();
    method public int getCompoundDrawablePadding();
@@ -50496,6 +50497,7 @@ package android.widget {
    method public void setAutoSizeMaxTextSize(int, float);
    method public void setAutoSizeMinTextSize(int, float);
    method public void setAutoSizeStepGranularity(int, float);
    method public void setAutoSizeTextPresetSizes(int[]);
    method public void setAutoSizeTextType(int);
    method public void setBreakStrategy(int);
    method public void setCompoundDrawablePadding(int);
+2 −0
Original line number Diff line number Diff line
@@ -54154,6 +54154,7 @@ package android.widget {
    method public int getAutoSizeMaxTextSize();
    method public int getAutoSizeMinTextSize();
    method public int getAutoSizeStepGranularity();
    method public int[] getAutoSizeTextPresetSizes();
    method public int getAutoSizeTextType();
    method public int getBreakStrategy();
    method public int getCompoundDrawablePadding();
@@ -54267,6 +54268,7 @@ package android.widget {
    method public void setAutoSizeMaxTextSize(int, float);
    method public void setAutoSizeMinTextSize(int, float);
    method public void setAutoSizeStepGranularity(int, float);
    method public void setAutoSizeTextPresetSizes(int[]);
    method public void setAutoSizeTextType(int);
    method public void setBreakStrategy(int);
    method public void setCompoundDrawablePadding(int);
+2 −0
Original line number Diff line number Diff line
@@ -50703,6 +50703,7 @@ package android.widget {
    method public int getAutoSizeMaxTextSize();
    method public int getAutoSizeMinTextSize();
    method public int getAutoSizeStepGranularity();
    method public int[] getAutoSizeTextPresetSizes();
    method public int getAutoSizeTextType();
    method public int getBreakStrategy();
    method public int getCompoundDrawablePadding();
@@ -50816,6 +50817,7 @@ package android.widget {
    method public void setAutoSizeMaxTextSize(int, float);
    method public void setAutoSizeMinTextSize(int, float);
    method public void setAutoSizeStepGranularity(int, float);
    method public void setAutoSizeTextPresetSizes(int[]);
    method public void setAutoSizeTextType(int);
    method public void setBreakStrategy(int);
    method public void setCompoundDrawablePadding(int);
+162 −50
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ import android.text.style.URLSpan;
import android.text.style.UpdateAppearance;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.util.IntArray;
import android.util.Log;
import android.util.TypedValue;
import android.view.AccessibilityIterators.TextSegmentIterator;
@@ -159,6 +160,8 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.util.FastMath;
import com.android.internal.widget.EditableInputConnection;

import libcore.util.EmptyArray;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
@@ -166,6 +169,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

/**
@@ -262,6 +266,7 @@ import java.util.Locale;
 * @attr ref android.R.styleable#TextView_autoSizeMinTextSize
 * @attr ref android.R.styleable#TextView_autoSizeMaxTextSize
 * @attr ref android.R.styleable#TextView_autoSizeStepGranularity
 * @attr ref android.R.styleable#TextView_autoSizeStepSizeSet
 */
@RemoteView
public class TextView extends View implements ViewTreeObserver.OnPreDrawListener {
@@ -708,8 +713,13 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    private int mAutoSizeMinTextSizeInPx = 0;
    // Maximum text size for auto-sizing in pixels.
    private int mAutoSizeMaxTextSizeInPx = 0;
    // Contains the sorted set of desired text sizes in pixels to pick from when auto-sizing text.
    private int[] mAutoSizeTextSizesInPx;
    // Contains a (specified or computed) distinct sorted set of text sizes in pixels to pick from
    // when auto-sizing text.
    private int[] mAutoSizeTextSizesInPx = EmptyArray.INT;
    // Specifies whether auto-size should use the provided auto size steps set or if it should
    // build the steps set using mAutoSizeMinTextSizeInPx, mAutoSizeMaxTextSizeInPx and
    // mAutoSizeStepGranularityInPx.
    private boolean mHasPredefinedAutoSizeSet = false;

    // Watcher used to notify changes to auto-fill manager.
    private AutoFillChangeWatcher mAutoFillChangeWatcher;
@@ -1301,8 +1311,19 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                case com.android.internal.R.styleable.TextView_autoSizeMaxTextSize:
                    mAutoSizeMaxTextSizeInPx = a.getDimensionPixelSize(attr, 0);
                    break;

                case com.android.internal.R.styleable.TextView_autoSizeStepSizeSet:
                    final int autoSizeStepSizeArrayResId = a.getResourceId(attr, 0);
                    if (autoSizeStepSizeArrayResId > 0) {
                        final TypedArray autoSizePreDefTextSizes = a.getResources()
                                .obtainTypedArray(autoSizeStepSizeArrayResId);
                        setupAutoSizeStepSizeSet(autoSizePreDefTextSizes);
                        autoSizePreDefTextSizes.recycle();
                    }
                    break;
            }
        }

        a.recycle();

        BufferType bufferType = BufferType.EDITABLE;
@@ -1599,7 +1620,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                        mAutoSizeMinTextSizeInPx = 0;
                        mAutoSizeMaxTextSizeInPx = 0;
                        mAutoSizeStepGranularityInPx = 0;
                        mAutoSizeTextSizesInPx = null;
                        mAutoSizeTextSizesInPx = EmptyArray.INT;
                        mNeedsAutoSizeText = false;
                    }
                    break;
@@ -1651,6 +1672,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        if (supportsAutoSizeText()) {
            mAutoSizeStepGranularityInPx = (int) TypedValue.applyDimension(
                    unit, size, getResources().getDisplayMetrics());
            mHasPredefinedAutoSizeSet = false;
            setupAutoSizeTextXY();
        }
    }
@@ -1683,6 +1705,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        if (supportsAutoSizeText()) {
            mAutoSizeMinTextSizeInPx = (int) TypedValue.applyDimension(
                    unit, size, getResources().getDisplayMetrics());
            mHasPredefinedAutoSizeSet = false;
            setupAutoSizeTextXY();
        }
    }
@@ -1716,6 +1739,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        if (supportsAutoSizeText()) {
            mAutoSizeMaxTextSizeInPx = (int) TypedValue.applyDimension(
                    unit, size, getResources().getDisplayMetrics());
            mHasPredefinedAutoSizeSet = false;
            setupAutoSizeTextXY();
        }
    }
@@ -1730,8 +1754,95 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        return mAutoSizeMaxTextSizeInPx;
    }

    /**
     * Sets a predefined array of sizes to be used when auto-sizing.
     *
     * <ul>Note:
     * <li>when <code>presetSizes</code> is not empty then the auto-size algorithm will use the
     * values provided here instead of calculating the values based on min, max and step size. Also
     * the values will be de-duplicated, sorted and negative or zero values will be removed.
     * <li>when <code>presetSizes</code> is empty then the auto-size algorithm will use the min, max
     * and step size to build the set of available sizes to choose from. Note that if no values have
     * been provided for any of min, max or step size then defaults will be used.
     * </ul>
     *
     * @param presetSizes an {@code int} array of sizes in pixels
     *
     * @attr ref android.R.styleable#TextView_autoSizeStepSizeSet
     *
     * @see #getAutoSizeTextPresetSizes()
     */
    public void setAutoSizeTextPresetSizes(@NonNull int[] presetSizes) {
        if (supportsAutoSizeText()) {
            if (presetSizes.length > 0) {
                mAutoSizeTextSizesInPx = cleanupAutoSizePredefinedSizes(presetSizes);
                final int sizesLength = mAutoSizeTextSizesInPx.length;
                mHasPredefinedAutoSizeSet = sizesLength > 0;
                if (mHasPredefinedAutoSizeSet) {
                    mAutoSizeMinTextSizeInPx = mAutoSizeTextSizesInPx[0];
                    mAutoSizeMaxTextSizeInPx = mAutoSizeTextSizesInPx[sizesLength - 1];
                    mAutoSizeStepGranularityInPx = 0;
                }
            } else {
                mHasPredefinedAutoSizeSet = false;
            }
            setupAutoSizeTextXY();
        }
    }

    /**
     * @return the current auto-size {@code int} sizes array (in pixels).
     *
     * @see #setAutoSizeTextPresetSizes(int[])
     * @see #setAutoSizeMinTextSize(int, float)
     * @see #setAutoSizeMaxTextSize(int, float)
     * @see #setAutoSizeStepGranularity(int, float)
     */
    public int[] getAutoSizeTextPresetSizes() {
        return mAutoSizeTextSizesInPx;
    }

    private void setupAutoSizeStepSizeSet(TypedArray textSizes) {
        final int textSizesLength = textSizes.length();
        final int[] parsedSizes = new int[textSizesLength];

        if (textSizesLength > 0) {
            for (int i = 0; i < textSizesLength; i++) {
                parsedSizes[i] = textSizes.getDimensionPixelSize(i, -1);
            }
            mAutoSizeTextSizesInPx = cleanupAutoSizePredefinedSizes(parsedSizes);
            mHasPredefinedAutoSizeSet = mAutoSizeTextSizesInPx.length > 0;
        }
    }

    // Returns distinct sorted positive values.
    private int[] cleanupAutoSizePredefinedSizes(int[] predefinedValues) {
        final int predefinedValuesLength = predefinedValues.length;
        if (predefinedValuesLength == 0) {
            return predefinedValues;
        }
        Arrays.sort(predefinedValues);

        final IntArray uniqueValidSizes = new IntArray();
        for (int i = 0; i < predefinedValuesLength; i++) {
            final int currentPredefinedValue = predefinedValues[i];

            if (currentPredefinedValue > 0
                    && uniqueValidSizes.binarySearch(currentPredefinedValue) < 0) {
                uniqueValidSizes.add(currentPredefinedValue);
            }
        }

        return predefinedValuesLength == uniqueValidSizes.size()
            ? predefinedValues
            : uniqueValidSizes.toArray();
    }

    private void setupAutoSizeTextXY() {
        if (supportsAutoSizeText() && mAutoSizeTextType == AUTO_SIZE_TEXT_TYPE_XY) {
            // Calculate the sizes set based on minimum size, maximum size and step size if we do
            // not have a predefined set of sizes or if the current sizes array is empty.
            if (!mHasPredefinedAutoSizeSet || mAutoSizeTextSizesInPx.length == 0) {
                // Set valid defaults.
                if (mAutoSizeMinTextSizeInPx <= 0) {
                    mAutoSizeMinTextSizeInPx = (int) TypedValue.applyDimension(
@@ -1754,21 +1865,27 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                // Validate.
                if (mAutoSizeMaxTextSizeInPx <= mAutoSizeMinTextSizeInPx) {
                    throw new IllegalStateException("Maximum auto-size text size ("
                        + mAutoSizeMaxTextSizeInPx + "px) is less or equal to minimum auto-size "
                            + mAutoSizeMaxTextSizeInPx
                            + "px) is less or equal to minimum auto-size "
                            + "text size (" + mAutoSizeMinTextSizeInPx + "px)");
                }

                // Calculate sizes to choose from based on the current auto-size configuration.
            final int autoSizeValuesLength = (int) Math.ceil(
                int autoSizeValuesLength = (int) Math.ceil(
                        (mAutoSizeMaxTextSizeInPx - mAutoSizeMinTextSizeInPx)
                            / mAutoSizeStepGranularityInPx);

                                / (float) mAutoSizeStepGranularityInPx);
                // Also reserve a slot for the max size if it fits.
                if ((mAutoSizeMaxTextSizeInPx - mAutoSizeMinTextSizeInPx)
                        % mAutoSizeStepGranularityInPx == 0) {
                    autoSizeValuesLength++;
                }
                mAutoSizeTextSizesInPx = new int[autoSizeValuesLength];
                int sizeToAdd = mAutoSizeMinTextSizeInPx;
                for (int i = 0; i < autoSizeValuesLength; i++) {
                    mAutoSizeTextSizesInPx[i] = sizeToAdd;
                    sizeToAdd += mAutoSizeStepGranularityInPx;
                }
            }

            mNeedsAutoSizeText = true;
            autoSizeText();
@@ -7815,16 +7932,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            scrollTo(0, 0);
        }

        if (isAutoSizeEnabled()) {
            if (mNeedsAutoSizeText) {
                // Call auto-size after the width and height have been calculated.
                autoSizeText();
            }
            // Always try to auto-size if enabled. Functions that do not want to trigger auto-sizing
            // after the next measuring round should set this to false.
            mNeedsAutoSizeText = true;
        }

        setMeasuredDimension(width, height);
    }

@@ -7832,8 +7939,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     * Automatically computes and sets the text size.
     */
    private void autoSizeText() {
        final int maxWidth = getMeasuredWidth() - getTotalPaddingLeft() - getTotalPaddingRight();
        final int maxHeight = getMeasuredHeight() - getTotalPaddingBottom() - getTotalPaddingTop();
        final int maxWidth = getWidth() - getTotalPaddingLeft() - getTotalPaddingRight();
        final int maxHeight = getHeight() - getExtendedPaddingBottom() - getExtendedPaddingTop();

        if (maxWidth <= 0 || maxHeight <= 0) {
            return;
@@ -7907,11 +8014,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                return false;
            }

            // Width overflow.
            if (layout.getWidth() > availableSpace.right) {
                return false;
            }

            // Height overflow.
            if (layout.getHeight() > availableSpace.bottom) {
                return false;
@@ -8079,6 +8181,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            mDeferScroll = -1;
            bringPointIntoView(Math.min(curs, mText.length()));
        }

        if (isAutoSizeEnabled()) {
            if (mNeedsAutoSizeText) {
                // Call auto-size after the width and height have been calculated.
                autoSizeText();
            }
            // Always try to auto-size if enabled. Functions that do not want to trigger auto-sizing
            // after the next layout round should set this to false.
            mNeedsAutoSizeText = true;
        }
    }

    private boolean isShowingHint() {