135 Commits

Author SHA1 Message Date
bachinger
2bde3f1e31 Clean up hashCode/equals of SessionTokenImplBase
#minor-release

PiperOrigin-RevId: 463338680
2022-08-08 08:08:41 +00:00
bachinger
428fc89635 Normalize constructors of SessionTokenImplBase/SessionTokenImplLegacy
#minor-release

PiperOrigin-RevId: 463328436
2022-08-08 08:07:47 +00:00
christosts
dd2c16bc45 DefaultMediaNotificationProvider: limit requests to load same bitmap
The DefaultMediaNotificationProvider caches the last loaded artwork
bitmap so that the bitmap isn't loaded again when the notification is
updated, e.g., the player is transiting from playing to paused. However,
loading bitmap requests for bitmaps that are already being loaded are
not suppressed. For example, if the notification is updated while the
artwork is still downloading, the same artwork might be downloaded
multiple times.

This change suppresses a bitmap load request if the same artwork is
still being loaded, to avoid additional artwork downloads.

#minor-release

PiperOrigin-RevId: 462572221
2022-08-08 07:59:04 +00:00
ibaker
2deb435625 Annotate methods that always return this with @CanIgnoreReturnValue
It's always safe to ignore the result of these methods, because the
caller already has a reference to the returned value.

PiperOrigin-RevId: 462388947
2022-08-08 07:55:25 +00:00
tonihei
ee209690cb Ensure pending commands are still sent in MediaController.release()
We currently clear all pending messages, including the one that flushes
pending commands to the MediaSession. To ensure all commands that have
been called before controller.release() are still sent, we can manually
trigger the flush message from the release call.

Related to handling the final flush because disconnecting the controller,
MediaSessionStub didn't post the removal of the controller to the
session thread, creating a race condition between removing the controller
and actually handling the flush.

