|
| 1 | +package de.danoeh.antennapod.view; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Canvas; |
| 5 | +import android.graphics.Paint; |
| 6 | +import android.util.AttributeSet; |
| 7 | +import de.danoeh.antennapod.ui.common.ThemeUtils; |
| 8 | + |
| 9 | +public class ChapterSeekBar extends androidx.appcompat.widget.AppCompatSeekBar { |
| 10 | + |
| 11 | + private float top; |
| 12 | + private float width; |
| 13 | + private float bottom; |
| 14 | + private float density; |
| 15 | + private float progressPrimary; |
| 16 | + private float progressSecondary; |
| 17 | + private float[] dividerPos; |
| 18 | + private final Paint paintBackground = new Paint(); |
| 19 | + private final Paint paintProgressPrimary = new Paint(); |
| 20 | + private final Paint paintProgressSecondary = new Paint(); |
| 21 | + |
| 22 | + public ChapterSeekBar(Context context) { |
| 23 | + super(context); |
| 24 | + init(context); |
| 25 | + } |
| 26 | + |
| 27 | + public ChapterSeekBar(Context context, AttributeSet attrs) { |
| 28 | + super(context, attrs); |
| 29 | + init(context); |
| 30 | + } |
| 31 | + |
| 32 | + public ChapterSeekBar(Context context, AttributeSet attrs, int defStyle) { |
| 33 | + super(context, attrs, defStyle); |
| 34 | + init(context); |
| 35 | + } |
| 36 | + |
| 37 | + private void init(Context context) { |
| 38 | + setBackground(null); // Removes the thumb shadow |
| 39 | + dividerPos = null; |
| 40 | + density = context.getResources().getDisplayMetrics().density; |
| 41 | + paintBackground.setColor(ThemeUtils.getColorFromAttr(getContext(), |
| 42 | + de.danoeh.antennapod.core.R.attr.currently_playing_background)); |
| 43 | + paintBackground.setAlpha(128); |
| 44 | + paintProgressPrimary.setColor(ThemeUtils.getColorFromAttr(getContext(), |
| 45 | + de.danoeh.antennapod.core.R.attr.colorPrimary)); |
| 46 | + paintProgressSecondary.setColor(ThemeUtils.getColorFromAttr(getContext(), |
| 47 | + de.danoeh.antennapod.core.R.attr.seek_background)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the relative positions of the chapter dividers. |
| 52 | + * @param dividerPos of the chapter dividers relative to the duration of the media. |
| 53 | + */ |
| 54 | + public void setDividerPos(final float[] dividerPos) { |
| 55 | + if (dividerPos != null) { |
| 56 | + this.dividerPos = new float[dividerPos.length + 2]; |
| 57 | + this.dividerPos[0] = 0; |
| 58 | + System.arraycopy(dividerPos, 0, this.dividerPos, 1, dividerPos.length); |
| 59 | + this.dividerPos[this.dividerPos.length - 1] = 1; |
| 60 | + } else { |
| 61 | + this.dividerPos = null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected synchronized void onDraw(Canvas canvas) { |
| 67 | + top = getTop() + density * 7.5f; |
| 68 | + bottom = getBottom() - density * 7.5f; |
| 69 | + width = (float) (getRight() - getPaddingRight() - getLeft() - getPaddingLeft()); |
| 70 | + progressSecondary = getSecondaryProgress() / (float) getMax() * width; |
| 71 | + progressPrimary = getProgress() / (float) getMax() * width; |
| 72 | + |
| 73 | + if (dividerPos == null) { |
| 74 | + drawProgress(canvas); |
| 75 | + } else { |
| 76 | + drawProgressChapters(canvas); |
| 77 | + } |
| 78 | + drawThumb(canvas); |
| 79 | + } |
| 80 | + |
| 81 | + private void drawProgress(Canvas canvas) { |
| 82 | + final int saveCount = canvas.save(); |
| 83 | + canvas.translate(getPaddingLeft(), getPaddingTop()); |
| 84 | + canvas.drawRect(0, top, width, bottom, paintBackground); |
| 85 | + canvas.drawRect(0, top, progressSecondary, bottom, paintProgressSecondary); |
| 86 | + canvas.drawRect(0, top, progressPrimary, bottom, paintProgressPrimary); |
| 87 | + canvas.restoreToCount(saveCount); |
| 88 | + } |
| 89 | + |
| 90 | + private void drawProgressChapters(Canvas canvas) { |
| 91 | + final int saveCount = canvas.save(); |
| 92 | + int currChapter = 1; |
| 93 | + float chapterMargin = density * 0.6f; |
| 94 | + float topExpanded = getTop() + density * 7; |
| 95 | + float bottomExpanded = getBottom() - density * 7; |
| 96 | + |
| 97 | + canvas.translate(getPaddingLeft(), getPaddingTop()); |
| 98 | + |
| 99 | + for (int i = 1; i < dividerPos.length; i++) { |
| 100 | + float right = dividerPos[i] * width - chapterMargin; |
| 101 | + float left = dividerPos[i - 1] * width + chapterMargin; |
| 102 | + float rightCurr = dividerPos[currChapter] * width - chapterMargin; |
| 103 | + float leftCurr = dividerPos[currChapter - 1] * width + chapterMargin; |
| 104 | + |
| 105 | + canvas.drawRect(left, top, right, bottom, paintBackground); |
| 106 | + |
| 107 | + if (right < progressPrimary) { |
| 108 | + currChapter = i + 1; |
| 109 | + canvas.drawRect(left, top, right, bottom, paintProgressPrimary); |
| 110 | + } else if (isPressed()) { |
| 111 | + canvas.drawRect(leftCurr, topExpanded, rightCurr, bottomExpanded, paintBackground); |
| 112 | + canvas.drawRect(leftCurr, topExpanded, progressPrimary, bottomExpanded, paintProgressPrimary); |
| 113 | + } else { |
| 114 | + if (progressSecondary > leftCurr) { |
| 115 | + canvas.drawRect(leftCurr, top, progressSecondary, bottom, paintProgressSecondary); |
| 116 | + } |
| 117 | + canvas.drawRect(leftCurr, top, progressPrimary, bottom, paintProgressPrimary); |
| 118 | + } |
| 119 | + } |
| 120 | + canvas.restoreToCount(saveCount); |
| 121 | + } |
| 122 | + |
| 123 | + private void drawThumb(Canvas canvas) { |
| 124 | + final int saveCount = canvas.save(); |
| 125 | + canvas.translate(getPaddingLeft() - getThumbOffset(), getPaddingTop()); |
| 126 | + getThumb().draw(canvas); |
| 127 | + canvas.restoreToCount(saveCount); |
| 128 | + } |
| 129 | +} |
0 commit comments