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

Commit 92bc2cbf authored by Rashi Patil's avatar Rashi Patil Committed by Android (Google) Code Review
Browse files

Merge "Do not show suggestion if content is already casting on the suggested device" into main

parents a755e323 4e8c0cb9
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -752,6 +752,9 @@ public abstract class InfoMediaManager {
                newSuggestedDeviceState = new SuggestedDeviceState(topSuggestion);
            }
        }
        if (newSuggestedDeviceState != null && isSuggestedDeviceSelected(newSuggestedDeviceState)) {
            newSuggestedDeviceState = null;
        }
        if (!Objects.equals(previousState, newSuggestedDeviceState)) {
            mSuggestedDeviceState = newSuggestedDeviceState;
            return true;
@@ -759,6 +762,16 @@ public abstract class InfoMediaManager {
        return false;
    }

    private boolean isSuggestedDeviceSelected(
            @NonNull SuggestedDeviceState newSuggestedDeviceState) {
        return getSelectedMediaDevices().stream().anyMatch(device ->
                Objects.equals(
                        device.getId(),
                        newSuggestedDeviceState
                                .getSuggestedDeviceInfo()
                                .getRouteId()));
    }

    final void onConnectionAttemptedForSuggestion(@NonNull SuggestedDeviceState suggestion) {
        if (!Objects.equals(suggestion, mSuggestedDeviceState)) {
            return;
+47 −0
Original line number Diff line number Diff line
@@ -1294,6 +1294,53 @@ public class InfoMediaManagerTest {
        assertThat(mediaDevice.isSuggestedDevice()).isTrue();
    }

    @EnableFlags(Flags.FLAG_ENABLE_SUGGESTED_DEVICE_API)
    @Test
    public void onSuggestionsUpdated_suggestedIsAlreadySelected_suggestionCleared() {
        MediaRoute2Info selectedRoute =
                new MediaRoute2Info.Builder(TEST_ID_2, "selected_device")
                        .setSystemRoute(true)
                        .addFeature(MediaRoute2Info.FEATURE_LIVE_AUDIO)
                        .build();
        SuggestedDeviceInfo initialSuggestedDeviceInfo =
                new SuggestedDeviceInfo.Builder("initial_suggested_device", TEST_ID_1, 0)
                        .build();
        MediaRoute2Info initialSuggestionRoute =
                new MediaRoute2Info.Builder(TEST_ID_1, "initial_suggested_device_route")
                        .setSystemRoute(false)
                        .addFeature(MediaRoute2Info.FEATURE_LIVE_AUDIO)
                        .build();
        when(mRoutingController.getSelectedRoutes()).thenReturn(List.of(selectedRoute));
        when(mRouter2.getRoutes()).thenReturn(List.of(selectedRoute, initialSuggestionRoute));
        RouterInfoMediaManager mediaManager = createRouterInfoMediaManager();
        mediaManager.registerCallback(mCallback);
        verify(mRouter2).registerDeviceSuggestionsUpdatesCallback(any(),
                mDeviceSuggestionsUpdatesCallback.capture());
        clearInvocations(mCallback);
        mDeviceSuggestionsUpdatesCallback
                .getValue()
                .onSuggestionsUpdated(TEST_PACKAGE_NAME, List.of(initialSuggestedDeviceInfo));
        verify(mCallback).onSuggestedDeviceUpdated(mSuggestedDeviceStateCaptor.capture());
        assertThat(mSuggestedDeviceStateCaptor.getValue()).isNotNull();
        assertThat(mediaManager.getSuggestedDevice().getSuggestedDeviceInfo().getRouteId())
                .isEqualTo(TEST_ID_1);

        // --- Now, trigger the scenario where the suggested device becomes selected ---
        SuggestedDeviceInfo newSuggestedDeviceInfo =
                new SuggestedDeviceInfo.Builder("suggested_selected_device", TEST_ID_2, 0)
                        .build();
        mediaManager.refreshDevices();
        clearInvocations(mCallback);

        mDeviceSuggestionsUpdatesCallback
                .getValue()
                .onSuggestionsUpdated(TEST_PACKAGE_NAME, List.of(newSuggestedDeviceInfo));

        verify(mCallback).onSuggestedDeviceUpdated(mSuggestedDeviceStateCaptor.capture());
        assertThat(mSuggestedDeviceStateCaptor.getValue()).isNull();
        assertThat(mediaManager.getSuggestedDevice()).isNull();
    }

    @EnableFlags(Flags.FLAG_ENABLE_SUGGESTED_DEVICE_API)
    @Test
    public void setDeviceState_suggestionListenerNotified() {