Avoid notifying connection twice from MediaControllerImplLegacy.

The connection to a legacy MediaSession may receive additional
onSessionReady callbacks that are treated as additional state updates.
We currently also set the "notifyConnected" flag for these updates
even though we are connected already, causing an IllegalStateException.

Fix the exception by not setting this flag.

We can also remove the wording about "locked" updates since this class
operates everything on a single application thread.

Issue: androidx/media#49
PiperOrigin-RevId: 487487286
This commit is contained in:
tonihei 2022-11-10 11:10:11 +00:00 committed by microkatz
parent 07f0589ce7
commit b24161a6f6
2 changed files with 10 additions and 10 deletions

View file

@ -99,6 +99,9 @@ Release notes
`DefaultNotificationProvider` on API 26 and API 27 (the badge is `DefaultNotificationProvider` on API 26 and API 27 (the badge is
automatically hidden on API 28+) automatically hidden on API 28+)
([#131](https://github.com/androidx/media/issues/131)). ([#131](https://github.com/androidx/media/issues/131)).
* Fix bug where a second binder connection from a legacy MediaSession to a
Media3 MediaController causes IllegalStateExceptions
([#49](https://github.com/androidx/media/issues/49)).
* RTSP: * RTSP:
* Add H263 fragmented packet handling * Add H263 fragmented packet handling
([#119](https://github.com/androidx/media/pull/119)). ([#119](https://github.com/androidx/media/pull/119)).

View file

@ -1211,8 +1211,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
return browserCompat; return browserCompat;
} }
// Should be used without a lock to prevent potential deadlock. void onConnected() {
void onConnectedNotLocked() {
if (released || connected) { if (released || connected) {
return; return;
} }
@ -1240,18 +1239,16 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
controllerCompat.registerCallback( controllerCompat.registerCallback(
controllerCompatCallback, getInstance().applicationHandler); controllerCompatCallback, getInstance().applicationHandler);
}); });
// Post a runnable to prevent callbacks from being called by onConnectedNotLocked() // Post a runnable to prevent callbacks from being called by onConnected()
// before the constructor returns (b/196941334). // before the constructor returns (b/196941334).
getInstance() getInstance()
.applicationHandler .applicationHandler
.post( .post(
() -> { () -> {
if (!controllerCompat.isSessionReady()) { if (!controllerCompat.isSessionReady()) {
// If the session not ready here, then call onConnectedNotLocked() immediately. The // If the session not ready here, then call onConnected() immediately. The session
// session may be a framework MediaSession and we cannot know whether it can be // may be a framework MediaSession and we cannot know whether it can be ready later.
// ready onConnected();
// later.
onConnectedNotLocked();
} }
}); });
} }
@ -1608,7 +1605,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
@Override @Override
public void onSessionReady() { public void onSessionReady() {
if (!connected) { if (!connected) {
onConnectedNotLocked(); onConnected();
} else { } else {
// Handle the case when extra binder is available after connectToSession(). // Handle the case when extra binder is available after connectToSession().
// Initial values are notified already, so only notify values that are available when // Initial values are notified already, so only notify values that are available when
@ -1622,7 +1619,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
onCaptioningEnabledChanged(isCaptioningEnabled); onCaptioningEnabledChanged(isCaptioningEnabled);
pendingChangesHandler.removeMessages(MSG_HANDLE_PENDING_UPDATES); pendingChangesHandler.removeMessages(MSG_HANDLE_PENDING_UPDATES);
handleNewLegacyParameters(/* notifyConnected= */ true, pendingLegacyPlayerInfo); handleNewLegacyParameters(/* notifyConnected= */ false, pendingLegacyPlayerInfo);
} }
} }