Fix output switcher will show 2 media session when remote playing

Before this CL, output switcher will show 2 media sessions when
some apps are remote playing. The root cause is some apps will
also create local media sessions when they cast media to remote
playing.

This CL add condition to only show remote sessions on output switcher
if apps both have remote and local sessions.

Bug: 169052790
Test: make -j42 RunSettingsRoboTests
Change-Id: I80479d35b2bb2e353cf41f41f457f2dfd15cadbf
This commit is contained in:
Hugh Chen
2020-10-07 15:55:51 +08:00
parent 79dc82fb6f
commit e16a8077b5
6 changed files with 274 additions and 44 deletions

View File

@@ -259,4 +259,29 @@ public class MediaOutputIndicatorWorkerTest {
assertThat(mMediaOutputIndicatorWorker.getActiveLocalMediaController()).isNull();
}
@Test
public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
final MediaController.PlaybackInfo playbackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
final PlaybackState playbackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController);
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
when(remoteMediaController.getPlaybackInfo()).thenReturn(playbackInfo);
when(remoteMediaController.getPlaybackState()).thenReturn(playbackState);
assertThat(mMediaOutputIndicatorWorker.getActiveLocalMediaController()).isNull();
}
}

View File

@@ -0,0 +1,153 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.media;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.VolumeProvider;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class MediaOutputUtilsTest {
@Mock
private MediaSessionManager mMediaSessionManager;
@Mock
private MediaController mMediaController;
private Context mContext;
private List<MediaController> mMediaControllers = new ArrayList<>();
private PlaybackState mPlaybackState;
private MediaController.PlaybackInfo mPlaybackInfo;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mMediaSessionManager).when(mContext).getSystemService(MediaSessionManager.class);
mMediaControllers.add(mMediaController);
doReturn(mMediaControllers).when(mMediaSessionManager).getActiveSessions(any());
}
@Test
public void getActiveLocalMediaController_localMediaPlaying_returnController() {
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isEqualTo(
mMediaController);
}
@Test
public void getActiveLocalMediaController_remoteMediaPlaying_returnNull() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_localMediaStopped_returnNull() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_STOPPED, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
final MediaController.PlaybackInfo playbackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
final PlaybackState playbackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController);
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
when(remoteMediaController.getPlaybackInfo()).thenReturn(playbackInfo);
when(remoteMediaController.getPlaybackState()).thenReturn(playbackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
private void initPlayback() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
}
}