Skip to content

Commit ef3c766

Browse files
committed
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.
1 parent fbafdeb commit ef3c766

5 files changed

Lines changed: 48 additions & 40 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,9 @@ public final class VideoDetailFragment
234234
// Service management
235235
//////////////////////////////////////////////////////////////////////////*/
236236
@Override
237-
public void onServiceConnected(final Player connectedPlayer,
238-
final PlayerService connectedPlayerService,
237+
public void onServiceConnected(final PlayerService connectedPlayerService,
239238
final boolean playAfterConnect) {
240-
player = connectedPlayer;
239+
player = connectedPlayerService.getPlayer();
241240
playerService = connectedPlayerService;
242241

243242
// It will do nothing if the player is not in fullscreen mode

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: 15 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,21 @@ public IBinder onBind(final Intent intent) {
167174
return mBinder;
168175
}
169176

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

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

177-
public PlayerService getService() {
187+
/** Get the PlayerService object itself.
188+
* @return this
189+
* */
190+
public @Nullable PlayerService getService() {
178191
return playerService.get();
179192
}
180-
181-
public Player getPlayer() {
182-
return playerService.get().player;
183-
}
184193
}
185194
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.schabi.newpipe.player.event;
22

33
import org.schabi.newpipe.player.PlayerService;
4-
import org.schabi.newpipe.player.Player;
54

65
public interface PlayerServiceExtendedEventListener extends PlayerServiceEventListener {
7-
void onServiceConnected(Player player,
8-
PlayerService playerService,
6+
void onServiceConnected(PlayerService playerService,
97
boolean playAfterConnect);
108
void onServiceDisconnected();
119
}

app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static synchronized PlayerHolder getInstance() {
4343

4444
private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection();
4545
private boolean bound;
46+
4647
@Nullable private PlayerService playerService;
4748
@Nullable private Player player;
4849

@@ -108,13 +109,16 @@ public void setListener(@Nullable final PlayerServiceExtendedEventListener newLi
108109

109110
// Force reload data from service
110111
if (player != null) {
111-
listener.onServiceConnected(player, playerService, false);
112-
startPlayerListener();
112+
listener.onServiceConnected(playerService, false);
113+
player.setFragmentListener(internalListener);
113114
}
114115
}
115116

116-
// helper to handle context in common place as using the same
117-
// context to bind/unbind a service is crucial
117+
/** Helper to handle context in common place as using the same
118+
* context to bind/unbind a service is crucial.
119+
*
120+
* @return the common context
121+
* */
118122
private Context getCommonContext() {
119123
return App.getInstance();
120124
}
@@ -131,7 +135,7 @@ public void startService(final boolean playAfterConnect,
131135
// bound twice. Prevent it with unbinding first
132136
unbind(context);
133137
ContextCompat.startForegroundService(context, new Intent(context, PlayerService.class));
134-
serviceConnection.doPlayAfterConnect(playAfterConnect);
138+
serviceConnection.playAfterConnect = playAfterConnect;
135139
bind(context);
136140
}
137141

@@ -145,10 +149,6 @@ class PlayerServiceConnection implements ServiceConnection {
145149

146150
private boolean playAfterConnect = false;
147151

148-
public void doPlayAfterConnect(final boolean playAfterConnection) {
149-
this.playAfterConnect = playAfterConnection;
150-
}
151-
152152
@Override
153153
public void onServiceDisconnected(final ComponentName compName) {
154154
if (DEBUG) {
@@ -167,14 +167,21 @@ public void onServiceConnected(final ComponentName compName, final IBinder servi
167167
final PlayerService.LocalBinder localBinder = (PlayerService.LocalBinder) service;
168168

169169
playerService = localBinder.getService();
170-
player = localBinder.getPlayer();
170+
player = playerService != null ? playerService.getPlayer() : null;
171+
171172
if (listener != null) {
172-
listener.onServiceConnected(player, playerService, playAfterConnect);
173+
listener.onServiceConnected(playerService, playAfterConnect);
174+
}
175+
if (player != null) {
176+
player.setFragmentListener(internalListener);
173177
}
174-
startPlayerListener();
175178
}
176179
}
177180

181+
/** Connect to (and if needed start) the {@link PlayerService}
182+
* and bind {@link PlayerServiceConnection} to it.
183+
* @param context common holder context
184+
* */
178185
private void bind(final Context context) {
179186
if (DEBUG) {
180187
Log.d(TAG, "bind() called");
@@ -196,7 +203,9 @@ private void unbind(final Context context) {
196203
if (bound) {
197204
context.unbindService(serviceConnection);
198205
bound = false;
199-
stopPlayerListener();
206+
if (player != null) {
207+
player.removeFragmentListener(internalListener);
208+
}
200209
playerService = null;
201210
player = null;
202211
if (listener != null) {
@@ -205,18 +214,6 @@ private void unbind(final Context context) {
205214
}
206215
}
207216

208-
private void startPlayerListener() {
209-
if (player != null) {
210-
player.setFragmentListener(internalListener);
211-
}
212-
}
213-
214-
private void stopPlayerListener() {
215-
if (player != null) {
216-
player.removeFragmentListener(internalListener);
217-
}
218-
}
219-
220217
private final PlayerServiceEventListener internalListener =
221218
new PlayerServiceEventListener() {
222219
@Override

0 commit comments

Comments
 (0)