Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d0f49d85 authored by Alex Shabalin's avatar Alex Shabalin Committed by Alexandr Shabalin
Browse files

Fix requesting suggestion after resuming the player.

- `visibleMediaPlayers` used to add a key when the player transitioned
from resumption state while keeping the old key associated with the
resumption state. After the change, the old key is replaced with the new
key.

- Set the `onSuggestionSpaceVisible` callback available to the UI even
if data.resumption is false. This allows the callback to be called when
the resumption player panel just transitioned to be active but haven't
received the new data.

Fix: 441358997
Test: atest MediaCarouselControllerTest, MediaDeviceManagerTest
Test: On a physical device
Flag: com.android.systemui.enable_suggested_device_ui
Change-Id: Ic9b2ef20b29306cf847c950dd42d8b676cd8ef29
parent e98a5dc9
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -242,7 +242,6 @@ constructor(
        fun start() =
            bgExecutor.execute {
                if (!started) {
                    // Fetch in case a suggestion already exists before registering for suggestions
                    localMediaManager.registerCallback(this)
                    if (!Flags.removeUnnecessaryRouteScanning()) {
                        localMediaManager.startScan()
@@ -251,13 +250,19 @@ constructor(
                    playbackType = controller?.playbackInfo?.playbackType ?: PLAYBACK_TYPE_UNKNOWN
                    playbackVolumeControlId = controller?.playbackInfo?.volumeControlId
                    controller?.registerCallback(this)
                    if (enableSuggestedDeviceUi() && !isResumption) {
                        suggestedDeviceManager.addListener(this)
                    if (enableSuggestedDeviceUi()) {
                        updateCurrent(notifyListeners = false)
                        if (isResumption) {
                            // Set the onSuggestionSpaceVisible callback only.
                            updateSuggestion(state = null, notifyListeners = false)
                        } else {
                            suggestedDeviceManager.addListener(this)
                            // Fetch in case a suggestion already exists before requesting a new one
                            updateSuggestion(
                            suggestedDeviceManager.getSuggestedDevice(),
                                state = suggestedDeviceManager.getSuggestedDevice(),
                                notifyListeners = false,
                            )
                        }
                        fgExecutor.execute {
                            processMediaDeviceAndSuggestionData(
                                key,
+11 −0
Original line number Diff line number Diff line
@@ -1185,6 +1185,9 @@ internal object MediaPlayerData {
            return
        }

        if (enableSuggestedDeviceUi()) {
            replaceVisiblePlayerKey(oldKey, newKey)
        }
        mediaData.remove(oldKey)?.let {
            // MediaPlayer should not be visible
            // no need to set isDismissed flag.
@@ -1197,6 +1200,14 @@ internal object MediaPlayerData {
        }
    }

    /** Changes the key in visibleMediaPlayers while preserving the order */
    private fun replaceVisiblePlayerKey(oldKey: String, newKey: String) {
        val newVisibleMediaPlayers =
            visibleMediaPlayers.mapKeys { (key, _) -> if (key == oldKey) newKey else key }
        visibleMediaPlayers.clear()
        visibleMediaPlayers.putAll(newVisibleMediaPlayers)
    }

    fun getMediaControlPanel(visibleIndex: Int): MediaControlPanel? {
        return mediaPlayers[visiblePlayerKeys().elementAt(visibleIndex)]
    }
+17 −5
Original line number Diff line number Diff line
@@ -394,11 +394,23 @@ public class MediaDeviceManagerTest(flags: FlagsParameterization) : SysuiTestCas
        fakeBgExecutor.runAllReady()
        fakeFgExecutor.runAllReady()

        verify(listener, never())
            .onMediaDeviceAndSuggestionDataChanged(eq(KEY), anyOrNull(), any(), any())
        val data = captureDeviceData(KEY)
        assertThat(data.enabled).isTrue()
        assertThat(data.name).isEqualTo(DEVICE_NAME)
        val mediaDeviceCaptor = argumentCaptor<MediaDeviceData>()
        val suggestionCaptor = argumentCaptor<SuggestionData>()
        verify(listener)
            .onMediaDeviceAndSuggestionDataChanged(
                eq(KEY),
                eq(null),
                mediaDeviceCaptor.capture(),
                suggestionCaptor.capture(),
            )
        val deviceData = mediaDeviceCaptor.firstValue
        assertThat(deviceData.enabled).isTrue()
        assertThat(deviceData.name).isEqualTo(DEVICE_NAME)
        assertThat(deviceData.icon).isEqualTo(icon)
        val suggestionData = suggestionCaptor.firstValue
        assertThat(suggestionData.suggestedMediaDeviceData).isNull()
        // The onSuggestionSpaceVisible should be set.
        assertThat(suggestionData.onSuggestionSpaceVisible).isNotNull()
    }

    @Test
+69 −0
Original line number Diff line number Diff line
@@ -392,6 +392,75 @@ class MediaCarouselControllerTest : SysuiTestCase() {
        )
    }

    @EnableFlags(Flags.FLAG_ENABLE_SUGGESTED_DEVICE_UI)
    @Test
    fun testChangingPlayerKeys_visibleMediaPlayersUpdated() {
        verify(mediaDataManager).addListener(capture(listener))

        val key1 = "key1"
        val key2 = "key2"
        val key3 = "key3"
        val newKey = "newKey"

        MediaPlayerData.addMediaPlayer(
            key1,
            DATA.copy(
                active = true,
                isPlaying = true,
                playbackLocation = MediaData.PLAYBACK_LOCAL,
                resumption = false,
                notificationKey = key1,
            ),
            panel,
            clock,
        )

        MediaPlayerData.addMediaPlayer(
            key2,
            DATA.copy(
                active = true,
                isPlaying = false,
                playbackLocation = MediaData.PLAYBACK_LOCAL,
                resumption = true,
                notificationKey = key2,
            ),
            panel,
            clock,
        )

        MediaPlayerData.addMediaPlayer(
            key3,
            DATA.copy(
                active = true,
                isPlaying = false,
                playbackLocation = MediaData.PLAYBACK_LOCAL,
                resumption = true,
                notificationKey = key1,
            ),
            panel,
            clock,
        )

        assertEquals(listOf(key1, key2, key3), MediaPlayerData.visiblePlayerKeys().map { it.key })

        // Replacing key2 with newKey.
        listener.value.onMediaDataLoaded(
            key = newKey,
            oldKey = key2,
            data =
                DATA.copy(
                    active = true,
                    isPlaying = false,
                    playbackLocation = MediaData.PLAYBACK_LOCAL,
                    resumption = false,
                ),
        )
        runAllReady()

        // newKey has the same position as key2 used to have.
        assertEquals(listOf(key1, newKey, key3), MediaPlayerData.visiblePlayerKeys().map { it.key })
    }

    @Test
    fun testSwipeDismiss_logged() {
        mediaCarouselController.mediaCarouselScrollHandler.dismissCallback.invoke()