MediaSessionService: Nullify the media button sesison when it's removed
When the media button sesison is removed, we'd better not pick the
alternative media button session within the app because the alternative
media session might be a dummy which isn't able to handle media keys.
Test: Run the CtsMediaTest (MediaSessionTest and MediaSessionManagerTest)
and the manual test (Launch a media app which creates two media
sessions, and confirmed that another media session doesn't become the
media button session when the media button session is removed)
Change-Id: I376a34ee4b5d2344007594134c40b135b2c5a1bf
diff --git a/services/core/java/com/android/server/media/MediaSessionStack.java b/services/core/java/com/android/server/media/MediaSessionStack.java
index 2f82915..0e69bca 100644
--- a/services/core/java/com/android/server/media/MediaSessionStack.java
+++ b/services/core/java/com/android/server/media/MediaSessionStack.java
@@ -121,9 +121,12 @@
public void removeSession(MediaSessionRecord record) {
mSessions.remove(record);
if (mMediaButtonSession == record) {
- // When the media button session is gone, try to find the alternative media session
- // in the media button session app.
- onMediaSessionChangeInMediaButtonSessionApp();
+ // When the media button session is removed, nullify the media button session and do not
+ // search for the alternative media session within the app. It's because the alternative
+ // media session might be a dummy which isn't able to handle the media key events.
+ mOnMediaButtonSessionChangedListener.onMediaButtonSessionChanged(
+ mMediaButtonSession, null);
+ mMediaButtonSession = null;
}
clearCache(record.getUserId());
}
@@ -157,7 +160,13 @@
// In that case, we pick the media session whose PlaybackState matches
// the audio playback configuration.
if (mMediaButtonSession != null && mMediaButtonSession.getUid() == record.getUid()) {
- onMediaSessionChangeInMediaButtonSessionApp();
+ MediaSessionRecord newMediaButtonSession =
+ findMediaButtonSession(mMediaButtonSession.getUid());
+ if (newMediaButtonSession != mMediaButtonSession) {
+ mOnMediaButtonSessionChangedListener.onMediaButtonSessionChanged(
+ mMediaButtonSession, newMediaButtonSession);
+ mMediaButtonSession = newMediaButtonSession;
+ }
}
}
@@ -200,22 +209,6 @@
}
/**
- * Handle the change in a media session in the media button session app.
- * <p>If the app has multiple media sessions, change in a media sesion in the app may change
- * the media button session.
- * @see #findMediaButtonSession
- */
- private void onMediaSessionChangeInMediaButtonSessionApp() {
- MediaSessionRecord newMediaButtonSession =
- findMediaButtonSession(mMediaButtonSession.getUid());
- if (newMediaButtonSession != mMediaButtonSession) {
- mOnMediaButtonSessionChangedListener.onMediaButtonSessionChanged(mMediaButtonSession,
- newMediaButtonSession);
- mMediaButtonSession = newMediaButtonSession;
- }
- }
-
- /**
* Find the media button session with the given {@param uid}.
* If the app has multiple media sessions, the media session matches the audio playback state
* becomes the media button session.