summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/view/PlaybackSpeedSeekBar.java
blob: 33f0d47b83975a1b68878cd118321cb5eaa43709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package de.danoeh.antennapod.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Consumer;
import de.danoeh.antennapod.R;

public class PlaybackSpeedSeekBar extends FrameLayout {
    private SeekBar seekBar;
    private Consumer<Float> progressChangedListener;

    public PlaybackSpeedSeekBar(@NonNull Context context) {
        super(context);
        setup();
    }

    public PlaybackSpeedSeekBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setup();
    }

    public PlaybackSpeedSeekBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setup();
    }

    private void setup() {
        View.inflate(getContext(), R.layout.playback_speed_seek_bar, this);
        seekBar = findViewById(R.id.playback_speed);
        findViewById(R.id.butDecSpeed).setOnClickListener(v -> seekBar.setProgress(seekBar.getProgress() - 2));
        findViewById(R.id.butIncSpeed).setOnClickListener(v -> seekBar.setProgress(seekBar.getProgress() + 2));

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float playbackSpeed = (progress + 10) / 20.0f;
                if (progressChangedListener != null) {
                    progressChangedListener.accept(playbackSpeed);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    public void updateSpeed(float speedMultiplier) {
        seekBar.setProgress(Math.round((20 * speedMultiplier) - 10));
    }

    public void setProgressChangedListener(Consumer<Float> progressChangedListener) {
        this.progressChangedListener = progressChangedListener;
    }

    public float getCurrentSpeed() {
        return (seekBar.getProgress() + 10) / 20.0f;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        seekBar.setEnabled(enabled);
        findViewById(R.id.butDecSpeed).setEnabled(enabled);
        findViewById(R.id.butIncSpeed).setEnabled(enabled);
    }
}