Merge "Set 'the' media notification to the notification actually playing." into mnc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
c2df1ab48d
@@ -436,6 +436,12 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
|
|||||||
public void onPlaybackStateChanged(PlaybackState state) {
|
public void onPlaybackStateChanged(PlaybackState state) {
|
||||||
super.onPlaybackStateChanged(state);
|
super.onPlaybackStateChanged(state);
|
||||||
if (DEBUG_MEDIA) Log.v(TAG, "DEBUG_MEDIA: onPlaybackStateChanged: " + state);
|
if (DEBUG_MEDIA) Log.v(TAG, "DEBUG_MEDIA: onPlaybackStateChanged: " + state);
|
||||||
|
if (state != null) {
|
||||||
|
if (!isPlaybackActive(state.getState())) {
|
||||||
|
clearCurrentMediaNotification();
|
||||||
|
updateMediaMetaData(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -1199,6 +1205,10 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
|
|||||||
if (mHeadsUpManager.isHeadsUp(key)) {
|
if (mHeadsUpManager.isHeadsUp(key)) {
|
||||||
deferRemoval = !mHeadsUpManager.removeNotification(key);
|
deferRemoval = !mHeadsUpManager.removeNotification(key);
|
||||||
}
|
}
|
||||||
|
if (key.equals(mMediaNotificationKey)) {
|
||||||
|
clearCurrentMediaNotification();
|
||||||
|
updateMediaMetaData(true);
|
||||||
|
}
|
||||||
if (deferRemoval) {
|
if (deferRemoval) {
|
||||||
mLatestRankingMap = ranking;
|
mLatestRankingMap = ranking;
|
||||||
mHeadsUpEntriesToRemoveOnSwitch.add(mHeadsUpManager.getEntry(key));
|
mHeadsUpEntriesToRemoveOnSwitch.add(mHeadsUpManager.getEntry(key));
|
||||||
@@ -1492,23 +1502,31 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
|
|||||||
synchronized (mNotificationData) {
|
synchronized (mNotificationData) {
|
||||||
ArrayList<Entry> activeNotifications = mNotificationData.getActiveNotifications();
|
ArrayList<Entry> activeNotifications = mNotificationData.getActiveNotifications();
|
||||||
final int N = activeNotifications.size();
|
final int N = activeNotifications.size();
|
||||||
|
|
||||||
|
// Promote the media notification with a controller in 'playing' state, if any.
|
||||||
Entry mediaNotification = null;
|
Entry mediaNotification = null;
|
||||||
MediaController controller = null;
|
MediaController controller = null;
|
||||||
for (int i = 0; i < N; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
final Entry entry = activeNotifications.get(i);
|
final Entry entry = activeNotifications.get(i);
|
||||||
if (isMediaNotification(entry)) {
|
if (isMediaNotification(entry)) {
|
||||||
final MediaSession.Token token = entry.notification.getNotification().extras
|
final MediaSession.Token token =
|
||||||
|
entry.notification.getNotification().extras
|
||||||
.getParcelable(Notification.EXTRA_MEDIA_SESSION);
|
.getParcelable(Notification.EXTRA_MEDIA_SESSION);
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
controller = new MediaController(mContext, token);
|
MediaController aController = new MediaController(mContext, token);
|
||||||
if (controller != null) {
|
if (PlaybackState.STATE_PLAYING ==
|
||||||
// we've got a live one, here
|
getMediaControllerPlaybackState(aController)) {
|
||||||
|
if (DEBUG_MEDIA) {
|
||||||
|
Log.v(TAG, "DEBUG_MEDIA: found mediastyle controller matching "
|
||||||
|
+ entry.notification.getKey());
|
||||||
|
}
|
||||||
mediaNotification = entry;
|
mediaNotification = entry;
|
||||||
|
controller = aController;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mediaNotification == null) {
|
if (mediaNotification == null) {
|
||||||
// Still nothing? OK, let's just look for live media sessions and see if they match
|
// Still nothing? OK, let's just look for live media sessions and see if they match
|
||||||
// one of our notifications. This will catch apps that aren't (yet!) using media
|
// one of our notifications. This will catch apps that aren't (yet!) using media
|
||||||
@@ -1521,83 +1539,88 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
|
|||||||
UserHandle.USER_ALL);
|
UserHandle.USER_ALL);
|
||||||
|
|
||||||
for (MediaController aController : sessions) {
|
for (MediaController aController : sessions) {
|
||||||
if (aController == null) continue;
|
if (PlaybackState.STATE_PLAYING ==
|
||||||
final PlaybackState state = aController.getPlaybackState();
|
getMediaControllerPlaybackState(aController)) {
|
||||||
if (state == null) continue;
|
// now to see if we have one like this
|
||||||
switch (state.getState()) {
|
final String pkg = aController.getPackageName();
|
||||||
case PlaybackState.STATE_STOPPED:
|
|
||||||
case PlaybackState.STATE_ERROR:
|
|
||||||
continue;
|
|
||||||
default:
|
|
||||||
// now to see if we have one like this
|
|
||||||
final String pkg = aController.getPackageName();
|
|
||||||
|
|
||||||
for (int i = 0; i < N; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
final Entry entry = activeNotifications.get(i);
|
final Entry entry = activeNotifications.get(i);
|
||||||
if (entry.notification.getPackageName().equals(pkg)) {
|
if (entry.notification.getPackageName().equals(pkg)) {
|
||||||
if (DEBUG_MEDIA) {
|
if (DEBUG_MEDIA) {
|
||||||
Log.v(TAG, "DEBUG_MEDIA: found controller matching "
|
Log.v(TAG, "DEBUG_MEDIA: found controller matching "
|
||||||
+ entry.notification.getKey());
|
+ entry.notification.getKey());
|
||||||
}
|
|
||||||
controller = aController;
|
|
||||||
mediaNotification = entry;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
controller = aController;
|
||||||
|
mediaNotification = entry;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sameSessions(mMediaController, controller)) {
|
if (controller != null && !sameSessions(mMediaController, controller)) {
|
||||||
// We have a new media session
|
// We have a new media session
|
||||||
|
clearCurrentMediaNotification();
|
||||||
if (mMediaController != null) {
|
|
||||||
// something old was playing
|
|
||||||
Log.v(TAG, "DEBUG_MEDIA: Disconnecting from old controller: "
|
|
||||||
+ mMediaController);
|
|
||||||
mMediaController.unregisterCallback(mMediaListener);
|
|
||||||
}
|
|
||||||
mMediaController = controller;
|
mMediaController = controller;
|
||||||
|
mMediaController.registerCallback(mMediaListener);
|
||||||
if (mMediaController != null) {
|
mMediaMetadata = mMediaController.getMetadata();
|
||||||
mMediaController.registerCallback(mMediaListener);
|
|
||||||
mMediaMetadata = mMediaController.getMetadata();
|
|
||||||
if (DEBUG_MEDIA) {
|
|
||||||
Log.v(TAG, "DEBUG_MEDIA: insert listener, receive metadata: "
|
|
||||||
+ mMediaMetadata);
|
|
||||||
}
|
|
||||||
|
|
||||||
final String notificationKey = mediaNotification == null
|
|
||||||
? null
|
|
||||||
: mediaNotification.notification.getKey();
|
|
||||||
|
|
||||||
if (notificationKey == null || !notificationKey.equals(mMediaNotificationKey)) {
|
|
||||||
// we have a new notification!
|
|
||||||
if (DEBUG_MEDIA) {
|
|
||||||
Log.v(TAG, "DEBUG_MEDIA: Found new media notification: key="
|
|
||||||
+ notificationKey + " controller=" + controller);
|
|
||||||
}
|
|
||||||
mMediaNotificationKey = notificationKey;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mMediaMetadata = null;
|
|
||||||
mMediaNotificationKey = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
metaDataChanged = true;
|
|
||||||
} else {
|
|
||||||
// Media session unchanged
|
|
||||||
|
|
||||||
if (DEBUG_MEDIA) {
|
if (DEBUG_MEDIA) {
|
||||||
Log.v(TAG, "DEBUG_MEDIA: Continuing media notification: key=" + mMediaNotificationKey);
|
Log.v(TAG, "DEBUG_MEDIA: insert listener, receive metadata: "
|
||||||
|
+ mMediaMetadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mediaNotification != null) {
|
||||||
|
mMediaNotificationKey = mediaNotification.notification.getKey();
|
||||||
|
if (DEBUG_MEDIA) {
|
||||||
|
Log.v(TAG, "DEBUG_MEDIA: Found new media notification: key="
|
||||||
|
+ mMediaNotificationKey + " controller=" + mMediaController);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
metaDataChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (metaDataChanged) {
|
||||||
|
updateNotifications();
|
||||||
|
}
|
||||||
updateMediaMetaData(metaDataChanged);
|
updateMediaMetaData(metaDataChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getMediaControllerPlaybackState(MediaController controller) {
|
||||||
|
if (controller != null) {
|
||||||
|
final PlaybackState playbackState = controller.getPlaybackState();
|
||||||
|
if (playbackState != null) {
|
||||||
|
return playbackState.getState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return PlaybackState.STATE_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isPlaybackActive(int state) {
|
||||||
|
if (state != PlaybackState.STATE_STOPPED
|
||||||
|
&& state != PlaybackState.STATE_ERROR
|
||||||
|
&& state != PlaybackState.STATE_NONE) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearCurrentMediaNotification() {
|
||||||
|
mMediaNotificationKey = null;
|
||||||
|
mMediaMetadata = null;
|
||||||
|
if (mMediaController != null) {
|
||||||
|
if (DEBUG_MEDIA) {
|
||||||
|
Log.v(TAG, "DEBUG_MEDIA: Disconnecting from old controller: "
|
||||||
|
+ mMediaController.getPackageName());
|
||||||
|
}
|
||||||
|
mMediaController.unregisterCallback(mMediaListener);
|
||||||
|
}
|
||||||
|
mMediaController = null;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean sameSessions(MediaController a, MediaController b) {
|
private boolean sameSessions(MediaController a, MediaController b) {
|
||||||
if (a == b) return true;
|
if (a == b) return true;
|
||||||
if (a == null) return false;
|
if (a == null) return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user