Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
Jin Seok Park | 482173e | 2018-01-24 21:48:28 +0900 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.content.Context; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 23 | import android.media.session.MediaController; |
| 24 | import android.media.update.ApiLoader; |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 25 | import android.media.update.MediaControlView2Provider; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 26 | import android.media.update.ViewProvider; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.view.KeyEvent; |
| 29 | import android.view.MotionEvent; |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame] | 30 | import android.view.View; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 31 | |
Jin Seok Park | 482173e | 2018-01-24 21:48:28 +0900 | [diff] [blame] | 32 | import java.lang.annotation.Retention; |
| 33 | import java.lang.annotation.RetentionPolicy; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 34 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 35 | * 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 Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 50 | * TODO PUBLIC API |
| 51 | * @hide |
| 52 | */ |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 53 | public class MediaControlView2 extends FrameLayout { |
Jin Seok Park | 482173e | 2018-01-24 21:48:28 +0900 | [diff] [blame] | 54 | /** @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 Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame] | 71 | 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 Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 83 | private final MediaControlView2Provider mProvider; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 84 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 85 | public MediaControlView2(@NonNull Context context) { |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 86 | this(context, null); |
| 87 | } |
| 88 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 89 | public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs) { |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 90 | this(context, attrs, 0); |
| 91 | } |
| 92 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 93 | public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs, |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 94 | int defStyleAttr) { |
| 95 | this(context, attrs, defStyleAttr, 0); |
| 96 | } |
| 97 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 98 | public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs, |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 99 | int defStyleAttr, int defStyleRes) { |
| 100 | super(context, attrs, defStyleAttr, defStyleRes); |
| 101 | |
| 102 | mProvider = ApiLoader.getProvider(context) |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 103 | .createMediaControlView2(this, new SuperProvider()); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 106 | /** |
| 107 | * @hide |
| 108 | */ |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 109 | public MediaControlView2Provider getProvider() { |
| 110 | return mProvider; |
| 111 | } |
| 112 | |
| 113 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 114 | * Sets MediaController2 instance to control corresponding MediaSession2. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 115 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 116 | public void setController(MediaController controller) { |
| 117 | mProvider.setController_impl(controller); |
| 118 | } |
| 119 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 120 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 121 | * Shows the control view on screen. It will disappear automatically after 3 seconds of |
| 122 | * inactivity. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 123 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 124 | public void show() { |
| 125 | mProvider.show_impl(); |
| 126 | } |
| 127 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 128 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 129 | * Shows the control view on screen. It will disappear automatically after {@code timeout} |
| 130 | * milliseconds of inactivity. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 131 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 132 | public void show(int timeout) { |
| 133 | mProvider.show_impl(timeout); |
| 134 | } |
| 135 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 136 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 137 | * Returns whether the control view is currently shown or hidden. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 138 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 139 | public boolean isShowing() { |
| 140 | return mProvider.isShowing_impl(); |
| 141 | } |
| 142 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 143 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 144 | * Hide the control view from the screen. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 145 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 146 | public void hide() { |
| 147 | mProvider.hide_impl(); |
| 148 | } |
| 149 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 150 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 151 | * 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 Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 154 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 155 | public void showSubtitle() { |
| 156 | mProvider.showSubtitle_impl(); |
| 157 | } |
| 158 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 159 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 160 | * Hides the currently displayed subtitle. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 161 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 162 | public void hideSubtitle() { |
| 163 | mProvider.hideSubtitle_impl(); |
| 164 | } |
| 165 | |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame] | 166 | /** |
| 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 Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 188 | @Override |
Insun Kang | fd3fdfd | 2018-01-23 15:01:37 +0900 | [diff] [blame] | 189 | protected void onAttachedToWindow() { |
| 190 | mProvider.onAttachedToWindow_impl(); |
| 191 | } |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame] | 192 | |
Insun Kang | fd3fdfd | 2018-01-23 15:01:37 +0900 | [diff] [blame] | 193 | @Override |
| 194 | protected void onDetachedFromWindow() { |
| 195 | mProvider.onDetachedFromWindow_impl(); |
| 196 | } |
| 197 | |
| 198 | @Override |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 199 | 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 Kang | fd3fdfd | 2018-01-23 15:01:37 +0900 | [diff] [blame] | 235 | 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 Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 245 | public CharSequence getAccessibilityClassName_impl() { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 246 | return MediaControlView2.super.getAccessibilityClassName(); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public boolean onTouchEvent_impl(MotionEvent ev) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 251 | return MediaControlView2.super.onTouchEvent(ev); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public boolean onTrackballEvent_impl(MotionEvent ev) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 256 | return MediaControlView2.super.onTrackballEvent(ev); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public boolean onKeyDown_impl(int keyCode, KeyEvent event) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 261 | return MediaControlView2.super.onKeyDown(keyCode, event); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void onFinishInflate_impl() { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 266 | MediaControlView2.super.onFinishInflate(); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public boolean dispatchKeyEvent_impl(KeyEvent event) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 271 | return MediaControlView2.super.dispatchKeyEvent(event); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void setEnabled_impl(boolean enabled) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 276 | MediaControlView2.super.setEnabled(enabled); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |