Merge "Do not keep MediaSession Tokens for Notification accounting" into tm-qpr-dev

This commit is contained in:
Jernej Virag
2022-12-21 11:51:44 +00:00
committed by Android (Google) Code Review

View File

@@ -58,10 +58,10 @@ constructor(
// Keep track of the key used for the session tokens. This information is used to know when to
// dispatch a removed event so that a media object for a local session will be removed.
private val keyedTokens: MutableMap<String, MutableSet<MediaSession.Token>> = mutableMapOf()
private val keyedTokens: MutableMap<String, MutableSet<TokenId>> = mutableMapOf()
// Keep track of which media session tokens have associated notifications.
private val tokensWithNotifications: MutableSet<MediaSession.Token> = mutableSetOf()
private val tokensWithNotifications: MutableSet<TokenId> = mutableSetOf()
private val sessionListener =
object : MediaSessionManager.OnActiveSessionsChangedListener {
@@ -101,15 +101,15 @@ constructor(
isSsReactivated: Boolean
) {
backgroundExecutor.execute {
data.token?.let { tokensWithNotifications.add(it) }
data.token?.let { tokensWithNotifications.add(TokenId(it)) }
val isMigration = oldKey != null && key != oldKey
if (isMigration) {
keyedTokens.remove(oldKey)?.let { removed -> keyedTokens.put(key, removed) }
}
if (data.token != null) {
keyedTokens.get(key)?.let { tokens -> tokens.add(data.token) }
keyedTokens.get(key)?.let { tokens -> tokens.add(TokenId(data.token)) }
?: run {
val tokens = mutableSetOf(data.token)
val tokens = mutableSetOf(TokenId(data.token))
keyedTokens.put(key, tokens)
}
}
@@ -125,7 +125,7 @@ constructor(
isMigration ||
remote == null ||
remote.sessionToken == data.token ||
!tokensWithNotifications.contains(remote.sessionToken)
!tokensWithNotifications.contains(TokenId(remote.sessionToken))
) {
// Not filtering in this case. Passing the event along to listeners.
dispatchMediaDataLoaded(key, oldKey, data, immediately)
@@ -136,7 +136,7 @@ constructor(
// If the local session uses a different notification key, then lets go a step
// farther and dismiss the media data so that media controls for the local session
// don't hang around while casting.
if (!keyedTokens.get(key)!!.contains(remote.sessionToken)) {
if (!keyedTokens.get(key)!!.contains(TokenId(remote.sessionToken))) {
dispatchMediaDataRemoved(key)
}
}
@@ -199,6 +199,15 @@ constructor(
packageControllers.put(controller.packageName, tokens)
}
}
tokensWithNotifications.retainAll(controllers.map { it.sessionToken })
tokensWithNotifications.retainAll(controllers.map { TokenId(it.sessionToken) })
}
/**
* Represents a unique identifier for a [MediaSession.Token].
*
* It's used to avoid storing strong binders for media session tokens.
*/
private data class TokenId(val id: Int) {
constructor(token: MediaSession.Token) : this(token.hashCode())
}
}