diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 62ccb5e2b7..537e6e66bd 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -135,6 +135,8 @@ ([#7475](https://github.com/google/ExoPlayer/issues/7475)). * Redefine `Cue.lineType=LINE_TYPE_NUMBER` in terms of aligning the cue text lines to grid of viewport lines, and ignore `Cue.lineAnchor`. + * Check `CaptionManager.isEnabled()` before using it for user-specified + font-scaling. * DRM: * Add support for attaching DRM sessions to clear content in the demo app. * Remove `DrmSessionManager` references from all renderers. diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleView.java index 72a3a8384e..452be5a3b7 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleView.java @@ -34,7 +34,6 @@ import android.widget.FrameLayout; import androidx.annotation.Dimension; import androidx.annotation.IntDef; import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.text.CaptionStyleCompat; import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.TextOutput; @@ -225,12 +224,13 @@ public final class SubtitleView extends FrameLayout implements TextOutput { } /** - * Sets the text size to one derived from {@link CaptioningManager#getFontScale()}, or to a - * default size before API level 19. + * Sets the text size based on {@link CaptioningManager#getFontScale()} if {@link + * CaptioningManager} is available and enabled. + * + *

Otherwise (and always before API level 19) uses a default font scale of 1.0. */ public void setUserDefaultTextSize() { - float fontScale = Util.SDK_INT >= 19 && !isInEditMode() ? getUserCaptionFontScaleV19() : 1f; - setFractionalTextSize(DEFAULT_TEXT_SIZE_FRACTION * fontScale); + setFractionalTextSize(DEFAULT_TEXT_SIZE_FRACTION * getUserCaptionFontScale()); } /** @@ -291,14 +291,13 @@ public final class SubtitleView extends FrameLayout implements TextOutput { } /** - * Sets the caption style to be equivalent to the one returned by - * {@link CaptioningManager#getUserStyle()}, or to a default style before API level 19. + * Styles the captions using {@link CaptioningManager#getUserStyle()} if {@link CaptioningManager} + * is available and enabled. + * + *

Otherwise (and always before API level 19) uses a default style. */ public void setUserDefaultStyle() { - setStyle( - Util.SDK_INT >= 19 && isCaptionManagerEnabled() && !isInEditMode() - ? getUserCaptionStyleV19() - : CaptionStyleCompat.DEFAULT); + setStyle(getUserCaptionStyle()); } /** @@ -325,30 +324,28 @@ public final class SubtitleView extends FrameLayout implements TextOutput { updateOutput(); } - @RequiresApi(19) - private boolean isCaptionManagerEnabled() { + private float getUserCaptionFontScale() { + if (Util.SDK_INT < 19 || isInEditMode()) { + return 1f; + } @Nullable CaptioningManager captioningManager = (CaptioningManager) getContext().getSystemService(Context.CAPTIONING_SERVICE); - return captioningManager != null && captioningManager.isEnabled(); + return captioningManager != null && captioningManager.isEnabled() + ? captioningManager.getFontScale() + : 1f; } - @RequiresApi(19) - private float getUserCaptionFontScaleV19() { + private CaptionStyleCompat getUserCaptionStyle() { + if (Util.SDK_INT < 19 || isInEditMode()) { + return CaptionStyleCompat.DEFAULT; + } @Nullable CaptioningManager captioningManager = (CaptioningManager) getContext().getSystemService(Context.CAPTIONING_SERVICE); - return captioningManager == null ? 1f : captioningManager.getFontScale(); - } - - @RequiresApi(19) - private CaptionStyleCompat getUserCaptionStyleV19() { - @Nullable - CaptioningManager captioningManager = - (CaptioningManager) getContext().getSystemService(Context.CAPTIONING_SERVICE); - return captioningManager == null - ? CaptionStyleCompat.DEFAULT - : CaptionStyleCompat.createFromCaptionStyle(captioningManager.getUserStyle()); + return captioningManager != null && captioningManager.isEnabled() + ? CaptionStyleCompat.createFromCaptionStyle(captioningManager.getUserStyle()) + : CaptionStyleCompat.DEFAULT; } private void updateOutput() {