This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
PopupWindow.java
2784 lines (2454 loc) · 101 KB
/
PopupWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_LAYOUT_CHILD_WINDOW_IN_PARENT_FRAME;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.IBinder;
import android.transition.Transition;
import android.transition.Transition.EpicenterCallback;
import android.transition.Transition.TransitionListener;
import android.transition.TransitionInflater;
import android.transition.TransitionListenerAdapter;
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.KeyboardShortcutGroup;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
import android.view.WindowManagerGlobal;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
import android.window.WindowOnBackInvokedDispatcher;
import com.android.internal.R;
import java.lang.ref.WeakReference;
import java.util.List;
/**
* <p>
* This class represents a popup window that can be used to display an
* arbitrary view. The popup window is a floating container that appears on top
* of the current activity.
* </p>
* <a name="Animation"></a>
* <h3>Animation</h3>
* <p>
* On all versions of Android, popup window enter and exit animations may be
* specified by calling {@link #setAnimationStyle(int)} and passing the
* resource ID for an animation style that defines {@code windowEnterAnimation}
* and {@code windowExitAnimation}. For example, passing
* {@link android.R.style#Animation_Dialog} will give a scale and alpha
* animation.
* </br>
* A window animation style may also be specified in the popup window's style
* XML via the {@link android.R.styleable#PopupWindow_popupAnimationStyle popupAnimationStyle}
* attribute.
* </p>
* <p>
* Starting with API 23, more complex popup window enter and exit transitions
* may be specified by calling either {@link #setEnterTransition(Transition)}
* or {@link #setExitTransition(Transition)} and passing a {@link Transition}.
* </br>
* Popup enter and exit transitions may also be specified in the popup window's
* style XML via the {@link android.R.styleable#PopupWindow_popupEnterTransition popupEnterTransition}
* and {@link android.R.styleable#PopupWindow_popupExitTransition popupExitTransition}
* attributes, respectively.
* </p>
*
* @attr ref android.R.styleable#PopupWindow_overlapAnchor
* @attr ref android.R.styleable#PopupWindow_popupAnimationStyle
* @attr ref android.R.styleable#PopupWindow_popupBackground
* @attr ref android.R.styleable#PopupWindow_popupElevation
* @attr ref android.R.styleable#PopupWindow_popupEnterTransition
* @attr ref android.R.styleable#PopupWindow_popupExitTransition
*
* @see android.widget.AutoCompleteTextView
* @see android.widget.Spinner
*/
public class PopupWindow {
/**
* Mode for {@link #setInputMethodMode(int)}: the requirements for the
* input method should be based on the focusability of the popup. That is
* if it is focusable than it needs to work with the input method, else
* it doesn't.
*/
public static final int INPUT_METHOD_FROM_FOCUSABLE = 0;
/**
* Mode for {@link #setInputMethodMode(int)}: this popup always needs to
* work with an input method, regardless of whether it is focusable. This
* means that it will always be displayed so that the user can also operate
* the input method while it is shown.
*/
public static final int INPUT_METHOD_NEEDED = 1;
/**
* Mode for {@link #setInputMethodMode(int)}: this popup never needs to
* work with an input method, regardless of whether it is focusable. This
* means that it will always be displayed to use as much space on the
* screen as needed, regardless of whether this covers the input method.
*/
public static final int INPUT_METHOD_NOT_NEEDED = 2;
private static final int DEFAULT_ANCHORED_GRAVITY = Gravity.TOP | Gravity.START;
/**
* Default animation style indicating that separate animations should be
* used for top/bottom anchoring states.
*/
private static final int ANIMATION_STYLE_DEFAULT = -1;
private final int[] mTmpDrawingLocation = new int[2];
private final int[] mTmpScreenLocation = new int[2];
private final int[] mTmpAppLocation = new int[2];
private final Rect mTempRect = new Rect();
@UnsupportedAppUsage
private Context mContext;
@UnsupportedAppUsage
private WindowManager mWindowManager;
/**
* Keeps track of popup's parent's decor view. This is needed to dispatch
* requestKeyboardShortcuts to the owning Activity.
*/
private WeakReference<View> mParentRootView;
@UnsupportedAppUsage
private boolean mIsShowing;
private boolean mIsTransitioningToDismiss;
@UnsupportedAppUsage
private boolean mIsDropdown;
/** View that handles event dispatch and content transitions. */
@UnsupportedAppUsage
private PopupDecorView mDecorView;
/** View that holds the background and may animate during a transition. */
@UnsupportedAppUsage
private View mBackgroundView;
/** The contents of the popup. May be identical to the background view. */
@UnsupportedAppUsage
private View mContentView;
private boolean mFocusable;
private int mInputMethodMode = INPUT_METHOD_FROM_FOCUSABLE;
@SoftInputModeFlags
private int mSoftInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED;
private boolean mTouchable = true;
private boolean mOutsideTouchable = false;
private boolean mClippingEnabled = true;
private int mSplitTouchEnabled = -1;
@UnsupportedAppUsage
private boolean mLayoutInScreen;
private boolean mClipToScreen;
private boolean mAllowScrollingAnchorParent = true;
private boolean mLayoutInsetDecor = false;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private boolean mNotTouchModal;
private boolean mAttachedInDecor = true;
private boolean mAttachedInDecorSet = false;
@UnsupportedAppUsage
private OnTouchListener mTouchInterceptor;
@UnsupportedAppUsage
private int mWidthMode;
private int mWidth = LayoutParams.WRAP_CONTENT;
@UnsupportedAppUsage
private int mLastWidth;
@UnsupportedAppUsage
private int mHeightMode;
private int mHeight = LayoutParams.WRAP_CONTENT;
@UnsupportedAppUsage
private int mLastHeight;
private float mElevation;
private Drawable mBackground;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
private Drawable mAboveAnchorBackgroundDrawable;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
private Drawable mBelowAnchorBackgroundDrawable;
private Transition mEnterTransition;
private Transition mExitTransition;
private Rect mEpicenterBounds;
@UnsupportedAppUsage
private boolean mAboveAnchor;
@UnsupportedAppUsage
private int mWindowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
@UnsupportedAppUsage
private OnDismissListener mOnDismissListener;
private boolean mIgnoreCheekPress = false;
@UnsupportedAppUsage
private int mAnimationStyle = ANIMATION_STYLE_DEFAULT;
private int mGravity = Gravity.NO_GRAVITY;
private static final int[] ABOVE_ANCHOR_STATE_SET = new int[] {
com.android.internal.R.attr.state_above_anchor
};
private final OnAttachStateChangeListener mOnAnchorDetachedListener =
new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
// Anchor might have been reattached in a different position.
alignToAnchor();
}
@Override
public void onViewDetachedFromWindow(View v) {
// Leave the popup in its current position.
// The anchor might become attached again.
}
};
private final OnAttachStateChangeListener mOnAnchorRootDetachedListener =
new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {}
@Override
public void onViewDetachedFromWindow(View v) {
mIsAnchorRootAttached = false;
}
};
@UnsupportedAppUsage
private WeakReference<View> mAnchor;
private WeakReference<View> mAnchorRoot;
private boolean mIsAnchorRootAttached;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private final OnScrollChangedListener mOnScrollChangedListener = this::alignToAnchor;
private final View.OnLayoutChangeListener mOnLayoutChangeListener =
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> alignToAnchor();
private int mAnchorXoff;
private int mAnchorYoff;
private int mAnchoredGravity;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private boolean mOverlapAnchor;
private boolean mPopupViewInitialLayoutDirectionInherited;
private OnBackInvokedCallback mBackCallback;
/**
* <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does provide a background.</p>
*/
public PopupWindow(Context context) {
this(context, null);
}
/**
* <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does provide a background.</p>
*/
public PopupWindow(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.popupWindowStyle);
}
/**
* <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does provide a background.</p>
*/
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
/**
* <p>Create a new, empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does not provide a background.</p>
*/
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
mContext = context;
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.PopupWindow, defStyleAttr, defStyleRes);
final Drawable bg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
mElevation = a.getDimension(R.styleable.PopupWindow_popupElevation, 0);
mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false);
// Preserve default behavior from Gingerbread. If the animation is
// undefined or explicitly specifies the Gingerbread animation style,
// use a sentinel value.
if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupAnimationStyle)) {
final int animStyle = a.getResourceId(R.styleable.PopupWindow_popupAnimationStyle, 0);
if (animStyle == R.style.Animation_PopupWindow) {
mAnimationStyle = ANIMATION_STYLE_DEFAULT;
} else {
mAnimationStyle = animStyle;
}
} else {
mAnimationStyle = ANIMATION_STYLE_DEFAULT;
}
final Transition enterTransition = getTransition(a.getResourceId(
R.styleable.PopupWindow_popupEnterTransition, 0));
final Transition exitTransition;
if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupExitTransition)) {
exitTransition = getTransition(a.getResourceId(
R.styleable.PopupWindow_popupExitTransition, 0));
} else {
exitTransition = enterTransition == null ? null : enterTransition.clone();
}
a.recycle();
setEnterTransition(enterTransition);
setExitTransition(exitTransition);
setBackgroundDrawable(bg);
}
/**
* <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*/
public PopupWindow() {
this(null, 0, 0);
}
/**
* <p>Create a new non focusable popup window which can display the
* <tt>contentView</tt>. The dimension of the window are (0,0).</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param contentView the popup's content
*/
public PopupWindow(View contentView) {
this(contentView, 0, 0);
}
/**
* <p>Create a new empty, non focusable popup window. The dimension of the
* window must be passed to this constructor.</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param width the popup's width
* @param height the popup's height
*/
public PopupWindow(int width, int height) {
this(null, width, height);
}
/**
* <p>Create a new non focusable popup window which can display the
* <tt>contentView</tt>. The dimension of the window must be passed to
* this constructor.</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param contentView the popup's content
* @param width the popup's width
* @param height the popup's height
*/
public PopupWindow(View contentView, int width, int height) {
this(contentView, width, height, false);
}
/**
* <p>Create a new popup window which can display the <tt>contentView</tt>.
* The dimension of the window must be passed to this constructor.</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param contentView the popup's content
* @param width the popup's width
* @param height the popup's height
* @param focusable true if the popup can be focused, false otherwise
*/
public PopupWindow(View contentView, int width, int height, boolean focusable) {
if (contentView != null) {
mContext = contentView.getContext();
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
setContentView(contentView);
setWidth(width);
setHeight(height);
setFocusable(focusable);
}
/**
* Sets the enter transition to be used when the popup window is shown.
*
* @param enterTransition the enter transition, or {@code null} to clear
* @see #getEnterTransition()
* @attr ref android.R.styleable#PopupWindow_popupEnterTransition
*/
public void setEnterTransition(@Nullable Transition enterTransition) {
mEnterTransition = enterTransition;
}
/**
* Returns the enter transition to be used when the popup window is shown.
*
* @return the enter transition, or {@code null} if not set
* @see #setEnterTransition(Transition)
* @attr ref android.R.styleable#PopupWindow_popupEnterTransition
*/
@Nullable
public Transition getEnterTransition() {
return mEnterTransition;
}
/**
* Sets the exit transition to be used when the popup window is dismissed.
*
* @param exitTransition the exit transition, or {@code null} to clear
* @see #getExitTransition()
* @attr ref android.R.styleable#PopupWindow_popupExitTransition
*/
public void setExitTransition(@Nullable Transition exitTransition) {
mExitTransition = exitTransition;
}
/**
* Returns the exit transition to be used when the popup window is
* dismissed.
*
* @return the exit transition, or {@code null} if not set
* @see #setExitTransition(Transition)
* @attr ref android.R.styleable#PopupWindow_popupExitTransition
*/
@Nullable
public Transition getExitTransition() {
return mExitTransition;
}
/**
* <p>Returns bounds which are used as a center of the enter and exit transitions.<p/>
*
* <p>Transitions use Rect, referred to as the epicenter, to orient
* the direction of travel. For popup windows, the anchor view bounds are
* used as the default epicenter.</p>
*
* <p>See {@link Transition#setEpicenterCallback(EpicenterCallback)} for more
* information about how transition epicenters work.</p>
*
* @return bounds relative to anchor view, or {@code null} if not set
* @see #setEpicenterBounds(Rect)
*/
@Nullable
public Rect getEpicenterBounds() {
return mEpicenterBounds != null ? new Rect(mEpicenterBounds) : null;
}
/**
* <p>Sets the bounds used as the epicenter of the enter and exit transitions.</p>
*
* <p>Transitions use Rect, referred to as the epicenter, to orient
* the direction of travel. For popup windows, the anchor view bounds are
* used as the default epicenter.</p>
*
* <p>See {@link Transition#setEpicenterCallback(EpicenterCallback)} for more
* information about how transition epicenters work.</p>
*
* @param bounds the epicenter bounds relative to the anchor view, or
* {@code null} to use the default epicenter
*
* @see #getEpicenterBounds()
*/
public void setEpicenterBounds(@Nullable Rect bounds) {
mEpicenterBounds = bounds != null ? new Rect(bounds) : null;
}
private Transition getTransition(int resId) {
if (resId != 0 && resId != R.transition.no_transition) {
final TransitionInflater inflater = TransitionInflater.from(mContext);
final Transition transition = inflater.inflateTransition(resId);
if (transition != null) {
final boolean isEmpty = transition instanceof TransitionSet
&& ((TransitionSet) transition).getTransitionCount() == 0;
if (!isEmpty) {
return transition;
}
}
}
return null;
}
/**
* Return the drawable used as the popup window's background.
*
* @return the background drawable or {@code null} if not set
* @see #setBackgroundDrawable(Drawable)
* @attr ref android.R.styleable#PopupWindow_popupBackground
*/
public Drawable getBackground() {
return mBackground;
}
/**
* Specifies the background drawable for this popup window. The background
* can be set to {@code null}.
*
* @param background the popup's background
* @see #getBackground()
* @attr ref android.R.styleable#PopupWindow_popupBackground
*/
public void setBackgroundDrawable(Drawable background) {
mBackground = background;
// If this is a StateListDrawable, try to find and store the drawable to be
// used when the drop-down is placed above its anchor view, and the one to be
// used when the drop-down is placed below its anchor view. We extract
// the drawables ourselves to work around a problem with using refreshDrawableState
// that it will take into account the padding of all drawables specified in a
// StateListDrawable, thus adding superfluous padding to drop-down views.
//
// We assume a StateListDrawable will have a drawable for ABOVE_ANCHOR_STATE_SET and
// at least one other drawable, intended for the 'below-anchor state'.
if (mBackground instanceof StateListDrawable) {
StateListDrawable stateList = (StateListDrawable) mBackground;
// Find the above-anchor view - this one's easy, it should be labeled as such.
int aboveAnchorStateIndex = stateList.findStateDrawableIndex(ABOVE_ANCHOR_STATE_SET);
// Now, for the below-anchor view, look for any other drawable specified in the
// StateListDrawable which is not for the above-anchor state and use that.
int count = stateList.getStateCount();
int belowAnchorStateIndex = -1;
for (int i = 0; i < count; i++) {
if (i != aboveAnchorStateIndex) {
belowAnchorStateIndex = i;
break;
}
}
// Store the drawables we found, if we found them. Otherwise, set them both
// to null so that we'll just use refreshDrawableState.
if (aboveAnchorStateIndex != -1 && belowAnchorStateIndex != -1) {
mAboveAnchorBackgroundDrawable = stateList.getStateDrawable(aboveAnchorStateIndex);
mBelowAnchorBackgroundDrawable = stateList.getStateDrawable(belowAnchorStateIndex);
} else {
mBelowAnchorBackgroundDrawable = null;
mAboveAnchorBackgroundDrawable = null;
}
}
}
/**
* @return the elevation for this popup window in pixels
* @see #setElevation(float)
* @attr ref android.R.styleable#PopupWindow_popupElevation
*/
public float getElevation() {
return mElevation;
}
/**
* Specifies the elevation for this popup window.
*
* @param elevation the popup's elevation in pixels
* @see #getElevation()
* @attr ref android.R.styleable#PopupWindow_popupElevation
*/
public void setElevation(float elevation) {
mElevation = elevation;
}
/**
* <p>Return the animation style to use the popup appears and disappears</p>
*
* @return the animation style to use the popup appears and disappears
*/
public int getAnimationStyle() {
return mAnimationStyle;
}
/**
* Set the flag on popup to ignore cheek press events; by default this flag
* is set to false
* which means the popup will not ignore cheek press dispatch events.
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @see #update()
*/
public void setIgnoreCheekPress() {
mIgnoreCheekPress = true;
}
/**
* <p>Change the animation style resource for this popup.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @param animationStyle animation style to use when the popup appears
* and disappears. Set to -1 for the default animation, 0 for no
* animation, or a resource identifier for an explicit animation.
*
* @see #update()
*/
public void setAnimationStyle(int animationStyle) {
mAnimationStyle = animationStyle;
}
/**
* <p>Return the view used as the content of the popup window.</p>
*
* @return a {@link android.view.View} representing the popup's content
*
* @see #setContentView(android.view.View)
*/
public View getContentView() {
return mContentView;
}
/**
* <p>Change the popup's content. The content is represented by an instance
* of {@link android.view.View}.</p>
*
* <p>This method has no effect if called when the popup is showing.</p>
*
* @param contentView the new content for the popup
*
* @see #getContentView()
* @see #isShowing()
*/
public void setContentView(View contentView) {
if (isShowing()) {
return;
}
mContentView = contentView;
if (mContext == null && mContentView != null) {
mContext = mContentView.getContext();
}
if (mWindowManager == null && mContentView != null) {
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
// Setting the default for attachedInDecor based on SDK version here
// instead of in the constructor since we might not have the context
// object in the constructor. We only want to set default here if the
// app hasn't already set the attachedInDecor.
if (mContext != null && !mAttachedInDecorSet) {
// Attach popup window in decor frame of parent window by default for
// {@link Build.VERSION_CODES.LOLLIPOP_MR1} or greater. Keep current
// behavior of not attaching to decor frame for older SDKs.
setAttachedInDecor(mContext.getApplicationInfo().targetSdkVersion
>= Build.VERSION_CODES.LOLLIPOP_MR1);
}
}
/**
* Set a callback for all touch events being dispatched to the popup
* window.
*/
public void setTouchInterceptor(OnTouchListener l) {
mTouchInterceptor = l;
}
/**
* <p>Indicate whether the popup window can grab the focus.</p>
*
* @return true if the popup is focusable, false otherwise
*
* @see #setFocusable(boolean)
*/
public boolean isFocusable() {
return mFocusable;
}
/**
* <p>Changes the focusability of the popup window. When focusable, the
* window will grab the focus from the current focused widget if the popup
* contains a focusable {@link android.view.View}. By default a popup
* window is not focusable.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @param focusable true if the popup should grab focus, false otherwise.
*
* @see #isFocusable()
* @see #isShowing()
* @see #update()
*/
public void setFocusable(boolean focusable) {
mFocusable = focusable;
}
/**
* Return the current value in {@link #setInputMethodMode(int)}.
*
* @see #setInputMethodMode(int)
*/
public int getInputMethodMode() {
return mInputMethodMode;
}
/**
* Control how the popup operates with an input method: one of
* {@link #INPUT_METHOD_FROM_FOCUSABLE}, {@link #INPUT_METHOD_NEEDED},
* or {@link #INPUT_METHOD_NOT_NEEDED}.
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @see #getInputMethodMode()
* @see #update()
*/
public void setInputMethodMode(int mode) {
mInputMethodMode = mode;
}
/**
* Sets the operating mode for the soft input area.
*
* @param mode The desired mode, see
* {@link android.view.WindowManager.LayoutParams#softInputMode}
* for the full list
*
* @see android.view.WindowManager.LayoutParams#softInputMode
* @see #getSoftInputMode()
*/
public void setSoftInputMode(@SoftInputModeFlags int mode) {
mSoftInputMode = mode;
}
/**
* Returns the current value in {@link #setSoftInputMode(int)}.
*
* @see #setSoftInputMode(int)
* @see android.view.WindowManager.LayoutParams#softInputMode
*/
@SoftInputModeFlags
public int getSoftInputMode() {
return mSoftInputMode;
}
/**
* <p>Indicates whether the popup window receives touch events.</p>
*
* @return true if the popup is touchable, false otherwise
*
* @see #setTouchable(boolean)
*/
public boolean isTouchable() {
return mTouchable;
}
/**
* <p>Changes the touchability of the popup window. When touchable, the
* window will receive touch events, otherwise touch events will go to the
* window below it. By default the window is touchable.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @param touchable true if the popup should receive touch events, false otherwise
*
* @see #isTouchable()
* @see #isShowing()
* @see #update()
*/
public void setTouchable(boolean touchable) {
mTouchable = touchable;
}
/**
* <p>Indicates whether the popup window will be informed of touch events
* outside of its window.</p>
*
* @return true if the popup is outside touchable, false otherwise
*
* @see #setOutsideTouchable(boolean)
*/
public boolean isOutsideTouchable() {
return mOutsideTouchable;
}
/**
* <p>Controls whether the pop-up will be informed of touch events outside
* of its window. This only makes sense for pop-ups that are touchable
* but not focusable, which means touches outside of the window will
* be delivered to the window behind. The default is false.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @param touchable true if the popup should receive outside
* touch events, false otherwise
*
* @see #isOutsideTouchable()
* @see #isShowing()
* @see #update()
*/
public void setOutsideTouchable(boolean touchable) {
mOutsideTouchable = touchable;
}
/**
* <p>Indicates whether clipping of the popup window is enabled.</p>
*
* @return true if the clipping is enabled, false otherwise
*
* @see #setClippingEnabled(boolean)
*/
public boolean isClippingEnabled() {
return mClippingEnabled;
}
/**
* <p>Allows the popup window to extend beyond the bounds of the screen. By default the
* window is clipped to the screen boundaries. Setting this to false will allow windows to be
* accurately positioned.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @param enabled false if the window should be allowed to extend outside of the screen
* @see #isShowing()
* @see #isClippingEnabled()
* @see #update()
*/
public void setClippingEnabled(boolean enabled) {
mClippingEnabled = enabled;
}
/**
* <p>Indicates whether this popup will be clipped to the screen and not to the
* containing window<p/>
*
* @return true if popup will be clipped to the screen instead of the window, false otherwise
* @deprecated Use {@link #isClippedToScreen()} instead
* @removed
*/
@Deprecated
public boolean isClipToScreenEnabled() {
return mClipToScreen;
}
/**
* <p>Clip this popup window to the screen, but not to the containing window.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @deprecated Use {@link #setIsClippedToScreen(boolean)} instead
* @removed
*/
@Deprecated
public void setClipToScreenEnabled(boolean enabled) {
mClipToScreen = enabled;
}
/**
* <p>Indicates whether this popup will be clipped to the screen and not to the
* containing window<p/>
*
* @return true if popup will be clipped to the screen instead of the window, false otherwise
*
* @see #setIsClippedToScreen(boolean)
*/
public boolean isClippedToScreen() {
return mClipToScreen;
}
/**
* <p>Clip this popup window to the screen, but not to the containing window.</p>
*
* <p>If the popup is showing, calling this method will take effect only
* the next time the popup is shown or through a manual call to one of
* the {@link #update()} methods.</p>
*
* @param enabled true to clip to the screen.
*
* @see #isClippedToScreen()
*/
public void setIsClippedToScreen(boolean enabled) {
mClipToScreen = enabled;
}
/**
* Allow PopupWindow to scroll the anchor's parent to provide more room
* for the popup. Enabled by default.
*
* @param enabled True to scroll the anchor's parent when more room is desired by the popup.
*/
@UnsupportedAppUsage
void setAllowScrollingAnchorParent(boolean enabled) {
mAllowScrollingAnchorParent = enabled;
}
/** @hide */
protected final boolean getAllowScrollingAnchorParent() {
return mAllowScrollingAnchorParent;
}
/**
* <p>Indicates whether the popup window supports splitting touches.</p>
*
* @return true if the touch splitting is enabled, false otherwise
*
* @see #setSplitTouchEnabled(boolean)
*/
public boolean isSplitTouchEnabled() {
if (mSplitTouchEnabled < 0 && mContext != null) {
return mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB;
}
return mSplitTouchEnabled == 1;
}
/**
* <p>Allows the popup window to split touches across other windows that also
* support split touch. When this flag is false, the first pointer
* that goes down determines the window to which all subsequent touches
* go until all pointers go up. When this flag is true, each pointer
* (not necessarily the first) that goes down determines the window
* to which all subsequent touches of that pointer will go until that
* pointer goes up thereby enabling touches with multiple pointers
* to be split across multiple windows.</p>
*
* @param enabled true if the split touches should be enabled, false otherwise
* @see #isSplitTouchEnabled()
*/
public void setSplitTouchEnabled(boolean enabled) {
mSplitTouchEnabled = enabled ? 1 : 0;
}
/**
* <p>Indicates whether the popup window will be forced into using absolute screen coordinates
* for positioning.</p>
*
* @return true if the window will always be positioned in screen coordinates.
*
* @deprecated Use {@link #isLaidOutInScreen()} instead
* @removed
*/
@Deprecated
public boolean isLayoutInScreenEnabled() {
return mLayoutInScreen;