• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.widget;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.os.Looper;
22 import android.support.annotation.Nullable;
23 import android.util.AttributeSet;
24 import android.widget.FrameLayout;
25 
26 import com.android.media.subtitle.SubtitleController.Anchor;
27 import com.android.media.subtitle.SubtitleTrack.RenderingWidget;
28 
29 class SubtitleView extends FrameLayout implements Anchor {
30     private static final String TAG = "SubtitleView";
31 
32     private RenderingWidget mSubtitleWidget;
33     private RenderingWidget.OnChangedListener mSubtitlesChangedListener;
34 
SubtitleView(Context context)35     public SubtitleView(Context context) {
36         this(context, null);
37     }
38 
SubtitleView(Context context, @Nullable AttributeSet attrs)39     public SubtitleView(Context context, @Nullable AttributeSet attrs) {
40         this(context, attrs, 0);
41     }
42 
SubtitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)43     public SubtitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
44         this(context, attrs, defStyleAttr, 0);
45     }
46 
SubtitleView( Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)47     public SubtitleView(
48             Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49         super(context, attrs, defStyleAttr, defStyleRes);
50     }
51 
52     @Override
setSubtitleWidget(RenderingWidget subtitleWidget)53     public void setSubtitleWidget(RenderingWidget subtitleWidget) {
54         if (mSubtitleWidget == subtitleWidget) {
55             return;
56         }
57 
58         final boolean attachedToWindow = isAttachedToWindow();
59         if (mSubtitleWidget != null) {
60             if (attachedToWindow) {
61                 mSubtitleWidget.onDetachedFromWindow();
62             }
63 
64             mSubtitleWidget.setOnChangedListener(null);
65         }
66         mSubtitleWidget = subtitleWidget;
67 
68         if (subtitleWidget != null) {
69             if (mSubtitlesChangedListener == null) {
70                 mSubtitlesChangedListener = new RenderingWidget.OnChangedListener() {
71                     @Override
72                     public void onChanged(RenderingWidget renderingWidget) {
73                         invalidate();
74                     }
75                 };
76             }
77 
78             setWillNotDraw(false);
79             subtitleWidget.setOnChangedListener(mSubtitlesChangedListener);
80 
81             if (attachedToWindow) {
82                 subtitleWidget.onAttachedToWindow();
83                 requestLayout();
84             }
85         } else {
86             setWillNotDraw(true);
87         }
88 
89         invalidate();
90     }
91 
92     @Override
getSubtitleLooper()93     public Looper getSubtitleLooper() {
94         return Looper.getMainLooper();
95     }
96 
97     @Override
onAttachedToWindow()98     public void onAttachedToWindow() {
99         super.onAttachedToWindow();
100 
101         if (mSubtitleWidget != null) {
102             mSubtitleWidget.onAttachedToWindow();
103         }
104     }
105 
106     @Override
onDetachedFromWindow()107     public void onDetachedFromWindow() {
108         super.onDetachedFromWindow();
109 
110         if (mSubtitleWidget != null) {
111             mSubtitleWidget.onDetachedFromWindow();
112         }
113     }
114 
115     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)116     public void onLayout(boolean changed, int left, int top, int right, int bottom) {
117         super.onLayout(changed, left, top, right, bottom);
118 
119         if (mSubtitleWidget != null) {
120             final int width = getWidth() - getPaddingLeft() - getPaddingRight();
121             final int height = getHeight() - getPaddingTop() - getPaddingBottom();
122 
123             mSubtitleWidget.setSize(width, height);
124         }
125     }
126 
127     @Override
draw(Canvas canvas)128     public void draw(Canvas canvas) {
129         super.draw(canvas);
130 
131         if (mSubtitleWidget != null) {
132             final int saveCount = canvas.save();
133             canvas.translate(getPaddingLeft(), getPaddingTop());
134             mSubtitleWidget.draw(canvas);
135             canvas.restoreToCount(saveCount);
136         }
137     }
138 
139     @Override
getAccessibilityClassName()140     public CharSequence getAccessibilityClassName() {
141         return SubtitleView.class.getName();
142     }
143 }
144