summaryrefslogtreecommitdiff
path: root/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackServiceStarter.java
blob: 3efded9ed0a4da23f6ea57f10af9c62cfbc1a6dd (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
77
78
79
80
81
82
83
84
85
package de.danoeh.antennapod.core.util.playback;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import androidx.core.content.ContextCompat;

import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.model.playback.Playable;

public class PlaybackServiceStarter {
    private final Context context;
    private final Playable media;
    private boolean startWhenPrepared = false;
    private boolean shouldStream = false;
    private boolean shouldStreamThisTime = false;
    private boolean callEvenIfRunning = false;
    private boolean prepareImmediately = true;

    public PlaybackServiceStarter(Context context, Playable media) {
        this.context = context;
        this.media = media;
    }

    /**
     * Default value: false
     */
    public PlaybackServiceStarter shouldStream(boolean shouldStream) {
        this.shouldStream = shouldStream;
        return this;
    }

    public PlaybackServiceStarter streamIfLastWasStream() {
        boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream();
        return shouldStream(lastIsStream);
    }

    /**
     * Default value: false
     */
    public PlaybackServiceStarter startWhenPrepared(boolean startWhenPrepared) {
        this.startWhenPrepared = startWhenPrepared;
        return this;
    }

    /**
     * Default value: false
     */
    public PlaybackServiceStarter callEvenIfRunning(boolean callEvenIfRunning) {
        this.callEvenIfRunning = callEvenIfRunning;
        return this;
    }

    /**
     * Default value: true
     */
    public PlaybackServiceStarter prepareImmediately(boolean prepareImmediately) {
        this.prepareImmediately = prepareImmediately;
        return this;
    }

    public PlaybackServiceStarter shouldStreamThisTime(boolean shouldStreamThisTime) {
        this.shouldStreamThisTime = shouldStreamThisTime;
        return this;
    }

    public Intent getIntent() {
        Intent launchIntent = new Intent(context, PlaybackService.class);
        launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, (Parcelable) media);
        launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, startWhenPrepared);
        launchIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, shouldStream);
        launchIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY, prepareImmediately);
        launchIntent.putExtra(PlaybackService.EXTRA_ALLOW_STREAM_THIS_TIME, shouldStreamThisTime);

        return launchIntent;
    }

    public void start() {
        if (PlaybackService.isRunning && !callEvenIfRunning) {
            return;
        }
        ContextCompat.startForegroundService(context, getIntent());
    }
}