Issue: androidx/media#99
PiperOrigin-RevId: 462342860
2022-07-21 13:00:20 +00:00
ibaker
287c757944 Slightly disentangle MediaBrowser/Controller(Impl)Base/Legacy
These constructors are currently very intertwined, passing `this`
references from the constructor of one to the constructor of another
before the first constructor is complete (and so the `this` reference
isn't really valid yet).

This change uses checker framework `@UnderInitialization` and
`@NotOnlyInitialized` annotations to make it more clear that the
references are not available yet. For the one 'direct' access needed
in the second constructor (calling `getApplicationLooper()`) we now
pass the `applicationLooper` directly alongside (to avoid needing to
dereference the reference 'too early').

This change also ensures that where a class hierarchy has a
'dependent' class hierarchy, the 'subclass' instance is always used
(by both subclass and superclass) without casting or manually hiding
the superclass field, by defining an overridable `getFoo()` method
instead and always using it.

#minor-release

PiperOrigin-RevId: 462335043
2022-07-21 12:59:23 +00:00
tonihei
4a4a74edff Rename seq to sequenceNumber in MediaSessionStub
"seq" is not a well-defined abbreviation and the value is
also an integer, so sequenceNumber is better than just sequence.

PiperOrigin-RevId: 462129581
2022-07-21 12:56:13 +00:00
christosts
436ff6d86a Make DefaultMediaNotificationProvider more configurable
Add a Builder to constructor DefaultMediaNotificationProvider. The
Builder can also set the provider's:
- notification ID
- notification channel ID
- notification channel name

The change adds an API for apps to set the small icon in notifications.

#minor-release
Issue: androidx/media#104
PiperOrigin-RevId: 462111536
2022-07-21 12:54:59 +00:00
tonihei
7cb7636ed9 Run MediaSessionStub commands in order
Some commands are run asynchronously and subsequent commands need
to wait until the previous one finished. This can be supported
by returning a Future for each command and using the existing
command execution logic to wait for each Future to complete.

As some MediaSessionStub code is now executed delayed to when it
was originally created, we also need to check if the session is
not released before triggering any actions or sending result codes.

Issue: androidx/media#85
PiperOrigin-RevId: 462101136
2022-07-21 12:54:06 +00:00
tonihei
45f1f5b378 Properly chain commands in MediaSessionStub
The commands currently use a task and a postTask that are chained
together manually. In some cases, e.g. when adding MediaItems,
the postTask is already a chain of commands in itself.

To allow using the entire command handling as a single task
(for simplified queueing), we can change the implementation to
always create a single task. If multiple subtasks need to be
chained together, we can do that by wrapping the method calls.
In case a task is asynchronous, we can also use Futures to
chain them together.

Overall, this is just a refactoring and changes no logic.

Issue: androidx/media#85
PiperOrigin-RevId: 462085724
2022-07-21 12:53:11 +00:00
bachinger
5bf9e2fb31 Exclude tracks if COMMAND_GET_TRACKS is not available
Issue: androidx/media#102
#minor-release
PiperOrigin-RevId: 461891031
2022-07-21 12:50:29 +00:00
bachinger
e8b5aad3d7 Remove unused argument
PiperOrigin-RevId: 461830378
2022-07-21 12:45:10 +00:00
tonihei
dee80788e4 Use Futures for MediaSession command queue instead of Runnables
Some commands may be asynchronous and subsequent commands need to
wait for them to complete before running. This change updates the
queue to use (and listen to) Futures instead of calling Runnables
directly. The commands are currently still added as Runanbles
though, so this change is a no-op.

Also moves the permission check in MediaSessionImpl to before
queueing the command because the permission should be check at
the time of calling the method.

When executing the comamnds in the queue, we need to be careful
to avoid recursion in the same thread (which happens when both
the Future is immediate and running on the correct thread already).
To avoid recursion, we detect this case and loop the commands
instead.

Issue: androidx/media#85
PiperOrigin-RevId: 461827264
2022-07-21 12:44:16 +00:00
bachinger
9a895cd18f Implement getCurrentTracks in MediaController
After this change the current tracks are sent to the controller as part of
`PlayerInfo` and call `Listener.onTracksChanged()` in case of a change in tracks.

PiperOrigin-RevId: 461578695
2022-07-21 12:40:39 +00:00
olly
9271572e95 Add TODOs for registerReceiver calls without flag
PiperOrigin-RevId: 461165173
2022-07-15 12:19:04 +00:00
bachinger
87adb88f57 Fix incorrect link tags
PiperOrigin-RevId: 459215618
2022-07-07 16:44:05 +00:00
bachinger
2c0806814b Document custom commands in the DefaultMediaNotificationProvider
Issue: androidx/media#103
#minor-release
PiperOrigin-RevId: 458465479
2022-07-04 19:51:25 +00:00
rohks
74d61bbffb Add timestamp to CueGroup
`TextRenderer` is updated to output `CueGroup`, which contains the presentation time of the cues, in microseconds.

PiperOrigin-RevId: 456531399
2022-06-27 10:38:20 +01:00
bachinger
8f844b32fd Avoid out of bounds when setting less media items than in playlist
Issue: androidx/media#86
#minor-release
PiperOrigin-RevId: 455182232
2022-06-15 18:55:54 +00:00
bachinger
958105c91c Updating translations
#minor-release

PiperOrigin-RevId: 455128997
2022-06-15 15:34:08 +00:00
tonihei
21d4e85817 Remove setMediaUri/onSetMediaUri
This code path is now completely redundant as the same effect can be
achieved by using player.setMediaItem.

#minor-release

PiperOrigin-RevId: 455115567
2022-06-15 15:32:14 +00:00
tonihei
b475f1f2da Support setMediaItem(s) in MediaControllerImplLegacy
These calls were not implemented so far as they require a mix of
initial prepareFrom/playFrom calls and addQueueItem. We can also
support clients without queue handling to set single MediaItems.

To make the calls consistent and predictable in the session,
we need to ensure that none of the play/pause/addQueueItem/
removeQueueItem/prepare/playFromXYZ/prepareFromXYZ are called
before the controller is prepared and has media.

#minor-release

PiperOrigin-RevId: 455110246
2022-06-15 15:30:05 +00:00
bachinger
29bf4c8aab Add lint base to make gradle lint run without errors
PiperOrigin-RevId: 454951844
2022-06-15 15:27:34 +00:00
bachinger
252ae4c7a3 Suppress lint errors RestrictedApis in lib-session
PiperOrigin-RevId: 454943102
2022-06-15 15:24:49 +00:00
bachinger
8b592fc77a Add session URI to Intent used with the notification
The service handles three different types of `Intents`. Custom command and media
command Intents created by the library and media button event Intents from other
sources.

Media commands from the library as well as from external sources have the action
set to `android.intent.action.MEDIA_BUTTON`. If the data URI is set and can be
used to identify a session then it is a library Intent. If the Intent is coming
from an external KeyEvent, the service implementation is asked which session to use
by calling `onGetSession(controllerInfo)` with the controller info being an
anonymous legacy controller info.

Intents representing a custom command are always coming from the library and hence
always have a data URI.

Issue: androidx/media#82
PiperOrigin-RevId: 453932972
2022-06-09 17:48:28 +00:00
tonihei
5333c67d08 Add COMMAND_SET_MEDIA_ITEM to Player.Commands
Some Player implementations have no playlist capability but can still
set a MediaItem for playback. Examples are a MediaController connected
to a legacy MediaSession, ExoPlayer up to 2.12 or MediaPlayer.

To indicate this capability, we need an allowed command in addition
to COMMAND_CHANGE_MEDIA_ITEMS that just allows to set a single item
that replaces everything that is currently played.

#minor-release

PiperOrigin-RevId: 453879626
2022-06-09 17:46:14 +00:00
christosts
d506c709c9 Avoid using ConcurrentHashMap
Use Collections.synchronizedSet() instead of creating a set from a
ConcurrentHashMap because ConcurrentHashMap has a bug in APIs 21/22
that can result in lost updates.

PiperOrigin-RevId: 453696565
2022-06-09 17:45:17 +00:00
tonihei
bd126ec5c5 Forward legacy controller onPlay/PrepareFromXY calls to onAddMediaItems
These legacy callbacks are currently forwarded to onSetMediaUri which
will be removed in the future.

Also make sure to only call player.prepare/play after the items have
been set.

The calls to onAddQueueItem are also forwarded to onAddMediaItems to
actually allow a session to resolve these items to playable media, which
wasn't possible so far.

PiperOrigin-RevId: 453625204
2022-06-09 17:40:26 +00:00
bachinger
839d4f4390 Use resource ID from drawable.xml as the notification icon
Issue: androidx/media#66
Issue: androidx/media#65

#minor-release

PiperOrigin-RevId: 452004492
2022-05-31 10:46:41 +00:00
ibaker
b8ca5b8951 Add @deprecated javadoc to all @Deprecated @Override methods
This ensures the 'use X instead' message is easily visible in the
generated HTML for the overriding method. Currently it's not, e.g.:
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/BasePlayer.html#getCurrentWindowIndex()

#minor-release

PiperOrigin-RevId: 452002224
2022-05-31 10:44:48 +00:00
bachinger
40f5fb55cc Add constants for custom actions UI customization
PiperOrigin-RevId: 451875964
2022-05-30 16:54:22 +00:00
bachinger
85a936ecb1 Implement session extras for Media3 and legacy controllers
This provides an (unstable) API for apps to broadcast session extras
Bundle to all connected controllers and set the extras in the legacy
session.

Similar to the custom layout, the extras Bundle is not part of the
Media3 session state. This means that when a Media3 controller
connects to the session after the broadcast, the extras needs to be
sent to that controller in  `MediaSession.Callback.onPostConnect(MediaSession session, ControllerInfo controller)`.

PiperOrigin-RevId: 451871731
2022-05-30 16:52:43 +00:00
bachinger
7af9f02067 Add a constant for the legacy controller version
PiperOrigin-RevId: 451857459
2022-05-30 16:50:22 +00:00
tonihei
581c6b5429 Remove MediaMetadata.mediaUrl
This value only existed to allow setting media URLs from external sources
(e.g. in a MediaController) so that a player can start playing this item.
Now that we have MediaItem.RequestMetadata.mediaUrl we can remove this value
from MediaMetadata because it's request metadata, not media metadata.

PiperOrigin-RevId: 451857413
2022-05-30 16:49:28 +00:00
tonihei
6b782d1011 Replace MediaItemFiller by asynchronous callback.
The MediaItemFiller is not flexible enough for most realworld usages
because:
 - it doesn't allow asynchronous resolution of MediaItems (e.g. to
   look up URIs from a database)
 - it doesn't allow to batch updates for multiple items or do more
   advanced customizations (e.g. expanding a mediaId representing
   a playlist to multiple items).

