blob: f1d633a2313115b473607b55a008f6056b6dc9f4 [file] [log] [blame]
Christofer Åkerstencfa03702017-12-20 17:58:15 +09001/*
2 * Copyright (C) 2017 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 android.widget;
18
Jin Seok Park482173e2018-01-24 21:48:28 +090019import android.annotation.IntDef;
Christofer Åkerstencfa03702017-12-20 17:58:15 +090020import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.content.Context;
Christofer Åkerstencfa03702017-12-20 17:58:15 +090023import android.media.session.MediaController;
24import android.media.update.ApiLoader;
Insun Kangde16c4d2018-01-17 23:06:18 +090025import android.media.update.MediaControlView2Provider;
Christofer Åkerstencfa03702017-12-20 17:58:15 +090026import android.media.update.ViewProvider;
27import android.util.AttributeSet;
28import android.view.KeyEvent;
29import android.view.MotionEvent;
Hyundo Moonefeb45e2018-01-24 11:16:19 +090030import android.view.View;
Christofer Åkerstencfa03702017-12-20 17:58:15 +090031
Jin Seok Park482173e2018-01-24 21:48:28 +090032import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
Christofer Åkerstencfa03702017-12-20 17:58:15 +090034/**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +090035 * A View that contains the controls for MediaPlayer2.
36 * It provides a wide range of UI including buttons such as "Play/Pause", "Rewind", "Fast Forward",
37 * "Subtitle", "Full Screen", and it is also possible to add multiple custom buttons.
38 *
39 * <p>
40 * <em> MediaControlView2 can be initialized in two different ways: </em>
41 * 1) When VideoView2 is initialized, it automatically initializes a MediaControlView2 instance and
42 * adds it to the view.
43 * 2) Initialize MediaControlView2 programmatically and add it to a ViewGroup instance.
44 *
45 * In the first option, VideoView2 automatically connects MediaControlView2 to MediaController2,
46 * which is necessary to communicate with MediaSession2. In the second option, however, the
47 * developer needs to manually retrieve a MediaController2 instance and set it to MediaControlView2
48 * by calling setController(MediaController2 controller).
49 *
Christofer Åkerstencfa03702017-12-20 17:58:15 +090050 * TODO PUBLIC API
51 * @hide
52 */
Insun Kangde16c4d2018-01-17 23:06:18 +090053public class MediaControlView2 extends FrameLayout {
Jin Seok Park482173e2018-01-24 21:48:28 +090054 /** @hide */
55 @IntDef({
56 BUTTON_PLAY_PAUSE,
57 BUTTON_FFWD,
58 BUTTON_REW,
59 BUTTON_NEXT,
60 BUTTON_PREV,
61 BUTTON_SUBTITLE,
62 BUTTON_FULL_SCREEN,
63 BUTTON_OVERFLOW,
64 BUTTON_MUTE,
65 BUTTON_ASPECT_RATIO,
66 BUTTON_SETTINGS
67 })
68 @Retention(RetentionPolicy.SOURCE)
69 public @interface Button {}
70
Hyundo Moonefeb45e2018-01-24 11:16:19 +090071 public static final int BUTTON_PLAY_PAUSE = 1;
72 public static final int BUTTON_FFWD = 2;
73 public static final int BUTTON_REW = 3;
74 public static final int BUTTON_NEXT = 4;
75 public static final int BUTTON_PREV = 5;
76 public static final int BUTTON_SUBTITLE = 6;
77 public static final int BUTTON_FULL_SCREEN = 7;
78 public static final int BUTTON_OVERFLOW = 8;
79 public static final int BUTTON_MUTE = 9;
80 public static final int BUTTON_ASPECT_RATIO = 10;
81 public static final int BUTTON_SETTINGS = 11;
82
Insun Kangde16c4d2018-01-17 23:06:18 +090083 private final MediaControlView2Provider mProvider;
Christofer Åkerstencfa03702017-12-20 17:58:15 +090084
Insun Kangde16c4d2018-01-17 23:06:18 +090085 public MediaControlView2(@NonNull Context context) {
Christofer Åkerstencfa03702017-12-20 17:58:15 +090086 this(context, null);
87 }
88
Insun Kangde16c4d2018-01-17 23:06:18 +090089 public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs) {
Christofer Åkerstencfa03702017-12-20 17:58:15 +090090 this(context, attrs, 0);
91 }
92
Insun Kangde16c4d2018-01-17 23:06:18 +090093 public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs,
Christofer Åkerstencfa03702017-12-20 17:58:15 +090094 int defStyleAttr) {
95 this(context, attrs, defStyleAttr, 0);
96 }
97
Insun Kangde16c4d2018-01-17 23:06:18 +090098 public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs,
Christofer Åkerstencfa03702017-12-20 17:58:15 +090099 int defStyleAttr, int defStyleRes) {
100 super(context, attrs, defStyleAttr, defStyleRes);
101
102 mProvider = ApiLoader.getProvider(context)
Insun Kangde16c4d2018-01-17 23:06:18 +0900103 .createMediaControlView2(this, new SuperProvider());
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900104 }
105
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900106 /**
107 * @hide
108 */
Insun Kangde16c4d2018-01-17 23:06:18 +0900109 public MediaControlView2Provider getProvider() {
110 return mProvider;
111 }
112
113 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900114 * Sets MediaController2 instance to control corresponding MediaSession2.
Insun Kangde16c4d2018-01-17 23:06:18 +0900115 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900116 public void setController(MediaController controller) {
117 mProvider.setController_impl(controller);
118 }
119
Insun Kangde16c4d2018-01-17 23:06:18 +0900120 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900121 * Shows the control view on screen. It will disappear automatically after 3 seconds of
122 * inactivity.
Insun Kangde16c4d2018-01-17 23:06:18 +0900123 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900124 public void show() {
125 mProvider.show_impl();
126 }
127
Insun Kangde16c4d2018-01-17 23:06:18 +0900128 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900129 * Shows the control view on screen. It will disappear automatically after {@code timeout}
130 * milliseconds of inactivity.
Insun Kangde16c4d2018-01-17 23:06:18 +0900131 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900132 public void show(int timeout) {
133 mProvider.show_impl(timeout);
134 }
135
Insun Kangde16c4d2018-01-17 23:06:18 +0900136 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900137 * Returns whether the control view is currently shown or hidden.
Insun Kangde16c4d2018-01-17 23:06:18 +0900138 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900139 public boolean isShowing() {
140 return mProvider.isShowing_impl();
141 }
142
Insun Kangde16c4d2018-01-17 23:06:18 +0900143 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900144 * Hide the control view from the screen.
Insun Kangde16c4d2018-01-17 23:06:18 +0900145 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900146 public void hide() {
147 mProvider.hide_impl();
148 }
149
Insun Kangde16c4d2018-01-17 23:06:18 +0900150 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900151 * If the media selected has a subtitle track, calling this method will display the subtitle at
152 * the bottom of the view. If a media has multiple subtitle tracks, this method will select the
153 * first one of them.
Insun Kangde16c4d2018-01-17 23:06:18 +0900154 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900155 public void showSubtitle() {
156 mProvider.showSubtitle_impl();
157 }
158
Insun Kangde16c4d2018-01-17 23:06:18 +0900159 /**
Jin Seok Parkd9ce1bc2018-01-24 00:52:11 +0900160 * Hides the currently displayed subtitle.
Insun Kangde16c4d2018-01-17 23:06:18 +0900161 */
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900162 public void hideSubtitle() {
163 mProvider.hideSubtitle_impl();
164 }
165
Hyundo Moonefeb45e2018-01-24 11:16:19 +0900166 /**
167 * Set listeners for previous and next buttons to customize the behavior of clicking them.
168 * The UI for these buttons are provided as default and will be automatically displayed when
169 * this method is called.
170 *
171 * @param next Listener for clicking next button
172 * @param prev Listener for clicking previous button
173 */
174 public void setPrevNextListeners(View.OnClickListener next, View.OnClickListener prev) {
175 mProvider.setPrevNextListeners_impl(next, prev);
176 }
177
178 /**
179 * Hides the specified button from view.
180 *
181 * @param button the constant integer assigned to individual buttons
182 * @param visible whether the button should be visible or not
183 */
184 public void setButtonVisibility(int button, boolean visible) {
185 mProvider.setButtonVisibility_impl(button, visible);
186 }
187
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900188 @Override
Insun Kangfd3fdfd2018-01-23 15:01:37 +0900189 protected void onAttachedToWindow() {
190 mProvider.onAttachedToWindow_impl();
191 }
Hyundo Moonefeb45e2018-01-24 11:16:19 +0900192
Insun Kangfd3fdfd2018-01-23 15:01:37 +0900193 @Override
194 protected void onDetachedFromWindow() {
195 mProvider.onDetachedFromWindow_impl();
196 }
197
198 @Override
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900199 public CharSequence getAccessibilityClassName() {
200 return mProvider.getAccessibilityClassName_impl();
201 }
202
203 @Override
204 public boolean onTouchEvent(MotionEvent ev) {
205 return mProvider.onTouchEvent_impl(ev);
206 }
207
208 @Override
209 public boolean onTrackballEvent(MotionEvent ev) {
210 return mProvider.onTrackballEvent_impl(ev);
211 }
212
213 @Override
214 public boolean onKeyDown(int keyCode, KeyEvent event) {
215 return mProvider.onKeyDown_impl(keyCode, event);
216 }
217
218 @Override
219 public void onFinishInflate() {
220 mProvider.onFinishInflate_impl();
221 }
222
223 @Override
224 public boolean dispatchKeyEvent(KeyEvent event) {
225 return mProvider.dispatchKeyEvent_impl(event);
226 }
227
228 @Override
229 public void setEnabled(boolean enabled) {
230 mProvider.setEnabled_impl(enabled);
231 }
232
233 private class SuperProvider implements ViewProvider {
234 @Override
Insun Kangfd3fdfd2018-01-23 15:01:37 +0900235 public void onAttachedToWindow_impl() {
236 MediaControlView2.super.onAttachedToWindow();
237 }
238
239 @Override
240 public void onDetachedFromWindow_impl() {
241 MediaControlView2.super.onDetachedFromWindow();
242 }
243
244 @Override
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900245 public CharSequence getAccessibilityClassName_impl() {
Insun Kangde16c4d2018-01-17 23:06:18 +0900246 return MediaControlView2.super.getAccessibilityClassName();
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900247 }
248
249 @Override
250 public boolean onTouchEvent_impl(MotionEvent ev) {
Insun Kangde16c4d2018-01-17 23:06:18 +0900251 return MediaControlView2.super.onTouchEvent(ev);
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900252 }
253
254 @Override
255 public boolean onTrackballEvent_impl(MotionEvent ev) {
Insun Kangde16c4d2018-01-17 23:06:18 +0900256 return MediaControlView2.super.onTrackballEvent(ev);
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900257 }
258
259 @Override
260 public boolean onKeyDown_impl(int keyCode, KeyEvent event) {
Insun Kangde16c4d2018-01-17 23:06:18 +0900261 return MediaControlView2.super.onKeyDown(keyCode, event);
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900262 }
263
264 @Override
265 public void onFinishInflate_impl() {
Insun Kangde16c4d2018-01-17 23:06:18 +0900266 MediaControlView2.super.onFinishInflate();
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900267 }
268
269 @Override
270 public boolean dispatchKeyEvent_impl(KeyEvent event) {
Insun Kangde16c4d2018-01-17 23:06:18 +0900271 return MediaControlView2.super.dispatchKeyEvent(event);
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900272 }
273
274 @Override
275 public void setEnabled_impl(boolean enabled) {
Insun Kangde16c4d2018-01-17 23:06:18 +0900276 MediaControlView2.super.setEnabled(enabled);
Christofer Åkerstencfa03702017-12-20 17:58:15 +0900277 }
278 }
279}