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

Commit a118f936 authored by Xiaowen Lei's avatar Xiaowen Lei
Browse files

Handle the cases where the segment isn't long enough.

1. When the segment has a negative length (after subtracting gaps and
   caps).
2. When the segment isn't long enough for drawing the caps (after
   subtracting gaps).

Also fixed:
  1. For dashed segments, there is no caps. Therefore we should draw
     from `start` to `end`, rather than from `start + capWidth` to `end
     - capWidth`.
  2. Set `mPointRectF` directly from values rather than from
     `mPointRect`.

Flag: android.app.api_rich_ongoing
Fix: 376491336
Bug: 376488208
Bug: 376487227
Bug: 376492598
Test: Post ProgressStyle notifs (as defined in the bugs) and observe the notification.
Change-Id: I6a0a45db4b136e024f41ee5a99e2eb8d27d43799
parent 9839809a
Loading
Loading
Loading
Loading
+35 −14
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ public final class NotificationProgressDrawable extends Drawable {

    private final ArrayList<Part> mParts = new ArrayList<>();

    private final RectF mSegRectF = new RectF();
    private final Rect mPointRect = new Rect();
    private final RectF mPointRectF = new RectF();

@@ -198,22 +199,42 @@ public final class NotificationProgressDrawable extends Drawable {
                        mState.mSegSegGap, x + segWidth, totalWidth);
                final float end = x + segWidth - endOffset;

                // Transparent is not allowed (and also is the default in the data), so use that
                // as a sentinel to be replaced by default
                mStrokePaint.setColor(segment.mColor != Color.TRANSPARENT ? segment.mColor
                        : mState.mStrokeColor);
                // Advance the current position to account for the segment's fraction of the total
                // width (ignoring offset and padding)
                x += segWidth;

                // No space left to draw the segment
                if (start > end) continue;

                if (segment.mDashed) {
                    // No caps when the segment is dashed.

                    mDashedStrokePaint.setColor(segment.mColor != Color.TRANSPARENT ? segment.mColor
                            : mState.mFadedStrokeColor);
                    canvas.drawLine(start, centerY, end, centerY, mDashedStrokePaint);
                } else if (end - start < mState.mStrokeWidth) {
                    // Not enough segment length to draw the caps

                    final float rad = (end - start) / 2F;
                    final float capWidth = mStrokePaint.getStrokeWidth() / 2F;

                    mFillPaint.setColor(segment.mColor != Color.TRANSPARENT ? segment.mColor
                            : mState.mStrokeColor);

                    mSegRectF.set(start, centerY - capWidth, end, centerY + capWidth);
                    canvas.drawRoundRect(mSegRectF, rad, rad, mFillPaint);
                } else {
                    // Leave space for the rounded line cap which extends beyond start/end.
                    final float capWidth = mStrokePaint.getStrokeWidth() / 2F;

                canvas.drawLine(start + capWidth, centerY, end - capWidth, centerY,
                        segment.mDashed ? mDashedStrokePaint : mStrokePaint);
                    // Transparent is not allowed (and also is the default in the data), so use that
                    // as a sentinel to be replaced by default
                    mStrokePaint.setColor(segment.mColor != Color.TRANSPARENT ? segment.mColor
                            : mState.mStrokeColor);

                // Advance the current position to account for the segment's fraction of the total
                // width (ignoring offset and padding)
                x += segWidth;
                    canvas.drawLine(start + capWidth, centerY, end - capWidth, centerY,
                            mStrokePaint);
                }
            } else if (part instanceof Point point) {
                final float pointWidth = 2 * pointRadius;
                float start = x - pointRadius;
@@ -232,7 +253,7 @@ public final class NotificationProgressDrawable extends Drawable {
                } else {
                    // TODO: b/367804171 - actually use a vector asset for the default point
                    //  rather than drawing it as a box?
                    mPointRectF.set(mPointRect);
                    mPointRectF.set(start, centerY - pointRadius, end, centerY + pointRadius);
                    final float inset = mState.mPointRectInset;
                    final float cornerRadius = mState.mPointRectCornerRadius;
                    mPointRectF.inset(inset, inset);