am 8f095396: am 7fbcac6a: Merge "Tweak the behavior of the MediaSessionStack" into lmp-mr1-dev

* commit '8f0953963d91a22021676cc29b0f572c1a913124':
  Tweak the behavior of the MediaSessionStack
This commit is contained in:
RoboErik
2014-12-05 15:51:39 +00:00
committed by Android Git Automerger
2 changed files with 35 additions and 4 deletions

View File

@@ -443,7 +443,7 @@ public class MediaSessionService extends SystemService implements Monitor {
synchronized (mLock) { synchronized (mLock) {
List<MediaSessionRecord> records = mPriorityStack.getActiveSessions(userId); List<MediaSessionRecord> records = mPriorityStack.getActiveSessions(userId);
int size = records.size(); int size = records.size();
if (size > 0) { if (size > 0 && records.get(0).isPlaybackActive(false)) {
rememberMediaButtonReceiverLocked(records.get(0)); rememberMediaButtonReceiverLocked(records.get(0));
} }
ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>(); ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>();
@@ -702,8 +702,12 @@ public class MediaSessionService extends SystemService implements Monitor {
try { try {
synchronized (mLock) { synchronized (mLock) {
// If we don't have a media button receiver to fall back on
// include non-playing sessions for dispatching
boolean useNotPlayingSessions = mUserRecords.get(
ActivityManager.getCurrentUser()).mLastMediaButtonReceiver == null;
MediaSessionRecord session = mPriorityStack MediaSessionRecord session = mPriorityStack
.getDefaultMediaButtonSession(mCurrentUserId); .getDefaultMediaButtonSession(mCurrentUserId, useNotPlayingSessions);
if (isVoiceKey(keyEvent.getKeyCode())) { if (isVoiceKey(keyEvent.getKeyCode())) {
handleVoiceKeyEventLocked(keyEvent, needWakeLock, session); handleVoiceKeyEventLocked(keyEvent, needWakeLock, session);
} else { } else {

View File

@@ -51,6 +51,9 @@ public class MediaSessionStack {
private MediaSessionRecord mGlobalPrioritySession; private MediaSessionRecord mGlobalPrioritySession;
// The last record that either entered one of the playing states or was
// added.
private MediaSessionRecord mLastInterestingRecord;
private MediaSessionRecord mCachedButtonReceiver; private MediaSessionRecord mCachedButtonReceiver;
private MediaSessionRecord mCachedDefault; private MediaSessionRecord mCachedDefault;
private MediaSessionRecord mCachedVolumeDefault; private MediaSessionRecord mCachedVolumeDefault;
@@ -65,6 +68,7 @@ public class MediaSessionStack {
public void addSession(MediaSessionRecord record) { public void addSession(MediaSessionRecord record) {
mSessions.add(record); mSessions.add(record);
clearCache(); clearCache();
mLastInterestingRecord = record;
} }
/** /**
@@ -93,6 +97,9 @@ public class MediaSessionStack {
mSessions.remove(record); mSessions.remove(record);
mSessions.add(0, record); mSessions.add(0, record);
clearCache(); clearCache();
// This becomes the last interesting record since it entered a
// playing state
mLastInterestingRecord = record;
return true; return true;
} else if (!MediaSession.isActiveState(newState)) { } else if (!MediaSession.isActiveState(newState)) {
// Just clear the volume cache when a state goes inactive // Just clear the volume cache when a state goes inactive
@@ -168,9 +175,11 @@ public class MediaSessionStack {
* Get the highest priority session that can handle media buttons. * Get the highest priority session that can handle media buttons.
* *
* @param userId The user to check. * @param userId The user to check.
* @param includeNotPlaying Return a non-playing session if nothing else is
* available
* @return The default media button session or null. * @return The default media button session or null.
*/ */
public MediaSessionRecord getDefaultMediaButtonSession(int userId) { public MediaSessionRecord getDefaultMediaButtonSession(int userId, boolean includeNotPlaying) {
if (mGlobalPrioritySession != null && mGlobalPrioritySession.isActive()) { if (mGlobalPrioritySession != null && mGlobalPrioritySession.isActive()) {
return mGlobalPrioritySession; return mGlobalPrioritySession;
} }
@@ -180,7 +189,25 @@ public class MediaSessionStack {
ArrayList<MediaSessionRecord> records = getPriorityListLocked(true, ArrayList<MediaSessionRecord> records = getPriorityListLocked(true,
MediaSession.FLAG_HANDLES_MEDIA_BUTTONS, userId); MediaSession.FLAG_HANDLES_MEDIA_BUTTONS, userId);
if (records.size() > 0) { if (records.size() > 0) {
mCachedButtonReceiver = records.get(0); MediaSessionRecord record = records.get(0);
if (record.isPlaybackActive(false)) {
// Since we're going to send a button event to this record make
// it the last interesting one.
mLastInterestingRecord = record;
mCachedButtonReceiver = record;
} else if (mLastInterestingRecord != null) {
if (records.contains(mLastInterestingRecord)) {
mCachedButtonReceiver = mLastInterestingRecord;
} else {
// That record is no longer used. Clear its reference.
mLastInterestingRecord = null;
}
}
if (includeNotPlaying && mCachedButtonReceiver == null) {
// If we really want a record and we didn't find one yet use the
// highest priority session even if it's not playing.
mCachedButtonReceiver = record;
}
} }
return mCachedButtonReceiver; return mCachedButtonReceiver;
} }