From a3d9098c85316cf6f5b78fe1df26a5d78f8fdd16 Mon Sep 17 00:00:00 2001 From: Beth Thibodeau Date: Mon, 13 Apr 2020 23:42:48 -0400 Subject: [PATCH] Match players based on notification key One player per package caused problems with RCN because all RCN notifications come from the same package. We can't remove this step completely because then every time the user restarts media it would create a new set of controls. Instead, match based on the notification key. This will allow one player per notification tag + ID, so RCN can use different IDs per device to have multiple players visible at the same time. This is consistent with how NotificationManager identifies notifications to update. Fixes: 153236983 Test: manual - used cast test apk and cast to multiple devices on a different phone, observed multiple players Change-Id: Iaa98b4180725209982beb079d96bfedaa9742b58 --- .../android/systemui/media/MediaControlPanel.java | 14 ++++++++++++-- .../src/com/android/systemui/qs/QSMediaPlayer.java | 7 +++---- .../src/com/android/systemui/qs/QSPanel.java | 14 +++++++------- .../android/systemui/qs/QuickQSMediaPlayer.java | 5 +++-- .../NotificationMediaTemplateViewWrapper.java | 6 ++++-- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java index 62efd8ce4cee9..8492fef97df54 100644 --- a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java +++ b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java @@ -82,6 +82,7 @@ public class MediaControlPanel { protected ComponentName mRecvComponent; private MediaDevice mDevice; private boolean mIsRegistered = false; + private String mKey; private final int[] mActionIds; @@ -203,14 +204,15 @@ public class MediaControlPanel { * @param bgColor * @param contentIntent * @param appNameString - * @param device + * @param key */ public void setMediaSession(MediaSession.Token token, Icon icon, int iconColor, - int bgColor, PendingIntent contentIntent, String appNameString) { + int bgColor, PendingIntent contentIntent, String appNameString, String key) { mToken = token; mForegroundColor = iconColor; mBackgroundColor = bgColor; mController = new MediaController(mContext, mToken); + mKey = key; MediaMetadata mediaMetadata = mController.getMetadata(); @@ -325,6 +327,14 @@ public class MediaControlPanel { return mController.getPackageName(); } + /** + * Return the original notification's key + * @return The notification key + */ + public String getKey() { + return mKey; + } + /** * Check whether this player has an attached media session. * @return whether there is a controller with a current media session. diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java b/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java index e636707a97221..c943884b83476 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java @@ -99,15 +99,14 @@ public class QSMediaPlayer extends MediaControlPanel { * @param bgColor background color * @param actionsContainer a LinearLayout containing the media action buttons * @param notif reference to original notification - * @param device current playback device + * @param key original notification's key */ public void setMediaSession(MediaSession.Token token, Icon icon, int iconColor, - int bgColor, View actionsContainer, Notification notif) { + int bgColor, View actionsContainer, Notification notif, String key) { String appName = Notification.Builder.recoverBuilder(getContext(), notif) .loadHeaderAppName(); - super.setMediaSession(token, icon, iconColor, bgColor, notif.contentIntent, - appName); + super.setMediaSession(token, icon, iconColor, bgColor, notif.contentIntent, appName, key); // Media controls LinearLayout parentActionsLayout = (LinearLayout) actionsContainer; diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java index 0566b2e621db2..40c8aadfd5d4d 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java @@ -208,9 +208,10 @@ public class QSPanel extends LinearLayout implements Tunable, Callback, Brightne * @param bgColor * @param actionsContainer * @param notif + * @param key */ public void addMediaSession(MediaSession.Token token, Icon icon, int iconColor, int bgColor, - View actionsContainer, StatusBarNotification notif) { + View actionsContainer, StatusBarNotification notif, String key) { if (!useQsMediaPlayer(mContext)) { // Shouldn't happen, but just in case Log.e(TAG, "Tried to add media session without player!"); @@ -225,13 +226,12 @@ public class QSPanel extends LinearLayout implements Tunable, Callback, Brightne String packageName = notif.getPackageName(); for (QSMediaPlayer p : mMediaPlayers) { if (p.getMediaSessionToken().equals(token)) { - Log.d(TAG, "a player for this session already exists"); + Log.d(TAG, "Found matching player by token " + packageName); player = p; break; - } - - if (packageName.equals(p.getMediaPlayerPackage())) { - Log.d(TAG, "found an old session for this app"); + } else if (packageName.equals(p.getMediaPlayerPackage()) && key.equals(p.getKey())) { + // Also match if it's the same package and notification key + Log.d(TAG, "Found matching player by package " + packageName + ", " + key); player = p; break; } @@ -267,7 +267,7 @@ public class QSPanel extends LinearLayout implements Tunable, Callback, Brightne Log.d(TAG, "setting player session"); player.setMediaSession(token, icon, iconColor, bgColor, actionsContainer, - notif.getNotification()); + notif.getNotification(), key); if (mMediaPlayers.size() > 0) { ((View) mMediaCarousel.getParent()).setVisibility(View.VISIBLE); diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickQSMediaPlayer.java b/packages/SystemUI/src/com/android/systemui/qs/QuickQSMediaPlayer.java index 0ba4cb159024b..794677912626c 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QuickQSMediaPlayer.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QuickQSMediaPlayer.java @@ -67,9 +67,10 @@ public class QuickQSMediaPlayer extends MediaControlPanel { * @param actionsToShow indices of which actions to display in the mini player * (max 3: Notification.MediaStyle.MAX_MEDIA_BUTTONS_IN_COMPACT) * @param contentIntent Intent to send when user taps on the view + * @param key original notification's key */ public void setMediaSession(MediaSession.Token token, Icon icon, int iconColor, int bgColor, - View actionsContainer, int[] actionsToShow, PendingIntent contentIntent) { + View actionsContainer, int[] actionsToShow, PendingIntent contentIntent, String key) { // Only update if this is a different session and currently playing String oldPackage = ""; if (getController() != null) { @@ -84,7 +85,7 @@ public class QuickQSMediaPlayer extends MediaControlPanel { return; } - super.setMediaSession(token, icon, iconColor, bgColor, contentIntent, null); + super.setMediaSession(token, icon, iconColor, bgColor, contentIntent, null, key); LinearLayout parentActionsLayout = (LinearLayout) actionsContainer; int i = 0; diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/wrapper/NotificationMediaTemplateViewWrapper.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/wrapper/NotificationMediaTemplateViewWrapper.java index 874d81db0bd20..2da2724aacb20 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/wrapper/NotificationMediaTemplateViewWrapper.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/wrapper/NotificationMediaTemplateViewWrapper.java @@ -193,7 +193,8 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi mBackgroundColor, mActions, compactActions, - notif.contentIntent); + notif.contentIntent, + sbn.getKey()); QSPanel bigPanel = ctrl.getNotificationShadeView().findViewById( com.android.systemui.R.id.quick_settings_panel); bigPanel.addMediaSession(token, @@ -201,7 +202,8 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi tintColor, mBackgroundColor, mActions, - sbn); + sbn, + sbn.getKey()); } boolean showCompactSeekbar = mMediaManager.getShowCompactMediaSeekbar();