Don't reject notifications and wait until new addressed player is ready (1/2)

There are some carkits like the ones found in the 2016 Honda CRZ and some
Audi's that do not follow the spec and do not honor reject pending notification
messages after switching players. This causes an issue whenever you switch
players, the metadata freezes due to the fact that the carkit never re-registers
for new track changed notifications. This patch removes the reject notification
and reorders the current notifications.

Bug: 64142363
Test: Test with Audi S7 and 2016 Honda CRZ and see that switching players works
      TestTracker/105391/3975
Change-Id: Ifcc236c54ccbec8e8ec0bd7f16e62e4282361bac
Merged-In: Ifcc236c54ccbec8e8ec0bd7f16e62e4282361bac
(cherry picked from commit 67c982aed4e67772e155f934c2055c72b83f1f22)
(cherry picked from commit ee17270444b9abc2711fc6323513a8d589515f0c)
diff --git a/src/com/android/bluetooth/avrcp/AddressedMediaPlayer.java b/src/com/android/bluetooth/avrcp/AddressedMediaPlayer.java
index cc0957e..d2c941e 100644
--- a/src/com/android/bluetooth/avrcp/AddressedMediaPlayer.java
+++ b/src/com/android/bluetooth/avrcp/AddressedMediaPlayer.java
@@ -149,7 +149,7 @@
 
     private void sendNowPlayingListChanged() {
         if (mMediaInterface == null) return;
-        mMediaInterface.uidsChangedRsp(AvrcpConstants.NOTIFICATION_TYPE_CHANGED);
+        if (DEBUG) Log.d(TAG, "sendNowPlayingListChanged()");
         mMediaInterface.nowPlayingChangedRsp(AvrcpConstants.NOTIFICATION_TYPE_CHANGED);
         mNowPlayingListUpdated = false;
     }
diff --git a/src/com/android/bluetooth/avrcp/Avrcp.java b/src/com/android/bluetooth/avrcp/Avrcp.java
index 61d7adf..274c10b 100644
--- a/src/com/android/bluetooth/avrcp/Avrcp.java
+++ b/src/com/android/bluetooth/avrcp/Avrcp.java
@@ -371,13 +371,13 @@
         @Override
         public void onMetadataChanged(MediaMetadata metadata) {
             if (DEBUG) Log.v(TAG, "onMetadataChanged");
-            updateCurrentMediaState(false);
+            updateCurrentMediaState();
         }
         @Override
         public synchronized void onPlaybackStateChanged(PlaybackState state) {
             if (DEBUG) Log.v(TAG, "onPlaybackStateChanged: state " + state.toString());
 
-            updateCurrentMediaState(false);
+            updateCurrentMediaState();
         }
 
         @Override
@@ -477,7 +477,7 @@
             case MSG_NOW_PLAYING_CHANGED_RSP:
                 if (DEBUG) Log.v(TAG, "MSG_NOW_PLAYING_CHANGED_RSP");
                 removeMessages(MSG_NOW_PLAYING_CHANGED_RSP);
-                updateCurrentMediaState(false);
+                updateCurrentMediaState();
                 break;
 
             case MSG_PLAY_INTERVAL_TIMEOUT:
@@ -691,7 +691,7 @@
             case MSG_SET_A2DP_AUDIO_STATE:
                 if (DEBUG) Log.v(TAG, "MSG_SET_A2DP_AUDIO_STATE:" + msg.arg1);
                 mA2dpState = msg.arg1;
-                updateCurrentMediaState(false);
+                updateCurrentMediaState();
                 break;
 
             case MSG_NATIVE_REQ_GET_FOLDER_ITEMS: {
@@ -962,36 +962,8 @@
         }
     }
 
