Merge "[Sound Panel] Fix bugs for sound panel slice" into tm-qpr-dev

This commit is contained in:
Shaowei Shen
2022-10-24 12:43:06 +00:00
committed by Android (Google) Code Review
4 changed files with 71 additions and 15 deletions

View File

@@ -64,7 +64,9 @@ public class MediaOutputUtils {
+ ", play back type : " + pi.getPlaybackType() + ", play back state : " + ", play back type : " + pi.getPlaybackType() + ", play back state : "
+ playbackState.getState()); + playbackState.getState());
} }
if (playbackState.getState() != PlaybackState.STATE_PLAYING) { if (playbackState.getState() == PlaybackState.STATE_STOPPED
|| playbackState.getState() == PlaybackState.STATE_NONE
|| playbackState.getState() == PlaybackState.STATE_ERROR) {
// do nothing // do nothing
continue; continue;
} }

View File

@@ -60,6 +60,7 @@ import com.android.settingslib.utils.ThreadUtils;
import com.google.android.setupdesign.DividerItemDecoration; import com.google.android.setupdesign.DividerItemDecoration;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@@ -339,8 +340,13 @@ public class PanelFragment extends Fragment {
mSliceLiveData.put(uri, sliceLiveData); mSliceLiveData.put(uri, sliceLiveData);
sliceLiveData.observe(getViewLifecycleOwner(), slice -> { sliceLiveData.observe(getViewLifecycleOwner(), slice -> {
// If the Slice has already loaded, do nothing.
// If the Slice has already loaded, refresh list with slice data.
if (mPanelSlicesLoaderCountdownLatch.isSliceLoaded(uri)) { if (mPanelSlicesLoaderCountdownLatch.isSliceLoaded(uri)) {
if (mAdapter != null) {
int itemIndex = (new ArrayList<>(mSliceLiveData.keySet())).indexOf(uri);
mAdapter.notifyItemChanged(itemIndex);
}
return; return;
} }

View File

@@ -84,7 +84,7 @@ public class PanelSlicesAdapter
@Override @Override
public void onBindViewHolder(@NonNull SliceRowViewHolder sliceRowViewHolder, int position) { public void onBindViewHolder(@NonNull SliceRowViewHolder sliceRowViewHolder, int position) {
sliceRowViewHolder.onBind(mSliceLiveData.get(position), position); sliceRowViewHolder.onBind(mSliceLiveData.get(position).getValue());
} }
/** /**
@@ -132,15 +132,14 @@ public class PanelSlicesAdapter
/** /**
* Called when the view is displayed. * Called when the view is displayed.
*/ */
public void onBind(LiveData<Slice> sliceLiveData, int position) { public void onBind(Slice slice) {
sliceLiveData.observe(mPanelFragment.getViewLifecycleOwner(), sliceView);
// Do not show the divider above media devices switcher slice per request
final Slice slice = sliceLiveData.getValue();
// Hides slice which reports with error hint or not contain any slice sub-item. // Hides slice which reports with error hint or not contain any slice sub-item.
if (slice == null || !isValidSlice(slice)) { if (slice == null || !isValidSlice(slice)) {
sliceView.setVisibility(View.GONE); sliceView.setVisibility(View.GONE);
return;
} else {
sliceView.setSlice(slice);
sliceView.setVisibility(View.VISIBLE);
} }
// Add divider for the end icon // Add divider for the end icon
@@ -154,7 +153,7 @@ public class PanelSlicesAdapter
.action(0 /* attribution */, .action(0 /* attribution */,
SettingsEnums.ACTION_PANEL_INTERACTION, SettingsEnums.ACTION_PANEL_INTERACTION,
mMetricsCategory, mMetricsCategory,
sliceLiveData.getValue().getUri().getLastPathSegment() slice.getUri().getLastPathSegment()
/* log key */, /* log key */,
eventInfo.actionType /* value */); eventInfo.actionType /* value */);
}) })

View File

@@ -66,7 +66,18 @@ public class MediaOutputUtilsTest {
@Test @Test
public void getActiveLocalMediaController_localMediaPlaying_returnController() { public void getActiveLocalMediaController_localMediaPlaying_returnController() {
initPlayback(); initPlayback(PlaybackState.STATE_PLAYING);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isEqualTo(
mMediaController);
}
@Test
public void getActiveLocalMediaController_localMediaPause_returnController() {
initPlayback(PlaybackState.STATE_PAUSED);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo); when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState); when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -113,6 +124,44 @@ public class MediaOutputUtilsTest {
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull(); assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
} }
@Test
public void getActiveLocalMediaController_localMediaNone_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_NONE, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_localMediaError_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_ERROR, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test @Test
public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() { public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
mMediaControllers.clear(); mMediaControllers.clear();
@@ -130,7 +179,7 @@ public class MediaOutputUtilsTest {
mMediaControllers.add(remoteMediaController); mMediaControllers.add(remoteMediaController);
mMediaControllers.add(mMediaController); mMediaControllers.add(mMediaController);
initPlayback(); initPlayback(PlaybackState.STATE_PLAYING);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo); when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState); when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -155,7 +204,7 @@ public class MediaOutputUtilsTest {
final MediaController remoteMediaController = mock(MediaController.class); final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController); mMediaControllers.add(remoteMediaController);
initPlayback(); initPlayback(PlaybackState.STATE_PLAYING);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo); when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState); when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -165,7 +214,7 @@ public class MediaOutputUtilsTest {
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull(); assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
} }
private void initPlayback() { private void initPlayback(int playbackState) {
mPlaybackInfo = new MediaController.PlaybackInfo( mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL, MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE, VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
@@ -174,7 +223,7 @@ public class MediaOutputUtilsTest {
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(), new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null); null);
mPlaybackState = new PlaybackState.Builder() mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1) .setState(playbackState, 0, 1)
.build(); .build();
} }
} }