1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.media.widget;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.os.Looper;
22import android.util.AttributeSet;
23
24import androidx.annotation.Nullable;
25import androidx.annotation.RequiresApi;
26import androidx.media.subtitle.SubtitleController.Anchor;
27import androidx.media.subtitle.SubtitleTrack.RenderingWidget;
28
29@RequiresApi(21)
30class SubtitleView extends BaseLayout implements Anchor {
31    private static final String TAG = "SubtitleView";
32
33    private RenderingWidget mSubtitleWidget;
34    private RenderingWidget.OnChangedListener mSubtitlesChangedListener;
35
36    SubtitleView(Context context) {
37        this(context, null);
38    }
39
40    SubtitleView(Context context, @Nullable AttributeSet attrs) {
41        this(context, attrs, 0);
42    }
43
44    SubtitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
45        this(context, attrs, defStyleAttr, 0);
46    }
47
48    SubtitleView(
49            Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
50        super(context, attrs, defStyleAttr, defStyleRes);
51    }
52
53    @Override
54    public void setSubtitleWidget(RenderingWidget subtitleWidget) {
55        if (mSubtitleWidget == subtitleWidget) {
56            return;
57        }
58
59        final boolean attachedToWindow = isAttachedToWindow();
60        if (mSubtitleWidget != null) {
61            if (attachedToWindow) {
62                mSubtitleWidget.onDetachedFromWindow();
63            }
64
65            mSubtitleWidget.setOnChangedListener(null);
66        }
67        mSubtitleWidget = subtitleWidget;
68
69        if (subtitleWidget != null) {
70            if (mSubtitlesChangedListener == null) {
71                mSubtitlesChangedListener = new RenderingWidget.OnChangedListener() {
72                    @Override
73                    public void onChanged(RenderingWidget renderingWidget) {
74                        invalidate();
75                    }
76                };
77            }
78
79            setWillNotDraw(false);
80            subtitleWidget.setOnChangedListener(mSubtitlesChangedListener);
81
82            if (attachedToWindow) {
83                subtitleWidget.onAttachedToWindow();
84                requestLayout();
85            }
86        } else {
87            setWillNotDraw(true);
88        }
89
90        invalidate();
91    }
92
93    @Override
94    public Looper getSubtitleLooper() {
95        return Looper.getMainLooper();
96    }
97
98    @Override
99    public void onAttachedToWindow() {
100        super.onAttachedToWindow();
101
102        if (mSubtitleWidget != null) {
103            mSubtitleWidget.onAttachedToWindow();
104        }
105    }
106
107    @Override
108    public void onDetachedFromWindow() {
109        super.onDetachedFromWindow();
110
111        if (mSubtitleWidget != null) {
112            mSubtitleWidget.onDetachedFromWindow();
113        }
114    }
115
116    @Override
117    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
118        super.onLayout(changed, left, top, right, bottom);
119
120        if (mSubtitleWidget != null) {
121            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
122            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
123
124            mSubtitleWidget.setSize(width, height);
125        }
126    }
127
128    @Override
129    public void draw(Canvas canvas) {
130        super.draw(canvas);
131
132        if (mSubtitleWidget != null) {
133            final int saveCount = canvas.save();
134            canvas.translate(getPaddingLeft(), getPaddingTop());
135            mSubtitleWidget.draw(canvas);
136            canvas.restoreToCount(saveCount);
137        }
138    }
139
140    @Override
141    public CharSequence getAccessibilityClassName() {
142        return SubtitleView.class.getName();
143    }
144}
145