-    private void updateCurrentMediaState(boolean registering) {
+    private void updateCurrentMediaState() {
         // Only do player updates when we aren't registering for track changes.
-        if (!registering) {
-            if (mAvailablePlayerViewChanged) {
-                registerNotificationRspAvalPlayerChangedNative(
-                        AvrcpConstants.NOTIFICATION_TYPE_CHANGED);
-                mAvailablePlayerViewChanged = false;
-                return;
-            }
-            if (mAddrPlayerChangedNT == AvrcpConstants.NOTIFICATION_TYPE_INTERIM
-                    && mReportedPlayerID != mCurrAddrPlayerID) {
-                registerNotificationRspAvalPlayerChangedNative(
-                        AvrcpConstants.NOTIFICATION_TYPE_CHANGED);
-                mAvailablePlayerViewChanged = false;
-                registerNotificationRspAddrPlayerChangedNative(
-                        AvrcpConstants.NOTIFICATION_TYPE_CHANGED, mCurrAddrPlayerID, sUIDCounter);
-                mAddrPlayerChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
-                // Changing player sends reject to anything else we would notify...
-                mReportedPlayerID = mCurrAddrPlayerID;
-                mPlayStatusChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
-                mTrackChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
-                mPlayPosChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
-                mNowPlayingListChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
-                mAddressedMediaPlayer.updateNowPlayingList(mMediaController);
-                // If the player changed, they need to re-request anything here again
-                // so we can skip the rest of the update.
-                return;
-            }
-        }
-
         MediaAttributes currentAttributes;
         PlaybackState newState = updatePlaybackState();
 
@@ -1013,6 +985,30 @@
                             + currentAttributes.toRedactedString() + " : "
                             + mMediaAttributes.toRedactedString());
 
+            if (mAvailablePlayerViewChanged) {
+                registerNotificationRspAvalPlayerChangedNative(
+                        AvrcpConstants.NOTIFICATION_TYPE_CHANGED);
+                mAvailablePlayerViewChanged = false;
+                return;
+            }
+
+            if (mAddrPlayerChangedNT == AvrcpConstants.NOTIFICATION_TYPE_INTERIM
+                    && mReportedPlayerID != mCurrAddrPlayerID) {
+                registerNotificationRspAvalPlayerChangedNative(
+                        AvrcpConstants.NOTIFICATION_TYPE_CHANGED);
+                registerNotificationRspAddrPlayerChangedNative(
+                        AvrcpConstants.NOTIFICATION_TYPE_CHANGED, mCurrAddrPlayerID, sUIDCounter);
+
+                mAvailablePlayerViewChanged = false;
+                mAddrPlayerChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
+                mReportedPlayerID = mCurrAddrPlayerID;
+
+                // Update the now playing list without sending the notification
+                mNowPlayingListChangedNT = AvrcpConstants.NOTIFICATION_TYPE_CHANGED;
+                mAddressedMediaPlayer.updateNowPlayingList(mMediaController);
+                mNowPlayingListChangedNT = AvrcpConstants.NOTIFICATION_TYPE_INTERIM;
+            }
+
             // Dont send now playing list changed if the player doesn't support browsing
             MediaPlayerInfo info = getAddressedPlayerInfo();
             if (info != null && info.isBrowseSupported()) {
@@ -1029,21 +1025,25 @@
                 // to update their metadata. Hold of on sending the playback state
                 // update until after we know the current metadata is up to date
                 // and track changed has been sent. This was seen on BMW carkits
-                Log.i(TAG, "Waiting for metadata update to send track changed");
+                Log.i(TAG,
+                        "Waiting for metadata update to send track changed: " + newQueueId + " : "
+                                + currentAttributes + " : " + mMediaAttributes);
 
                 return;
             }
 
             // Notify track changed if:
-            //  - The CT is registering for the notification
+            //  - The CT is registered for the notification
             //  - Queue ID is UNKNOWN and MediaMetadata is different
-            //  - Queue ID is valid and different and MediaMetadata is different
-            if (registering || ((newQueueId == -1 || newQueueId != mLastQueueId)
-                                       && !currentAttributes.equals(mMediaAttributes))) {
+            //  - Queue ID is valid and different from last Queue ID sent
+            if ((newQueueId == -1 || newQueueId != mLastQueueId)
+                    && mTrackChangedNT == AvrcpConstants.NOTIFICATION_TYPE_INTERIM
+                    && !currentAttributes.equals(mMediaAttributes)
+                    && newPlayStatus == PLAYSTATUS_PLAYING) {
                 Log.v(TAG, "Send track changed");
                 mMediaAttributes = currentAttributes;
                 mLastQueueId = newQueueId;
-                sendTrackChangedRsp(registering);
+                sendTrackChangedRsp(false);
             }
         } else {
             Log.i(TAG, "Skipping update due to invalid playback state");
@@ -1631,7 +1631,7 @@
                             Log.v(TAG, "No addressed player but active sessions, taking first.");
                         setAddressedMediaSessionPackage(newControllers.get(0).getPackageName());
                     }
-                    updateCurrentMediaState(false);
+                    updateCurrentMediaState();
                 }
             };
 
@@ -1651,7 +1651,7 @@
         // If the player doesn't exist, we need to add it.
         if (getMediaPlayerInfo(packageName) == null) {
             addMediaPlayerPackage(packageName);
-            updateCurrentMediaState(false);
+            updateCurrentMediaState();
         }
         synchronized (mMediaPlayerInfoList) {
             for (Map.Entry<Integer, MediaPlayerInfo> entry : mMediaPlayerInfoList.entrySet()) {
@@ -1659,7 +1659,7 @@
                     int newAddrID = entry.getKey();
                     if (DEBUG) Log.v(TAG, "Set addressed #" + newAddrID + " " + entry.getValue());
                     updateCurrentController(newAddrID, mCurrBrowsePlayerID);
-                    updateCurrentMediaState(false);
+                    updateCurrentMediaState();
                     return;
                 }
             }
@@ -1740,7 +1740,7 @@
                     addMediaPlayerPackage(packageName);
                 }
             }
-            updateCurrentMediaState(false);
+            updateCurrentMediaState();
         }
     }
 
@@ -1764,7 +1764,7 @@
                 addMediaPlayerController(controller);
             }
 
-            updateCurrentMediaState(false);
+            updateCurrentMediaState();
 
             if (mMediaPlayerInfoList.size() > 0) {
                 // Set the first one as the Addressed Player
@@ -2153,7 +2153,7 @@
                 }
             }
         }
-        updateCurrentMediaState(false);
+        updateCurrentMediaState();
         return registerRsp;
     }
 
@@ -2567,6 +2567,7 @@
         if (!KeyEvent.isMediaKey(code)) {
             Log.w(TAG, "Passthrough non-media key " + op + " (code " + code + ") state " + state);
         }
+
         mMediaSessionManager.dispatchMediaKeyEvent(event);
         addKeyPending(event);
     }