Skip to content

Commit ce91921

Browse files
committed
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!
1 parent 6a4aaba commit ce91921

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

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

Lines changed: 12 additions & 8 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

@@ -392,7 +394,7 @@ public void onDestroy() {
392394
if (activity.isFinishing() && isPlayerAvailable() && player.videoPlayerSelected()) {
393395
playerHolder.stopService();
394396
} else {
395-
playerHolder.setListener(null);
397+
playerHolder.unsetListeners();
396398
}
397399

398400
PreferenceManager.getDefaultSharedPreferences(activity)
@@ -660,7 +662,7 @@ protected void initListeners() {
660662
if (playerHolder.isNotBoundYet()) {
661663
setHeightThumbnail();
662664
} else {
663-
playerHolder.startService(false, this);
665+
playerHolder.startService(false, this, this);
664666
}
665667
}
666668

@@ -1051,7 +1053,7 @@ private void openPopupPlayer(final boolean append) {
10511053

10521054
// See UI changes while remote playQueue changes
10531055
if (!isPlayerAvailable()) {
1054-
playerHolder.startService(false, this);
1056+
playerHolder.startService(false, this, this);
10551057
} else {
10561058
// FIXME Workaround #7427
10571059
player.setRecovery();
@@ -1114,7 +1116,7 @@ public void openVideoPlayerAutoFullscreen() {
11141116
private void openNormalBackgroundPlayer(final boolean append) {
11151117
// See UI changes while remote playQueue changes
11161118
if (!isPlayerAvailable()) {
1117-
playerHolder.startService(false, this);
1119+
playerHolder.startService(false, this, this);
11181120
}
11191121

11201122
final PlayQueue queue = setupPlayQueueForIntent(append);
@@ -1128,7 +1130,7 @@ private void openNormalBackgroundPlayer(final boolean append) {
11281130

11291131
private void openMainPlayer() {
11301132
if (!isPlayerServiceAvailable()) {
1131-
playerHolder.startService(autoPlayEnabled, this);
1133+
playerHolder.startService(autoPlayEnabled, this, this);
11321134
return;
11331135
}
11341136
if (currentInfo == null) {
@@ -1385,7 +1387,9 @@ public void onReceive(final Context context, final Intent intent) {
13851387
// Rebound to the service if it was closed via notification or mini player
13861388
if (playerHolder.isNotBoundYet()) {
13871389
playerHolder.startService(
1388-
false, VideoDetailFragment.this);
1390+
false,
1391+
VideoDetailFragment.this,
1392+
VideoDetailFragment.this);
13891393
}
13901394
break;
13911395
}

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.schabi.newpipe.player.PlayerService;
44

5-
public interface PlayerServiceExtendedEventListener extends PlayerServiceEventListener {
5+
/** Gets signalled if our PlayerHolder (dis)connects from the PlayerService. */
6+
public interface PlayerHolderLifecycleEventListener {
67
void onServiceConnected(PlayerService playerService,
78
boolean playAfterConnect);
89
void onServiceDisconnected();

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/helper/PlayerHolder.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.os.IBinder;
88
import android.util.Log;
99

10+
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
1112
import androidx.core.content.ContextCompat;
1213

@@ -20,7 +21,7 @@
2021
import org.schabi.newpipe.player.PlayerService;
2122
import org.schabi.newpipe.player.PlayerType;
2223
import org.schabi.newpipe.player.event.PlayerServiceEventListener;
23-
import org.schabi.newpipe.player.event.PlayerServiceExtendedEventListener;
24+
import org.schabi.newpipe.player.event.PlayerHolderLifecycleEventListener;
2425
import org.schabi.newpipe.player.playqueue.PlayQueue;
2526

2627
/** Singleton that manages a `PlayerService`
@@ -41,7 +42,8 @@ public static synchronized PlayerHolder getInstance() {
4142
private static final boolean DEBUG = MainActivity.DEBUG;
4243
private static final String TAG = PlayerHolder.class.getSimpleName();
4344

44-
@Nullable private PlayerServiceExtendedEventListener listener;
45+
@Nullable private PlayerServiceEventListener listener;
46+
@Nullable private PlayerHolderLifecycleEventListener holderListener;
4547

4648
private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection();
4749
private boolean bound;
@@ -102,16 +104,19 @@ public int getQueuePosition() {
102104
return player.getPlayQueue().getIndex();
103105
}
104106

105-
public void setListener(@Nullable final PlayerServiceExtendedEventListener newListener) {
106-
listener = newListener;
107+
public void unsetListeners() {
108+
listener = null;
109+
holderListener = null;
110+
}
107111

108-
if (listener == null) {
109-
return;
110-
}
112+
public void setListener(@NonNull final PlayerServiceEventListener newListener,
113+
@NonNull final PlayerHolderLifecycleEventListener newHolderListener) {
114+
listener = newListener;
115+
holderListener = newHolderListener;
111116

112117
// Force reload data from service
113118
if (player != null) {
114-
listener.onServiceConnected(playerService, false);
119+
holderListener.onServiceConnected(playerService, false);
115120
player.setFragmentListener(internalListener);
116121
}
117122
}
@@ -131,11 +136,14 @@ private Context getCommonContext() {
131136
* If the service is already started, only set the listener.
132137
* @param playAfterConnect If the service is started, start playing immediately
133138
* @param newListener set this listener
139+
* @param newHolderListener set this listener
134140
* */
135141
public void startService(final boolean playAfterConnect,
136-
final PlayerServiceExtendedEventListener newListener) {
142+
final PlayerServiceEventListener newListener,
143+
final PlayerHolderLifecycleEventListener newHolderListener
144+
) {
137145
final Context context = getCommonContext();
138-
setListener(newListener);
146+
setListener(newListener, newHolderListener);
139147
if (bound) {
140148
return;
141149
}
@@ -182,8 +190,8 @@ private void unbind(final Context context) {
182190
}
183191
playerService = null;
184192
player = null;
185-
if (listener != null) {
186-
listener.onServiceDisconnected();
193+
if (holderListener != null) {
194+
holderListener.onServiceDisconnected();
187195
}
188196
}
189197
}
@@ -212,8 +220,8 @@ public void onServiceConnected(final ComponentName compName, final IBinder servi
212220
playerService = localBinder.getService();
213221
player = playerService != null ? playerService.getPlayer() : null;
214222

215-
if (listener != null) {
216-
listener.onServiceConnected(playerService, playAfterConnect);
223+
if (holderListener != null) {
224+
holderListener.onServiceConnected(playerService, playAfterConnect);
217225
}
218226
if (player != null) {
219227
player.setFragmentListener(internalListener);

0 commit comments

Comments
 (0)