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

Commit 003f7371 authored by Haoyu Zhang's avatar Haoyu Zhang
Browse files

Add default implementation for LineBackgroundSpan

Bug: 1526775
Test: atest LineBackgroundSpan_StandardTest
Change-Id: Id97981857a90804387d93e89a1b93c1a74498f47
parent 69f49b4a
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -44894,6 +44894,16 @@ package android.text.style {
    method public abstract void drawBackground(android.graphics.Canvas, android.graphics.Paint, int, int, int, int, int, java.lang.CharSequence, int, int, int);
  }
  public static class LineBackgroundSpan.Standard implements android.text.style.LineBackgroundSpan android.text.ParcelableSpan {
    ctor public LineBackgroundSpan.Standard(int);
    ctor public LineBackgroundSpan.Standard(android.os.Parcel);
    method public int describeContents();
    method public void drawBackground(android.graphics.Canvas, android.graphics.Paint, int, int, int, int, int, java.lang.CharSequence, int, int, int);
    method public final int getColor();
    method public int getSpanTypeId();
    method public void writeToParcel(android.os.Parcel, int);
  }
  public abstract interface LineHeightSpan implements android.text.style.ParagraphStyle android.text.style.WrapTogetherSpan {
    method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt);
  }
+8 −1
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.text.style.CharacterStyle;
import android.text.style.EasyEditSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.LineBackgroundSpan;
import android.text.style.LocaleSpan;
import android.text.style.ParagraphStyle;
import android.text.style.QuoteSpan;
@@ -687,7 +688,9 @@ public class TextUtils {
    /** @hide */
    public static final int ACCESSIBILITY_URL_SPAN = 26;
    /** @hide */
    public static final int LAST_SPAN = ACCESSIBILITY_URL_SPAN;
    public static final int LINE_BACKGROUND_SPAN = 27;
    /** @hide */
    public static final int LAST_SPAN = LINE_BACKGROUND_SPAN;

    /**
     * Flatten a CharSequence and whatever styles can be copied across processes
@@ -878,6 +881,10 @@ public class TextUtils {
                    readSpan(p, sp, new AccessibilityURLSpan(p));
                    break;

                case LINE_BACKGROUND_SPAN:
                    readSpan(p, sp, new LineBackgroundSpan.Standard(p));
                    break;

                default:
                    throw new RuntimeException("bogus span encoding " + kind);
                }
+102 −7
Original line number Diff line number Diff line
@@ -16,15 +16,110 @@

package android.text.style;

import android.annotation.ColorInt;
import android.annotation.NonNull;
import android.annotation.Px;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.text.ParcelableSpan;
import android.text.TextUtils;

public interface LineBackgroundSpan
extends ParagraphStyle
/**
 * Used to change the background of lines where the span is attached to.
 */
public interface LineBackgroundSpan extends ParagraphStyle
{
    public void drawBackground(Canvas c, Paint p,
                               int left, int right,
                               int top, int baseline, int bottom,
                               CharSequence text, int start, int end,
                               int lnum);
    /**
     * Draw the background on the canvas.
     *
     * @param canvas      canvas on which the span should be rendered
     * @param paint       paint used to draw text, which should be left unchanged on exit
     * @param left        left position of the line relative to input canvas, in pixels
     * @param right       right position of the line relative to input canvas, in pixels
     * @param top         top position of the line relative to input canvas, in pixels
     * @param baseline    baseline of the text relative to input canvas, in pixels
     * @param bottom      bottom position of the line relative to input canvas, in pixels
     * @param text        current text
     * @param start       start character index of the line
     * @param end         end character index of the line
     * @param lineNumber  line number in the current text layout
     */
    void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint,
                               @Px int left, @Px int right,
                               @Px int top, @Px int baseline, @Px int bottom,
                               @NonNull CharSequence text, int start, int end,
                               int lineNumber);
    /**
     * Default implementation of the {@link LineBackgroundSpan}, which changes the background
     * color of the lines to which the span is attached.
     */
    class Standard implements LineBackgroundSpan, ParcelableSpan {

        private final int mColor;

        /**
         * Constructor taking a color integer.
         *
         * @param color Color integer that defines the background color.
         */
        public Standard(@ColorInt int color) {
            mColor = color;
        }

        /**
         * Creates a {@link LineBackgroundSpan.Standard} from a parcel
         */
        public Standard(@NonNull Parcel src) {
            mColor = src.readInt();
        }

        @Override
        public int getSpanTypeId() {
            return getSpanTypeIdInternal();
        }

        /** @hide */
        @Override
        public int getSpanTypeIdInternal() {
            return TextUtils.LINE_BACKGROUND_SPAN;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            writeToParcelInternal(dest, flags);
        }

        /** @hide */
        @Override
        public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
            dest.writeInt(mColor);
        }

        /**
         * @return the color of this span.
         * @see Standard#Standard(int)
         */
        @ColorInt
        public final int getColor() {
            return mColor;
        }

        @Override
        public void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint,
                @Px int left, @Px int right,
                @Px int top, @Px int baseline, @Px int bottom,
                @NonNull CharSequence text, int start, int end,
                int lineNumber) {
            final int originColor = paint.getColor();
            paint.setColor(mColor);
            canvas.drawRect(left, right, top, bottom, paint);
            paint.setColor(originColor);
        }
    }
}