Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
052c4b3c
authored
Jan 19, 2023
by
tonihei
Committed by
christosts
Jan 23, 2023
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add command check for metadata in DefaultMediaNotificationProvider
PiperOrigin-RevId: 503172986
parent
e961c1b5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
28 deletions
libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java
libraries/session/src/test/java/androidx/media3/session/DefaultMediaNotificationProviderTest.java
libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java
View file @
052c4b3c
...
...
@@ -319,33 +319,35 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
mediaStyle
.
setShowActionsInCompactView
(
compactViewIndices
);
// Set metadata info in the notification.
MediaMetadata
metadata
=
player
.
getMediaMetadata
();
builder
.
setContentTitle
(
getNotificationContentTitle
(
metadata
))
.
setContentText
(
getNotificationContentText
(
metadata
));
@Nullable
ListenableFuture
<
Bitmap
>
bitmapFuture
=
mediaSession
.
getBitmapLoader
().
loadBitmapFromMetadata
(
metadata
);
if
(
bitmapFuture
!=
null
)
{
if
(
pendingOnBitmapLoadedFutureCallback
!=
null
)
{
pendingOnBitmapLoadedFutureCallback
.
discardIfPending
();
}
if
(
bitmapFuture
.
isDone
())
{
try
{
builder
.
setLargeIcon
(
Futures
.
getDone
(
bitmapFuture
));
}
catch
(
ExecutionException
e
)
{
Log
.
w
(
TAG
,
getBitmapLoadErrorMessage
(
e
));
if
(
player
.
isCommandAvailable
(
Player
.
COMMAND_GET_MEDIA_ITEMS_METADATA
))
{
MediaMetadata
metadata
=
player
.
getMediaMetadata
();
builder
.
setContentTitle
(
getNotificationContentTitle
(
metadata
))
.
setContentText
(
getNotificationContentText
(
metadata
));
@Nullable
ListenableFuture
<
Bitmap
>
bitmapFuture
=
mediaSession
.
getBitmapLoader
().
loadBitmapFromMetadata
(
metadata
);
if
(
bitmapFuture
!=
null
)
{
if
(
pendingOnBitmapLoadedFutureCallback
!=
null
)
{
pendingOnBitmapLoadedFutureCallback
.
discardIfPending
();
}
if
(
bitmapFuture
.
isDone
())
{
try
{
builder
.
setLargeIcon
(
Futures
.
getDone
(
bitmapFuture
));
}
catch
(
ExecutionException
e
)
{
Log
.
w
(
TAG
,
getBitmapLoadErrorMessage
(
e
));
}
}
else
{
pendingOnBitmapLoadedFutureCallback
=
new
OnBitmapLoadedFutureCallback
(
notificationId
,
builder
,
onNotificationChangedCallback
);
Futures
.
addCallback
(
bitmapFuture
,
pendingOnBitmapLoadedFutureCallback
,
// This callback must be executed on the next looper iteration, after this method has
// returned a media notification.
mainHandler:
:
post
);
}
}
else
{
pendingOnBitmapLoadedFutureCallback
=
new
OnBitmapLoadedFutureCallback
(
notificationId
,
builder
,
onNotificationChangedCallback
);
Futures
.
addCallback
(
bitmapFuture
,
pendingOnBitmapLoadedFutureCallback
,
// This callback must be executed on the next looper iteration, after this method has
// returned a media notification.
mainHandler:
:
post
);
}
}
...
...
libraries/session/src/test/java/androidx/media3/session/DefaultMediaNotificationProviderTest.java
View file @
052c4b3c
...
...
@@ -20,6 +20,7 @@ import static androidx.media3.session.DefaultMediaNotificationProvider.DEFAULT_C
import
static
androidx
.
media3
.
session
.
DefaultMediaNotificationProvider
.
DEFAULT_NOTIFICATION_ID
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyInt
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
times
;
...
...
@@ -629,6 +630,33 @@ public class DefaultMediaNotificationProviderTest {
assertThat
(
isMediaMetadataArtistEqualToNotificationContentText
).
isTrue
();
}
@Test
public
void
setMediaMetadata_withoutAvailableCommandToGetMetadata_doesNotUseMetadataForNotification
()
{
Context
context
=
ApplicationProvider
.
getApplicationContext
();
DefaultMediaNotificationProvider
defaultMediaNotificationProvider
=
new
DefaultMediaNotificationProvider
.
Builder
(
context
).
build
();
DefaultActionFactory
defaultActionFactory
=
new
DefaultActionFactory
(
Robolectric
.
setupService
(
TestService
.
class
));
MediaSession
mockMediaSession
=
createMockMediaSessionForNotification
(
new
MediaMetadata
.
Builder
().
setArtist
(
"artist"
).
setTitle
(
"title"
).
build
(),
/* getMetadataCommandAvailable= */
false
);
BitmapLoader
mockBitmapLoader
=
mock
(
BitmapLoader
.
class
);
when
(
mockBitmapLoader
.
loadBitmapFromMetadata
(
any
())).
thenReturn
(
null
);
when
(
mockMediaSession
.
getBitmapLoader
()).
thenReturn
(
mockBitmapLoader
);
MediaNotification
notification
=
defaultMediaNotificationProvider
.
createNotification
(
mockMediaSession
,
ImmutableList
.
of
(),
defaultActionFactory
,
mock
(
MediaNotification
.
Provider
.
Callback
.
class
));
assertThat
(
NotificationCompat
.
getContentText
(
notification
.
notification
)).
isNull
();
assertThat
(
NotificationCompat
.
getContentTitle
(
notification
.
notification
)).
isNull
();
}
/**
* {@link DefaultMediaNotificationProvider} is designed to be extendable. Public constructor
* should not be removed.
...
...
@@ -721,9 +749,22 @@ public class DefaultMediaNotificationProviderTest {
}
private
static
MediaSession
createMockMediaSessionForNotification
(
MediaMetadata
mediaMetadata
)
{
return
createMockMediaSessionForNotification
(
mediaMetadata
,
/* getMetadataCommandAvailable= */
true
);
}
private
static
MediaSession
createMockMediaSessionForNotification
(
MediaMetadata
mediaMetadata
,
boolean
getMetadataCommandAvailable
)
{
Player
mockPlayer
=
mock
(
Player
.
class
);
when
(
mockPlayer
.
getAvailableCommands
()).
thenReturn
(
Commands
.
EMPTY
);
when
(
mockPlayer
.
getMediaMetadata
()).
thenReturn
(
mediaMetadata
);
when
(
mockPlayer
.
isCommandAvailable
(
anyInt
())).
thenReturn
(
false
);
if
(
getMetadataCommandAvailable
)
{
when
(
mockPlayer
.
getAvailableCommands
())
.
thenReturn
(
new
Commands
.
Builder
().
add
(
Player
.
COMMAND_GET_MEDIA_ITEMS_METADATA
).
build
());
when
(
mockPlayer
.
isCommandAvailable
(
Player
.
COMMAND_GET_MEDIA_ITEMS_METADATA
)).
thenReturn
(
true
);
when
(
mockPlayer
.
getMediaMetadata
()).
thenReturn
(
mediaMetadata
);
}
else
{
when
(
mockPlayer
.
getAvailableCommands
()).
thenReturn
(
Commands
.
EMPTY
);
}
MediaSession
mockMediaSession
=
mock
(
MediaSession
.
class
);
when
(
mockMediaSession
.
getPlayer
()).
thenReturn
(
mockPlayer
);
MediaSessionImpl
mockMediaSessionImpl
=
mock
(
MediaSessionImpl
.
class
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment