mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-11-04 09:13:00 +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:
		@@ -95,7 +95,8 @@ import org.schabi.newpipe.player.Player;
 | 
			
		||||
import org.schabi.newpipe.player.PlayerService;
 | 
			
		||||
import org.schabi.newpipe.player.PlayerType;
 | 
			
		||||
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.PlayerHolder;
 | 
			
		||||
import org.schabi.newpipe.player.playqueue.PlayQueue;
 | 
			
		||||
@@ -136,7 +137,8 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
 | 
			
		||||
public final class VideoDetailFragment
 | 
			
		||||
        extends BaseStateFragment<StreamInfo>
 | 
			
		||||
        implements BackPressable,
 | 
			
		||||
        PlayerServiceExtendedEventListener,
 | 
			
		||||
        PlayerServiceEventListener,
 | 
			
		||||
        PlayerHolderLifecycleEventListener,
 | 
			
		||||
        OnKeyDownListener {
 | 
			
		||||
    public static final String KEY_SWITCHING_PLAYERS = "switching_players";
 | 
			
		||||
 | 
			
		||||
@@ -392,7 +394,7 @@ public final class VideoDetailFragment
 | 
			
		||||
        if (activity.isFinishing() && isPlayerAvailable() && player.videoPlayerSelected()) {
 | 
			
		||||
            playerHolder.stopService();
 | 
			
		||||
        } else {
 | 
			
		||||
            playerHolder.setListener(null);
 | 
			
		||||
            playerHolder.unsetListeners();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        PreferenceManager.getDefaultSharedPreferences(activity)
 | 
			
		||||
@@ -660,7 +662,7 @@ public final class VideoDetailFragment
 | 
			
		||||
        if (playerHolder.isNotBoundYet()) {
 | 
			
		||||
            setHeightThumbnail();
 | 
			
		||||
        } 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
 | 
			
		||||
        if (!isPlayerAvailable()) {
 | 
			
		||||
            playerHolder.startService(false, this);
 | 
			
		||||
            playerHolder.startService(false, this, this);
 | 
			
		||||
        } else {
 | 
			
		||||
            // FIXME Workaround #7427
 | 
			
		||||
            player.setRecovery();
 | 
			
		||||
@@ -1114,7 +1116,7 @@ public final class VideoDetailFragment
 | 
			
		||||
    private void openNormalBackgroundPlayer(final boolean append) {
 | 
			
		||||
        // See UI changes while remote playQueue changes
 | 
			
		||||
        if (!isPlayerAvailable()) {
 | 
			
		||||
            playerHolder.startService(false, this);
 | 
			
		||||
            playerHolder.startService(false, this, this);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        final PlayQueue queue = setupPlayQueueForIntent(append);
 | 
			
		||||
@@ -1128,7 +1130,7 @@ public final class VideoDetailFragment
 | 
			
		||||
 | 
			
		||||
    private void openMainPlayer() {
 | 
			
		||||
        if (!isPlayerServiceAvailable()) {
 | 
			
		||||
            playerHolder.startService(autoPlayEnabled, this);
 | 
			
		||||
            playerHolder.startService(autoPlayEnabled, this, this);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (currentInfo == null) {
 | 
			
		||||
@@ -1385,7 +1387,9 @@ public final class VideoDetailFragment
 | 
			
		||||
                        // Rebound to the service if it was closed via notification or mini player
 | 
			
		||||
                        if (playerHolder.isNotBoundYet()) {
 | 
			
		||||
                            playerHolder.startService(
 | 
			
		||||
                                    false, VideoDetailFragment.this);
 | 
			
		||||
                                    false,
 | 
			
		||||
                                    VideoDetailFragment.this,
 | 
			
		||||
                                    VideoDetailFragment.this);
 | 
			
		||||
                        }
 | 
			
		||||
                        break;
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ import com.google.android.exoplayer2.PlaybackParameters;
 | 
			
		||||
import org.schabi.newpipe.extractor.stream.StreamInfo;
 | 
			
		||||
import org.schabi.newpipe.player.playqueue.PlayQueue;
 | 
			
		||||
 | 
			
		||||
/** Player-specific events like queue or progress updates. */
 | 
			
		||||
public interface PlayerEventListener {
 | 
			
		||||
    void onQueueUpdate(PlayQueue queue);
 | 
			
		||||
    void onPlaybackUpdate(int state, int repeatMode, boolean shuffled,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,8 @@ package org.schabi.newpipe.player.event;
 | 
			
		||||
 | 
			
		||||
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,
 | 
			
		||||
                            boolean playAfterConnect);
 | 
			
		||||
    void onServiceDisconnected();
 | 
			
		||||
@@ -2,6 +2,9 @@ package org.schabi.newpipe.player.event;
 | 
			
		||||
 | 
			
		||||
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 {
 | 
			
		||||
    void onViewCreated();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import android.content.ServiceConnection;
 | 
			
		||||
import android.os.IBinder;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
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.PlayerType;
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
/** Singleton that manages a `PlayerService`
 | 
			
		||||
@@ -41,7 +42,8 @@ public final class PlayerHolder {
 | 
			
		||||
    private static final boolean DEBUG = MainActivity.DEBUG;
 | 
			
		||||
    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 boolean bound;
 | 
			
		||||
@@ -102,16 +104,19 @@ public final class PlayerHolder {
 | 
			
		||||
        return player.getPlayQueue().getIndex();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setListener(@Nullable final PlayerServiceExtendedEventListener newListener) {
 | 
			
		||||
        listener = newListener;
 | 
			
		||||
    public void unsetListeners() {
 | 
			
		||||
        listener = null;
 | 
			
		||||
        holderListener = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        if (listener == null) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    public void setListener(@NonNull final PlayerServiceEventListener newListener,
 | 
			
		||||
                            @NonNull final PlayerHolderLifecycleEventListener newHolderListener) {
 | 
			
		||||
        listener = newListener;
 | 
			
		||||
        holderListener = newHolderListener;
 | 
			
		||||
 | 
			
		||||
        // Force reload data from service
 | 
			
		||||
        if (player != null) {
 | 
			
		||||
            listener.onServiceConnected(playerService, false);
 | 
			
		||||
            holderListener.onServiceConnected(playerService, false);
 | 
			
		||||
            player.setFragmentListener(internalListener);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -131,11 +136,14 @@ public final class PlayerHolder {
 | 
			
		||||
     * If the service is already started, only set the listener.
 | 
			
		||||
     * @param playAfterConnect If the service is started, start playing immediately
 | 
			
		||||
     * @param newListener set this listener
 | 
			
		||||
     * @param newHolderListener set this listener
 | 
			
		||||
     * */
 | 
			
		||||
    public void startService(final boolean playAfterConnect,
 | 
			
		||||
                             final PlayerServiceExtendedEventListener newListener) {
 | 
			
		||||
                             final PlayerServiceEventListener newListener,
 | 
			
		||||
                             final PlayerHolderLifecycleEventListener newHolderListener
 | 
			
		||||
    ) {
 | 
			
		||||
        final Context context = getCommonContext();
 | 
			
		||||
        setListener(newListener);
 | 
			
		||||
        setListener(newListener, newHolderListener);
 | 
			
		||||
        if (bound) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -182,8 +190,8 @@ public final class PlayerHolder {
 | 
			
		||||
            }
 | 
			
		||||
            playerService = null;
 | 
			
		||||
            player = null;
 | 
			
		||||
            if (listener != null) {
 | 
			
		||||
                listener.onServiceDisconnected();
 | 
			
		||||
            if (holderListener != null) {
 | 
			
		||||
                holderListener.onServiceDisconnected();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -212,8 +220,8 @@ public final class PlayerHolder {
 | 
			
		||||
            playerService = localBinder.getService();
 | 
			
		||||
            player = playerService != null ? playerService.getPlayer() : null;
 | 
			
		||||
 | 
			
		||||
            if (listener != null) {
 | 
			
		||||
                listener.onServiceConnected(playerService, playAfterConnect);
 | 
			
		||||
            if (holderListener != null) {
 | 
			
		||||
                holderListener.onServiceConnected(playerService, playAfterConnect);
 | 
			
		||||
            }
 | 
			
		||||
            if (player != null) {
 | 
			
		||||
                player.setFragmentListener(internalListener);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user