Skip to content

Commit 7edcad7

Browse files
committed
Add action to startSelfIntent to easily distinguish it as app
I was debugging start commands I shouldn't be recieving, and the startSelfIntent with empty action was sort of confusing because it wasn't immediately obvious where it came from. Adding a unique action string doesn't have any ill effects and allows easily identifying these intents in onStartCommand().
1 parent 8c5bbd7 commit 7edcad7

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

libraries/session/src/main/java/androidx/media3/session/DefaultActionFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
/** The default {@link MediaNotification.ActionFactory}. */
4949
/* package */ final class DefaultActionFactory implements MediaNotification.ActionFactory {
5050

51-
private static final String ACTION_CUSTOM = "androidx.media3.session.CUSTOM_NOTIFICATION_ACTION";
5251
private static final String EXTRAS_KEY_ACTION_CUSTOM =
5352
"androidx.media3.session.EXTRAS_KEY_CUSTOM_NOTIFICATION_ACTION";
5453
public static final String EXTRAS_KEY_ACTION_CUSTOM_EXTRAS =
@@ -162,7 +161,7 @@ private int toKeyCode(@Player.Command long action) {
162161
@SuppressWarnings("PendingIntentMutability") // We can't use SaferPendingIntent
163162
private PendingIntent createCustomActionPendingIntent(
164163
MediaSession mediaSession, String action, Bundle extras) {
165-
Intent intent = new Intent(ACTION_CUSTOM);
164+
Intent intent = new Intent(MediaSessionService.ACTION_CUSTOM_NOTIFICATION_ACTION);
166165
intent.setData(mediaSession.getImpl().getUri());
167166
intent.setComponent(new ComponentName(service, service.getClass()));
168167
intent.putExtra(EXTRAS_KEY_ACTION_CUSTOM, action);
@@ -182,7 +181,7 @@ public boolean isMediaAction(Intent intent) {
182181

183182
/** Returns whether {@code intent} was part of a {@link #createCustomAction custom action }. */
184183
public boolean isCustomAction(Intent intent) {
185-
return ACTION_CUSTOM.equals(intent.getAction());
184+
return MediaSessionService.ACTION_CUSTOM_NOTIFICATION_ACTION.equals(intent.getAction());
186185
}
187186

188187
/**

libraries/session/src/main/java/androidx/media3/session/MediaNotificationManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public MediaNotificationManager(
9191
mainHandler = Util.createHandler(Looper.getMainLooper(), /* callback= */ this);
9292
mainExecutor = (runnable) -> Util.postOrRun(mainHandler, runnable);
9393
startSelfIntent = new Intent(mediaSessionService, mediaSessionService.getClass());
94+
startSelfIntent.setAction(MediaSessionService.ACTION_START_SELF);
9495
controllerMap = new HashMap<>();
9596
startedInForeground = false;
9697
isUserEngagedTimeoutEnabled = true;

libraries/session/src/main/java/androidx/media3/session/MediaSessionService.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,39 @@ default void onForegroundServiceStartNotAllowedException() {}
166166
/** The action for {@link Intent} filter that must be declared by the service. */
167167
public static final String SERVICE_INTERFACE = "androidx.media3.session.MediaSessionService";
168168

169+
/**
170+
* The action of an {@link Intent} sent to this service by itself when it is already running, but
171+
* wishes to move into the foreground state.
172+
*
173+
* <p>This action is not intended for starting the service from anywhere except custom foreground
174+
* service handling. Most apps will never need to send this action themselves.
175+
*
176+
* <p>If an {@link Intent} with this action is received in a subclass' {@link
177+
* #onStartCommand(Intent, int, int)} method, the intent should be passed to the superclass
178+
* (MediaSessionService) implementation. No further action is required to handle it.
179+
*/
180+
@UnstableApi
181+
public static final String ACTION_START_SELF =
182+
"androidx.media3.session.MediaSessionService.ACTION_START_SELF";
183+
184+
/**
185+
* The action of an {@link Intent} sent to this service by the system when a custom notification
186+
* or session action was clicked.
187+
*
188+
* <p>If an {@link Intent} with this action is received in a subclass' {@link
189+
* #onStartCommand(Intent, int, int)} method, the intent should be passed to the superclass
190+
* (MediaSessionService) implementation.
191+
*
192+
* <p>This {@link Intent} is not intended to be intercepted by subclasses. If subclasses wish to
193+
* handle custom notification actions, {@link
194+
* MediaNotification.Provider#handleCustomCommand(MediaSession, String, Bundle)} or {@link
195+
* MediaSession.Callback#onCustomCommand(MediaSession, ControllerInfo, SessionCommand, Bundle)}
196+
* are appropriate.
197+
*/
198+
@UnstableApi
199+
public static final String ACTION_CUSTOM_NOTIFICATION_ACTION =
200+
"androidx.media3.session.CUSTOM_NOTIFICATION_ACTION";
201+
169202
/**
170203
* The default timeout for a session to stay in a foreground service state after it paused,
171204
* stopped, failed or ended.
@@ -439,9 +472,18 @@ public IBinder onBind(@Nullable Intent intent) {
439472
/**
440473
* Called when a component calls {@link android.content.Context#startService(Intent)}.
441474
*
442-
* <p>The default implementation handles the incoming media button events. In this case, the
443-
* intent will have the action {@link Intent#ACTION_MEDIA_BUTTON}. Override this method if this
444-
* service also needs to handle actions other than {@link Intent#ACTION_MEDIA_BUTTON}.
475+
* <p>The default implementation handles the following events:
476+
*
477+
* <ul>
478+
* <li>incoming media button events with the {@link Intent} action {@link
479+
* Intent#ACTION_MEDIA_BUTTON}.
480+
* <li>custom notification actions with {@link Intent} action {@link
481+
* #ACTION_CUSTOM_NOTIFICATION_ACTION}
482+
* <li>foreground service state changes with {@link Intent} action {@link #ACTION_START_SELF}
483+
* </ul>
484+
*
485+
* <p>Override this method if this service also needs to handle actions other than those mentioned
486+
* above.
445487
*
446488
* <p>This method will be called on the main thread.
447489
*/

0 commit comments

Comments
 (0)