Skip to content

Commit 5819546

Browse files
Stypoxhaggaie
andcommitted
Have PlayerService implement MediaBrowserServiceCompat
Co-authored-by: Haggai Eran <haggai.eran@gmail.com>
1 parent b764ad3 commit 5819546

5 files changed

Lines changed: 61 additions & 3 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<intent-filter>
6565
<action android:name="android.intent.action.MEDIA_BUTTON" />
6666
</intent-filter>
67+
<intent-filter>
68+
<action android:name="android.media.browse.MediaBrowserService"/>
69+
</intent-filter>
6770
</service>
6871

6972
<activity
@@ -424,5 +427,10 @@
424427
<meta-data
425428
android:name="com.samsung.android.multidisplay.keep_process_alive"
426429
android:value="true" />
430+
<!-- Android Auto -->
431+
<meta-data android:name="com.google.android.gms.car.application"
432+
android:resource="@xml/automotive_app_desc" />
433+
<meta-data android:name="com.google.android.gms.car.notification.SmallIcon"
434+
android:resource="@mipmap/ic_launcher" />
427435
</application>
428436
</manifest>

app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ protected void onDestroy() {
183183
////////////////////////////////////////////////////////////////////////////
184184

185185
private void bind() {
186+
// Note: this code should not really exist, and PlayerHolder should be used instead, but
187+
// it will be rewritten when NewPlayer will replace the current player.
186188
final Intent bindIntent = new Intent(this, PlayerService.class);
189+
bindIntent.setAction(PlayerService.BIND_PLAYER_HOLDER_ACTION);
187190
final boolean success = bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
188191
if (!success) {
189192
unbindService(serviceConnection);

app/src/main/java/org/schabi/newpipe/player/PlayerService.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,36 @@
2121

2222
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
2323

24-
import android.app.Service;
2524
import android.content.Context;
2625
import android.content.Intent;
2726
import android.os.Binder;
27+
import android.os.Bundle;
2828
import android.os.IBinder;
29+
import android.support.v4.media.MediaBrowserCompat;
2930
import android.util.Log;
3031

32+
import androidx.annotation.NonNull;
33+
import androidx.annotation.Nullable;
34+
import androidx.media.MediaBrowserServiceCompat;
35+
3136
import org.schabi.newpipe.ktx.BundleKt;
3237
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
3338
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
3439
import org.schabi.newpipe.util.ThemeHelper;
3540

3641
import 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
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ private void bind(final Context context) {
183183
}
184184

185185
final Intent serviceIntent = new Intent(context, PlayerService.class);
186+
serviceIntent.setAction(PlayerService.BIND_PLAYER_HOLDER_ACTION);
186187
bound = context.bindService(serviceIntent, serviceConnection,
187188
Context.BIND_AUTO_CREATE);
188189
if (!bound) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<automotiveApp>
2+
<uses name="media" />
3+
</automotiveApp>

0 commit comments

Comments
 (0)