Do not throw on valid SubtitleView.setViewType

Calling setViewType with the same view type as the
subtitleView is using would throw InvalidArgumentException
instead of being a noop.

PiperOrigin-RevId: 291937202
This commit is contained in:
krocard 2020-01-28 16:21:50 +00:00 committed by Ian Baker
parent 2fd8cf0206
commit e92ea31fcd

View File

@ -84,6 +84,7 @@ public final class SubtitleView extends ViewGroup implements TextOutput {
@Retention(SOURCE) @Retention(SOURCE)
public @interface ViewType {} public @interface ViewType {}
private @ViewType int viewType;
private Output output; private Output output;
private View innerSubtitleView; private View innerSubtitleView;
@ -97,6 +98,7 @@ public final class SubtitleView extends ViewGroup implements TextOutput {
output = subtitleTextView; output = subtitleTextView;
innerSubtitleView = subtitleTextView; innerSubtitleView = subtitleTextView;
addView(innerSubtitleView); addView(innerSubtitleView);
viewType = VIEW_TYPE_TEXT;
} }
@Override @Override
@ -126,14 +128,18 @@ public final class SubtitleView extends ViewGroup implements TextOutput {
* <p>NOTE: {@link #VIEW_TYPE_WEB} is currently very experimental, and doesn't support most * <p>NOTE: {@link #VIEW_TYPE_WEB} is currently very experimental, and doesn't support most
* styling and layout properties of {@link Cue}. * styling and layout properties of {@link Cue}.
*/ */
public void setViewType(@ViewType int viewType) { public void setViewType(@ViewType int newViewType) {
if (viewType == VIEW_TYPE_TEXT && !(innerSubtitleView instanceof SubtitleTextView)) { if (viewType == newViewType) {
return;
}
if (newViewType == VIEW_TYPE_TEXT) {
setView(new SubtitleTextView(getContext())); setView(new SubtitleTextView(getContext()));
} else if (viewType == VIEW_TYPE_WEB && !(innerSubtitleView instanceof SubtitleWebView)) { } else if (newViewType == VIEW_TYPE_WEB) {
setView(new SubtitleWebView(getContext())); setView(new SubtitleWebView(getContext()));
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
viewType = newViewType;
} }
private <T extends View & Output> void setView(T view) { private <T extends View & Output> void setView(T view) {