Fix PlayerService leakead by Binder instance

Also see https://stackoverflow.com/q/63787707
This commit is contained in:
Stypox 2023-07-14 18:34:20 +02:00
parent 135f0f7249
commit 00257e969e
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 11 additions and 4 deletions

View File

@ -31,6 +31,8 @@ import android.util.Log;
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import java.lang.ref.WeakReference;
/** /**
* One service for all players. * One service for all players.
@ -41,7 +43,7 @@ public final class PlayerService extends Service {
private Player player; private Player player;
private final IBinder mBinder = new PlayerService.LocalBinder(); private final IBinder mBinder = new PlayerService.LocalBinder(this);
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
@ -134,14 +136,19 @@ public final class PlayerService extends Service {
return mBinder; return mBinder;
} }
public class LocalBinder extends Binder { public static class LocalBinder extends Binder {
private final WeakReference<PlayerService> playerService;
LocalBinder(final PlayerService playerService) {
this.playerService = new WeakReference<>(playerService);
}
public PlayerService getService() { public PlayerService getService() {
return PlayerService.this; return playerService.get();
} }
public Player getPlayer() { public Player getPlayer() {
return PlayerService.this.player; return playerService.get().player;
} }
} }
} }