mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-06-14 04:24:06 +00:00
Have PlayerService implement MediaBrowserServiceCompat
Co-authored-by: Haggai Eran <haggai.eran@gmail.com>
This commit is contained in:
parent
b764ad33c4
commit
5819546ea9
@ -64,6 +64,9 @@
|
|||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MEDIA_BUTTON" />
|
<action android:name="android.intent.action.MEDIA_BUTTON" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.media.browse.MediaBrowserService"/>
|
||||||
|
</intent-filter>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
@ -424,5 +427,10 @@
|
|||||||
<meta-data
|
<meta-data
|
||||||
android:name="com.samsung.android.multidisplay.keep_process_alive"
|
android:name="com.samsung.android.multidisplay.keep_process_alive"
|
||||||
android:value="true" />
|
android:value="true" />
|
||||||
|
<!-- Android Auto -->
|
||||||
|
<meta-data android:name="com.google.android.gms.car.application"
|
||||||
|
android:resource="@xml/automotive_app_desc" />
|
||||||
|
<meta-data android:name="com.google.android.gms.car.notification.SmallIcon"
|
||||||
|
android:resource="@mipmap/ic_launcher" />
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -183,7 +183,10 @@ public final class PlayQueueActivity extends AppCompatActivity
|
|||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
|
// Note: this code should not really exist, and PlayerHolder should be used instead, but
|
||||||
|
// it will be rewritten when NewPlayer will replace the current player.
|
||||||
final Intent bindIntent = new Intent(this, PlayerService.class);
|
final Intent bindIntent = new Intent(this, PlayerService.class);
|
||||||
|
bindIntent.setAction(PlayerService.BIND_PLAYER_HOLDER_ACTION);
|
||||||
final boolean success = bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
|
final boolean success = bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
unbindService(serviceConnection);
|
unbindService(serviceConnection);
|
||||||
|
@ -21,28 +21,36 @@ package org.schabi.newpipe.player;
|
|||||||
|
|
||||||
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
||||||
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.support.v4.media.MediaBrowserCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.media.MediaBrowserServiceCompat;
|
||||||
|
|
||||||
import org.schabi.newpipe.ktx.BundleKt;
|
import org.schabi.newpipe.ktx.BundleKt;
|
||||||
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
|
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
|
||||||
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
|
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
|
||||||
import org.schabi.newpipe.util.ThemeHelper;
|
import org.schabi.newpipe.util.ThemeHelper;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One service for all players.
|
* One service for all players.
|
||||||
*/
|
*/
|
||||||
public final class PlayerService extends Service {
|
public final class PlayerService extends MediaBrowserServiceCompat {
|
||||||
private static final String TAG = PlayerService.class.getSimpleName();
|
private static final String TAG = PlayerService.class.getSimpleName();
|
||||||
private static final boolean DEBUG = Player.DEBUG;
|
private static final boolean DEBUG = Player.DEBUG;
|
||||||
|
|
||||||
public static final String SHOULD_START_FOREGROUND_EXTRA = "should_start_foreground_extra";
|
public static final String SHOULD_START_FOREGROUND_EXTRA = "should_start_foreground_extra";
|
||||||
|
public static final String BIND_PLAYER_HOLDER_ACTION = "bind_player_holder_action";
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
|
|
||||||
@ -55,6 +63,8 @@ public final class PlayerService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "onCreate() called");
|
Log.d(TAG, "onCreate() called");
|
||||||
}
|
}
|
||||||
@ -148,6 +158,7 @@ public final class PlayerService extends Service {
|
|||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "destroy() called");
|
Log.d(TAG, "destroy() called");
|
||||||
}
|
}
|
||||||
|
super.onDestroy();
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,7 +181,25 @@ public final class PlayerService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(final Intent intent) {
|
public IBinder onBind(final Intent intent) {
|
||||||
|
if (DEBUG) {
|
||||||
|
Log.d(TAG, "onBind() called with: intent = [" + intent
|
||||||
|
+ "], extras = [" + BundleKt.toDebugString(intent.getExtras()) + "]");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BIND_PLAYER_HOLDER_ACTION.equals(intent.getAction())) {
|
||||||
|
// Note that this binder might be reused multiple times while the service is alive, even
|
||||||
|
// after unbind() has been called: https://stackoverflow.com/a/8794930 .
|
||||||
return mBinder;
|
return mBinder;
|
||||||
|
|
||||||
|
} else if (MediaBrowserServiceCompat.SERVICE_INTERFACE.equals(intent.getAction())) {
|
||||||
|
// MediaBrowserService also uses its own binder, so for actions related to the media
|
||||||
|
// browser service, pass the onBind to the superclass.
|
||||||
|
return super.onBind(intent);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// This is an unknown request, avoid returning any binder to not leak objects.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class LocalBinder extends Binder {
|
public static class LocalBinder extends Binder {
|
||||||
@ -188,4 +217,18 @@ public final class PlayerService extends Service {
|
|||||||
return playerService.get().player;
|
return playerService.get().player;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public BrowserRoot onGetRoot(@NonNull final String clientPackageName,
|
||||||
|
final int clientUid,
|
||||||
|
@Nullable final Bundle rootHints) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadChildren(@NonNull final String parentId,
|
||||||
|
@NonNull final Result<List<MediaBrowserCompat.MediaItem>> result) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,6 +183,7 @@ public final class PlayerHolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Intent serviceIntent = new Intent(context, PlayerService.class);
|
final Intent serviceIntent = new Intent(context, PlayerService.class);
|
||||||
|
serviceIntent.setAction(PlayerService.BIND_PLAYER_HOLDER_ACTION);
|
||||||
bound = context.bindService(serviceIntent, serviceConnection,
|
bound = context.bindService(serviceIntent, serviceConnection,
|
||||||
Context.BIND_AUTO_CREATE);
|
Context.BIND_AUTO_CREATE);
|
||||||
if (!bound) {
|
if (!bound) {
|
||||||
|
3
app/src/main/res/xml/automotive_app_desc.xml
Normal file
3
app/src/main/res/xml/automotive_app_desc.xml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<automotiveApp>
|
||||||
|
<uses name="media" />
|
||||||
|
</automotiveApp>
|
Loading…
x
Reference in New Issue
Block a user