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. */ /** Properties for a text track. */
public static final class Subtitle { 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. */ /** The {@link Uri} to the subtitle file. */
public final Uri uri; public final Uri uri;
/** The optional MIME type of the subtitle file, or {@code null} if unspecified. */ /** 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. */ /** The label. */
@Nullable public final String label; @Nullable public final String label;
/** /** @deprecated Use {@link Builder} instead. */
* Creates an instance. @Deprecated
*
* @param uri The {@link Uri URI} to the subtitle file.
* @param mimeType The MIME type.
* @param language The optional language.
*/
public Subtitle(Uri uri, String mimeType, @Nullable String language) { public Subtitle(Uri uri, String mimeType, @Nullable String language) {
this(uri, mimeType, language, /* selectionFlags= */ 0); this(uri, mimeType, language, /* selectionFlags= */ 0);
} }
/** /** @deprecated Use {@link Builder} instead. */
* Creates an instance. @Deprecated
*
* @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.
*/
public Subtitle( public Subtitle(
Uri uri, String mimeType, @Nullable String language, @C.SelectionFlags int selectionFlags) { Uri uri, String mimeType, @Nullable String language, @C.SelectionFlags int selectionFlags) {
this(uri, mimeType, language, selectionFlags, /* roleFlags= */ 0, /* label= */ null); this(uri, mimeType, language, selectionFlags, /* roleFlags= */ 0, /* label= */ null);
} }
/** /** @deprecated Use {@link Builder} instead. */
* Creates an instance. @Deprecated
*
* @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.
*/
public Subtitle( public Subtitle(
Uri uri, Uri uri,
String mimeType, String mimeType,
@ -1249,6 +1299,20 @@ public final class MediaItem implements Bundleable {
this.label = label; 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 @Override
public boolean equals(@Nullable Object obj) { public boolean equals(@Nullable Object obj) {
if (this == obj) { if (this == obj) {

View File

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