1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-08 16:30:34 +00:00

PlayerHolder: Separate holder and service event interface

Should make it easier to seperate the two further later, both of them
are only implemented by VideoDetailFragment anyway, which is kind of a
code smell!
This commit is contained in:
Profpatsch 2024-12-26 01:31:17 +01:00
parent 6a4aaba431
commit ce919215fb
5 changed files with 40 additions and 23 deletions

View File

@ -95,7 +95,8 @@ import org.schabi.newpipe.player.Player;
import org.schabi.newpipe.player.PlayerService; import org.schabi.newpipe.player.PlayerService;
import org.schabi.newpipe.player.PlayerType; import org.schabi.newpipe.player.PlayerType;
import org.schabi.newpipe.player.event.OnKeyDownListener; import org.schabi.newpipe.player.event.OnKeyDownListener;
import org.schabi.newpipe.player.event.PlayerServiceExtendedEventListener; import org.schabi.newpipe.player.event.PlayerHolderLifecycleEventListener;
import org.schabi.newpipe.player.event.PlayerServiceEventListener;
import org.schabi.newpipe.player.helper.PlayerHelper; import org.schabi.newpipe.player.helper.PlayerHelper;
import org.schabi.newpipe.player.helper.PlayerHolder; import org.schabi.newpipe.player.helper.PlayerHolder;
import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.PlayQueue;
@ -136,7 +137,8 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
public final class VideoDetailFragment public final class VideoDetailFragment
extends BaseStateFragment<StreamInfo> extends BaseStateFragment<StreamInfo>
implements BackPressable, implements BackPressable,
PlayerServiceExtendedEventListener, PlayerServiceEventListener,
PlayerHolderLifecycleEventListener,
OnKeyDownListener { OnKeyDownListener {
public static final String KEY_SWITCHING_PLAYERS = "switching_players"; public static final String KEY_SWITCHING_PLAYERS = "switching_players";
@ -392,7 +394,7 @@ public final class VideoDetailFragment
if (activity.isFinishing() && isPlayerAvailable() && player.videoPlayerSelected()) { if (activity.isFinishing() && isPlayerAvailable() && player.videoPlayerSelected()) {
playerHolder.stopService(); playerHolder.stopService();
} else { } else {
playerHolder.setListener(null); playerHolder.unsetListeners();
} }
PreferenceManager.getDefaultSharedPreferences(activity) PreferenceManager.getDefaultSharedPreferences(activity)
@ -660,7 +662,7 @@ public final class VideoDetailFragment
if (playerHolder.isNotBoundYet()) { if (playerHolder.isNotBoundYet()) {
setHeightThumbnail(); setHeightThumbnail();
} else { } else {
playerHolder.startService(false, this); playerHolder.startService(false, this, this);
} }
} }
@ -1051,7 +1053,7 @@ public final class VideoDetailFragment
// See UI changes while remote playQueue changes // See UI changes while remote playQueue changes
if (!isPlayerAvailable()) { if (!isPlayerAvailable()) {
playerHolder.startService(false, this); playerHolder.startService(false, this, this);
} else { } else {
// FIXME Workaround #7427 // FIXME Workaround #7427
player.setRecovery(); player.setRecovery();
@ -1114,7 +1116,7 @@ public final class VideoDetailFragment
private void openNormalBackgroundPlayer(final boolean append) { private void openNormalBackgroundPlayer(final boolean append) {
// See UI changes while remote playQueue changes // See UI changes while remote playQueue changes
if (!isPlayerAvailable()) { if (!isPlayerAvailable()) {
playerHolder.startService(false, this); playerHolder.startService(false, this, this);
} }
final PlayQueue queue = setupPlayQueueForIntent(append); final PlayQueue queue = setupPlayQueueForIntent(append);
@ -1128,7 +1130,7 @@ public final class VideoDetailFragment
private void openMainPlayer() { private void openMainPlayer() {
if (!isPlayerServiceAvailable()) { if (!isPlayerServiceAvailable()) {
playerHolder.startService(autoPlayEnabled, this); playerHolder.startService(autoPlayEnabled, this, this);
return; return;
} }
if (currentInfo == null) { if (currentInfo == null) {
@ -1385,7 +1387,9 @@ public final class VideoDetailFragment
// Rebound to the service if it was closed via notification or mini player // Rebound to the service if it was closed via notification or mini player
if (playerHolder.isNotBoundYet()) { if (playerHolder.isNotBoundYet()) {
playerHolder.startService( playerHolder.startService(
false, VideoDetailFragment.this); false,
VideoDetailFragment.this,
VideoDetailFragment.this);
} }
break; break;
} }

View File

@ -5,6 +5,7 @@ import com.google.android.exoplayer2.PlaybackParameters;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.PlayQueue;
/** Player-specific events like queue or progress updates. */
public interface PlayerEventListener { public interface PlayerEventListener {
void onQueueUpdate(PlayQueue queue); void onQueueUpdate(PlayQueue queue);
void onPlaybackUpdate(int state, int repeatMode, boolean shuffled, void onPlaybackUpdate(int state, int repeatMode, boolean shuffled,

View File

@ -2,7 +2,8 @@ package org.schabi.newpipe.player.event;
import org.schabi.newpipe.player.PlayerService; import org.schabi.newpipe.player.PlayerService;
public interface PlayerServiceExtendedEventListener extends PlayerServiceEventListener { /** Gets signalled if our PlayerHolder (dis)connects from the PlayerService. */
public interface PlayerHolderLifecycleEventListener {
void onServiceConnected(PlayerService playerService, void onServiceConnected(PlayerService playerService,
boolean playAfterConnect); boolean playAfterConnect);
void onServiceDisconnected(); void onServiceDisconnected();

View File

@ -2,6 +2,9 @@ package org.schabi.newpipe.player.event;
import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.PlaybackException;
/** {@link org.schabi.newpipe.player.event.PlayerEventListener} that also gets called for
* application-specific events like screen rotation or UI changes.
*/
public interface PlayerServiceEventListener extends PlayerEventListener { public interface PlayerServiceEventListener extends PlayerEventListener {
void onViewCreated(); void onViewCreated();

View File

@ -7,6 +7,7 @@ import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -20,7 +21,7 @@ import org.schabi.newpipe.player.Player;
import org.schabi.newpipe.player.PlayerService; import org.schabi.newpipe.player.PlayerService;
import org.schabi.newpipe.player.PlayerType; import org.schabi.newpipe.player.PlayerType;
import org.schabi.newpipe.player.event.PlayerServiceEventListener; import org.schabi.newpipe.player.event.PlayerServiceEventListener;
import org.schabi.newpipe.player.event.PlayerServiceExtendedEventListener; import org.schabi.newpipe.player.event.PlayerHolderLifecycleEventListener;
import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.PlayQueue;
/** Singleton that manages a `PlayerService` /** Singleton that manages a `PlayerService`
@ -41,7 +42,8 @@ public final class PlayerHolder {
private static final boolean DEBUG = MainActivity.DEBUG; private static final boolean DEBUG = MainActivity.DEBUG;
private static final String TAG = PlayerHolder.class.getSimpleName(); private static final String TAG = PlayerHolder.class.getSimpleName();
@Nullable private PlayerServiceExtendedEventListener listener; @Nullable private PlayerServiceEventListener listener;
@Nullable private PlayerHolderLifecycleEventListener holderListener;
private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection(); private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection();
private boolean bound; private boolean bound;
@ -102,16 +104,19 @@ public final class PlayerHolder {
return player.getPlayQueue().getIndex(); return player.getPlayQueue().getIndex();
} }
public void setListener(@Nullable final PlayerServiceExtendedEventListener newListener) { public void unsetListeners() {
listener = newListener; listener = null;
holderListener = null;
}
if (listener == null) { public void setListener(@NonNull final PlayerServiceEventListener newListener,
return; @NonNull final PlayerHolderLifecycleEventListener newHolderListener) {
} listener = newListener;
holderListener = newHolderListener;
// Force reload data from service // Force reload data from service
if (player != null) { if (player != null) {
listener.onServiceConnected(playerService, false); holderListener.onServiceConnected(playerService, false);
player.setFragmentListener(internalListener); player.setFragmentListener(internalListener);
} }
} }
@ -131,11 +136,14 @@ public final class PlayerHolder {
* If the service is already started, only set the listener. * If the service is already started, only set the listener.
* @param playAfterConnect If the service is started, start playing immediately * @param playAfterConnect If the service is started, start playing immediately
* @param newListener set this listener * @param newListener set this listener
* @param newHolderListener set this listener
* */ * */
public void startService(final boolean playAfterConnect, public void startService(final boolean playAfterConnect,
final PlayerServiceExtendedEventListener newListener) { final PlayerServiceEventListener newListener,
final PlayerHolderLifecycleEventListener newHolderListener
) {
final Context context = getCommonContext(); final Context context = getCommonContext();
setListener(newListener); setListener(newListener, newHolderListener);
if (bound) { if (bound) {
return; return;
} }
@ -182,8 +190,8 @@ public final class PlayerHolder {
} }
playerService = null; playerService = null;
player = null; player = null;
if (listener != null) { if (holderListener != null) {
listener.onServiceDisconnected(); holderListener.onServiceDisconnected();
} }
} }
} }
@ -212,8 +220,8 @@ public final class PlayerHolder {
playerService = localBinder.getService(); playerService = localBinder.getService();
player = playerService != null ? playerService.getPlayer() : null; player = playerService != null ? playerService.getPlayer() : null;
if (listener != null) { if (holderListener != null) {
listener.onServiceConnected(playerService, playAfterConnect); holderListener.onServiceConnected(playerService, playAfterConnect);
} }
if (player != null) { if (player != null) {
player.setFragmentListener(internalListener); player.setFragmentListener(internalListener);