The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.graphics.PixelFormat; |
| 23 | import android.media.AudioManager; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame^] | 24 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.util.Log; |
| 27 | import android.view.Gravity; |
| 28 | import android.view.KeyEvent; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.MotionEvent; |
| 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
| 33 | import android.view.Window; |
| 34 | import android.view.WindowManager; |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 35 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 37 | |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 38 | import com.android.internal.policy.PhoneWindow; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import java.util.Formatter; |
| 41 | import java.util.Locale; |
| 42 | |
| 43 | /** |
| 44 | * A view containing controls for a MediaPlayer. Typically contains the |
| 45 | * buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress |
| 46 | * slider. It takes care of synchronizing the controls with the state |
| 47 | * of the MediaPlayer. |
| 48 | * <p> |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 49 | * The way to use this class is to instantiate it programmatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * The MediaController will create a default set of controls |
| 51 | * and put them in a window floating above your application. Specifically, |
| 52 | * the controls will float above the view specified with setAnchorView(). |
| 53 | * The window will disappear if left idle for three seconds and reappear |
| 54 | * when the user touches the anchor view. |
| 55 | * <p> |
| 56 | * Functions like show() and hide() have no effect when MediaController |
| 57 | * is created in an xml layout. |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 58 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | * MediaController will hide and |
| 60 | * show the buttons according to these rules: |
| 61 | * <ul> |
| 62 | * <li> The "previous" and "next" buttons are hidden until setPrevNextListeners() |
| 63 | * has been called |
| 64 | * <li> The "previous" and "next" buttons are visible but disabled if |
| 65 | * setPrevNextListeners() was called with null listeners |
| 66 | * <li> The "rewind" and "fastforward" buttons are shown unless requested |
| 67 | * otherwise by using the MediaController(Context, boolean) constructor |
| 68 | * with the boolean set to false |
| 69 | * </ul> |
| 70 | */ |
| 71 | public class MediaController extends FrameLayout { |
| 72 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 74 | private MediaPlayerControl mPlayer; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 75 | @UnsupportedAppUsage |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 76 | private final Context mContext; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 77 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 78 | private View mAnchor; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 79 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 80 | private View mRoot; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 81 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 82 | private WindowManager mWindowManager; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 83 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 84 | private Window mWindow; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 85 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 86 | private View mDecor; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 87 | @UnsupportedAppUsage |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 88 | private WindowManager.LayoutParams mDecorLayoutParams; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 89 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 90 | private ProgressBar mProgress; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame^] | 91 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Mathew Inwood | 986d790 | 2018-08-20 14:55:21 +0100 | [diff] [blame] | 92 | private TextView mEndTime; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame^] | 93 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Mathew Inwood | 986d790 | 2018-08-20 14:55:21 +0100 | [diff] [blame] | 94 | private TextView mCurrentTime; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 95 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 96 | private boolean mShowing; |
| 97 | private boolean mDragging; |
| 98 | private static final int sDefaultTimeout = 3000; |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 99 | private final boolean mUseFastForward; |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 100 | private boolean mFromXml; |
| 101 | private boolean mListenersSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | private View.OnClickListener mNextListener, mPrevListener; |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 103 | StringBuilder mFormatBuilder; |
| 104 | Formatter mFormatter; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 105 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 106 | private ImageButton mPauseButton; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 107 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 108 | private ImageButton mFfwdButton; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 109 | @UnsupportedAppUsage |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 110 | private ImageButton mRewButton; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame^] | 111 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 112 | private ImageButton mNextButton; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame^] | 113 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 114 | private ImageButton mPrevButton; |
| 115 | private CharSequence mPlayDescription; |
| 116 | private CharSequence mPauseDescription; |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 117 | private final AccessibilityManager mAccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
| 119 | public MediaController(Context context, AttributeSet attrs) { |
| 120 | super(context, attrs); |
| 121 | mRoot = this; |
| 122 | mContext = context; |
| 123 | mUseFastForward = true; |
| 124 | mFromXml = true; |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 125 | mAccessibilityManager = AccessibilityManager.getInstance(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onFinishInflate() { |
| 130 | if (mRoot != null) |
| 131 | initControllerView(mRoot); |
| 132 | } |
| 133 | |
| 134 | public MediaController(Context context, boolean useFastForward) { |
| 135 | super(context); |
| 136 | mContext = context; |
| 137 | mUseFastForward = useFastForward; |
Chih-Chung Chang | 02dd17d | 2011-09-29 12:49:04 +0800 | [diff] [blame] | 138 | initFloatingWindowLayout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | initFloatingWindow(); |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 140 | mAccessibilityManager = AccessibilityManager.getInstance(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | public MediaController(Context context) { |
Chih-Chung Chang | 02dd17d | 2011-09-29 12:49:04 +0800 | [diff] [blame] | 144 | this(context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private void initFloatingWindow() { |
Christian Mehlmauer | ef36752 | 2010-05-31 23:08:30 +0200 | [diff] [blame] | 148 | mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 149 | mWindow = new PhoneWindow(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | mWindow.setWindowManager(mWindowManager, null, null); |
| 151 | mWindow.requestFeature(Window.FEATURE_NO_TITLE); |
| 152 | mDecor = mWindow.getDecorView(); |
| 153 | mDecor.setOnTouchListener(mTouchListener); |
| 154 | mWindow.setContentView(this); |
| 155 | mWindow.setBackgroundDrawableResource(android.R.color.transparent); |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | // While the media controller is up, the volume control keys should |
| 158 | // affect the media stream type |
| 159 | mWindow.setVolumeControlStream(AudioManager.STREAM_MUSIC); |
| 160 | |
| 161 | setFocusable(true); |
| 162 | setFocusableInTouchMode(true); |
| 163 | setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); |
| 164 | requestFocus(); |
| 165 | } |
| 166 | |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 167 | // Allocate and initialize the static parts of mDecorLayoutParams. Must |
| 168 | // also call updateFloatingWindowLayout() to fill in the dynamic parts |
| 169 | // (y and width) before mDecorLayoutParams can be used. |
| 170 | private void initFloatingWindowLayout() { |
| 171 | mDecorLayoutParams = new WindowManager.LayoutParams(); |
| 172 | WindowManager.LayoutParams p = mDecorLayoutParams; |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 173 | p.gravity = Gravity.TOP | Gravity.LEFT; |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 174 | p.height = LayoutParams.WRAP_CONTENT; |
| 175 | p.x = 0; |
| 176 | p.format = PixelFormat.TRANSLUCENT; |
| 177 | p.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; |
| 178 | p.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
| 179 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 180 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; |
| 181 | p.token = null; |
| 182 | p.windowAnimations = 0; // android.R.style.DropDownAnimationDown; |
| 183 | } |
| 184 | |
| 185 | // Update the dynamic parts of mDecorLayoutParams |
| 186 | // Must be called with mAnchor != NULL. |
| 187 | private void updateFloatingWindowLayout() { |
| 188 | int [] anchorPos = new int[2]; |
| 189 | mAnchor.getLocationOnScreen(anchorPos); |
| 190 | |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 191 | // we need to know the size of the controller so we can properly position it |
| 192 | // within its space |
| 193 | mDecor.measure(MeasureSpec.makeMeasureSpec(mAnchor.getWidth(), MeasureSpec.AT_MOST), |
| 194 | MeasureSpec.makeMeasureSpec(mAnchor.getHeight(), MeasureSpec.AT_MOST)); |
| 195 | |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 196 | WindowManager.LayoutParams p = mDecorLayoutParams; |
| 197 | p.width = mAnchor.getWidth(); |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 198 | p.x = anchorPos[0] + (mAnchor.getWidth() - p.width) / 2; |
| 199 | p.y = anchorPos[1] + mAnchor.getHeight() - mDecor.getMeasuredHeight(); |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // This is called whenever mAnchor's layout bound changes |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 203 | private final OnLayoutChangeListener mLayoutChangeListener = |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 204 | new OnLayoutChangeListener() { |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 205 | @Override |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 206 | public void onLayoutChange(View v, int left, int top, int right, |
| 207 | int bottom, int oldLeft, int oldTop, int oldRight, |
| 208 | int oldBottom) { |
| 209 | updateFloatingWindowLayout(); |
| 210 | if (mShowing) { |
| 211 | mWindowManager.updateViewLayout(mDecor, mDecorLayoutParams); |
| 212 | } |
| 213 | } |
| 214 | }; |
| 215 | |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 216 | private final OnTouchListener mTouchListener = new OnTouchListener() { |
| 217 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | public boolean onTouch(View v, MotionEvent event) { |
| 219 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
| 220 | if (mShowing) { |
| 221 | hide(); |
| 222 | } |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | }; |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | public void setMediaPlayer(MediaPlayerControl player) { |
| 229 | mPlayer = player; |
| 230 | updatePausePlay(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Set the view that acts as the anchor for the control view. |
| 235 | * This can for example be a VideoView, or your Activity's main view. |
Marco Nelissen | 0b2d872 | 2012-11-12 15:20:57 -0800 | [diff] [blame] | 236 | * When VideoView calls this method, it will use the VideoView's parent |
| 237 | * as the anchor. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | * @param view The view to which to anchor the controller when it is visible. |
| 239 | */ |
| 240 | public void setAnchorView(View view) { |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 241 | if (mAnchor != null) { |
| 242 | mAnchor.removeOnLayoutChangeListener(mLayoutChangeListener); |
| 243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | mAnchor = view; |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 245 | if (mAnchor != null) { |
| 246 | mAnchor.addOnLayoutChangeListener(mLayoutChangeListener); |
| 247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
| 249 | FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams( |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 250 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 251 | ViewGroup.LayoutParams.MATCH_PARENT |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | ); |
| 253 | |
| 254 | removeAllViews(); |
| 255 | View v = makeControllerView(); |
| 256 | addView(v, frameParams); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Create the view that holds the widgets that control playback. |
| 261 | * Derived classes can override this to create their own. |
| 262 | * @return The controller view. |
| 263 | * @hide This doesn't work as advertised |
| 264 | */ |
| 265 | protected View makeControllerView() { |
| 266 | LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 267 | mRoot = inflate.inflate(com.android.internal.R.layout.media_controller, null); |
| 268 | |
| 269 | initControllerView(mRoot); |
| 270 | |
| 271 | return mRoot; |
| 272 | } |
| 273 | |
| 274 | private void initControllerView(View v) { |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 275 | Resources res = mContext.getResources(); |
| 276 | mPlayDescription = res |
| 277 | .getText(com.android.internal.R.string.lockscreen_transport_play_description); |
| 278 | mPauseDescription = res |
| 279 | .getText(com.android.internal.R.string.lockscreen_transport_pause_description); |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 280 | mPauseButton = v.findViewById(com.android.internal.R.id.pause); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | if (mPauseButton != null) { |
| 282 | mPauseButton.requestFocus(); |
| 283 | mPauseButton.setOnClickListener(mPauseListener); |
| 284 | } |
| 285 | |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 286 | mFfwdButton = v.findViewById(com.android.internal.R.id.ffwd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | if (mFfwdButton != null) { |
| 288 | mFfwdButton.setOnClickListener(mFfwdListener); |
| 289 | if (!mFromXml) { |
| 290 | mFfwdButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE); |
| 291 | } |
| 292 | } |
| 293 | |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 294 | mRewButton = v.findViewById(com.android.internal.R.id.rew); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | if (mRewButton != null) { |
| 296 | mRewButton.setOnClickListener(mRewListener); |
| 297 | if (!mFromXml) { |
| 298 | mRewButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE); |
| 299 | } |
| 300 | } |
| 301 | |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 302 | // By default these are hidden. They will be enabled when setPrevNextListeners() is called |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 303 | mNextButton = v.findViewById(com.android.internal.R.id.next); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | if (mNextButton != null && !mFromXml && !mListenersSet) { |
| 305 | mNextButton.setVisibility(View.GONE); |
| 306 | } |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 307 | mPrevButton = v.findViewById(com.android.internal.R.id.prev); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | if (mPrevButton != null && !mFromXml && !mListenersSet) { |
| 309 | mPrevButton.setVisibility(View.GONE); |
| 310 | } |
| 311 | |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 312 | mProgress = v.findViewById(com.android.internal.R.id.mediacontroller_progress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | if (mProgress != null) { |
| 314 | if (mProgress instanceof SeekBar) { |
| 315 | SeekBar seeker = (SeekBar) mProgress; |
| 316 | seeker.setOnSeekBarChangeListener(mSeekListener); |
| 317 | } |
| 318 | mProgress.setMax(1000); |
| 319 | } |
| 320 | |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 321 | mEndTime = v.findViewById(com.android.internal.R.id.time); |
| 322 | mCurrentTime = v.findViewById(com.android.internal.R.id.time_current); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | mFormatBuilder = new StringBuilder(); |
| 324 | mFormatter = new Formatter(mFormatBuilder, Locale.getDefault()); |
| 325 | |
| 326 | installPrevNextListeners(); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Show the controller on screen. It will go away |
| 331 | * automatically after 3 seconds of inactivity. |
| 332 | */ |
| 333 | public void show() { |
| 334 | show(sDefaultTimeout); |
| 335 | } |
| 336 | |
| 337 | /** |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 338 | * Disable pause or seek buttons if the stream cannot be paused or seeked. |
| 339 | * This requires the control interface to be a MediaPlayerControlExt |
| 340 | */ |
| 341 | private void disableUnsupportedButtons() { |
| 342 | try { |
| 343 | if (mPauseButton != null && !mPlayer.canPause()) { |
| 344 | mPauseButton.setEnabled(false); |
| 345 | } |
| 346 | if (mRewButton != null && !mPlayer.canSeekBackward()) { |
| 347 | mRewButton.setEnabled(false); |
| 348 | } |
| 349 | if (mFfwdButton != null && !mPlayer.canSeekForward()) { |
| 350 | mFfwdButton.setEnabled(false); |
| 351 | } |
Robert Shih | 39dbbd9 | 2015-04-27 12:50:39 -0700 | [diff] [blame] | 352 | // TODO What we really should do is add a canSeek to the MediaPlayerControl interface; |
| 353 | // this scheme can break the case when applications want to allow seek through the |
| 354 | // progress bar but disable forward/backward buttons. |
| 355 | // |
| 356 | // However, currently the flags SEEK_BACKWARD_AVAILABLE, SEEK_FORWARD_AVAILABLE, |
| 357 | // and SEEK_AVAILABLE are all (un)set together; as such the aforementioned issue |
| 358 | // shouldn't arise in existing applications. |
| 359 | if (mProgress != null && !mPlayer.canSeekBackward() && !mPlayer.canSeekForward()) { |
| 360 | mProgress.setEnabled(false); |
| 361 | } |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 362 | } catch (IncompatibleClassChangeError ex) { |
| 363 | // We were given an old version of the interface, that doesn't have |
| 364 | // the canPause/canSeekXYZ methods. This is OK, it just means we |
| 365 | // assume the media can be paused and seeked, and so we don't disable |
| 366 | // the buttons. |
| 367 | } |
| 368 | } |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 369 | |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 370 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | * Show the controller on screen. It will go away |
| 372 | * automatically after 'timeout' milliseconds of inactivity. |
| 373 | * @param timeout The timeout in milliseconds. Use 0 to show |
| 374 | * the controller until hide() is called. |
| 375 | */ |
| 376 | public void show(int timeout) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | if (!mShowing && mAnchor != null) { |
| 378 | setProgress(); |
Marco Nelissen | d701e02 | 2009-08-19 15:39:23 -0700 | [diff] [blame] | 379 | if (mPauseButton != null) { |
| 380 | mPauseButton.requestFocus(); |
| 381 | } |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 382 | disableUnsupportedButtons(); |
Chih-Chung Chang | 85d4ea6 | 2011-09-28 17:57:10 +0800 | [diff] [blame] | 383 | updateFloatingWindowLayout(); |
| 384 | mWindowManager.addView(mDecor, mDecorLayoutParams); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | mShowing = true; |
| 386 | } |
| 387 | updatePausePlay(); |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | // cause the progress bar to be updated even if mShowing |
| 390 | // was already true. This happens, for example, if we're |
| 391 | // paused with the progress bar showing the user hits play. |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 392 | post(mShowProgress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 394 | if (timeout != 0 && !mAccessibilityManager.isTouchExplorationEnabled()) { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 395 | removeCallbacks(mFadeOut); |
| 396 | postDelayed(mFadeOut, timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 399 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | public boolean isShowing() { |
| 401 | return mShowing; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Remove the controller from the screen. |
| 406 | */ |
| 407 | public void hide() { |
| 408 | if (mAnchor == null) |
| 409 | return; |
| 410 | |
| 411 | if (mShowing) { |
| 412 | try { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 413 | removeCallbacks(mShowProgress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | mWindowManager.removeView(mDecor); |
| 415 | } catch (IllegalArgumentException ex) { |
| 416 | Log.w("MediaController", "already removed"); |
| 417 | } |
| 418 | mShowing = false; |
| 419 | } |
| 420 | } |
| 421 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 422 | private final Runnable mFadeOut = new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | @Override |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 424 | public void run() { |
| 425 | hide(); |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | private final Runnable mShowProgress = new Runnable() { |
| 430 | @Override |
| 431 | public void run() { |
| 432 | int pos = setProgress(); |
| 433 | if (!mDragging && mShowing && mPlayer.isPlaying()) { |
| 434 | postDelayed(mShowProgress, 1000 - (pos % 1000)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | }; |
| 438 | |
| 439 | private String stringForTime(int timeMs) { |
| 440 | int totalSeconds = timeMs / 1000; |
| 441 | |
| 442 | int seconds = totalSeconds % 60; |
| 443 | int minutes = (totalSeconds / 60) % 60; |
| 444 | int hours = totalSeconds / 3600; |
| 445 | |
| 446 | mFormatBuilder.setLength(0); |
| 447 | if (hours > 0) { |
| 448 | return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString(); |
| 449 | } else { |
| 450 | return mFormatter.format("%02d:%02d", minutes, seconds).toString(); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | private int setProgress() { |
| 455 | if (mPlayer == null || mDragging) { |
| 456 | return 0; |
| 457 | } |
| 458 | int position = mPlayer.getCurrentPosition(); |
| 459 | int duration = mPlayer.getDuration(); |
| 460 | if (mProgress != null) { |
| 461 | if (duration > 0) { |
| 462 | // use long to avoid overflow |
| 463 | long pos = 1000L * position / duration; |
| 464 | mProgress.setProgress( (int) pos); |
| 465 | } |
| 466 | int percent = mPlayer.getBufferPercentage(); |
| 467 | mProgress.setSecondaryProgress(percent * 10); |
| 468 | } |
| 469 | |
| 470 | if (mEndTime != null) |
| 471 | mEndTime.setText(stringForTime(duration)); |
| 472 | if (mCurrentTime != null) |
| 473 | mCurrentTime.setText(stringForTime(position)); |
| 474 | |
| 475 | return position; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public boolean onTouchEvent(MotionEvent event) { |
Robert Shih | cfe4b59 | 2013-11-06 11:53:56 -0800 | [diff] [blame] | 480 | switch (event.getAction()) { |
| 481 | case MotionEvent.ACTION_DOWN: |
| 482 | show(0); // show until hide is called |
| 483 | break; |
| 484 | case MotionEvent.ACTION_UP: |
| 485 | show(sDefaultTimeout); // start timeout |
| 486 | break; |
| 487 | case MotionEvent.ACTION_CANCEL: |
| 488 | hide(); |
| 489 | break; |
| 490 | default: |
| 491 | break; |
| 492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public boolean onTrackballEvent(MotionEvent ev) { |
| 498 | show(sDefaultTimeout); |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 504 | int keyCode = event.getKeyCode(); |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 505 | final boolean uniqueDown = event.getRepeatCount() == 0 |
| 506 | && event.getAction() == KeyEvent.ACTION_DOWN; |
| 507 | if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK |
| 508 | || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE |
| 509 | || keyCode == KeyEvent.KEYCODE_SPACE) { |
| 510 | if (uniqueDown) { |
| 511 | doPauseResume(); |
| 512 | show(sDefaultTimeout); |
| 513 | if (mPauseButton != null) { |
| 514 | mPauseButton.requestFocus(); |
| 515 | } |
Marco Nelissen | d701e02 | 2009-08-19 15:39:23 -0700 | [diff] [blame] | 516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | return true; |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 518 | } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) { |
| 519 | if (uniqueDown && !mPlayer.isPlaying()) { |
| 520 | mPlayer.start(); |
| 521 | updatePausePlay(); |
| 522 | show(sDefaultTimeout); |
| 523 | } |
| 524 | return true; |
| 525 | } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP |
| 526 | || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) { |
| 527 | if (uniqueDown && mPlayer.isPlaying()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | mPlayer.pause(); |
| 529 | updatePausePlay(); |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 530 | show(sDefaultTimeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } |
| 532 | return true; |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 533 | } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 534 | || keyCode == KeyEvent.KEYCODE_VOLUME_UP |
bxu10X | 96ac720 | 2012-04-10 16:52:36 +0800 | [diff] [blame] | 535 | || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE |
| 536 | || keyCode == KeyEvent.KEYCODE_CAMERA) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | // don't show the controls for volume adjustment |
| 538 | return super.dispatchKeyEvent(event); |
| 539 | } else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) { |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 540 | if (uniqueDown) { |
| 541 | hide(); |
| 542 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 543 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | } |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 545 | |
| 546 | show(sDefaultTimeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | return super.dispatchKeyEvent(event); |
| 548 | } |
| 549 | |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 550 | private final View.OnClickListener mPauseListener = new View.OnClickListener() { |
| 551 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | public void onClick(View v) { |
| 553 | doPauseResume(); |
| 554 | show(sDefaultTimeout); |
| 555 | } |
| 556 | }; |
| 557 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 558 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | private void updatePausePlay() { |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 560 | if (mRoot == null || mPauseButton == null) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | return; |
| 562 | |
| 563 | if (mPlayer.isPlaying()) { |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 564 | mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_pause); |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 565 | mPauseButton.setContentDescription(mPauseDescription); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | } else { |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 567 | mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_play); |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 568 | mPauseButton.setContentDescription(mPlayDescription); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | private void doPauseResume() { |
| 573 | if (mPlayer.isPlaying()) { |
| 574 | mPlayer.pause(); |
| 575 | } else { |
| 576 | mPlayer.start(); |
| 577 | } |
| 578 | updatePausePlay(); |
| 579 | } |
| 580 | |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 581 | // There are two scenarios that can trigger the seekbar listener to trigger: |
| 582 | // |
| 583 | // The first is the user using the touchpad to adjust the posititon of the |
| 584 | // seekbar's thumb. In this case onStartTrackingTouch is called followed by |
| 585 | // a number of onProgressChanged notifications, concluded by onStopTrackingTouch. |
| 586 | // We're setting the field "mDragging" to true for the duration of the dragging |
| 587 | // session to avoid jumps in the position in case of ongoing playback. |
| 588 | // |
| 589 | // The second scenario involves the user operating the scroll ball, in this |
| 590 | // case there WON'T BE onStartTrackingTouch/onStopTrackingTouch notifications, |
| 591 | // we will simply apply the updated position without suspending regular updates. |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 592 | @UnsupportedAppUsage |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 593 | private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() { |
| 594 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | public void onStartTrackingTouch(SeekBar bar) { |
| 596 | show(3600000); |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 597 | |
| 598 | mDragging = true; |
| 599 | |
| 600 | // By removing these pending progress messages we make sure |
| 601 | // that a) we won't update the progress while the user adjusts |
| 602 | // the seekbar and b) once the user is done dragging the thumb |
| 603 | // we will post one of these messages to the queue again and |
| 604 | // this ensures that there will be exactly one message queued up. |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 605 | removeCallbacks(mShowProgress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | } |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 607 | |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 608 | @Override |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 609 | public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) { |
| 610 | if (!fromuser) { |
| 611 | // We're not interested in programmatically generated changes to |
| 612 | // the progress bar's position. |
| 613 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | } |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 615 | |
| 616 | long duration = mPlayer.getDuration(); |
| 617 | long newposition = (duration * progress) / 1000L; |
| 618 | mPlayer.seekTo( (int) newposition); |
| 619 | if (mCurrentTime != null) |
| 620 | mCurrentTime.setText(stringForTime( (int) newposition)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | } |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 622 | |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 623 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | public void onStopTrackingTouch(SeekBar bar) { |
| 625 | mDragging = false; |
| 626 | setProgress(); |
| 627 | updatePausePlay(); |
| 628 | show(sDefaultTimeout); |
Andreas Huber | 0de7dcd | 2009-04-20 14:22:31 -0700 | [diff] [blame] | 629 | |
| 630 | // Ensure that progress is properly updated in the future, |
| 631 | // the call to show() does not guarantee this because it is a |
| 632 | // no-op if we are already showing. |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 633 | post(mShowProgress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | } |
| 635 | }; |
| 636 | |
| 637 | @Override |
| 638 | public void setEnabled(boolean enabled) { |
| 639 | if (mPauseButton != null) { |
| 640 | mPauseButton.setEnabled(enabled); |
| 641 | } |
| 642 | if (mFfwdButton != null) { |
| 643 | mFfwdButton.setEnabled(enabled); |
| 644 | } |
| 645 | if (mRewButton != null) { |
| 646 | mRewButton.setEnabled(enabled); |
| 647 | } |
| 648 | if (mNextButton != null) { |
| 649 | mNextButton.setEnabled(enabled && mNextListener != null); |
| 650 | } |
| 651 | if (mPrevButton != null) { |
| 652 | mPrevButton.setEnabled(enabled && mPrevListener != null); |
| 653 | } |
| 654 | if (mProgress != null) { |
| 655 | mProgress.setEnabled(enabled); |
| 656 | } |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 657 | disableUnsupportedButtons(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | super.setEnabled(enabled); |
| 659 | } |
| 660 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 661 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 662 | public CharSequence getAccessibilityClassName() { |
| 663 | return MediaController.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 666 | @UnsupportedAppUsage |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 667 | private final View.OnClickListener mRewListener = new View.OnClickListener() { |
| 668 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | public void onClick(View v) { |
| 670 | int pos = mPlayer.getCurrentPosition(); |
| 671 | pos -= 5000; // milliseconds |
| 672 | mPlayer.seekTo(pos); |
| 673 | setProgress(); |
| 674 | |
| 675 | show(sDefaultTimeout); |
| 676 | } |
| 677 | }; |
| 678 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 679 | @UnsupportedAppUsage |
Jae Seo | 63d37d9 | 2015-06-02 16:06:52 -0700 | [diff] [blame] | 680 | private final View.OnClickListener mFfwdListener = new View.OnClickListener() { |
| 681 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | public void onClick(View v) { |
| 683 | int pos = mPlayer.getCurrentPosition(); |
| 684 | pos += 15000; // milliseconds |
| 685 | mPlayer.seekTo(pos); |
| 686 | setProgress(); |
| 687 | |
| 688 | show(sDefaultTimeout); |
| 689 | } |
| 690 | }; |
| 691 | |
| 692 | private void installPrevNextListeners() { |
| 693 | if (mNextButton != null) { |
| 694 | mNextButton.setOnClickListener(mNextListener); |
| 695 | mNextButton.setEnabled(mNextListener != null); |
| 696 | } |
| 697 | |
| 698 | if (mPrevButton != null) { |
| 699 | mPrevButton.setOnClickListener(mPrevListener); |
| 700 | mPrevButton.setEnabled(mPrevListener != null); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | public void setPrevNextListeners(View.OnClickListener next, View.OnClickListener prev) { |
| 705 | mNextListener = next; |
| 706 | mPrevListener = prev; |
| 707 | mListenersSet = true; |
| 708 | |
| 709 | if (mRoot != null) { |
| 710 | installPrevNextListeners(); |
RoboErik | 94ca536 | 2014-08-05 18:16:43 -0700 | [diff] [blame] | 711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | if (mNextButton != null && !mFromXml) { |
| 713 | mNextButton.setVisibility(View.VISIBLE); |
| 714 | } |
| 715 | if (mPrevButton != null && !mFromXml) { |
| 716 | mPrevButton.setVisibility(View.VISIBLE); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | public interface MediaPlayerControl { |
| 722 | void start(); |
| 723 | void pause(); |
| 724 | int getDuration(); |
| 725 | int getCurrentPosition(); |
| 726 | void seekTo(int pos); |
| 727 | boolean isPlaying(); |
| 728 | int getBufferPercentage(); |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 729 | boolean canPause(); |
| 730 | boolean canSeekBackward(); |
| 731 | boolean canSeekForward(); |
Marco Nelissen | 13bfebd | 2013-05-09 15:36:53 -0700 | [diff] [blame] | 732 | |
| 733 | /** |
| 734 | * Get the audio session id for the player used by this VideoView. This can be used to |
| 735 | * apply audio effects to the audio track of a video. |
| 736 | * @return The audio session, or 0 if there was an error. |
| 737 | */ |
| 738 | int getAudioSessionId(); |
Marco Nelissen | c818b14 | 2009-08-19 08:32:21 -0700 | [diff] [blame] | 739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | } |