Commit 37908dd4 by ibaker Committed by Ian Baker

Remove TTML package from null-checking blacklist

PiperOrigin-RevId: 290629644
parent 8a2a5271
...@@ -31,6 +31,7 @@ import java.util.Map; ...@@ -31,6 +31,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** /**
* A package internal representation of TTML node. * A package internal representation of TTML node.
...@@ -93,7 +94,7 @@ import java.util.TreeSet; ...@@ -93,7 +94,7 @@ import java.util.TreeSet;
private final HashMap<String, Integer> nodeStartsByRegion; private final HashMap<String, Integer> nodeStartsByRegion;
private final HashMap<String, Integer> nodeEndsByRegion; private final HashMap<String, Integer> nodeEndsByRegion;
private List<TtmlNode> children; @MonotonicNonNull private List<TtmlNode> children;
public static TtmlNode buildTextNode(String text) { public static TtmlNode buildTextNode(String text) {
return new TtmlNode( return new TtmlNode(
...@@ -196,6 +197,7 @@ import java.util.TreeSet; ...@@ -196,6 +197,7 @@ import java.util.TreeSet;
} }
} }
@Nullable
public String[] getStyleIds() { public String[] getStyleIds() {
return styleIds; return styleIds;
} }
...@@ -217,7 +219,7 @@ import java.util.TreeSet; ...@@ -217,7 +219,7 @@ import java.util.TreeSet;
// Create image based cues. // Create image based cues.
for (Pair<String, String> regionImagePair : regionImageOutputs) { for (Pair<String, String> regionImagePair : regionImageOutputs) {
String encodedBitmapData = imageMap.get(regionImagePair.second); @Nullable String encodedBitmapData = imageMap.get(regionImagePair.second);
if (encodedBitmapData == null) { if (encodedBitmapData == null) {
// Image reference points to an invalid image. Do nothing. // Image reference points to an invalid image. Do nothing.
continue; continue;
...@@ -225,7 +227,7 @@ import java.util.TreeSet; ...@@ -225,7 +227,7 @@ import java.util.TreeSet;
byte[] bitmapData = Base64.decode(encodedBitmapData, Base64.DEFAULT); byte[] bitmapData = Base64.decode(encodedBitmapData, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, /* offset= */ 0, bitmapData.length); Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, /* offset= */ 0, bitmapData.length);
TtmlRegion region = regionMap.get(regionImagePair.first); TtmlRegion region = Assertions.checkNotNull(regionMap.get(regionImagePair.first));
cues.add( cues.add(
new Cue.Builder() new Cue.Builder()
...@@ -241,7 +243,7 @@ import java.util.TreeSet; ...@@ -241,7 +243,7 @@ import java.util.TreeSet;
// Create text based cues. // Create text based cues.
for (Entry<String, SpannableStringBuilder> entry : regionTextOutputs.entrySet()) { for (Entry<String, SpannableStringBuilder> entry : regionTextOutputs.entrySet()) {
TtmlRegion region = regionMap.get(entry.getKey()); TtmlRegion region = Assertions.checkNotNull(regionMap.get(entry.getKey()));
cues.add( cues.add(
new Cue( new Cue(
cleanUpText(entry.getValue()), cleanUpText(entry.getValue()),
...@@ -286,7 +288,7 @@ import java.util.TreeSet; ...@@ -286,7 +288,7 @@ import java.util.TreeSet;
String resolvedRegionId = ANONYMOUS_REGION_ID.equals(regionId) ? inheritedRegion : regionId; String resolvedRegionId = ANONYMOUS_REGION_ID.equals(regionId) ? inheritedRegion : regionId;
if (isTextNode && descendsPNode) { if (isTextNode && descendsPNode) {
getRegionOutput(resolvedRegionId, regionOutputs).append(text); getRegionOutput(resolvedRegionId, regionOutputs).append(Assertions.checkNotNull(text));
} else if (TAG_BR.equals(tag) && descendsPNode) { } else if (TAG_BR.equals(tag) && descendsPNode) {
getRegionOutput(resolvedRegionId, regionOutputs).append('\n'); getRegionOutput(resolvedRegionId, regionOutputs).append('\n');
} else if (isActive(timeUs)) { } else if (isActive(timeUs)) {
...@@ -330,7 +332,7 @@ import java.util.TreeSet; ...@@ -330,7 +332,7 @@ import java.util.TreeSet;
int start = nodeStartsByRegion.containsKey(regionId) ? nodeStartsByRegion.get(regionId) : 0; int start = nodeStartsByRegion.containsKey(regionId) ? nodeStartsByRegion.get(regionId) : 0;
int end = entry.getValue(); int end = entry.getValue();
if (start != end) { if (start != end) {
SpannableStringBuilder regionOutput = regionOutputs.get(regionId); SpannableStringBuilder regionOutput = Assertions.checkNotNull(regionOutputs.get(regionId));
applyStyleToOutput(globalStyles, regionOutput, start, end); applyStyleToOutput(globalStyles, regionOutput, start, end);
} }
} }
...@@ -344,7 +346,7 @@ import java.util.TreeSet; ...@@ -344,7 +346,7 @@ import java.util.TreeSet;
SpannableStringBuilder regionOutput, SpannableStringBuilder regionOutput,
int start, int start,
int end) { int end) {
TtmlStyle resolvedStyle = TtmlRenderUtil.resolveStyle(style, styleIds, globalStyles); @Nullable TtmlStyle resolvedStyle = TtmlRenderUtil.resolveStyle(style, styleIds, globalStyles);
if (resolvedStyle != null) { if (resolvedStyle != null) {
TtmlRenderUtil.applyStylesToSpan(regionOutput, start, end, resolvedStyle); TtmlRenderUtil.applyStylesToSpan(regionOutput, start, end, resolvedStyle);
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.text.ttml; package com.google.android.exoplayer2.text.ttml;
import android.text.Layout.Alignment;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan; import android.text.style.AbsoluteSizeSpan;
...@@ -26,6 +27,7 @@ import android.text.style.StrikethroughSpan; ...@@ -26,6 +27,7 @@ import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.text.SpanUtil; import com.google.android.exoplayer2.text.SpanUtil;
import java.util.Map; import java.util.Map;
...@@ -34,30 +36,35 @@ import java.util.Map; ...@@ -34,30 +36,35 @@ import java.util.Map;
*/ */
/* package */ final class TtmlRenderUtil { /* package */ final class TtmlRenderUtil {
public static TtmlStyle resolveStyle(TtmlStyle style, String[] styleIds, @Nullable
Map<String, TtmlStyle> globalStyles) { public static TtmlStyle resolveStyle(
if (style == null && styleIds == null) { @Nullable TtmlStyle style, @Nullable String[] styleIds, Map<String, TtmlStyle> globalStyles) {
// No styles at all. if (style == null) {
return null; if (styleIds == null) {
} else if (style == null && styleIds.length == 1) { // No styles at all.
// Only one single referential style present. return null;
return globalStyles.get(styleIds[0]); } else if (styleIds.length == 1) {
} else if (style == null && styleIds.length > 1) { // Only one single referential style present.
// Only multiple referential styles present. return globalStyles.get(styleIds[0]);
TtmlStyle chainedStyle = new TtmlStyle(); } else if (styleIds.length > 1) {
for (String id : styleIds) { // Only multiple referential styles present.
chainedStyle.chain(globalStyles.get(id)); TtmlStyle chainedStyle = new TtmlStyle();
for (String id : styleIds) {
chainedStyle.chain(globalStyles.get(id));
}
return chainedStyle;
} }
return chainedStyle; } else /* style != null */ {
} else if (style != null && styleIds != null && styleIds.length == 1) { if (styleIds != null && styleIds.length == 1) {
// Merge a single referential style into inline style. // Merge a single referential style into inline style.
return style.chain(globalStyles.get(styleIds[0])); return style.chain(globalStyles.get(styleIds[0]));
} else if (style != null && styleIds != null && styleIds.length > 1) { } else if (styleIds != null && styleIds.length > 1) {
// Merge multiple referential styles into inline style. // Merge multiple referential styles into inline style.
for (String id : styleIds) { for (String id : styleIds) {
style.chain(globalStyles.get(id)); style.chain(globalStyles.get(id));
}
return style;
} }
return style;
} }
// Only inline styles available. // Only inline styles available.
return style; return style;
...@@ -100,10 +107,11 @@ import java.util.Map; ...@@ -100,10 +107,11 @@ import java.util.Map;
end, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
if (style.getTextAlign() != null) { @Nullable Alignment textAlign = style.getTextAlign();
if (textAlign != null) {
SpanUtil.addOrReplaceSpan( SpanUtil.addOrReplaceSpan(
builder, builder,
new AlignmentSpan.Standard(style.getTextAlign()), new AlignmentSpan.Standard(textAlign),
start, start,
end, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
......
...@@ -18,9 +18,11 @@ package com.google.android.exoplayer2.text.ttml; ...@@ -18,9 +18,11 @@ package com.google.android.exoplayer2.text.ttml;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.text.Layout; import android.text.Layout;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** /**
* Style object of a <code>TtmlNode</code> * Style object of a <code>TtmlNode</code>
...@@ -58,7 +60,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -58,7 +60,7 @@ import java.lang.annotation.RetentionPolicy;
private static final int OFF = 0; private static final int OFF = 0;
private static final int ON = 1; private static final int ON = 1;
private String fontFamily; private @MonotonicNonNull String fontFamily;
private int fontColor; private int fontColor;
private boolean hasFontColor; private boolean hasFontColor;
private int backgroundColor; private int backgroundColor;
...@@ -69,8 +71,8 @@ import java.lang.annotation.RetentionPolicy; ...@@ -69,8 +71,8 @@ import java.lang.annotation.RetentionPolicy;
@OptionalBoolean private int italic; @OptionalBoolean private int italic;
@FontSizeUnit private int fontSizeUnit; @FontSizeUnit private int fontSizeUnit;
private float fontSize; private float fontSize;
private String id; private @MonotonicNonNull String id;
private Layout.Alignment textAlign; private Layout.@MonotonicNonNull Alignment textAlign;
public TtmlStyle() { public TtmlStyle() {
linethrough = UNSPECIFIED; linethrough = UNSPECIFIED;
...@@ -122,6 +124,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -122,6 +124,7 @@ import java.lang.annotation.RetentionPolicy;
return this; return this;
} }
@Nullable
public String getFontFamily() { public String getFontFamily() {
return fontFamily; return fontFamily;
} }
...@@ -171,7 +174,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -171,7 +174,7 @@ import java.lang.annotation.RetentionPolicy;
* *
* @param ancestor the referential style to inherit from * @param ancestor the referential style to inherit from
*/ */
public TtmlStyle chain(TtmlStyle ancestor) { public TtmlStyle chain(@Nullable TtmlStyle ancestor) {
return inherit(ancestor, true); return inherit(ancestor, true);
} }
...@@ -182,11 +185,11 @@ import java.lang.annotation.RetentionPolicy; ...@@ -182,11 +185,11 @@ import java.lang.annotation.RetentionPolicy;
* *
* @param ancestor the ancestor style to inherit from * @param ancestor the ancestor style to inherit from
*/ */
public TtmlStyle inherit(TtmlStyle ancestor) { public TtmlStyle inherit(@Nullable TtmlStyle ancestor) {
return inherit(ancestor, false); return inherit(ancestor, false);
} }
private TtmlStyle inherit(TtmlStyle ancestor, boolean chaining) { private TtmlStyle inherit(@Nullable TtmlStyle ancestor, boolean chaining) {
if (ancestor != null) { if (ancestor != null) {
if (!hasFontColor && ancestor.hasFontColor) { if (!hasFontColor && ancestor.hasFontColor) {
setFontColor(ancestor.fontColor); setFontColor(ancestor.fontColor);
...@@ -197,7 +200,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -197,7 +200,7 @@ import java.lang.annotation.RetentionPolicy;
if (italic == UNSPECIFIED) { if (italic == UNSPECIFIED) {
italic = ancestor.italic; italic = ancestor.italic;
} }
if (fontFamily == null) { if (fontFamily == null && ancestor.fontFamily != null) {
fontFamily = ancestor.fontFamily; fontFamily = ancestor.fontFamily;
} }
if (linethrough == UNSPECIFIED) { if (linethrough == UNSPECIFIED) {
...@@ -206,7 +209,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -206,7 +209,7 @@ import java.lang.annotation.RetentionPolicy;
if (underline == UNSPECIFIED) { if (underline == UNSPECIFIED) {
underline = ancestor.underline; underline = ancestor.underline;
} }
if (textAlign == null) { if (textAlign == null && ancestor.textAlign != null) {
textAlign = ancestor.textAlign; textAlign = ancestor.textAlign;
} }
if (fontSizeUnit == UNSPECIFIED) { if (fontSizeUnit == UNSPECIFIED) {
...@@ -226,10 +229,12 @@ import java.lang.annotation.RetentionPolicy; ...@@ -226,10 +229,12 @@ import java.lang.annotation.RetentionPolicy;
return this; return this;
} }
@Nullable
public String getId() { public String getId() {
return id; return id;
} }
@Nullable
public Layout.Alignment getTextAlign() { public Layout.Alignment getTextAlign() {
return textAlign; return textAlign;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment