@@ -75,6 +75,7 @@ public class MediaController extends FrameLayout {
7575 private WindowManager mWindowManager ;
7676 private Window mWindow ;
7777 private View mDecor ;
78+ private WindowManager .LayoutParams mDecorLayoutParams ;
7879 private ProgressBar mProgress ;
7980 private TextView mEndTime , mCurrentTime ;
8081 private boolean mShowing ;
@@ -120,6 +121,7 @@ public MediaController(Context context) {
120121 mContext = context ;
121122 mUseFastForward = true ;
122123 initFloatingWindow ();
124+ initFloatingWindowLayout ();
123125 }
124126
125127 private void initFloatingWindow () {
@@ -142,6 +144,48 @@ private void initFloatingWindow() {
142144 requestFocus ();
143145 }
144146
147+ // Allocate and initialize the static parts of mDecorLayoutParams. Must
148+ // also call updateFloatingWindowLayout() to fill in the dynamic parts
149+ // (y and width) before mDecorLayoutParams can be used.
150+ private void initFloatingWindowLayout () {
151+ mDecorLayoutParams = new WindowManager .LayoutParams ();
152+ WindowManager .LayoutParams p = mDecorLayoutParams ;
153+ p .gravity = Gravity .TOP ;
154+ p .height = LayoutParams .WRAP_CONTENT ;
155+ p .x = 0 ;
156+ p .format = PixelFormat .TRANSLUCENT ;
157+ p .type = WindowManager .LayoutParams .TYPE_APPLICATION_PANEL ;
158+ p .flags |= WindowManager .LayoutParams .FLAG_ALT_FOCUSABLE_IM
159+ | WindowManager .LayoutParams .FLAG_NOT_TOUCH_MODAL
160+ | WindowManager .LayoutParams .FLAG_SPLIT_TOUCH ;
161+ p .token = null ;
162+ p .windowAnimations = 0 ; // android.R.style.DropDownAnimationDown;
163+ }
164+
165+ // Update the dynamic parts of mDecorLayoutParams
166+ // Must be called with mAnchor != NULL.
167+ private void updateFloatingWindowLayout () {
168+ int [] anchorPos = new int [2 ];
169+ mAnchor .getLocationOnScreen (anchorPos );
170+
171+ WindowManager .LayoutParams p = mDecorLayoutParams ;
172+ p .width = mAnchor .getWidth ();
173+ p .y = anchorPos [1 ] + mAnchor .getHeight ();
174+ }
175+
176+ // This is called whenever mAnchor's layout bound changes
177+ private OnLayoutChangeListener mLayoutChangeListener =
178+ new OnLayoutChangeListener () {
179+ public void onLayoutChange (View v , int left , int top , int right ,
180+ int bottom , int oldLeft , int oldTop , int oldRight ,
181+ int oldBottom ) {
182+ updateFloatingWindowLayout ();
183+ if (mShowing ) {
184+ mWindowManager .updateViewLayout (mDecor , mDecorLayoutParams );
185+ }
186+ }
187+ };
188+
145189 private OnTouchListener mTouchListener = new OnTouchListener () {
146190 public boolean onTouch (View v , MotionEvent event ) {
147191 if (event .getAction () == MotionEvent .ACTION_DOWN ) {
@@ -164,7 +208,13 @@ public void setMediaPlayer(MediaPlayerControl player) {
164208 * @param view The view to which to anchor the controller when it is visible.
165209 */
166210 public void setAnchorView (View view ) {
211+ if (mAnchor != null ) {
212+ mAnchor .removeOnLayoutChangeListener (mLayoutChangeListener );
213+ }
167214 mAnchor = view ;
215+ if (mAnchor != null ) {
216+ mAnchor .addOnLayoutChangeListener (mLayoutChangeListener );
217+ }
168218
169219 FrameLayout .LayoutParams frameParams = new FrameLayout .LayoutParams (
170220 ViewGroup .LayoutParams .MATCH_PARENT ,
@@ -279,31 +329,14 @@ private void disableUnsupportedButtons() {
279329 * the controller until hide() is called.
280330 */
281331 public void show (int timeout ) {
282-
283332 if (!mShowing && mAnchor != null ) {
284333 setProgress ();
285334 if (mPauseButton != null ) {
286335 mPauseButton .requestFocus ();
287336 }
288337 disableUnsupportedButtons ();
289-
290- int [] anchorpos = new int [2 ];
291- mAnchor .getLocationOnScreen (anchorpos );
292-
293- WindowManager .LayoutParams p = new WindowManager .LayoutParams ();
294- p .gravity = Gravity .TOP ;
295- p .width = mAnchor .getWidth ();
296- p .height = LayoutParams .WRAP_CONTENT ;
297- p .x = 0 ;
298- p .y = anchorpos [1 ] + mAnchor .getHeight () - p .height ;
299- p .format = PixelFormat .TRANSLUCENT ;
300- p .type = WindowManager .LayoutParams .TYPE_APPLICATION_PANEL ;
301- p .flags |= WindowManager .LayoutParams .FLAG_ALT_FOCUSABLE_IM
302- | WindowManager .LayoutParams .FLAG_NOT_TOUCH_MODAL
303- | WindowManager .LayoutParams .FLAG_SPLIT_TOUCH ;
304- p .token = null ;
305- p .windowAnimations = 0 ; // android.R.style.DropDownAnimationDown;
306- mWindowManager .addView (mDecor , p );
338+ updateFloatingWindowLayout ();
339+ mWindowManager .addView (mDecor , mDecorLayoutParams );
307340 mShowing = true ;
308341 }
309342 updatePausePlay ();
0 commit comments