Skip to content

Commit bae5090

Browse files
marcbaechingermicrokatz
authored andcommitted
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
1 parent 1ee185c commit bae5090

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import androidx.media3.common.VideoSize;
4747
import androidx.media3.common.text.CueGroup;
4848
import androidx.media3.common.util.Assertions;
49+
import androidx.media3.common.util.UnstableApi;
50+
import com.google.common.base.Objects;
4951
import com.google.errorprone.annotations.CanIgnoreReturnValue;
5052
import java.lang.annotation.Documented;
5153
import java.lang.annotation.Retention;
@@ -58,6 +60,75 @@
5860
*/
5961
/* package */ class PlayerInfo implements Bundleable {
6062

63+
/**
64+
* Holds information about what properties of the {@link PlayerInfo} have been excluded when sent
65+
* to the controller.
66+
*/
67+
public static class BundlingExclusions implements Bundleable {
68+
69+
/** Whether the {@linkplain PlayerInfo#timeline timeline} is excluded. */
70+
public final boolean isTimelineExcluded;
71+
/** Whether the {@linkplain PlayerInfo#currentTracks current tracks} are excluded. */
72+
public final boolean areCurrentTracksExcluded;
73+
74+
/** Creates a new instance. */
75+
public BundlingExclusions(boolean isTimelineExcluded, boolean areCurrentTracksExcluded) {
76+
this.isTimelineExcluded = isTimelineExcluded;
77+
this.areCurrentTracksExcluded = areCurrentTracksExcluded;
78+
}
79+
80+
// Bundleable implementation.
81+
82+
@Documented
83+
@Retention(RetentionPolicy.SOURCE)
84+
@Target(TYPE_USE)
85+
@IntDef({FIELD_IS_TIMELINE_EXCLUDED, FIELD_ARE_CURRENT_TRACKS_EXCLUDED})
86+
private @interface FieldNumber {}
87+
88+
private static final int FIELD_IS_TIMELINE_EXCLUDED = 0;
89+
private static final int FIELD_ARE_CURRENT_TRACKS_EXCLUDED = 1;
90+
// Next field key = 2
91+
92+
@UnstableApi
93+
@Override
94+
public Bundle toBundle() {
95+
Bundle bundle = new Bundle();
96+
bundle.putBoolean(keyForField(FIELD_IS_TIMELINE_EXCLUDED), isTimelineExcluded);
97+
bundle.putBoolean(keyForField(FIELD_ARE_CURRENT_TRACKS_EXCLUDED), areCurrentTracksExcluded);
98+
return bundle;
99+
}
100+
101+
public static final Creator<BundlingExclusions> CREATOR =
102+
bundle ->
103+
new BundlingExclusions(
104+
bundle.getBoolean(
105+
keyForField(FIELD_IS_TIMELINE_EXCLUDED), /* defaultValue= */ false),
106+
bundle.getBoolean(
107+
keyForField(FIELD_ARE_CURRENT_TRACKS_EXCLUDED), /* defaultValue= */ false));
108+
109+
private static String keyForField(@BundlingExclusions.FieldNumber int field) {
110+
return Integer.toString(field, Character.MAX_RADIX);
111+
}
112+
113+
@Override
114+
public boolean equals(@Nullable Object o) {
115+
if (this == o) {
116+
return true;
117+
}
118+
if (!(o instanceof BundlingExclusions)) {
119+
return false;
120+
}
121+
BundlingExclusions that = (BundlingExclusions) o;
122+
return isTimelineExcluded == that.isTimelineExcluded
123+
&& areCurrentTracksExcluded == that.areCurrentTracksExcluded;
124+
}
125+
126+
@Override
127+
public int hashCode() {
128+
return Objects.hashCode(isTimelineExcluded, areCurrentTracksExcluded);
129+
}
130+
}
131+
61132
public static class Builder {
62133

63134
@Nullable private PlaybackException playerError;
@@ -983,7 +1054,7 @@ private static PlayerInfo fromBundle(Bundle bundle) {
9831054
trackSelectionParameters);
9841055
}
9851056

986-
private static String keyForField(@FieldNumber int field) {
1057+
private static String keyForField(@PlayerInfo.FieldNumber int field) {
9871058
return Integer.toString(field, Character.MAX_RADIX);
9881059
}
9891060
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.media3.session;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import android.os.Bundle;
21+
import androidx.test.ext.junit.runners.AndroidJUnit4;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
/** Tests for {@link PlayerInfo}. */
26+
@RunWith(AndroidJUnit4.class)
27+
public class PlayerInfoTest {
28+
29+
@Test
30+
public void bundlingExclusionEquals_equalInstances() {
31+
PlayerInfo.BundlingExclusions bundlingExclusions1 =
32+
new PlayerInfo.BundlingExclusions(
33+
/* isTimelineExcluded= */ true, /* areCurrentTracksExcluded= */ false);
34+
PlayerInfo.BundlingExclusions bundlingExclusions2 =
35+
new PlayerInfo.BundlingExclusions(
36+
/* isTimelineExcluded= */ true, /* areCurrentTracksExcluded= */ false);
37+
38+
assertThat(bundlingExclusions1).isEqualTo(bundlingExclusions2);
39+
}
40+
41+
@Test
42+
public void bundlingExclusionFromBundle_toBundleRoundTrip_equalInstances() {
43+
PlayerInfo.BundlingExclusions bundlingExclusions =
44+
new PlayerInfo.BundlingExclusions(
45+
/* isTimelineExcluded= */ true, /* areCurrentTracksExcluded= */ true);
46+
Bundle bundle = bundlingExclusions.toBundle();
47+
48+
PlayerInfo.BundlingExclusions resultingBundlingExclusions =
49+
PlayerInfo.BundlingExclusions.CREATOR.fromBundle(bundle);
50+
51+
assertThat(resultingBundlingExclusions).isEqualTo(bundlingExclusions);
52+
}
53+
}

0 commit comments

Comments
 (0)