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)
This commit is contained in:
bachinger 2022-11-16 15:52:36 +00:00 committed by christosts
parent bbf7324494
commit 73d40e1cfc
2 changed files with 125 additions and 1 deletions

View File

@ -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<BundlingExclusions> 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);
}
}

View File

@ -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);
}
}