Using builder to call PlaybackService

This commit is contained in:
ByteHamster 2018-05-06 19:44:07 +02:00
parent 4411b0ffaa
commit f6082f5808
8 changed files with 117 additions and 46 deletions

View File

@ -1,7 +1,6 @@
package de.danoeh.antennapod.activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.Log;
@ -14,6 +13,7 @@ import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.util.playback.ExternalMedia;
import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
/**
@ -35,14 +35,13 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
Log.d(TAG, "Received VIEW intent: " + intent.getData().getPath());
ExternalMedia media = new ExternalMedia(intent.getData().getPath(),
MediaType.AUDIO);
Intent launchIntent = new Intent(this, PlaybackService.class);
launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED,
true);
launchIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, false);
launchIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY,
true);
ContextCompat.startForegroundService(this, launchIntent);
new PlaybackServiceStarter(this, media)
.startWhenPrepared(true)
.shouldStream(false)
.prepareImmediately(true)
.start();
} else if (PlaybackService.isCasting()) {
Intent intent = PlaybackService.getPlayerActivityIntent(this);
if (intent.getComponent() != null &&

View File

@ -6,7 +6,6 @@ import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.WindowCompat;
import android.support.v7.app.ActionBar;
import android.util.Log;
@ -31,6 +30,7 @@ import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.util.gui.PictureInPictureUtil;
import de.danoeh.antennapod.core.util.playback.ExternalMedia;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import de.danoeh.antennapod.view.AspectRatioVideoView;
import java.lang.ref.WeakReference;
@ -83,14 +83,12 @@ public class VideoplayerActivity extends MediaplayerActivity {
Log.d(TAG, "Received VIEW intent: " + intent.getData().getPath());
ExternalMedia media = new ExternalMedia(intent.getData().getPath(),
MediaType.VIDEO);
Intent launchIntent = new Intent(this, PlaybackService.class);
launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED,
true);
launchIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, false);
launchIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY,
true);
ContextCompat.startForegroundService(this, launchIntent);
new PlaybackServiceStarter(this, media)
.startWhenPrepared(true)
.shouldStream(false)
.prepareImmediately(true)
.start();
} else if (PlaybackService.isCasting()) {
Intent intent = PlaybackService.getPlayerActivityIntent(this);
if (!intent.getComponent().getClassName().equals(VideoplayerActivity.class.getName())) {

View File

@ -6,6 +6,7 @@ import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import org.apache.commons.lang3.Validate;
import de.danoeh.antennapod.R;
@ -81,10 +82,16 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
}
} else { // media is downloaded
if (media.isCurrentlyPlaying()) {
PlaybackService.startIfNotRunning(context, media, true, false);
new PlaybackServiceStarter(context, media)
.startWhenPrepared(false)
.shouldStream(false)
.start();
context.sendBroadcast(new Intent(PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE));
} else if (media.isCurrentlyPaused()) {
PlaybackService.startIfNotRunning(context, media, true, false);
new PlaybackServiceStarter(context, media)
.startWhenPrepared(false)
.shouldStream(false)
.start();
context.sendBroadcast(new Intent(PlaybackService.ACTION_RESUME_PLAY_CURRENT_EPISODE));
} else {
DBTasks.playMedia(context, media, false, true, false);

View File

@ -244,7 +244,6 @@ public class ItemFragment extends Fragment implements OnSwipeGesture {
if (item.hasMedia()) {
FeedMedia media = item.getMedia();
if (!media.isDownloaded()) {
PlaybackService.startIfNotRunning(getActivity(), media, true, false);
DBTasks.playMedia(getActivity(), media, true, true, true);
((MainActivity) getActivity()).dismissChildFragment();
} else {

View File

@ -804,21 +804,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
};
public static void startIfNotRunning(final Context context, final Playable media, boolean startWhenPrepared, boolean shouldStream) {
if (!isRunning) {
startService(context, media, startWhenPrepared, shouldStream);
}
}
public static void startService(final Context context, final Playable media, boolean startWhenPrepared, boolean shouldStream) {
Intent launchIntent = new Intent(context, PlaybackService.class);
launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, startWhenPrepared);
launchIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, shouldStream);
launchIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY, true);
ContextCompat.startForegroundService(context, launchIntent);
}
private Playable getNextInQueue(final Playable currentMedia) {
if (!(currentMedia instanceof FeedMedia)) {
Log.d(TAG, "getNextInQueue(), but playable not an instance of FeedMedia, so not proceeding");

View File

@ -40,6 +40,7 @@ import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator;
import de.danoeh.antennapod.core.util.exception.MediaFileNotFoundException;
import de.danoeh.antennapod.core.util.flattr.FlattrUtils;
import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import static android.content.Context.MODE_PRIVATE;
@ -125,8 +126,13 @@ public final class DBTasks {
media);
}
}
// Needs to be called even if the service is already running to deliver the new media intent
PlaybackService.startService(context, media, startWhenPrepared, shouldStream);
new PlaybackServiceStarter(context, media)
.callEvenIfRunning(true)
.startWhenPrepared(startWhenPrepared)
.shouldStream(shouldStream)
.start();
if (showPlayer) {
// Launch media player
context.startActivity(PlaybackService.getPlayerActivityIntent(

View File

@ -221,18 +221,16 @@ public abstract class PlaybackController {
return null;
}
Intent serviceIntent = new Intent(activity, PlaybackService.class);
serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
serviceIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, false);
serviceIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY, true);
boolean fileExists = media.localFileAvailable();
boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream();
if (!fileExists && !lastIsStream && media instanceof FeedMedia) {
DBTasks.notifyMissingFeedMediaFile(activity, (FeedMedia) media);
}
serviceIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM,
lastIsStream || !fileExists);
return serviceIntent;
return new PlaybackServiceStarter(activity, media)
.startWhenPrepared(false)
.shouldStream(lastIsStream || !fileExists)
.getIntent();
}
@ -586,7 +584,10 @@ public abstract class PlaybackController {
public void playPause() {
if (playbackService == null) {
PlaybackService.startService(activity, media, true, false);
new PlaybackServiceStarter(activity, media)
.startWhenPrepared(true)
.streamIfLastWasStream()
.start();
Log.w(TAG, "Play/Pause button was pressed, but playbackservice was null!");
return;
}

View File

@ -0,0 +1,76 @@
package de.danoeh.antennapod.core.util.playback;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v4.content.ContextCompat;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
public class PlaybackServiceStarter {
private final Context context;
private final Playable media;
private boolean startWhenPrepared = false;
private boolean shouldStream = 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 Intent getIntent() {
Intent launchIntent = new Intent(context, PlaybackService.class);
launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, startWhenPrepared);
launchIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, shouldStream);
launchIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY, prepareImmediately);
return launchIntent;
}
public void start() {
if (PlaybackService.isRunning && !callEvenIfRunning) {
return;
}
ContextCompat.startForegroundService(context, getIntent());
}
}