Conserve activity flag after notif update

When reloading the metadata or changing the stream state, let's conserve
the previous activity state.

Fixes: 159642373
Test: manual
Test: atest MediaDataManagerTest
Test: atest MediaTimeoutListenerTest
Change-Id: I23cc9485c36be31c3fc2d6b3935dc829f2131afd
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt b/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt
index 416c81a..5052386 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt
@@ -432,12 +432,13 @@
             }
         }
 
-        val resumeAction: Runnable? = mediaEntries.get(key)?.resumeAction
-        val hasCheckedForResume = mediaEntries.get(key)?.hasCheckedForResume == true
         foregroundExecutor.execute {
+            val resumeAction: Runnable? = mediaEntries[key]?.resumeAction
+            val hasCheckedForResume = mediaEntries[key]?.hasCheckedForResume == true
+            val active = mediaEntries[key]?.active ?: true
             onMediaDataLoaded(key, oldKey, MediaData(true, bgColor, app, smallIconDrawable, artist,
                     song, artWorkIcon, actionIcons, actionsToShowCollapsed, sbn.packageName, token,
-                    notif.contentIntent, null, active = true, resumeAction = resumeAction,
+                    notif.contentIntent, null, active, resumeAction = resumeAction,
                     notificationKey = key, hasCheckedForResume = hasCheckedForResume))
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaTimeoutListener.kt b/packages/SystemUI/src/com/android/systemui/media/MediaTimeoutListener.kt
index cf8a636..9a134db 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaTimeoutListener.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaTimeoutListener.kt
@@ -71,6 +71,7 @@
     ) : MediaController.Callback() {
 
         var timedOut = false
+        private var playing: Boolean? = null
 
         // Resume controls may have null token
         private val mediaController = if (data.token != null) {
@@ -94,7 +95,13 @@
                 Log.v(TAG, "onPlaybackStateChanged: $state")
             }
 
-            if (state == null || !isPlayingState(state.state)) {
+            val isPlaying = state != null && isPlayingState(state.state)
+            if (playing == isPlaying && playing != null) {
+                return
+            }
+            playing = isPlaying
+
+            if (!isPlaying) {
                 if (DEBUG) {
                     Log.v(TAG, "schedule timeout for $key")
                 }