Both issues can be solved by passing in a list of items and
returning a ListenableFuture. The callback itself can also move
into MediaSession.Callback for consistency with the other
callbacks.

PiperOrigin-RevId: 451857319
2022-05-30 16:48:41 +00:00
tonihei
c814f791d7 Add sessionImpl to PostSessionTask
In some cases it's helpful to have access to the session
(e.g. to get the PlayerWrapper) from within the PostSessionTask
implementations. This change forwards the existing sessionImpl
instance to all these callbacks.

PiperOrigin-RevId: 451857191
2022-05-30 16:46:49 +00:00
bachinger
8aec86bde0 Fix typo
PiperOrigin-RevId: 451824740
2022-05-30 16:43:34 +00:00
bachinger
821615cea0 Avoid usage of MediaController in MediaNotification.Provider
PiperOrigin-RevId: 451155897
2022-05-30 16:33:49 +00:00
bachinger
8d03fdfe34 Support custom actions with DefaultMediaNotificationProvider
Refactors the DefaultMediaNotificationProvider by separating the
selection of actions and building the notification with it.

The custom commands of the custom layout of the session are turned
into notification actions and when received from the notification
converted back to custom session commands that are sent to the
session.

PiperOrigin-RevId: 450404350
2022-05-24 11:10:51 +01:00
ibaker
cd142872bb Rename MediaSession.Builder.setSessionCallback to setCallback
Follow-up to c78c1f5891

