From 73d40e1cfc339ef3d8a7466d34c762dcc65627af Mon Sep 17 00:00:00 2001 From: bachinger Date: Wed, 16 Nov 2022 15:52:36 +0000 Subject: [PATCH] Add bundling exclusions with unit tests The exclusion will be used in a follow-up CL when sending PlayerInfo updates. #minor-release PiperOrigin-RevId: 488939258 (cherry picked from commit bae509009bd62554876ecb7485708e50af4eaa2a) --- .../androidx/media3/session/PlayerInfo.java | 73 ++++++++++++++++++- .../media3/session/PlayerInfoTest.java | 53 ++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 libraries/session/src/test/java/androidx/media3/session/PlayerInfoTest.java diff --git a/libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java b/libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java index c1b67c6207..8fe6eece28 100644 --- a/libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java +++ b/libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java @@ -46,6 +46,8 @@ import androidx.media3.common.Tracks; import androidx.media3.common.VideoSize; import androidx.media3.common.text.CueGroup; import androidx.media3.common.util.Assertions; +import androidx.media3.common.util.UnstableApi; +import com.google.common.base.Objects; import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -58,6 +60,75 @@ import java.lang.annotation.Target; */ /* package */ class PlayerInfo implements Bundleable { + /** + * Holds information about what properties of the {@link PlayerInfo} have been excluded when sent + * to the controller. + */ + public static class BundlingExclusions implements Bundleable { + + /** Whether the {@linkplain PlayerInfo#timeline timeline} is excluded. */ + public final boolean isTimelineExcluded; + /** Whether the {@linkplain PlayerInfo#currentTracks current tracks} are excluded. */ + public final boolean areCurrentTracksExcluded; + + /** Creates a new instance. */ + public BundlingExclusions(boolean isTimelineExcluded, boolean areCurrentTracksExcluded) { + this.isTimelineExcluded = isTimelineExcluded; + this.areCurrentTracksExcluded = areCurrentTracksExcluded; + } + + // Bundleable implementation. + + @Documented + @Retention(RetentionPolicy.SOURCE) + @Target(TYPE_USE) + @IntDef({FIELD_IS_TIMELINE_EXCLUDED, FIELD_ARE_CURRENT_TRACKS_EXCLUDED}) + private @interface FieldNumber {} + + private static final int FIELD_IS_TIMELINE_EXCLUDED = 0; + private static final int FIELD_ARE_CURRENT_TRACKS_EXCLUDED = 1; + // Next field key = 2 + + @UnstableApi + @Override + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putBoolean(keyForField(FIELD_IS_TIMELINE_EXCLUDED), isTimelineExcluded); + bundle.putBoolean(keyForField(FIELD_ARE_CURRENT_TRACKS_EXCLUDED), areCurrentTracksExcluded); + return bundle; + } + + public static final Creator CREATOR = + bundle -> + new BundlingExclusions( + bundle.getBoolean( + keyForField(FIELD_IS_TIMELINE_EXCLUDED), /* defaultValue= */ false), + bundle.getBoolean( + keyForField(FIELD_ARE_CURRENT_TRACKS_EXCLUDED), /* defaultValue= */ false)); + + private static String keyForField(@BundlingExclusions.FieldNumber int field) { + return Integer.toString(field, Character.MAX_RADIX); + } + + @Override + public boolean equals(@Nullable Object o) { + if (this == o) { + return true; + } + if (!(o instanceof BundlingExclusions)) { + return false; + } + BundlingExclusions that = (BundlingExclusions) o; + return isTimelineExcluded == that.isTimelineExcluded + && areCurrentTracksExcluded == that.areCurrentTracksExcluded; + } + + @Override + public int hashCode() { + return Objects.hashCode(isTimelineExcluded, areCurrentTracksExcluded); + } + } + public static class Builder { @Nullable private PlaybackException playerError; @@ -983,7 +1054,7 @@ import java.lang.annotation.Target; trackSelectionParameters); } - private static String keyForField(@FieldNumber int field) { + private static String keyForField(@PlayerInfo.FieldNumber int field) { return Integer.toString(field, Character.MAX_RADIX); } } diff --git a/libraries/session/src/test/java/androidx/media3/session/PlayerInfoTest.java b/libraries/session/src/test/java/androidx/media3/session/PlayerInfoTest.java new file mode 100644 index 0000000000..7e8738f9d9 --- /dev/null +++ b/libraries/session/src/test/java/androidx/media3/session/PlayerInfoTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022 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 androidx.media3.session; + +import static com.google.common.truth.Truth.assertThat; + +import android.os.Bundle; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Tests for {@link PlayerInfo}. */ +@RunWith(AndroidJUnit4.class) +public class PlayerInfoTest { + + @Test + public void bundlingExclusionEquals_equalInstances() { + PlayerInfo.BundlingExclusions bundlingExclusions1 = + new PlayerInfo.BundlingExclusions( + /* isTimelineExcluded= */ true, /* areCurrentTracksExcluded= */ false); + PlayerInfo.BundlingExclusions bundlingExclusions2 = + new PlayerInfo.BundlingExclusions( + /* isTimelineExcluded= */ true, /* areCurrentTracksExcluded= */ false); + + assertThat(bundlingExclusions1).isEqualTo(bundlingExclusions2); + } + + @Test + public void bundlingExclusionFromBundle_toBundleRoundTrip_equalInstances() { + PlayerInfo.BundlingExclusions bundlingExclusions = + new PlayerInfo.BundlingExclusions( + /* isTimelineExcluded= */ true, /* areCurrentTracksExcluded= */ true); + Bundle bundle = bundlingExclusions.toBundle(); + + PlayerInfo.BundlingExclusions resultingBundlingExclusions = + PlayerInfo.BundlingExclusions.CREATOR.fromBundle(bundle); + + assertThat(resultingBundlingExclusions).isEqualTo(bundlingExclusions); + } +}