Skip to content

Commit b109e4d

Browse files
authored
Merge pull request #11867 from Profpatsch/player-holder-refactor
PlayerHolder refactor
2 parents 3e6e980 + 137ade2 commit b109e4d

File tree

8 files changed

+143
-99
lines changed

8 files changed

+143
-99
lines changed

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
import org.schabi.newpipe.player.PlayerService;
9696
import org.schabi.newpipe.player.PlayerType;
9797
import org.schabi.newpipe.player.event.OnKeyDownListener;
98-
import org.schabi.newpipe.player.event.PlayerServiceExtendedEventListener;
98+
import org.schabi.newpipe.player.event.PlayerHolderLifecycleEventListener;
99+
import org.schabi.newpipe.player.event.PlayerServiceEventListener;
99100
import org.schabi.newpipe.player.helper.PlayerHelper;
100101
import org.schabi.newpipe.player.helper.PlayerHolder;
101102
import org.schabi.newpipe.player.playqueue.PlayQueue;
@@ -136,7 +137,8 @@
136137
public final class VideoDetailFragment
137138
extends BaseStateFragment<StreamInfo>
138139
implements BackPressable,
139-
PlayerServiceExtendedEventListener,
140+
PlayerServiceEventListener,
141+
PlayerHolderLifecycleEventListener,
140142
OnKeyDownListener {
141143
public static final String KEY_SWITCHING_PLAYERS = "switching_players";
142144

@@ -234,10 +236,9 @@ public final class VideoDetailFragment
234236
// Service management
235237
//////////////////////////////////////////////////////////////////////////*/
236238
@Override
237-
public void onServiceConnected(final Player connectedPlayer,
238-
final PlayerService connectedPlayerService,
239+
public void onServiceConnected(final PlayerService connectedPlayerService,
239240
final boolean playAfterConnect) {
240-
player = connectedPlayer;
241+
player = connectedPlayerService.getPlayer();
241242
playerService = connectedPlayerService;
242243

243244
// It will do nothing if the player is not in fullscreen mode
@@ -393,7 +394,7 @@ public void onDestroy() {
393394
if (activity.isFinishing() && isPlayerAvailable() && player.videoPlayerSelected()) {
394395
playerHolder.stopService();
395396
} else {
396-
playerHolder.setListener(null);
397+
playerHolder.unsetListeners();
397398
}
398399

399400
PreferenceManager.getDefaultSharedPreferences(activity)
@@ -658,10 +659,10 @@ protected void initListeners() {
658659
});
659660

660661
setupBottomPlayer();
661-
if (!playerHolder.isBound()) {
662+
if (playerHolder.isNotBoundYet()) {
662663
setHeightThumbnail();
663664
} else {
664-
playerHolder.startService(false, this);
665+
playerHolder.startService(false, this, this);
665666
}
666667
}
667668

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

10531054
// See UI changes while remote playQueue changes
10541055
if (!isPlayerAvailable()) {
1055-
playerHolder.startService(false, this);
1056+
playerHolder.startService(false, this, this);
10561057
} else {
10571058
// FIXME Workaround #7427
10581059
player.setRecovery();
@@ -1115,7 +1116,7 @@ public void openVideoPlayerAutoFullscreen() {
11151116
private void openNormalBackgroundPlayer(final boolean append) {
11161117
// See UI changes while remote playQueue changes
11171118
if (!isPlayerAvailable()) {
1118-
playerHolder.startService(false, this);
1119+
playerHolder.startService(false, this, this);
11191120
}
11201121

11211122
final PlayQueue queue = setupPlayQueueForIntent(append);
@@ -1129,7 +1130,7 @@ private void openNormalBackgroundPlayer(final boolean append) {
11291130

11301131
private void openMainPlayer() {
11311132
if (!isPlayerServiceAvailable()) {
1132-
playerHolder.startService(autoPlayEnabled, this);
1133+
playerHolder.startService(autoPlayEnabled, this, this);
11331134
return;
11341135
}
11351136
if (currentInfo == null) {
@@ -1384,9 +1385,11 @@ public void onReceive(final Context context, final Intent intent) {
13841385
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
13851386
}
13861387
// Rebound to the service if it was closed via notification or mini player
1387-
if (!playerHolder.isBound()) {
1388+
if (playerHolder.isNotBoundYet()) {
13881389
playerHolder.startService(
1389-
false, VideoDetailFragment.this);
1390+
false,
1391+
VideoDetailFragment.this,
1392+
VideoDetailFragment.this);
13901393
}
13911394
break;
13921395
}

app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,16 @@ public void onServiceDisconnected(final ComponentName name) {
217217
}
218218

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

223-
if (service instanceof PlayerService.LocalBinder) {
224-
player = ((PlayerService.LocalBinder) service).getPlayer();
223+
if (binder instanceof PlayerService.LocalBinder localBinder) {
224+
final @Nullable PlayerService s = localBinder.getService();
225+
if (s == null) {
226+
player = null;
227+
} else {
228+
player = s.getPlayer();
229+
}
225230
}
226231

227232
if (player == null || player.getPlayQueue() == null || player.exoPlayerIsNull()) {

app/src/main/java/org/schabi/newpipe/player/PlayerService.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import android.os.IBinder;
2929
import android.util.Log;
3030

31+
import androidx.annotation.Nullable;
32+
3133
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
3234
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
3335
import org.schabi.newpipe.util.ThemeHelper;
@@ -36,7 +38,9 @@
3638

3739

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

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

53+
public Player getPlayer() {
54+
return player;
55+
}
4956

5057
/*//////////////////////////////////////////////////////////////////////////
5158
// Service's LifeCycle
@@ -167,19 +174,23 @@ public IBinder onBind(final Intent intent) {
167174
return mBinder;
168175
}
169176

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

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

177-
public PlayerService getService() {
188+
/**
189+
* Get the PlayerService object itself.
190+
* @return this
191+
* */
192+
public @Nullable PlayerService getService() {
178193
return playerService.get();
179194
}
180-
181-
public Player getPlayer() {
182-
return playerService.get().player;
183-
}
184195
}
185196
}

app/src/main/java/org/schabi/newpipe/player/event/PlayerEventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.schabi.newpipe.extractor.stream.StreamInfo;
66
import org.schabi.newpipe.player.playqueue.PlayQueue;
77

8+
/** Player-specific events like queue or progress updates. */
89
public interface PlayerEventListener {
910
void onQueueUpdate(PlayQueue queue);
1011
void onPlaybackUpdate(int state, int repeatMode, boolean shuffled,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.schabi.newpipe.player.event;
2+
3+
import org.schabi.newpipe.player.PlayerService;
4+
5+
/** Gets signalled if our PlayerHolder (dis)connects from the PlayerService. */
6+
public interface PlayerHolderLifecycleEventListener {
7+
void onServiceConnected(PlayerService playerService,
8+
boolean playAfterConnect);
9+
void onServiceDisconnected();
10+
}

app/src/main/java/org/schabi/newpipe/player/event/PlayerServiceEventListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.google.android.exoplayer2.PlaybackException;
44

5+
/** {@link org.schabi.newpipe.player.event.PlayerEventListener} that also gets called for
6+
* application-specific events like screen rotation or UI changes.
7+
*/
58
public interface PlayerServiceEventListener extends PlayerEventListener {
69
void onViewCreated();
710

app/src/main/java/org/schabi/newpipe/player/event/PlayerServiceExtendedEventListener.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)