PopupWindow.java revision 81f08086b44a117097960195d2c9072e29644962
1/* 2 * Copyright (C) 2007 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 19import com.android.internal.R; 20 21import android.content.Context; 22import android.content.res.Resources; 23import android.content.res.TypedArray; 24import android.graphics.PixelFormat; 25import android.graphics.Rect; 26import android.graphics.drawable.Drawable; 27import android.graphics.drawable.StateListDrawable; 28import android.os.Build; 29import android.os.IBinder; 30import android.util.AttributeSet; 31import android.view.Gravity; 32import android.view.KeyEvent; 33import android.view.MotionEvent; 34import android.view.View; 35import android.view.View.OnTouchListener; 36import android.view.ViewGroup; 37import android.view.ViewTreeObserver; 38import android.view.ViewTreeObserver.OnScrollChangedListener; 39import android.view.WindowManager; 40 41import java.lang.ref.WeakReference; 42 43/** 44 * <p>A popup window that can be used to display an arbitrary view. The popup 45 * windows is a floating container that appears on top of the current 46 * activity.</p> 47 * 48 * @see android.widget.AutoCompleteTextView 49 * @see android.widget.Spinner 50 */ 51public class PopupWindow { 52 /** 53 * Mode for {@link #setInputMethodMode(int)}: the requirements for the 54 * input method should be based on the focusability of the popup. That is 55 * if it is focusable than it needs to work with the input method, else 56 * it doesn't. 57 */ 58 public static final int INPUT_METHOD_FROM_FOCUSABLE = 0; 59 60 /** 61 * Mode for {@link #setInputMethodMode(int)}: this popup always needs to 62 * work with an input method, regardless of whether it is focusable. This 63 * means that it will always be displayed so that the user can also operate 64 * the input method while it is shown. 65 */ 66 public static final int INPUT_METHOD_NEEDED = 1; 67 68 /** 69 * Mode for {@link #setInputMethodMode(int)}: this popup never needs to 70 * work with an input method, regardless of whether it is focusable. This 71 * means that it will always be displayed to use as much space on the 72 * screen as needed, regardless of whether this covers the input method. 73 */ 74 public static final int INPUT_METHOD_NOT_NEEDED = 2; 75 76 private Context mContext; 77 private WindowManager mWindowManager; 78 79 private boolean mIsShowing; 80 private boolean mIsDropdown; 81 82 private View mContentView; 83 private View mPopupView; 84 private boolean mFocusable; 85 private int mInputMethodMode = INPUT_METHOD_FROM_FOCUSABLE; 86 private int mSoftInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED; 87 private boolean mTouchable = true; 88 private boolean mOutsideTouchable = false; 89 private boolean mClippingEnabled = true; 90 private int mSplitTouchEnabled = -1; 91 private boolean mLayoutInScreen; 92 private boolean mClipToScreen; 93 private boolean mAllowScrollingAnchorParent = true; 94 95 private OnTouchListener mTouchInterceptor; 96 97 private int mWidthMode; 98 private int mWidth; 99 private int mLastWidth; 100 private int mHeightMode; 101 private int mHeight; 102 private int mLastHeight; 103 104 private int mPopupWidth; 105 private int mPopupHeight; 106 107 private int[] mDrawingLocation = new int[2]; 108 private int[] mScreenLocation = new int[2]; 109 private Rect mTempRect = new Rect(); 110 111 private Drawable mBackground; 112 private Drawable mAboveAnchorBackgroundDrawable; 113 private Drawable mBelowAnchorBackgroundDrawable; 114 115 private boolean mAboveAnchor; 116 private int mWindowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; 117 118 private OnDismissListener mOnDismissListener; 119 private boolean mIgnoreCheekPress = false; 120 121 private int mAnimationStyle = -1; 122 123 private static final int[] ABOVE_ANCHOR_STATE_SET = new int[] { 124 com.android.internal.R.attr.state_above_anchor 125 }; 126 127 private WeakReference<View> mAnchor; 128 private OnScrollChangedListener mOnScrollChangedListener = 129 new OnScrollChangedListener() { 130 public void onScrollChanged() { 131 View anchor = mAnchor != null ? mAnchor.get() : null; 132 if (anchor != null && mPopupView != null) { 133 WindowManager.LayoutParams p = (WindowManager.LayoutParams) 134 mPopupView.getLayoutParams(); 135 136 updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff)); 137 update(p.x, p.y, -1, -1, true); 138 } 139 } 140 }; 141 private int mAnchorXoff, mAnchorYoff; 142 143 /** 144 * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> 145 * 146 * <p>The popup does provide a background.</p> 147 */ 148 public PopupWindow(Context context) { 149 this(context, null); 150 } 151 152 /** 153 * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> 154 * 155 * <p>The popup does provide a background.</p> 156 */ 157 public PopupWindow(Context context, AttributeSet attrs) { 158 this(context, attrs, com.android.internal.R.attr.popupWindowStyle); 159 } 160 161 /** 162 * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> 163 * 164 * <p>The popup does provide a background.</p> 165 */ 166 public PopupWindow(Context context, AttributeSet attrs, int defStyle) { 167 this(context, attrs, defStyle, 0); 168 } 169 170 /** 171 * <p>Create a new, empty, non focusable popup window of dimension (0,0).</p> 172 * 173 * <p>The popup does not provide a background.</p> 174 */ 175 public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 176 mContext = context; 177 mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 178 179 TypedArray a = 180 context.obtainStyledAttributes( 181 attrs, com.android.internal.R.styleable.PopupWindow, defStyleAttr, defStyleRes); 182 183 mBackground = a.getDrawable(R.styleable.PopupWindow_popupBackground); 184 185 final int animStyle = a.getResourceId(R.styleable.PopupWindow_popupAnimationStyle, -1); 186 mAnimationStyle = animStyle == com.android.internal.R.style.Animation_PopupWindow ? -1 : 187 animStyle; 188 189 // If this is a StateListDrawable, try to find and store the drawable to be 190 // used when the drop-down is placed above its anchor view, and the one to be 191 // used when the drop-down is placed below its anchor view. We extract 192 // the drawables ourselves to work around a problem with using refreshDrawableState 193 // that it will take into account the padding of all drawables specified in a 194 // StateListDrawable, thus adding superfluous padding to drop-down views. 195 // 196 // We assume a StateListDrawable will have a drawable for ABOVE_ANCHOR_STATE_SET and 197 // at least one other drawable, intended for the 'below-anchor state'. 198 if (mBackground instanceof StateListDrawable) { 199 StateListDrawable background = (StateListDrawable) mBackground; 200 201 // Find the above-anchor view - this one's easy, it should be labeled as such. 202 int aboveAnchorStateIndex = background.getStateDrawableIndex(ABOVE_ANCHOR_STATE_SET); 203 204 // Now, for the below-anchor view, look for any other drawable specified in the 205 // StateListDrawable which is not for the above-anchor state and use that. 206 int count = background.getStateCount(); 207 int belowAnchorStateIndex = -1; 208 for (int i = 0; i < count; i++) { 209 if (i != aboveAnchorStateIndex) { 210 belowAnchorStateIndex = i; 211 break; 212 } 213 } 214 215 // Store the drawables we found, if we found them. Otherwise, set them both 216 // to null so that we'll just use refreshDrawableState. 217 if (aboveAnchorStateIndex != -1 && belowAnchorStateIndex != -1) { 218 mAboveAnchorBackgroundDrawable = background.getStateDrawable(aboveAnchorStateIndex); 219 mBelowAnchorBackgroundDrawable = background.getStateDrawable(belowAnchorStateIndex); 220 } else { 221 mBelowAnchorBackgroundDrawable = null; 222 mAboveAnchorBackgroundDrawable = null; 223 } 224 } 225 226 a.recycle(); 227 } 228 229 /** 230 * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> 231 * 232 * <p>The popup does not provide any background. This should be handled 233 * by the content view.</p> 234 */ 235 public PopupWindow() { 236 this(null, 0, 0); 237 } 238 239 /** 240 * <p>Create a new non focusable popup window which can display the 241 * <tt>contentView</tt>. The dimension of the window are (0,0).</p> 242 * 243 * <p>The popup does not provide any background. This should be handled 244 * by the content view.</p> 245 * 246 * @param contentView the popup's content 247 */ 248 public PopupWindow(View contentView) { 249 this(contentView, 0, 0); 250 } 251 252 /** 253 * <p>Create a new empty, non focusable popup window. The dimension of the 254 * window must be passed to this constructor.</p> 255 * 256 * <p>The popup does not provide any background. This should be handled 257 * by the content view.</p> 258 * 259 * @param width the popup's width 260 * @param height the popup's height 261 */ 262 public PopupWindow(int width, int height) { 263 this(null, width, height); 264 } 265 266 /** 267 * <p>Create a new non focusable popup window which can display the 268 * <tt>contentView</tt>. The dimension of the window must be passed to 269 * this constructor.</p> 270 * 271 * <p>The popup does not provide any background. This should be handled 272 * by the content view.</p> 273 * 274 * @param contentView the popup's content 275 * @param width the popup's width 276 * @param height the popup's height 277 */ 278 public PopupWindow(View contentView, int width, int height) { 279 this(contentView, width, height, false); 280 } 281 282 /** 283 * <p>Create a new popup window which can display the <tt>contentView</tt>. 284 * The dimension of the window must be passed to this constructor.</p> 285 * 286 * <p>The popup does not provide any background. This should be handled 287 * by the content view.</p> 288 * 289 * @param contentView the popup's content 290 * @param width the popup's width 291 * @param height the popup's height 292 * @param focusable true if the popup can be focused, false otherwise 293 */ 294 public PopupWindow(View contentView, int width, int height, boolean focusable) { 295 if (contentView != null) { 296 mContext = contentView.getContext(); 297 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 298 } 299 setContentView(contentView); 300 setWidth(width); 301 setHeight(height); 302 setFocusable(focusable); 303 } 304 305 /** 306 * <p>Return the drawable used as the popup window's background.</p> 307 * 308 * @return the background drawable or null 309 */ 310 public Drawable getBackground() { 311 return mBackground; 312 } 313 314 /** 315 * <p>Change the background drawable for this popup window. The background 316 * can be set to null.</p> 317 * 318 * @param background the popup's background 319 */ 320 public void setBackgroundDrawable(Drawable background) { 321 mBackground = background; 322 } 323 324 /** 325 * <p>Return the animation style to use the popup appears and disappears</p> 326 * 327 * @return the animation style to use the popup appears and disappears 328 */ 329 public int getAnimationStyle() { 330 return mAnimationStyle; 331 } 332 333 /** 334 * Set the flag on popup to ignore cheek press eventt; by default this flag 335 * is set to false 336 * which means the pop wont ignore cheek press dispatch events. 337 * 338 * <p>If the popup is showing, calling this method will take effect only 339 * the next time the popup is shown or through a manual call to one of 340 * the {@link #update()} methods.</p> 341 * 342 * @see #update() 343 */ 344 public void setIgnoreCheekPress() { 345 mIgnoreCheekPress = true; 346 } 347 348 349 /** 350 * <p>Change the animation style resource for this popup.</p> 351 * 352 * <p>If the popup is showing, calling this method will take effect only 353 * the next time the popup is shown or through a manual call to one of 354 * the {@link #update()} methods.</p> 355 * 356 * @param animationStyle animation style to use when the popup appears 357 * and disappears. Set to -1 for the default animation, 0 for no 358 * animation, or a resource identifier for an explicit animation. 359 * 360 * @see #update() 361 */ 362 public void setAnimationStyle(int animationStyle) { 363 mAnimationStyle = animationStyle; 364 } 365 366 /** 367 * <p>Return the view used as the content of the popup window.</p> 368 * 369 * @return a {@link android.view.View} representing the popup's content 370 * 371 * @see #setContentView(android.view.View) 372 */ 373 public View getContentView() { 374 return mContentView; 375 } 376 377 /** 378 * <p>Change the popup's content. The content is represented by an instance 379 * of {@link android.view.View}.</p> 380 * 381 * <p>This method has no effect if called when the popup is showing.</p> 382 * 383 * @param contentView the new content for the popup 384 * 385 * @see #getContentView() 386 * @see #isShowing() 387 */ 388 public void setContentView(View contentView) { 389 if (isShowing()) { 390 return; 391 } 392 393 mContentView = contentView; 394 395 if (mContext == null) { 396 mContext = mContentView.getContext(); 397 } 398 399 if (mWindowManager == null) { 400 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 401 } 402 } 403 404 /** 405 * Set a callback for all touch events being dispatched to the popup 406 * window. 407 */ 408 public void setTouchInterceptor(OnTouchListener l) { 409 mTouchInterceptor = l; 410 } 411 412 /** 413 * <p>Indicate whether the popup window can grab the focus.</p> 414 * 415 * @return true if the popup is focusable, false otherwise 416 * 417 * @see #setFocusable(boolean) 418 */ 419 public boolean isFocusable() { 420 return mFocusable; 421 } 422 423 /** 424 * <p>Changes the focusability of the popup window. When focusable, the 425 * window will grab the focus from the current focused widget if the popup 426 * contains a focusable {@link android.view.View}. By default a popup 427 * window is not focusable.</p> 428 * 429 * <p>If the popup is showing, calling this method will take effect only 430 * the next time the popup is shown or through a manual call to one of 431 * the {@link #update()} methods.</p> 432 * 433 * @param focusable true if the popup should grab focus, false otherwise. 434 * 435 * @see #isFocusable() 436 * @see #isShowing() 437 * @see #update() 438 */ 439 public void setFocusable(boolean focusable) { 440 mFocusable = focusable; 441 } 442 443 /** 444 * Return the current value in {@link #setInputMethodMode(int)}. 445 * 446 * @see #setInputMethodMode(int) 447 */ 448 public int getInputMethodMode() { 449 return mInputMethodMode; 450 451 } 452 453 /** 454 * Control how the popup operates with an input method: one of 455 * {@link #INPUT_METHOD_FROM_FOCUSABLE}, {@link #INPUT_METHOD_NEEDED}, 456 * or {@link #INPUT_METHOD_NOT_NEEDED}. 457 * 458 * <p>If the popup is showing, calling this method will take effect only 459 * the next time the popup is shown or through a manual call to one of 460 * the {@link #update()} methods.</p> 461 * 462 * @see #getInputMethodMode() 463 * @see #update() 464 */ 465 public void setInputMethodMode(int mode) { 466 mInputMethodMode = mode; 467 } 468 469 /** 470 * Sets the operating mode for the soft input area. 471 * 472 * @param mode The desired mode, see 473 * {@link android.view.WindowManager.LayoutParams#softInputMode} 474 * for the full list 475 * 476 * @see android.view.WindowManager.LayoutParams#softInputMode 477 * @see #getSoftInputMode() 478 */ 479 public void setSoftInputMode(int mode) { 480 mSoftInputMode = mode; 481 } 482 483 /** 484 * Returns the current value in {@link #setSoftInputMode(int)}. 485 * 486 * @see #setSoftInputMode(int) 487 * @see android.view.WindowManager.LayoutParams#softInputMode 488 */ 489 public int getSoftInputMode() { 490 return mSoftInputMode; 491 } 492 493 /** 494 * <p>Indicates whether the popup window receives touch events.</p> 495 * 496 * @return true if the popup is touchable, false otherwise 497 * 498 * @see #setTouchable(boolean) 499 */ 500 public boolean isTouchable() { 501 return mTouchable; 502 } 503 504 /** 505 * <p>Changes the touchability of the popup window. When touchable, the 506 * window will receive touch events, otherwise touch events will go to the 507 * window below it. By default the window is touchable.</p> 508 * 509 * <p>If the popup is showing, calling this method will take effect only 510 * the next time the popup is shown or through a manual call to one of 511 * the {@link #update()} methods.</p> 512 * 513 * @param touchable true if the popup should receive touch events, false otherwise 514 * 515 * @see #isTouchable() 516 * @see #isShowing() 517 * @see #update() 518 */ 519 public void setTouchable(boolean touchable) { 520 mTouchable = touchable; 521 } 522 523 /** 524 * <p>Indicates whether the popup window will be informed of touch events 525 * outside of its window.</p> 526 * 527 * @return true if the popup is outside touchable, false otherwise 528 * 529 * @see #setOutsideTouchable(boolean) 530 */ 531 public boolean isOutsideTouchable() { 532 return mOutsideTouchable; 533 } 534 535 /** 536 * <p>Controls whether the pop-up will be informed of touch events outside 537 * of its window. This only makes sense for pop-ups that are touchable 538 * but not focusable, which means touches outside of the window will 539 * be delivered to the window behind. The default is false.</p> 540 * 541 * <p>If the popup is showing, calling this method will take effect only 542 * the next time the popup is shown or through a manual call to one of 543 * the {@link #update()} methods.</p> 544 * 545 * @param touchable true if the popup should receive outside 546 * touch events, false otherwise 547 * 548 * @see #isOutsideTouchable() 549 * @see #isShowing() 550 * @see #update() 551 */ 552 public void setOutsideTouchable(boolean touchable) { 553 mOutsideTouchable = touchable; 554 } 555 556 /** 557 * <p>Indicates whether clipping of the popup window is enabled.</p> 558 * 559 * @return true if the clipping is enabled, false otherwise 560 * 561 * @see #setClippingEnabled(boolean) 562 */ 563 public boolean isClippingEnabled() { 564 return mClippingEnabled; 565 } 566 567 /** 568 * <p>Allows the popup window to extend beyond the bounds of the screen. By default the 569 * window is clipped to the screen boundaries. Setting this to false will allow windows to be 570 * accurately positioned.</p> 571 * 572 * <p>If the popup is showing, calling this method will take effect only 573 * the next time the popup is shown or through a manual call to one of 574 * the {@link #update()} methods.</p> 575 * 576 * @param enabled false if the window should be allowed to extend outside of the screen 577 * @see #isShowing() 578 * @see #isClippingEnabled() 579 * @see #update() 580 */ 581 public void setClippingEnabled(boolean enabled) { 582 mClippingEnabled = enabled; 583 } 584 585 /** 586 * Clip this popup window to the screen, but not to the containing window. 587 * 588 * @param enabled True to clip to the screen. 589 * @hide 590 */ 591 public void setClipToScreenEnabled(boolean enabled) { 592 mClipToScreen = enabled; 593 setClippingEnabled(!enabled); 594 } 595 596 /** 597 * Allow PopupWindow to scroll the anchor's parent to provide more room 598 * for the popup. Enabled by default. 599 * 600 * @param enabled True to scroll the anchor's parent when more room is desired by the popup. 601 */ 602 void setAllowScrollingAnchorParent(boolean enabled) { 603 mAllowScrollingAnchorParent = enabled; 604 } 605 606 /** 607 * <p>Indicates whether the popup window supports splitting touches.</p> 608 * 609 * @return true if the touch splitting is enabled, false otherwise 610 * 611 * @see #setSplitTouchEnabled(boolean) 612 */ 613 public boolean isSplitTouchEnabled() { 614 if (mSplitTouchEnabled < 0 && mContext != null) { 615 return mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB; 616 } 617 return mSplitTouchEnabled == 1; 618 } 619 620 /** 621 * <p>Allows the popup window to split touches across other windows that also 622 * support split touch. When this flag is false, the first pointer 623 * that goes down determines the window to which all subsequent touches 624 * go until all pointers go up. When this flag is true, each pointer 625 * (not necessarily the first) that goes down determines the window 626 * to which all subsequent touches of that pointer will go until that 627 * pointer goes up thereby enabling touches with multiple pointers 628 * to be split across multiple windows.</p> 629 * 630 * @param enabled true if the split touches should be enabled, false otherwise 631 * @see #isSplitTouchEnabled() 632 */ 633 public void setSplitTouchEnabled(boolean enabled) { 634 mSplitTouchEnabled = enabled ? 1 : 0; 635 } 636 637 /** 638 * <p>Indicates whether the popup window will be forced into using absolute screen coordinates 639 * for positioning.</p> 640 * 641 * @return true if the window will always be positioned in screen coordinates. 642 * @hide 643 */ 644 public boolean isLayoutInScreenEnabled() { 645 return mLayoutInScreen; 646 } 647 648 /** 649 * <p>Allows the popup window to force the flag 650 * {@link WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN}, overriding default behavior. 651 * This will cause the popup to be positioned in absolute screen coordinates.</p> 652 * 653 * @param enabled true if the popup should always be positioned in screen coordinates 654 * @hide 655 */ 656 public void setLayoutInScreenEnabled(boolean enabled) { 657 mLayoutInScreen = enabled; 658 } 659 660 /** 661 * Set the layout type for this window. Should be one of the TYPE constants defined in 662 * {@link WindowManager.LayoutParams}. 663 * 664 * @param layoutType Layout type for this window. 665 * @hide 666 */ 667 public void setWindowLayoutType(int layoutType) { 668 mWindowLayoutType = layoutType; 669 } 670 671 /** 672 * @return The layout type for this window. 673 * @hide 674 */ 675 public int getWindowLayoutType() { 676 return mWindowLayoutType; 677 } 678 679 /** 680 * <p>Change the width and height measure specs that are given to the 681 * window manager by the popup. By default these are 0, meaning that 682 * the current width or height is requested as an explicit size from 683 * the window manager. You can supply 684 * {@link ViewGroup.LayoutParams#WRAP_CONTENT} or 685 * {@link ViewGroup.LayoutParams#MATCH_PARENT} to have that measure 686 * spec supplied instead, replacing the absolute width and height that 687 * has been set in the popup.</p> 688 * 689 * <p>If the popup is showing, calling this method will take effect only 690 * the next time the popup is shown.</p> 691 * 692 * @param widthSpec an explicit width measure spec mode, either 693 * {@link ViewGroup.LayoutParams#WRAP_CONTENT}, 694 * {@link ViewGroup.LayoutParams#MATCH_PARENT}, or 0 to use the absolute 695 * width. 696 * @param heightSpec an explicit height measure spec mode, either 697 * {@link ViewGroup.LayoutParams#WRAP_CONTENT}, 698 * {@link ViewGroup.LayoutParams#MATCH_PARENT}, or 0 to use the absolute 699 * height. 700 */ 701 public void setWindowLayoutMode(int widthSpec, int heightSpec) { 702 mWidthMode = widthSpec; 703 mHeightMode = heightSpec; 704 } 705 706 /** 707 * <p>Return this popup's height MeasureSpec</p> 708 * 709 * @return the height MeasureSpec of the popup 710 * 711 * @see #setHeight(int) 712 */ 713 public int getHeight() { 714 return mHeight; 715 } 716 717 /** 718 * <p>Change the popup's height MeasureSpec</p> 719 * 720 * <p>If the popup is showing, calling this method will take effect only 721 * the next time the popup is shown.</p> 722 * 723 * @param height the height MeasureSpec of the popup 724 * 725 * @see #getHeight() 726 * @see #isShowing() 727 */ 728 public void setHeight(int height) { 729 mHeight = height; 730 } 731 732 /** 733 * <p>Return this popup's width MeasureSpec</p> 734 * 735 * @return the width MeasureSpec of the popup 736 * 737 * @see #setWidth(int) 738 */ 739 public int getWidth() { 740 return mWidth; 741 } 742 743 /** 744 * <p>Change the popup's width MeasureSpec</p> 745 * 746 * <p>If the popup is showing, calling this method will take effect only 747 * the next time the popup is shown.</p> 748 * 749 * @param width the width MeasureSpec of the popup 750 * 751 * @see #getWidth() 752 * @see #isShowing() 753 */ 754 public void setWidth(int width) { 755 mWidth = width; 756 } 757 758 /** 759 * <p>Indicate whether this popup window is showing on screen.</p> 760 * 761 * @return true if the popup is showing, false otherwise 762 */ 763 public boolean isShowing() { 764 return mIsShowing; 765 } 766 767 /** 768 * <p> 769 * Display the content view in a popup window at the specified location. If the popup window 770 * cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams} 771 * for more information on how gravity and the x and y parameters are related. Specifying 772 * a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying 773 * <code>Gravity.LEFT | Gravity.TOP</code>. 774 * </p> 775 * 776 * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from 777 * @param gravity the gravity which controls the placement of the popup window 778 * @param x the popup's x location offset 779 * @param y the popup's y location offset 780 */ 781 public void showAtLocation(View parent, int gravity, int x, int y) { 782 if (isShowing() || mContentView == null) { 783 return; 784 } 785 786 unregisterForScrollChanged(); 787 788 mIsShowing = true; 789 mIsDropdown = false; 790 791 WindowManager.LayoutParams p = createPopupLayout(parent.getWindowToken()); 792 p.windowAnimations = computeAnimationResource(); 793 794 preparePopup(p); 795 if (gravity == Gravity.NO_GRAVITY) { 796 gravity = Gravity.TOP | Gravity.LEFT; 797 } 798 p.gravity = gravity; 799 p.x = x; 800 p.y = y; 801 if (mHeightMode < 0) p.height = mLastHeight = mHeightMode; 802 if (mWidthMode < 0) p.width = mLastWidth = mWidthMode; 803 invokePopup(p); 804 } 805 806 /** 807 * <p>Display the content view in a popup window anchored to the bottom-left 808 * corner of the anchor view. If there is not enough room on screen to show 809 * the popup in its entirety, this method tries to find a parent scroll 810 * view to scroll. If no parent scroll view can be scrolled, the bottom-left 811 * corner of the popup is pinned at the top left corner of the anchor view.</p> 812 * 813 * @param anchor the view on which to pin the popup window 814 * 815 * @see #dismiss() 816 */ 817 public void showAsDropDown(View anchor) { 818 showAsDropDown(anchor, 0, 0); 819 } 820 821 /** 822 * <p>Display the content view in a popup window anchored to the bottom-left 823 * corner of the anchor view offset by the specified x and y coordinates. 824 * If there is not enough room on screen to show 825 * the popup in its entirety, this method tries to find a parent scroll 826 * view to scroll. If no parent scroll view can be scrolled, the bottom-left 827 * corner of the popup is pinned at the top left corner of the anchor view.</p> 828 * <p>If the view later scrolls to move <code>anchor</code> to a different 829 * location, the popup will be moved correspondingly.</p> 830 * 831 * @param anchor the view on which to pin the popup window 832 * 833 * @see #dismiss() 834 */ 835 public void showAsDropDown(View anchor, int xoff, int yoff) { 836 if (isShowing() || mContentView == null) { 837 return; 838 } 839 840 registerForScrollChanged(anchor, xoff, yoff); 841 842 mIsShowing = true; 843 mIsDropdown = true; 844 845 WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken()); 846 preparePopup(p); 847 848 updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff)); 849 850 if (mHeightMode < 0) p.height = mLastHeight = mHeightMode; 851 if (mWidthMode < 0) p.width = mLastWidth = mWidthMode; 852 853 p.windowAnimations = computeAnimationResource(); 854 855 invokePopup(p); 856 } 857 858 private void updateAboveAnchor(boolean aboveAnchor) { 859 if (aboveAnchor != mAboveAnchor) { 860 mAboveAnchor = aboveAnchor; 861 862 if (mBackground != null) { 863 // If the background drawable provided was a StateListDrawable with above-anchor 864 // and below-anchor states, use those. Otherwise rely on refreshDrawableState to 865 // do the job. 866 if (mAboveAnchorBackgroundDrawable != null) { 867 if (mAboveAnchor) { 868 mPopupView.setBackgroundDrawable(mAboveAnchorBackgroundDrawable); 869 } else { 870 mPopupView.setBackgroundDrawable(mBelowAnchorBackgroundDrawable); 871 } 872 } else { 873 mPopupView.refreshDrawableState(); 874 } 875 } 876 } 877 } 878 879 /** 880 * Indicates whether the popup is showing above (the y coordinate of the popup's bottom 881 * is less than the y coordinate of the anchor) or below the anchor view (the y coordinate 882 * of the popup is greater than y coordinate of the anchor's bottom). 883 * 884 * The value returned 885 * by this method is meaningful only after {@link #showAsDropDown(android.view.View)} 886 * or {@link #showAsDropDown(android.view.View, int, int)} was invoked. 887 * 888 * @return True if this popup is showing above the anchor view, false otherwise. 889 */ 890 public boolean isAboveAnchor() { 891 return mAboveAnchor; 892 } 893 894 /** 895 * <p>Prepare the popup by embedding in into a new ViewGroup if the 896 * background drawable is not null. If embedding is required, the layout 897 * parameters' height is mnodified to take into account the background's 898 * padding.</p> 899 * 900 * @param p the layout parameters of the popup's content view 901 */ 902 private void preparePopup(WindowManager.LayoutParams p) { 903 if (mContentView == null || mContext == null || mWindowManager == null) { 904 throw new IllegalStateException("You must specify a valid content view by " 905 + "calling setContentView() before attempting to show the popup."); 906 } 907 908 if (mBackground != null) { 909 final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams(); 910 int height = ViewGroup.LayoutParams.MATCH_PARENT; 911 if (layoutParams != null && 912 layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) { 913 height = ViewGroup.LayoutParams.WRAP_CONTENT; 914 } 915 916 // when a background is available, we embed the content view 917 // within another view that owns the background drawable 918 PopupViewContainer popupViewContainer = new PopupViewContainer(mContext); 919 PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams( 920 ViewGroup.LayoutParams.MATCH_PARENT, height 921 ); 922 popupViewContainer.setBackgroundDrawable(mBackground); 923 popupViewContainer.addView(mContentView, listParams); 924 925 mPopupView = popupViewContainer; 926 } else { 927 mPopupView = mContentView; 928 } 929 mPopupWidth = p.width; 930 mPopupHeight = p.height; 931 } 932 933 /** 934 * <p>Invoke the popup window by adding the content view to the window 935 * manager.</p> 936 * 937 * <p>The content view must be non-null when this method is invoked.</p> 938 * 939 * @param p the layout parameters of the popup's content view 940 */ 941 private void invokePopup(WindowManager.LayoutParams p) { 942 p.packageName = mContext.getPackageName(); 943 mWindowManager.addView(mPopupView, p); 944 } 945 946 /** 947 * <p>Generate the layout parameters for the popup window.</p> 948 * 949 * @param token the window token used to bind the popup's window 950 * 951 * @return the layout parameters to pass to the window manager 952 */ 953 private WindowManager.LayoutParams createPopupLayout(IBinder token) { 954 // generates the layout parameters for the drop down 955 // we want a fixed size view located at the bottom left of the anchor 956 WindowManager.LayoutParams p = new WindowManager.LayoutParams(); 957 // these gravity settings put the view at the top left corner of the 958 // screen. The view is then positioned to the appropriate location 959 // by setting the x and y offsets to match the anchor's bottom 960 // left corner 961 p.gravity = Gravity.LEFT | Gravity.TOP; 962 p.width = mLastWidth = mWidth; 963 p.height = mLastHeight = mHeight; 964 if (mBackground != null) { 965 p.format = mBackground.getOpacity(); 966 } else { 967 p.format = PixelFormat.TRANSLUCENT; 968 } 969 p.flags = computeFlags(p.flags); 970 p.type = mWindowLayoutType; 971 p.token = token; 972 p.softInputMode = mSoftInputMode; 973 p.setTitle("PopupWindow:" + Integer.toHexString(hashCode())); 974 975 return p; 976 } 977 978 private int computeFlags(int curFlags) { 979 curFlags &= ~( 980 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES | 981 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 982 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | 983 WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | 984 WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | 985 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | 986 WindowManager.LayoutParams.FLAG_SPLIT_TOUCH); 987 if(mIgnoreCheekPress) { 988 curFlags |= WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES; 989 } 990 if (!mFocusable) { 991 curFlags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 992 if (mInputMethodMode == INPUT_METHOD_NEEDED) { 993 curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 994 } 995 } else if (mInputMethodMode == INPUT_METHOD_NOT_NEEDED) { 996 curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 997 } 998 if (!mTouchable) { 999 curFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; 1000 } 1001 if (mOutsideTouchable) { 1002 curFlags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; 1003 } 1004 if (!mClippingEnabled) { 1005 curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; 1006 } 1007 if (isSplitTouchEnabled()) { 1008 curFlags |= WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; 1009 } 1010 if (mLayoutInScreen) { 1011 curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; 1012 } 1013 return curFlags; 1014 } 1015 1016 private int computeAnimationResource() { 1017 if (mAnimationStyle == -1) { 1018 if (mIsDropdown) { 1019 return mAboveAnchor 1020 ? com.android.internal.R.style.Animation_DropDownUp 1021 : com.android.internal.R.style.Animation_DropDownDown; 1022 } 1023 return 0; 1024 } 1025 return mAnimationStyle; 1026 } 1027 1028 /** 1029 * <p>Positions the popup window on screen. When the popup window is too 1030 * tall to fit under the anchor, a parent scroll view is seeked and scrolled 1031 * up to reclaim space. If scrolling is not possible or not enough, the 1032 * popup window gets moved on top of the anchor.</p> 1033 * 1034 * <p>The height must have been set on the layout parameters prior to 1035 * calling this method.</p> 1036 * 1037 * @param anchor the view on which the popup window must be anchored 1038 * @param p the layout parameters used to display the drop down 1039 * 1040 * @return true if the popup is translated upwards to fit on screen 1041 */ 1042 boolean findDropDownPosition(View anchor, WindowManager.LayoutParams p, 1043 int xoff, int yoff) { 1044 1045 anchor.getLocationInWindow(mDrawingLocation); 1046 p.x = mDrawingLocation[0] + xoff; 1047 p.y = mDrawingLocation[1] + anchor.getHeight() + yoff; 1048 1049 boolean onTop = false; 1050 1051 p.gravity = Gravity.LEFT | Gravity.TOP; 1052 1053 anchor.getLocationOnScreen(mScreenLocation); 1054 final Rect displayFrame = new Rect(); 1055 anchor.getWindowVisibleDisplayFrame(displayFrame); 1056 1057 final View root = anchor.getRootView(); 1058 if (p.y + mPopupHeight > displayFrame.bottom || p.x + mPopupWidth - root.getWidth() > 0) { 1059 // if the drop down disappears at the bottom of the screen. we try to 1060 // scroll a parent scrollview or move the drop down back up on top of 1061 // the edit box 1062 if (mAllowScrollingAnchorParent) { 1063 int scrollX = anchor.getScrollX(); 1064 int scrollY = anchor.getScrollY(); 1065 Rect r = new Rect(scrollX, scrollY, scrollX + mPopupWidth + xoff, 1066 scrollY + mPopupHeight + anchor.getHeight() + yoff); 1067 anchor.requestRectangleOnScreen(r, true); 1068 } 1069 1070 // now we re-evaluate the space available, and decide from that 1071 // whether the pop-up will go above or below the anchor. 1072 anchor.getLocationInWindow(mDrawingLocation); 1073 p.x = mDrawingLocation[0] + xoff; 1074 p.y = mDrawingLocation[1] + anchor.getHeight() + yoff; 1075 1076 // determine whether there is more space above or below the anchor 1077 anchor.getLocationOnScreen(mScreenLocation); 1078 1079 onTop = (displayFrame.bottom - mScreenLocation[1] - anchor.getHeight() - yoff) < 1080 (mScreenLocation[1] - yoff - displayFrame.top); 1081 if (onTop) { 1082 p.gravity = Gravity.LEFT | Gravity.BOTTOM; 1083 p.y = root.getHeight() - mDrawingLocation[1] + yoff; 1084 } else { 1085 p.y = mDrawingLocation[1] + anchor.getHeight() + yoff; 1086 } 1087 } 1088 1089 if (mClipToScreen) { 1090 final int displayFrameWidth = displayFrame.right - displayFrame.left; 1091 1092 int right = p.x + p.width; 1093 if (right > displayFrameWidth) { 1094 p.x -= right - displayFrameWidth; 1095 } 1096 if (p.x < displayFrame.left) { 1097 p.x = displayFrame.left; 1098 p.width = Math.min(p.width, displayFrameWidth); 1099 } 1100 1101 if (onTop) { 1102 int popupTop = mScreenLocation[1] + yoff - mPopupHeight; 1103 if (popupTop < 0) { 1104 p.y += popupTop; 1105 } 1106 } else { 1107 p.y = Math.max(p.y, displayFrame.top); 1108 } 1109 } 1110 1111 p.gravity |= Gravity.DISPLAY_CLIP_VERTICAL; 1112 1113 return onTop; 1114 } 1115 1116 /** 1117 * Returns the maximum height that is available for the popup to be 1118 * completely shown. It is recommended that this height be the maximum for 1119 * the popup's height, otherwise it is possible that the popup will be 1120 * clipped. 1121 * 1122 * @param anchor The view on which the popup window must be anchored. 1123 * @return The maximum available height for the popup to be completely 1124 * shown. 1125 */ 1126 public int getMaxAvailableHeight(View anchor) { 1127 return getMaxAvailableHeight(anchor, 0); 1128 } 1129 1130 /** 1131 * Returns the maximum height that is available for the popup to be 1132 * completely shown. It is recommended that this height be the maximum for 1133 * the popup's height, otherwise it is possible that the popup will be 1134 * clipped. 1135 * 1136 * @param anchor The view on which the popup window must be anchored. 1137 * @param yOffset y offset from the view's bottom edge 1138 * @return The maximum available height for the popup to be completely 1139 * shown. 1140 */ 1141 public int getMaxAvailableHeight(View anchor, int yOffset) { 1142 return getMaxAvailableHeight(anchor, yOffset, false); 1143 } 1144 1145 /** 1146 * Returns the maximum height that is available for the popup to be 1147 * completely shown, optionally ignoring any bottom decorations such as 1148 * the input method. It is recommended that this height be the maximum for 1149 * the popup's height, otherwise it is possible that the popup will be 1150 * clipped. 1151 * 1152 * @param anchor The view on which the popup window must be anchored. 1153 * @param yOffset y offset from the view's bottom edge 1154 * @param ignoreBottomDecorations if true, the height returned will be 1155 * all the way to the bottom of the display, ignoring any 1156 * bottom decorations 1157 * @return The maximum available height for the popup to be completely 1158 * shown. 1159 * 1160 * @hide Pending API council approval. 1161 */ 1162 public int getMaxAvailableHeight(View anchor, int yOffset, boolean ignoreBottomDecorations) { 1163 final Rect displayFrame = new Rect(); 1164 anchor.getWindowVisibleDisplayFrame(displayFrame); 1165 1166 final int[] anchorPos = mDrawingLocation; 1167 anchor.getLocationOnScreen(anchorPos); 1168 1169 int bottomEdge = displayFrame.bottom; 1170 if (ignoreBottomDecorations) { 1171 Resources res = anchor.getContext().getResources(); 1172 bottomEdge = res.getDisplayMetrics().heightPixels - 1173 (int) res.getDimension(com.android.internal.R.dimen.screen_margin_bottom); 1174 } 1175 final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset; 1176 final int distanceToTop = anchorPos[1] - displayFrame.top + yOffset; 1177 1178 // anchorPos[1] is distance from anchor to top of screen 1179 int returnedHeight = Math.max(distanceToBottom, distanceToTop); 1180 if (mBackground != null) { 1181 mBackground.getPadding(mTempRect); 1182 returnedHeight -= mTempRect.top + mTempRect.bottom; 1183 } 1184 1185 return returnedHeight; 1186 } 1187 1188 /** 1189 * <p>Dispose of the popup window. This method can be invoked only after 1190 * {@link #showAsDropDown(android.view.View)} has been executed. Failing that, calling 1191 * this method will have no effect.</p> 1192 * 1193 * @see #showAsDropDown(android.view.View) 1194 */ 1195 public void dismiss() { 1196 if (isShowing() && mPopupView != null) { 1197 unregisterForScrollChanged(); 1198 1199 try { 1200 mWindowManager.removeView(mPopupView); 1201 } finally { 1202 if (mPopupView != mContentView && mPopupView instanceof ViewGroup) { 1203 ((ViewGroup) mPopupView).removeView(mContentView); 1204 } 1205 mPopupView = null; 1206 mIsShowing = false; 1207 1208 if (mOnDismissListener != null) { 1209 mOnDismissListener.onDismiss(); 1210 } 1211 } 1212 } 1213 } 1214 1215 /** 1216 * Sets the listener to be called when the window is dismissed. 1217 * 1218 * @param onDismissListener The listener. 1219 */ 1220 public void setOnDismissListener(OnDismissListener onDismissListener) { 1221 mOnDismissListener = onDismissListener; 1222 } 1223 1224 /** 1225 * Updates the state of the popup window, if it is currently being displayed, 1226 * from the currently set state. This include: 1227 * {@link #setClippingEnabled(boolean)}, {@link #setFocusable(boolean)}, 1228 * {@link #setIgnoreCheekPress()}, {@link #setInputMethodMode(int)}, 1229 * {@link #setTouchable(boolean)}, and {@link #setAnimationStyle(int)}. 1230 */ 1231 public void update() { 1232 if (!isShowing() || mContentView == null) { 1233 return; 1234 } 1235 1236 WindowManager.LayoutParams p = (WindowManager.LayoutParams) 1237 mPopupView.getLayoutParams(); 1238 1239 boolean update = false; 1240 1241 final int newAnim = computeAnimationResource(); 1242 if (newAnim != p.windowAnimations) { 1243 p.windowAnimations = newAnim; 1244 update = true; 1245 } 1246 1247 final int newFlags = computeFlags(p.flags); 1248 if (newFlags != p.flags) { 1249 p.flags = newFlags; 1250 update = true; 1251 } 1252 1253 if (update) { 1254 mWindowManager.updateViewLayout(mPopupView, p); 1255 } 1256 } 1257 1258 /** 1259 * <p>Updates the dimension of the popup window. Calling this function 1260 * also updates the window with the current popup state as described 1261 * for {@link #update()}.</p> 1262 * 1263 * @param width the new width 1264 * @param height the new height 1265 */ 1266 public void update(int width, int height) { 1267 WindowManager.LayoutParams p = (WindowManager.LayoutParams) 1268 mPopupView.getLayoutParams(); 1269 update(p.x, p.y, width, height, false); 1270 } 1271 1272 /** 1273 * <p>Updates the position and the dimension of the popup window. Width and 1274 * height can be set to -1 to update location only. Calling this function 1275 * also updates the window with the current popup state as 1276 * described for {@link #update()}.</p> 1277 * 1278 * @param x the new x location 1279 * @param y the new y location 1280 * @param width the new width, can be -1 to ignore 1281 * @param height the new height, can be -1 to ignore 1282 */ 1283 public void update(int x, int y, int width, int height) { 1284 update(x, y, width, height, false); 1285 } 1286 1287 /** 1288 * <p>Updates the position and the dimension of the popup window. Width and 1289 * height can be set to -1 to update location only. Calling this function 1290 * also updates the window with the current popup state as 1291 * described for {@link #update()}.</p> 1292 * 1293 * @param x the new x location 1294 * @param y the new y location 1295 * @param width the new width, can be -1 to ignore 1296 * @param height the new height, can be -1 to ignore 1297 * @param force reposition the window even if the specified position 1298 * already seems to correspond to the LayoutParams 1299 */ 1300 public void update(int x, int y, int width, int height, boolean force) { 1301 if (width != -1) { 1302 mLastWidth = width; 1303 setWidth(width); 1304 } 1305 1306 if (height != -1) { 1307 mLastHeight = height; 1308 setHeight(height); 1309 } 1310 1311 if (!isShowing() || mContentView == null) { 1312 return; 1313 } 1314 1315 WindowManager.LayoutParams p = (WindowManager.LayoutParams) mPopupView.getLayoutParams(); 1316 1317 boolean update = force; 1318 1319 final int finalWidth = mWidthMode < 0 ? mWidthMode : mLastWidth; 1320 if (width != -1 && p.width != finalWidth) { 1321 p.width = mLastWidth = finalWidth; 1322 update = true; 1323 } 1324 1325 final int finalHeight = mHeightMode < 0 ? mHeightMode : mLastHeight; 1326 if (height != -1 && p.height != finalHeight) { 1327 p.height = mLastHeight = finalHeight; 1328 update = true; 1329 } 1330 1331 if (p.x != x) { 1332 p.x = x; 1333 update = true; 1334 } 1335 1336 if (p.y != y) { 1337 p.y = y; 1338 update = true; 1339 } 1340 1341 final int newAnim = computeAnimationResource(); 1342 if (newAnim != p.windowAnimations) { 1343 p.windowAnimations = newAnim; 1344 update = true; 1345 } 1346 1347 final int newFlags = computeFlags(p.flags); 1348 if (newFlags != p.flags) { 1349 p.flags = newFlags; 1350 update = true; 1351 } 1352 1353 if (update) { 1354 mWindowManager.updateViewLayout(mPopupView, p); 1355 } 1356 } 1357 1358 /** 1359 * <p>Updates the position and the dimension of the popup window. Calling this 1360 * function also updates the window with the current popup state as described 1361 * for {@link #update()}.</p> 1362 * 1363 * @param anchor the popup's anchor view 1364 * @param width the new width, can be -1 to ignore 1365 * @param height the new height, can be -1 to ignore 1366 */ 1367 public void update(View anchor, int width, int height) { 1368 update(anchor, false, 0, 0, true, width, height); 1369 } 1370 1371 /** 1372 * <p>Updates the position and the dimension of the popup window. Width and 1373 * height can be set to -1 to update location only. Calling this function 1374 * also updates the window with the current popup state as 1375 * described for {@link #update()}.</p> 1376 * 1377 * <p>If the view later scrolls to move <code>anchor</code> to a different 1378 * location, the popup will be moved correspondingly.</p> 1379 * 1380 * @param anchor the popup's anchor view 1381 * @param xoff x offset from the view's left edge 1382 * @param yoff y offset from the view's bottom edge 1383 * @param width the new width, can be -1 to ignore 1384 * @param height the new height, can be -1 to ignore 1385 */ 1386 public void update(View anchor, int xoff, int yoff, int width, int height) { 1387 update(anchor, true, xoff, yoff, true, width, height); 1388 } 1389 1390 private void update(View anchor, boolean updateLocation, int xoff, int yoff, 1391 boolean updateDimension, int width, int height) { 1392 1393 if (!isShowing() || mContentView == null) { 1394 return; 1395 } 1396 1397 WeakReference<View> oldAnchor = mAnchor; 1398 final boolean needsUpdate = updateLocation && (mAnchorXoff != xoff || mAnchorYoff != yoff); 1399 if (oldAnchor == null || oldAnchor.get() != anchor || (needsUpdate && !mIsDropdown)) { 1400 registerForScrollChanged(anchor, xoff, yoff); 1401 } else if (needsUpdate) { 1402 // No need to register again if this is a DropDown, showAsDropDown already did. 1403 mAnchorXoff = xoff; 1404 mAnchorYoff = yoff; 1405 } 1406 1407 WindowManager.LayoutParams p = (WindowManager.LayoutParams) mPopupView.getLayoutParams(); 1408 1409 if (updateDimension) { 1410 if (width == -1) { 1411 width = mPopupWidth; 1412 } else { 1413 mPopupWidth = width; 1414 } 1415 if (height == -1) { 1416 height = mPopupHeight; 1417 } else { 1418 mPopupHeight = height; 1419 } 1420 } 1421 1422 int x = p.x; 1423 int y = p.y; 1424 1425 if (updateLocation) { 1426 updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff)); 1427 } else { 1428 updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff)); 1429 } 1430 1431 update(p.x, p.y, width, height, x != p.x || y != p.y); 1432 } 1433 1434 /** 1435 * Listener that is called when this popup window is dismissed. 1436 */ 1437 public interface OnDismissListener { 1438 /** 1439 * Called when this popup window is dismissed. 1440 */ 1441 public void onDismiss(); 1442 } 1443 1444 private void unregisterForScrollChanged() { 1445 WeakReference<View> anchorRef = mAnchor; 1446 View anchor = null; 1447 if (anchorRef != null) { 1448 anchor = anchorRef.get(); 1449 } 1450 if (anchor != null) { 1451 ViewTreeObserver vto = anchor.getViewTreeObserver(); 1452 vto.removeOnScrollChangedListener(mOnScrollChangedListener); 1453 } 1454 mAnchor = null; 1455 } 1456 1457 private void registerForScrollChanged(View anchor, int xoff, int yoff) { 1458 unregisterForScrollChanged(); 1459 1460 mAnchor = new WeakReference<View>(anchor); 1461 ViewTreeObserver vto = anchor.getViewTreeObserver(); 1462 if (vto != null) { 1463 vto.addOnScrollChangedListener(mOnScrollChangedListener); 1464 } 1465 1466 mAnchorXoff = xoff; 1467 mAnchorYoff = yoff; 1468 } 1469 1470 private class PopupViewContainer extends FrameLayout { 1471 private static final String TAG = "PopupWindow.PopupViewContainer"; 1472 1473 public PopupViewContainer(Context context) { 1474 super(context); 1475 } 1476 1477 @Override 1478 protected int[] onCreateDrawableState(int extraSpace) { 1479 if (mAboveAnchor) { 1480 // 1 more needed for the above anchor state 1481 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 1482 View.mergeDrawableStates(drawableState, ABOVE_ANCHOR_STATE_SET); 1483 return drawableState; 1484 } else { 1485 return super.onCreateDrawableState(extraSpace); 1486 } 1487 } 1488 1489 @Override 1490 public boolean dispatchKeyEvent(KeyEvent event) { 1491 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { 1492 if (event.getAction() == KeyEvent.ACTION_DOWN 1493 && event.getRepeatCount() == 0) { 1494 KeyEvent.DispatcherState state = getKeyDispatcherState(); 1495 if (state != null) { 1496 state.startTracking(event, this); 1497 } 1498 return true; 1499 } else if (event.getAction() == KeyEvent.ACTION_UP) { 1500 KeyEvent.DispatcherState state = getKeyDispatcherState(); 1501 if (state != null && state.isTracking(event) && !event.isCanceled()) { 1502 dismiss(); 1503 return true; 1504 } 1505 } 1506 return super.dispatchKeyEvent(event); 1507 } else { 1508 return super.dispatchKeyEvent(event); 1509 } 1510 } 1511 1512 @Override 1513 public boolean dispatchTouchEvent(MotionEvent ev) { 1514 if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) { 1515 return true; 1516 } 1517 return super.dispatchTouchEvent(ev); 1518 } 1519 1520 @Override 1521 public boolean onTouchEvent(MotionEvent event) { 1522 final int x = (int) event.getX(); 1523 final int y = (int) event.getY(); 1524 1525 if ((event.getAction() == MotionEvent.ACTION_DOWN) 1526 && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) { 1527 dismiss(); 1528 return true; 1529 } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { 1530 dismiss(); 1531 return true; 1532 } else { 1533 return super.onTouchEvent(event); 1534 } 1535 } 1536 1537 @Override 1538 public void sendAccessibilityEvent(int eventType) { 1539 // clinets are interested in the content not the container, make it event source 1540 if (mContentView != null) { 1541 mContentView.sendAccessibilityEvent(eventType); 1542 } else { 1543 super.sendAccessibilityEvent(eventType); 1544 } 1545 } 1546 } 1547 1548} 1549