2121
2222import static org .schabi .newpipe .util .Localization .assureCorrectAppLanguage ;
2323
24- import android .app .Service ;
2524import android .content .Context ;
2625import android .content .Intent ;
2726import android .os .Binder ;
27+ import android .os .Bundle ;
2828import android .os .IBinder ;
29+ import android .support .v4 .media .MediaBrowserCompat ;
2930import android .util .Log ;
3031
32+ import androidx .annotation .NonNull ;
33+ import androidx .annotation .Nullable ;
34+ import androidx .media .MediaBrowserServiceCompat ;
35+
3136import org .schabi .newpipe .ktx .BundleKt ;
3237import org .schabi .newpipe .player .mediasession .MediaSessionPlayerUi ;
3338import org .schabi .newpipe .player .notification .NotificationPlayerUi ;
3439import org .schabi .newpipe .util .ThemeHelper ;
3540
3641import java .lang .ref .WeakReference ;
42+ import java .util .List ;
3743
3844
3945/**
4046 * One service for all players.
4147 */
42- public final class PlayerService extends Service {
48+ public final class PlayerService extends MediaBrowserServiceCompat {
4349 private static final String TAG = PlayerService .class .getSimpleName ();
4450 private static final boolean DEBUG = Player .DEBUG ;
51+
4552 public static final String SHOULD_START_FOREGROUND_EXTRA = "should_start_foreground_extra" ;
53+ public static final String BIND_PLAYER_HOLDER_ACTION = "bind_player_holder_action" ;
4654
4755 private Player player ;
4856
@@ -55,6 +63,8 @@ public final class PlayerService extends Service {
5563
5664 @ Override
5765 public void onCreate () {
66+ super .onCreate ();
67+
5868 if (DEBUG ) {
5969 Log .d (TAG , "onCreate() called" );
6070 }
@@ -148,6 +158,7 @@ public void onDestroy() {
148158 if (DEBUG ) {
149159 Log .d (TAG , "destroy() called" );
150160 }
161+ super .onDestroy ();
151162 cleanup ();
152163 }
153164
@@ -170,7 +181,25 @@ protected void attachBaseContext(final Context base) {
170181
171182 @ Override
172183 public IBinder onBind (final Intent intent ) {
173- return mBinder ;
184+ if (DEBUG ) {
185+ Log .d (TAG , "onBind() called with: intent = [" + intent
186+ + "], extras = [" + BundleKt .toDebugString (intent .getExtras ()) + "]" );
187+ }
188+
189+ if (BIND_PLAYER_HOLDER_ACTION .equals (intent .getAction ())) {
190+ // Note that this binder might be reused multiple times while the service is alive, even
191+ // after unbind() has been called: https://stackoverflow.com/a/8794930 .
192+ return mBinder ;
193+
194+ } else if (MediaBrowserServiceCompat .SERVICE_INTERFACE .equals (intent .getAction ())) {
195+ // MediaBrowserService also uses its own binder, so for actions related to the media
196+ // browser service, pass the onBind to the superclass.
197+ return super .onBind (intent );
198+
199+ } else {
200+ // This is an unknown request, avoid returning any binder to not leak objects.
201+ return null ;
202+ }
174203 }
175204
176205 public static class LocalBinder extends Binder {
@@ -188,4 +217,18 @@ public Player getPlayer() {
188217 return playerService .get ().player ;
189218 }
190219 }
220+
221+ @ Nullable
222+ @ Override
223+ public BrowserRoot onGetRoot (@ NonNull final String clientPackageName ,
224+ final int clientUid ,
225+ @ Nullable final Bundle rootHints ) {
226+ return null ;
227+ }
228+
229+ @ Override
230+ public void onLoadChildren (@ NonNull final String parentId ,
231+ @ NonNull final Result <List <MediaBrowserCompat .MediaItem >> result ) {
232+
233+ }
191234}
0 commit comments