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
5595c487
authored
May 05, 2021
by
bachinger
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Optionally deduplicate updating MediaMetadataCompat
PiperOrigin-RevId: 372146631
parent
1f83926d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java
extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java
View file @
5595c487
...
@@ -59,6 +59,7 @@ import java.util.Collections;
...
@@ -59,6 +59,7 @@ import java.util.Collections;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
org.checkerframework.checker.nullness.qual.EnsuresNonNullIf
;
import
org.checkerframework.checker.nullness.qual.EnsuresNonNullIf
;
/**
/**
...
@@ -420,6 +421,45 @@ public final class MediaSessionConnector {
...
@@ -420,6 +421,45 @@ public final class MediaSessionConnector {
* @return The {@link MediaMetadataCompat} to be published to the session.
* @return The {@link MediaMetadataCompat} to be published to the session.
*/
*/
MediaMetadataCompat
getMetadata
(
Player
player
);
MediaMetadataCompat
getMetadata
(
Player
player
);
/** Returns whether the old and the new metadata are considered the same. */
default
boolean
sameAs
(
MediaMetadataCompat
oldMetadata
,
MediaMetadataCompat
newMetadata
)
{
if
(
oldMetadata
==
newMetadata
)
{
return
true
;
}
if
(
oldMetadata
.
size
()
!=
newMetadata
.
size
())
{
return
false
;
}
Set
<
String
>
oldKeySet
=
oldMetadata
.
keySet
();
Bundle
oldMetadataBundle
=
oldMetadata
.
getBundle
();
Bundle
newMetadataBundle
=
newMetadata
.
getBundle
();
for
(
String
key
:
oldKeySet
)
{
Object
oldProperty
=
oldMetadataBundle
.
get
(
key
);
Object
newProperty
=
newMetadataBundle
.
get
(
key
);
if
(
oldProperty
==
newProperty
)
{
continue
;
}
if
(
oldProperty
instanceof
Bitmap
&&
newProperty
instanceof
Bitmap
)
{
if
(!((
Bitmap
)
oldProperty
).
sameAs
(((
Bitmap
)
newProperty
)))
{
return
false
;
}
}
else
if
(
oldProperty
instanceof
RatingCompat
&&
newProperty
instanceof
RatingCompat
)
{
RatingCompat
oldRating
=
(
RatingCompat
)
oldProperty
;
RatingCompat
newRating
=
(
RatingCompat
)
newProperty
;
if
(
oldRating
.
hasHeart
()
!=
newRating
.
hasHeart
()
||
oldRating
.
isRated
()
!=
newRating
.
isRated
()
||
oldRating
.
isThumbUp
()
!=
newRating
.
isThumbUp
()
||
oldRating
.
getPercentRating
()
!=
newRating
.
getPercentRating
()
||
oldRating
.
getStarRating
()
!=
newRating
.
getStarRating
()
||
oldRating
.
getRatingStyle
()
!=
newRating
.
getRatingStyle
())
{
return
false
;
}
}
else
if
(!
Util
.
areEqual
(
oldProperty
,
newProperty
))
{
return
false
;
}
}
return
true
;
}
}
}
/** The wrapped {@link MediaSessionCompat}. */
/** The wrapped {@link MediaSessionCompat}. */
...
@@ -446,6 +486,7 @@ public final class MediaSessionConnector {
...
@@ -446,6 +486,7 @@ public final class MediaSessionConnector {
@Nullable
private
MediaButtonEventHandler
mediaButtonEventHandler
;
@Nullable
private
MediaButtonEventHandler
mediaButtonEventHandler
;
private
long
enabledPlaybackActions
;
private
long
enabledPlaybackActions
;
private
boolean
metadataDeduplicationEnabled
;
/**
/**
* Creates an instance.
* Creates an instance.
...
@@ -685,6 +726,19 @@ public final class MediaSessionConnector {
...
@@ -685,6 +726,19 @@ public final class MediaSessionConnector {
}
}
/**
/**
* Sets whether {@link MediaMetadataProvider#sameAs(MediaMetadataCompat, MediaMetadataCompat)}
* should be consulted before calling {@link MediaSessionCompat#setMetadata(MediaMetadataCompat)}.
*
* <p>Note that this comparison is normally only required when you are using media sources that
* may introduce duplicate updates of the metadata for the same media item (e.g. live streams).
*
* @param metadataDeduplicationEnabled Whether to deduplicate metadata objects on invalidation.
*/
public
void
setMetadataDeduplicationEnabled
(
boolean
metadataDeduplicationEnabled
)
{
this
.
metadataDeduplicationEnabled
=
metadataDeduplicationEnabled
;
}
/**
* Updates the metadata of the media session.
* Updates the metadata of the media session.
*
*
* <p>Apps normally only need to call this method when the backing data for a given media item has
* <p>Apps normally only need to call this method when the backing data for a given media item has
...
@@ -698,6 +752,14 @@ public final class MediaSessionConnector {
...
@@ -698,6 +752,14 @@ public final class MediaSessionConnector {
mediaMetadataProvider
!=
null
&&
player
!=
null
mediaMetadataProvider
!=
null
&&
player
!=
null
?
mediaMetadataProvider
.
getMetadata
(
player
)
?
mediaMetadataProvider
.
getMetadata
(
player
)
:
METADATA_EMPTY
;
:
METADATA_EMPTY
;
@Nullable
MediaMetadataProvider
mediaMetadataProvider
=
this
.
mediaMetadataProvider
;
if
(
metadataDeduplicationEnabled
&&
mediaMetadataProvider
!=
null
)
{
@Nullable
MediaMetadataCompat
oldMetadata
=
mediaSession
.
getController
().
getMetadata
();
if
(
oldMetadata
!=
null
&&
mediaMetadataProvider
.
sameAs
(
oldMetadata
,
metadata
))
{
// Do not update if metadata did not change.
return
;
}
}
mediaSession
.
setMetadata
(
metadata
);
mediaSession
.
setMetadata
(
metadata
);
}
}
...
...
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