Refactor initTrackSelectionAdapter

As suggested in parent change, return a list of
`TrackType` instead of appending to it.

This has the slight disadvantage of iterating twice
over the (short) list, but clarifies the code.

PiperOrigin-RevId: 402844458
This commit is contained in:
krocard 2021-10-13 16:58:04 +01:00 committed by Oliver Woodman
parent 98ee159df1
commit 3b8eba2dea

View File

@ -62,7 +62,6 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.DefaultControlDispatcher;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ForwardingPlayer; import com.google.android.exoplayer2.ForwardingPlayer;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.Events; import com.google.android.exoplayer2.Player.Events;
@ -76,6 +75,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionParameters.Tra
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.RepeatModeUtil; import com.google.android.exoplayer2.util.RepeatModeUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.ArrayList; import java.util.ArrayList;
@ -1251,42 +1251,35 @@ public class StyledPlayerControlView extends FrameLayout {
return; return;
} }
TracksInfo tracksInfo = player.getCurrentTracksInfo(); TracksInfo tracksInfo = player.getCurrentTracksInfo();
List<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos(); audioTrackSelectionAdapter.init(
if (trackGroupInfos.isEmpty()) { gatherSupportedTrackInfosOfType(tracksInfo, C.TRACK_TYPE_AUDIO));
return; if (controlViewLayoutManager.getShowButton(subtitleButton)) {
textTrackSelectionAdapter.init(
gatherSupportedTrackInfosOfType(tracksInfo, C.TRACK_TYPE_TEXT));
} else {
textTrackSelectionAdapter.init(ImmutableList.of());
} }
List<TrackInformation> textTracks = new ArrayList<>(); }
List<TrackInformation> audioTracks = new ArrayList<>();
private ImmutableList<TrackInformation> gatherSupportedTrackInfosOfType(
TracksInfo tracksInfo, @C.TrackType int trackType) {
ImmutableList.Builder<TrackInformation> tracks = new ImmutableList.Builder<>();
List<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos();
for (int trackGroupIndex = 0; trackGroupIndex < trackGroupInfos.size(); trackGroupIndex++) { for (int trackGroupIndex = 0; trackGroupIndex < trackGroupInfos.size(); trackGroupIndex++) {
TrackGroupInfo trackGroupInfo = trackGroupInfos.get(trackGroupIndex); TrackGroupInfo trackGroupInfo = trackGroupInfos.get(trackGroupIndex);
if (!trackGroupInfo.isSupported()) { if (trackGroupInfo.getTrackType() != trackType) {
continue; continue;
} }
if (trackGroupInfo.getTrackType() == C.TRACK_TYPE_TEXT
&& controlViewLayoutManager.getShowButton(subtitleButton)) {
// Get TrackSelection at the corresponding renderer index.
gatherTrackInfosForAdapter(tracksInfo, trackGroupIndex, textTracks);
} else if (trackGroupInfo.getTrackType() == C.TRACK_TYPE_AUDIO) {
gatherTrackInfosForAdapter(tracksInfo, trackGroupIndex, audioTracks);
}
}
textTrackSelectionAdapter.init(textTracks);
audioTrackSelectionAdapter.init(audioTracks);
}
private void gatherTrackInfosForAdapter(
TracksInfo tracksInfo, int trackGroupIndex, List<TrackInformation> tracks) {
TrackGroupInfo trackGroupInfo = tracksInfo.getTrackGroupInfos().get(trackGroupIndex);
TrackGroup trackGroup = trackGroupInfo.getTrackGroup(); TrackGroup trackGroup = trackGroupInfo.getTrackGroup();
for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) { for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
Format format = trackGroup.getFormat(trackIndex); if (!trackGroupInfo.isTrackSupported(trackIndex)) {
if (trackGroupInfo.isTrackSupported(trackIndex)) { continue;
tracks.add( }
new TrackInformation( String trackName = trackNameProvider.getTrackName(trackGroup.getFormat(trackIndex));
tracksInfo, trackGroupIndex, trackIndex, trackNameProvider.getTrackName(format))); tracks.add(new TrackInformation(tracksInfo, trackGroupIndex, trackIndex, trackName));
} }
} }
return tracks.build();
} }
private void updateTimeline() { private void updateTimeline() {