1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-06-27 15:43:07 +00:00

PlayerHolder/PlayerService: inline & remove duplicate player passing

The player in playerHolder is exactly the player inside the
`PlayerService`, which in turn is exactly passed through the IBinder
interface. Thus we don’t have to pass both.

Instead add `PlayerService.getPlayer()`.

Also inline a few methods of `PlayerHolder` and simplify.
This commit is contained in:
Profpatsch 2024-12-25 22:14:22 +01:00
parent fbafdeb2ca
commit ef3c76645f
5 changed files with 48 additions and 40 deletions

View File

@ -234,10 +234,9 @@ public final class VideoDetailFragment
// Service management // Service management
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@Override @Override
public void onServiceConnected(final Player connectedPlayer, public void onServiceConnected(final PlayerService connectedPlayerService,
final PlayerService connectedPlayerService,
final boolean playAfterConnect) { final boolean playAfterConnect) {
player = connectedPlayer; player = connectedPlayerService.getPlayer();
playerService = connectedPlayerService; playerService = connectedPlayerService;
// It will do nothing if the player is not in fullscreen mode // It will do nothing if the player is not in fullscreen mode

View File

@ -217,11 +217,16 @@ public final class PlayQueueActivity extends AppCompatActivity
} }
@Override @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"); Log.d(TAG, "Player service is connected");
if (service instanceof PlayerService.LocalBinder) { if (binder instanceof PlayerService.LocalBinder localBinder) {
player = ((PlayerService.LocalBinder) service).getPlayer(); final @Nullable PlayerService s = localBinder.getService();
if (s == null) {
player = null;
} else {
player = s.getPlayer();
}
} }
if (player == null || player.getPlayQueue() == null || player.exoPlayerIsNull()) { if (player == null || player.getPlayQueue() == null || player.exoPlayerIsNull()) {

View File

@ -28,6 +28,8 @@ import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable;
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.player.notification.NotificationPlayerUi; import org.schabi.newpipe.player.notification.NotificationPlayerUi;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
@ -36,7 +38,9 @@ import java.lang.ref.WeakReference;
/** /**
* 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 { public final class PlayerService extends Service {
private static final String TAG = PlayerService.class.getSimpleName(); private static final String TAG = PlayerService.class.getSimpleName();
@ -46,6 +50,9 @@ public final class PlayerService extends Service {
private final IBinder mBinder = new PlayerService.LocalBinder(this); private final IBinder mBinder = new PlayerService.LocalBinder(this);
public Player getPlayer() {
return player;
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Service's LifeCycle // Service's LifeCycle
@ -167,6 +174,9 @@ public final class PlayerService extends Service {
return mBinder; 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 { public static class LocalBinder extends Binder {
private final WeakReference<PlayerService> playerService; private final WeakReference<PlayerService> playerService;
@ -174,12 +184,11 @@ public final class PlayerService extends Service {
this.playerService = new WeakReference<>(playerService); this.playerService = new WeakReference<>(playerService);
} }
public PlayerService getService() { /** Get the PlayerService object itself.
* @return this
* */
public @Nullable PlayerService getService() {
return playerService.get(); return playerService.get();
} }
public Player getPlayer() {
return playerService.get().player;
}
} }
} }

View File

@ -1,11 +1,9 @@
package org.schabi.newpipe.player.event; package org.schabi.newpipe.player.event;
import org.schabi.newpipe.player.PlayerService; import org.schabi.newpipe.player.PlayerService;
import org.schabi.newpipe.player.Player;
public interface PlayerServiceExtendedEventListener extends PlayerServiceEventListener { public interface PlayerServiceExtendedEventListener extends PlayerServiceEventListener {
void onServiceConnected(Player player, void onServiceConnected(PlayerService playerService,
PlayerService playerService,
boolean playAfterConnect); boolean playAfterConnect);
void onServiceDisconnected(); void onServiceDisconnected();
} }

View File

@ -43,6 +43,7 @@ public final class PlayerHolder {
private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection(); private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection();
private boolean bound; private boolean bound;
@Nullable private PlayerService playerService; @Nullable private PlayerService playerService;
@Nullable private Player player; @Nullable private Player player;
@ -108,13 +109,16 @@ public final class PlayerHolder {
// Force reload data from service // Force reload data from service
if (player != null) { if (player != null) {
listener.onServiceConnected(player, playerService, false); listener.onServiceConnected(playerService, false);
startPlayerListener(); player.setFragmentListener(internalListener);
} }
} }
// helper to handle context in common place as using the same /** Helper to handle context in common place as using the same
// context to bind/unbind a service is crucial * context to bind/unbind a service is crucial.
*
* @return the common context
* */
private Context getCommonContext() { private Context getCommonContext() {
return App.getInstance(); return App.getInstance();
} }
@ -131,7 +135,7 @@ public final class PlayerHolder {
// bound twice. Prevent it with unbinding first // bound twice. Prevent it with unbinding first
unbind(context); unbind(context);
ContextCompat.startForegroundService(context, new Intent(context, PlayerService.class)); ContextCompat.startForegroundService(context, new Intent(context, PlayerService.class));
serviceConnection.doPlayAfterConnect(playAfterConnect); serviceConnection.playAfterConnect = playAfterConnect;
bind(context); bind(context);
} }
@ -145,10 +149,6 @@ public final class PlayerHolder {
private boolean playAfterConnect = false; private boolean playAfterConnect = false;
public void doPlayAfterConnect(final boolean playAfterConnection) {
this.playAfterConnect = playAfterConnection;
}
@Override @Override
public void onServiceDisconnected(final ComponentName compName) { public void onServiceDisconnected(final ComponentName compName) {
if (DEBUG) { if (DEBUG) {
@ -167,14 +167,21 @@ public final class PlayerHolder {
final PlayerService.LocalBinder localBinder = (PlayerService.LocalBinder) service; final PlayerService.LocalBinder localBinder = (PlayerService.LocalBinder) service;
playerService = localBinder.getService(); playerService = localBinder.getService();
player = localBinder.getPlayer(); player = playerService != null ? playerService.getPlayer() : null;
if (listener != null) { if (listener != null) {
listener.onServiceConnected(player, playerService, playAfterConnect); listener.onServiceConnected(playerService, playAfterConnect);
}
if (player != null) {
player.setFragmentListener(internalListener);
} }
startPlayerListener();
} }
} }
/** Connect to (and if needed start) the {@link PlayerService}
* and bind {@link PlayerServiceConnection} to it.
* @param context common holder context
* */
private void bind(final Context context) { private void bind(final Context context) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "bind() called"); Log.d(TAG, "bind() called");
@ -196,7 +203,9 @@ public final class PlayerHolder {
if (bound) { if (bound) {
context.unbindService(serviceConnection); context.unbindService(serviceConnection);
bound = false; bound = false;
stopPlayerListener(); if (player != null) {
player.removeFragmentListener(internalListener);
}
playerService = null; playerService = null;
player = null; player = null;
if (listener != null) { if (listener != null) {
@ -205,18 +214,6 @@ public final class PlayerHolder {
} }
} }
private void startPlayerListener() {
if (player != null) {
player.setFragmentListener(internalListener);
}
}
private void stopPlayerListener() {
if (player != null) {
player.removeFragmentListener(internalListener);
}
}
private final PlayerServiceEventListener internalListener = private final PlayerServiceEventListener internalListener =
new PlayerServiceEventListener() { new PlayerServiceEventListener() {
@Override @Override