From ec3ba648175a9eba85a399a7d58bb01b7bdb0cb9 Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 17 Feb 2022 16:12:15 +0000 Subject: [PATCH] Simplify TracksInfo API isTypeSupportedOrEmpty is very specific and a little hard to understand unless you know the one thing it's useful for. This commit replaces it with isTypeSupported, which can be used in conjunction with the recently added containsType method. PiperOrigin-RevId: 429312712 --- .../exoplayer2/castdemo/PlayerManager.java | 8 ++--- .../exoplayer2/demo/PlayerActivity.java | 10 +++--- .../google/android/exoplayer2/TracksInfo.java | 33 ++++++++++------- .../android/exoplayer2/TracksInfoTest.java | 36 ++++++++----------- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java b/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java index 8d78ca1112..bd91dbb0da 100644 --- a/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java +++ b/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java @@ -223,12 +223,12 @@ import java.util.ArrayList; if (currentPlayer != localPlayer || tracksInfo == lastSeenTrackGroupInfo) { return; } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO) + && !tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { listener.onUnsupportedTrack(C.TRACK_TYPE_VIDEO); } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO) + && !tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { listener.onUnsupportedTrack(C.TRACK_TYPE_AUDIO); } lastSeenTrackGroupInfo = tracksInfo; diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index abf11e4e36..b7d1650215 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -466,12 +466,14 @@ public class PlayerActivity extends AppCompatActivity if (tracksInfo == lastSeenTracksInfo) { return; } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO) + && !tracksInfo.isTypeSupported( + C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { showToast(R.string.error_unsupported_video); } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO) + && !tracksInfo.isTypeSupported( + C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { showToast(R.string.error_unsupported_audio); } lastSeenTracksInfo = tracksInfo; diff --git a/library/common/src/main/java/com/google/android/exoplayer2/TracksInfo.java b/library/common/src/main/java/com/google/android/exoplayer2/TracksInfo.java index f0998f565e..20af7b84ee 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/TracksInfo.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/TracksInfo.java @@ -284,7 +284,7 @@ public final class TracksInfo implements Bundleable { } /** Returns true if there are tracks of type {@code trackType}, and false otherwise. */ - public boolean hasTracksOfType(@C.TrackType int trackType) { + public boolean containsType(@C.TrackType int trackType) { for (int i = 0; i < trackGroupInfos.size(); i++) { if (trackGroupInfos.get(i).getTrackType() == trackType) { return true; @@ -295,16 +295,15 @@ public final class TracksInfo implements Bundleable { /** * Returns true if at least one track of type {@code trackType} is {@link - * TrackGroupInfo#isTrackSupported(int) supported} or if there are no tracks of this type. + * TrackGroupInfo#isTrackSupported(int) supported}. */ - public boolean isTypeSupportedOrEmpty(@C.TrackType int trackType) { - return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false); + public boolean isTypeSupported(@C.TrackType int trackType) { + return isTypeSupported(trackType, /* allowExceedsCapabilities= */ false); } /** * Returns true if at least one track of type {@code trackType} is {@link - * TrackGroupInfo#isTrackSupported(int, boolean) supported} or if there are no tracks of this - * type. + * TrackGroupInfo#isTrackSupported(int, boolean) supported}. * * @param allowExceedsCapabilities Whether to consider the track as supported if it has a * supported {@link Format#sampleMimeType MIME type}, but otherwise exceeds the advertised @@ -312,19 +311,29 @@ public final class TracksInfo implements Bundleable { * decoder whose maximum advertised resolution is exceeded by the resolution of the track. * Such tracks may be playable in some cases. */ - public boolean isTypeSupportedOrEmpty( - @C.TrackType int trackType, boolean allowExceedsCapabilities) { - boolean supported = true; + public boolean isTypeSupported(@C.TrackType int trackType, boolean allowExceedsCapabilities) { for (int i = 0; i < trackGroupInfos.size(); i++) { if (trackGroupInfos.get(i).getTrackType() == trackType) { if (trackGroupInfos.get(i).isSupported(allowExceedsCapabilities)) { return true; - } else { - supported = false; } } } - return supported; + return false; + } + + /** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int)}. */ + @Deprecated + @SuppressWarnings("deprecation") + public boolean isTypeSupportedOrEmpty(@C.TrackType int trackType) { + return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false); + } + + /** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int, boolean)}. */ + @Deprecated + public boolean isTypeSupportedOrEmpty( + @C.TrackType int trackType, boolean allowExceedsCapabilities) { + return !containsType(trackType) || isTypeSupported(trackType, allowExceedsCapabilities); } /** Returns true if at least one track of the type {@code trackType} is selected for playback. */ diff --git a/library/common/src/test/java/com/google/android/exoplayer2/TracksInfoTest.java b/library/common/src/test/java/com/google/android/exoplayer2/TracksInfoTest.java index 2a2b5afc7e..f1b0eb6159 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/TracksInfoTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/TracksInfoTest.java @@ -62,12 +62,10 @@ public class TracksInfoTest { public void tracksInfoGetters_withoutTrack_returnExpectedValues() { TracksInfo tracksInfo = new TracksInfo(ImmutableList.of()); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isFalse(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) - .isTrue(); + assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) + .isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse(); ImmutableList trackGroupInfos = tracksInfo.getTrackGroupInfos(); assertThat(trackGroupInfos).isEmpty(); @@ -99,24 +97,18 @@ public class TracksInfoTest { /* tracksSelected= */ new boolean[] {false, true}); TracksInfo tracksInfo = new TracksInfo(ImmutableList.of(trackGroupInfo0, trackGroupInfo1)); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isTrue(); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_VIDEO)).isTrue(); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_TEXT)).isFalse(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isFalse(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_VIDEO)).isTrue(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_TEXT)).isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) + assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isTrue(); + assertThat(tracksInfo.containsType(C.TRACK_TYPE_VIDEO)).isTrue(); + assertThat(tracksInfo.containsType(C.TRACK_TYPE_TEXT)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO)).isTrue(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) .isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) - .isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true)) + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) .isTrue(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true)) + .isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_VIDEO)).isTrue(); ImmutableList trackGroupInfos = tracksInfo.getTrackGroupInfos();