Rename SubtitleTextView to CanvasSubtitleOutput

It displays images too, and in fact it's used exclusively to display
images in SubtitleWebView. It also doesn't use a TextView - so all round
a slightly confusing name.

Also rename SubtitleWebView to WebViewSubtitleOutput to match the same
pattern.

PiperOrigin-RevId: 311312758
This commit is contained in:
ibaker 2020-05-13 14:00:10 +01:00 committed by Oliver Woodman
parent 6e47819be4
commit 1c3c7c58ab
4 changed files with 63 additions and 56 deletions

View File

@ -30,10 +30,10 @@ import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* A {@link SubtitleView.Output} that uses Android's native text tooling via {@link * A {@link SubtitleView.Output} that uses Android's native layout framework via {@link
* SubtitlePainter}. * SubtitlePainter}.
*/ */
/* package */ final class SubtitleTextView extends View implements SubtitleView.Output { /* package */ final class CanvasSubtitleOutput extends View implements SubtitleView.Output {
private final List<SubtitlePainter> painters; private final List<SubtitlePainter> painters;
@ -43,11 +43,11 @@ import java.util.List;
private CaptionStyleCompat style; private CaptionStyleCompat style;
private float bottomPaddingFraction; private float bottomPaddingFraction;
public SubtitleTextView(Context context) { public CanvasSubtitleOutput(Context context) {
this(context, /* attrs= */ null); this(context, /* attrs= */ null);
} }
public SubtitleTextView(Context context, @Nullable AttributeSet attrs) { public CanvasSubtitleOutput(Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
painters = new ArrayList<>(); painters = new ArrayList<>();
cues = Collections.emptyList(); cues = Collections.emptyList();

View File

@ -20,7 +20,7 @@ import androidx.annotation.ColorInt;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
/** /**
* Utility methods for generating HTML and CSS for use with {@link SubtitleWebView} and {@link * Utility methods for generating HTML and CSS for use with {@link WebViewSubtitleOutput} and {@link
* SpannedToHtmlConverter}. * SpannedToHtmlConverter}.
*/ */
/* package */ final class HtmlUtils { /* package */ final class HtmlUtils {

View File

@ -20,6 +20,7 @@ import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Canvas;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan; import android.text.style.AbsoluteSizeSpan;
@ -28,6 +29,7 @@ import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.accessibility.CaptioningManager; import android.view.accessibility.CaptioningManager;
import android.webkit.WebView;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import androidx.annotation.Dimension; import androidx.annotation.Dimension;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
@ -46,6 +48,37 @@ import java.util.List;
/** A view for displaying subtitle {@link Cue}s. */ /** A view for displaying subtitle {@link Cue}s. */
public final class SubtitleView extends FrameLayout implements TextOutput { public final class SubtitleView extends FrameLayout implements TextOutput {
/**
* An output for displaying subtitles.
*
* <p>Implementations of this also need to extend {@link View} in order to be attached to the
* Android view hierarchy.
*/
/* package */ interface Output {
/**
* Updates the list of cues displayed.
*
* @param cues The cues to display.
* @param style A {@link CaptionStyleCompat} to use for styling unset properties of cues.
* @param defaultTextSize The default font size to apply when {@link Cue#textSize} is {@link
* Cue#DIMEN_UNSET}.
* @param defaultTextSizeType The type of {@code defaultTextSize}.
* @param bottomPaddingFraction The bottom padding to apply when {@link Cue#line} is {@link
* Cue#DIMEN_UNSET}, as a fraction of the view's remaining height after its top and bottom
* padding have been subtracted.
* @see #setStyle(CaptionStyleCompat)
* @see #setTextSize(int, float)
* @see #setBottomPaddingFraction(float)
*/
void update(
List<Cue> cues,
CaptionStyleCompat style,
float defaultTextSize,
@Cue.TextSizeType int defaultTextSizeType,
float bottomPaddingFraction);
}
/** /**
* The default fractional text size. * The default fractional text size.
* *
@ -61,17 +94,14 @@ public final class SubtitleView extends FrameLayout implements TextOutput {
*/ */
public static final float DEFAULT_BOTTOM_PADDING_FRACTION = 0.08f; public static final float DEFAULT_BOTTOM_PADDING_FRACTION = 0.08f;
/** /** Indicates subtitles should be displayed using a {@link Canvas}. This is the default. */
* Indicates a {@link SubtitleTextView} should be used to display subtitles. This is the default. public static final int VIEW_TYPE_CANVAS = 1;
*/
public static final int VIEW_TYPE_TEXT = 1;
/** /**
* Indicates a {@link SubtitleWebView} should be used to display subtitles. * Indicates subtitles should be displayed using a {@link WebView}.
* *
* <p>This will instantiate a {@link android.webkit.WebView} and use CSS and HTML styling to * <p>This will use CSS and HTML styling to render the subtitles. This supports some additional
* render the subtitles. This supports some additional styling features beyond those supported by * styling features beyond those supported by {@link #VIEW_TYPE_CANVAS} such as vertical text.
* {@link SubtitleTextView} such as vertical text.
*/ */
public static final int VIEW_TYPE_WEB = 2; public static final int VIEW_TYPE_WEB = 2;
@ -81,13 +111,13 @@ public final class SubtitleView extends FrameLayout implements TextOutput {
* <p>One of: * <p>One of:
* *
* <ul> * <ul>
* <li>{@link #VIEW_TYPE_TEXT} * <li>{@link #VIEW_TYPE_CANVAS}
* <li>{@link #VIEW_TYPE_WEB} * <li>{@link #VIEW_TYPE_WEB}
* </ul> * </ul>
*/ */
@Documented @Documented
@Retention(SOURCE) @Retention(SOURCE)
@IntDef({VIEW_TYPE_TEXT, VIEW_TYPE_WEB}) @IntDef({VIEW_TYPE_CANVAS, VIEW_TYPE_WEB})
public @interface ViewType {} public @interface ViewType {}
private List<Cue> cues; private List<Cue> cues;
@ -116,11 +146,11 @@ public final class SubtitleView extends FrameLayout implements TextOutput {
applyEmbeddedStyles = true; applyEmbeddedStyles = true;
applyEmbeddedFontSizes = true; applyEmbeddedFontSizes = true;
SubtitleTextView subtitleTextView = new SubtitleTextView(context, attrs); CanvasSubtitleOutput canvasSubtitleOutput = new CanvasSubtitleOutput(context, attrs);
output = subtitleTextView; output = canvasSubtitleOutput;
innerSubtitleView = subtitleTextView; innerSubtitleView = canvasSubtitleOutput;
addView(innerSubtitleView); addView(innerSubtitleView);
viewType = VIEW_TYPE_TEXT; viewType = VIEW_TYPE_CANVAS;
} }
@Override @Override
@ -151,11 +181,11 @@ public final class SubtitleView extends FrameLayout implements TextOutput {
return; return;
} }
switch (viewType) { switch (viewType) {
case VIEW_TYPE_TEXT: case VIEW_TYPE_CANVAS:
setView(new SubtitleTextView(getContext())); setView(new CanvasSubtitleOutput(getContext()));
break; break;
case VIEW_TYPE_WEB: case VIEW_TYPE_WEB:
setView(new SubtitleWebView(getContext())); setView(new WebViewSubtitleOutput(getContext()));
break; break;
default: default:
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -165,8 +195,8 @@ public final class SubtitleView extends FrameLayout implements TextOutput {
private <T extends View & Output> void setView(T view) { private <T extends View & Output> void setView(T view) {
removeView(innerSubtitleView); removeView(innerSubtitleView);
if (innerSubtitleView instanceof SubtitleWebView) { if (innerSubtitleView instanceof WebViewSubtitleOutput) {
((SubtitleWebView) innerSubtitleView).destroy(); ((WebViewSubtitleOutput) innerSubtitleView).destroy();
} }
innerSubtitleView = view; innerSubtitleView = view;
output = view; output = view;
@ -383,28 +413,5 @@ public final class SubtitleView extends FrameLayout implements TextOutput {
return cue; return cue;
} }
/* package */ interface Output {
/**
* Updates the list of cues displayed.
*
* @param cues The cues to display.
* @param style A {@link CaptionStyleCompat} to use for styling unset properties of cues.
* @param defaultTextSize The default font size to apply when {@link Cue#textSize} is {@link
* Cue#DIMEN_UNSET}.
* @param defaultTextSizeType The type of {@code defaultTextSize}.
* @param bottomPaddingFraction The bottom padding to apply when {@link Cue#line} is {@link
* Cue#DIMEN_UNSET}, as a fraction of the view's remaining height after its top and bottom
* padding have been subtracted.
* @see #setStyle(CaptionStyleCompat)
* @see #setTextSize(int, float)
* @see #setBottomPaddingFraction(float)
*/
void update(
List<Cue> cues,
CaptionStyleCompat style,
float defaultTextSize,
@Cue.TextSizeType int defaultTextSizeType,
float bottomPaddingFraction);
}
} }

View File

@ -43,26 +43,26 @@ import java.util.List;
* <p>NOTE: This is currently extremely experimental and doesn't support most {@link Cue} styling * <p>NOTE: This is currently extremely experimental and doesn't support most {@link Cue} styling
* properties. * properties.
*/ */
/* package */ final class SubtitleWebView extends FrameLayout implements SubtitleView.Output { /* package */ final class WebViewSubtitleOutput extends FrameLayout implements SubtitleView.Output {
/** /**
* A {@link SubtitleTextView} used for displaying bitmap cues. * A {@link CanvasSubtitleOutput} used for displaying bitmap cues.
* *
* <p>There's no advantage to displaying bitmap cues in a {@link WebView}, so we re-use the * <p>There's no advantage to displaying bitmap cues in a {@link WebView}, so we re-use the
* existing logic. * existing logic.
*/ */
private final SubtitleTextView subtitleTextView; private final CanvasSubtitleOutput canvasSubtitleOutput;
private final WebView webView; private final WebView webView;
public SubtitleWebView(Context context) { public WebViewSubtitleOutput(Context context) {
this(context, null); this(context, null);
} }
public SubtitleWebView(Context context, @Nullable AttributeSet attrs) { public WebViewSubtitleOutput(Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
subtitleTextView = new SubtitleTextView(context, attrs); canvasSubtitleOutput = new CanvasSubtitleOutput(context, attrs);
webView = webView =
new WebView(context, attrs) { new WebView(context, attrs) {
@Override @Override
@ -81,7 +81,7 @@ import java.util.List;
}; };
webView.setBackgroundColor(Color.TRANSPARENT); webView.setBackgroundColor(Color.TRANSPARENT);
addView(subtitleTextView); addView(canvasSubtitleOutput);
addView(webView); addView(webView);
} }
@ -102,8 +102,8 @@ import java.util.List;
textCues.add(cue); textCues.add(cue);
} }
} }
subtitleTextView.update(bitmapCues, style, textSize, textSizeType, bottomPaddingFraction); canvasSubtitleOutput.update(bitmapCues, style, textSize, textSizeType, bottomPaddingFraction);
// Invalidate to trigger subtitleTextView to draw. // Invalidate to trigger canvasSubtitleOutput to draw.
invalidate(); invalidate();
updateWebView(textCues, style, textSize, textSizeType, bottomPaddingFraction); updateWebView(textCues, style, textSize, textSizeType, bottomPaddingFraction);
} }