mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Use service context in DefaultActionFactory
We need to use a Service to create the pending intents in the `DefaultActionFactory`. PiperOrigin-RevId: 429115746
This commit is contained in:
parent
00f93ac035
commit
bb7ee69870
@ -16,8 +16,8 @@
|
||||
package androidx.media3.session;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.media.session.PlaybackStateCompat;
|
||||
@ -39,10 +39,10 @@ import androidx.media3.common.util.Util;
|
||||
public static final String EXTRAS_KEY_ACTION_CUSTOM_EXTRAS =
|
||||
"androidx.media3.session.EXTRAS_KEY_CUSTOM_NOTIFICATION_ACTION_EXTRAS";
|
||||
|
||||
private final Context context;
|
||||
private final Service service;
|
||||
|
||||
public DefaultActionFactory(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
public DefaultActionFactory(Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,13 +62,13 @@ import androidx.media3.common.util.Util;
|
||||
public PendingIntent createMediaActionPendingIntent(@Command long command) {
|
||||
int keyCode = PlaybackStateCompat.toKeyCode(command);
|
||||
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
|
||||
intent.setComponent(new ComponentName(context, context.getClass()));
|
||||
intent.setComponent(new ComponentName(service, service.getClass()));
|
||||
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
|
||||
if (Util.SDK_INT >= 26 && command != COMMAND_PAUSE && command != COMMAND_STOP) {
|
||||
return Api26.createPendingIntent(context, /* requestCode= */ keyCode, intent);
|
||||
return Api26.createPendingIntent(service, /* requestCode= */ keyCode, intent);
|
||||
} else {
|
||||
return PendingIntent.getService(
|
||||
context,
|
||||
service,
|
||||
/* requestCode= */ keyCode,
|
||||
intent,
|
||||
Util.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0);
|
||||
@ -77,15 +77,15 @@ import androidx.media3.common.util.Util;
|
||||
|
||||
private PendingIntent createCustomActionPendingIntent(String action, Bundle extras) {
|
||||
Intent intent = new Intent(ACTION_CUSTOM);
|
||||
intent.setComponent(new ComponentName(context, context.getClass()));
|
||||
intent.setComponent(new ComponentName(service, service.getClass()));
|
||||
intent.putExtra(EXTRAS_KEY_ACTION_CUSTOM, action);
|
||||
intent.putExtra(EXTRAS_KEY_ACTION_CUSTOM_EXTRAS, extras);
|
||||
if (Util.SDK_INT >= 26) {
|
||||
return Api26.createPendingIntent(
|
||||
context, /* requestCode= */ KeyEvent.KEYCODE_UNKNOWN, intent);
|
||||
service, /* requestCode= */ KeyEvent.KEYCODE_UNKNOWN, intent);
|
||||
} else {
|
||||
return PendingIntent.getService(
|
||||
context,
|
||||
service,
|
||||
/* requestCode= */ KeyEvent.KEYCODE_UNKNOWN,
|
||||
intent,
|
||||
Util.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0);
|
||||
@ -141,9 +141,9 @@ import androidx.media3.common.util.Util;
|
||||
private static final class Api26 {
|
||||
private Api26() {}
|
||||
|
||||
public static PendingIntent createPendingIntent(Context context, int keyCode, Intent intent) {
|
||||
public static PendingIntent createPendingIntent(Service service, int keyCode, Intent intent) {
|
||||
return PendingIntent.getForegroundService(
|
||||
context, /* requestCode= */ keyCode, intent, PendingIntent.FLAG_IMMUTABLE);
|
||||
service, /* requestCode= */ keyCode, intent, PendingIntent.FLAG_IMMUTABLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -396,7 +396,7 @@ public abstract class MediaSessionService extends Service {
|
||||
if (mediaNotificationProvider == null) {
|
||||
mediaNotificationProvider = new DefaultMediaNotificationProvider(getApplicationContext());
|
||||
}
|
||||
actionFactory = new DefaultActionFactory(getApplicationContext());
|
||||
actionFactory = new DefaultActionFactory(/* service= */ this);
|
||||
mediaNotificationManager =
|
||||
new MediaNotificationManager(
|
||||
/* mediaSessionService= */ this, mediaNotificationProvider, actionFactory);
|
||||
|
@ -20,10 +20,11 @@ import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.shadows.ShadowPendingIntent;
|
||||
|
||||
/** Tests for {@link DefaultActionFactory}. */
|
||||
@ -33,7 +34,7 @@ public class DefaultActionFactoryTest {
|
||||
@Test
|
||||
public void createMediaPendingIntent_intentIsMediaAction() {
|
||||
DefaultActionFactory actionFactory =
|
||||
new DefaultActionFactory(ApplicationProvider.getApplicationContext());
|
||||
new DefaultActionFactory(Robolectric.setupService(TestService.class));
|
||||
|
||||
PendingIntent pendingIntent =
|
||||
actionFactory.createMediaActionPendingIntent(MediaNotification.ActionFactory.COMMAND_PLAY);
|
||||
@ -45,7 +46,7 @@ public class DefaultActionFactoryTest {
|
||||
@Test
|
||||
public void isMediaAction_withNonMediaIntent_returnsFalse() {
|
||||
DefaultActionFactory actionFactory =
|
||||
new DefaultActionFactory(ApplicationProvider.getApplicationContext());
|
||||
new DefaultActionFactory(Robolectric.setupService(TestService.class));
|
||||
|
||||
Intent intent = new Intent("invalid_action");
|
||||
|
||||
@ -55,10 +56,19 @@ public class DefaultActionFactoryTest {
|
||||
@Test
|
||||
public void isCustomAction_withNonCustomActionIntent_returnsFalse() {
|
||||
DefaultActionFactory actionFactory =
|
||||
new DefaultActionFactory(ApplicationProvider.getApplicationContext());
|
||||
new DefaultActionFactory(Robolectric.setupService(TestService.class));
|
||||
|
||||
Intent intent = new Intent("invalid_action");
|
||||
|
||||
assertThat(actionFactory.isCustomAction(intent)).isFalse();
|
||||
}
|
||||
|
||||
/** A test service for unit tests. */
|
||||
public static final class TestService extends MediaLibraryService {
|
||||
@Nullable
|
||||
@Override
|
||||
public MediaLibrarySession onGetSession(MediaSession.ControllerInfo controllerInfo) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user