The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.widget; |
| 18 | |
Adam Powell | a7287f4 | 2010-08-17 21:17:04 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Adam Powell | a7287f4 | 2010-08-17 21:17:04 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.graphics.PixelFormat; |
| 25 | import android.graphics.Rect; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.graphics.drawable.StateListDrawable; |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 28 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.IBinder; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 30 | import android.transition.Transition; |
| 31 | import android.transition.Transition.EpicenterCallback; |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 32 | import android.transition.Transition.TransitionListener; |
| 33 | import android.transition.Transition.TransitionListenerAdapter; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 34 | import android.transition.TransitionInflater; |
| 35 | import android.transition.TransitionManager; |
| 36 | import android.transition.TransitionSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.util.AttributeSet; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 38 | import android.view.Gravity; |
| 39 | import android.view.KeyEvent; |
| 40 | import android.view.MotionEvent; |
| 41 | import android.view.View; |
Adam Powell | a7287f4 | 2010-08-17 21:17:04 -0700 | [diff] [blame] | 42 | import android.view.View.OnTouchListener; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 43 | import android.view.ViewGroup; |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 44 | import android.view.ViewParent; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 45 | import android.view.ViewTreeObserver; |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 46 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 47 | import android.view.ViewTreeObserver.OnScrollChangedListener; |
Adam Powell | a7287f4 | 2010-08-17 21:17:04 -0700 | [diff] [blame] | 48 | import android.view.WindowManager; |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 49 | import android.view.WindowManager.LayoutParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Adam Powell | a7287f4 | 2010-08-17 21:17:04 -0700 | [diff] [blame] | 51 | import java.lang.ref.WeakReference; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * <p>A popup window that can be used to display an arbitrary view. The popup |
Scott Kennedy | 7ed189e | 2013-01-11 22:31:43 -0800 | [diff] [blame] | 55 | * window is a floating container that appears on top of the current |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | * activity.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 57 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | * @see android.widget.AutoCompleteTextView |
| 59 | * @see android.widget.Spinner |
| 60 | */ |
| 61 | public class PopupWindow { |
| 62 | /** |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 63 | * Mode for {@link #setInputMethodMode(int)}: the requirements for the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | * input method should be based on the focusability of the popup. That is |
| 65 | * if it is focusable than it needs to work with the input method, else |
| 66 | * it doesn't. |
| 67 | */ |
| 68 | public static final int INPUT_METHOD_FROM_FOCUSABLE = 0; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /** |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 71 | * Mode for {@link #setInputMethodMode(int)}: this popup always needs to |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | * work with an input method, regardless of whether it is focusable. This |
| 73 | * means that it will always be displayed so that the user can also operate |
| 74 | * the input method while it is shown. |
| 75 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | public static final int INPUT_METHOD_NEEDED = 1; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 77 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | /** |
Romain Guy | e29f064 | 2009-06-23 21:27:02 -0700 | [diff] [blame] | 79 | * Mode for {@link #setInputMethodMode(int)}: this popup never needs to |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | * work with an input method, regardless of whether it is focusable. This |
| 81 | * means that it will always be displayed to use as much space on the |
| 82 | * screen as needed, regardless of whether this covers the input method. |
| 83 | */ |
| 84 | public static final int INPUT_METHOD_NOT_NEEDED = 2; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 85 | |
| 86 | private static final int DEFAULT_ANCHORED_GRAVITY = Gravity.TOP | Gravity.START; |
| 87 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Default animation style indicating that separate animations should be |
| 90 | * used for top/bottom anchoring states. |
| 91 | */ |
| 92 | private static final int ANIMATION_STYLE_DEFAULT = -1; |
| 93 | |
| 94 | private final int[] mDrawingLocation = new int[2]; |
| 95 | private final int[] mScreenLocation = new int[2]; |
| 96 | private final Rect mTempRect = new Rect(); |
| 97 | private final Rect mAnchorBounds = new Rect(); |
| 98 | |
Romain Guy | 448ecf5 | 2009-05-14 16:03:42 -0700 | [diff] [blame] | 99 | private Context mContext; |
| 100 | private WindowManager mWindowManager; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | private boolean mIsShowing; |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 103 | private boolean mIsTransitioningToDismiss; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | private boolean mIsDropdown; |
| 105 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 106 | /** View that handles event dispatch and content transitions. */ |
| 107 | private PopupDecorView mDecorView; |
| 108 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 109 | /** The contents of the popup. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private View mContentView; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | private boolean mFocusable; |
| 113 | private int mInputMethodMode = INPUT_METHOD_FROM_FOCUSABLE; |
Dianne Hackborn | 7eab094 | 2011-01-01 13:21:50 -0800 | [diff] [blame] | 114 | private int mSoftInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | private boolean mTouchable = true; |
| 116 | private boolean mOutsideTouchable = false; |
| 117 | private boolean mClippingEnabled = true; |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 118 | private int mSplitTouchEnabled = -1; |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 119 | private boolean mLayoutInScreen; |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 120 | private boolean mClipToScreen; |
Adam Powell | 348e69c | 2011-02-16 16:49:50 -0800 | [diff] [blame] | 121 | private boolean mAllowScrollingAnchorParent = true; |
Adam Powell | 0bd1d0a | 2011-07-22 19:35:06 -0700 | [diff] [blame] | 122 | private boolean mLayoutInsetDecor = false; |
Adam Powell | e0b6cd1 | 2011-09-28 22:06:11 -0700 | [diff] [blame] | 123 | private boolean mNotTouchModal; |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 124 | private boolean mAttachedInDecor = true; |
| 125 | private boolean mAttachedInDecorSet = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
| 127 | private OnTouchListener mTouchInterceptor; |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | private int mWidthMode; |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 130 | private int mWidth = LayoutParams.WRAP_CONTENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | private int mLastWidth; |
| 132 | private int mHeightMode; |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 133 | private int mHeight = LayoutParams.WRAP_CONTENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | private int mLastHeight; |
| 135 | |
| 136 | private int mPopupWidth; |
| 137 | private int mPopupHeight; |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 138 | |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 139 | private float mElevation; |
| 140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | private Drawable mBackground; |
| 142 | private Drawable mAboveAnchorBackgroundDrawable; |
| 143 | private Drawable mBelowAnchorBackgroundDrawable; |
| 144 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 145 | private Transition mEnterTransition; |
| 146 | private Transition mExitTransition; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | private boolean mAboveAnchor; |
Adam Powell | 574b37e | 2010-10-07 11:15:19 -0700 | [diff] [blame] | 149 | private int mWindowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | private OnDismissListener mOnDismissListener; |
| 152 | private boolean mIgnoreCheekPress = false; |
| 153 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 154 | private int mAnimationStyle = ANIMATION_STYLE_DEFAULT; |
| 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | private static final int[] ABOVE_ANCHOR_STATE_SET = new int[] { |
| 157 | com.android.internal.R.attr.state_above_anchor |
| 158 | }; |
| 159 | |
| 160 | private WeakReference<View> mAnchor; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 161 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 162 | private final EpicenterCallback mEpicenterCallback = new EpicenterCallback() { |
| 163 | @Override |
| 164 | public Rect onGetEpicenter(Transition transition) { |
Alan Viverette | df4639a0 | 2015-03-02 12:40:34 -0800 | [diff] [blame] | 165 | final View anchor = mAnchor != null ? mAnchor.get() : null; |
Alan Viverette | 7878edf | 2015-02-03 15:49:18 -0800 | [diff] [blame] | 166 | final View decor = mDecorView; |
| 167 | if (anchor == null || decor == null) { |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | final Rect anchorBounds = mAnchorBounds; |
Alan Viverette | df4639a0 | 2015-03-02 12:40:34 -0800 | [diff] [blame] | 172 | final int[] anchorLocation = anchor.getLocationOnScreen(); |
Alan Viverette | 7878edf | 2015-02-03 15:49:18 -0800 | [diff] [blame] | 173 | final int[] popupLocation = mDecorView.getLocationOnScreen(); |
| 174 | |
| 175 | // Compute the position of the anchor relative to the popup. |
| 176 | anchorBounds.set(0, 0, anchor.getWidth(), anchor.getHeight()); |
| 177 | anchorBounds.offset(anchorLocation[0] - popupLocation[0], |
| 178 | anchorLocation[1] - popupLocation[1]); |
| 179 | |
| 180 | return anchorBounds; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 181 | } |
| 182 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 184 | private final OnScrollChangedListener mOnScrollChangedListener = new OnScrollChangedListener() { |
| 185 | @Override |
| 186 | public void onScrollChanged() { |
| 187 | final View anchor = mAnchor != null ? mAnchor.get() : null; |
| 188 | if (anchor != null && mDecorView != null) { |
| 189 | final WindowManager.LayoutParams p = (WindowManager.LayoutParams) |
| 190 | mDecorView.getLayoutParams(); |
| 191 | |
| 192 | updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff, |
| 193 | mAnchoredGravity)); |
| 194 | update(p.x, p.y, -1, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 196 | } |
| 197 | }; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 198 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 199 | private int mAnchorXoff; |
| 200 | private int mAnchorYoff; |
| 201 | private int mAnchoredGravity; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 202 | private boolean mOverlapAnchor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 204 | private boolean mPopupViewInitialLayoutDirectionInherited; |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | /** |
| 207 | * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> |
| 208 | * |
| 209 | * <p>The popup does provide a background.</p> |
| 210 | */ |
| 211 | public PopupWindow(Context context) { |
| 212 | this(context, null); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> |
| 217 | * |
| 218 | * <p>The popup does provide a background.</p> |
| 219 | */ |
| 220 | public PopupWindow(Context context, AttributeSet attrs) { |
| 221 | this(context, attrs, com.android.internal.R.attr.popupWindowStyle); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> |
| 226 | * |
| 227 | * <p>The popup does provide a background.</p> |
| 228 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 229 | public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) { |
| 230 | this(context, attrs, defStyleAttr, 0); |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 231 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 232 | |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 233 | /** |
| 234 | * <p>Create a new, empty, non focusable popup window of dimension (0,0).</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 235 | * |
Adam Powell | c3fa630 | 2010-05-18 11:36:27 -0700 | [diff] [blame] | 236 | * <p>The popup does not provide a background.</p> |
| 237 | */ |
| 238 | public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | mContext = context; |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 240 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 242 | final TypedArray a = context.obtainStyledAttributes( |
Alan Viverette | ce8c358 | 2014-11-07 13:19:38 -0800 | [diff] [blame] | 243 | attrs, R.styleable.PopupWindow, defStyleAttr, defStyleRes); |
| 244 | final Drawable bg = a.getDrawable(R.styleable.PopupWindow_popupBackground); |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 245 | mElevation = a.getDimension(R.styleable.PopupWindow_popupElevation, 0); |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 246 | mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false); |
Adam Powell | c3808b5 | 2010-10-04 10:06:59 -0700 | [diff] [blame] | 247 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 248 | // Preserve default behavior from Gingerbread. If the animation is |
| 249 | // undefined or explicitly specifies the Gingerbread animation style, |
| 250 | // use a sentinel value. |
| 251 | if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupAnimationStyle)) { |
| 252 | final int animStyle = a.getResourceId(R.styleable.PopupWindow_popupAnimationStyle, 0); |
| 253 | if (animStyle == R.style.Animation_PopupWindow) { |
| 254 | mAnimationStyle = ANIMATION_STYLE_DEFAULT; |
| 255 | } else { |
| 256 | mAnimationStyle = animStyle; |
| 257 | } |
| 258 | } else { |
| 259 | mAnimationStyle = ANIMATION_STYLE_DEFAULT; |
| 260 | } |
| 261 | |
| 262 | final Transition enterTransition = getTransition(a.getResourceId( |
| 263 | R.styleable.PopupWindow_popupEnterTransition, 0)); |
| 264 | final Transition exitTransition; |
| 265 | if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupExitTransition)) { |
| 266 | exitTransition = getTransition(a.getResourceId( |
| 267 | R.styleable.PopupWindow_popupExitTransition, 0)); |
| 268 | } else { |
| 269 | exitTransition = enterTransition == null ? null : enterTransition.clone(); |
| 270 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | a.recycle(); |
Alan Viverette | ce8c358 | 2014-11-07 13:19:38 -0800 | [diff] [blame] | 273 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 274 | setEnterTransition(enterTransition); |
| 275 | setExitTransition(exitTransition); |
Alan Viverette | ce8c358 | 2014-11-07 13:19:38 -0800 | [diff] [blame] | 276 | setBackgroundDrawable(bg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * <p>Create a new empty, non focusable popup window of dimension (0,0).</p> |
| 281 | * |
| 282 | * <p>The popup does not provide any background. This should be handled |
| 283 | * by the content view.</p> |
| 284 | */ |
| 285 | public PopupWindow() { |
| 286 | this(null, 0, 0); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * <p>Create a new non focusable popup window which can display the |
| 291 | * <tt>contentView</tt>. The dimension of the window are (0,0).</p> |
| 292 | * |
| 293 | * <p>The popup does not provide any background. This should be handled |
| 294 | * by the content view.</p> |
| 295 | * |
| 296 | * @param contentView the popup's content |
| 297 | */ |
| 298 | public PopupWindow(View contentView) { |
| 299 | this(contentView, 0, 0); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * <p>Create a new empty, non focusable popup window. The dimension of the |
| 304 | * window must be passed to this constructor.</p> |
| 305 | * |
| 306 | * <p>The popup does not provide any background. This should be handled |
| 307 | * by the content view.</p> |
| 308 | * |
| 309 | * @param width the popup's width |
| 310 | * @param height the popup's height |
| 311 | */ |
| 312 | public PopupWindow(int width, int height) { |
| 313 | this(null, width, height); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * <p>Create a new non focusable popup window which can display the |
| 318 | * <tt>contentView</tt>. The dimension of the window must be passed to |
| 319 | * this constructor.</p> |
| 320 | * |
| 321 | * <p>The popup does not provide any background. This should be handled |
| 322 | * by the content view.</p> |
| 323 | * |
| 324 | * @param contentView the popup's content |
| 325 | * @param width the popup's width |
| 326 | * @param height the popup's height |
| 327 | */ |
| 328 | public PopupWindow(View contentView, int width, int height) { |
| 329 | this(contentView, width, height, false); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * <p>Create a new popup window which can display the <tt>contentView</tt>. |
| 334 | * The dimension of the window must be passed to this constructor.</p> |
| 335 | * |
| 336 | * <p>The popup does not provide any background. This should be handled |
| 337 | * by the content view.</p> |
| 338 | * |
| 339 | * @param contentView the popup's content |
| 340 | * @param width the popup's width |
| 341 | * @param height the popup's height |
| 342 | * @param focusable true if the popup can be focused, false otherwise |
| 343 | */ |
Romain Guy | 448ecf5 | 2009-05-14 16:03:42 -0700 | [diff] [blame] | 344 | public PopupWindow(View contentView, int width, int height, boolean focusable) { |
| 345 | if (contentView != null) { |
| 346 | mContext = contentView.getContext(); |
| 347 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
| 348 | } |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | setContentView(contentView); |
| 351 | setWidth(width); |
| 352 | setHeight(height); |
| 353 | setFocusable(focusable); |
| 354 | } |
| 355 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 356 | public void setEnterTransition(Transition enterTransition) { |
| 357 | mEnterTransition = enterTransition; |
| 358 | |
| 359 | if (mEnterTransition != null) { |
| 360 | mEnterTransition.setEpicenterCallback(mEpicenterCallback); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | public void setExitTransition(Transition exitTransition) { |
| 365 | mExitTransition = exitTransition; |
| 366 | |
| 367 | if (mExitTransition != null) { |
| 368 | mExitTransition.setEpicenterCallback(mEpicenterCallback); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | private Transition getTransition(int resId) { |
| 373 | if (resId != 0 && resId != R.transition.no_transition) { |
| 374 | final TransitionInflater inflater = TransitionInflater.from(mContext); |
| 375 | final Transition transition = inflater.inflateTransition(resId); |
| 376 | if (transition != null) { |
| 377 | final boolean isEmpty = transition instanceof TransitionSet |
| 378 | && ((TransitionSet) transition).getTransitionCount() == 0; |
| 379 | if (!isEmpty) { |
| 380 | return transition; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | return null; |
| 385 | } |
| 386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | /** |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 388 | * Return the drawable used as the popup window's background. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | * |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 390 | * @return the background drawable or {@code null} if not set |
| 391 | * @see #setBackgroundDrawable(Drawable) |
| 392 | * @attr ref android.R.styleable#PopupWindow_popupBackground |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | */ |
| 394 | public Drawable getBackground() { |
| 395 | return mBackground; |
| 396 | } |
| 397 | |
| 398 | /** |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 399 | * Specifies the background drawable for this popup window. The background |
| 400 | * can be set to {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | * |
| 402 | * @param background the popup's background |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 403 | * @see #getBackground() |
| 404 | * @attr ref android.R.styleable#PopupWindow_popupBackground |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | */ |
| 406 | public void setBackgroundDrawable(Drawable background) { |
| 407 | mBackground = background; |
Alan Viverette | ce8c358 | 2014-11-07 13:19:38 -0800 | [diff] [blame] | 408 | |
| 409 | // If this is a StateListDrawable, try to find and store the drawable to be |
| 410 | // used when the drop-down is placed above its anchor view, and the one to be |
| 411 | // used when the drop-down is placed below its anchor view. We extract |
| 412 | // the drawables ourselves to work around a problem with using refreshDrawableState |
| 413 | // that it will take into account the padding of all drawables specified in a |
| 414 | // StateListDrawable, thus adding superfluous padding to drop-down views. |
| 415 | // |
| 416 | // We assume a StateListDrawable will have a drawable for ABOVE_ANCHOR_STATE_SET and |
| 417 | // at least one other drawable, intended for the 'below-anchor state'. |
| 418 | if (mBackground instanceof StateListDrawable) { |
| 419 | StateListDrawable stateList = (StateListDrawable) mBackground; |
| 420 | |
| 421 | // Find the above-anchor view - this one's easy, it should be labeled as such. |
| 422 | int aboveAnchorStateIndex = stateList.getStateDrawableIndex(ABOVE_ANCHOR_STATE_SET); |
| 423 | |
| 424 | // Now, for the below-anchor view, look for any other drawable specified in the |
| 425 | // StateListDrawable which is not for the above-anchor state and use that. |
| 426 | int count = stateList.getStateCount(); |
| 427 | int belowAnchorStateIndex = -1; |
| 428 | for (int i = 0; i < count; i++) { |
| 429 | if (i != aboveAnchorStateIndex) { |
| 430 | belowAnchorStateIndex = i; |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Store the drawables we found, if we found them. Otherwise, set them both |
| 436 | // to null so that we'll just use refreshDrawableState. |
| 437 | if (aboveAnchorStateIndex != -1 && belowAnchorStateIndex != -1) { |
| 438 | mAboveAnchorBackgroundDrawable = stateList.getStateDrawable(aboveAnchorStateIndex); |
| 439 | mBelowAnchorBackgroundDrawable = stateList.getStateDrawable(belowAnchorStateIndex); |
| 440 | } else { |
| 441 | mBelowAnchorBackgroundDrawable = null; |
| 442 | mAboveAnchorBackgroundDrawable = null; |
| 443 | } |
| 444 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 448 | * @return the elevation for this popup window in pixels |
| 449 | * @see #setElevation(float) |
| 450 | * @attr ref android.R.styleable#PopupWindow_popupElevation |
| 451 | */ |
| 452 | public float getElevation() { |
| 453 | return mElevation; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Specifies the elevation for this popup window. |
| 458 | * |
| 459 | * @param elevation the popup's elevation in pixels |
| 460 | * @see #getElevation() |
| 461 | * @attr ref android.R.styleable#PopupWindow_popupElevation |
| 462 | */ |
| 463 | public void setElevation(float elevation) { |
| 464 | mElevation = elevation; |
| 465 | } |
| 466 | |
| 467 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | * <p>Return the animation style to use the popup appears and disappears</p> |
| 469 | * |
| 470 | * @return the animation style to use the popup appears and disappears |
| 471 | */ |
| 472 | public int getAnimationStyle() { |
| 473 | return mAnimationStyle; |
| 474 | } |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | /** |
Shuhrat Dehkanov | 54ec76d | 2014-08-28 17:12:23 +0900 | [diff] [blame] | 477 | * Set the flag on popup to ignore cheek press events; by default this flag |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | * is set to false |
Shuhrat Dehkanov | 54ec76d | 2014-08-28 17:12:23 +0900 | [diff] [blame] | 479 | * which means the popup will not ignore cheek press dispatch events. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 480 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | * <p>If the popup is showing, calling this method will take effect only |
| 482 | * the next time the popup is shown or through a manual call to one of |
| 483 | * the {@link #update()} methods.</p> |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 484 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | * @see #update() |
| 486 | */ |
| 487 | public void setIgnoreCheekPress() { |
| 488 | mIgnoreCheekPress = true; |
| 489 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | |
| 492 | /** |
| 493 | * <p>Change the animation style resource for this popup.</p> |
| 494 | * |
| 495 | * <p>If the popup is showing, calling this method will take effect only |
| 496 | * the next time the popup is shown or through a manual call to one of |
| 497 | * the {@link #update()} methods.</p> |
| 498 | * |
| 499 | * @param animationStyle animation style to use when the popup appears |
| 500 | * and disappears. Set to -1 for the default animation, 0 for no |
| 501 | * animation, or a resource identifier for an explicit animation. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 502 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | * @see #update() |
| 504 | */ |
| 505 | public void setAnimationStyle(int animationStyle) { |
| 506 | mAnimationStyle = animationStyle; |
| 507 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 508 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | /** |
| 510 | * <p>Return the view used as the content of the popup window.</p> |
| 511 | * |
| 512 | * @return a {@link android.view.View} representing the popup's content |
| 513 | * |
| 514 | * @see #setContentView(android.view.View) |
| 515 | */ |
| 516 | public View getContentView() { |
| 517 | return mContentView; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * <p>Change the popup's content. The content is represented by an instance |
| 522 | * of {@link android.view.View}.</p> |
| 523 | * |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 524 | * <p>This method has no effect if called when the popup is showing.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | * |
| 526 | * @param contentView the new content for the popup |
| 527 | * |
| 528 | * @see #getContentView() |
| 529 | * @see #isShowing() |
| 530 | */ |
| 531 | public void setContentView(View contentView) { |
| 532 | if (isShowing()) { |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | mContentView = contentView; |
Romain Guy | 448ecf5 | 2009-05-14 16:03:42 -0700 | [diff] [blame] | 537 | |
Romain Guy | 0c0b768 | 2011-05-16 11:54:09 -0700 | [diff] [blame] | 538 | if (mContext == null && mContentView != null) { |
Romain Guy | 448ecf5 | 2009-05-14 16:03:42 -0700 | [diff] [blame] | 539 | mContext = mContentView.getContext(); |
| 540 | } |
| 541 | |
Romain Guy | 0c0b768 | 2011-05-16 11:54:09 -0700 | [diff] [blame] | 542 | if (mWindowManager == null && mContentView != null) { |
Romain Guy | 448ecf5 | 2009-05-14 16:03:42 -0700 | [diff] [blame] | 543 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
| 544 | } |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 545 | |
| 546 | // Setting the default for attachedInDecor based on SDK version here |
| 547 | // instead of in the constructor since we might not have the context |
| 548 | // object in the constructor. We only want to set default here if the |
| 549 | // app hasn't already set the attachedInDecor. |
| 550 | if (mContext != null && !mAttachedInDecorSet) { |
| 551 | // Attach popup window in decor frame of parent window by default for |
| 552 | // {@link Build.VERSION_CODES.LOLLIPOP_MR1} or greater. Keep current |
| 553 | // behavior of not attaching to decor frame for older SDKs. |
| 554 | setAttachedInDecor(mContext.getApplicationInfo().targetSdkVersion |
| 555 | >= Build.VERSION_CODES.LOLLIPOP_MR1); |
| 556 | } |
| 557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Set a callback for all touch events being dispatched to the popup |
| 562 | * window. |
| 563 | */ |
| 564 | public void setTouchInterceptor(OnTouchListener l) { |
| 565 | mTouchInterceptor = l; |
| 566 | } |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | /** |
| 569 | * <p>Indicate whether the popup window can grab the focus.</p> |
| 570 | * |
| 571 | * @return true if the popup is focusable, false otherwise |
| 572 | * |
| 573 | * @see #setFocusable(boolean) |
| 574 | */ |
| 575 | public boolean isFocusable() { |
| 576 | return mFocusable; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * <p>Changes the focusability of the popup window. When focusable, the |
| 581 | * window will grab the focus from the current focused widget if the popup |
| 582 | * contains a focusable {@link android.view.View}. By default a popup |
| 583 | * window is not focusable.</p> |
| 584 | * |
| 585 | * <p>If the popup is showing, calling this method will take effect only |
| 586 | * the next time the popup is shown or through a manual call to one of |
| 587 | * the {@link #update()} methods.</p> |
| 588 | * |
| 589 | * @param focusable true if the popup should grab focus, false otherwise. |
| 590 | * |
| 591 | * @see #isFocusable() |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 592 | * @see #isShowing() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | * @see #update() |
| 594 | */ |
| 595 | public void setFocusable(boolean focusable) { |
| 596 | mFocusable = focusable; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Return the current value in {@link #setInputMethodMode(int)}. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 601 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | * @see #setInputMethodMode(int) |
| 603 | */ |
| 604 | public int getInputMethodMode() { |
| 605 | return mInputMethodMode; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | /** |
| 610 | * Control how the popup operates with an input method: one of |
| 611 | * {@link #INPUT_METHOD_FROM_FOCUSABLE}, {@link #INPUT_METHOD_NEEDED}, |
| 612 | * or {@link #INPUT_METHOD_NOT_NEEDED}. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 613 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | * <p>If the popup is showing, calling this method will take effect only |
| 615 | * the next time the popup is shown or through a manual call to one of |
| 616 | * the {@link #update()} methods.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 617 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | * @see #getInputMethodMode() |
| 619 | * @see #update() |
| 620 | */ |
| 621 | public void setInputMethodMode(int mode) { |
| 622 | mInputMethodMode = mode; |
| 623 | } |
Romain Guy | 374aaaed3 | 2009-07-14 15:11:59 -0700 | [diff] [blame] | 624 | |
| 625 | /** |
| 626 | * Sets the operating mode for the soft input area. |
| 627 | * |
| 628 | * @param mode The desired mode, see |
| 629 | * {@link android.view.WindowManager.LayoutParams#softInputMode} |
| 630 | * for the full list |
| 631 | * |
| 632 | * @see android.view.WindowManager.LayoutParams#softInputMode |
| 633 | * @see #getSoftInputMode() |
| 634 | */ |
| 635 | public void setSoftInputMode(int mode) { |
| 636 | mSoftInputMode = mode; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Returns the current value in {@link #setSoftInputMode(int)}. |
| 641 | * |
| 642 | * @see #setSoftInputMode(int) |
| 643 | * @see android.view.WindowManager.LayoutParams#softInputMode |
| 644 | */ |
| 645 | public int getSoftInputMode() { |
| 646 | return mSoftInputMode; |
| 647 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 648 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | /** |
| 650 | * <p>Indicates whether the popup window receives touch events.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 651 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | * @return true if the popup is touchable, false otherwise |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 653 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | * @see #setTouchable(boolean) |
| 655 | */ |
| 656 | public boolean isTouchable() { |
| 657 | return mTouchable; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * <p>Changes the touchability of the popup window. When touchable, the |
| 662 | * window will receive touch events, otherwise touch events will go to the |
| 663 | * window below it. By default the window is touchable.</p> |
| 664 | * |
| 665 | * <p>If the popup is showing, calling this method will take effect only |
| 666 | * the next time the popup is shown or through a manual call to one of |
| 667 | * the {@link #update()} methods.</p> |
| 668 | * |
| 669 | * @param touchable true if the popup should receive touch events, false otherwise |
| 670 | * |
| 671 | * @see #isTouchable() |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 672 | * @see #isShowing() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | * @see #update() |
| 674 | */ |
| 675 | public void setTouchable(boolean touchable) { |
| 676 | mTouchable = touchable; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * <p>Indicates whether the popup window will be informed of touch events |
| 681 | * outside of its window.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 682 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | * @return true if the popup is outside touchable, false otherwise |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 684 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | * @see #setOutsideTouchable(boolean) |
| 686 | */ |
| 687 | public boolean isOutsideTouchable() { |
| 688 | return mOutsideTouchable; |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * <p>Controls whether the pop-up will be informed of touch events outside |
| 693 | * of its window. This only makes sense for pop-ups that are touchable |
| 694 | * but not focusable, which means touches outside of the window will |
| 695 | * be delivered to the window behind. The default is false.</p> |
| 696 | * |
| 697 | * <p>If the popup is showing, calling this method will take effect only |
| 698 | * the next time the popup is shown or through a manual call to one of |
| 699 | * the {@link #update()} methods.</p> |
| 700 | * |
| 701 | * @param touchable true if the popup should receive outside |
| 702 | * touch events, false otherwise |
| 703 | * |
| 704 | * @see #isOutsideTouchable() |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 705 | * @see #isShowing() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | * @see #update() |
| 707 | */ |
| 708 | public void setOutsideTouchable(boolean touchable) { |
| 709 | mOutsideTouchable = touchable; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * <p>Indicates whether clipping of the popup window is enabled.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 714 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | * @return true if the clipping is enabled, false otherwise |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 716 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | * @see #setClippingEnabled(boolean) |
| 718 | */ |
| 719 | public boolean isClippingEnabled() { |
| 720 | return mClippingEnabled; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * <p>Allows the popup window to extend beyond the bounds of the screen. By default the |
| 725 | * window is clipped to the screen boundaries. Setting this to false will allow windows to be |
| 726 | * accurately positioned.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 727 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | * <p>If the popup is showing, calling this method will take effect only |
| 729 | * the next time the popup is shown or through a manual call to one of |
| 730 | * the {@link #update()} methods.</p> |
| 731 | * |
| 732 | * @param enabled false if the window should be allowed to extend outside of the screen |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 733 | * @see #isShowing() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | * @see #isClippingEnabled() |
| 735 | * @see #update() |
| 736 | */ |
| 737 | public void setClippingEnabled(boolean enabled) { |
| 738 | mClippingEnabled = enabled; |
| 739 | } |
| 740 | |
| 741 | /** |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 742 | * Clip this popup window to the screen, but not to the containing window. |
| 743 | * |
| 744 | * @param enabled True to clip to the screen. |
| 745 | * @hide |
| 746 | */ |
| 747 | public void setClipToScreenEnabled(boolean enabled) { |
| 748 | mClipToScreen = enabled; |
| 749 | setClippingEnabled(!enabled); |
| 750 | } |
Adam Powell | 348e69c | 2011-02-16 16:49:50 -0800 | [diff] [blame] | 751 | |
| 752 | /** |
| 753 | * Allow PopupWindow to scroll the anchor's parent to provide more room |
| 754 | * for the popup. Enabled by default. |
| 755 | * |
| 756 | * @param enabled True to scroll the anchor's parent when more room is desired by the popup. |
| 757 | */ |
| 758 | void setAllowScrollingAnchorParent(boolean enabled) { |
| 759 | mAllowScrollingAnchorParent = enabled; |
| 760 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 761 | |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 762 | /** |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 763 | * <p>Indicates whether the popup window supports splitting touches.</p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 764 | * |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 765 | * @return true if the touch splitting is enabled, false otherwise |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 766 | * |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 767 | * @see #setSplitTouchEnabled(boolean) |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 768 | */ |
| 769 | public boolean isSplitTouchEnabled() { |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 770 | if (mSplitTouchEnabled < 0 && mContext != null) { |
| 771 | return mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB; |
| 772 | } |
| 773 | return mSplitTouchEnabled == 1; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /** |
| 777 | * <p>Allows the popup window to split touches across other windows that also |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 778 | * support split touch. When this flag is false, the first pointer |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 779 | * that goes down determines the window to which all subsequent touches |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 780 | * go until all pointers go up. When this flag is true, each pointer |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 781 | * (not necessarily the first) that goes down determines the window |
| 782 | * to which all subsequent touches of that pointer will go until that |
| 783 | * pointer goes up thereby enabling touches with multiple pointers |
| 784 | * to be split across multiple windows.</p> |
| 785 | * |
| 786 | * @param enabled true if the split touches should be enabled, false otherwise |
| 787 | * @see #isSplitTouchEnabled() |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 788 | */ |
| 789 | public void setSplitTouchEnabled(boolean enabled) { |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 790 | mSplitTouchEnabled = enabled ? 1 : 0; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 794 | * <p>Indicates whether the popup window will be forced into using absolute screen coordinates |
| 795 | * for positioning.</p> |
| 796 | * |
| 797 | * @return true if the window will always be positioned in screen coordinates. |
| 798 | * @hide |
| 799 | */ |
| 800 | public boolean isLayoutInScreenEnabled() { |
| 801 | return mLayoutInScreen; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * <p>Allows the popup window to force the flag |
| 806 | * {@link WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN}, overriding default behavior. |
| 807 | * This will cause the popup to be positioned in absolute screen coordinates.</p> |
| 808 | * |
| 809 | * @param enabled true if the popup should always be positioned in screen coordinates |
| 810 | * @hide |
| 811 | */ |
| 812 | public void setLayoutInScreenEnabled(boolean enabled) { |
| 813 | mLayoutInScreen = enabled; |
| 814 | } |
| 815 | |
| 816 | /** |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 817 | * <p>Indicates whether the popup window will be attached in the decor frame of its parent |
| 818 | * window. |
| 819 | * |
| 820 | * @return true if the window will be attached to the decor frame of its parent window. |
| 821 | * |
| 822 | * @see #setAttachedInDecor(boolean) |
| 823 | * @see WindowManager.LayoutParams#FLAG_LAYOUT_ATTACHED_IN_DECOR |
| 824 | */ |
| 825 | public boolean isAttachedInDecor() { |
| 826 | return mAttachedInDecor; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * <p>This will attach the popup window to the decor frame of the parent window to avoid |
| 831 | * overlaping with screen decorations like the navigation bar. Overrides the default behavior of |
| 832 | * the flag {@link WindowManager.LayoutParams#FLAG_LAYOUT_ATTACHED_IN_DECOR}. |
| 833 | * |
| 834 | * <p>By default the flag is set on SDK version {@link Build.VERSION_CODES#LOLLIPOP_MR1} or |
| 835 | * greater and cleared on lesser SDK versions. |
| 836 | * |
| 837 | * @param enabled true if the popup should be attached to the decor frame of its parent window. |
| 838 | * |
| 839 | * @see WindowManager.LayoutParams#FLAG_LAYOUT_ATTACHED_IN_DECOR |
| 840 | */ |
| 841 | public void setAttachedInDecor(boolean enabled) { |
| 842 | mAttachedInDecor = enabled; |
| 843 | mAttachedInDecorSet = true; |
| 844 | } |
| 845 | |
| 846 | /** |
Adam Powell | 0bd1d0a | 2011-07-22 19:35:06 -0700 | [diff] [blame] | 847 | * Allows the popup window to force the flag |
| 848 | * {@link WindowManager.LayoutParams#FLAG_LAYOUT_INSET_DECOR}, overriding default behavior. |
| 849 | * This will cause the popup to inset its content to account for system windows overlaying |
| 850 | * the screen, such as the status bar. |
| 851 | * |
| 852 | * <p>This will often be combined with {@link #setLayoutInScreenEnabled(boolean)}. |
| 853 | * |
| 854 | * @param enabled true if the popup's views should inset content to account for system windows, |
| 855 | * the way that decor views behave for full-screen windows. |
| 856 | * @hide |
| 857 | */ |
| 858 | public void setLayoutInsetDecor(boolean enabled) { |
| 859 | mLayoutInsetDecor = enabled; |
| 860 | } |
| 861 | |
| 862 | /** |
Chris Banes | 36344a9 | 2015-04-14 10:43:16 +0100 | [diff] [blame] | 863 | * Set the layout type for this window. This value will be passed through to |
| 864 | * {@link WindowManager.LayoutParams#type} therefore the value should match any value |
| 865 | * {@link WindowManager.LayoutParams#type} accepts. |
Adam Powell | 574b37e | 2010-10-07 11:15:19 -0700 | [diff] [blame] | 866 | * |
| 867 | * @param layoutType Layout type for this window. |
Chris Banes | 36344a9 | 2015-04-14 10:43:16 +0100 | [diff] [blame] | 868 | * |
| 869 | * @see WindowManager.LayoutParams#type |
Adam Powell | 574b37e | 2010-10-07 11:15:19 -0700 | [diff] [blame] | 870 | */ |
| 871 | public void setWindowLayoutType(int layoutType) { |
| 872 | mWindowLayoutType = layoutType; |
| 873 | } |
| 874 | |
| 875 | /** |
Chris Banes | 36344a9 | 2015-04-14 10:43:16 +0100 | [diff] [blame] | 876 | * Returns the layout type for this window. |
| 877 | * |
| 878 | * @see #setWindowLayoutType(int) |
Adam Powell | 574b37e | 2010-10-07 11:15:19 -0700 | [diff] [blame] | 879 | */ |
| 880 | public int getWindowLayoutType() { |
| 881 | return mWindowLayoutType; |
| 882 | } |
| 883 | |
| 884 | /** |
Adam Powell | e0b6cd1 | 2011-09-28 22:06:11 -0700 | [diff] [blame] | 885 | * Set whether this window is touch modal or if outside touches will be sent to |
| 886 | * other windows behind it. |
| 887 | * @hide |
| 888 | */ |
| 889 | public void setTouchModal(boolean touchModal) { |
| 890 | mNotTouchModal = !touchModal; |
| 891 | } |
| 892 | |
| 893 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | * <p>Change the width and height measure specs that are given to the |
| 895 | * window manager by the popup. By default these are 0, meaning that |
| 896 | * the current width or height is requested as an explicit size from |
| 897 | * the window manager. You can supply |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 898 | * {@link ViewGroup.LayoutParams#WRAP_CONTENT} or |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 899 | * {@link ViewGroup.LayoutParams#MATCH_PARENT} to have that measure |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | * spec supplied instead, replacing the absolute width and height that |
| 901 | * has been set in the popup.</p> |
| 902 | * |
| 903 | * <p>If the popup is showing, calling this method will take effect only |
| 904 | * the next time the popup is shown.</p> |
| 905 | * |
| 906 | * @param widthSpec an explicit width measure spec mode, either |
| 907 | * {@link ViewGroup.LayoutParams#WRAP_CONTENT}, |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 908 | * {@link ViewGroup.LayoutParams#MATCH_PARENT}, or 0 to use the absolute |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | * width. |
| 910 | * @param heightSpec an explicit height measure spec mode, either |
| 911 | * {@link ViewGroup.LayoutParams#WRAP_CONTENT}, |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 912 | * {@link ViewGroup.LayoutParams#MATCH_PARENT}, or 0 to use the absolute |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | * height. |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 914 | * |
| 915 | * @deprecated Use {@link #setWidth(int)} and {@link #setHeight(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | */ |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 917 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | public void setWindowLayoutMode(int widthSpec, int heightSpec) { |
| 919 | mWidthMode = widthSpec; |
| 920 | mHeightMode = heightSpec; |
| 921 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 922 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 924 | * Returns the popup's height MeasureSpec. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | * |
| 926 | * @return the height MeasureSpec of the popup |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | * @see #setHeight(int) |
| 928 | */ |
| 929 | public int getHeight() { |
| 930 | return mHeight; |
| 931 | } |
| 932 | |
| 933 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 934 | * Sets the popup's height MeasureSpec. |
| 935 | * <p> |
| 936 | * If the popup is showing, calling this method will take effect the next |
| 937 | * time the popup is shown. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | * |
| 939 | * @param height the height MeasureSpec of the popup |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | * @see #getHeight() |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 941 | * @see #isShowing() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | */ |
| 943 | public void setHeight(int height) { |
| 944 | mHeight = height; |
| 945 | } |
| 946 | |
| 947 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 948 | * Returns the popup's width MeasureSpec. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | * |
| 950 | * @return the width MeasureSpec of the popup |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 951 | * @see #setWidth(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | */ |
| 953 | public int getWidth() { |
| 954 | return mWidth; |
| 955 | } |
| 956 | |
| 957 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 958 | * Sets the popup's width MeasureSpec. |
| 959 | * <p> |
| 960 | * If the popup is showing, calling this method will take effect the next |
| 961 | * time the popup is shown. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | * |
| 963 | * @param width the width MeasureSpec of the popup |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | * @see #getWidth() |
| 965 | * @see #isShowing() |
| 966 | */ |
| 967 | public void setWidth(int width) { |
| 968 | mWidth = width; |
| 969 | } |
| 970 | |
| 971 | /** |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 972 | * Sets whether the popup window should overlap its anchor view when |
| 973 | * displayed as a drop-down. |
| 974 | * <p> |
| 975 | * If the popup is showing, calling this method will take effect only |
| 976 | * the next time the popup is shown. |
| 977 | * |
| 978 | * @param overlapAnchor Whether the popup should overlap its anchor. |
| 979 | * |
| 980 | * @see #getOverlapAnchor() |
| 981 | * @see #isShowing() |
| 982 | */ |
| 983 | public void setOverlapAnchor(boolean overlapAnchor) { |
| 984 | mOverlapAnchor = overlapAnchor; |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * Returns whether the popup window should overlap its anchor view when |
| 989 | * displayed as a drop-down. |
| 990 | * |
| 991 | * @return Whether the popup should overlap its anchor. |
| 992 | * |
| 993 | * @see #setOverlapAnchor(boolean) |
| 994 | */ |
| 995 | public boolean getOverlapAnchor() { |
| 996 | return mOverlapAnchor; |
| 997 | } |
| 998 | |
| 999 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | * <p>Indicate whether this popup window is showing on screen.</p> |
| 1001 | * |
| 1002 | * @return true if the popup is showing, false otherwise |
| 1003 | */ |
| 1004 | public boolean isShowing() { |
| 1005 | return mIsShowing; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * <p> |
| 1010 | * Display the content view in a popup window at the specified location. If the popup window |
| 1011 | * cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams} |
| 1012 | * for more information on how gravity and the x and y parameters are related. Specifying |
| 1013 | * a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying |
| 1014 | * <code>Gravity.LEFT | Gravity.TOP</code>. |
| 1015 | * </p> |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1016 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from |
| 1018 | * @param gravity the gravity which controls the placement of the popup window |
| 1019 | * @param x the popup's x location offset |
| 1020 | * @param y the popup's y location offset |
| 1021 | */ |
| 1022 | public void showAtLocation(View parent, int gravity, int x, int y) { |
Adam Powell | 8ee6d7c | 2011-09-18 14:59:28 -0700 | [diff] [blame] | 1023 | showAtLocation(parent.getWindowToken(), gravity, x, y); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Display the content view in a popup window at the specified location. |
| 1028 | * |
| 1029 | * @param token Window token to use for creating the new window |
| 1030 | * @param gravity the gravity which controls the placement of the popup window |
| 1031 | * @param x the popup's x location offset |
| 1032 | * @param y the popup's y location offset |
| 1033 | * |
| 1034 | * @hide Internal use only. Applications should use |
| 1035 | * {@link #showAtLocation(View, int, int, int)} instead. |
| 1036 | */ |
| 1037 | public void showAtLocation(IBinder token, int gravity, int x, int y) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | if (isShowing() || mContentView == null) { |
| 1039 | return; |
| 1040 | } |
| 1041 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1042 | TransitionManager.endTransitions(mDecorView); |
| 1043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | unregisterForScrollChanged(); |
| 1045 | |
| 1046 | mIsShowing = true; |
| 1047 | mIsDropdown = false; |
| 1048 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1049 | final WindowManager.LayoutParams p = createPopupLayoutParams(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | preparePopup(p); |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1051 | |
| 1052 | // Only override the default if some gravity was specified. |
| 1053 | if (gravity != Gravity.NO_GRAVITY) { |
| 1054 | p.gravity = gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | } |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1056 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | p.x = x; |
| 1058 | p.y = y; |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1059 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | invokePopup(p); |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1064 | * Display the content view in a popup window anchored to the bottom-left |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | * corner of the anchor view. If there is not enough room on screen to show |
| 1066 | * the popup in its entirety, this method tries to find a parent scroll |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1067 | * view to scroll. If no parent scroll view can be scrolled, the |
| 1068 | * bottom-left corner of the popup is pinned at the top left corner of the |
| 1069 | * anchor view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | * |
| 1071 | * @param anchor the view on which to pin the popup window |
| 1072 | * |
| 1073 | * @see #dismiss() |
| 1074 | */ |
| 1075 | public void showAsDropDown(View anchor) { |
| 1076 | showAsDropDown(anchor, 0, 0); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1080 | * Display the content view in a popup window anchored to the bottom-left |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | * corner of the anchor view offset by the specified x and y coordinates. |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1082 | * If there is not enough room on screen to show the popup in its entirety, |
| 1083 | * this method tries to find a parent scroll view to scroll. If no parent |
| 1084 | * scroll view can be scrolled, the bottom-left corner of the popup is |
| 1085 | * pinned at the top left corner of the anchor view. |
| 1086 | * <p> |
| 1087 | * If the view later scrolls to move <code>anchor</code> to a different |
| 1088 | * location, the popup will be moved correspondingly. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | * |
| 1090 | * @param anchor the view on which to pin the popup window |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1091 | * @param xoff A horizontal offset from the anchor in pixels |
| 1092 | * @param yoff A vertical offset from the anchor in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | * |
| 1094 | * @see #dismiss() |
| 1095 | */ |
| 1096 | public void showAsDropDown(View anchor, int xoff, int yoff) { |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1097 | showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY); |
| 1098 | } |
| 1099 | |
| 1100 | /** |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1101 | * Displays the content view in a popup window anchored to the corner of |
| 1102 | * another view. The window is positioned according to the specified |
| 1103 | * gravity and offset by the specified x and y coordinates. |
| 1104 | * <p> |
| 1105 | * If there is not enough room on screen to show the popup in its entirety, |
| 1106 | * this method tries to find a parent scroll view to scroll. If no parent |
| 1107 | * view can be scrolled, the specified vertical gravity will be ignored and |
| 1108 | * the popup will anchor itself such that it is visible. |
| 1109 | * <p> |
| 1110 | * If the view later scrolls to move <code>anchor</code> to a different |
| 1111 | * location, the popup will be moved correspondingly. |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1112 | * |
| 1113 | * @param anchor the view on which to pin the popup window |
| 1114 | * @param xoff A horizontal offset from the anchor in pixels |
| 1115 | * @param yoff A vertical offset from the anchor in pixels |
| 1116 | * @param gravity Alignment of the popup relative to the anchor |
| 1117 | * |
| 1118 | * @see #dismiss() |
| 1119 | */ |
| 1120 | public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | if (isShowing() || mContentView == null) { |
| 1122 | return; |
| 1123 | } |
| 1124 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1125 | TransitionManager.endTransitions(mDecorView); |
| 1126 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1127 | registerForScrollChanged(anchor, xoff, yoff, gravity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | |
| 1129 | mIsShowing = true; |
| 1130 | mIsDropdown = true; |
| 1131 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1132 | final WindowManager.LayoutParams p = createPopupLayoutParams(anchor.getWindowToken()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | preparePopup(p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1135 | final boolean aboveAnchor = findDropDownPosition(anchor, p, xoff, yoff, gravity); |
| 1136 | updateAboveAnchor(aboveAnchor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | |
| 1138 | invokePopup(p); |
| 1139 | } |
| 1140 | |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1141 | private void updateAboveAnchor(boolean aboveAnchor) { |
| 1142 | if (aboveAnchor != mAboveAnchor) { |
| 1143 | mAboveAnchor = aboveAnchor; |
| 1144 | |
| 1145 | if (mBackground != null) { |
| 1146 | // If the background drawable provided was a StateListDrawable with above-anchor |
| 1147 | // and below-anchor states, use those. Otherwise rely on refreshDrawableState to |
| 1148 | // do the job. |
| 1149 | if (mAboveAnchorBackgroundDrawable != null) { |
| 1150 | if (mAboveAnchor) { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1151 | mDecorView.setBackground(mAboveAnchorBackgroundDrawable); |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1152 | } else { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1153 | mDecorView.setBackground(mBelowAnchorBackgroundDrawable); |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } else { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1156 | mDecorView.refreshDrawableState(); |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | /** |
| 1163 | * Indicates whether the popup is showing above (the y coordinate of the popup's bottom |
| 1164 | * is less than the y coordinate of the anchor) or below the anchor view (the y coordinate |
| 1165 | * of the popup is greater than y coordinate of the anchor's bottom). |
| 1166 | * |
| 1167 | * The value returned |
| 1168 | * by this method is meaningful only after {@link #showAsDropDown(android.view.View)} |
| 1169 | * or {@link #showAsDropDown(android.view.View, int, int)} was invoked. |
| 1170 | * |
| 1171 | * @return True if this popup is showing above the anchor view, false otherwise. |
| 1172 | */ |
| 1173 | public boolean isAboveAnchor() { |
| 1174 | return mAboveAnchor; |
| 1175 | } |
| 1176 | |
| 1177 | /** |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1178 | * Prepare the popup by embedding it into a new ViewGroup if the background |
| 1179 | * drawable is not null. If embedding is required, the layout parameters' |
| 1180 | * height is modified to take into account the background's padding. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | * |
| 1182 | * @param p the layout parameters of the popup's content view |
| 1183 | */ |
| 1184 | private void preparePopup(WindowManager.LayoutParams p) { |
Romain Guy | 448ecf5 | 2009-05-14 16:03:42 -0700 | [diff] [blame] | 1185 | if (mContentView == null || mContext == null || mWindowManager == null) { |
| 1186 | throw new IllegalStateException("You must specify a valid content view by " |
| 1187 | + "calling setContentView() before attempting to show the popup."); |
| 1188 | } |
| 1189 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1190 | // The old decor view may be transitioning out. Make sure it finishes |
| 1191 | // and cleans up before we try to create another one. |
| 1192 | if (mDecorView != null) { |
| 1193 | mDecorView.cancelTransitions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | } |
Alan Viverette | ccb11e1 | 2014-07-08 16:04:02 -0700 | [diff] [blame] | 1195 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1196 | // When a background is available, we embed the content view within |
| 1197 | // another view that owns the background drawable. |
| 1198 | final View backgroundView; |
| 1199 | if (mBackground != null) { |
| 1200 | backgroundView = createBackgroundView(mContentView); |
| 1201 | backgroundView.setBackground(mBackground); |
| 1202 | } else { |
| 1203 | backgroundView = mContentView; |
| 1204 | } |
| 1205 | |
| 1206 | mDecorView = createDecorView(backgroundView); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1207 | |
| 1208 | // The background owner should be elevated so that it casts a shadow. |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1209 | backgroundView.setElevation(mElevation); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1210 | |
| 1211 | // We may wrap that in another view, so we'll need to manually specify |
| 1212 | // the surface insets. |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1213 | final int surfaceInset = (int) Math.ceil(backgroundView.getZ() * 2); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1214 | p.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset); |
| 1215 | p.hasManualSurfaceInsets = true; |
| 1216 | |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1217 | mPopupViewInitialLayoutDirectionInherited = |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1218 | (mContentView.getRawLayoutDirection() == View.LAYOUT_DIRECTION_INHERIT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | mPopupWidth = p.width; |
| 1220 | mPopupHeight = p.height; |
| 1221 | } |
| 1222 | |
| 1223 | /** |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1224 | * Wraps a content view in a PopupViewContainer. |
| 1225 | * |
| 1226 | * @param contentView the content view to wrap |
| 1227 | * @return a PopupViewContainer that wraps the content view |
| 1228 | */ |
| 1229 | private PopupBackgroundView createBackgroundView(View contentView) { |
| 1230 | final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams(); |
| 1231 | final int height; |
| 1232 | if (layoutParams != null && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) { |
| 1233 | height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 1234 | } else { |
| 1235 | height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 1236 | } |
| 1237 | |
| 1238 | final PopupBackgroundView backgroundView = new PopupBackgroundView(mContext); |
| 1239 | final PopupBackgroundView.LayoutParams listParams = new PopupBackgroundView.LayoutParams( |
| 1240 | ViewGroup.LayoutParams.MATCH_PARENT, height); |
| 1241 | backgroundView.addView(contentView, listParams); |
| 1242 | |
| 1243 | return backgroundView; |
| 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * Wraps a content view in a FrameLayout. |
| 1248 | * |
| 1249 | * @param contentView the content view to wrap |
| 1250 | * @return a FrameLayout that wraps the content view |
| 1251 | */ |
| 1252 | private PopupDecorView createDecorView(View contentView) { |
| 1253 | final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams(); |
| 1254 | final int height; |
| 1255 | if (layoutParams != null && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) { |
| 1256 | height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 1257 | } else { |
| 1258 | height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 1259 | } |
| 1260 | |
| 1261 | final PopupDecorView decorView = new PopupDecorView(mContext); |
| 1262 | decorView.addView(contentView, ViewGroup.LayoutParams.MATCH_PARENT, height); |
| 1263 | decorView.setClipChildren(false); |
| 1264 | decorView.setClipToPadding(false); |
| 1265 | |
| 1266 | return decorView; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | * <p>Invoke the popup window by adding the content view to the window |
| 1271 | * manager.</p> |
| 1272 | * |
| 1273 | * <p>The content view must be non-null when this method is invoked.</p> |
| 1274 | * |
| 1275 | * @param p the layout parameters of the popup's content view |
| 1276 | */ |
| 1277 | private void invokePopup(WindowManager.LayoutParams p) { |
Romain Guy | 0c0b768 | 2011-05-16 11:54:09 -0700 | [diff] [blame] | 1278 | if (mContext != null) { |
| 1279 | p.packageName = mContext.getPackageName(); |
| 1280 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1281 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1282 | final PopupDecorView decorView = mDecorView; |
| 1283 | decorView.setFitsSystemWindows(mLayoutInsetDecor); |
| 1284 | decorView.requestEnterTransition(mEnterTransition); |
| 1285 | |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1286 | setLayoutDirectionFromAnchor(); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1287 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1288 | mWindowManager.addView(decorView, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1291 | private void setLayoutDirectionFromAnchor() { |
| 1292 | if (mAnchor != null) { |
| 1293 | View anchor = mAnchor.get(); |
| 1294 | if (anchor != null && mPopupViewInitialLayoutDirectionInherited) { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1295 | mDecorView.setLayoutDirection(anchor.getLayoutDirection()); |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | /** |
| 1301 | * <p>Generate the layout parameters for the popup window.</p> |
| 1302 | * |
| 1303 | * @param token the window token used to bind the popup's window |
| 1304 | * |
| 1305 | * @return the layout parameters to pass to the window manager |
| 1306 | */ |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1307 | private WindowManager.LayoutParams createPopupLayoutParams(IBinder token) { |
| 1308 | final WindowManager.LayoutParams p = new WindowManager.LayoutParams(); |
| 1309 | |
| 1310 | // These gravity settings put the view at the top left corner of the |
| 1311 | // screen. The view is then positioned to the appropriate location by |
| 1312 | // setting the x and y offsets to match the anchor's bottom-left |
| 1313 | // corner. |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 1314 | p.gravity = Gravity.START | Gravity.TOP; |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1315 | p.flags = computeFlags(p.flags); |
| 1316 | p.type = mWindowLayoutType; |
| 1317 | p.token = token; |
| 1318 | p.softInputMode = mSoftInputMode; |
| 1319 | p.windowAnimations = computeAnimationResource(); |
| 1320 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | if (mBackground != null) { |
| 1322 | p.format = mBackground.getOpacity(); |
| 1323 | } else { |
| 1324 | p.format = PixelFormat.TRANSLUCENT; |
| 1325 | } |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1326 | |
| 1327 | if (mHeightMode < 0) { |
| 1328 | p.height = mLastHeight = mHeightMode; |
| 1329 | } else { |
| 1330 | p.height = mLastHeight = mHeight; |
| 1331 | } |
| 1332 | |
| 1333 | if (mWidthMode < 0) { |
| 1334 | p.width = mLastWidth = mWidthMode; |
| 1335 | } else { |
| 1336 | p.width = mLastWidth = mWidth; |
| 1337 | } |
| 1338 | |
| 1339 | // Used for debugging. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1340 | p.setTitle("PopupWindow:" + Integer.toHexString(hashCode())); |
| 1341 | |
| 1342 | return p; |
| 1343 | } |
| 1344 | |
| 1345 | private int computeFlags(int curFlags) { |
| 1346 | curFlags &= ~( |
| 1347 | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES | |
| 1348 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | |
| 1349 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | |
| 1350 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | |
| 1351 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 1352 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | |
| 1353 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1354 | if(mIgnoreCheekPress) { |
| 1355 | curFlags |= WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES; |
| 1356 | } |
| 1357 | if (!mFocusable) { |
| 1358 | curFlags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 1359 | if (mInputMethodMode == INPUT_METHOD_NEEDED) { |
| 1360 | curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 1361 | } |
| 1362 | } else if (mInputMethodMode == INPUT_METHOD_NOT_NEEDED) { |
| 1363 | curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 1364 | } |
| 1365 | if (!mTouchable) { |
| 1366 | curFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 1367 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1368 | if (mOutsideTouchable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | curFlags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; |
| 1370 | } |
| 1371 | if (!mClippingEnabled) { |
| 1372 | curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; |
| 1373 | } |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 1374 | if (isSplitTouchEnabled()) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1375 | curFlags |= WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; |
| 1376 | } |
Adam Powell | ba0a2c3 | 2010-09-28 17:41:23 -0700 | [diff] [blame] | 1377 | if (mLayoutInScreen) { |
| 1378 | curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
| 1379 | } |
Adam Powell | 0bd1d0a | 2011-07-22 19:35:06 -0700 | [diff] [blame] | 1380 | if (mLayoutInsetDecor) { |
| 1381 | curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 1382 | } |
Adam Powell | e0b6cd1 | 2011-09-28 22:06:11 -0700 | [diff] [blame] | 1383 | if (mNotTouchModal) { |
| 1384 | curFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; |
| 1385 | } |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 1386 | if (mAttachedInDecor) { |
| 1387 | curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR; |
| 1388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | return curFlags; |
| 1390 | } |
Wale Ogunwale | 393b1c1 | 2014-10-18 16:22:01 -0700 | [diff] [blame] | 1391 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | private int computeAnimationResource() { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1393 | if (mAnimationStyle == ANIMATION_STYLE_DEFAULT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1394 | if (mIsDropdown) { |
| 1395 | return mAboveAnchor |
| 1396 | ? com.android.internal.R.style.Animation_DropDownUp |
| 1397 | : com.android.internal.R.style.Animation_DropDownDown; |
| 1398 | } |
| 1399 | return 0; |
| 1400 | } |
| 1401 | return mAnimationStyle; |
| 1402 | } |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | /** |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1405 | * Positions the popup window on screen. When the popup window is too tall |
| 1406 | * to fit under the anchor, a parent scroll view is seeked and scrolled up |
| 1407 | * to reclaim space. If scrolling is not possible or not enough, the popup |
| 1408 | * window gets moved on top of the anchor. |
| 1409 | * <p> |
| 1410 | * The height must have been set on the layout parameters prior to calling |
| 1411 | * this method. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1412 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | * @param anchor the view on which the popup window must be anchored |
| 1414 | * @param p the layout parameters used to display the drop down |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1415 | * @param xoff horizontal offset used to adjust for background padding |
| 1416 | * @param yoff vertical offset used to adjust for background padding |
| 1417 | * @param gravity horizontal gravity specifying popup alignment |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | * @return true if the popup is translated upwards to fit on screen |
| 1419 | */ |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1420 | private boolean findDropDownPosition(View anchor, WindowManager.LayoutParams p, int xoff, |
| 1421 | int yoff, int gravity) { |
Adam Powell | 62e2bde | 2011-08-15 15:50:05 -0700 | [diff] [blame] | 1422 | final int anchorHeight = anchor.getHeight(); |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1423 | final int anchorWidth = anchor.getWidth(); |
| 1424 | if (mOverlapAnchor) { |
| 1425 | yoff -= anchorHeight; |
| 1426 | } |
| 1427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | anchor.getLocationInWindow(mDrawingLocation); |
| 1429 | p.x = mDrawingLocation[0] + xoff; |
Adam Powell | 62e2bde | 2011-08-15 15:50:05 -0700 | [diff] [blame] | 1430 | p.y = mDrawingLocation[1] + anchorHeight + yoff; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1431 | |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1432 | final int hgrav = Gravity.getAbsoluteGravity(gravity, anchor.getLayoutDirection()) |
| 1433 | & Gravity.HORIZONTAL_GRAVITY_MASK; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1434 | if (hgrav == Gravity.RIGHT) { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1435 | // Flip the location to align the right sides of the popup and |
| 1436 | // anchor instead of left. |
| 1437 | p.x -= mPopupWidth - anchorWidth; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1438 | } |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1439 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | boolean onTop = false; |
| 1441 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1442 | p.gravity = Gravity.LEFT | Gravity.TOP; |
| 1443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | anchor.getLocationOnScreen(mScreenLocation); |
| 1445 | final Rect displayFrame = new Rect(); |
| 1446 | anchor.getWindowVisibleDisplayFrame(displayFrame); |
Adam Powell | 62e2bde | 2011-08-15 15:50:05 -0700 | [diff] [blame] | 1447 | |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1448 | final int screenY = mScreenLocation[1] + anchorHeight + yoff; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | final View root = anchor.getRootView(); |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1450 | if (screenY + mPopupHeight > displayFrame.bottom |
| 1451 | || p.x + mPopupWidth - root.getWidth() > 0) { |
| 1452 | // If the drop down disappears at the bottom of the screen, we try |
| 1453 | // to scroll a parent scrollview or move the drop down back up on |
| 1454 | // top of the edit box. |
Adam Powell | b7c1b20 | 2011-02-17 12:03:09 -0800 | [diff] [blame] | 1455 | if (mAllowScrollingAnchorParent) { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1456 | final int scrollX = anchor.getScrollX(); |
| 1457 | final int scrollY = anchor.getScrollY(); |
| 1458 | final Rect r = new Rect(scrollX, scrollY, scrollX + mPopupWidth + xoff, |
| 1459 | scrollY + mPopupHeight + anchorHeight + yoff); |
Adam Powell | b7c1b20 | 2011-02-17 12:03:09 -0800 | [diff] [blame] | 1460 | anchor.requestRectangleOnScreen(r, true); |
| 1461 | } |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1462 | |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1463 | // Now we re-evaluate the space available, and decide from that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | // whether the pop-up will go above or below the anchor. |
| 1465 | anchor.getLocationInWindow(mDrawingLocation); |
| 1466 | p.x = mDrawingLocation[0] + xoff; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1467 | p.y = mDrawingLocation[1] + anchorHeight + yoff; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1468 | |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1469 | // Preserve the gravity adjustment. |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1470 | if (hgrav == Gravity.RIGHT) { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1471 | p.x -= mPopupWidth - anchorWidth; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1472 | } |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1473 | |
| 1474 | // Determine whether there is more space above or below the anchor. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | anchor.getLocationOnScreen(mScreenLocation); |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1476 | onTop = (displayFrame.bottom - mScreenLocation[1] - anchorHeight - yoff) < |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | (mScreenLocation[1] - yoff - displayFrame.top); |
| 1478 | if (onTop) { |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1479 | p.gravity = Gravity.LEFT | Gravity.BOTTOM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | p.y = root.getHeight() - mDrawingLocation[1] + yoff; |
| 1481 | } else { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1482 | p.y = mDrawingLocation[1] + anchorHeight + yoff; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 1486 | if (mClipToScreen) { |
| 1487 | final int displayFrameWidth = displayFrame.right - displayFrame.left; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1488 | final int right = p.x + p.width; |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 1489 | if (right > displayFrameWidth) { |
| 1490 | p.x -= right - displayFrameWidth; |
| 1491 | } |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1492 | |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 1493 | if (p.x < displayFrame.left) { |
| 1494 | p.x = displayFrame.left; |
| 1495 | p.width = Math.min(p.width, displayFrameWidth); |
| 1496 | } |
| 1497 | |
Adam Powell | 5f83a60 | 2011-01-19 17:58:04 -0800 | [diff] [blame] | 1498 | if (onTop) { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1499 | final int popupTop = mScreenLocation[1] + yoff - mPopupHeight; |
Adam Powell | 5f83a60 | 2011-01-19 17:58:04 -0800 | [diff] [blame] | 1500 | if (popupTop < 0) { |
| 1501 | p.y += popupTop; |
| 1502 | } |
| 1503 | } else { |
| 1504 | p.y = Math.max(p.y, displayFrame.top); |
| 1505 | } |
Adam Powell | 56c2d33 | 2010-11-05 20:03:03 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | p.gravity |= Gravity.DISPLAY_CLIP_VERTICAL; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 1509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | return onTop; |
| 1511 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1513 | /** |
| 1514 | * Returns the maximum height that is available for the popup to be |
| 1515 | * completely shown. It is recommended that this height be the maximum for |
| 1516 | * the popup's height, otherwise it is possible that the popup will be |
| 1517 | * clipped. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1518 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | * @param anchor The view on which the popup window must be anchored. |
| 1520 | * @return The maximum available height for the popup to be completely |
| 1521 | * shown. |
| 1522 | */ |
| 1523 | public int getMaxAvailableHeight(View anchor) { |
| 1524 | return getMaxAvailableHeight(anchor, 0); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * Returns the maximum height that is available for the popup to be |
| 1529 | * completely shown. It is recommended that this height be the maximum for |
| 1530 | * the popup's height, otherwise it is possible that the popup will be |
| 1531 | * clipped. |
| 1532 | * |
| 1533 | * @param anchor The view on which the popup window must be anchored. |
| 1534 | * @param yOffset y offset from the view's bottom edge |
| 1535 | * @return The maximum available height for the popup to be completely |
| 1536 | * shown. |
| 1537 | */ |
| 1538 | public int getMaxAvailableHeight(View anchor, int yOffset) { |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1539 | return getMaxAvailableHeight(anchor, yOffset, false); |
| 1540 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1541 | |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1542 | /** |
| 1543 | * Returns the maximum height that is available for the popup to be |
| 1544 | * completely shown, optionally ignoring any bottom decorations such as |
| 1545 | * the input method. It is recommended that this height be the maximum for |
| 1546 | * the popup's height, otherwise it is possible that the popup will be |
| 1547 | * clipped. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1548 | * |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1549 | * @param anchor The view on which the popup window must be anchored. |
| 1550 | * @param yOffset y offset from the view's bottom edge |
| 1551 | * @param ignoreBottomDecorations if true, the height returned will be |
| 1552 | * all the way to the bottom of the display, ignoring any |
| 1553 | * bottom decorations |
| 1554 | * @return The maximum available height for the popup to be completely |
| 1555 | * shown. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1556 | * |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1557 | * @hide Pending API council approval. |
| 1558 | */ |
| 1559 | public int getMaxAvailableHeight(View anchor, int yOffset, boolean ignoreBottomDecorations) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | final Rect displayFrame = new Rect(); |
| 1561 | anchor.getWindowVisibleDisplayFrame(displayFrame); |
| 1562 | |
| 1563 | final int[] anchorPos = mDrawingLocation; |
| 1564 | anchor.getLocationOnScreen(anchorPos); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1565 | |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1566 | int bottomEdge = displayFrame.bottom; |
| 1567 | if (ignoreBottomDecorations) { |
Adam Powell | a7287f4 | 2010-08-17 21:17:04 -0700 | [diff] [blame] | 1568 | Resources res = anchor.getContext().getResources(); |
Adam Powell | 3f4a764 | 2011-05-20 15:56:25 -0700 | [diff] [blame] | 1569 | bottomEdge = res.getDisplayMetrics().heightPixels; |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1570 | } |
| 1571 | final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | final int distanceToTop = anchorPos[1] - displayFrame.top + yOffset; |
| 1573 | |
| 1574 | // anchorPos[1] is distance from anchor to top of screen |
| 1575 | int returnedHeight = Math.max(distanceToBottom, distanceToTop); |
| 1576 | if (mBackground != null) { |
| 1577 | mBackground.getPadding(mTempRect); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1578 | returnedHeight -= mTempRect.top + mTempRect.bottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1580 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | return returnedHeight; |
| 1582 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | /** |
Alan Viverette | 7878edf | 2015-02-03 15:49:18 -0800 | [diff] [blame] | 1585 | * Disposes of the popup window. This method can be invoked only after |
| 1586 | * {@link #showAsDropDown(android.view.View)} has been executed. Failing |
| 1587 | * that, calling this method will have no effect. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | * |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1589 | * @see #showAsDropDown(android.view.View) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | */ |
| 1591 | public void dismiss() { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1592 | if (!isShowing() || mIsTransitioningToDismiss) { |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1593 | return; |
| 1594 | } |
Svetoslav Ganov | 06f938e | 2011-11-10 14:31:37 -0800 | [diff] [blame] | 1595 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1596 | final PopupDecorView decorView = mDecorView; |
| 1597 | final View contentView = mContentView; |
| 1598 | |
| 1599 | final ViewGroup contentHolder; |
| 1600 | final ViewParent contentParent = contentView.getParent(); |
| 1601 | if (contentParent instanceof ViewGroup) { |
| 1602 | contentHolder = ((ViewGroup) contentParent); |
| 1603 | } else { |
| 1604 | contentHolder = null; |
| 1605 | } |
| 1606 | |
| 1607 | // Ensure any ongoing or pending transitions are canceled. |
| 1608 | decorView.cancelTransitions(); |
| 1609 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1610 | unregisterForScrollChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1612 | mIsShowing = false; |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1613 | mIsTransitioningToDismiss = true; |
Craig Mautner | b82d074 | 2012-05-23 08:48:39 -0700 | [diff] [blame] | 1614 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1615 | if (mExitTransition != null && decorView.isLaidOut()) { |
| 1616 | decorView.startExitTransition(mExitTransition, new TransitionListenerAdapter() { |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1617 | @Override |
| 1618 | public void onTransitionEnd(Transition transition) { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1619 | dismissImmediate(decorView, contentHolder, contentView); |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1620 | } |
| 1621 | }); |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1622 | } else { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1623 | dismissImmediate(decorView, contentHolder, contentView); |
Alan Viverette | 7878edf | 2015-02-03 15:49:18 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | if (mOnDismissListener != null) { |
| 1627 | mOnDismissListener.onDismiss(); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | /** |
| 1632 | * Removes the popup from the window manager and tears down the supporting |
| 1633 | * view hierarchy, if necessary. |
| 1634 | */ |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1635 | private void dismissImmediate(View decorView, ViewGroup contentHolder, View contentView) { |
| 1636 | // If this method gets called and the decor view doesn't have a parent, |
| 1637 | // then it was either never added or was already removed. That should |
| 1638 | // never happen, but it's worth checking to avoid potential crashes. |
| 1639 | if (decorView.getParent() != null) { |
| 1640 | mWindowManager.removeViewImmediate(decorView); |
Alan Viverette | df4639a0 | 2015-03-02 12:40:34 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1643 | if (contentHolder != null) { |
| 1644 | contentHolder.removeView(contentView); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | } |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1646 | |
| 1647 | // This needs to stay until after all transitions have ended since we |
| 1648 | // need the reference to cancel transitions in preparePopup(). |
| 1649 | mDecorView = null; |
| 1650 | mIsTransitioningToDismiss = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /** |
| 1654 | * Sets the listener to be called when the window is dismissed. |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1655 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | * @param onDismissListener The listener. |
| 1657 | */ |
| 1658 | public void setOnDismissListener(OnDismissListener onDismissListener) { |
| 1659 | mOnDismissListener = onDismissListener; |
| 1660 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1661 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | /** |
| 1663 | * Updates the state of the popup window, if it is currently being displayed, |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1664 | * from the currently set state. |
| 1665 | * <p> |
| 1666 | * This includes: |
| 1667 | * <ul> |
| 1668 | * <li>{@link #setClippingEnabled(boolean)}</li> |
| 1669 | * <li>{@link #setFocusable(boolean)}</li> |
| 1670 | * <li>{@link #setIgnoreCheekPress()}</li> |
| 1671 | * <li>{@link #setInputMethodMode(int)}</li> |
| 1672 | * <li>{@link #setTouchable(boolean)}</li> |
| 1673 | * <li>{@link #setAnimationStyle(int)}</li> |
| 1674 | * </ul> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1675 | */ |
| 1676 | public void update() { |
| 1677 | if (!isShowing() || mContentView == null) { |
| 1678 | return; |
| 1679 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1680 | |
| 1681 | final WindowManager.LayoutParams p = |
| 1682 | (WindowManager.LayoutParams) mDecorView.getLayoutParams(); |
| 1683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | boolean update = false; |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1685 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | final int newAnim = computeAnimationResource(); |
| 1687 | if (newAnim != p.windowAnimations) { |
| 1688 | p.windowAnimations = newAnim; |
| 1689 | update = true; |
| 1690 | } |
| 1691 | |
| 1692 | final int newFlags = computeFlags(p.flags); |
| 1693 | if (newFlags != p.flags) { |
| 1694 | p.flags = newFlags; |
| 1695 | update = true; |
| 1696 | } |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | if (update) { |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1699 | setLayoutDirectionFromAnchor(); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1700 | mWindowManager.updateViewLayout(mDecorView, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1701 | } |
| 1702 | } |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 1703 | |
| 1704 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1705 | * Updates the dimension of the popup window. |
| 1706 | * <p> |
| 1707 | * Calling this function also updates the window with the current popup |
| 1708 | * state as described for {@link #update()}. |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 1709 | * |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1710 | * @param width the new width, must be >= 0 or -1 to ignore |
| 1711 | * @param height the new height, must be >= 0 or -1 to ignore |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 1712 | */ |
| 1713 | public void update(int width, int height) { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1714 | final WindowManager.LayoutParams p = |
| 1715 | (WindowManager.LayoutParams) mDecorView.getLayoutParams(); |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 1716 | update(p.x, p.y, width, height, false); |
| 1717 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1720 | * Updates the position and the dimension of the popup window. |
| 1721 | * <p> |
| 1722 | * Width and height can be set to -1 to update location only. Calling this |
| 1723 | * function also updates the window with the current popup state as |
| 1724 | * described for {@link #update()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1725 | * |
| 1726 | * @param x the new x location |
| 1727 | * @param y the new y location |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1728 | * @param width the new width, must be >= 0 or -1 to ignore |
| 1729 | * @param height the new height, must be >= 0 or -1 to ignore |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1730 | */ |
| 1731 | public void update(int x, int y, int width, int height) { |
| 1732 | update(x, y, width, height, false); |
| 1733 | } |
| 1734 | |
| 1735 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1736 | * Updates the position and the dimension of the popup window. |
| 1737 | * <p> |
| 1738 | * Width and height can be set to -1 to update location only. Calling this |
| 1739 | * function also updates the window with the current popup state as |
| 1740 | * described for {@link #update()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | * |
| 1742 | * @param x the new x location |
| 1743 | * @param y the new y location |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1744 | * @param width the new width, must be >= 0 or -1 to ignore |
| 1745 | * @param height the new height, must be >= 0 or -1 to ignore |
| 1746 | * @param force {@code true} to reposition the window even if the specified |
| 1747 | * position already seems to correspond to the LayoutParams, |
| 1748 | * {@code false} to only reposition if needed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1749 | */ |
| 1750 | public void update(int x, int y, int width, int height, boolean force) { |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1751 | if (width >= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | mLastWidth = width; |
| 1753 | setWidth(width); |
| 1754 | } |
| 1755 | |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1756 | if (height >= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | mLastHeight = height; |
| 1758 | setHeight(height); |
| 1759 | } |
| 1760 | |
| 1761 | if (!isShowing() || mContentView == null) { |
| 1762 | return; |
| 1763 | } |
| 1764 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1765 | final WindowManager.LayoutParams p = |
| 1766 | (WindowManager.LayoutParams) mDecorView.getLayoutParams(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | |
| 1768 | boolean update = force; |
| 1769 | |
| 1770 | final int finalWidth = mWidthMode < 0 ? mWidthMode : mLastWidth; |
| 1771 | if (width != -1 && p.width != finalWidth) { |
| 1772 | p.width = mLastWidth = finalWidth; |
| 1773 | update = true; |
| 1774 | } |
| 1775 | |
| 1776 | final int finalHeight = mHeightMode < 0 ? mHeightMode : mLastHeight; |
| 1777 | if (height != -1 && p.height != finalHeight) { |
| 1778 | p.height = mLastHeight = finalHeight; |
| 1779 | update = true; |
| 1780 | } |
| 1781 | |
| 1782 | if (p.x != x) { |
| 1783 | p.x = x; |
| 1784 | update = true; |
| 1785 | } |
| 1786 | |
| 1787 | if (p.y != y) { |
| 1788 | p.y = y; |
| 1789 | update = true; |
| 1790 | } |
| 1791 | |
| 1792 | final int newAnim = computeAnimationResource(); |
| 1793 | if (newAnim != p.windowAnimations) { |
| 1794 | p.windowAnimations = newAnim; |
| 1795 | update = true; |
| 1796 | } |
| 1797 | |
| 1798 | final int newFlags = computeFlags(p.flags); |
| 1799 | if (newFlags != p.flags) { |
| 1800 | p.flags = newFlags; |
| 1801 | update = true; |
| 1802 | } |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 1803 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | if (update) { |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1805 | setLayoutDirectionFromAnchor(); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1806 | mWindowManager.updateViewLayout(mDecorView, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1811 | * Updates the position and the dimension of the popup window. |
| 1812 | * <p> |
| 1813 | * Calling this function also updates the window with the current popup |
| 1814 | * state as described for {@link #update()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1815 | * |
| 1816 | * @param anchor the popup's anchor view |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1817 | * @param width the new width, must be >= 0 or -1 to ignore |
| 1818 | * @param height the new height, must be >= 0 or -1 to ignore |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1819 | */ |
| 1820 | public void update(View anchor, int width, int height) { |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1821 | update(anchor, false, 0, 0, true, width, height); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | /** |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1825 | * Updates the position and the dimension of the popup window. |
| 1826 | * <p> |
| 1827 | * Width and height can be set to -1 to update location only. Calling this |
| 1828 | * function also updates the window with the current popup state as |
| 1829 | * described for {@link #update()}. |
| 1830 | * <p> |
| 1831 | * If the view later scrolls to move {@code anchor} to a different |
| 1832 | * location, the popup will be moved correspondingly. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | * |
| 1834 | * @param anchor the popup's anchor view |
| 1835 | * @param xoff x offset from the view's left edge |
| 1836 | * @param yoff y offset from the view's bottom edge |
Alan Viverette | 259c284 | 2015-03-22 17:39:39 -0700 | [diff] [blame] | 1837 | * @param width the new width, must be >= 0 or -1 to ignore |
| 1838 | * @param height the new height, must be >= 0 or -1 to ignore |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | */ |
| 1840 | public void update(View anchor, int xoff, int yoff, int width, int height) { |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1841 | update(anchor, true, xoff, yoff, true, width, height); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | private void update(View anchor, boolean updateLocation, int xoff, int yoff, |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1845 | boolean updateDimension, int width, int height) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | if (!isShowing() || mContentView == null) { |
| 1848 | return; |
| 1849 | } |
| 1850 | |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1851 | final WeakReference<View> oldAnchor = mAnchor; |
| 1852 | final boolean needsUpdate = updateLocation && (mAnchorXoff != xoff || mAnchorYoff != yoff); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 1853 | if (oldAnchor == null || oldAnchor.get() != anchor || (needsUpdate && !mIsDropdown)) { |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1854 | registerForScrollChanged(anchor, xoff, yoff, mAnchoredGravity); |
Gilles Debunne | 81f0808 | 2011-02-17 14:07:19 -0800 | [diff] [blame] | 1855 | } else if (needsUpdate) { |
| 1856 | // No need to register again if this is a DropDown, showAsDropDown already did. |
| 1857 | mAnchorXoff = xoff; |
| 1858 | mAnchorYoff = yoff; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1861 | if (updateDimension) { |
| 1862 | if (width == -1) { |
| 1863 | width = mPopupWidth; |
| 1864 | } else { |
| 1865 | mPopupWidth = width; |
| 1866 | } |
| 1867 | if (height == -1) { |
| 1868 | height = mPopupHeight; |
| 1869 | } else { |
| 1870 | mPopupHeight = height; |
| 1871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1872 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1873 | |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1874 | final WindowManager.LayoutParams p = |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1875 | (WindowManager.LayoutParams) mDecorView.getLayoutParams(); |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1876 | final int x = p.x; |
| 1877 | final int y = p.y; |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1878 | if (updateLocation) { |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 1879 | updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff, mAnchoredGravity)); |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1880 | } else { |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1881 | updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff, |
| 1882 | mAnchoredGravity)); |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1883 | } |
Fabrice Di Meglio | b003e28 | 2012-10-17 17:20:19 -0700 | [diff] [blame] | 1884 | |
Romain Guy | 3e14168 | 2010-03-08 17:44:40 -0800 | [diff] [blame] | 1885 | update(p.x, p.y, width, height, x != p.x || y != p.y); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | /** |
| 1889 | * Listener that is called when this popup window is dismissed. |
| 1890 | */ |
| 1891 | public interface OnDismissListener { |
| 1892 | /** |
| 1893 | * Called when this popup window is dismissed. |
| 1894 | */ |
| 1895 | public void onDismiss(); |
| 1896 | } |
| 1897 | |
| 1898 | private void unregisterForScrollChanged() { |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1899 | final WeakReference<View> anchorRef = mAnchor; |
| 1900 | final View anchor = anchorRef == null ? null : anchorRef.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | if (anchor != null) { |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1902 | final ViewTreeObserver vto = anchor.getViewTreeObserver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | vto.removeOnScrollChangedListener(mOnScrollChangedListener); |
| 1904 | } |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | mAnchor = null; |
| 1907 | } |
| 1908 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1909 | private void registerForScrollChanged(View anchor, int xoff, int yoff, int gravity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | unregisterForScrollChanged(); |
| 1911 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1912 | mAnchor = new WeakReference<>(anchor); |
Alan Viverette | e025ed2 | 2015-02-02 11:27:21 -0800 | [diff] [blame] | 1913 | |
| 1914 | final ViewTreeObserver vto = anchor.getViewTreeObserver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | if (vto != null) { |
| 1916 | vto.addOnScrollChangedListener(mOnScrollChangedListener); |
| 1917 | } |
| 1918 | |
| 1919 | mAnchorXoff = xoff; |
| 1920 | mAnchorYoff = yoff; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 1921 | mAnchoredGravity = gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1924 | private class PopupDecorView extends FrameLayout { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1925 | private TransitionListenerAdapter mPendingExitListener; |
| 1926 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1927 | public PopupDecorView(Context context) { |
| 1928 | super(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | @Override |
| 1932 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 1933 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
Per Andersson | 4ae02b3 | 2011-01-17 11:16:23 +0100 | [diff] [blame] | 1934 | if (getKeyDispatcherState() == null) { |
| 1935 | return super.dispatchKeyEvent(event); |
| 1936 | } |
| 1937 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1938 | if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { |
| 1939 | final KeyEvent.DispatcherState state = getKeyDispatcherState(); |
Jeff Brown | b3ea922 | 2011-01-10 16:26:36 -0800 | [diff] [blame] | 1940 | if (state != null) { |
| 1941 | state.startTracking(event, this); |
| 1942 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1943 | return true; |
Jeff Brown | b3ea922 | 2011-01-10 16:26:36 -0800 | [diff] [blame] | 1944 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1945 | final KeyEvent.DispatcherState state = getKeyDispatcherState(); |
Jeff Brown | b3ea922 | 2011-01-10 16:26:36 -0800 | [diff] [blame] | 1946 | if (state != null && state.isTracking(event) && !event.isCanceled()) { |
| 1947 | dismiss(); |
| 1948 | return true; |
| 1949 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1950 | } |
| 1951 | return super.dispatchKeyEvent(event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | } else { |
| 1953 | return super.dispatchKeyEvent(event); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | @Override |
| 1958 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 1959 | if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) { |
| 1960 | return true; |
| 1961 | } |
| 1962 | return super.dispatchTouchEvent(ev); |
| 1963 | } |
| 1964 | |
| 1965 | @Override |
| 1966 | public boolean onTouchEvent(MotionEvent event) { |
| 1967 | final int x = (int) event.getX(); |
| 1968 | final int y = (int) event.getY(); |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 1969 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1970 | if ((event.getAction() == MotionEvent.ACTION_DOWN) |
| 1971 | && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) { |
| 1972 | dismiss(); |
| 1973 | return true; |
| 1974 | } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { |
| 1975 | dismiss(); |
| 1976 | return true; |
| 1977 | } else { |
| 1978 | return super.onTouchEvent(event); |
| 1979 | } |
| 1980 | } |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 1981 | |
| 1982 | /** |
| 1983 | * Requests that an enter transition run after the next layout pass. |
| 1984 | */ |
| 1985 | public void requestEnterTransition(Transition transition) { |
| 1986 | final ViewTreeObserver observer = getViewTreeObserver(); |
| 1987 | if (observer != null && transition != null) { |
| 1988 | final Transition enterTransition = transition.clone(); |
| 1989 | |
| 1990 | // Postpone the enter transition after the first layout pass. |
| 1991 | observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { |
| 1992 | @Override |
| 1993 | public void onGlobalLayout() { |
| 1994 | final ViewTreeObserver observer = getViewTreeObserver(); |
| 1995 | if (observer != null) { |
| 1996 | observer.removeOnGlobalLayoutListener(this); |
| 1997 | } |
| 1998 | |
| 1999 | startEnterTransition(enterTransition); |
| 2000 | } |
| 2001 | }); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | /** |
| 2006 | * Starts the pending enter transition, if one is set. |
| 2007 | */ |
| 2008 | private void startEnterTransition(Transition enterTransition) { |
| 2009 | final int count = getChildCount(); |
| 2010 | for (int i = 0; i < count; i++) { |
| 2011 | final View child = getChildAt(i); |
| 2012 | enterTransition.addTarget(child); |
| 2013 | child.setVisibility(View.INVISIBLE); |
| 2014 | } |
| 2015 | |
| 2016 | TransitionManager.beginDelayedTransition(this, enterTransition); |
| 2017 | |
| 2018 | for (int i = 0; i < count; i++) { |
| 2019 | final View child = getChildAt(i); |
| 2020 | child.setVisibility(View.VISIBLE); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * Starts an exit transition immediately. |
| 2026 | * <p> |
| 2027 | * <strong>Note:</strong> The transition listener is guaranteed to have |
| 2028 | * its {@code onTransitionEnd} method called even if the transition |
| 2029 | * never starts; however, it may be called with a {@code null} argument. |
| 2030 | */ |
| 2031 | public void startExitTransition(Transition transition, final TransitionListener listener) { |
| 2032 | if (transition == null) { |
| 2033 | return; |
| 2034 | } |
| 2035 | |
| 2036 | // The exit listener MUST be called for cleanup, even if the |
| 2037 | // transition never starts or ends. Stash it for later. |
| 2038 | mPendingExitListener = new TransitionListenerAdapter() { |
| 2039 | @Override |
| 2040 | public void onTransitionEnd(Transition transition) { |
| 2041 | listener.onTransitionEnd(transition); |
| 2042 | |
| 2043 | // The listener was called. Our job here is done. |
| 2044 | mPendingExitListener = null; |
| 2045 | } |
| 2046 | }; |
| 2047 | |
| 2048 | final Transition exitTransition = transition.clone(); |
| 2049 | exitTransition.addListener(mPendingExitListener); |
| 2050 | |
| 2051 | final int count = getChildCount(); |
| 2052 | for (int i = 0; i < count; i++) { |
| 2053 | final View child = getChildAt(i); |
| 2054 | exitTransition.addTarget(child); |
| 2055 | } |
| 2056 | |
| 2057 | TransitionManager.beginDelayedTransition(this, exitTransition); |
| 2058 | |
| 2059 | for (int i = 0; i < count; i++) { |
| 2060 | final View child = getChildAt(i); |
| 2061 | child.setVisibility(View.INVISIBLE); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | /** |
| 2066 | * Cancels all pending or current transitions. |
| 2067 | */ |
| 2068 | public void cancelTransitions() { |
| 2069 | TransitionManager.endTransitions(this); |
| 2070 | |
| 2071 | if (mPendingExitListener != null) { |
| 2072 | mPendingExitListener.onTransitionEnd(null); |
| 2073 | } |
| 2074 | } |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 2075 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2076 | |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 2077 | private class PopupBackgroundView extends FrameLayout { |
| 2078 | public PopupBackgroundView(Context context) { |
| 2079 | super(context); |
| 2080 | } |
| 2081 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2082 | @Override |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 2083 | protected int[] onCreateDrawableState(int extraSpace) { |
| 2084 | if (mAboveAnchor) { |
| 2085 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 2086 | View.mergeDrawableStates(drawableState, ABOVE_ANCHOR_STATE_SET); |
| 2087 | return drawableState; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2088 | } else { |
Alan Viverette | 5435a30 | 2015-01-29 10:25:34 -0800 | [diff] [blame] | 2089 | return super.onCreateDrawableState(extraSpace); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2090 | } |
| 2091 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2092 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | } |