Merge pull request #3967 from ByteHamster/make-controller-smaller

Removed unused code from PlaybackController
This commit is contained in:
H. Lehmann 2020-03-26 23:41:30 +01:00 committed by GitHub
commit ddc8cc7cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 38 additions and 114 deletions

View File

@ -75,7 +75,7 @@ public class SpeedChangeTest {
EspressoTestUtils.tryKillPlaybackService(); EspressoTestUtils.tryKillPlaybackService();
activityRule.launchActivity(new Intent().putExtra(MainActivity.EXTRA_OPEN_PLAYER, true)); activityRule.launchActivity(new Intent().putExtra(MainActivity.EXTRA_OPEN_PLAYER, true));
controller = new PlaybackController(activityRule.getActivity(), true); controller = new PlaybackController(activityRule.getActivity());
controller.init(); controller.init();
controller.getMedia(); // To load media controller.getMedia(); // To load media
} }

View File

@ -93,7 +93,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
private Disposable disposable; private Disposable disposable;
private PlaybackController newPlaybackController() { private PlaybackController newPlaybackController() {
return new PlaybackController(this, false) { return new PlaybackController(this) {
@Override @Override
public void setupGUI() { public void setupGUI() {
@ -150,11 +150,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
MediaplayerActivity.this.onAwaitingVideoSurface(); MediaplayerActivity.this.onAwaitingVideoSurface();
} }
@Override
public void onServiceQueried() {
MediaplayerActivity.this.onServiceQueried();
}
@Override @Override
public void onShutdownNotification() { public void onShutdownNotification() {
finish(); finish();
@ -197,10 +192,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
updatePlaybackSpeedButtonText(); updatePlaybackSpeedButtonText();
} }
private void onServiceQueried() {
supportInvalidateOptionsMenu();
}
void chooseTheme() { void chooseTheme() {
setTheme(UserPreferences.getTheme()); setTheme(UserPreferences.getTheme());
} }
@ -648,31 +639,33 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
private float prog; private float prog;
@Override @Override
public void onProgressChanged(SeekBar seekBar,int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (controller == null || txtvLength == null) { if (controller == null || txtvLength == null) {
return; return;
} }
prog = controller.onSeekBarProgressChanged(seekBar, progress, fromUser, txtvPosition); if (fromUser) {
if (showTimeLeft && prog != 0) { prog = progress / ((float) seekBar.getMax());
int duration = controller.getDuration(); int duration = controller.getDuration();
TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier()); TimeSpeedConverter converter = new TimeSpeedConverter(controller.getCurrentPlaybackSpeedMultiplier());
int timeLeft = converter.convert(duration - (int) (prog * duration)); int position = converter.convert((int) (prog * duration));
String length = "-" + Converter.getDurationStringLong(timeLeft); txtvPosition.setText(Converter.getDurationStringLong(position));
txtvLength.setText(length);
if (showTimeLeft) {
int timeLeft = converter.convert(duration - (int) (prog * duration));
txtvLength.setText("-" + Converter.getDurationStringLong(timeLeft));
}
} }
} }
@Override @Override
public void onStartTrackingTouch(SeekBar seekBar) { public void onStartTrackingTouch(SeekBar seekBar) {
if (controller != null) {
controller.onSeekBarStartTrackingTouch(seekBar);
}
} }
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {
if (controller != null) { if (controller != null) {
controller.onSeekBarStopTrackingTouch(seekBar, prog); controller.seekTo((int) (prog * controller.getDuration()));
} }
} }

View File

@ -247,11 +247,7 @@ public class VideoplayerActivity extends MediaplayerActivity {
Log.d(TAG, "Videoview holder created"); Log.d(TAG, "Videoview holder created");
videoSurfaceCreated = true; videoSurfaceCreated = true;
if (controller != null && controller.getStatus() == PlayerStatus.PLAYING) { if (controller != null && controller.getStatus() == PlayerStatus.PLAYING) {
if (controller.serviceAvailable()) { controller.setVideoSurface(holder);
controller.setVideoSurface(holder);
} else {
Log.e(TAG, "Couldn't attach surface to mediaplayer - reference to service was null");
}
} }
setupVideoAspectRatio(); setupVideoAspectRatio();
} }

View File

@ -39,7 +39,7 @@ public class PlaybackControlsDialog extends DialogFragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
controller = new PlaybackController(getActivity(), false) { controller = new PlaybackController(getActivity()) {
@Override @Override
public void setupGUI() { public void setupGUI() {
setupUi(); setupUi();

View File

@ -49,7 +49,7 @@ public class SleepTimerDialog extends DialogFragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
controller = new PlaybackController(getActivity(), false) { controller = new PlaybackController(getActivity()) {
@Override @Override
public void setupGUI() { public void setupGUI() {
updateTime(); updateTime();

View File

@ -246,7 +246,7 @@ public class AudioPlayerFragment extends Fragment implements
} }
private PlaybackController newPlaybackController() { private PlaybackController newPlaybackController() {
return new PlaybackController(getActivity(), false) { return new PlaybackController(getActivity()) {
@Override @Override
public void setupGUI() { public void setupGUI() {

View File

@ -70,7 +70,7 @@ public class ChaptersFragment extends Fragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
controller = new PlaybackController(getActivity(), false) { controller = new PlaybackController(getActivity()) {
@Override @Override
public boolean loadMediaInfo() { public boolean loadMediaInfo() {
ChaptersFragment.this.loadMediaInfo(); ChaptersFragment.this.loadMediaInfo();

View File

@ -97,7 +97,7 @@ public class CoverFragment extends Fragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
controller = new PlaybackController(getActivity(), false) { controller = new PlaybackController(getActivity()) {
@Override @Override
public boolean loadMediaInfo() { public boolean loadMediaInfo() {
CoverFragment.this.loadMediaInfo(); CoverFragment.this.loadMediaInfo();

View File

@ -90,7 +90,7 @@ public class ExternalPlayerFragment extends Fragment {
} }
private PlaybackController setupPlaybackController() { private PlaybackController setupPlaybackController() {
return new PlaybackController(getActivity(), true) { return new PlaybackController(getActivity()) {
@Override @Override
public void onPositionObserverUpdate() { public void onPositionObserverUpdate() {

View File

@ -135,7 +135,7 @@ public class ItemDescriptionFragment extends Fragment {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
controller = new PlaybackController(getActivity(), false) { controller = new PlaybackController(getActivity()) {
@Override @Override
public boolean loadMediaInfo() { public boolean loadMediaInfo() {
if (getMedia() == null) { if (getMedia() == null) {

View File

@ -175,7 +175,7 @@ public class ItemFragment extends Fragment {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
controller = new PlaybackController(getActivity(), false); controller = new PlaybackController(getActivity());
controller.init(); controller.init();
} }

View File

@ -11,35 +11,28 @@ import android.content.res.TypedArray;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.SeekBar; import androidx.annotation.NonNull;
import android.widget.TextView; import androidx.core.content.ContextCompat;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.util.ThemeUtils;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.event.ServiceEvent; import de.danoeh.antennapod.core.event.ServiceEvent;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils; import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer; import de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer;
import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.Optional; import de.danoeh.antennapod.core.util.Optional;
import de.danoeh.antennapod.core.util.TimeSpeedConverter; import de.danoeh.antennapod.core.util.ThemeUtils;
import de.danoeh.antennapod.core.util.playback.Playable.PlayableUtils; import de.danoeh.antennapod.core.util.playback.Playable.PlayableUtils;
import io.reactivex.Maybe; import io.reactivex.Maybe;
import io.reactivex.MaybeOnSubscribe; import io.reactivex.MaybeOnSubscribe;
@ -51,6 +44,8 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode; import org.greenrobot.eventbus.ThreadMode;
import java.util.concurrent.ScheduledThreadPoolExecutor;
/** /**
* Communicates with the playback service. GUI classes should use this class to * Communicates with the playback service. GUI classes should use this class to
* control playback instead of communicating with the PlaybackService directly. * control playback instead of communicating with the PlaybackService directly.
@ -78,16 +73,9 @@ public class PlaybackController {
private Disposable serviceBinder; private Disposable serviceBinder;
private Disposable mediaLoader; private Disposable mediaLoader;
/** public PlaybackController(@NonNull Activity activity) {
* True if controller should reinit playback service if 'pause' button is
* pressed.
*/
private final boolean reinitOnPause;
public PlaybackController(@NonNull Activity activity, boolean reinitOnPause) {
this.activity = activity; this.activity = activity;
this.reinitOnPause = reinitOnPause;
schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOLSIZE, schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOLSIZE,
r -> { r -> {
Thread t = new Thread(r); Thread t = new Thread(r);
@ -276,7 +264,7 @@ public class PlaybackController {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received statusUpdate Intent."); Log.d(TAG, "Received statusUpdate Intent.");
if (isConnectedToPlaybackService()) { if (playbackService != null) {
PlaybackServiceMediaPlayer.PSMPInfo info = playbackService.getPSMPInfo(); PlaybackServiceMediaPlayer.PSMPInfo info = playbackService.getPSMPInfo();
status = info.playerStatus; status = info.playerStatus;
media = info.playable; media = info.playable;
@ -307,7 +295,7 @@ public class PlaybackController {
onBufferUpdate(progress); onBufferUpdate(progress);
break; break;
case PlaybackService.NOTIFICATION_TYPE_RELOAD: case PlaybackService.NOTIFICATION_TYPE_RELOAD:
if (!isConnectedToPlaybackService()) { if (playbackService == null) {
bindToService(); bindToService();
return; return;
} }
@ -343,7 +331,7 @@ public class PlaybackController {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (isConnectedToPlaybackService()) { if (playbackService != null) {
if (TextUtils.equals(intent.getAction(), if (TextUtils.equals(intent.getAction(),
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) { PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
release(); release();
@ -484,7 +472,6 @@ public class PlaybackController {
PlaybackServiceMediaPlayer.PSMPInfo info = playbackService.getPSMPInfo(); PlaybackServiceMediaPlayer.PSMPInfo info = playbackService.getPSMPInfo();
status = info.playerStatus; status = info.playerStatus;
media = info.playable; media = info.playable;
onServiceQueried();
setupGUI(); setupGUI();
handleStatus(); handleStatus();
@ -497,41 +484,6 @@ public class PlaybackController {
} }
} }
public void onServiceQueried() {}
/**
* Should be used by classes which implement the OnSeekBarChanged interface.
*/
public float onSeekBarProgressChanged(SeekBar seekBar, int progress,
boolean fromUser, TextView txtvPosition) {
if (fromUser && playbackService != null && media != null) {
float prog = progress / ((float) seekBar.getMax());
int duration = media.getDuration();
TimeSpeedConverter converter = new TimeSpeedConverter(playbackService.getCurrentPlaybackSpeed());
int position = converter.convert((int) (prog * duration));
txtvPosition.setText(Converter.getDurationStringLong(position));
return prog;
}
return 0;
}
/**
* Should be used by classes which implement the OnSeekBarChanged interface.
*/
public void onSeekBarStartTrackingTouch(SeekBar seekBar) {
// interrupt position Observer, restart later
}
/**
* Should be used by classes which implement the OnSeekBarChanged interface.
*/
public void onSeekBarStopTrackingTouch(SeekBar seekBar, float prog) {
if (playbackService != null && media != null) {
seekTo((int) (prog * getDuration()));
}
}
/** /**
* Should be implemented by classes that show a video. The default implementation * Should be implemented by classes that show a video. The default implementation
* does nothing * does nothing
@ -553,19 +505,14 @@ public class PlaybackController {
} }
switch (status) { switch (status) {
case PLAYING: case PLAYING:
playbackService.pause(true, reinitOnPause); playbackService.pause(true, false);
break; break;
case PAUSED: case PAUSED:
case PREPARED: case PREPARED:
playbackService.resume(); playbackService.resume();
break; break;
case PREPARING: case PREPARING:
playbackService.setStartWhenPrepared(!playbackService playbackService.setStartWhenPrepared(!playbackService.isStartWhenPrepared());
.isStartWhenPrepared());
if (reinitOnPause
&& !playbackService.isStartWhenPrepared()) {
playbackService.reinit();
}
break; break;
case INITIALIZED: case INITIALIZED:
playbackService.setStartWhenPrepared(true); playbackService.setStartWhenPrepared(true);
@ -574,10 +521,6 @@ public class PlaybackController {
} }
} }
public boolean serviceAvailable() {
return playbackService != null;
}
public int getPosition() { public int getPosition() {
if (playbackService != null) { if (playbackService != null) {
return playbackService.getCurrentPosition(); return playbackService.getCurrentPosition();
@ -665,6 +608,7 @@ public class PlaybackController {
onPlaybackSpeedChange(); onPlaybackSpeedChange();
} }
} }
public void setSkipSilence(boolean skipSilence) { public void setSkipSilence(boolean skipSilence) {
if (playbackService != null) { if (playbackService != null) {
playbackService.skipSilence(skipSilence); playbackService.skipSilence(skipSilence);
@ -691,7 +635,7 @@ public class PlaybackController {
} }
public void setDownmix(boolean enable) { public void setDownmix(boolean enable) {
if(playbackService != null) { if (playbackService != null) {
playbackService.setDownmix(enable); playbackService.setDownmix(enable);
} }
} }
@ -714,15 +658,6 @@ public class PlaybackController {
} }
} }
/**
* Returns true if PlaybackController can communicate with the playback
* service.
*/
private boolean isConnectedToPlaybackService() {
return playbackService != null;
}
public void notifyVideoSurfaceAbandoned() { public void notifyVideoSurfaceAbandoned() {
if (playbackService != null) { if (playbackService != null) {
playbackService.notifyVideoSurfaceAbandoned(); playbackService.notifyVideoSurfaceAbandoned();