Add MediaItem.Subtitle.Builder

PiperOrigin-RevId: 398200055
This commit is contained in:
ibaker 2021-09-22 11:33:27 +01:00 committed by bachinger
parent a194d73c75
commit fefa6cb817
2 changed files with 113 additions and 41 deletions

View File

@ -1187,6 +1187,75 @@ public final class MediaItem implements Bundleable {
/** Properties for a text track. */
public static final class Subtitle {
/** Builder for {@link Subtitle} instances. */
public static final class Builder {
private Uri uri;
@Nullable private String mimeType;
@Nullable private String language;
@C.SelectionFlags private int selectionFlags;
@C.RoleFlags private int roleFlags;
@Nullable private String label;
/**
* Constructs an instance.
*
* @param uri The {@link Uri} to the subtitle file.
*/
public Builder(Uri uri) {
this.uri = uri;
}
private Builder(Subtitle subtitle) {
this.uri = subtitle.uri;
this.mimeType = subtitle.mimeType;
this.language = subtitle.language;
this.selectionFlags = subtitle.selectionFlags;
this.roleFlags = subtitle.roleFlags;
this.label = subtitle.label;
}
/** Sets the {@link Uri} to the subtitle file. */
public Builder setUri(Uri uri) {
this.uri = uri;
return this;
}
/** Sets the MIME type. */
public Builder setMimeType(String mimeType) {
this.mimeType = mimeType;
return this;
}
/** Sets the optional language of the subtitle file. */
public Builder setLanguage(@Nullable String language) {
this.language = language;
return this;
}
/** Sets the flags used for track selection. */
public Builder setSelectionFlags(@C.SelectionFlags int selectionFlags) {
this.selectionFlags = selectionFlags;
return this;
}
/** Sets the role flags. These are used for track selection. */
public Builder setRoleFlags(@C.RoleFlags int roleFlags) {
this.roleFlags = roleFlags;
return this;
}
/** Sets the optional label for this subtitle track. */
public Builder setLabel(@Nullable String label) {
this.label = label;
return this;
}
/** Creates a {@link Subtitle} from the values of this builder. */
public Subtitle build() {
return new Subtitle(this);
}
}
/** The {@link Uri} to the subtitle file. */
public final Uri uri;
/** The optional MIME type of the subtitle file, or {@code null} if unspecified. */
@ -1200,40 +1269,21 @@ public final class MediaItem implements Bundleable {
/** The label. */
@Nullable public final String label;
/**
* Creates an instance.
*
* @param uri The {@link Uri URI} to the subtitle file.
* @param mimeType The MIME type.
* @param language The optional language.
*/
/** @deprecated Use {@link Builder} instead. */
@Deprecated
public Subtitle(Uri uri, String mimeType, @Nullable String language) {
this(uri, mimeType, language, /* selectionFlags= */ 0);
}
/**
* Creates an instance.
*
* @param uri The {@link Uri URI} to the subtitle file.
* @param mimeType The MIME type.
* @param language The optional language.
* @param selectionFlags The selection flags.
*/
/** @deprecated Use {@link Builder} instead. */
@Deprecated
public Subtitle(
Uri uri, String mimeType, @Nullable String language, @C.SelectionFlags int selectionFlags) {
this(uri, mimeType, language, selectionFlags, /* roleFlags= */ 0, /* label= */ null);
}
/**
* Creates an instance.
*
* @param uri The {@link Uri URI} to the subtitle file.
* @param mimeType The MIME type.
* @param language The optional language.
* @param selectionFlags The selection flags.
* @param roleFlags The role flags.
* @param label The optional label.
*/
/** @deprecated Use {@link Builder} instead. */
@Deprecated
public Subtitle(
Uri uri,
String mimeType,
@ -1249,6 +1299,20 @@ public final class MediaItem implements Bundleable {
this.label = label;
}
private Subtitle(Builder builder) {
this.uri = builder.uri;
this.mimeType = builder.mimeType;
this.language = builder.language;
this.selectionFlags = builder.selectionFlags;
this.roleFlags = builder.roleFlags;
this.label = builder.label;
}
/** Returns a {@link Builder} initialized with the values of this instance. */
public Builder buildUpon() {
return new Builder(this);
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {

View File

@ -258,9 +258,17 @@ public class MediaItemTest {
}
@Test
@SuppressWarnings("deprecation") // Using deprecated constructors
public void builderSetSubtitles_setsSubtitles() {
List<MediaItem.Subtitle> subtitles =
Arrays.asList(
ImmutableList.of(
new MediaItem.Subtitle.Builder(Uri.parse(URI_STRING + "/es"))
.setMimeType(MimeTypes.TEXT_SSA)
.setLanguage(/* language= */ "es")
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
.setRoleFlags(C.ROLE_FLAG_ALTERNATE)
.setLabel("label")
.build(),
new MediaItem.Subtitle(
Uri.parse(URI_STRING + "/en"), MimeTypes.APPLICATION_TTML, /* language= */ "en"),
new MediaItem.Subtitle(
@ -531,14 +539,14 @@ public class MediaItemTest {
.setLiveMinOffsetMs(2222)
.setLiveMaxOffsetMs(4444)
.setSubtitles(
Collections.singletonList(
new MediaItem.Subtitle(
Uri.parse(URI_STRING + "/en"),
MimeTypes.APPLICATION_TTML,
/* language= */ "en",
C.SELECTION_FLAG_FORCED,
C.ROLE_FLAG_ALTERNATE,
"label")))
ImmutableList.of(
new MediaItem.Subtitle.Builder(Uri.parse(URI_STRING + "/en"))
.setMimeType(MimeTypes.APPLICATION_TTML)
.setLanguage("en")
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
.setRoleFlags(C.ROLE_FLAG_ALTERNATE)
.setLabel("label")
.build()))
.setTag(new Object())
.build();
@ -584,13 +592,13 @@ public class MediaItemTest {
.build())
.setSubtitles(
ImmutableList.of(
new MediaItem.Subtitle(
Uri.parse(URI_STRING + "/en"),
MimeTypes.APPLICATION_TTML,
/* language= */ "en",
C.SELECTION_FLAG_FORCED,
C.ROLE_FLAG_ALTERNATE,
"label")))
new MediaItem.Subtitle.Builder(Uri.parse(URI_STRING + "/en"))
.setMimeType(MimeTypes.APPLICATION_TTML)
.setLanguage("en")
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
.setRoleFlags(C.ROLE_FLAG_ALTERNATE)
.setLabel("label")
.build()))
.setTag(new Object())
.build();