PiperOrigin-RevId: 449492810
2022-05-24 10:59:06 +01:00
ibaker
c78c1f5891 Rename nested session callback interfaces to just Callback
This is consistent with other nested callback/listener interfaces like
`Player.Listener`.

PiperOrigin-RevId: 449476261
2022-05-24 10:57:02 +01:00
ibaker
7c235d2b84 Remove all null-tolerant methods from BundleableUtil.
In most cases it's clearer to in-line these null-checks with ternary
operators.

PiperOrigin-RevId: 449474621
2022-05-24 10:56:01 +01:00
rohks
c728647290 Create new class to store cues and timestamp.
We need to pass timestamp for the list of cues so we are defining a new class CueGroup which will store both cues and timestamp.

PiperOrigin-RevId: 449212054
2022-05-24 10:49:48 +01:00
bachinger
f3dd361076 Improve JavaDoc of MediaSession.release
Specifically mention that releasing the session does not release the player and the app needs to take care to release the player.

Issue: androidx/media#73
PiperOrigin-RevId: 448454338
2022-05-24 10:43:23 +01:00
bachinger
9b07d71f66 Remove all handler messages when controller is released
Issue: androidx/media#74
PiperOrigin-RevId: 448205908
2022-05-24 10:33:49 +01:00
bachinger
1d89c35f7b Add MediaSession.setCustomLayout(List<CommandButton>)
This provides a way for apps to send a custom layout to media3
controllers and legacy controllers by making sure to include custom
actions in the legacy playback state when built in the PlayerWrapper
for broadcasting.

PiperOrigin-RevId: 447967600
2022-05-24 10:30:00 +01:00
christosts
1bcabd3450 Session: Misc change from link tag to linkplain
PiperOrigin-RevId: 447939097
2022-05-24 10:28:00 +01:00
bachinger
4c838043d3 Dispatch custom action in MediaSessionLegacyStub
A custom action sent by legacy controllers as for instance the System UI mini player arrive at MediaSessionCompat.Callback.onCustomAction() and need to be dispatched to the session callback as a such.

PiperOrigin-RevId: 447486859
2022-05-10 17:50:30 +01:00
christosts
7a631f4050 MediaSessionService: define foregroundServiceType
PiperOrigin-RevId: 447467287
2022-05-10 17:49:22 +01:00
christosts
1b15d5c370 MediaSessionService: allow apps to opt-out from notifications
Issue: androidx/media#50
PiperOrigin-RevId: 447435259
2022-05-10 17:44:41 +01:00