Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlayerHolder refactor #11867

Open
wants to merge 6 commits into
base: refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
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;
Expand Down Expand Up @@ -136,7 +137,8 @@
public final class VideoDetailFragment
extends BaseStateFragment<StreamInfo>
implements BackPressable,
PlayerServiceExtendedEventListener,
PlayerServiceEventListener,
PlayerHolderLifecycleEventListener,
OnKeyDownListener {
public static final String KEY_SWITCHING_PLAYERS = "switching_players";

Expand Down Expand Up @@ -234,10 +236,9 @@ public final class VideoDetailFragment
// Service management
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onServiceConnected(final Player connectedPlayer,
final PlayerService connectedPlayerService,
public void onServiceConnected(final PlayerService connectedPlayerService,
final boolean playAfterConnect) {
player = connectedPlayer;
player = connectedPlayerService.getPlayer();
playerService = connectedPlayerService;

// It will do nothing if the player is not in fullscreen mode
Expand Down Expand Up @@ -393,7 +394,7 @@ public void onDestroy() {
if (activity.isFinishing() && isPlayerAvailable() && player.videoPlayerSelected()) {
playerHolder.stopService();
} else {
playerHolder.setListener(null);
playerHolder.unsetListeners();
}

PreferenceManager.getDefaultSharedPreferences(activity)
Expand Down Expand Up @@ -658,10 +659,10 @@ protected void initListeners() {
});

setupBottomPlayer();
if (!playerHolder.isBound()) {
if (playerHolder.isNotBoundYet()) {
setHeightThumbnail();
} else {
playerHolder.startService(false, this);
playerHolder.startService(false, this, this);
}
}

Expand Down Expand Up @@ -1052,7 +1053,7 @@ private void openPopupPlayer(final boolean append) {

// See UI changes while remote playQueue changes
if (!isPlayerAvailable()) {
playerHolder.startService(false, this);
playerHolder.startService(false, this, this);
} else {
// FIXME Workaround #7427
player.setRecovery();
Expand Down Expand Up @@ -1115,7 +1116,7 @@ public void openVideoPlayerAutoFullscreen() {
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);
Expand All @@ -1129,7 +1130,7 @@ private void openNormalBackgroundPlayer(final boolean append) {

private void openMainPlayer() {
if (!isPlayerServiceAvailable()) {
playerHolder.startService(autoPlayEnabled, this);
playerHolder.startService(autoPlayEnabled, this, this);
return;
}
if (currentInfo == null) {
Expand Down Expand Up @@ -1384,9 +1385,11 @@ public void onReceive(final Context context, final Intent intent) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
// Rebound to the service if it was closed via notification or mini player
if (!playerHolder.isBound()) {
if (playerHolder.isNotBoundYet()) {
playerHolder.startService(
false, VideoDetailFragment.this);
false,
VideoDetailFragment.this,
VideoDetailFragment.this);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,16 @@ public void onServiceDisconnected(final ComponentName name) {
}

@Override
public void onServiceConnected(final ComponentName name, final IBinder service) {
public void onServiceConnected(final ComponentName name, final IBinder binder) {
Log.d(TAG, "Player service is connected");

if (service instanceof PlayerService.LocalBinder) {
player = ((PlayerService.LocalBinder) service).getPlayer();
if (binder instanceof PlayerService.LocalBinder localBinder) {
final @Nullable PlayerService s = localBinder.getService();
if (s == null) {
player = null;
} else {
player = s.getPlayer();
}
}

if (player == null || player.getPlayQueue() == null || player.exoPlayerIsNull()) {
Expand Down
21 changes: 15 additions & 6 deletions app/src/main/java/org/schabi/newpipe/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
import org.schabi.newpipe.util.ThemeHelper;
Expand All @@ -36,7 +38,9 @@


/**
* One service for all players.
* One background service for our player. Even though the player has multiple UIs
* (e.g. the audio-only UI, the main UI, the pulldown-menu UI),
* this allows us to keep playing even when switching between the different UIs.
*/
public final class PlayerService extends Service {
private static final String TAG = PlayerService.class.getSimpleName();
Expand All @@ -46,6 +50,9 @@ public final class PlayerService extends Service {

private final IBinder mBinder = new PlayerService.LocalBinder(this);

public Player getPlayer() {
return player;
}

/*//////////////////////////////////////////////////////////////////////////
// Service's LifeCycle
Expand Down Expand Up @@ -167,19 +174,21 @@ public IBinder onBind(final Intent intent) {
return mBinder;
}

/** Allows us this {@link org.schabi.newpipe.player.PlayerService} over the Service boundary
* back to our {@link org.schabi.newpipe.player.helper.PlayerHolder}.
*/
public static class LocalBinder extends Binder {
private final WeakReference<PlayerService> playerService;

LocalBinder(final PlayerService playerService) {
this.playerService = new WeakReference<>(playerService);
}

public PlayerService getService() {
/** Get the PlayerService object itself.
* @return this
* */
public @Nullable PlayerService getService() {
return playerService.get();
}

public Player getPlayer() {
return playerService.get().player;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.schabi.newpipe.player.event;

import org.schabi.newpipe.player.PlayerService;

/** Gets signalled if our PlayerHolder (dis)connects from the PlayerService. */
public interface PlayerHolderLifecycleEventListener {
void onServiceConnected(PlayerService playerService,
boolean playAfterConnect);
void onServiceDisconnected();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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();

Expand Down

This file was deleted.

Loading
Loading