15
15
*/
16
16
package androidx .media3 .common ;
17
17
18
+ import static androidx .media3 .common .util .Assertions .checkState ;
18
19
import static java .lang .annotation .ElementType .TYPE_USE ;
19
20
20
21
import android .os .Bundle ;
@@ -140,7 +141,7 @@ public static final class Builder {
140
141
141
142
@ Nullable private String id ;
142
143
@ Nullable private String label ;
143
- @ Nullable private List <Label > labels ;
144
+ private List <Label > labels ;
144
145
@ Nullable private String language ;
145
146
private @ C .SelectionFlags int selectionFlags ;
146
147
private @ C .RoleFlags int roleFlags ;
@@ -196,6 +197,7 @@ public static final class Builder {
196
197
197
198
/** Creates a new instance with default values. */
198
199
public Builder () {
200
+ labels = ImmutableList .of ();
199
201
averageBitrate = NO_VALUE ;
200
202
peakBitrate = NO_VALUE ;
201
203
// Sample specific.
@@ -298,6 +300,9 @@ public Builder setId(int id) {
298
300
/**
299
301
* Sets {@link Format#label}. The default value is {@code null}.
300
302
*
303
+ * If both this default label and a list of {@link #setLabels labels} are set, this default
304
+ * label must be part of label list.
305
+ *
301
306
* @param label The {@link Format#label}.
302
307
* @return The builder.
303
308
*/
@@ -308,14 +313,17 @@ public Builder setLabel(@Nullable String label) {
308
313
}
309
314
310
315
/**
311
- * Sets {@link Format#labels}. The default value is {@code null}.
316
+ * Sets {@link Format#labels}. The default value is an empty list.
317
+ *
318
+ * If both the default {@linkplain #setLabel label} and this list are set, the default label
319
+ * must be part of this list of labels.
312
320
*
313
321
* @param labels The {@link Format#labels}.
314
322
* @return The builder.
315
323
*/
316
324
@ CanIgnoreReturnValue
317
- public Builder setLabels (@ Nullable List <Label > labels ) {
318
- this .labels = labels ;
325
+ public Builder setLabels (List <Label > labels ) {
326
+ this .labels = ImmutableList . copyOf ( labels ) ;
319
327
return this ;
320
328
}
321
329
@@ -757,10 +765,20 @@ public Format build() {
757
765
/** An identifier for the format, or null if unknown or not applicable. */
758
766
@ Nullable public final String id ;
759
767
760
- /** The human readable label, or null if unknown or not applicable. */
768
+ /**
769
+ * The default human readable label, or null if unknown or not applicable.
770
+ *
771
+ * If non-null, the same label will be part of {@link #labels} too. If null, {@link #labels}
772
+ * will be empty.
773
+ */
761
774
@ Nullable public final String label ;
762
775
763
- /** The human readable list of labels, or null if unknown or not applicable. */
776
+ /**
777
+ * The human readable list of labels, or an empty list if unknown or not applicable.
778
+ *
779
+ * If non-empty, the default {@link #label} will be part of this list. If empty, the default
780
+ * {@link #label} will be null.
781
+ */
764
782
@ UnstableApi public final List <Label > labels ;
765
783
766
784
/** The language as an IETF BCP 47 conformant tag, or null if unknown or not applicable. */
@@ -952,17 +970,19 @@ public Format build() {
952
970
private Format (Builder builder ) {
953
971
id = builder .id ;
954
972
language = Util .normalizeLanguageCode (builder .language );
955
- @ Nullable String tmpLabel = builder .label ;
956
- labels = builder .labels == null ? new ArrayList <>() : builder .labels ;
957
- if (labels .isEmpty () && !TextUtils .isEmpty (tmpLabel )) {
958
- labels .add (new Label (language , tmpLabel ));
959
- label = tmpLabel ;
960
- } else if (!labels .isEmpty () && TextUtils .isEmpty (tmpLabel )) {
961
- label = getDefaultLabel (tmpLabel , labels );
973
+ if (builder .labels .isEmpty () && builder .label != null ) {
974
+ labels = ImmutableList .of (new Label (language , builder .label ));
975
+ label = builder .label ;
976
+ } else if (!builder .labels .isEmpty () && builder .label == null ) {
977
+ labels = builder .labels ;
978
+ label = getDefaultLabel (builder .labels , language );
962
979
} else {
963
- label = tmpLabel ;
980
+ checkState (
981
+ (builder .labels .isEmpty () && builder .label == null )
982
+ || (builder .labels .stream ().anyMatch (l -> l .value .equals (builder .label ))));
983
+ labels = builder .labels ;
984
+ label = builder .label ;
964
985
}
965
- checkLabels (label , labels );
966
986
selectionFlags = builder .selectionFlags ;
967
987
roleFlags = builder .roleFlags ;
968
988
averageBitrate = builder .averageBitrate ;
@@ -1010,29 +1030,6 @@ private Format(Builder builder) {
1010
1030
}
1011
1031
}
1012
1032
1013
- private @ Nullable String getDefaultLabel (@ Nullable String label , List <Label > labels ) {
1014
- if (TextUtils .isEmpty (label )) {
1015
- for (Label l : labels ) {
1016
- if (TextUtils .equals (l .lang , language )) {
1017
- return l .value ;
1018
- }
1019
- }
1020
- if (!labels .isEmpty ()) {
1021
- return labels .get (0 ).value ;
1022
- }
1023
- }
1024
- return label ;
1025
- }
1026
-
1027
- private void checkLabels (@ Nullable String label , List <Label > labels )
1028
- throws IllegalStateException {
1029
- if (!TextUtils .isEmpty (label )
1030
- && !labels .isEmpty ()
1031
- && labels .stream ().noneMatch (l -> TextUtils .equals (l .value , label ))) {
1032
- throw new IllegalStateException ();
1033
- }
1034
- }
1035
-
1036
1033
/** Returns a {@link Format.Builder} initialized with the values of this instance. */
1037
1034
@ UnstableApi
1038
1035
public Builder buildUpon () {
@@ -1056,7 +1053,7 @@ public Format withManifestFormatInfo(Format manifestFormat) {
1056
1053
1057
1054
// Prefer manifest values, but fill in from sample format if missing.
1058
1055
@ Nullable String label = manifestFormat .label != null ? manifestFormat .label : this .label ;
1059
- List <Label > labels = manifestFormat .labels ;
1056
+ List <Label > labels = ! manifestFormat . labels . isEmpty () ? manifestFormat . labels : this .labels ;
1060
1057
@ Nullable String language = this .language ;
1061
1058
if ((trackType == C .TRACK_TYPE_TEXT || trackType == C .TRACK_TYPE_AUDIO )
1062
1059
&& manifestFormat .language != null ) {
@@ -1246,6 +1243,7 @@ public boolean equals(@Nullable Object obj) {
1246
1243
&& Float .compare (pixelWidthHeightRatio , other .pixelWidthHeightRatio ) == 0
1247
1244
&& Util .areEqual (id , other .id )
1248
1245
&& Util .areEqual (label , other .label )
1246
+ && labels .equals (other .labels )
1249
1247
&& Util .areEqual (codecs , other .codecs )
1250
1248
&& Util .areEqual (containerMimeType , other .containerMimeType )
1251
1249
&& Util .areEqual (sampleMimeType , other .sampleMimeType )
@@ -1254,8 +1252,7 @@ public boolean equals(@Nullable Object obj) {
1254
1252
&& Util .areEqual (metadata , other .metadata )
1255
1253
&& Util .areEqual (colorInfo , other .colorInfo )
1256
1254
&& Util .areEqual (drmInitData , other .drmInitData )
1257
- && initializationDataEquals (other )
1258
- && labels .equals (other .labels );
1255
+ && initializationDataEquals (other );
1259
1256
}
1260
1257
1261
1258
/**
@@ -1338,7 +1335,7 @@ public static String toLogString(@Nullable Format format) {
1338
1335
if (format .language != null ) {
1339
1336
builder .append (", language=" ).append (format .language );
1340
1337
}
1341
- if (format .labels .size () > 0 ) {
1338
+ if (! format .labels .isEmpty () ) {
1342
1339
builder .append (", labels=[" );
1343
1340
Joiner .on (',' ).appendTo (builder , format .labels );
1344
1341
builder .append ("]" );
@@ -1561,4 +1558,13 @@ private static String keyForInitializationData(int initialisationDataIndex) {
1561
1558
private static <T > T defaultIfNull (@ Nullable T value , @ Nullable T defaultValue ) {
1562
1559
return value != null ? value : defaultValue ;
1563
1560
}
1561
+
1562
+ private static String getDefaultLabel (List <Label > labels , @ Nullable String language ) {
1563
+ for (Label l : labels ) {
1564
+ if (TextUtils .equals (l .language , language )) {
1565
+ return l .value ;
1566
+ }
1567
+ }
1568
+ return labels .get (0 ).value ;
1569
+ }
1564
1570
}
0 commit comments