convert PlayerHolder to Singleton, handle context within, bugfix ServiceConnection leak
- bugfix: have ServiceConnection created only once! - select the context within the PlayerHolder to start, stop, bind or unbind the service -> we have to make sure the Service is started AND stopped within the same context -> so let PlayerHolder be the one to select the context - remove removeListener() and replace the call with setListener(null) - Compatibility: use ContextCompat.startForegroundService instead of startService()
This commit is contained in:
parent
435813355f
commit
48c2c156cb
11 changed files with 98 additions and 76 deletions
|
|
@ -8,6 +8,7 @@ import android.os.IBinder;
|
|||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||
import com.google.android.exoplayer2.PlaybackParameters;
|
||||
|
|
@ -22,18 +23,27 @@ import org.schabi.newpipe.player.event.PlayerServiceExtendedEventListener;
|
|||
import org.schabi.newpipe.player.playqueue.PlayQueue;
|
||||
|
||||
public final class PlayerHolder {
|
||||
|
||||
private PlayerHolder() {
|
||||
}
|
||||
|
||||
private static final boolean DEBUG = MainActivity.DEBUG;
|
||||
private static final String TAG = "PlayerHolder";
|
||||
private static PlayerHolder instance;
|
||||
public static synchronized PlayerHolder getInstance() {
|
||||
if (PlayerHolder.instance == null) {
|
||||
PlayerHolder.instance = new PlayerHolder();
|
||||
}
|
||||
return PlayerHolder.instance;
|
||||
}
|
||||
|
||||
private static PlayerServiceExtendedEventListener listener;
|
||||
private final boolean DEBUG = MainActivity.DEBUG;
|
||||
private final String TAG = PlayerHolder.class.getSimpleName();
|
||||
|
||||
private static ServiceConnection serviceConnection;
|
||||
public static boolean bound;
|
||||
private static MainPlayer playerService;
|
||||
private static Player player;
|
||||
private PlayerServiceExtendedEventListener listener;
|
||||
|
||||
private final PlayerServiceConnection serviceConnection = new PlayerServiceConnection();
|
||||
public boolean bound;
|
||||
private MainPlayer playerService;
|
||||
private Player player;
|
||||
|
||||
/**
|
||||
* Returns the current {@link MainPlayer.PlayerType} of the {@link MainPlayer} service,
|
||||
|
|
@ -42,26 +52,31 @@ public final class PlayerHolder {
|
|||
* @return Current PlayerType
|
||||
*/
|
||||
@Nullable
|
||||
public static MainPlayer.PlayerType getType() {
|
||||
public MainPlayer.PlayerType getType() {
|
||||
if (player == null) {
|
||||
return null;
|
||||
}
|
||||
return player.getPlayerType();
|
||||
}
|
||||
|
||||
public static boolean isPlaying() {
|
||||
public boolean isPlaying() {
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
return player.isPlaying();
|
||||
}
|
||||
|
||||
public static boolean isPlayerOpen() {
|
||||
public boolean isPlayerOpen() {
|
||||
return player != null;
|
||||
}
|
||||
|
||||
public static void setListener(final PlayerServiceExtendedEventListener newListener) {
|
||||
public void setListener(@Nullable final PlayerServiceExtendedEventListener newListener) {
|
||||
listener = newListener;
|
||||
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Force reload data from service
|
||||
if (player != null) {
|
||||
listener.onServiceConnected(player, playerService, false);
|
||||
|
|
@ -69,14 +84,15 @@ public final class PlayerHolder {
|
|||
}
|
||||
}
|
||||
|
||||
public static void removeListener() {
|
||||
listener = null;
|
||||
// helper to handle context in common place as using the same
|
||||
// context to bind/unbind a service is crucial
|
||||
private Context getCommonContext() {
|
||||
return App.getApp();
|
||||
}
|
||||
|
||||
|
||||
public static void startService(final Context context,
|
||||
final boolean playAfterConnect,
|
||||
final PlayerServiceExtendedEventListener newListener) {
|
||||
public void startService(final boolean playAfterConnect,
|
||||
final PlayerServiceExtendedEventListener newListener) {
|
||||
final Context context = getCommonContext();
|
||||
setListener(newListener);
|
||||
if (bound) {
|
||||
return;
|
||||
|
|
@ -85,58 +101,65 @@ public final class PlayerHolder {
|
|||
// and NullPointerExceptions inside the service because the service will be
|
||||
// bound twice. Prevent it with unbinding first
|
||||
unbind(context);
|
||||
context.startService(new Intent(context, MainPlayer.class));
|
||||
serviceConnection = getServiceConnection(context, playAfterConnect);
|
||||
ContextCompat.startForegroundService(context, new Intent(context, MainPlayer.class));
|
||||
serviceConnection.doPlayAfterConnect(playAfterConnect);
|
||||
bind(context);
|
||||
}
|
||||
|
||||
public static void stopService(final Context context) {
|
||||
public void stopService() {
|
||||
final Context context = getCommonContext();
|
||||
unbind(context);
|
||||
context.stopService(new Intent(context, MainPlayer.class));
|
||||
}
|
||||
|
||||
private static ServiceConnection getServiceConnection(final Context context,
|
||||
final boolean playAfterConnect) {
|
||||
return new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceDisconnected(final ComponentName compName) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Player service is disconnected");
|
||||
}
|
||||
class PlayerServiceConnection implements ServiceConnection {
|
||||
|
||||
unbind(context);
|
||||
private boolean playAfterConnect = false;
|
||||
|
||||
public void doPlayAfterConnect(final boolean playAfterConnection) {
|
||||
this.playAfterConnect = playAfterConnection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(final ComponentName compName) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Player service is disconnected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected(final ComponentName compName, final IBinder service) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Player service is connected");
|
||||
}
|
||||
final MainPlayer.LocalBinder localBinder = (MainPlayer.LocalBinder) service;
|
||||
final Context context = getCommonContext();
|
||||
unbind(context);
|
||||
}
|
||||
|
||||
playerService = localBinder.getService();
|
||||
player = localBinder.getPlayer();
|
||||
if (listener != null) {
|
||||
listener.onServiceConnected(player, playerService, playAfterConnect);
|
||||
}
|
||||
startPlayerListener();
|
||||
@Override
|
||||
public void onServiceConnected(final ComponentName compName, final IBinder service) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Player service is connected");
|
||||
}
|
||||
};
|
||||
}
|
||||
final MainPlayer.LocalBinder localBinder = (MainPlayer.LocalBinder) service;
|
||||
|
||||
private static void bind(final Context context) {
|
||||
playerService = localBinder.getService();
|
||||
player = localBinder.getPlayer();
|
||||
if (listener != null) {
|
||||
listener.onServiceConnected(player, playerService, playAfterConnect);
|
||||
}
|
||||
startPlayerListener();
|
||||
}
|
||||
};
|
||||
|
||||
private void bind(final Context context) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "bind() called");
|
||||
}
|
||||
|
||||
final Intent serviceIntent = new Intent(context, MainPlayer.class);
|
||||
bound = context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
bound = context.bindService(serviceIntent, serviceConnection,
|
||||
Context.BIND_AUTO_CREATE);
|
||||
if (!bound) {
|
||||
context.unbindService(serviceConnection);
|
||||
}
|
||||
}
|
||||
|
||||
private static void unbind(final Context context) {
|
||||
private void unbind(final Context context) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "unbind() called");
|
||||
}
|
||||
|
|
@ -153,21 +176,19 @@ public final class PlayerHolder {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static void startPlayerListener() {
|
||||
private void startPlayerListener() {
|
||||
if (player != null) {
|
||||
player.setFragmentListener(INNER_LISTENER);
|
||||
player.setFragmentListener(internalListener);
|
||||
}
|
||||
}
|
||||
|
||||
private static void stopPlayerListener() {
|
||||
private void stopPlayerListener() {
|
||||
if (player != null) {
|
||||
player.removeFragmentListener(INNER_LISTENER);
|
||||
player.removeFragmentListener(internalListener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static final PlayerServiceEventListener INNER_LISTENER =
|
||||
private final PlayerServiceEventListener internalListener =
|
||||
new PlayerServiceEventListener() {
|
||||
@Override
|
||||
public void onFullscreenStateChanged(final boolean fullscreen) {
|
||||
|
|
@ -242,7 +263,7 @@ public final class PlayerHolder {
|
|||
if (listener != null) {
|
||||
listener.onServiceStopped();
|
||||
}
|
||||
unbind(App.getApp());
|
||||
unbind(getCommonContext());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue