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 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.content.Context; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 22 | import android.media.session.MediaController; |
| 23 | import android.media.update.ApiLoader; |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 24 | import android.media.update.MediaControlView2Provider; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 25 | import android.media.update.ViewProvider; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.view.KeyEvent; |
| 28 | import android.view.MotionEvent; |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame^] | 29 | import android.view.View; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 30 | |
| 31 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 32 | * A View that contains the controls for MediaPlayer2. |
| 33 | * It provides a wide range of UI including buttons such as "Play/Pause", "Rewind", "Fast Forward", |
| 34 | * "Subtitle", "Full Screen", and it is also possible to add multiple custom buttons. |
| 35 | * |
| 36 | * <p> |
| 37 | * <em> MediaControlView2 can be initialized in two different ways: </em> |
| 38 | * 1) When VideoView2 is initialized, it automatically initializes a MediaControlView2 instance and |
| 39 | * adds it to the view. |
| 40 | * 2) Initialize MediaControlView2 programmatically and add it to a ViewGroup instance. |
| 41 | * |
| 42 | * In the first option, VideoView2 automatically connects MediaControlView2 to MediaController2, |
| 43 | * which is necessary to communicate with MediaSession2. In the second option, however, the |
| 44 | * developer needs to manually retrieve a MediaController2 instance and set it to MediaControlView2 |
| 45 | * by calling setController(MediaController2 controller). |
| 46 | * |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 47 | * TODO PUBLIC API |
| 48 | * @hide |
| 49 | */ |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 50 | public class MediaControlView2 extends FrameLayout { |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame^] | 51 | // TODO: should overflow button be included? |
| 52 | public static final int BUTTON_PLAY_PAUSE = 1; |
| 53 | public static final int BUTTON_FFWD = 2; |
| 54 | public static final int BUTTON_REW = 3; |
| 55 | public static final int BUTTON_NEXT = 4; |
| 56 | public static final int BUTTON_PREV = 5; |
| 57 | public static final int BUTTON_SUBTITLE = 6; |
| 58 | public static final int BUTTON_FULL_SCREEN = 7; |
| 59 | public static final int BUTTON_OVERFLOW = 8; |
| 60 | public static final int BUTTON_MUTE = 9; |
| 61 | public static final int BUTTON_ASPECT_RATIO = 10; |
| 62 | public static final int BUTTON_SETTINGS = 11; |
| 63 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 64 | private final MediaControlView2Provider mProvider; |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 65 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 66 | public MediaControlView2(@NonNull Context context) { |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 67 | this(context, null); |
| 68 | } |
| 69 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 70 | public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs) { |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 71 | this(context, attrs, 0); |
| 72 | } |
| 73 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 74 | public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs, |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 75 | int defStyleAttr) { |
| 76 | this(context, attrs, defStyleAttr, 0); |
| 77 | } |
| 78 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 79 | public MediaControlView2(@NonNull Context context, @Nullable AttributeSet attrs, |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 80 | int defStyleAttr, int defStyleRes) { |
| 81 | super(context, attrs, defStyleAttr, defStyleRes); |
| 82 | |
| 83 | mProvider = ApiLoader.getProvider(context) |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 84 | .createMediaControlView2(this, new SuperProvider()); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 87 | /** |
| 88 | * @hide |
| 89 | */ |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 90 | public MediaControlView2Provider getProvider() { |
| 91 | return mProvider; |
| 92 | } |
| 93 | |
| 94 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 95 | * Sets MediaController2 instance to control corresponding MediaSession2. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 96 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 97 | public void setController(MediaController controller) { |
| 98 | mProvider.setController_impl(controller); |
| 99 | } |
| 100 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 101 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 102 | * Shows the control view on screen. It will disappear automatically after 3 seconds of |
| 103 | * inactivity. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 104 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 105 | public void show() { |
| 106 | mProvider.show_impl(); |
| 107 | } |
| 108 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 109 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 110 | * Shows the control view on screen. It will disappear automatically after {@code timeout} |
| 111 | * milliseconds of inactivity. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 112 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 113 | public void show(int timeout) { |
| 114 | mProvider.show_impl(timeout); |
| 115 | } |
| 116 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 117 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 118 | * Returns whether the control view is currently shown or hidden. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 119 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 120 | public boolean isShowing() { |
| 121 | return mProvider.isShowing_impl(); |
| 122 | } |
| 123 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 124 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 125 | * Hide the control view from the screen. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 126 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 127 | public void hide() { |
| 128 | mProvider.hide_impl(); |
| 129 | } |
| 130 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 131 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 132 | * Returns whether the media is currently playing or not. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 133 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 134 | public boolean isPlaying() { |
| 135 | return mProvider.isPlaying_impl(); |
| 136 | } |
| 137 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 138 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 139 | * Returns the current position of the media in milliseconds. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 140 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 141 | public int getCurrentPosition() { |
| 142 | return mProvider.getCurrentPosition_impl(); |
| 143 | } |
| 144 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 145 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 146 | * Returns the percentage of how much of the media is currently buffered in storage. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 147 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 148 | public int getBufferPercentage() { |
| 149 | return mProvider.getBufferPercentage_impl(); |
| 150 | } |
| 151 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 152 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 153 | * Returns whether the media can be paused or not. |
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 boolean canPause() { |
| 156 | return mProvider.canPause_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 | * Returns whether the media can be rewound or not. |
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 boolean canSeekBackward() { |
| 163 | return mProvider.canSeekBackward_impl(); |
| 164 | } |
| 165 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 166 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 167 | * Returns whether the media can be fast-forwarded or not. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 168 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 169 | public boolean canSeekForward() { |
| 170 | return mProvider.canSeekForward_impl(); |
| 171 | } |
| 172 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 173 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 174 | * If the media selected has a subtitle track, calling this method will display the subtitle at |
| 175 | * the bottom of the view. If a media has multiple subtitle tracks, this method will select the |
| 176 | * first one of them. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 177 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 178 | public void showSubtitle() { |
| 179 | mProvider.showSubtitle_impl(); |
| 180 | } |
| 181 | |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 182 | /** |
Jin Seok Park | d9ce1bc | 2018-01-24 00:52:11 +0900 | [diff] [blame] | 183 | * Hides the currently displayed subtitle. |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 184 | */ |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 185 | public void hideSubtitle() { |
| 186 | mProvider.hideSubtitle_impl(); |
| 187 | } |
| 188 | |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame^] | 189 | /** |
| 190 | * Set listeners for previous and next buttons to customize the behavior of clicking them. |
| 191 | * The UI for these buttons are provided as default and will be automatically displayed when |
| 192 | * this method is called. |
| 193 | * |
| 194 | * @param next Listener for clicking next button |
| 195 | * @param prev Listener for clicking previous button |
| 196 | */ |
| 197 | public void setPrevNextListeners(View.OnClickListener next, View.OnClickListener prev) { |
| 198 | mProvider.setPrevNextListeners_impl(next, prev); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Hides the specified button from view. |
| 203 | * |
| 204 | * @param button the constant integer assigned to individual buttons |
| 205 | * @param visible whether the button should be visible or not |
| 206 | */ |
| 207 | public void setButtonVisibility(int button, boolean visible) { |
| 208 | mProvider.setButtonVisibility_impl(button, visible); |
| 209 | } |
| 210 | |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 211 | @Override |
Insun Kang | fd3fdfd | 2018-01-23 15:01:37 +0900 | [diff] [blame] | 212 | protected void onAttachedToWindow() { |
| 213 | mProvider.onAttachedToWindow_impl(); |
| 214 | } |
Hyundo Moon | efeb45e | 2018-01-24 11:16:19 +0900 | [diff] [blame^] | 215 | |
Insun Kang | fd3fdfd | 2018-01-23 15:01:37 +0900 | [diff] [blame] | 216 | @Override |
| 217 | protected void onDetachedFromWindow() { |
| 218 | mProvider.onDetachedFromWindow_impl(); |
| 219 | } |
| 220 | |
| 221 | @Override |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 222 | public CharSequence getAccessibilityClassName() { |
| 223 | return mProvider.getAccessibilityClassName_impl(); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean onTouchEvent(MotionEvent ev) { |
| 228 | return mProvider.onTouchEvent_impl(ev); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean onTrackballEvent(MotionEvent ev) { |
| 233 | return mProvider.onTrackballEvent_impl(ev); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 238 | return mProvider.onKeyDown_impl(keyCode, event); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void onFinishInflate() { |
| 243 | mProvider.onFinishInflate_impl(); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 248 | return mProvider.dispatchKeyEvent_impl(event); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void setEnabled(boolean enabled) { |
| 253 | mProvider.setEnabled_impl(enabled); |
| 254 | } |
| 255 | |
| 256 | private class SuperProvider implements ViewProvider { |
| 257 | @Override |
Insun Kang | fd3fdfd | 2018-01-23 15:01:37 +0900 | [diff] [blame] | 258 | public void onAttachedToWindow_impl() { |
| 259 | MediaControlView2.super.onAttachedToWindow(); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onDetachedFromWindow_impl() { |
| 264 | MediaControlView2.super.onDetachedFromWindow(); |
| 265 | } |
| 266 | |
| 267 | @Override |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 268 | public CharSequence getAccessibilityClassName_impl() { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 269 | return MediaControlView2.super.getAccessibilityClassName(); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public boolean onTouchEvent_impl(MotionEvent ev) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 274 | return MediaControlView2.super.onTouchEvent(ev); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public boolean onTrackballEvent_impl(MotionEvent ev) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 279 | return MediaControlView2.super.onTrackballEvent(ev); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean onKeyDown_impl(int keyCode, KeyEvent event) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 284 | return MediaControlView2.super.onKeyDown(keyCode, event); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void onFinishInflate_impl() { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 289 | MediaControlView2.super.onFinishInflate(); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public boolean dispatchKeyEvent_impl(KeyEvent event) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 294 | return MediaControlView2.super.dispatchKeyEvent(event); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void setEnabled_impl(boolean enabled) { |
Insun Kang | de16c4d | 2018-01-17 23:06:18 +0900 | [diff] [blame] | 299 | MediaControlView2.super.setEnabled(enabled); |
Christofer Åkersten | cfa0370 | 2017-12-20 17:58:15 +0900 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |