AbsListView.java revision e0e4217330fc3fa86046c284696df3b2d9785632
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.res.TypedArray;
22import android.graphics.Canvas;
23import android.graphics.Rect;
24import android.graphics.drawable.Drawable;
25import android.graphics.drawable.TransitionDrawable;
26import android.os.Bundle;
27import android.os.Debug;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.os.StrictMode;
31import android.os.Trace;
32import android.text.Editable;
33import android.text.InputType;
34import android.text.TextUtils;
35import android.text.TextWatcher;
36import android.util.AttributeSet;
37import android.util.Log;
38import android.util.LongSparseArray;
39import android.util.SparseArray;
40import android.util.SparseBooleanArray;
41import android.util.StateSet;
42import android.view.ActionMode;
43import android.view.ContextMenu.ContextMenuInfo;
44import android.view.Gravity;
45import android.view.HapticFeedbackConstants;
46import android.view.InputDevice;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.Menu;
50import android.view.MenuItem;
51import android.view.MotionEvent;
52import android.view.VelocityTracker;
53import android.view.View;
54import android.view.ViewConfiguration;
55import android.view.ViewDebug;
56import android.view.ViewGroup;
57import android.view.ViewParent;
58import android.view.ViewTreeObserver;
59import android.view.accessibility.AccessibilityEvent;
60import android.view.accessibility.AccessibilityManager;
61import android.view.accessibility.AccessibilityNodeInfo;
62import android.view.accessibility.AccessibilityNodeInfo.CollectionInfo;
63import android.view.animation.Interpolator;
64import android.view.animation.LinearInterpolator;
65import android.view.inputmethod.BaseInputConnection;
66import android.view.inputmethod.CompletionInfo;
67import android.view.inputmethod.CorrectionInfo;
68import android.view.inputmethod.EditorInfo;
69import android.view.inputmethod.ExtractedText;
70import android.view.inputmethod.ExtractedTextRequest;
71import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
73import android.widget.RemoteViews.OnClickHandler;
74
75import com.android.internal.R;
76
77import java.util.ArrayList;
78import java.util.List;
79
80/**
81 * Base class that can be used to implement virtualized lists of items. A list does
82 * not have a spatial definition here. For instance, subclases of this class can
83 * display the content of the list in a grid, in a carousel, as stack, etc.
84 *
85 * @attr ref android.R.styleable#AbsListView_listSelector
86 * @attr ref android.R.styleable#AbsListView_drawSelectorOnTop
87 * @attr ref android.R.styleable#AbsListView_stackFromBottom
88 * @attr ref android.R.styleable#AbsListView_scrollingCache
89 * @attr ref android.R.styleable#AbsListView_textFilterEnabled
90 * @attr ref android.R.styleable#AbsListView_transcriptMode
91 * @attr ref android.R.styleable#AbsListView_cacheColorHint
92 * @attr ref android.R.styleable#AbsListView_fastScrollEnabled
93 * @attr ref android.R.styleable#AbsListView_smoothScrollbar
94 * @attr ref android.R.styleable#AbsListView_choiceMode
95 */
96public abstract class AbsListView extends AdapterView<ListAdapter> implements TextWatcher,
97        ViewTreeObserver.OnGlobalLayoutListener, Filter.FilterListener,
98        ViewTreeObserver.OnTouchModeChangeListener,
99        RemoteViewsAdapter.RemoteAdapterConnectionCallback {
100
101    @SuppressWarnings("UnusedDeclaration")
102    private static final String TAG = "AbsListView";
103
104    /**
105     * Disables the transcript mode.
106     *
107     * @see #setTranscriptMode(int)
108     */
109    public static final int TRANSCRIPT_MODE_DISABLED = 0;
110
111    /**
112     * The list will automatically scroll to the bottom when a data set change
113     * notification is received and only if the last item is already visible
114     * on screen.
115     *
116     * @see #setTranscriptMode(int)
117     */
118    public static final int TRANSCRIPT_MODE_NORMAL = 1;
119
120    /**
121     * The list will automatically scroll to the bottom, no matter what items
122     * are currently visible.
123     *
124     * @see #setTranscriptMode(int)
125     */
126    public static final int TRANSCRIPT_MODE_ALWAYS_SCROLL = 2;
127
128    /**
129     * Indicates that we are not in the middle of a touch gesture
130     */
131    static final int TOUCH_MODE_REST = -1;
132
133    /**
134     * Indicates we just received the touch event and we are waiting to see if the it is a tap or a
135     * scroll gesture.
136     */
137    static final int TOUCH_MODE_DOWN = 0;
138
139    /**
140     * Indicates the touch has been recognized as a tap and we are now waiting to see if the touch
141     * is a longpress
142     */
143    static final int TOUCH_MODE_TAP = 1;
144
145    /**
146     * Indicates we have waited for everything we can wait for, but the user's finger is still down
147     */
148    static final int TOUCH_MODE_DONE_WAITING = 2;
149
150    /**
151     * Indicates the touch gesture is a scroll
152     */
153    static final int TOUCH_MODE_SCROLL = 3;
154
155    /**
156     * Indicates the view is in the process of being flung
157     */
158    static final int TOUCH_MODE_FLING = 4;
159
160    /**
161     * Indicates the touch gesture is an overscroll - a scroll beyond the beginning or end.
162     */
163    static final int TOUCH_MODE_OVERSCROLL = 5;
164
165    /**
166     * Indicates the view is being flung outside of normal content bounds
167     * and will spring back.
168     */
169    static final int TOUCH_MODE_OVERFLING = 6;
170
171    /**
172     * Regular layout - usually an unsolicited layout from the view system
173     */
174    static final int LAYOUT_NORMAL = 0;
175
176    /**
177     * Show the first item
178     */
179    static final int LAYOUT_FORCE_TOP = 1;
180
181    /**
182     * Force the selected item to be on somewhere on the screen
183     */
184    static final int LAYOUT_SET_SELECTION = 2;
185
186    /**
187     * Show the last item
188     */
189    static final int LAYOUT_FORCE_BOTTOM = 3;
190
191    /**
192     * Make a mSelectedItem appear in a specific location and build the rest of
193     * the views from there. The top is specified by mSpecificTop.
194     */
195    static final int LAYOUT_SPECIFIC = 4;
196
197    /**
198     * Layout to sync as a result of a data change. Restore mSyncPosition to have its top
199     * at mSpecificTop
200     */
201    static final int LAYOUT_SYNC = 5;
202
203    /**
204     * Layout as a result of using the navigation keys
205     */
206    static final int LAYOUT_MOVE_SELECTION = 6;
207
208    /**
209     * Normal list that does not indicate choices
210     */
211    public static final int CHOICE_MODE_NONE = 0;
212
213    /**
214     * The list allows up to one choice
215     */
216    public static final int CHOICE_MODE_SINGLE = 1;
217
218    /**
219     * The list allows multiple choices
220     */
221    public static final int CHOICE_MODE_MULTIPLE = 2;
222
223    /**
224     * The list allows multiple choices in a modal selection mode
225     */
226    public static final int CHOICE_MODE_MULTIPLE_MODAL = 3;
227
228    /**
229     * The thread that created this view.
230     */
231    private final Thread mOwnerThread;
232
233    /**
234     * Controls if/how the user may choose/check items in the list
235     */
236    int mChoiceMode = CHOICE_MODE_NONE;
237
238    /**
239     * Controls CHOICE_MODE_MULTIPLE_MODAL. null when inactive.
240     */
241    ActionMode mChoiceActionMode;
242
243    /**
244     * Wrapper for the multiple choice mode callback; AbsListView needs to perform
245     * a few extra actions around what application code does.
246     */
247    MultiChoiceModeWrapper mMultiChoiceModeCallback;
248
249    /**
250     * Running count of how many items are currently checked
251     */
252    int mCheckedItemCount;
253
254    /**
255     * Running state of which positions are currently checked
256     */
257    SparseBooleanArray mCheckStates;
258
259    /**
260     * Running state of which IDs are currently checked.
261     * If there is a value for a given key, the checked state for that ID is true
262     * and the value holds the last known position in the adapter for that id.
263     */
264    LongSparseArray<Integer> mCheckedIdStates;
265
266    /**
267     * Controls how the next layout will happen
268     */
269    int mLayoutMode = LAYOUT_NORMAL;
270
271    /**
272     * Should be used by subclasses to listen to changes in the dataset
273     */
274    AdapterDataSetObserver mDataSetObserver;
275
276    /**
277     * The adapter containing the data to be displayed by this view
278     */
279    ListAdapter mAdapter;
280
281    /**
282     * The remote adapter containing the data to be displayed by this view to be set
283     */
284    private RemoteViewsAdapter mRemoteAdapter;
285
286    /**
287     * If mAdapter != null, whenever this is true the adapter has stable IDs.
288     */
289    boolean mAdapterHasStableIds;
290
291    /**
292     * This flag indicates the a full notify is required when the RemoteViewsAdapter connects
293     */
294    private boolean mDeferNotifyDataSetChanged = false;
295
296    /**
297     * Indicates whether the list selector should be drawn on top of the children or behind
298     */
299    boolean mDrawSelectorOnTop = false;
300
301    /**
302     * The drawable used to draw the selector
303     */
304    Drawable mSelector;
305
306    /**
307     * The current position of the selector in the list.
308     */
309    int mSelectorPosition = INVALID_POSITION;
310
311    /**
312     * Defines the selector's location and dimension at drawing time
313     */
314    Rect mSelectorRect = new Rect();
315
316    /**
317     * The data set used to store unused views that should be reused during the next layout
318     * to avoid creating new ones
319     */
320    final RecycleBin mRecycler = new RecycleBin();
321
322    /**
323     * The selection's left padding
324     */
325    int mSelectionLeftPadding = 0;
326
327    /**
328     * The selection's top padding
329     */
330    int mSelectionTopPadding = 0;
331
332    /**
333     * The selection's right padding
334     */
335    int mSelectionRightPadding = 0;
336
337    /**
338     * The selection's bottom padding
339     */
340    int mSelectionBottomPadding = 0;
341
342    /**
343     * This view's padding
344     */
345    Rect mListPadding = new Rect();
346
347    /**
348     * Subclasses must retain their measure spec from onMeasure() into this member
349     */
350    int mWidthMeasureSpec = 0;
351
352    /**
353     * The top scroll indicator
354     */
355    View mScrollUp;
356
357    /**
358     * The down scroll indicator
359     */
360    View mScrollDown;
361
362    /**
363     * When the view is scrolling, this flag is set to true to indicate subclasses that
364     * the drawing cache was enabled on the children
365     */
366    boolean mCachingStarted;
367    boolean mCachingActive;
368
369    /**
370     * The position of the view that received the down motion event
371     */
372    int mMotionPosition;
373
374    /**
375     * The offset to the top of the mMotionPosition view when the down motion event was received
376     */
377    int mMotionViewOriginalTop;
378
379    /**
380     * The desired offset to the top of the mMotionPosition view after a scroll
381     */
382    int mMotionViewNewTop;
383
384    /**
385     * The X value associated with the the down motion event
386     */
387    int mMotionX;
388
389    /**
390     * The Y value associated with the the down motion event
391     */
392    int mMotionY;
393
394    /**
395     * One of TOUCH_MODE_REST, TOUCH_MODE_DOWN, TOUCH_MODE_TAP, TOUCH_MODE_SCROLL, or
396     * TOUCH_MODE_DONE_WAITING
397     */
398    int mTouchMode = TOUCH_MODE_REST;
399
400    /**
401     * Y value from on the previous motion event (if any)
402     */
403    int mLastY;
404
405    /**
406     * How far the finger moved before we started scrolling
407     */
408    int mMotionCorrection;
409
410    /**
411     * Determines speed during touch scrolling
412     */
413    private VelocityTracker mVelocityTracker;
414
415    /**
416     * Handles one frame of a fling
417     */
418    private FlingRunnable mFlingRunnable;
419
420    /**
421     * Handles scrolling between positions within the list.
422     */
423    AbsPositionScroller mPositionScroller;
424
425    /**
426     * The offset in pixels form the top of the AdapterView to the top
427     * of the currently selected view. Used to save and restore state.
428     */
429    int mSelectedTop = 0;
430
431    /**
432     * Indicates whether the list is stacked from the bottom edge or
433     * the top edge.
434     */
435    boolean mStackFromBottom;
436
437    /**
438     * When set to true, the list automatically discards the children's
439     * bitmap cache after scrolling.
440     */
441    boolean mScrollingCacheEnabled;
442
443    /**
444     * Whether or not to enable the fast scroll feature on this list
445     */
446    boolean mFastScrollEnabled;
447
448    /**
449     * Whether or not to always show the fast scroll feature on this list
450     */
451    boolean mFastScrollAlwaysVisible;
452
453    /**
454     * Optional callback to notify client when scroll position has changed
455     */
456    private OnScrollListener mOnScrollListener;
457
458    /**
459     * Keeps track of our accessory window
460     */
461    PopupWindow mPopup;
462
463    /**
464     * Used with type filter window
465     */
466    EditText mTextFilter;
467
468    /**
469     * Indicates whether to use pixels-based or position-based scrollbar
470     * properties.
471     */
472    private boolean mSmoothScrollbarEnabled = true;
473
474    /**
475     * Indicates that this view supports filtering
476     */
477    private boolean mTextFilterEnabled;
478
479    /**
480     * Indicates that this view is currently displaying a filtered view of the data
481     */
482    private boolean mFiltered;
483
484    /**
485     * Rectangle used for hit testing children
486     */
487    private Rect mTouchFrame;
488
489    /**
490     * The position to resurrect the selected position to.
491     */
492    int mResurrectToPosition = INVALID_POSITION;
493
494    private ContextMenuInfo mContextMenuInfo = null;
495
496    /**
497     * Maximum distance to record overscroll
498     */
499    int mOverscrollMax;
500
501    /**
502     * Content height divided by this is the overscroll limit.
503     */
504    static final int OVERSCROLL_LIMIT_DIVISOR = 3;
505
506    /**
507     * How many positions in either direction we will search to try to
508     * find a checked item with a stable ID that moved position across
509     * a data set change. If the item isn't found it will be unselected.
510     */
511    private static final int CHECK_POSITION_SEARCH_DISTANCE = 20;
512
513    /**
514     * Used to request a layout when we changed touch mode
515     */
516    private static final int TOUCH_MODE_UNKNOWN = -1;
517    private static final int TOUCH_MODE_ON = 0;
518    private static final int TOUCH_MODE_OFF = 1;
519
520    private int mLastTouchMode = TOUCH_MODE_UNKNOWN;
521
522    private static final boolean PROFILE_SCROLLING = false;
523    private boolean mScrollProfilingStarted = false;
524
525    private static final boolean PROFILE_FLINGING = false;
526    private boolean mFlingProfilingStarted = false;
527
528    /**
529     * The StrictMode "critical time span" objects to catch animation
530     * stutters.  Non-null when a time-sensitive animation is
531     * in-flight.  Must call finish() on them when done animating.
532     * These are no-ops on user builds.
533     */
534    private StrictMode.Span mScrollStrictSpan = null;
535    private StrictMode.Span mFlingStrictSpan = null;
536
537    /**
538     * The last CheckForLongPress runnable we posted, if any
539     */
540    private CheckForLongPress mPendingCheckForLongPress;
541
542    /**
543     * The last CheckForTap runnable we posted, if any
544     */
545    private CheckForTap mPendingCheckForTap;
546
547    /**
548     * The last CheckForKeyLongPress runnable we posted, if any
549     */
550    private CheckForKeyLongPress mPendingCheckForKeyLongPress;
551
552    /**
553     * Acts upon click
554     */
555    private AbsListView.PerformClick mPerformClick;
556
557    /**
558     * Delayed action for touch mode.
559     */
560    private Runnable mTouchModeReset;
561
562    /**
563     * This view is in transcript mode -- it shows the bottom of the list when the data
564     * changes
565     */
566    private int mTranscriptMode;
567
568    /**
569     * Indicates that this list is always drawn on top of a solid, single-color, opaque
570     * background
571     */
572    private int mCacheColorHint;
573
574    /**
575     * The select child's view (from the adapter's getView) is enabled.
576     */
577    private boolean mIsChildViewEnabled;
578
579    /**
580     * The last scroll state reported to clients through {@link OnScrollListener}.
581     */
582    private int mLastScrollState = OnScrollListener.SCROLL_STATE_IDLE;
583
584    /**
585     * Helper object that renders and controls the fast scroll thumb.
586     */
587    private FastScroller mFastScroll;
588
589    /**
590     * Temporary holder for fast scroller style until a FastScroller object
591     * is created.
592     */
593    private int mFastScrollStyle;
594
595    private boolean mGlobalLayoutListenerAddedFilter;
596
597    private int mTouchSlop;
598    private float mDensityScale;
599
600    private InputConnection mDefInputConnection;
601    private InputConnectionWrapper mPublicInputConnection;
602
603    private Runnable mClearScrollingCache;
604    Runnable mPositionScrollAfterLayout;
605    private int mMinimumVelocity;
606    private int mMaximumVelocity;
607    private float mVelocityScale = 1.0f;
608
609    final boolean[] mIsScrap = new boolean[1];
610
611    private final int[] mScrollOffset = new int[2];
612    private final int[] mScrollConsumed = new int[2];
613
614    // True when the popup should be hidden because of a call to
615    // dispatchDisplayHint()
616    private boolean mPopupHidden;
617
618    /**
619     * ID of the active pointer. This is used to retain consistency during
620     * drags/flings if multiple pointers are used.
621     */
622    private int mActivePointerId = INVALID_POINTER;
623
624    /**
625     * Sentinel value for no current active pointer.
626     * Used by {@link #mActivePointerId}.
627     */
628    private static final int INVALID_POINTER = -1;
629
630    /**
631     * Maximum distance to overscroll by during edge effects
632     */
633    int mOverscrollDistance;
634
635    /**
636     * Maximum distance to overfling during edge effects
637     */
638    int mOverflingDistance;
639
640    // These two EdgeGlows are always set and used together.
641    // Checking one for null is as good as checking both.
642
643    /**
644     * Tracks the state of the top edge glow.
645     */
646    private EdgeEffect mEdgeGlowTop;
647
648    /**
649     * Tracks the state of the bottom edge glow.
650     */
651    private EdgeEffect mEdgeGlowBottom;
652
653    /**
654     * An estimate of how many pixels are between the top of the list and
655     * the top of the first position in the adapter, based on the last time
656     * we saw it. Used to hint where to draw edge glows.
657     */
658    private int mFirstPositionDistanceGuess;
659
660    /**
661     * An estimate of how many pixels are between the bottom of the list and
662     * the bottom of the last position in the adapter, based on the last time
663     * we saw it. Used to hint where to draw edge glows.
664     */
665    private int mLastPositionDistanceGuess;
666
667    /**
668     * Used for determining when to cancel out of overscroll.
669     */
670    private int mDirection = 0;
671
672    /**
673     * Tracked on measurement in transcript mode. Makes sure that we can still pin to
674     * the bottom correctly on resizes.
675     */
676    private boolean mForceTranscriptScroll;
677
678    private int mGlowPaddingLeft;
679    private int mGlowPaddingRight;
680
681    /**
682     * Used for interacting with list items from an accessibility service.
683     */
684    private ListItemAccessibilityDelegate mAccessibilityDelegate;
685
686    private int mLastAccessibilityScrollEventFromIndex;
687    private int mLastAccessibilityScrollEventToIndex;
688
689    /**
690     * Track the item count from the last time we handled a data change.
691     */
692    private int mLastHandledItemCount;
693
694    /**
695     * Used for smooth scrolling at a consistent rate
696     */
697    static final Interpolator sLinearInterpolator = new LinearInterpolator();
698
699    /**
700     * The saved state that we will be restoring from when we next sync.
701     * Kept here so that if we happen to be asked to save our state before
702     * the sync happens, we can return this existing data rather than losing
703     * it.
704     */
705    private SavedState mPendingSync;
706
707    /**
708     * Whether the view is in the process of detaching from its window.
709     */
710    private boolean mIsDetaching;
711
712    /**
713     * Interface definition for a callback to be invoked when the list or grid
714     * has been scrolled.
715     */
716    public interface OnScrollListener {
717
718        /**
719         * The view is not scrolling. Note navigating the list using the trackball counts as
720         * being in the idle state since these transitions are not animated.
721         */
722        public static int SCROLL_STATE_IDLE = 0;
723
724        /**
725         * The user is scrolling using touch, and their finger is still on the screen
726         */
727        public static int SCROLL_STATE_TOUCH_SCROLL = 1;
728
729        /**
730         * The user had previously been scrolling using touch and had performed a fling. The
731         * animation is now coasting to a stop
732         */
733        public static int SCROLL_STATE_FLING = 2;
734
735        /**
736         * Callback method to be invoked while the list view or grid view is being scrolled. If the
737         * view is being scrolled, this method will be called before the next frame of the scroll is
738         * rendered. In particular, it will be called before any calls to
739         * {@link Adapter#getView(int, View, ViewGroup)}.
740         *
741         * @param view The view whose scroll state is being reported
742         *
743         * @param scrollState The current scroll state. One of
744         * {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.
745         */
746        public void onScrollStateChanged(AbsListView view, int scrollState);
747
748        /**
749         * Callback method to be invoked when the list or grid has been scrolled. This will be
750         * called after the scroll has completed
751         * @param view The view whose scroll state is being reported
752         * @param firstVisibleItem the index of the first visible cell (ignore if
753         *        visibleItemCount == 0)
754         * @param visibleItemCount the number of visible cells
755         * @param totalItemCount the number of items in the list adaptor
756         */
757        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
758                int totalItemCount);
759    }
760
761    /**
762     * The top-level view of a list item can implement this interface to allow
763     * itself to modify the bounds of the selection shown for that item.
764     */
765    public interface SelectionBoundsAdjuster {
766        /**
767         * Called to allow the list item to adjust the bounds shown for
768         * its selection.
769         *
770         * @param bounds On call, this contains the bounds the list has
771         * selected for the item (that is the bounds of the entire view).  The
772         * values can be modified as desired.
773         */
774        public void adjustListItemSelectionBounds(Rect bounds);
775    }
776
777    public AbsListView(Context context) {
778        super(context);
779        initAbsListView();
780
781        mOwnerThread = Thread.currentThread();
782
783        setVerticalScrollBarEnabled(true);
784        TypedArray a = context.obtainStyledAttributes(R.styleable.View);
785        initializeScrollbarsInternal(a);
786        a.recycle();
787    }
788
789    public AbsListView(Context context, AttributeSet attrs) {
790        this(context, attrs, com.android.internal.R.attr.absListViewStyle);
791    }
792
793    public AbsListView(Context context, AttributeSet attrs, int defStyleAttr) {
794        this(context, attrs, defStyleAttr, 0);
795    }
796
797    public AbsListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
798        super(context, attrs, defStyleAttr, defStyleRes);
799        initAbsListView();
800
801        mOwnerThread = Thread.currentThread();
802
803        final TypedArray a = context.obtainStyledAttributes(
804                attrs, com.android.internal.R.styleable.AbsListView, defStyleAttr, defStyleRes);
805
806        Drawable d = a.getDrawable(com.android.internal.R.styleable.AbsListView_listSelector);
807        if (d != null) {
808            setSelector(d);
809        }
810
811        mDrawSelectorOnTop = a.getBoolean(
812                com.android.internal.R.styleable.AbsListView_drawSelectorOnTop, false);
813
814        boolean stackFromBottom = a.getBoolean(R.styleable.AbsListView_stackFromBottom, false);
815        setStackFromBottom(stackFromBottom);
816
817        boolean scrollingCacheEnabled = a.getBoolean(R.styleable.AbsListView_scrollingCache, true);
818        setScrollingCacheEnabled(scrollingCacheEnabled);
819
820        boolean useTextFilter = a.getBoolean(R.styleable.AbsListView_textFilterEnabled, false);
821        setTextFilterEnabled(useTextFilter);
822
823        int transcriptMode = a.getInt(R.styleable.AbsListView_transcriptMode,
824                TRANSCRIPT_MODE_DISABLED);
825        setTranscriptMode(transcriptMode);
826
827        int color = a.getColor(R.styleable.AbsListView_cacheColorHint, 0);
828        setCacheColorHint(color);
829
830        boolean enableFastScroll = a.getBoolean(R.styleable.AbsListView_fastScrollEnabled, false);
831        setFastScrollEnabled(enableFastScroll);
832
833        int fastScrollStyle = a.getResourceId(R.styleable.AbsListView_fastScrollStyle, 0);
834        setFastScrollStyle(fastScrollStyle);
835
836        boolean smoothScrollbar = a.getBoolean(R.styleable.AbsListView_smoothScrollbar, true);
837        setSmoothScrollbarEnabled(smoothScrollbar);
838
839        setChoiceMode(a.getInt(R.styleable.AbsListView_choiceMode, CHOICE_MODE_NONE));
840        setFastScrollAlwaysVisible(
841                a.getBoolean(R.styleable.AbsListView_fastScrollAlwaysVisible, false));
842
843        a.recycle();
844    }
845
846    private void initAbsListView() {
847        // Setting focusable in touch mode will set the focusable property to true
848        setClickable(true);
849        setFocusableInTouchMode(true);
850        setWillNotDraw(false);
851        setAlwaysDrawnWithCacheEnabled(false);
852        setScrollingCacheEnabled(true);
853
854        final ViewConfiguration configuration = ViewConfiguration.get(mContext);
855        mTouchSlop = configuration.getScaledTouchSlop();
856        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
857        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
858        mOverscrollDistance = configuration.getScaledOverscrollDistance();
859        mOverflingDistance = configuration.getScaledOverflingDistance();
860
861        mDensityScale = getContext().getResources().getDisplayMetrics().density;
862    }
863
864    @Override
865    public void setOverScrollMode(int mode) {
866        if (mode != OVER_SCROLL_NEVER) {
867            if (mEdgeGlowTop == null) {
868                Context context = getContext();
869                mEdgeGlowTop = new EdgeEffect(context);
870                mEdgeGlowBottom = new EdgeEffect(context);
871            }
872        } else {
873            mEdgeGlowTop = null;
874            mEdgeGlowBottom = null;
875        }
876        super.setOverScrollMode(mode);
877    }
878
879    /**
880     * {@inheritDoc}
881     */
882    @Override
883    public void setAdapter(ListAdapter adapter) {
884        if (adapter != null) {
885            mAdapterHasStableIds = mAdapter.hasStableIds();
886            if (mChoiceMode != CHOICE_MODE_NONE && mAdapterHasStableIds &&
887                    mCheckedIdStates == null) {
888                mCheckedIdStates = new LongSparseArray<Integer>();
889            }
890        }
891
892        if (mCheckStates != null) {
893            mCheckStates.clear();
894        }
895
896        if (mCheckedIdStates != null) {
897            mCheckedIdStates.clear();
898        }
899    }
900
901    /**
902     * Returns the number of items currently selected. This will only be valid
903     * if the choice mode is not {@link #CHOICE_MODE_NONE} (default).
904     *
905     * <p>To determine the specific items that are currently selected, use one of
906     * the <code>getChecked*</code> methods.
907     *
908     * @return The number of items currently selected
909     *
910     * @see #getCheckedItemPosition()
911     * @see #getCheckedItemPositions()
912     * @see #getCheckedItemIds()
913     */
914    public int getCheckedItemCount() {
915        return mCheckedItemCount;
916    }
917
918    /**
919     * Returns the checked state of the specified position. The result is only
920     * valid if the choice mode has been set to {@link #CHOICE_MODE_SINGLE}
921     * or {@link #CHOICE_MODE_MULTIPLE}.
922     *
923     * @param position The item whose checked state to return
924     * @return The item's checked state or <code>false</code> if choice mode
925     *         is invalid
926     *
927     * @see #setChoiceMode(int)
928     */
929    public boolean isItemChecked(int position) {
930        if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
931            return mCheckStates.get(position);
932        }
933
934        return false;
935    }
936
937    /**
938     * Returns the currently checked item. The result is only valid if the choice
939     * mode has been set to {@link #CHOICE_MODE_SINGLE}.
940     *
941     * @return The position of the currently checked item or
942     *         {@link #INVALID_POSITION} if nothing is selected
943     *
944     * @see #setChoiceMode(int)
945     */
946    public int getCheckedItemPosition() {
947        if (mChoiceMode == CHOICE_MODE_SINGLE && mCheckStates != null && mCheckStates.size() == 1) {
948            return mCheckStates.keyAt(0);
949        }
950
951        return INVALID_POSITION;
952    }
953
954    /**
955     * Returns the set of checked items in the list. The result is only valid if
956     * the choice mode has not been set to {@link #CHOICE_MODE_NONE}.
957     *
958     * @return  A SparseBooleanArray which will return true for each call to
959     *          get(int position) where position is a checked position in the
960     *          list and false otherwise, or <code>null</code> if the choice
961     *          mode is set to {@link #CHOICE_MODE_NONE}.
962     */
963    public SparseBooleanArray getCheckedItemPositions() {
964        if (mChoiceMode != CHOICE_MODE_NONE) {
965            return mCheckStates;
966        }
967        return null;
968    }
969
970    /**
971     * Returns the set of checked items ids. The result is only valid if the
972     * choice mode has not been set to {@link #CHOICE_MODE_NONE} and the adapter
973     * has stable IDs. ({@link ListAdapter#hasStableIds()} == {@code true})
974     *
975     * @return A new array which contains the id of each checked item in the
976     *         list.
977     */
978    public long[] getCheckedItemIds() {
979        if (mChoiceMode == CHOICE_MODE_NONE || mCheckedIdStates == null || mAdapter == null) {
980            return new long[0];
981        }
982
983        final LongSparseArray<Integer> idStates = mCheckedIdStates;
984        final int count = idStates.size();
985        final long[] ids = new long[count];
986
987        for (int i = 0; i < count; i++) {
988            ids[i] = idStates.keyAt(i);
989        }
990
991        return ids;
992    }
993
994    /**
995     * Clear any choices previously set
996     */
997    public void clearChoices() {
998        if (mCheckStates != null) {
999            mCheckStates.clear();
1000        }
1001        if (mCheckedIdStates != null) {
1002            mCheckedIdStates.clear();
1003        }
1004        mCheckedItemCount = 0;
1005    }
1006
1007    /**
1008     * Sets the checked state of the specified position. The is only valid if
1009     * the choice mode has been set to {@link #CHOICE_MODE_SINGLE} or
1010     * {@link #CHOICE_MODE_MULTIPLE}.
1011     *
1012     * @param position The item whose checked state is to be checked
1013     * @param value The new checked state for the item
1014     */
1015    public void setItemChecked(int position, boolean value) {
1016        if (mChoiceMode == CHOICE_MODE_NONE) {
1017            return;
1018        }
1019
1020        // Start selection mode if needed. We don't need to if we're unchecking something.
1021        if (value && mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode == null) {
1022            if (mMultiChoiceModeCallback == null ||
1023                    !mMultiChoiceModeCallback.hasWrappedCallback()) {
1024                throw new IllegalStateException("AbsListView: attempted to start selection mode " +
1025                        "for CHOICE_MODE_MULTIPLE_MODAL but no choice mode callback was " +
1026                        "supplied. Call setMultiChoiceModeListener to set a callback.");
1027            }
1028            mChoiceActionMode = startActionMode(mMultiChoiceModeCallback);
1029        }
1030
1031        if (mChoiceMode == CHOICE_MODE_MULTIPLE || mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
1032            boolean oldValue = mCheckStates.get(position);
1033            mCheckStates.put(position, value);
1034            if (mCheckedIdStates != null && mAdapter.hasStableIds()) {
1035                if (value) {
1036                    mCheckedIdStates.put(mAdapter.getItemId(position), position);
1037                } else {
1038                    mCheckedIdStates.delete(mAdapter.getItemId(position));
1039                }
1040            }
1041            if (oldValue != value) {
1042                if (value) {
1043                    mCheckedItemCount++;
1044                } else {
1045                    mCheckedItemCount--;
1046                }
1047            }
1048            if (mChoiceActionMode != null) {
1049                final long id = mAdapter.getItemId(position);
1050                mMultiChoiceModeCallback.onItemCheckedStateChanged(mChoiceActionMode,
1051                        position, id, value);
1052            }
1053        } else {
1054            boolean updateIds = mCheckedIdStates != null && mAdapter.hasStableIds();
1055            // Clear all values if we're checking something, or unchecking the currently
1056            // selected item
1057            if (value || isItemChecked(position)) {
1058                mCheckStates.clear();
1059                if (updateIds) {
1060                    mCheckedIdStates.clear();
1061                }
1062            }
1063            // this may end up selecting the value we just cleared but this way
1064            // we ensure length of mCheckStates is 1, a fact getCheckedItemPosition relies on
1065            if (value) {
1066                mCheckStates.put(position, true);
1067                if (updateIds) {
1068                    mCheckedIdStates.put(mAdapter.getItemId(position), position);
1069                }
1070                mCheckedItemCount = 1;
1071            } else if (mCheckStates.size() == 0 || !mCheckStates.valueAt(0)) {
1072                mCheckedItemCount = 0;
1073            }
1074        }
1075
1076        // Do not generate a data change while we are in the layout phase
1077        if (!mInLayout && !mBlockLayoutRequests) {
1078            mDataChanged = true;
1079            rememberSyncState();
1080            requestLayout();
1081        }
1082    }
1083
1084    @Override
1085    public boolean performItemClick(View view, int position, long id) {
1086        boolean handled = false;
1087        boolean dispatchItemClick = true;
1088
1089        if (mChoiceMode != CHOICE_MODE_NONE) {
1090            handled = true;
1091            boolean checkedStateChanged = false;
1092
1093            if (mChoiceMode == CHOICE_MODE_MULTIPLE ||
1094                    (mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode != null)) {
1095                boolean checked = !mCheckStates.get(position, false);
1096                mCheckStates.put(position, checked);
1097                if (mCheckedIdStates != null && mAdapter.hasStableIds()) {
1098                    if (checked) {
1099                        mCheckedIdStates.put(mAdapter.getItemId(position), position);
1100                    } else {
1101                        mCheckedIdStates.delete(mAdapter.getItemId(position));
1102                    }
1103                }
1104                if (checked) {
1105                    mCheckedItemCount++;
1106                } else {
1107                    mCheckedItemCount--;
1108                }
1109                if (mChoiceActionMode != null) {
1110                    mMultiChoiceModeCallback.onItemCheckedStateChanged(mChoiceActionMode,
1111                            position, id, checked);
1112                    dispatchItemClick = false;
1113                }
1114                checkedStateChanged = true;
1115            } else if (mChoiceMode == CHOICE_MODE_SINGLE) {
1116                boolean checked = !mCheckStates.get(position, false);
1117                if (checked) {
1118                    mCheckStates.clear();
1119                    mCheckStates.put(position, true);
1120                    if (mCheckedIdStates != null && mAdapter.hasStableIds()) {
1121                        mCheckedIdStates.clear();
1122                        mCheckedIdStates.put(mAdapter.getItemId(position), position);
1123                    }
1124                    mCheckedItemCount = 1;
1125                } else if (mCheckStates.size() == 0 || !mCheckStates.valueAt(0)) {
1126                    mCheckedItemCount = 0;
1127                }
1128                checkedStateChanged = true;
1129            }
1130
1131            if (checkedStateChanged) {
1132                updateOnScreenCheckedViews();
1133            }
1134        }
1135
1136        if (dispatchItemClick) {
1137            handled |= super.performItemClick(view, position, id);
1138        }
1139
1140        return handled;
1141    }
1142
1143    /**
1144     * Perform a quick, in-place update of the checked or activated state
1145     * on all visible item views. This should only be called when a valid
1146     * choice mode is active.
1147     */
1148    private void updateOnScreenCheckedViews() {
1149        final int firstPos = mFirstPosition;
1150        final int count = getChildCount();
1151        final boolean useActivated = getContext().getApplicationInfo().targetSdkVersion
1152                >= android.os.Build.VERSION_CODES.HONEYCOMB;
1153        for (int i = 0; i < count; i++) {
1154            final View child = getChildAt(i);
1155            final int position = firstPos + i;
1156
1157            if (child instanceof Checkable) {
1158                ((Checkable) child).setChecked(mCheckStates.get(position));
1159            } else if (useActivated) {
1160                child.setActivated(mCheckStates.get(position));
1161            }
1162        }
1163    }
1164
1165    /**
1166     * @see #setChoiceMode(int)
1167     *
1168     * @return The current choice mode
1169     */
1170    public int getChoiceMode() {
1171        return mChoiceMode;
1172    }
1173
1174    /**
1175     * Defines the choice behavior for the List. By default, Lists do not have any choice behavior
1176     * ({@link #CHOICE_MODE_NONE}). By setting the choiceMode to {@link #CHOICE_MODE_SINGLE}, the
1177     * List allows up to one item to  be in a chosen state. By setting the choiceMode to
1178     * {@link #CHOICE_MODE_MULTIPLE}, the list allows any number of items to be chosen.
1179     *
1180     * @param choiceMode One of {@link #CHOICE_MODE_NONE}, {@link #CHOICE_MODE_SINGLE}, or
1181     * {@link #CHOICE_MODE_MULTIPLE}
1182     */
1183    public void setChoiceMode(int choiceMode) {
1184        mChoiceMode = choiceMode;
1185        if (mChoiceActionMode != null) {
1186            mChoiceActionMode.finish();
1187            mChoiceActionMode = null;
1188        }
1189        if (mChoiceMode != CHOICE_MODE_NONE) {
1190            if (mCheckStates == null) {
1191                mCheckStates = new SparseBooleanArray(0);
1192            }
1193            if (mCheckedIdStates == null && mAdapter != null && mAdapter.hasStableIds()) {
1194                mCheckedIdStates = new LongSparseArray<Integer>(0);
1195            }
1196            // Modal multi-choice mode only has choices when the mode is active. Clear them.
1197            if (mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
1198                clearChoices();
1199                setLongClickable(true);
1200            }
1201        }
1202    }
1203
1204    /**
1205     * Set a {@link MultiChoiceModeListener} that will manage the lifecycle of the
1206     * selection {@link ActionMode}. Only used when the choice mode is set to
1207     * {@link #CHOICE_MODE_MULTIPLE_MODAL}.
1208     *
1209     * @param listener Listener that will manage the selection mode
1210     *
1211     * @see #setChoiceMode(int)
1212     */
1213    public void setMultiChoiceModeListener(MultiChoiceModeListener listener) {
1214        if (mMultiChoiceModeCallback == null) {
1215            mMultiChoiceModeCallback = new MultiChoiceModeWrapper();
1216        }
1217        mMultiChoiceModeCallback.setWrapped(listener);
1218    }
1219
1220    /**
1221     * @return true if all list content currently fits within the view boundaries
1222     */
1223    private boolean contentFits() {
1224        final int childCount = getChildCount();
1225        if (childCount == 0) return true;
1226        if (childCount != mItemCount) return false;
1227
1228        return getChildAt(0).getTop() >= mListPadding.top &&
1229                getChildAt(childCount - 1).getBottom() <= getHeight() - mListPadding.bottom;
1230    }
1231
1232    /**
1233     * Specifies whether fast scrolling is enabled or disabled.
1234     * <p>
1235     * When fast scrolling is enabled, the user can quickly scroll through lists
1236     * by dragging the fast scroll thumb.
1237     * <p>
1238     * If the adapter backing this list implements {@link SectionIndexer}, the
1239     * fast scroller will display section header previews as the user scrolls.
1240     * Additionally, the user will be able to quickly jump between sections by
1241     * tapping along the length of the scroll bar.
1242     *
1243     * @see SectionIndexer
1244     * @see #isFastScrollEnabled()
1245     * @param enabled true to enable fast scrolling, false otherwise
1246     */
1247    public void setFastScrollEnabled(final boolean enabled) {
1248        if (mFastScrollEnabled != enabled) {
1249            mFastScrollEnabled = enabled;
1250
1251            if (isOwnerThread()) {
1252                setFastScrollerEnabledUiThread(enabled);
1253            } else {
1254                post(new Runnable() {
1255                    @Override
1256                    public void run() {
1257                        setFastScrollerEnabledUiThread(enabled);
1258                    }
1259                });
1260            }
1261        }
1262    }
1263
1264    private void setFastScrollerEnabledUiThread(boolean enabled) {
1265        if (mFastScroll != null) {
1266            mFastScroll.setEnabled(enabled);
1267        } else if (enabled) {
1268            mFastScroll = new FastScroller(this, mFastScrollStyle);
1269            mFastScroll.setEnabled(true);
1270        }
1271
1272        resolvePadding();
1273
1274        if (mFastScroll != null) {
1275            mFastScroll.updateLayout();
1276        }
1277    }
1278
1279    /**
1280     * Specifies the style of the fast scroller decorations.
1281     *
1282     * @param styleResId style resource containing fast scroller properties
1283     * @see android.R.styleable#FastScroll
1284     */
1285    public void setFastScrollStyle(int styleResId) {
1286        if (mFastScroll == null) {
1287            mFastScrollStyle = styleResId;
1288        } else {
1289            mFastScroll.setStyle(styleResId);
1290        }
1291    }
1292
1293    /**
1294     * Set whether or not the fast scroller should always be shown in place of
1295     * the standard scroll bars. This will enable fast scrolling if it is not
1296     * already enabled.
1297     * <p>
1298     * Fast scrollers shown in this way will not fade out and will be a
1299     * permanent fixture within the list. This is best combined with an inset
1300     * scroll bar style to ensure the scroll bar does not overlap content.
1301     *
1302     * @param alwaysShow true if the fast scroller should always be displayed,
1303     *            false otherwise
1304     * @see #setScrollBarStyle(int)
1305     * @see #setFastScrollEnabled(boolean)
1306     */
1307    public void setFastScrollAlwaysVisible(final boolean alwaysShow) {
1308        if (mFastScrollAlwaysVisible != alwaysShow) {
1309            if (alwaysShow && !mFastScrollEnabled) {
1310                setFastScrollEnabled(true);
1311            }
1312
1313            mFastScrollAlwaysVisible = alwaysShow;
1314
1315            if (isOwnerThread()) {
1316                setFastScrollerAlwaysVisibleUiThread(alwaysShow);
1317            } else {
1318                post(new Runnable() {
1319                    @Override
1320                    public void run() {
1321                        setFastScrollerAlwaysVisibleUiThread(alwaysShow);
1322                    }
1323                });
1324            }
1325        }
1326    }
1327
1328    private void setFastScrollerAlwaysVisibleUiThread(boolean alwaysShow) {
1329        if (mFastScroll != null) {
1330            mFastScroll.setAlwaysShow(alwaysShow);
1331        }
1332    }
1333
1334    /**
1335     * @return whether the current thread is the one that created the view
1336     */
1337    private boolean isOwnerThread() {
1338        return mOwnerThread == Thread.currentThread();
1339    }
1340
1341    /**
1342     * Returns true if the fast scroller is set to always show on this view.
1343     *
1344     * @return true if the fast scroller will always show
1345     * @see #setFastScrollAlwaysVisible(boolean)
1346     */
1347    public boolean isFastScrollAlwaysVisible() {
1348        if (mFastScroll == null) {
1349            return mFastScrollEnabled && mFastScrollAlwaysVisible;
1350        } else {
1351            return mFastScroll.isEnabled() && mFastScroll.isAlwaysShowEnabled();
1352        }
1353    }
1354
1355    @Override
1356    public int getVerticalScrollbarWidth() {
1357        if (mFastScroll != null && mFastScroll.isEnabled()) {
1358            return Math.max(super.getVerticalScrollbarWidth(), mFastScroll.getWidth());
1359        }
1360        return super.getVerticalScrollbarWidth();
1361    }
1362
1363    /**
1364     * Returns true if the fast scroller is enabled.
1365     *
1366     * @see #setFastScrollEnabled(boolean)
1367     * @return true if fast scroll is enabled, false otherwise
1368     */
1369    @ViewDebug.ExportedProperty
1370    public boolean isFastScrollEnabled() {
1371        if (mFastScroll == null) {
1372            return mFastScrollEnabled;
1373        } else {
1374            return mFastScroll.isEnabled();
1375        }
1376    }
1377
1378    @Override
1379    public void setVerticalScrollbarPosition(int position) {
1380        super.setVerticalScrollbarPosition(position);
1381        if (mFastScroll != null) {
1382            mFastScroll.setScrollbarPosition(position);
1383        }
1384    }
1385
1386    @Override
1387    public void setScrollBarStyle(int style) {
1388        super.setScrollBarStyle(style);
1389        if (mFastScroll != null) {
1390            mFastScroll.setScrollBarStyle(style);
1391        }
1392    }
1393
1394    /**
1395     * If fast scroll is enabled, then don't draw the vertical scrollbar.
1396     * @hide
1397     */
1398    @Override
1399    protected boolean isVerticalScrollBarHidden() {
1400        return isFastScrollEnabled();
1401    }
1402
1403    /**
1404     * When smooth scrollbar is enabled, the position and size of the scrollbar thumb
1405     * is computed based on the number of visible pixels in the visible items. This
1406     * however assumes that all list items have the same height. If you use a list in
1407     * which items have different heights, the scrollbar will change appearance as the
1408     * user scrolls through the list. To avoid this issue, you need to disable this
1409     * property.
1410     *
1411     * When smooth scrollbar is disabled, the position and size of the scrollbar thumb
1412     * is based solely on the number of items in the adapter and the position of the
1413     * visible items inside the adapter. This provides a stable scrollbar as the user
1414     * navigates through a list of items with varying heights.
1415     *
1416     * @param enabled Whether or not to enable smooth scrollbar.
1417     *
1418     * @see #setSmoothScrollbarEnabled(boolean)
1419     * @attr ref android.R.styleable#AbsListView_smoothScrollbar
1420     */
1421    public void setSmoothScrollbarEnabled(boolean enabled) {
1422        mSmoothScrollbarEnabled = enabled;
1423    }
1424
1425    /**
1426     * Returns the current state of the fast scroll feature.
1427     *
1428     * @return True if smooth scrollbar is enabled is enabled, false otherwise.
1429     *
1430     * @see #setSmoothScrollbarEnabled(boolean)
1431     */
1432    @ViewDebug.ExportedProperty
1433    public boolean isSmoothScrollbarEnabled() {
1434        return mSmoothScrollbarEnabled;
1435    }
1436
1437    /**
1438     * Set the listener that will receive notifications every time the list scrolls.
1439     *
1440     * @param l the scroll listener
1441     */
1442    public void setOnScrollListener(OnScrollListener l) {
1443        mOnScrollListener = l;
1444        invokeOnItemScrollListener();
1445    }
1446
1447    /**
1448     * Notify our scroll listener (if there is one) of a change in scroll state
1449     */
1450    void invokeOnItemScrollListener() {
1451        if (mFastScroll != null) {
1452            mFastScroll.onScroll(mFirstPosition, getChildCount(), mItemCount);
1453        }
1454        if (mOnScrollListener != null) {
1455            mOnScrollListener.onScroll(this, mFirstPosition, getChildCount(), mItemCount);
1456        }
1457        onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these.
1458    }
1459
1460    @Override
1461    public void sendAccessibilityEvent(int eventType) {
1462        // Since this class calls onScrollChanged even if the mFirstPosition and the
1463        // child count have not changed we will avoid sending duplicate accessibility
1464        // events.
1465        if (eventType == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1466            final int firstVisiblePosition = getFirstVisiblePosition();
1467            final int lastVisiblePosition = getLastVisiblePosition();
1468            if (mLastAccessibilityScrollEventFromIndex == firstVisiblePosition
1469                    && mLastAccessibilityScrollEventToIndex == lastVisiblePosition) {
1470                return;
1471            } else {
1472                mLastAccessibilityScrollEventFromIndex = firstVisiblePosition;
1473                mLastAccessibilityScrollEventToIndex = lastVisiblePosition;
1474            }
1475        }
1476        super.sendAccessibilityEvent(eventType);
1477    }
1478
1479    @Override
1480    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1481        super.onInitializeAccessibilityEvent(event);
1482        event.setClassName(AbsListView.class.getName());
1483    }
1484
1485    @Override
1486    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1487        super.onInitializeAccessibilityNodeInfo(info);
1488        info.setClassName(AbsListView.class.getName());
1489        if (isEnabled()) {
1490            if (getFirstVisiblePosition() > 0) {
1491                info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1492                info.setScrollable(true);
1493            }
1494            if (getLastVisiblePosition() < getCount() - 1) {
1495                info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1496                info.setScrollable(true);
1497            }
1498        }
1499    }
1500
1501    int getSelectionModeForAccessibility() {
1502        final int choiceMode = getChoiceMode();
1503        switch (choiceMode) {
1504            case CHOICE_MODE_NONE:
1505                return CollectionInfo.SELECTION_MODE_NONE;
1506            case CHOICE_MODE_SINGLE:
1507                return CollectionInfo.SELECTION_MODE_SINGLE;
1508            case CHOICE_MODE_MULTIPLE:
1509            case CHOICE_MODE_MULTIPLE_MODAL:
1510                return CollectionInfo.SELECTION_MODE_MULTIPLE;
1511            default:
1512                return CollectionInfo.SELECTION_MODE_NONE;
1513        }
1514    }
1515
1516    @Override
1517    public boolean performAccessibilityAction(int action, Bundle arguments) {
1518        if (super.performAccessibilityAction(action, arguments)) {
1519            return true;
1520        }
1521        switch (action) {
1522            case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1523                if (isEnabled() && getLastVisiblePosition() < getCount() - 1) {
1524                    final int viewportHeight = getHeight() - mListPadding.top - mListPadding.bottom;
1525                    smoothScrollBy(viewportHeight, PositionScroller.SCROLL_DURATION);
1526                    return true;
1527                }
1528            } return false;
1529            case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1530                if (isEnabled() && mFirstPosition > 0) {
1531                    final int viewportHeight = getHeight() - mListPadding.top - mListPadding.bottom;
1532                    smoothScrollBy(-viewportHeight, PositionScroller.SCROLL_DURATION);
1533                    return true;
1534                }
1535            } return false;
1536        }
1537        return false;
1538    }
1539
1540    /** @hide */
1541    @Override
1542    public View findViewByAccessibilityIdTraversal(int accessibilityId) {
1543        if (accessibilityId == getAccessibilityViewId()) {
1544            return this;
1545        }
1546        // If the data changed the children are invalid since the data model changed.
1547        // Hence, we pretend they do not exist. After a layout the children will sync
1548        // with the model at which point we notify that the accessibility state changed,
1549        // so a service will be able to re-fetch the views.
1550        if (mDataChanged) {
1551            return null;
1552        }
1553        return super.findViewByAccessibilityIdTraversal(accessibilityId);
1554    }
1555
1556    /**
1557     * Indicates whether the children's drawing cache is used during a scroll.
1558     * By default, the drawing cache is enabled but this will consume more memory.
1559     *
1560     * @return true if the scrolling cache is enabled, false otherwise
1561     *
1562     * @see #setScrollingCacheEnabled(boolean)
1563     * @see View#setDrawingCacheEnabled(boolean)
1564     */
1565    @ViewDebug.ExportedProperty
1566    public boolean isScrollingCacheEnabled() {
1567        return mScrollingCacheEnabled;
1568    }
1569
1570    /**
1571     * Enables or disables the children's drawing cache during a scroll.
1572     * By default, the drawing cache is enabled but this will use more memory.
1573     *
1574     * When the scrolling cache is enabled, the caches are kept after the
1575     * first scrolling. You can manually clear the cache by calling
1576     * {@link android.view.ViewGroup#setChildrenDrawingCacheEnabled(boolean)}.
1577     *
1578     * @param enabled true to enable the scroll cache, false otherwise
1579     *
1580     * @see #isScrollingCacheEnabled()
1581     * @see View#setDrawingCacheEnabled(boolean)
1582     */
1583    public void setScrollingCacheEnabled(boolean enabled) {
1584        if (mScrollingCacheEnabled && !enabled) {
1585            clearScrollingCache();
1586        }
1587        mScrollingCacheEnabled = enabled;
1588    }
1589
1590    /**
1591     * Enables or disables the type filter window. If enabled, typing when
1592     * this view has focus will filter the children to match the users input.
1593     * Note that the {@link Adapter} used by this view must implement the
1594     * {@link Filterable} interface.
1595     *
1596     * @param textFilterEnabled true to enable type filtering, false otherwise
1597     *
1598     * @see Filterable
1599     */
1600    public void setTextFilterEnabled(boolean textFilterEnabled) {
1601        mTextFilterEnabled = textFilterEnabled;
1602    }
1603
1604    /**
1605     * Indicates whether type filtering is enabled for this view
1606     *
1607     * @return true if type filtering is enabled, false otherwise
1608     *
1609     * @see #setTextFilterEnabled(boolean)
1610     * @see Filterable
1611     */
1612    @ViewDebug.ExportedProperty
1613    public boolean isTextFilterEnabled() {
1614        return mTextFilterEnabled;
1615    }
1616
1617    @Override
1618    public void getFocusedRect(Rect r) {
1619        View view = getSelectedView();
1620        if (view != null && view.getParent() == this) {
1621            // the focused rectangle of the selected view offset into the
1622            // coordinate space of this view.
1623            view.getFocusedRect(r);
1624            offsetDescendantRectToMyCoords(view, r);
1625        } else {
1626            // otherwise, just the norm
1627            super.getFocusedRect(r);
1628        }
1629    }
1630
1631    private void useDefaultSelector() {
1632        setSelector(getContext().getDrawable(
1633                com.android.internal.R.drawable.list_selector_background));
1634    }
1635
1636    /**
1637     * Indicates whether the content of this view is pinned to, or stacked from,
1638     * the bottom edge.
1639     *
1640     * @return true if the content is stacked from the bottom edge, false otherwise
1641     */
1642    @ViewDebug.ExportedProperty
1643    public boolean isStackFromBottom() {
1644        return mStackFromBottom;
1645    }
1646
1647    /**
1648     * When stack from bottom is set to true, the list fills its content starting from
1649     * the bottom of the view.
1650     *
1651     * @param stackFromBottom true to pin the view's content to the bottom edge,
1652     *        false to pin the view's content to the top edge
1653     */
1654    public void setStackFromBottom(boolean stackFromBottom) {
1655        if (mStackFromBottom != stackFromBottom) {
1656            mStackFromBottom = stackFromBottom;
1657            requestLayoutIfNecessary();
1658        }
1659    }
1660
1661    void requestLayoutIfNecessary() {
1662        if (getChildCount() > 0) {
1663            resetList();
1664            requestLayout();
1665            invalidate();
1666        }
1667    }
1668
1669    static class SavedState extends BaseSavedState {
1670        long selectedId;
1671        long firstId;
1672        int viewTop;
1673        int position;
1674        int height;
1675        String filter;
1676        boolean inActionMode;
1677        int checkedItemCount;
1678        SparseBooleanArray checkState;
1679        LongSparseArray<Integer> checkIdState;
1680
1681        /**
1682         * Constructor called from {@link AbsListView#onSaveInstanceState()}
1683         */
1684        SavedState(Parcelable superState) {
1685            super(superState);
1686        }
1687
1688        /**
1689         * Constructor called from {@link #CREATOR}
1690         */
1691        private SavedState(Parcel in) {
1692            super(in);
1693            selectedId = in.readLong();
1694            firstId = in.readLong();
1695            viewTop = in.readInt();
1696            position = in.readInt();
1697            height = in.readInt();
1698            filter = in.readString();
1699            inActionMode = in.readByte() != 0;
1700            checkedItemCount = in.readInt();
1701            checkState = in.readSparseBooleanArray();
1702            final int N = in.readInt();
1703            if (N > 0) {
1704                checkIdState = new LongSparseArray<Integer>();
1705                for (int i=0; i<N; i++) {
1706                    final long key = in.readLong();
1707                    final int value = in.readInt();
1708                    checkIdState.put(key, value);
1709                }
1710            }
1711        }
1712
1713        @Override
1714        public void writeToParcel(Parcel out, int flags) {
1715            super.writeToParcel(out, flags);
1716            out.writeLong(selectedId);
1717            out.writeLong(firstId);
1718            out.writeInt(viewTop);
1719            out.writeInt(position);
1720            out.writeInt(height);
1721            out.writeString(filter);
1722            out.writeByte((byte) (inActionMode ? 1 : 0));
1723            out.writeInt(checkedItemCount);
1724            out.writeSparseBooleanArray(checkState);
1725            final int N = checkIdState != null ? checkIdState.size() : 0;
1726            out.writeInt(N);
1727            for (int i=0; i<N; i++) {
1728                out.writeLong(checkIdState.keyAt(i));
1729                out.writeInt(checkIdState.valueAt(i));
1730            }
1731        }
1732
1733        @Override
1734        public String toString() {
1735            return "AbsListView.SavedState{"
1736                    + Integer.toHexString(System.identityHashCode(this))
1737                    + " selectedId=" + selectedId
1738                    + " firstId=" + firstId
1739                    + " viewTop=" + viewTop
1740                    + " position=" + position
1741                    + " height=" + height
1742                    + " filter=" + filter
1743                    + " checkState=" + checkState + "}";
1744        }
1745
1746        public static final Parcelable.Creator<SavedState> CREATOR
1747                = new Parcelable.Creator<SavedState>() {
1748            @Override
1749            public SavedState createFromParcel(Parcel in) {
1750                return new SavedState(in);
1751            }
1752
1753            @Override
1754            public SavedState[] newArray(int size) {
1755                return new SavedState[size];
1756            }
1757        };
1758    }
1759
1760    @Override
1761    public Parcelable onSaveInstanceState() {
1762        /*
1763         * This doesn't really make sense as the place to dismiss the
1764         * popups, but there don't seem to be any other useful hooks
1765         * that happen early enough to keep from getting complaints
1766         * about having leaked the window.
1767         */
1768        dismissPopup();
1769
1770        Parcelable superState = super.onSaveInstanceState();
1771
1772        SavedState ss = new SavedState(superState);
1773
1774        if (mPendingSync != null) {
1775            // Just keep what we last restored.
1776            ss.selectedId = mPendingSync.selectedId;
1777            ss.firstId = mPendingSync.firstId;
1778            ss.viewTop = mPendingSync.viewTop;
1779            ss.position = mPendingSync.position;
1780            ss.height = mPendingSync.height;
1781            ss.filter = mPendingSync.filter;
1782            ss.inActionMode = mPendingSync.inActionMode;
1783            ss.checkedItemCount = mPendingSync.checkedItemCount;
1784            ss.checkState = mPendingSync.checkState;
1785            ss.checkIdState = mPendingSync.checkIdState;
1786            return ss;
1787        }
1788
1789        boolean haveChildren = getChildCount() > 0 && mItemCount > 0;
1790        long selectedId = getSelectedItemId();
1791        ss.selectedId = selectedId;
1792        ss.height = getHeight();
1793
1794        if (selectedId >= 0) {
1795            // Remember the selection
1796            ss.viewTop = mSelectedTop;
1797            ss.position = getSelectedItemPosition();
1798            ss.firstId = INVALID_POSITION;
1799        } else {
1800            if (haveChildren && mFirstPosition > 0) {
1801                // Remember the position of the first child.
1802                // We only do this if we are not currently at the top of
1803                // the list, for two reasons:
1804                // (1) The list may be in the process of becoming empty, in
1805                // which case mItemCount may not be 0, but if we try to
1806                // ask for any information about position 0 we will crash.
1807                // (2) Being "at the top" seems like a special case, anyway,
1808                // and the user wouldn't expect to end up somewhere else when
1809                // they revisit the list even if its content has changed.
1810                View v = getChildAt(0);
1811                ss.viewTop = v.getTop();
1812                int firstPos = mFirstPosition;
1813                if (firstPos >= mItemCount) {
1814                    firstPos = mItemCount - 1;
1815                }
1816                ss.position = firstPos;
1817                ss.firstId = mAdapter.getItemId(firstPos);
1818            } else {
1819                ss.viewTop = 0;
1820                ss.firstId = INVALID_POSITION;
1821                ss.position = 0;
1822            }
1823        }
1824
1825        ss.filter = null;
1826        if (mFiltered) {
1827            final EditText textFilter = mTextFilter;
1828            if (textFilter != null) {
1829                Editable filterText = textFilter.getText();
1830                if (filterText != null) {
1831                    ss.filter = filterText.toString();
1832                }
1833            }
1834        }
1835
1836        ss.inActionMode = mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode != null;
1837
1838        if (mCheckStates != null) {
1839            ss.checkState = mCheckStates.clone();
1840        }
1841        if (mCheckedIdStates != null) {
1842            final LongSparseArray<Integer> idState = new LongSparseArray<Integer>();
1843            final int count = mCheckedIdStates.size();
1844            for (int i = 0; i < count; i++) {
1845                idState.put(mCheckedIdStates.keyAt(i), mCheckedIdStates.valueAt(i));
1846            }
1847            ss.checkIdState = idState;
1848        }
1849        ss.checkedItemCount = mCheckedItemCount;
1850
1851        if (mRemoteAdapter != null) {
1852            mRemoteAdapter.saveRemoteViewsCache();
1853        }
1854
1855        return ss;
1856    }
1857
1858    @Override
1859    public void onRestoreInstanceState(Parcelable state) {
1860        SavedState ss = (SavedState) state;
1861
1862        super.onRestoreInstanceState(ss.getSuperState());
1863        mDataChanged = true;
1864
1865        mSyncHeight = ss.height;
1866
1867        if (ss.selectedId >= 0) {
1868            mNeedSync = true;
1869            mPendingSync = ss;
1870            mSyncRowId = ss.selectedId;
1871            mSyncPosition = ss.position;
1872            mSpecificTop = ss.viewTop;
1873            mSyncMode = SYNC_SELECTED_POSITION;
1874        } else if (ss.firstId >= 0) {
1875            setSelectedPositionInt(INVALID_POSITION);
1876            // Do this before setting mNeedSync since setNextSelectedPosition looks at mNeedSync
1877            setNextSelectedPositionInt(INVALID_POSITION);
1878            mSelectorPosition = INVALID_POSITION;
1879            mNeedSync = true;
1880            mPendingSync = ss;
1881            mSyncRowId = ss.firstId;
1882            mSyncPosition = ss.position;
1883            mSpecificTop = ss.viewTop;
1884            mSyncMode = SYNC_FIRST_POSITION;
1885        }
1886
1887        setFilterText(ss.filter);
1888
1889        if (ss.checkState != null) {
1890            mCheckStates = ss.checkState;
1891        }
1892
1893        if (ss.checkIdState != null) {
1894            mCheckedIdStates = ss.checkIdState;
1895        }
1896
1897        mCheckedItemCount = ss.checkedItemCount;
1898
1899        if (ss.inActionMode && mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL &&
1900                mMultiChoiceModeCallback != null) {
1901            mChoiceActionMode = startActionMode(mMultiChoiceModeCallback);
1902        }
1903
1904        requestLayout();
1905    }
1906
1907    private boolean acceptFilter() {
1908        return mTextFilterEnabled && getAdapter() instanceof Filterable &&
1909                ((Filterable) getAdapter()).getFilter() != null;
1910    }
1911
1912    /**
1913     * Sets the initial value for the text filter.
1914     * @param filterText The text to use for the filter.
1915     *
1916     * @see #setTextFilterEnabled
1917     */
1918    public void setFilterText(String filterText) {
1919        // TODO: Should we check for acceptFilter()?
1920        if (mTextFilterEnabled && !TextUtils.isEmpty(filterText)) {
1921            createTextFilter(false);
1922            // This is going to call our listener onTextChanged, but we might not
1923            // be ready to bring up a window yet
1924            mTextFilter.setText(filterText);
1925            mTextFilter.setSelection(filterText.length());
1926            if (mAdapter instanceof Filterable) {
1927                // if mPopup is non-null, then onTextChanged will do the filtering
1928                if (mPopup == null) {
1929                    Filter f = ((Filterable) mAdapter).getFilter();
1930                    f.filter(filterText);
1931                }
1932                // Set filtered to true so we will display the filter window when our main
1933                // window is ready
1934                mFiltered = true;
1935                mDataSetObserver.clearSavedState();
1936            }
1937        }
1938    }
1939
1940    /**
1941     * Returns the list's text filter, if available.
1942     * @return the list's text filter or null if filtering isn't enabled
1943     */
1944    public CharSequence getTextFilter() {
1945        if (mTextFilterEnabled && mTextFilter != null) {
1946            return mTextFilter.getText();
1947        }
1948        return null;
1949    }
1950
1951    @Override
1952    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
1953        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
1954        if (gainFocus && mSelectedPosition < 0 && !isInTouchMode()) {
1955            if (!isAttachedToWindow() && mAdapter != null) {
1956                // Data may have changed while we were detached and it's valid
1957                // to change focus while detached. Refresh so we don't die.
1958                mDataChanged = true;
1959                mOldItemCount = mItemCount;
1960                mItemCount = mAdapter.getCount();
1961            }
1962            resurrectSelection();
1963        }
1964    }
1965
1966    @Override
1967    public void requestLayout() {
1968        if (!mBlockLayoutRequests && !mInLayout) {
1969            super.requestLayout();
1970        }
1971    }
1972
1973    /**
1974     * The list is empty. Clear everything out.
1975     */
1976    void resetList() {
1977        removeAllViewsInLayout();
1978        mFirstPosition = 0;
1979        mDataChanged = false;
1980        mPositionScrollAfterLayout = null;
1981        mNeedSync = false;
1982        mPendingSync = null;
1983        mOldSelectedPosition = INVALID_POSITION;
1984        mOldSelectedRowId = INVALID_ROW_ID;
1985        setSelectedPositionInt(INVALID_POSITION);
1986        setNextSelectedPositionInt(INVALID_POSITION);
1987        mSelectedTop = 0;
1988        mSelectorPosition = INVALID_POSITION;
1989        mSelectorRect.setEmpty();
1990        invalidate();
1991    }
1992
1993    @Override
1994    protected int computeVerticalScrollExtent() {
1995        final int count = getChildCount();
1996        if (count > 0) {
1997            if (mSmoothScrollbarEnabled) {
1998                int extent = count * 100;
1999
2000                View view = getChildAt(0);
2001                final int top = view.getTop();
2002                int height = view.getHeight();
2003                if (height > 0) {
2004                    extent += (top * 100) / height;
2005                }
2006
2007                view = getChildAt(count - 1);
2008                final int bottom = view.getBottom();
2009                height = view.getHeight();
2010                if (height > 0) {
2011                    extent -= ((bottom - getHeight()) * 100) / height;
2012                }
2013
2014                return extent;
2015            } else {
2016                return 1;
2017            }
2018        }
2019        return 0;
2020    }
2021
2022    @Override
2023    protected int computeVerticalScrollOffset() {
2024        final int firstPosition = mFirstPosition;
2025        final int childCount = getChildCount();
2026        if (firstPosition >= 0 && childCount > 0) {
2027            if (mSmoothScrollbarEnabled) {
2028                final View view = getChildAt(0);
2029                final int top = view.getTop();
2030                int height = view.getHeight();
2031                if (height > 0) {
2032                    return Math.max(firstPosition * 100 - (top * 100) / height +
2033                            (int)((float)mScrollY / getHeight() * mItemCount * 100), 0);
2034                }
2035            } else {
2036                int index;
2037                final int count = mItemCount;
2038                if (firstPosition == 0) {
2039                    index = 0;
2040                } else if (firstPosition + childCount == count) {
2041                    index = count;
2042                } else {
2043                    index = firstPosition + childCount / 2;
2044                }
2045                return (int) (firstPosition + childCount * (index / (float) count));
2046            }
2047        }
2048        return 0;
2049    }
2050
2051    @Override
2052    protected int computeVerticalScrollRange() {
2053        int result;
2054        if (mSmoothScrollbarEnabled) {
2055            result = Math.max(mItemCount * 100, 0);
2056            if (mScrollY != 0) {
2057                // Compensate for overscroll
2058                result += Math.abs((int) ((float) mScrollY / getHeight() * mItemCount * 100));
2059            }
2060        } else {
2061            result = mItemCount;
2062        }
2063        return result;
2064    }
2065
2066    @Override
2067    protected float getTopFadingEdgeStrength() {
2068        final int count = getChildCount();
2069        final float fadeEdge = super.getTopFadingEdgeStrength();
2070        if (count == 0) {
2071            return fadeEdge;
2072        } else {
2073            if (mFirstPosition > 0) {
2074                return 1.0f;
2075            }
2076
2077            final int top = getChildAt(0).getTop();
2078            final float fadeLength = getVerticalFadingEdgeLength();
2079            return top < mPaddingTop ? -(top - mPaddingTop) / fadeLength : fadeEdge;
2080        }
2081    }
2082
2083    @Override
2084    protected float getBottomFadingEdgeStrength() {
2085        final int count = getChildCount();
2086        final float fadeEdge = super.getBottomFadingEdgeStrength();
2087        if (count == 0) {
2088            return fadeEdge;
2089        } else {
2090            if (mFirstPosition + count - 1 < mItemCount - 1) {
2091                return 1.0f;
2092            }
2093
2094            final int bottom = getChildAt(count - 1).getBottom();
2095            final int height = getHeight();
2096            final float fadeLength = getVerticalFadingEdgeLength();
2097            return bottom > height - mPaddingBottom ?
2098                    (bottom - height + mPaddingBottom) / fadeLength : fadeEdge;
2099        }
2100    }
2101
2102    @Override
2103    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2104        if (mSelector == null) {
2105            useDefaultSelector();
2106        }
2107        final Rect listPadding = mListPadding;
2108        listPadding.left = mSelectionLeftPadding + mPaddingLeft;
2109        listPadding.top = mSelectionTopPadding + mPaddingTop;
2110        listPadding.right = mSelectionRightPadding + mPaddingRight;
2111        listPadding.bottom = mSelectionBottomPadding + mPaddingBottom;
2112
2113        // Check if our previous measured size was at a point where we should scroll later.
2114        if (mTranscriptMode == TRANSCRIPT_MODE_NORMAL) {
2115            final int childCount = getChildCount();
2116            final int listBottom = getHeight() - getPaddingBottom();
2117            final View lastChild = getChildAt(childCount - 1);
2118            final int lastBottom = lastChild != null ? lastChild.getBottom() : listBottom;
2119            mForceTranscriptScroll = mFirstPosition + childCount >= mLastHandledItemCount &&
2120                    lastBottom <= listBottom;
2121        }
2122    }
2123
2124    /**
2125     * Subclasses should NOT override this method but
2126     *  {@link #layoutChildren()} instead.
2127     */
2128    @Override
2129    protected void onLayout(boolean changed, int l, int t, int r, int b) {
2130        super.onLayout(changed, l, t, r, b);
2131
2132        mInLayout = true;
2133
2134        final int childCount = getChildCount();
2135        if (changed) {
2136            for (int i = 0; i < childCount; i++) {
2137                getChildAt(i).forceLayout();
2138            }
2139            mRecycler.markChildrenDirty();
2140        }
2141
2142        layoutChildren();
2143        mInLayout = false;
2144
2145        mOverscrollMax = (b - t) / OVERSCROLL_LIMIT_DIVISOR;
2146
2147        // TODO: Move somewhere sane. This doesn't belong in onLayout().
2148        if (mFastScroll != null) {
2149            mFastScroll.onItemCountChanged(getChildCount(), mItemCount);
2150        }
2151    }
2152
2153    /**
2154     * @hide
2155     */
2156    @Override
2157    protected boolean setFrame(int left, int top, int right, int bottom) {
2158        final boolean changed = super.setFrame(left, top, right, bottom);
2159
2160        if (changed) {
2161            // Reposition the popup when the frame has changed. This includes
2162            // translating the widget, not just changing its dimension. The
2163            // filter popup needs to follow the widget.
2164            final boolean visible = getWindowVisibility() == View.VISIBLE;
2165            if (mFiltered && visible && mPopup != null && mPopup.isShowing()) {
2166                positionPopup();
2167            }
2168        }
2169
2170        return changed;
2171    }
2172
2173    /**
2174     * Subclasses must override this method to layout their children.
2175     */
2176    protected void layoutChildren() {
2177    }
2178
2179    /**
2180     * @param focusedView view that holds accessibility focus
2181     * @return direct child that contains accessibility focus, or null if no
2182     *         child contains accessibility focus
2183     */
2184    View getAccessibilityFocusedChild(View focusedView) {
2185        ViewParent viewParent = focusedView.getParent();
2186        while ((viewParent instanceof View) && (viewParent != this)) {
2187            focusedView = (View) viewParent;
2188            viewParent = viewParent.getParent();
2189        }
2190
2191        if (!(viewParent instanceof View)) {
2192            return null;
2193        }
2194
2195        return focusedView;
2196    }
2197
2198    void updateScrollIndicators() {
2199        if (mScrollUp != null) {
2200            boolean canScrollUp;
2201            // 0th element is not visible
2202            canScrollUp = mFirstPosition > 0;
2203
2204            // ... Or top of 0th element is not visible
2205            if (!canScrollUp) {
2206                if (getChildCount() > 0) {
2207                    View child = getChildAt(0);
2208                    canScrollUp = child.getTop() < mListPadding.top;
2209                }
2210            }
2211
2212            mScrollUp.setVisibility(canScrollUp ? View.VISIBLE : View.INVISIBLE);
2213        }
2214
2215        if (mScrollDown != null) {
2216            boolean canScrollDown;
2217            int count = getChildCount();
2218
2219            // Last item is not visible
2220            canScrollDown = (mFirstPosition + count) < mItemCount;
2221
2222            // ... Or bottom of the last element is not visible
2223            if (!canScrollDown && count > 0) {
2224                View child = getChildAt(count - 1);
2225                canScrollDown = child.getBottom() > mBottom - mListPadding.bottom;
2226            }
2227
2228            mScrollDown.setVisibility(canScrollDown ? View.VISIBLE : View.INVISIBLE);
2229        }
2230    }
2231
2232    @Override
2233    @ViewDebug.ExportedProperty
2234    public View getSelectedView() {
2235        if (mItemCount > 0 && mSelectedPosition >= 0) {
2236            return getChildAt(mSelectedPosition - mFirstPosition);
2237        } else {
2238            return null;
2239        }
2240    }
2241
2242    /**
2243     * List padding is the maximum of the normal view's padding and the padding of the selector.
2244     *
2245     * @see android.view.View#getPaddingTop()
2246     * @see #getSelector()
2247     *
2248     * @return The top list padding.
2249     */
2250    public int getListPaddingTop() {
2251        return mListPadding.top;
2252    }
2253
2254    /**
2255     * List padding is the maximum of the normal view's padding and the padding of the selector.
2256     *
2257     * @see android.view.View#getPaddingBottom()
2258     * @see #getSelector()
2259     *
2260     * @return The bottom list padding.
2261     */
2262    public int getListPaddingBottom() {
2263        return mListPadding.bottom;
2264    }
2265
2266    /**
2267     * List padding is the maximum of the normal view's padding and the padding of the selector.
2268     *
2269     * @see android.view.View#getPaddingLeft()
2270     * @see #getSelector()
2271     *
2272     * @return The left list padding.
2273     */
2274    public int getListPaddingLeft() {
2275        return mListPadding.left;
2276    }
2277
2278    /**
2279     * List padding is the maximum of the normal view's padding and the padding of the selector.
2280     *
2281     * @see android.view.View#getPaddingRight()
2282     * @see #getSelector()
2283     *
2284     * @return The right list padding.
2285     */
2286    public int getListPaddingRight() {
2287        return mListPadding.right;
2288    }
2289
2290    /**
2291     * Get a view and have it show the data associated with the specified
2292     * position. This is called when we have already discovered that the view is
2293     * not available for reuse in the recycle bin. The only choices left are
2294     * converting an old view or making a new one.
2295     *
2296     * @param position The position to display
2297     * @param isScrap Array of at least 1 boolean, the first entry will become true if
2298     *                the returned view was taken from the scrap heap, false if otherwise.
2299     *
2300     * @return A view displaying the data associated with the specified position
2301     */
2302    View obtainView(int position, boolean[] isScrap) {
2303        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "obtainView");
2304
2305        isScrap[0] = false;
2306
2307        // Check whether we have a transient state view. Attempt to re-bind the
2308        // data and discard the view if we fail.
2309        final View transientView = mRecycler.getTransientStateView(position);
2310        if (transientView != null) {
2311            final LayoutParams params = (LayoutParams) transientView.getLayoutParams();
2312
2313            // If the view type hasn't changed, attempt to re-bind the data.
2314            if (params.viewType == mAdapter.getItemViewType(position)) {
2315                final View updatedView = mAdapter.getView(position, transientView, this);
2316
2317                // If we failed to re-bind the data, scrap the obtained view.
2318                if (updatedView != transientView) {
2319                    setItemViewLayoutParams(updatedView, position);
2320                    mRecycler.addScrapView(updatedView, position);
2321                }
2322            }
2323
2324            // Scrap view implies temporary detachment.
2325            isScrap[0] = true;
2326            return transientView;
2327        }
2328
2329        final View scrapView = mRecycler.getScrapView(position);
2330        final View child = mAdapter.getView(position, scrapView, this);
2331        if (scrapView != null) {
2332            if (child != scrapView) {
2333                // Failed to re-bind the data, return scrap to the heap.
2334                mRecycler.addScrapView(scrapView, position);
2335            } else {
2336                isScrap[0] = true;
2337
2338                child.dispatchFinishTemporaryDetach();
2339            }
2340        }
2341
2342        if (mCacheColorHint != 0) {
2343            child.setDrawingCacheBackgroundColor(mCacheColorHint);
2344        }
2345
2346        if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
2347            child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
2348        }
2349
2350        setItemViewLayoutParams(child, position);
2351
2352        if (AccessibilityManager.getInstance(mContext).isEnabled()) {
2353            if (mAccessibilityDelegate == null) {
2354                mAccessibilityDelegate = new ListItemAccessibilityDelegate();
2355            }
2356            if (child.getAccessibilityDelegate() == null) {
2357                child.setAccessibilityDelegate(mAccessibilityDelegate);
2358            }
2359        }
2360
2361        Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2362
2363        return child;
2364    }
2365
2366    private void setItemViewLayoutParams(View child, int position) {
2367        final ViewGroup.LayoutParams vlp = child.getLayoutParams();
2368        LayoutParams lp;
2369        if (vlp == null) {
2370            lp = (LayoutParams) generateDefaultLayoutParams();
2371        } else if (!checkLayoutParams(vlp)) {
2372            lp = (LayoutParams) generateLayoutParams(vlp);
2373        } else {
2374            lp = (LayoutParams) vlp;
2375        }
2376
2377        if (mAdapterHasStableIds) {
2378            lp.itemId = mAdapter.getItemId(position);
2379        }
2380        lp.viewType = mAdapter.getItemViewType(position);
2381        child.setLayoutParams(lp);
2382    }
2383
2384    class ListItemAccessibilityDelegate extends AccessibilityDelegate {
2385        @Override
2386        public AccessibilityNodeInfo createAccessibilityNodeInfo(View host) {
2387            // If the data changed the children are invalid since the data model changed.
2388            // Hence, we pretend they do not exist. After a layout the children will sync
2389            // with the model at which point we notify that the accessibility state changed,
2390            // so a service will be able to re-fetch the views.
2391            if (mDataChanged) {
2392                return null;
2393            }
2394            return super.createAccessibilityNodeInfo(host);
2395        }
2396
2397        @Override
2398        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
2399            super.onInitializeAccessibilityNodeInfo(host, info);
2400
2401            final int position = getPositionForView(host);
2402            onInitializeAccessibilityNodeInfoForItem(host, position, info);
2403        }
2404
2405        @Override
2406        public boolean performAccessibilityAction(View host, int action, Bundle arguments) {
2407            if (super.performAccessibilityAction(host, action, arguments)) {
2408                return true;
2409            }
2410
2411            final int position = getPositionForView(host);
2412            final ListAdapter adapter = getAdapter();
2413
2414            if ((position == INVALID_POSITION) || (adapter == null)) {
2415                // Cannot perform actions on invalid items.
2416                return false;
2417            }
2418
2419            if (!isEnabled() || !adapter.isEnabled(position)) {
2420                // Cannot perform actions on disabled items.
2421                return false;
2422            }
2423
2424            final long id = getItemIdAtPosition(position);
2425
2426            switch (action) {
2427                case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
2428                    if (getSelectedItemPosition() == position) {
2429                        setSelection(INVALID_POSITION);
2430                        return true;
2431                    }
2432                } return false;
2433                case AccessibilityNodeInfo.ACTION_SELECT: {
2434                    if (getSelectedItemPosition() != position) {
2435                        setSelection(position);
2436                        return true;
2437                    }
2438                } return false;
2439                case AccessibilityNodeInfo.ACTION_CLICK: {
2440                    if (isClickable()) {
2441                        return performItemClick(host, position, id);
2442                    }
2443                } return false;
2444                case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
2445                    if (isLongClickable()) {
2446                        return performLongPress(host, position, id);
2447                    }
2448                } return false;
2449            }
2450
2451            return false;
2452        }
2453    }
2454
2455    /**
2456     * Initializes an {@link AccessibilityNodeInfo} with information about a
2457     * particular item in the list.
2458     *
2459     * @param view View representing the list item.
2460     * @param position Position of the list item within the adapter.
2461     * @param info Node info to populate.
2462     */
2463    public void onInitializeAccessibilityNodeInfoForItem(
2464            View view, int position, AccessibilityNodeInfo info) {
2465        final ListAdapter adapter = getAdapter();
2466        if (position == INVALID_POSITION || adapter == null) {
2467            // The item doesn't exist, so there's not much we can do here.
2468            return;
2469        }
2470
2471        if (!isEnabled() || !adapter.isEnabled(position)) {
2472            info.setEnabled(false);
2473            return;
2474        }
2475
2476        if (position == getSelectedItemPosition()) {
2477            info.setSelected(true);
2478            info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
2479        } else {
2480            info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
2481        }
2482
2483        if (isClickable()) {
2484            info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
2485            info.setClickable(true);
2486        }
2487
2488        if (isLongClickable()) {
2489            info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
2490            info.setLongClickable(true);
2491        }
2492    }
2493
2494    /**
2495     * Positions the selector in a way that mimics touch.
2496     */
2497    void positionSelectorLikeTouch(int position, View sel, float x, float y) {
2498        positionSelectorLikeFocus(position, sel);
2499
2500        if (mSelector != null && position != INVALID_POSITION) {
2501            mSelector.setHotspot(x, y);
2502        }
2503    }
2504
2505    /**
2506     * Positions the selector in a way that mimics keyboard focus.
2507     */
2508    void positionSelectorLikeFocus(int position, View sel) {
2509        // If we're changing position, update the visibility since the selector
2510        // is technically being detached from the previous selection.
2511        final Drawable selector = mSelector;
2512        final boolean manageState = selector != null && mSelectorPosition != position
2513                && position != INVALID_POSITION;
2514        if (manageState) {
2515            selector.setVisible(false, false);
2516        }
2517
2518        positionSelector(position, sel);
2519
2520        if (manageState) {
2521            final Rect bounds = mSelectorRect;
2522            final float x = bounds.exactCenterX();
2523            final float y = bounds.exactCenterY();
2524            selector.setVisible(getVisibility() == VISIBLE, false);
2525            selector.setHotspot(x, y);
2526        }
2527    }
2528
2529    void positionSelector(int position, View sel) {
2530        if (position != INVALID_POSITION) {
2531            mSelectorPosition = position;
2532        }
2533
2534        final Rect selectorRect = mSelectorRect;
2535        selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());
2536        if (sel instanceof SelectionBoundsAdjuster) {
2537            ((SelectionBoundsAdjuster)sel).adjustListItemSelectionBounds(selectorRect);
2538        }
2539
2540        // Adjust for selection padding.
2541        selectorRect.left -= mSelectionLeftPadding;
2542        selectorRect.top -= mSelectionTopPadding;
2543        selectorRect.right += mSelectionRightPadding;
2544        selectorRect.bottom += mSelectionBottomPadding;
2545
2546        // Update the selector drawable.
2547        final Drawable selector = mSelector;
2548        if (selector != null) {
2549            selector.setBounds(selectorRect);
2550        }
2551
2552        final boolean isChildViewEnabled = mIsChildViewEnabled;
2553        if (sel.isEnabled() != isChildViewEnabled) {
2554            mIsChildViewEnabled = !isChildViewEnabled;
2555            if (getSelectedItemPosition() != INVALID_POSITION) {
2556                refreshDrawableState();
2557            }
2558        }
2559    }
2560
2561    @Override
2562    protected void dispatchDraw(Canvas canvas) {
2563        int saveCount = 0;
2564        final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
2565        if (clipToPadding) {
2566            saveCount = canvas.save();
2567            final int scrollX = mScrollX;
2568            final int scrollY = mScrollY;
2569            canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop,
2570                    scrollX + mRight - mLeft - mPaddingRight,
2571                    scrollY + mBottom - mTop - mPaddingBottom);
2572            mGroupFlags &= ~CLIP_TO_PADDING_MASK;
2573        }
2574
2575        final boolean drawSelectorOnTop = mDrawSelectorOnTop;
2576        if (!drawSelectorOnTop) {
2577            drawSelector(canvas);
2578        }
2579
2580        super.dispatchDraw(canvas);
2581
2582        if (drawSelectorOnTop) {
2583            drawSelector(canvas);
2584        }
2585
2586        if (clipToPadding) {
2587            canvas.restoreToCount(saveCount);
2588            mGroupFlags |= CLIP_TO_PADDING_MASK;
2589        }
2590    }
2591
2592    @Override
2593    protected boolean isPaddingOffsetRequired() {
2594        return (mGroupFlags & CLIP_TO_PADDING_MASK) != CLIP_TO_PADDING_MASK;
2595    }
2596
2597    @Override
2598    protected int getLeftPaddingOffset() {
2599        return (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK ? 0 : -mPaddingLeft;
2600    }
2601
2602    @Override
2603    protected int getTopPaddingOffset() {
2604        return (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK ? 0 : -mPaddingTop;
2605    }
2606
2607    @Override
2608    protected int getRightPaddingOffset() {
2609        return (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK ? 0 : mPaddingRight;
2610    }
2611
2612    @Override
2613    protected int getBottomPaddingOffset() {
2614        return (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK ? 0 : mPaddingBottom;
2615    }
2616
2617    @Override
2618    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
2619        if (getChildCount() > 0) {
2620            mDataChanged = true;
2621            rememberSyncState();
2622        }
2623
2624        if (mFastScroll != null) {
2625            mFastScroll.onSizeChanged(w, h, oldw, oldh);
2626        }
2627    }
2628
2629    /**
2630     * @return True if the current touch mode requires that we draw the selector in the pressed
2631     *         state.
2632     */
2633    boolean touchModeDrawsInPressedState() {
2634        // FIXME use isPressed for this
2635        switch (mTouchMode) {
2636        case TOUCH_MODE_TAP:
2637        case TOUCH_MODE_DONE_WAITING:
2638            return true;
2639        default:
2640            return false;
2641        }
2642    }
2643
2644    /**
2645     * Indicates whether this view is in a state where the selector should be drawn. This will
2646     * happen if we have focus but are not in touch mode, or we are in the middle of displaying
2647     * the pressed state for an item.
2648     *
2649     * @return True if the selector should be shown
2650     */
2651    boolean shouldShowSelector() {
2652        return (!isInTouchMode()) || (touchModeDrawsInPressedState() && isPressed());
2653    }
2654
2655    private void drawSelector(Canvas canvas) {
2656        if (!mSelectorRect.isEmpty()) {
2657            final Drawable selector = mSelector;
2658            selector.setBounds(mSelectorRect);
2659            selector.draw(canvas);
2660        }
2661    }
2662
2663    /**
2664     * Controls whether the selection highlight drawable should be drawn on top of the item or
2665     * behind it.
2666     *
2667     * @param onTop If true, the selector will be drawn on the item it is highlighting. The default
2668     *        is false.
2669     *
2670     * @attr ref android.R.styleable#AbsListView_drawSelectorOnTop
2671     */
2672    public void setDrawSelectorOnTop(boolean onTop) {
2673        mDrawSelectorOnTop = onTop;
2674    }
2675
2676    /**
2677     * Set a Drawable that should be used to highlight the currently selected item.
2678     *
2679     * @param resID A Drawable resource to use as the selection highlight.
2680     *
2681     * @attr ref android.R.styleable#AbsListView_listSelector
2682     */
2683    public void setSelector(int resID) {
2684        setSelector(getContext().getDrawable(resID));
2685    }
2686
2687    public void setSelector(Drawable sel) {
2688        if (mSelector != null) {
2689            mSelector.setCallback(null);
2690            unscheduleDrawable(mSelector);
2691        }
2692        mSelector = sel;
2693        Rect padding = new Rect();
2694        sel.getPadding(padding);
2695        mSelectionLeftPadding = padding.left;
2696        mSelectionTopPadding = padding.top;
2697        mSelectionRightPadding = padding.right;
2698        mSelectionBottomPadding = padding.bottom;
2699        sel.setCallback(this);
2700        updateSelectorState();
2701    }
2702
2703    /**
2704     * Returns the selector {@link android.graphics.drawable.Drawable} that is used to draw the
2705     * selection in the list.
2706     *
2707     * @return the drawable used to display the selector
2708     */
2709    public Drawable getSelector() {
2710        return mSelector;
2711    }
2712
2713    /**
2714     * Sets the selector state to "pressed" and posts a CheckForKeyLongPress to see if
2715     * this is a long press.
2716     */
2717    void keyPressed() {
2718        if (!isEnabled() || !isClickable()) {
2719            return;
2720        }
2721
2722        Drawable selector = mSelector;
2723        Rect selectorRect = mSelectorRect;
2724        if (selector != null && (isFocused() || touchModeDrawsInPressedState())
2725                && !selectorRect.isEmpty()) {
2726
2727            final View v = getChildAt(mSelectedPosition - mFirstPosition);
2728
2729            if (v != null) {
2730                if (v.hasFocusable()) return;
2731                v.setPressed(true);
2732            }
2733            setPressed(true);
2734
2735            final boolean longClickable = isLongClickable();
2736            Drawable d = selector.getCurrent();
2737            if (d != null && d instanceof TransitionDrawable) {
2738                if (longClickable) {
2739                    ((TransitionDrawable) d).startTransition(
2740                            ViewConfiguration.getLongPressTimeout());
2741                } else {
2742                    ((TransitionDrawable) d).resetTransition();
2743                }
2744            }
2745            if (longClickable && !mDataChanged) {
2746                if (mPendingCheckForKeyLongPress == null) {
2747                    mPendingCheckForKeyLongPress = new CheckForKeyLongPress();
2748                }
2749                mPendingCheckForKeyLongPress.rememberWindowAttachCount();
2750                postDelayed(mPendingCheckForKeyLongPress, ViewConfiguration.getLongPressTimeout());
2751            }
2752        }
2753    }
2754
2755    public void setScrollIndicators(View up, View down) {
2756        mScrollUp = up;
2757        mScrollDown = down;
2758    }
2759
2760    void updateSelectorState() {
2761        if (mSelector != null) {
2762            if (shouldShowSelector()) {
2763                mSelector.setState(getDrawableState());
2764            } else {
2765                mSelector.setState(StateSet.NOTHING);
2766            }
2767        }
2768    }
2769
2770    @Override
2771    protected void drawableStateChanged() {
2772        super.drawableStateChanged();
2773        updateSelectorState();
2774    }
2775
2776    @Override
2777    protected int[] onCreateDrawableState(int extraSpace) {
2778        // If the child view is enabled then do the default behavior.
2779        if (mIsChildViewEnabled) {
2780            // Common case
2781            return super.onCreateDrawableState(extraSpace);
2782        }
2783
2784        // The selector uses this View's drawable state. The selected child view
2785        // is disabled, so we need to remove the enabled state from the drawable
2786        // states.
2787        final int enabledState = ENABLED_STATE_SET[0];
2788
2789        // If we don't have any extra space, it will return one of the static state arrays,
2790        // and clearing the enabled state on those arrays is a bad thing!  If we specify
2791        // we need extra space, it will create+copy into a new array that safely mutable.
2792        int[] state = super.onCreateDrawableState(extraSpace + 1);
2793        int enabledPos = -1;
2794        for (int i = state.length - 1; i >= 0; i--) {
2795            if (state[i] == enabledState) {
2796                enabledPos = i;
2797                break;
2798            }
2799        }
2800
2801        // Remove the enabled state
2802        if (enabledPos >= 0) {
2803            System.arraycopy(state, enabledPos + 1, state, enabledPos,
2804                    state.length - enabledPos - 1);
2805        }
2806
2807        return state;
2808    }
2809
2810    @Override
2811    public boolean verifyDrawable(Drawable dr) {
2812        return mSelector == dr || super.verifyDrawable(dr);
2813    }
2814
2815    @Override
2816    public void jumpDrawablesToCurrentState() {
2817        super.jumpDrawablesToCurrentState();
2818        if (mSelector != null) mSelector.jumpToCurrentState();
2819    }
2820
2821    @Override
2822    protected void onAttachedToWindow() {
2823        super.onAttachedToWindow();
2824
2825        final ViewTreeObserver treeObserver = getViewTreeObserver();
2826        treeObserver.addOnTouchModeChangeListener(this);
2827        if (mTextFilterEnabled && mPopup != null && !mGlobalLayoutListenerAddedFilter) {
2828            treeObserver.addOnGlobalLayoutListener(this);
2829        }
2830
2831        if (mAdapter != null && mDataSetObserver == null) {
2832            mDataSetObserver = new AdapterDataSetObserver();
2833            mAdapter.registerDataSetObserver(mDataSetObserver);
2834
2835            // Data may have changed while we were detached. Refresh.
2836            mDataChanged = true;
2837            mOldItemCount = mItemCount;
2838            mItemCount = mAdapter.getCount();
2839        }
2840    }
2841
2842    @Override
2843    protected void onDetachedFromWindow() {
2844        super.onDetachedFromWindow();
2845
2846        mIsDetaching = true;
2847
2848        // Dismiss the popup in case onSaveInstanceState() was not invoked
2849        dismissPopup();
2850
2851        // Detach any view left in the scrap heap
2852        mRecycler.clear();
2853
2854        final ViewTreeObserver treeObserver = getViewTreeObserver();
2855        treeObserver.removeOnTouchModeChangeListener(this);
2856        if (mTextFilterEnabled && mPopup != null) {
2857            treeObserver.removeOnGlobalLayoutListener(this);
2858            mGlobalLayoutListenerAddedFilter = false;
2859        }
2860
2861        if (mAdapter != null && mDataSetObserver != null) {
2862            mAdapter.unregisterDataSetObserver(mDataSetObserver);
2863            mDataSetObserver = null;
2864        }
2865
2866        if (mScrollStrictSpan != null) {
2867            mScrollStrictSpan.finish();
2868            mScrollStrictSpan = null;
2869        }
2870
2871        if (mFlingStrictSpan != null) {
2872            mFlingStrictSpan.finish();
2873            mFlingStrictSpan = null;
2874        }
2875
2876        if (mFlingRunnable != null) {
2877            removeCallbacks(mFlingRunnable);
2878        }
2879
2880        if (mPositionScroller != null) {
2881            mPositionScroller.stop();
2882        }
2883
2884        if (mClearScrollingCache != null) {
2885            removeCallbacks(mClearScrollingCache);
2886        }
2887
2888        if (mPerformClick != null) {
2889            removeCallbacks(mPerformClick);
2890        }
2891
2892        if (mTouchModeReset != null) {
2893            removeCallbacks(mTouchModeReset);
2894            mTouchModeReset.run();
2895        }
2896
2897        mIsDetaching = false;
2898    }
2899
2900    @Override
2901    public void onWindowFocusChanged(boolean hasWindowFocus) {
2902        super.onWindowFocusChanged(hasWindowFocus);
2903
2904        final int touchMode = isInTouchMode() ? TOUCH_MODE_ON : TOUCH_MODE_OFF;
2905
2906        if (!hasWindowFocus) {
2907            setChildrenDrawingCacheEnabled(false);
2908            if (mFlingRunnable != null) {
2909                removeCallbacks(mFlingRunnable);
2910                // let the fling runnable report it's new state which
2911                // should be idle
2912                mFlingRunnable.endFling();
2913                if (mPositionScroller != null) {
2914                    mPositionScroller.stop();
2915                }
2916                if (mScrollY != 0) {
2917                    mScrollY = 0;
2918                    invalidateParentCaches();
2919                    finishGlows();
2920                    invalidate();
2921                }
2922            }
2923            // Always hide the type filter
2924            dismissPopup();
2925
2926            if (touchMode == TOUCH_MODE_OFF) {
2927                // Remember the last selected element
2928                mResurrectToPosition = mSelectedPosition;
2929            }
2930        } else {
2931            if (mFiltered && !mPopupHidden) {
2932                // Show the type filter only if a filter is in effect
2933                showPopup();
2934            }
2935
2936            // If we changed touch mode since the last time we had focus
2937            if (touchMode != mLastTouchMode && mLastTouchMode != TOUCH_MODE_UNKNOWN) {
2938                // If we come back in trackball mode, we bring the selection back
2939                if (touchMode == TOUCH_MODE_OFF) {
2940                    // This will trigger a layout
2941                    resurrectSelection();
2942
2943                // If we come back in touch mode, then we want to hide the selector
2944                } else {
2945                    hideSelector();
2946                    mLayoutMode = LAYOUT_NORMAL;
2947                    layoutChildren();
2948                }
2949            }
2950        }
2951
2952        mLastTouchMode = touchMode;
2953    }
2954
2955    @Override
2956    public void onRtlPropertiesChanged(int layoutDirection) {
2957        super.onRtlPropertiesChanged(layoutDirection);
2958        if (mFastScroll != null) {
2959           mFastScroll.setScrollbarPosition(getVerticalScrollbarPosition());
2960        }
2961    }
2962
2963    /**
2964     * Creates the ContextMenuInfo returned from {@link #getContextMenuInfo()}. This
2965     * methods knows the view, position and ID of the item that received the
2966     * long press.
2967     *
2968     * @param view The view that received the long press.
2969     * @param position The position of the item that received the long press.
2970     * @param id The ID of the item that received the long press.
2971     * @return The extra information that should be returned by
2972     *         {@link #getContextMenuInfo()}.
2973     */
2974    ContextMenuInfo createContextMenuInfo(View view, int position, long id) {
2975        return new AdapterContextMenuInfo(view, position, id);
2976    }
2977
2978    @Override
2979    public void onCancelPendingInputEvents() {
2980        super.onCancelPendingInputEvents();
2981        if (mPerformClick != null) {
2982            removeCallbacks(mPerformClick);
2983        }
2984        if (mPendingCheckForTap != null) {
2985            removeCallbacks(mPendingCheckForTap);
2986        }
2987        if (mPendingCheckForLongPress != null) {
2988            removeCallbacks(mPendingCheckForLongPress);
2989        }
2990        if (mPendingCheckForKeyLongPress != null) {
2991            removeCallbacks(mPendingCheckForKeyLongPress);
2992        }
2993    }
2994
2995    /**
2996     * A base class for Runnables that will check that their view is still attached to
2997     * the original window as when the Runnable was created.
2998     *
2999     */
3000    private class WindowRunnnable {
3001        private int mOriginalAttachCount;
3002
3003        public void rememberWindowAttachCount() {
3004            mOriginalAttachCount = getWindowAttachCount();
3005        }
3006
3007        public boolean sameWindow() {
3008            return getWindowAttachCount() == mOriginalAttachCount;
3009        }
3010    }
3011
3012    private class PerformClick extends WindowRunnnable implements Runnable {
3013        int mClickMotionPosition;
3014
3015        @Override
3016        public void run() {
3017            // The data has changed since we posted this action in the event queue,
3018            // bail out before bad things happen
3019            if (mDataChanged) return;
3020
3021            final ListAdapter adapter = mAdapter;
3022            final int motionPosition = mClickMotionPosition;
3023            if (adapter != null && mItemCount > 0 &&
3024                    motionPosition != INVALID_POSITION &&
3025                    motionPosition < adapter.getCount() && sameWindow()) {
3026                final View view = getChildAt(motionPosition - mFirstPosition);
3027                // If there is no view, something bad happened (the view scrolled off the
3028                // screen, etc.) and we should cancel the click
3029                if (view != null) {
3030                    performItemClick(view, motionPosition, adapter.getItemId(motionPosition));
3031                }
3032            }
3033        }
3034    }
3035
3036    private class CheckForLongPress extends WindowRunnnable implements Runnable {
3037        @Override
3038        public void run() {
3039            final int motionPosition = mMotionPosition;
3040            final View child = getChildAt(motionPosition - mFirstPosition);
3041            if (child != null) {
3042                final int longPressPosition = mMotionPosition;
3043                final long longPressId = mAdapter.getItemId(mMotionPosition);
3044
3045                boolean handled = false;
3046                if (sameWindow() && !mDataChanged) {
3047                    handled = performLongPress(child, longPressPosition, longPressId);
3048                }
3049                if (handled) {
3050                    mTouchMode = TOUCH_MODE_REST;
3051                    setPressed(false);
3052                    child.setPressed(false);
3053                } else {
3054                    mTouchMode = TOUCH_MODE_DONE_WAITING;
3055                }
3056            }
3057        }
3058    }
3059
3060    private class CheckForKeyLongPress extends WindowRunnnable implements Runnable {
3061        @Override
3062        public void run() {
3063            if (isPressed() && mSelectedPosition >= 0) {
3064                int index = mSelectedPosition - mFirstPosition;
3065                View v = getChildAt(index);
3066
3067                if (!mDataChanged) {
3068                    boolean handled = false;
3069                    if (sameWindow()) {
3070                        handled = performLongPress(v, mSelectedPosition, mSelectedRowId);
3071                    }
3072                    if (handled) {
3073                        setPressed(false);
3074                        v.setPressed(false);
3075                    }
3076                } else {
3077                    setPressed(false);
3078                    if (v != null) v.setPressed(false);
3079                }
3080            }
3081        }
3082    }
3083
3084    boolean performLongPress(final View child,
3085            final int longPressPosition, final long longPressId) {
3086        // CHOICE_MODE_MULTIPLE_MODAL takes over long press.
3087        if (mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
3088            if (mChoiceActionMode == null &&
3089                    (mChoiceActionMode = startActionMode(mMultiChoiceModeCallback)) != null) {
3090                setItemChecked(longPressPosition, true);
3091                performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3092            }
3093            return true;
3094        }
3095
3096        boolean handled = false;
3097        if (mOnItemLongClickListener != null) {
3098            handled = mOnItemLongClickListener.onItemLongClick(AbsListView.this, child,
3099                    longPressPosition, longPressId);
3100        }
3101        if (!handled) {
3102            mContextMenuInfo = createContextMenuInfo(child, longPressPosition, longPressId);
3103            handled = super.showContextMenuForChild(AbsListView.this);
3104        }
3105        if (handled) {
3106            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3107        }
3108        return handled;
3109    }
3110
3111    @Override
3112    protected ContextMenuInfo getContextMenuInfo() {
3113        return mContextMenuInfo;
3114    }
3115
3116    /** @hide */
3117    @Override
3118    public boolean showContextMenu(float x, float y, int metaState) {
3119        final int position = pointToPosition((int)x, (int)y);
3120        if (position != INVALID_POSITION) {
3121            final long id = mAdapter.getItemId(position);
3122            View child = getChildAt(position - mFirstPosition);
3123            if (child != null) {
3124                mContextMenuInfo = createContextMenuInfo(child, position, id);
3125                return super.showContextMenuForChild(AbsListView.this);
3126            }
3127        }
3128        return super.showContextMenu(x, y, metaState);
3129    }
3130
3131    @Override
3132    public boolean showContextMenuForChild(View originalView) {
3133        final int longPressPosition = getPositionForView(originalView);
3134        if (longPressPosition >= 0) {
3135            final long longPressId = mAdapter.getItemId(longPressPosition);
3136            boolean handled = false;
3137
3138            if (mOnItemLongClickListener != null) {
3139                handled = mOnItemLongClickListener.onItemLongClick(AbsListView.this, originalView,
3140                        longPressPosition, longPressId);
3141            }
3142            if (!handled) {
3143                mContextMenuInfo = createContextMenuInfo(
3144                        getChildAt(longPressPosition - mFirstPosition),
3145                        longPressPosition, longPressId);
3146                handled = super.showContextMenuForChild(originalView);
3147            }
3148
3149            return handled;
3150        }
3151        return false;
3152    }
3153
3154    @Override
3155    public boolean onKeyDown(int keyCode, KeyEvent event) {
3156        return false;
3157    }
3158
3159    @Override
3160    public boolean onKeyUp(int keyCode, KeyEvent event) {
3161        if (KeyEvent.isConfirmKey(keyCode)) {
3162            if (!isEnabled()) {
3163                return true;
3164            }
3165            if (isClickable() && isPressed() &&
3166                    mSelectedPosition >= 0 && mAdapter != null &&
3167                    mSelectedPosition < mAdapter.getCount()) {
3168
3169                final View view = getChildAt(mSelectedPosition - mFirstPosition);
3170                if (view != null) {
3171                    performItemClick(view, mSelectedPosition, mSelectedRowId);
3172                    view.setPressed(false);
3173                }
3174                setPressed(false);
3175                return true;
3176            }
3177        }
3178        return super.onKeyUp(keyCode, event);
3179    }
3180
3181    @Override
3182    protected void dispatchSetPressed(boolean pressed) {
3183        // Don't dispatch setPressed to our children. We call setPressed on ourselves to
3184        // get the selector in the right state, but we don't want to press each child.
3185    }
3186
3187    /**
3188     * Maps a point to a position in the list.
3189     *
3190     * @param x X in local coordinate
3191     * @param y Y in local coordinate
3192     * @return The position of the item which contains the specified point, or
3193     *         {@link #INVALID_POSITION} if the point does not intersect an item.
3194     */
3195    public int pointToPosition(int x, int y) {
3196        Rect frame = mTouchFrame;
3197        if (frame == null) {
3198            mTouchFrame = new Rect();
3199            frame = mTouchFrame;
3200        }
3201
3202        final int count = getChildCount();
3203        for (int i = count - 1; i >= 0; i--) {
3204            final View child = getChildAt(i);
3205            if (child.getVisibility() == View.VISIBLE) {
3206                child.getHitRect(frame);
3207                if (frame.contains(x, y)) {
3208                    return mFirstPosition + i;
3209                }
3210            }
3211        }
3212        return INVALID_POSITION;
3213    }
3214
3215
3216    /**
3217     * Maps a point to a the rowId of the item which intersects that point.
3218     *
3219     * @param x X in local coordinate
3220     * @param y Y in local coordinate
3221     * @return The rowId of the item which contains the specified point, or {@link #INVALID_ROW_ID}
3222     *         if the point does not intersect an item.
3223     */
3224    public long pointToRowId(int x, int y) {
3225        int position = pointToPosition(x, y);
3226        if (position >= 0) {
3227            return mAdapter.getItemId(position);
3228        }
3229        return INVALID_ROW_ID;
3230    }
3231
3232    private final class CheckForTap implements Runnable {
3233        float x;
3234        float y;
3235
3236        @Override
3237        public void run() {
3238            if (mTouchMode == TOUCH_MODE_DOWN) {
3239                mTouchMode = TOUCH_MODE_TAP;
3240                final View child = getChildAt(mMotionPosition - mFirstPosition);
3241                if (child != null && !child.hasFocusable()) {
3242                    mLayoutMode = LAYOUT_NORMAL;
3243
3244                    if (!mDataChanged) {
3245                        child.setPressed(true);
3246                        setPressed(true);
3247                        layoutChildren();
3248                        positionSelector(mMotionPosition, child);
3249                        refreshDrawableState();
3250
3251                        final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
3252                        final boolean longClickable = isLongClickable();
3253
3254                        if (mSelector != null) {
3255                            final Drawable d = mSelector.getCurrent();
3256                            if (d != null && d instanceof TransitionDrawable) {
3257                                if (longClickable) {
3258                                    ((TransitionDrawable) d).startTransition(longPressTimeout);
3259                                } else {
3260                                    ((TransitionDrawable) d).resetTransition();
3261                                }
3262                            }
3263                            mSelector.setHotspot(x, y);
3264                        }
3265
3266                        if (longClickable) {
3267                            if (mPendingCheckForLongPress == null) {
3268                                mPendingCheckForLongPress = new CheckForLongPress();
3269                            }
3270                            mPendingCheckForLongPress.rememberWindowAttachCount();
3271                            postDelayed(mPendingCheckForLongPress, longPressTimeout);
3272                        } else {
3273                            mTouchMode = TOUCH_MODE_DONE_WAITING;
3274                        }
3275                    } else {
3276                        mTouchMode = TOUCH_MODE_DONE_WAITING;
3277                    }
3278                }
3279            }
3280        }
3281    }
3282
3283    private boolean startScrollIfNeeded(int x, int y, MotionEvent vtev) {
3284        // Check if we have moved far enough that it looks more like a
3285        // scroll than a tap
3286        final int deltaY = y - mMotionY;
3287        final int distance = Math.abs(deltaY);
3288        final boolean overscroll = mScrollY != 0;
3289        if ((overscroll || distance > mTouchSlop) &&
3290                (getNestedScrollAxes() & SCROLL_AXIS_VERTICAL) == 0) {
3291            createScrollingCache();
3292            if (overscroll) {
3293                mTouchMode = TOUCH_MODE_OVERSCROLL;
3294                mMotionCorrection = 0;
3295            } else {
3296                mTouchMode = TOUCH_MODE_SCROLL;
3297                mMotionCorrection = deltaY > 0 ? mTouchSlop : -mTouchSlop;
3298            }
3299            removeCallbacks(mPendingCheckForLongPress);
3300            setPressed(false);
3301            final View motionView = getChildAt(mMotionPosition - mFirstPosition);
3302            if (motionView != null) {
3303                motionView.setPressed(false);
3304            }
3305            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
3306            // Time to start stealing events! Once we've stolen them, don't let anyone
3307            // steal from us
3308            final ViewParent parent = getParent();
3309            if (parent != null) {
3310                parent.requestDisallowInterceptTouchEvent(true);
3311            }
3312            scrollIfNeeded(x, y, vtev);
3313            return true;
3314        }
3315
3316        return false;
3317    }
3318
3319    private void scrollIfNeeded(int x, int y, MotionEvent vtev) {
3320        int rawDeltaY = y - mMotionY;
3321        int scrollOffsetCorrection = 0;
3322        int scrollConsumedCorrection = 0;
3323        if (mLastY == Integer.MIN_VALUE) {
3324            rawDeltaY -= mMotionCorrection;
3325        }
3326        if (dispatchNestedPreScroll(0, mLastY != Integer.MIN_VALUE ? mLastY - y : -rawDeltaY,
3327                mScrollConsumed, mScrollOffset)) {
3328            rawDeltaY += mScrollConsumed[1];
3329            scrollOffsetCorrection = -mScrollOffset[1];
3330            scrollConsumedCorrection = mScrollConsumed[1];
3331            if (vtev != null) {
3332                vtev.offsetLocation(0, mScrollOffset[1]);
3333            }
3334        }
3335        final int deltaY = rawDeltaY;
3336        int incrementalDeltaY =
3337                mLastY != Integer.MIN_VALUE ? y - mLastY + scrollConsumedCorrection : deltaY;
3338        int lastYCorrection = 0;
3339
3340        if (mTouchMode == TOUCH_MODE_SCROLL) {
3341            if (PROFILE_SCROLLING) {
3342                if (!mScrollProfilingStarted) {
3343                    Debug.startMethodTracing("AbsListViewScroll");
3344                    mScrollProfilingStarted = true;
3345                }
3346            }
3347
3348            if (mScrollStrictSpan == null) {
3349                // If it's non-null, we're already in a scroll.
3350                mScrollStrictSpan = StrictMode.enterCriticalSpan("AbsListView-scroll");
3351            }
3352
3353            if (y != mLastY) {
3354                // We may be here after stopping a fling and continuing to scroll.
3355                // If so, we haven't disallowed intercepting touch events yet.
3356                // Make sure that we do so in case we're in a parent that can intercept.
3357                if ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) == 0 &&
3358                        Math.abs(rawDeltaY) > mTouchSlop) {
3359                    final ViewParent parent = getParent();
3360                    if (parent != null) {
3361                        parent.requestDisallowInterceptTouchEvent(true);
3362                    }
3363                }
3364
3365                final int motionIndex;
3366                if (mMotionPosition >= 0) {
3367                    motionIndex = mMotionPosition - mFirstPosition;
3368                } else {
3369                    // If we don't have a motion position that we can reliably track,
3370                    // pick something in the middle to make a best guess at things below.
3371                    motionIndex = getChildCount() / 2;
3372                }
3373
3374                int motionViewPrevTop = 0;
3375                View motionView = this.getChildAt(motionIndex);
3376                if (motionView != null) {
3377                    motionViewPrevTop = motionView.getTop();
3378                }
3379
3380                // No need to do all this work if we're not going to move anyway
3381                boolean atEdge = false;
3382                if (incrementalDeltaY != 0) {
3383                    atEdge = trackMotionScroll(deltaY, incrementalDeltaY);
3384                }
3385
3386                // Check to see if we have bumped into the scroll limit
3387                motionView = this.getChildAt(motionIndex);
3388                if (motionView != null) {
3389                    // Check if the top of the motion view is where it is
3390                    // supposed to be
3391                    final int motionViewRealTop = motionView.getTop();
3392                    if (atEdge) {
3393                        // Apply overscroll
3394
3395                        int overscroll = -incrementalDeltaY -
3396                                (motionViewRealTop - motionViewPrevTop);
3397                        if (dispatchNestedScroll(0, overscroll - incrementalDeltaY, 0, overscroll,
3398                                mScrollOffset)) {
3399                            lastYCorrection -= mScrollOffset[1];
3400                            if (vtev != null) {
3401                                vtev.offsetLocation(0, mScrollOffset[1]);
3402                            }
3403                        } else {
3404                            final boolean atOverscrollEdge = overScrollBy(0, overscroll,
3405                                    0, mScrollY, 0, 0, 0, mOverscrollDistance, true);
3406
3407                            if (atOverscrollEdge && mVelocityTracker != null) {
3408                                // Don't allow overfling if we're at the edge
3409                                mVelocityTracker.clear();
3410                            }
3411
3412                            final int overscrollMode = getOverScrollMode();
3413                            if (overscrollMode == OVER_SCROLL_ALWAYS ||
3414                                    (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS &&
3415                                            !contentFits())) {
3416                                if (!atOverscrollEdge) {
3417                                    mDirection = 0; // Reset when entering overscroll.
3418                                    mTouchMode = TOUCH_MODE_OVERSCROLL;
3419                                }
3420                                if (incrementalDeltaY > 0) {
3421                                    mEdgeGlowTop.onPull((float) -overscroll / getHeight(),
3422                                            (float) x / getWidth());
3423                                    if (!mEdgeGlowBottom.isFinished()) {
3424                                        mEdgeGlowBottom.onRelease();
3425                                    }
3426                                    invalidate(0, 0, getWidth(),
3427                                            mEdgeGlowTop.getMaxHeight() + getPaddingTop());
3428                                } else if (incrementalDeltaY < 0) {
3429                                    mEdgeGlowBottom.onPull((float) overscroll / getHeight(),
3430                                            1.f - (float) x / getWidth());
3431                                    if (!mEdgeGlowTop.isFinished()) {
3432                                        mEdgeGlowTop.onRelease();
3433                                    }
3434                                    invalidate(0, getHeight() - getPaddingBottom() -
3435                                            mEdgeGlowBottom.getMaxHeight(), getWidth(),
3436                                            getHeight());
3437                                }
3438                            }
3439                        }
3440                    }
3441                    mMotionY = y + lastYCorrection + scrollOffsetCorrection;
3442                }
3443                mLastY = y + lastYCorrection + scrollOffsetCorrection;
3444            }
3445        } else if (mTouchMode == TOUCH_MODE_OVERSCROLL) {
3446            if (y != mLastY) {
3447                final int oldScroll = mScrollY;
3448                final int newScroll = oldScroll - incrementalDeltaY;
3449                int newDirection = y > mLastY ? 1 : -1;
3450
3451                if (mDirection == 0) {
3452                    mDirection = newDirection;
3453                }
3454
3455                int overScrollDistance = -incrementalDeltaY;
3456                if ((newScroll < 0 && oldScroll >= 0) || (newScroll > 0 && oldScroll <= 0)) {
3457                    overScrollDistance = -oldScroll;
3458                    incrementalDeltaY += overScrollDistance;
3459                } else {
3460                    incrementalDeltaY = 0;
3461                }
3462
3463                if (overScrollDistance != 0) {
3464                    overScrollBy(0, overScrollDistance, 0, mScrollY, 0, 0,
3465                            0, mOverscrollDistance, true);
3466                    final int overscrollMode = getOverScrollMode();
3467                    if (overscrollMode == OVER_SCROLL_ALWAYS ||
3468                            (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS &&
3469                                    !contentFits())) {
3470                        if (rawDeltaY > 0) {
3471                            mEdgeGlowTop.onPull((float) overScrollDistance / getHeight(),
3472                                    (float) x / getWidth());
3473                            if (!mEdgeGlowBottom.isFinished()) {
3474                                mEdgeGlowBottom.onRelease();
3475                            }
3476                            invalidate(0, 0, getWidth(),
3477                                    mEdgeGlowTop.getMaxHeight() + getPaddingTop());
3478                        } else if (rawDeltaY < 0) {
3479                            mEdgeGlowBottom.onPull((float) overScrollDistance / getHeight(),
3480                                    1.f - (float) x / getWidth());
3481                            if (!mEdgeGlowTop.isFinished()) {
3482                                mEdgeGlowTop.onRelease();
3483                            }
3484                            invalidate(0, getHeight() - getPaddingBottom() -
3485                                    mEdgeGlowBottom.getMaxHeight(), getWidth(),
3486                                    getHeight());
3487                        }
3488                    }
3489                }
3490
3491                if (incrementalDeltaY != 0) {
3492                    // Coming back to 'real' list scrolling
3493                    if (mScrollY != 0) {
3494                        mScrollY = 0;
3495                        invalidateParentIfNeeded();
3496                    }
3497
3498                    trackMotionScroll(incrementalDeltaY, incrementalDeltaY);
3499
3500                    mTouchMode = TOUCH_MODE_SCROLL;
3501
3502                    // We did not scroll the full amount. Treat this essentially like the
3503                    // start of a new touch scroll
3504                    final int motionPosition = findClosestMotionRow(y);
3505
3506                    mMotionCorrection = 0;
3507                    View motionView = getChildAt(motionPosition - mFirstPosition);
3508                    mMotionViewOriginalTop = motionView != null ? motionView.getTop() : 0;
3509                    mMotionY =  y + scrollOffsetCorrection;
3510                    mMotionPosition = motionPosition;
3511                }
3512                mLastY = y + lastYCorrection + scrollOffsetCorrection;
3513                mDirection = newDirection;
3514            }
3515        }
3516    }
3517
3518    @Override
3519    public void onTouchModeChanged(boolean isInTouchMode) {
3520        if (isInTouchMode) {
3521            // Get rid of the selection when we enter touch mode
3522            hideSelector();
3523            // Layout, but only if we already have done so previously.
3524            // (Otherwise may clobber a LAYOUT_SYNC layout that was requested to restore
3525            // state.)
3526            if (getHeight() > 0 && getChildCount() > 0) {
3527                // We do not lose focus initiating a touch (since AbsListView is focusable in
3528                // touch mode). Force an initial layout to get rid of the selection.
3529                layoutChildren();
3530            }
3531            updateSelectorState();
3532        } else {
3533            int touchMode = mTouchMode;
3534            if (touchMode == TOUCH_MODE_OVERSCROLL || touchMode == TOUCH_MODE_OVERFLING) {
3535                if (mFlingRunnable != null) {
3536                    mFlingRunnable.endFling();
3537                }
3538                if (mPositionScroller != null) {
3539                    mPositionScroller.stop();
3540                }
3541
3542                if (mScrollY != 0) {
3543                    mScrollY = 0;
3544                    invalidateParentCaches();
3545                    finishGlows();
3546                    invalidate();
3547                }
3548            }
3549        }
3550    }
3551
3552    @Override
3553    public boolean onTouchEvent(MotionEvent ev) {
3554        if (!isEnabled()) {
3555            // A disabled view that is clickable still consumes the touch
3556            // events, it just doesn't respond to them.
3557            return isClickable() || isLongClickable();
3558        }
3559
3560        if (mPositionScroller != null) {
3561            mPositionScroller.stop();
3562        }
3563
3564        if (mIsDetaching || !isAttachedToWindow()) {
3565            // Something isn't right.
3566            // Since we rely on being attached to get data set change notifications,
3567            // don't risk doing anything where we might try to resync and find things
3568            // in a bogus state.
3569            return false;
3570        }
3571
3572        startNestedScroll(SCROLL_AXIS_VERTICAL);
3573
3574        if (mFastScroll != null) {
3575            boolean intercepted = mFastScroll.onTouchEvent(ev);
3576            if (intercepted) {
3577                return true;
3578            }
3579        }
3580
3581        initVelocityTrackerIfNotExists();
3582        final MotionEvent vtev = MotionEvent.obtain(ev);
3583
3584        final int actionMasked = ev.getActionMasked();
3585        switch (actionMasked) {
3586            case MotionEvent.ACTION_DOWN: {
3587                onTouchDown(ev);
3588                break;
3589            }
3590
3591            case MotionEvent.ACTION_MOVE: {
3592                onTouchMove(ev, vtev);
3593                break;
3594            }
3595
3596            case MotionEvent.ACTION_UP: {
3597                onTouchUp(ev);
3598                break;
3599            }
3600
3601            case MotionEvent.ACTION_CANCEL: {
3602                onTouchCancel();
3603                break;
3604            }
3605
3606            case MotionEvent.ACTION_POINTER_UP: {
3607                onSecondaryPointerUp(ev);
3608                final int x = mMotionX;
3609                final int y = mMotionY;
3610                final int motionPosition = pointToPosition(x, y);
3611                if (motionPosition >= 0) {
3612                    // Remember where the motion event started
3613                    final View child = getChildAt(motionPosition - mFirstPosition);
3614                    mMotionViewOriginalTop = child.getTop();
3615                    mMotionPosition = motionPosition;
3616                }
3617                mLastY = y;
3618                break;
3619            }
3620
3621            case MotionEvent.ACTION_POINTER_DOWN: {
3622                // New pointers take over dragging duties
3623                final int index = ev.getActionIndex();
3624                final int id = ev.getPointerId(index);
3625                final int x = (int) ev.getX(index);
3626                final int y = (int) ev.getY(index);
3627                mMotionCorrection = 0;
3628                mActivePointerId = id;
3629                mMotionX = x;
3630                mMotionY = y;
3631                final int motionPosition = pointToPosition(x, y);
3632                if (motionPosition >= 0) {
3633                    // Remember where the motion event started
3634                    final View child = getChildAt(motionPosition - mFirstPosition);
3635                    mMotionViewOriginalTop = child.getTop();
3636                    mMotionPosition = motionPosition;
3637                }
3638                mLastY = y;
3639                break;
3640            }
3641        }
3642
3643        if (mVelocityTracker != null) {
3644            mVelocityTracker.addMovement(vtev);
3645        }
3646        vtev.recycle();
3647        return true;
3648    }
3649
3650    private void onTouchDown(MotionEvent ev) {
3651        mActivePointerId = ev.getPointerId(0);
3652
3653        if (mTouchMode == TOUCH_MODE_OVERFLING) {
3654            // Stopped the fling. It is a scroll.
3655            mFlingRunnable.endFling();
3656            if (mPositionScroller != null) {
3657                mPositionScroller.stop();
3658            }
3659            mTouchMode = TOUCH_MODE_OVERSCROLL;
3660            mMotionX = (int) ev.getX();
3661            mMotionY = (int) ev.getY();
3662            mLastY = mMotionY;
3663            mMotionCorrection = 0;
3664            mDirection = 0;
3665        } else {
3666            final int x = (int) ev.getX();
3667            final int y = (int) ev.getY();
3668            int motionPosition = pointToPosition(x, y);
3669
3670            if (!mDataChanged) {
3671                if (mTouchMode == TOUCH_MODE_FLING) {
3672                    // Stopped a fling. It is a scroll.
3673                    createScrollingCache();
3674                    mTouchMode = TOUCH_MODE_SCROLL;
3675                    mMotionCorrection = 0;
3676                    motionPosition = findMotionRow(y);
3677                    mFlingRunnable.flywheelTouch();
3678                } else if ((motionPosition >= 0) && getAdapter().isEnabled(motionPosition)) {
3679                    // User clicked on an actual view (and was not stopping a
3680                    // fling). It might be a click or a scroll. Assume it is a
3681                    // click until proven otherwise.
3682                    mTouchMode = TOUCH_MODE_DOWN;
3683
3684                    // FIXME Debounce
3685                    if (mPendingCheckForTap == null) {
3686                        mPendingCheckForTap = new CheckForTap();
3687                    }
3688
3689                    mPendingCheckForTap.x = ev.getX();
3690                    mPendingCheckForTap.y = ev.getY();
3691                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
3692                }
3693            }
3694
3695            if (motionPosition >= 0) {
3696                // Remember where the motion event started
3697                final View v = getChildAt(motionPosition - mFirstPosition);
3698                mMotionViewOriginalTop = v.getTop();
3699            }
3700
3701            mMotionX = x;
3702            mMotionY = y;
3703            mMotionPosition = motionPosition;
3704            mLastY = Integer.MIN_VALUE;
3705        }
3706
3707        if (mTouchMode == TOUCH_MODE_DOWN && mMotionPosition != INVALID_POSITION
3708                && performButtonActionOnTouchDown(ev)) {
3709            removeCallbacks(mPendingCheckForTap);
3710        }
3711    }
3712
3713    private void onTouchMove(MotionEvent ev, MotionEvent vtev) {
3714        int pointerIndex = ev.findPointerIndex(mActivePointerId);
3715        if (pointerIndex == -1) {
3716            pointerIndex = 0;
3717            mActivePointerId = ev.getPointerId(pointerIndex);
3718        }
3719
3720        if (mDataChanged) {
3721            // Re-sync everything if data has been changed
3722            // since the scroll operation can query the adapter.
3723            layoutChildren();
3724        }
3725
3726        final int y = (int) ev.getY(pointerIndex);
3727
3728        switch (mTouchMode) {
3729            case TOUCH_MODE_DOWN:
3730            case TOUCH_MODE_TAP:
3731            case TOUCH_MODE_DONE_WAITING:
3732                // Check if we have moved far enough that it looks more like a
3733                // scroll than a tap. If so, we'll enter scrolling mode.
3734                if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, vtev)) {
3735                    break;
3736                }
3737                // Otherwise, check containment within list bounds. If we're
3738                // outside bounds, cancel any active presses.
3739                final float x = ev.getX(pointerIndex);
3740                if (!pointInView(x, y, mTouchSlop)) {
3741                    setPressed(false);
3742                    final View motionView = getChildAt(mMotionPosition - mFirstPosition);
3743                    if (motionView != null) {
3744                        motionView.setPressed(false);
3745                    }
3746                    removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ?
3747                            mPendingCheckForTap : mPendingCheckForLongPress);
3748                    mTouchMode = TOUCH_MODE_DONE_WAITING;
3749                    updateSelectorState();
3750                }
3751                break;
3752            case TOUCH_MODE_SCROLL:
3753            case TOUCH_MODE_OVERSCROLL:
3754                scrollIfNeeded((int) ev.getX(pointerIndex), y, vtev);
3755                break;
3756        }
3757    }
3758
3759    private void onTouchUp(MotionEvent ev) {
3760        switch (mTouchMode) {
3761        case TOUCH_MODE_DOWN:
3762        case TOUCH_MODE_TAP:
3763        case TOUCH_MODE_DONE_WAITING:
3764            final int motionPosition = mMotionPosition;
3765            final View child = getChildAt(motionPosition - mFirstPosition);
3766            if (child != null) {
3767                if (mTouchMode != TOUCH_MODE_DOWN) {
3768                    child.setPressed(false);
3769                }
3770
3771                final float x = ev.getX();
3772                final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right;
3773                if (inList && !child.hasFocusable()) {
3774                    if (mPerformClick == null) {
3775                        mPerformClick = new PerformClick();
3776                    }
3777
3778                    final AbsListView.PerformClick performClick = mPerformClick;
3779                    performClick.mClickMotionPosition = motionPosition;
3780                    performClick.rememberWindowAttachCount();
3781
3782                    mResurrectToPosition = motionPosition;
3783
3784                    if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
3785                        removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ?
3786                                mPendingCheckForTap : mPendingCheckForLongPress);
3787                        mLayoutMode = LAYOUT_NORMAL;
3788                        if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
3789                            mTouchMode = TOUCH_MODE_TAP;
3790                            setSelectedPositionInt(mMotionPosition);
3791                            layoutChildren();
3792                            child.setPressed(true);
3793                            positionSelector(mMotionPosition, child);
3794                            setPressed(true);
3795                            if (mSelector != null) {
3796                                Drawable d = mSelector.getCurrent();
3797                                if (d != null && d instanceof TransitionDrawable) {
3798                                    ((TransitionDrawable) d).resetTransition();
3799                                }
3800                                mSelector.setHotspot(x, ev.getY());
3801                            }
3802                            if (mTouchModeReset != null) {
3803                                removeCallbacks(mTouchModeReset);
3804                            }
3805                            mTouchModeReset = new Runnable() {
3806                                @Override
3807                                public void run() {
3808                                    mTouchModeReset = null;
3809                                    mTouchMode = TOUCH_MODE_REST;
3810                                    child.setPressed(false);
3811                                    setPressed(false);
3812                                    if (!mDataChanged && !mIsDetaching && isAttachedToWindow()) {
3813                                        performClick.run();
3814                                    }
3815                                }
3816                            };
3817                            postDelayed(mTouchModeReset,
3818                                    ViewConfiguration.getPressedStateDuration());
3819                        } else {
3820                            mTouchMode = TOUCH_MODE_REST;
3821                            updateSelectorState();
3822                        }
3823                        return;
3824                    } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
3825                        performClick.run();
3826                    }
3827                }
3828            }
3829            mTouchMode = TOUCH_MODE_REST;
3830            updateSelectorState();
3831            break;
3832        case TOUCH_MODE_SCROLL:
3833            final int childCount = getChildCount();
3834            if (childCount > 0) {
3835                final int firstChildTop = getChildAt(0).getTop();
3836                final int lastChildBottom = getChildAt(childCount - 1).getBottom();
3837                final int contentTop = mListPadding.top;
3838                final int contentBottom = getHeight() - mListPadding.bottom;
3839                if (mFirstPosition == 0 && firstChildTop >= contentTop &&
3840                        mFirstPosition + childCount < mItemCount &&
3841                        lastChildBottom <= getHeight() - contentBottom) {
3842                    mTouchMode = TOUCH_MODE_REST;
3843                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
3844                } else {
3845                    final VelocityTracker velocityTracker = mVelocityTracker;
3846                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
3847
3848                    final int initialVelocity = (int)
3849                            (velocityTracker.getYVelocity(mActivePointerId) * mVelocityScale);
3850                    // Fling if we have enough velocity and we aren't at a boundary.
3851                    // Since we can potentially overfling more than we can overscroll, don't
3852                    // allow the weird behavior where you can scroll to a boundary then
3853                    // fling further.
3854                    boolean flingVelocity = Math.abs(initialVelocity) > mMinimumVelocity;
3855                    if (flingVelocity &&
3856                            !((mFirstPosition == 0 &&
3857                                    firstChildTop == contentTop - mOverscrollDistance) ||
3858                              (mFirstPosition + childCount == mItemCount &&
3859                                    lastChildBottom == contentBottom + mOverscrollDistance))) {
3860                        if (!dispatchNestedPreFling(0, -initialVelocity)) {
3861                            if (mFlingRunnable == null) {
3862                                mFlingRunnable = new FlingRunnable();
3863                            }
3864                            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
3865                            mFlingRunnable.start(-initialVelocity);
3866                            dispatchNestedFling(0, -initialVelocity, true);
3867                        } else {
3868                            mTouchMode = TOUCH_MODE_REST;
3869                            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
3870                        }
3871                    } else {
3872                        mTouchMode = TOUCH_MODE_REST;
3873                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
3874                        if (mFlingRunnable != null) {
3875                            mFlingRunnable.endFling();
3876                        }
3877                        if (mPositionScroller != null) {
3878                            mPositionScroller.stop();
3879                        }
3880                        if (flingVelocity && !dispatchNestedPreFling(0, -initialVelocity)) {
3881                            dispatchNestedFling(0, -initialVelocity, false);
3882                        }
3883                    }
3884                }
3885            } else {
3886                mTouchMode = TOUCH_MODE_REST;
3887                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
3888            }
3889            break;
3890
3891        case TOUCH_MODE_OVERSCROLL:
3892            if (mFlingRunnable == null) {
3893                mFlingRunnable = new FlingRunnable();
3894            }
3895            final VelocityTracker velocityTracker = mVelocityTracker;
3896            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
3897            final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);
3898
3899            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
3900            if (Math.abs(initialVelocity) > mMinimumVelocity) {
3901                mFlingRunnable.startOverfling(-initialVelocity);
3902            } else {
3903                mFlingRunnable.startSpringback();
3904            }
3905
3906            break;
3907        }
3908
3909        setPressed(false);
3910
3911        if (mEdgeGlowTop != null) {
3912            mEdgeGlowTop.onRelease();
3913            mEdgeGlowBottom.onRelease();
3914        }
3915
3916        // Need to redraw since we probably aren't drawing the selector anymore
3917        invalidate();
3918        removeCallbacks(mPendingCheckForLongPress);
3919        recycleVelocityTracker();
3920
3921        mActivePointerId = INVALID_POINTER;
3922
3923        if (PROFILE_SCROLLING) {
3924            if (mScrollProfilingStarted) {
3925                Debug.stopMethodTracing();
3926                mScrollProfilingStarted = false;
3927            }
3928        }
3929
3930        if (mScrollStrictSpan != null) {
3931            mScrollStrictSpan.finish();
3932            mScrollStrictSpan = null;
3933        }
3934    }
3935
3936    private void onTouchCancel() {
3937        switch (mTouchMode) {
3938        case TOUCH_MODE_OVERSCROLL:
3939            if (mFlingRunnable == null) {
3940                mFlingRunnable = new FlingRunnable();
3941            }
3942            mFlingRunnable.startSpringback();
3943            break;
3944
3945        case TOUCH_MODE_OVERFLING:
3946            // Do nothing - let it play out.
3947            break;
3948
3949        default:
3950            mTouchMode = TOUCH_MODE_REST;
3951            setPressed(false);
3952            final View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
3953            if (motionView != null) {
3954                motionView.setPressed(false);
3955            }
3956            clearScrollingCache();
3957            removeCallbacks(mPendingCheckForLongPress);
3958            recycleVelocityTracker();
3959        }
3960
3961        if (mEdgeGlowTop != null) {
3962            mEdgeGlowTop.onRelease();
3963            mEdgeGlowBottom.onRelease();
3964        }
3965        mActivePointerId = INVALID_POINTER;
3966    }
3967
3968    @Override
3969    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
3970        if (mScrollY != scrollY) {
3971            onScrollChanged(mScrollX, scrollY, mScrollX, mScrollY);
3972            mScrollY = scrollY;
3973            invalidateParentIfNeeded();
3974
3975            awakenScrollBars();
3976        }
3977    }
3978
3979    @Override
3980    public boolean onGenericMotionEvent(MotionEvent event) {
3981        if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
3982            switch (event.getAction()) {
3983                case MotionEvent.ACTION_SCROLL: {
3984                    if (mTouchMode == TOUCH_MODE_REST) {
3985                        final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
3986                        if (vscroll != 0) {
3987                            final int delta = (int) (vscroll * getVerticalScrollFactor());
3988                            if (!trackMotionScroll(delta, delta)) {
3989                                return true;
3990                            }
3991                        }
3992                    }
3993                }
3994            }
3995        }
3996        return super.onGenericMotionEvent(event);
3997    }
3998
3999    /**
4000     * Initiate a fling with the given velocity.
4001     *
4002     * <p>Applications can use this method to manually initiate a fling as if the user
4003     * initiated it via touch interaction.</p>
4004     *
4005     * @param velocityY Vertical velocity in pixels per second. Note that this is velocity of
4006     *                  content, not velocity of a touch that initiated the fling.
4007     */
4008    public void fling(int velocityY) {
4009        if (mFlingRunnable == null) {
4010            mFlingRunnable = new FlingRunnable();
4011        }
4012        reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
4013        mFlingRunnable.start(velocityY);
4014    }
4015
4016    @Override
4017    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
4018        return ((nestedScrollAxes & SCROLL_AXIS_VERTICAL) != 0);
4019    }
4020
4021    @Override
4022    public void onNestedScrollAccepted(View child, View target, int axes) {
4023        super.onNestedScrollAccepted(child, target, axes);
4024        startNestedScroll(SCROLL_AXIS_VERTICAL);
4025    }
4026
4027    @Override
4028    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
4029            int dxUnconsumed, int dyUnconsumed) {
4030        final int motionIndex = getChildCount() / 2;
4031        final View motionView = getChildAt(motionIndex);
4032        final int oldTop = motionView != null ? motionView.getTop() : 0;
4033        if (motionView == null || trackMotionScroll(-dyUnconsumed, -dyUnconsumed)) {
4034            int myUnconsumed = dyUnconsumed;
4035            int myConsumed = 0;
4036            if (motionView != null) {
4037                myConsumed = motionView.getTop() - oldTop;
4038                myUnconsumed -= myConsumed;
4039            }
4040            dispatchNestedScroll(0, myConsumed, 0, myUnconsumed, null);
4041        }
4042    }
4043
4044    @Override
4045    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
4046        final int childCount = getChildCount();
4047        if (!consumed && childCount > 0 && canScrollList((int) velocityY) &&
4048                Math.abs(velocityY) > mMinimumVelocity) {
4049            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
4050            if (mFlingRunnable == null) {
4051                mFlingRunnable = new FlingRunnable();
4052            }
4053            if (!dispatchNestedPreFling(0, velocityY)) {
4054                mFlingRunnable.start((int) velocityY);
4055            }
4056            return true;
4057        }
4058        return dispatchNestedFling(velocityX, velocityY, consumed);
4059    }
4060
4061    @Override
4062    public void draw(Canvas canvas) {
4063        super.draw(canvas);
4064        if (mEdgeGlowTop != null) {
4065            final int scrollY = mScrollY;
4066            if (!mEdgeGlowTop.isFinished()) {
4067                final int restoreCount = canvas.save();
4068                final int width = getWidth();
4069
4070                int edgeY = Math.min(0, scrollY + mFirstPositionDistanceGuess);
4071                canvas.translate(0, edgeY);
4072                mEdgeGlowTop.setSize(width, getHeight());
4073                if (mEdgeGlowTop.draw(canvas)) {
4074                    invalidate(0, 0, getWidth(),
4075                            mEdgeGlowTop.getMaxHeight() + getPaddingTop());
4076                }
4077                canvas.restoreToCount(restoreCount);
4078            }
4079            if (!mEdgeGlowBottom.isFinished()) {
4080                final int restoreCount = canvas.save();
4081                final int width = getWidth();
4082                final int height = getHeight();
4083
4084                int edgeX = -width;
4085                int edgeY = Math.max(height, scrollY + mLastPositionDistanceGuess);
4086                canvas.translate(edgeX, edgeY);
4087                canvas.rotate(180, width, 0);
4088                mEdgeGlowBottom.setSize(width, height);
4089                if (mEdgeGlowBottom.draw(canvas)) {
4090                    invalidate(0, getHeight() - getPaddingBottom() -
4091                            mEdgeGlowBottom.getMaxHeight(), getWidth(),
4092                            getHeight());
4093                }
4094                canvas.restoreToCount(restoreCount);
4095            }
4096        }
4097    }
4098
4099    /**
4100     * @hide
4101     */
4102    public void setOverScrollEffectPadding(int leftPadding, int rightPadding) {
4103        mGlowPaddingLeft = leftPadding;
4104        mGlowPaddingRight = rightPadding;
4105    }
4106
4107    private void initOrResetVelocityTracker() {
4108        if (mVelocityTracker == null) {
4109            mVelocityTracker = VelocityTracker.obtain();
4110        } else {
4111            mVelocityTracker.clear();
4112        }
4113    }
4114
4115    private void initVelocityTrackerIfNotExists() {
4116        if (mVelocityTracker == null) {
4117            mVelocityTracker = VelocityTracker.obtain();
4118        }
4119    }
4120
4121    private void recycleVelocityTracker() {
4122        if (mVelocityTracker != null) {
4123            mVelocityTracker.recycle();
4124            mVelocityTracker = null;
4125        }
4126    }
4127
4128    @Override
4129    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
4130        if (disallowIntercept) {
4131            recycleVelocityTracker();
4132        }
4133        super.requestDisallowInterceptTouchEvent(disallowIntercept);
4134    }
4135
4136    @Override
4137    public boolean onInterceptHoverEvent(MotionEvent event) {
4138        if (mFastScroll != null && mFastScroll.onInterceptHoverEvent(event)) {
4139            return true;
4140        }
4141
4142        return super.onInterceptHoverEvent(event);
4143    }
4144
4145    @Override
4146    public boolean onInterceptTouchEvent(MotionEvent ev) {
4147        int action = ev.getAction();
4148        View v;
4149
4150        if (mPositionScroller != null) {
4151            mPositionScroller.stop();
4152        }
4153
4154        if (mIsDetaching || !isAttachedToWindow()) {
4155            // Something isn't right.
4156            // Since we rely on being attached to get data set change notifications,
4157            // don't risk doing anything where we might try to resync and find things
4158            // in a bogus state.
4159            return false;
4160        }
4161
4162        if (mFastScroll != null && mFastScroll.onInterceptTouchEvent(ev)) {
4163            return true;
4164        }
4165
4166        switch (action & MotionEvent.ACTION_MASK) {
4167        case MotionEvent.ACTION_DOWN: {
4168            int touchMode = mTouchMode;
4169            if (touchMode == TOUCH_MODE_OVERFLING || touchMode == TOUCH_MODE_OVERSCROLL) {
4170                mMotionCorrection = 0;
4171                return true;
4172            }
4173
4174            final int x = (int) ev.getX();
4175            final int y = (int) ev.getY();
4176            mActivePointerId = ev.getPointerId(0);
4177
4178            int motionPosition = findMotionRow(y);
4179            if (touchMode != TOUCH_MODE_FLING && motionPosition >= 0) {
4180                // User clicked on an actual view (and was not stopping a fling).
4181                // Remember where the motion event started
4182                v = getChildAt(motionPosition - mFirstPosition);
4183                mMotionViewOriginalTop = v.getTop();
4184                mMotionX = x;
4185                mMotionY = y;
4186                mMotionPosition = motionPosition;
4187                mTouchMode = TOUCH_MODE_DOWN;
4188                clearScrollingCache();
4189            }
4190            mLastY = Integer.MIN_VALUE;
4191            initOrResetVelocityTracker();
4192            mVelocityTracker.addMovement(ev);
4193            startNestedScroll(SCROLL_AXIS_VERTICAL);
4194            if (touchMode == TOUCH_MODE_FLING) {
4195                return true;
4196            }
4197            break;
4198        }
4199
4200        case MotionEvent.ACTION_MOVE: {
4201            switch (mTouchMode) {
4202            case TOUCH_MODE_DOWN:
4203                int pointerIndex = ev.findPointerIndex(mActivePointerId);
4204                if (pointerIndex == -1) {
4205                    pointerIndex = 0;
4206                    mActivePointerId = ev.getPointerId(pointerIndex);
4207                }
4208                final int y = (int) ev.getY(pointerIndex);
4209                initVelocityTrackerIfNotExists();
4210                mVelocityTracker.addMovement(ev);
4211                if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
4212                    return true;
4213                }
4214                break;
4215            }
4216            break;
4217        }
4218
4219        case MotionEvent.ACTION_CANCEL:
4220        case MotionEvent.ACTION_UP: {
4221            mTouchMode = TOUCH_MODE_REST;
4222            mActivePointerId = INVALID_POINTER;
4223            recycleVelocityTracker();
4224            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
4225            stopNestedScroll();
4226            break;
4227        }
4228
4229        case MotionEvent.ACTION_POINTER_UP: {
4230            onSecondaryPointerUp(ev);
4231            break;
4232        }
4233        }
4234
4235        return false;
4236    }
4237
4238    private void onSecondaryPointerUp(MotionEvent ev) {
4239        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
4240                MotionEvent.ACTION_POINTER_INDEX_SHIFT;
4241        final int pointerId = ev.getPointerId(pointerIndex);
4242        if (pointerId == mActivePointerId) {
4243            // This was our active pointer going up. Choose a new
4244            // active pointer and adjust accordingly.
4245            // TODO: Make this decision more intelligent.
4246            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
4247            mMotionX = (int) ev.getX(newPointerIndex);
4248            mMotionY = (int) ev.getY(newPointerIndex);
4249            mMotionCorrection = 0;
4250            mActivePointerId = ev.getPointerId(newPointerIndex);
4251        }
4252    }
4253
4254    /**
4255     * {@inheritDoc}
4256     */
4257    @Override
4258    public void addTouchables(ArrayList<View> views) {
4259        final int count = getChildCount();
4260        final int firstPosition = mFirstPosition;
4261        final ListAdapter adapter = mAdapter;
4262
4263        if (adapter == null) {
4264            return;
4265        }
4266
4267        for (int i = 0; i < count; i++) {
4268            final View child = getChildAt(i);
4269            if (adapter.isEnabled(firstPosition + i)) {
4270                views.add(child);
4271            }
4272            child.addTouchables(views);
4273        }
4274    }
4275
4276    /**
4277     * Fires an "on scroll state changed" event to the registered
4278     * {@link android.widget.AbsListView.OnScrollListener}, if any. The state change
4279     * is fired only if the specified state is different from the previously known state.
4280     *
4281     * @param newState The new scroll state.
4282     */
4283    void reportScrollStateChange(int newState) {
4284        if (newState != mLastScrollState) {
4285            if (mOnScrollListener != null) {
4286                mLastScrollState = newState;
4287                mOnScrollListener.onScrollStateChanged(this, newState);
4288            }
4289        }
4290    }
4291
4292    /**
4293     * Responsible for fling behavior. Use {@link #start(int)} to
4294     * initiate a fling. Each frame of the fling is handled in {@link #run()}.
4295     * A FlingRunnable will keep re-posting itself until the fling is done.
4296     *
4297     */
4298    private class FlingRunnable implements Runnable {
4299        /**
4300         * Tracks the decay of a fling scroll
4301         */
4302        private final OverScroller mScroller;
4303
4304        /**
4305         * Y value reported by mScroller on the previous fling
4306         */
4307        private int mLastFlingY;
4308
4309        private final Runnable mCheckFlywheel = new Runnable() {
4310            @Override
4311            public void run() {
4312                final int activeId = mActivePointerId;
4313                final VelocityTracker vt = mVelocityTracker;
4314                final OverScroller scroller = mScroller;
4315                if (vt == null || activeId == INVALID_POINTER) {
4316                    return;
4317                }
4318
4319                vt.computeCurrentVelocity(1000, mMaximumVelocity);
4320                final float yvel = -vt.getYVelocity(activeId);
4321
4322                if (Math.abs(yvel) >= mMinimumVelocity
4323                        && scroller.isScrollingInDirection(0, yvel)) {
4324                    // Keep the fling alive a little longer
4325                    postDelayed(this, FLYWHEEL_TIMEOUT);
4326                } else {
4327                    endFling();
4328                    mTouchMode = TOUCH_MODE_SCROLL;
4329                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
4330                }
4331            }
4332        };
4333
4334        private static final int FLYWHEEL_TIMEOUT = 40; // milliseconds
4335
4336        FlingRunnable() {
4337            mScroller = new OverScroller(getContext());
4338        }
4339
4340        void start(int initialVelocity) {
4341            int initialY = initialVelocity < 0 ? Integer.MAX_VALUE : 0;
4342            mLastFlingY = initialY;
4343            mScroller.setInterpolator(null);
4344            mScroller.fling(0, initialY, 0, initialVelocity,
4345                    0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
4346            mTouchMode = TOUCH_MODE_FLING;
4347            postOnAnimation(this);
4348
4349            if (PROFILE_FLINGING) {
4350                if (!mFlingProfilingStarted) {
4351                    Debug.startMethodTracing("AbsListViewFling");
4352                    mFlingProfilingStarted = true;
4353                }
4354            }
4355
4356            if (mFlingStrictSpan == null) {
4357                mFlingStrictSpan = StrictMode.enterCriticalSpan("AbsListView-fling");
4358            }
4359        }
4360
4361        void startSpringback() {
4362            if (mScroller.springBack(0, mScrollY, 0, 0, 0, 0)) {
4363                mTouchMode = TOUCH_MODE_OVERFLING;
4364                invalidate();
4365                postOnAnimation(this);
4366            } else {
4367                mTouchMode = TOUCH_MODE_REST;
4368                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
4369            }
4370        }
4371
4372        void startOverfling(int initialVelocity) {
4373            mScroller.setInterpolator(null);
4374            mScroller.fling(0, mScrollY, 0, initialVelocity, 0, 0,
4375                    Integer.MIN_VALUE, Integer.MAX_VALUE, 0, getHeight());
4376            mTouchMode = TOUCH_MODE_OVERFLING;
4377            invalidate();
4378            postOnAnimation(this);
4379        }
4380
4381        void edgeReached(int delta) {
4382            mScroller.notifyVerticalEdgeReached(mScrollY, 0, mOverflingDistance);
4383            final int overscrollMode = getOverScrollMode();
4384            if (overscrollMode == OVER_SCROLL_ALWAYS ||
4385                    (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && !contentFits())) {
4386                mTouchMode = TOUCH_MODE_OVERFLING;
4387                final int vel = (int) mScroller.getCurrVelocity();
4388                if (delta > 0) {
4389                    mEdgeGlowTop.onAbsorb(vel);
4390                } else {
4391                    mEdgeGlowBottom.onAbsorb(vel);
4392                }
4393            } else {
4394                mTouchMode = TOUCH_MODE_REST;
4395                if (mPositionScroller != null) {
4396                    mPositionScroller.stop();
4397                }
4398            }
4399            invalidate();
4400            postOnAnimation(this);
4401        }
4402
4403        void startScroll(int distance, int duration, boolean linear) {
4404            int initialY = distance < 0 ? Integer.MAX_VALUE : 0;
4405            mLastFlingY = initialY;
4406            mScroller.setInterpolator(linear ? sLinearInterpolator : null);
4407            mScroller.startScroll(0, initialY, 0, distance, duration);
4408            mTouchMode = TOUCH_MODE_FLING;
4409            postOnAnimation(this);
4410        }
4411
4412        void endFling() {
4413            mTouchMode = TOUCH_MODE_REST;
4414
4415            removeCallbacks(this);
4416            removeCallbacks(mCheckFlywheel);
4417
4418            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
4419            clearScrollingCache();
4420            mScroller.abortAnimation();
4421
4422            if (mFlingStrictSpan != null) {
4423                mFlingStrictSpan.finish();
4424                mFlingStrictSpan = null;
4425            }
4426        }
4427
4428        void flywheelTouch() {
4429            postDelayed(mCheckFlywheel, FLYWHEEL_TIMEOUT);
4430        }
4431
4432        @Override
4433        public void run() {
4434            switch (mTouchMode) {
4435            default:
4436                endFling();
4437                return;
4438
4439            case TOUCH_MODE_SCROLL:
4440                if (mScroller.isFinished()) {
4441                    return;
4442                }
4443                // Fall through
4444            case TOUCH_MODE_FLING: {
4445                if (mDataChanged) {
4446                    layoutChildren();
4447                }
4448
4449                if (mItemCount == 0 || getChildCount() == 0) {
4450                    endFling();
4451                    return;
4452                }
4453
4454                final OverScroller scroller = mScroller;
4455                boolean more = scroller.computeScrollOffset();
4456                final int y = scroller.getCurrY();
4457
4458                // Flip sign to convert finger direction to list items direction
4459                // (e.g. finger moving down means list is moving towards the top)
4460                int delta = mLastFlingY - y;
4461
4462                // Pretend that each frame of a fling scroll is a touch scroll
4463                if (delta > 0) {
4464                    // List is moving towards the top. Use first view as mMotionPosition
4465                    mMotionPosition = mFirstPosition;
4466                    final View firstView = getChildAt(0);
4467                    mMotionViewOriginalTop = firstView.getTop();
4468
4469                    // Don't fling more than 1 screen
4470                    delta = Math.min(getHeight() - mPaddingBottom - mPaddingTop - 1, delta);
4471                } else {
4472                    // List is moving towards the bottom. Use last view as mMotionPosition
4473                    int offsetToLast = getChildCount() - 1;
4474                    mMotionPosition = mFirstPosition + offsetToLast;
4475
4476                    final View lastView = getChildAt(offsetToLast);
4477                    mMotionViewOriginalTop = lastView.getTop();
4478
4479                    // Don't fling more than 1 screen
4480                    delta = Math.max(-(getHeight() - mPaddingBottom - mPaddingTop - 1), delta);
4481                }
4482
4483                // Check to see if we have bumped into the scroll limit
4484                View motionView = getChildAt(mMotionPosition - mFirstPosition);
4485                int oldTop = 0;
4486                if (motionView != null) {
4487                    oldTop = motionView.getTop();
4488                }
4489
4490                // Don't stop just because delta is zero (it could have been rounded)
4491                final boolean atEdge = trackMotionScroll(delta, delta);
4492                final boolean atEnd = atEdge && (delta != 0);
4493                if (atEnd) {
4494                    if (motionView != null) {
4495                        // Tweak the scroll for how far we overshot
4496                        int overshoot = -(delta - (motionView.getTop() - oldTop));
4497                        overScrollBy(0, overshoot, 0, mScrollY, 0, 0,
4498                                0, mOverflingDistance, false);
4499                    }
4500                    if (more) {
4501                        edgeReached(delta);
4502                    }
4503                    break;
4504                }
4505
4506                if (more && !atEnd) {
4507                    if (atEdge) invalidate();
4508                    mLastFlingY = y;
4509                    postOnAnimation(this);
4510                } else {
4511                    endFling();
4512
4513                    if (PROFILE_FLINGING) {
4514                        if (mFlingProfilingStarted) {
4515                            Debug.stopMethodTracing();
4516                            mFlingProfilingStarted = false;
4517                        }
4518
4519                        if (mFlingStrictSpan != null) {
4520                            mFlingStrictSpan.finish();
4521                            mFlingStrictSpan = null;
4522                        }
4523                    }
4524                }
4525                break;
4526            }
4527
4528            case TOUCH_MODE_OVERFLING: {
4529                final OverScroller scroller = mScroller;
4530                if (scroller.computeScrollOffset()) {
4531                    final int scrollY = mScrollY;
4532                    final int currY = scroller.getCurrY();
4533                    final int deltaY = currY - scrollY;
4534                    if (overScrollBy(0, deltaY, 0, scrollY, 0, 0,
4535                            0, mOverflingDistance, false)) {
4536                        final boolean crossDown = scrollY <= 0 && currY > 0;
4537                        final boolean crossUp = scrollY >= 0 && currY < 0;
4538                        if (crossDown || crossUp) {
4539                            int velocity = (int) scroller.getCurrVelocity();
4540                            if (crossUp) velocity = -velocity;
4541
4542                            // Don't flywheel from this; we're just continuing things.
4543                            scroller.abortAnimation();
4544                            start(velocity);
4545                        } else {
4546                            startSpringback();
4547                        }
4548                    } else {
4549                        invalidate();
4550                        postOnAnimation(this);
4551                    }
4552                } else {
4553                    endFling();
4554                }
4555                break;
4556            }
4557            }
4558        }
4559    }
4560
4561    /**
4562     * The amount of friction applied to flings. The default value
4563     * is {@link ViewConfiguration#getScrollFriction}.
4564     */
4565    public void setFriction(float friction) {
4566        if (mFlingRunnable == null) {
4567            mFlingRunnable = new FlingRunnable();
4568        }
4569        mFlingRunnable.mScroller.setFriction(friction);
4570    }
4571
4572    /**
4573     * Sets a scale factor for the fling velocity. The initial scale
4574     * factor is 1.0.
4575     *
4576     * @param scale The scale factor to multiply the velocity by.
4577     */
4578    public void setVelocityScale(float scale) {
4579        mVelocityScale = scale;
4580    }
4581
4582    /**
4583     * Override this for better control over position scrolling.
4584     */
4585    AbsPositionScroller createPositionScroller() {
4586        return new PositionScroller();
4587    }
4588
4589    /**
4590     * Smoothly scroll to the specified adapter position. The view will
4591     * scroll such that the indicated position is displayed.
4592     * @param position Scroll to this adapter position.
4593     */
4594    public void smoothScrollToPosition(int position) {
4595        if (mPositionScroller == null) {
4596            mPositionScroller = createPositionScroller();
4597        }
4598        mPositionScroller.start(position);
4599    }
4600
4601    /**
4602     * Smoothly scroll to the specified adapter position. The view will scroll
4603     * such that the indicated position is displayed <code>offset</code> pixels below
4604     * the top edge of the view. If this is impossible, (e.g. the offset would scroll
4605     * the first or last item beyond the boundaries of the list) it will get as close
4606     * as possible. The scroll will take <code>duration</code> milliseconds to complete.
4607     *
4608     * @param position Position to scroll to
4609     * @param offset Desired distance in pixels of <code>position</code> from the top
4610     *               of the view when scrolling is finished
4611     * @param duration Number of milliseconds to use for the scroll
4612     */
4613    public void smoothScrollToPositionFromTop(int position, int offset, int duration) {
4614        if (mPositionScroller == null) {
4615            mPositionScroller = createPositionScroller();
4616        }
4617        mPositionScroller.startWithOffset(position, offset, duration);
4618    }
4619
4620    /**
4621     * Smoothly scroll to the specified adapter position. The view will scroll
4622     * such that the indicated position is displayed <code>offset</code> pixels below
4623     * the top edge of the view. If this is impossible, (e.g. the offset would scroll
4624     * the first or last item beyond the boundaries of the list) it will get as close
4625     * as possible.
4626     *
4627     * @param position Position to scroll to
4628     * @param offset Desired distance in pixels of <code>position</code> from the top
4629     *               of the view when scrolling is finished
4630     */
4631    public void smoothScrollToPositionFromTop(int position, int offset) {
4632        if (mPositionScroller == null) {
4633            mPositionScroller = createPositionScroller();
4634        }
4635        mPositionScroller.startWithOffset(position, offset, offset);
4636    }
4637
4638    /**
4639     * Smoothly scroll to the specified adapter position. The view will
4640     * scroll such that the indicated position is displayed, but it will
4641     * stop early if scrolling further would scroll boundPosition out of
4642     * view.
4643     *
4644     * @param position Scroll to this adapter position.
4645     * @param boundPosition Do not scroll if it would move this adapter
4646     *          position out of view.
4647     */
4648    public void smoothScrollToPosition(int position, int boundPosition) {
4649        if (mPositionScroller == null) {
4650            mPositionScroller = createPositionScroller();
4651        }
4652        mPositionScroller.start(position, boundPosition);
4653    }
4654
4655    /**
4656     * Smoothly scroll by distance pixels over duration milliseconds.
4657     * @param distance Distance to scroll in pixels.
4658     * @param duration Duration of the scroll animation in milliseconds.
4659     */
4660    public void smoothScrollBy(int distance, int duration) {
4661        smoothScrollBy(distance, duration, false);
4662    }
4663
4664    void smoothScrollBy(int distance, int duration, boolean linear) {
4665        if (mFlingRunnable == null) {
4666            mFlingRunnable = new FlingRunnable();
4667        }
4668
4669        // No sense starting to scroll if we're not going anywhere
4670        final int firstPos = mFirstPosition;
4671        final int childCount = getChildCount();
4672        final int lastPos = firstPos + childCount;
4673        final int topLimit = getPaddingTop();
4674        final int bottomLimit = getHeight() - getPaddingBottom();
4675
4676        if (distance == 0 || mItemCount == 0 || childCount == 0 ||
4677                (firstPos == 0 && getChildAt(0).getTop() == topLimit && distance < 0) ||
4678                (lastPos == mItemCount &&
4679                        getChildAt(childCount - 1).getBottom() == bottomLimit && distance > 0)) {
4680            mFlingRunnable.endFling();
4681            if (mPositionScroller != null) {
4682                mPositionScroller.stop();
4683            }
4684        } else {
4685            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
4686            mFlingRunnable.startScroll(distance, duration, linear);
4687        }
4688    }
4689
4690    /**
4691     * Allows RemoteViews to scroll relatively to a position.
4692     */
4693    void smoothScrollByOffset(int position) {
4694        int index = -1;
4695        if (position < 0) {
4696            index = getFirstVisiblePosition();
4697        } else if (position > 0) {
4698            index = getLastVisiblePosition();
4699        }
4700
4701        if (index > -1) {
4702            View child = getChildAt(index - getFirstVisiblePosition());
4703            if (child != null) {
4704                Rect visibleRect = new Rect();
4705                if (child.getGlobalVisibleRect(visibleRect)) {
4706                    // the child is partially visible
4707                    int childRectArea = child.getWidth() * child.getHeight();
4708                    int visibleRectArea = visibleRect.width() * visibleRect.height();
4709                    float visibleArea = (visibleRectArea / (float) childRectArea);
4710                    final float visibleThreshold = 0.75f;
4711                    if ((position < 0) && (visibleArea < visibleThreshold)) {
4712                        // the top index is not perceivably visible so offset
4713                        // to account for showing that top index as well
4714                        ++index;
4715                    } else if ((position > 0) && (visibleArea < visibleThreshold)) {
4716                        // the bottom index is not perceivably visible so offset
4717                        // to account for showing that bottom index as well
4718                        --index;
4719                    }
4720                }
4721                smoothScrollToPosition(Math.max(0, Math.min(getCount(), index + position)));
4722            }
4723        }
4724    }
4725
4726    private void createScrollingCache() {
4727        if (mScrollingCacheEnabled && !mCachingStarted && !isHardwareAccelerated()) {
4728            setChildrenDrawnWithCacheEnabled(true);
4729            setChildrenDrawingCacheEnabled(true);
4730            mCachingStarted = mCachingActive = true;
4731        }
4732    }
4733
4734    private void clearScrollingCache() {
4735        if (!isHardwareAccelerated()) {
4736            if (mClearScrollingCache == null) {
4737                mClearScrollingCache = new Runnable() {
4738                    @Override
4739                    public void run() {
4740                        if (mCachingStarted) {
4741                            mCachingStarted = mCachingActive = false;
4742                            setChildrenDrawnWithCacheEnabled(false);
4743                            if ((mPersistentDrawingCache & PERSISTENT_SCROLLING_CACHE) == 0) {
4744                                setChildrenDrawingCacheEnabled(false);
4745                            }
4746                            if (!isAlwaysDrawnWithCacheEnabled()) {
4747                                invalidate();
4748                            }
4749                        }
4750                    }
4751                };
4752            }
4753            post(mClearScrollingCache);
4754        }
4755    }
4756
4757    /**
4758     * Scrolls the list items within the view by a specified number of pixels.
4759     *
4760     * @param y the amount of pixels to scroll by vertically
4761     * @see #canScrollList(int)
4762     */
4763    public void scrollListBy(int y) {
4764        trackMotionScroll(-y, -y);
4765    }
4766
4767    /**
4768     * Check if the items in the list can be scrolled in a certain direction.
4769     *
4770     * @param direction Negative to check scrolling up, positive to check
4771     *            scrolling down.
4772     * @return true if the list can be scrolled in the specified direction,
4773     *         false otherwise.
4774     * @see #scrollListBy(int)
4775     */
4776    public boolean canScrollList(int direction) {
4777        final int childCount = getChildCount();
4778        if (childCount == 0) {
4779            return false;
4780        }
4781
4782        final int firstPosition = mFirstPosition;
4783        final Rect listPadding = mListPadding;
4784        if (direction > 0) {
4785            final int lastBottom = getChildAt(childCount - 1).getBottom();
4786            final int lastPosition = firstPosition + childCount;
4787            return lastPosition < mItemCount || lastBottom > getHeight() - listPadding.bottom;
4788        } else {
4789            final int firstTop = getChildAt(0).getTop();
4790            return firstPosition > 0 || firstTop < listPadding.top;
4791        }
4792    }
4793
4794    /**
4795     * Track a motion scroll
4796     *
4797     * @param deltaY Amount to offset mMotionView. This is the accumulated delta since the motion
4798     *        began. Positive numbers mean the user's finger is moving down the screen.
4799     * @param incrementalDeltaY Change in deltaY from the previous event.
4800     * @return true if we're already at the beginning/end of the list and have nothing to do.
4801     */
4802    boolean trackMotionScroll(int deltaY, int incrementalDeltaY) {
4803        final int childCount = getChildCount();
4804        if (childCount == 0) {
4805            return true;
4806        }
4807
4808        final int firstTop = getChildAt(0).getTop();
4809        final int lastBottom = getChildAt(childCount - 1).getBottom();
4810
4811        final Rect listPadding = mListPadding;
4812
4813        // "effective padding" In this case is the amount of padding that affects
4814        // how much space should not be filled by items. If we don't clip to padding
4815        // there is no effective padding.
4816        int effectivePaddingTop = 0;
4817        int effectivePaddingBottom = 0;
4818        if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
4819            effectivePaddingTop = listPadding.top;
4820            effectivePaddingBottom = listPadding.bottom;
4821        }
4822
4823         // FIXME account for grid vertical spacing too?
4824        final int spaceAbove = effectivePaddingTop - firstTop;
4825        final int end = getHeight() - effectivePaddingBottom;
4826        final int spaceBelow = lastBottom - end;
4827
4828        final int height = getHeight() - mPaddingBottom - mPaddingTop;
4829        if (deltaY < 0) {
4830            deltaY = Math.max(-(height - 1), deltaY);
4831        } else {
4832            deltaY = Math.min(height - 1, deltaY);
4833        }
4834
4835        if (incrementalDeltaY < 0) {
4836            incrementalDeltaY = Math.max(-(height - 1), incrementalDeltaY);
4837        } else {
4838            incrementalDeltaY = Math.min(height - 1, incrementalDeltaY);
4839        }
4840
4841        final int firstPosition = mFirstPosition;
4842
4843        // Update our guesses for where the first and last views are
4844        if (firstPosition == 0) {
4845            mFirstPositionDistanceGuess = firstTop - listPadding.top;
4846        } else {
4847            mFirstPositionDistanceGuess += incrementalDeltaY;
4848        }
4849        if (firstPosition + childCount == mItemCount) {
4850            mLastPositionDistanceGuess = lastBottom + listPadding.bottom;
4851        } else {
4852            mLastPositionDistanceGuess += incrementalDeltaY;
4853        }
4854
4855        final boolean cannotScrollDown = (firstPosition == 0 &&
4856                firstTop >= listPadding.top && incrementalDeltaY >= 0);
4857        final boolean cannotScrollUp = (firstPosition + childCount == mItemCount &&
4858                lastBottom <= getHeight() - listPadding.bottom && incrementalDeltaY <= 0);
4859
4860        if (cannotScrollDown || cannotScrollUp) {
4861            return incrementalDeltaY != 0;
4862        }
4863
4864        final boolean down = incrementalDeltaY < 0;
4865
4866        final boolean inTouchMode = isInTouchMode();
4867        if (inTouchMode) {
4868            hideSelector();
4869        }
4870
4871        final int headerViewsCount = getHeaderViewsCount();
4872        final int footerViewsStart = mItemCount - getFooterViewsCount();
4873
4874        int start = 0;
4875        int count = 0;
4876
4877        if (down) {
4878            int top = -incrementalDeltaY;
4879            if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
4880                top += listPadding.top;
4881            }
4882            for (int i = 0; i < childCount; i++) {
4883                final View child = getChildAt(i);
4884                if (child.getBottom() >= top) {
4885                    break;
4886                } else {
4887                    count++;
4888                    int position = firstPosition + i;
4889                    if (position >= headerViewsCount && position < footerViewsStart) {
4890                        // The view will be rebound to new data, clear any
4891                        // system-managed transient state.
4892                        if (child.isAccessibilityFocused()) {
4893                            child.clearAccessibilityFocus();
4894                        }
4895                        mRecycler.addScrapView(child, position);
4896                    }
4897                }
4898            }
4899        } else {
4900            int bottom = getHeight() - incrementalDeltaY;
4901            if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
4902                bottom -= listPadding.bottom;
4903            }
4904            for (int i = childCount - 1; i >= 0; i--) {
4905                final View child = getChildAt(i);
4906                if (child.getTop() <= bottom) {
4907                    break;
4908                } else {
4909                    start = i;
4910                    count++;
4911                    int position = firstPosition + i;
4912                    if (position >= headerViewsCount && position < footerViewsStart) {
4913                        // The view will be rebound to new data, clear any
4914                        // system-managed transient state.
4915                        if (child.isAccessibilityFocused()) {
4916                            child.clearAccessibilityFocus();
4917                        }
4918                        mRecycler.addScrapView(child, position);
4919                    }
4920                }
4921            }
4922        }
4923
4924        mMotionViewNewTop = mMotionViewOriginalTop + deltaY;
4925
4926        mBlockLayoutRequests = true;
4927
4928        if (count > 0) {
4929            detachViewsFromParent(start, count);
4930            mRecycler.removeSkippedScrap();
4931        }
4932
4933        // invalidate before moving the children to avoid unnecessary invalidate
4934        // calls to bubble up from the children all the way to the top
4935        if (!awakenScrollBars()) {
4936           invalidate();
4937        }
4938
4939        offsetChildrenTopAndBottom(incrementalDeltaY);
4940
4941        if (down) {
4942            mFirstPosition += count;
4943        }
4944
4945        final int absIncrementalDeltaY = Math.abs(incrementalDeltaY);
4946        if (spaceAbove < absIncrementalDeltaY || spaceBelow < absIncrementalDeltaY) {
4947            fillGap(down);
4948        }
4949
4950        if (!inTouchMode && mSelectedPosition != INVALID_POSITION) {
4951            final int childIndex = mSelectedPosition - mFirstPosition;
4952            if (childIndex >= 0 && childIndex < getChildCount()) {
4953                positionSelector(mSelectedPosition, getChildAt(childIndex));
4954            }
4955        } else if (mSelectorPosition != INVALID_POSITION) {
4956            final int childIndex = mSelectorPosition - mFirstPosition;
4957            if (childIndex >= 0 && childIndex < getChildCount()) {
4958                positionSelector(INVALID_POSITION, getChildAt(childIndex));
4959            }
4960        } else {
4961            mSelectorRect.setEmpty();
4962        }
4963
4964        mBlockLayoutRequests = false;
4965
4966        invokeOnItemScrollListener();
4967
4968        return false;
4969    }
4970
4971    /**
4972     * Returns the number of header views in the list. Header views are special views
4973     * at the top of the list that should not be recycled during a layout.
4974     *
4975     * @return The number of header views, 0 in the default implementation.
4976     */
4977    int getHeaderViewsCount() {
4978        return 0;
4979    }
4980
4981    /**
4982     * Returns the number of footer views in the list. Footer views are special views
4983     * at the bottom of the list that should not be recycled during a layout.
4984     *
4985     * @return The number of footer views, 0 in the default implementation.
4986     */
4987    int getFooterViewsCount() {
4988        return 0;
4989    }
4990
4991    /**
4992     * Fills the gap left open by a touch-scroll. During a touch scroll, children that
4993     * remain on screen are shifted and the other ones are discarded. The role of this
4994     * method is to fill the gap thus created by performing a partial layout in the
4995     * empty space.
4996     *
4997     * @param down true if the scroll is going down, false if it is going up
4998     */
4999    abstract void fillGap(boolean down);
5000
5001    void hideSelector() {
5002        if (mSelectedPosition != INVALID_POSITION) {
5003            if (mLayoutMode != LAYOUT_SPECIFIC) {
5004                mResurrectToPosition = mSelectedPosition;
5005            }
5006            if (mNextSelectedPosition >= 0 && mNextSelectedPosition != mSelectedPosition) {
5007                mResurrectToPosition = mNextSelectedPosition;
5008            }
5009            setSelectedPositionInt(INVALID_POSITION);
5010            setNextSelectedPositionInt(INVALID_POSITION);
5011            mSelectedTop = 0;
5012        }
5013    }
5014
5015    /**
5016     * @return A position to select. First we try mSelectedPosition. If that has been clobbered by
5017     * entering touch mode, we then try mResurrectToPosition. Values are pinned to the range
5018     * of items available in the adapter
5019     */
5020    int reconcileSelectedPosition() {
5021        int position = mSelectedPosition;
5022        if (position < 0) {
5023            position = mResurrectToPosition;
5024        }
5025        position = Math.max(0, position);
5026        position = Math.min(position, mItemCount - 1);
5027        return position;
5028    }
5029
5030    /**
5031     * Find the row closest to y. This row will be used as the motion row when scrolling
5032     *
5033     * @param y Where the user touched
5034     * @return The position of the first (or only) item in the row containing y
5035     */
5036    abstract int findMotionRow(int y);
5037
5038    /**
5039     * Find the row closest to y. This row will be used as the motion row when scrolling.
5040     *
5041     * @param y Where the user touched
5042     * @return The position of the first (or only) item in the row closest to y
5043     */
5044    int findClosestMotionRow(int y) {
5045        final int childCount = getChildCount();
5046        if (childCount == 0) {
5047            return INVALID_POSITION;
5048        }
5049
5050        final int motionRow = findMotionRow(y);
5051        return motionRow != INVALID_POSITION ? motionRow : mFirstPosition + childCount - 1;
5052    }
5053
5054    /**
5055     * Causes all the views to be rebuilt and redrawn.
5056     */
5057    public void invalidateViews() {
5058        mDataChanged = true;
5059        rememberSyncState();
5060        requestLayout();
5061        invalidate();
5062    }
5063
5064    /**
5065     * If there is a selection returns false.
5066     * Otherwise resurrects the selection and returns true if resurrected.
5067     */
5068    boolean resurrectSelectionIfNeeded() {
5069        if (mSelectedPosition < 0 && resurrectSelection()) {
5070            updateSelectorState();
5071            return true;
5072        }
5073        return false;
5074    }
5075
5076    /**
5077     * Makes the item at the supplied position selected.
5078     *
5079     * @param position the position of the new selection
5080     */
5081    abstract void setSelectionInt(int position);
5082
5083    /**
5084     * Attempt to bring the selection back if the user is switching from touch
5085     * to trackball mode
5086     * @return Whether selection was set to something.
5087     */
5088    boolean resurrectSelection() {
5089        final int childCount = getChildCount();
5090
5091        if (childCount <= 0) {
5092            return false;
5093        }
5094
5095        int selectedTop = 0;
5096        int selectedPos;
5097        int childrenTop = mListPadding.top;
5098        int childrenBottom = mBottom - mTop - mListPadding.bottom;
5099        final int firstPosition = mFirstPosition;
5100        final int toPosition = mResurrectToPosition;
5101        boolean down = true;
5102
5103        if (toPosition >= firstPosition && toPosition < firstPosition + childCount) {
5104            selectedPos = toPosition;
5105
5106            final View selected = getChildAt(selectedPos - mFirstPosition);
5107            selectedTop = selected.getTop();
5108            int selectedBottom = selected.getBottom();
5109
5110            // We are scrolled, don't get in the fade
5111            if (selectedTop < childrenTop) {
5112                selectedTop = childrenTop + getVerticalFadingEdgeLength();
5113            } else if (selectedBottom > childrenBottom) {
5114                selectedTop = childrenBottom - selected.getMeasuredHeight()
5115                        - getVerticalFadingEdgeLength();
5116            }
5117        } else {
5118            if (toPosition < firstPosition) {
5119                // Default to selecting whatever is first
5120                selectedPos = firstPosition;
5121                for (int i = 0; i < childCount; i++) {
5122                    final View v = getChildAt(i);
5123                    final int top = v.getTop();
5124
5125                    if (i == 0) {
5126                        // Remember the position of the first item
5127                        selectedTop = top;
5128                        // See if we are scrolled at all
5129                        if (firstPosition > 0 || top < childrenTop) {
5130                            // If we are scrolled, don't select anything that is
5131                            // in the fade region
5132                            childrenTop += getVerticalFadingEdgeLength();
5133                        }
5134                    }
5135                    if (top >= childrenTop) {
5136                        // Found a view whose top is fully visisble
5137                        selectedPos = firstPosition + i;
5138                        selectedTop = top;
5139                        break;
5140                    }
5141                }
5142            } else {
5143                final int itemCount = mItemCount;
5144                down = false;
5145                selectedPos = firstPosition + childCount - 1;
5146
5147                for (int i = childCount - 1; i >= 0; i--) {
5148                    final View v = getChildAt(i);
5149                    final int top = v.getTop();
5150                    final int bottom = v.getBottom();
5151
5152                    if (i == childCount - 1) {
5153                        selectedTop = top;
5154                        if (firstPosition + childCount < itemCount || bottom > childrenBottom) {
5155                            childrenBottom -= getVerticalFadingEdgeLength();
5156                        }
5157                    }
5158
5159                    if (bottom <= childrenBottom) {
5160                        selectedPos = firstPosition + i;
5161                        selectedTop = top;
5162                        break;
5163                    }
5164                }
5165            }
5166        }
5167
5168        mResurrectToPosition = INVALID_POSITION;
5169        removeCallbacks(mFlingRunnable);
5170        if (mPositionScroller != null) {
5171            mPositionScroller.stop();
5172        }
5173        mTouchMode = TOUCH_MODE_REST;
5174        clearScrollingCache();
5175        mSpecificTop = selectedTop;
5176        selectedPos = lookForSelectablePosition(selectedPos, down);
5177        if (selectedPos >= firstPosition && selectedPos <= getLastVisiblePosition()) {
5178            mLayoutMode = LAYOUT_SPECIFIC;
5179            updateSelectorState();
5180            setSelectionInt(selectedPos);
5181            invokeOnItemScrollListener();
5182        } else {
5183            selectedPos = INVALID_POSITION;
5184        }
5185        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
5186
5187        return selectedPos >= 0;
5188    }
5189
5190    void confirmCheckedPositionsById() {
5191        // Clear out the positional check states, we'll rebuild it below from IDs.
5192        mCheckStates.clear();
5193
5194        boolean checkedCountChanged = false;
5195        for (int checkedIndex = 0; checkedIndex < mCheckedIdStates.size(); checkedIndex++) {
5196            final long id = mCheckedIdStates.keyAt(checkedIndex);
5197            final int lastPos = mCheckedIdStates.valueAt(checkedIndex);
5198
5199            final long lastPosId = mAdapter.getItemId(lastPos);
5200            if (id != lastPosId) {
5201                // Look around to see if the ID is nearby. If not, uncheck it.
5202                final int start = Math.max(0, lastPos - CHECK_POSITION_SEARCH_DISTANCE);
5203                final int end = Math.min(lastPos + CHECK_POSITION_SEARCH_DISTANCE, mItemCount);
5204                boolean found = false;
5205                for (int searchPos = start; searchPos < end; searchPos++) {
5206                    final long searchId = mAdapter.getItemId(searchPos);
5207                    if (id == searchId) {
5208                        found = true;
5209                        mCheckStates.put(searchPos, true);
5210                        mCheckedIdStates.setValueAt(checkedIndex, searchPos);
5211                        break;
5212                    }
5213                }
5214
5215                if (!found) {
5216                    mCheckedIdStates.delete(id);
5217                    checkedIndex--;
5218                    mCheckedItemCount--;
5219                    checkedCountChanged = true;
5220                    if (mChoiceActionMode != null && mMultiChoiceModeCallback != null) {
5221                        mMultiChoiceModeCallback.onItemCheckedStateChanged(mChoiceActionMode,
5222                                lastPos, id, false);
5223                    }
5224                }
5225            } else {
5226                mCheckStates.put(lastPos, true);
5227            }
5228        }
5229
5230        if (checkedCountChanged && mChoiceActionMode != null) {
5231            mChoiceActionMode.invalidate();
5232        }
5233    }
5234
5235    @Override
5236    protected void handleDataChanged() {
5237        int count = mItemCount;
5238        int lastHandledItemCount = mLastHandledItemCount;
5239        mLastHandledItemCount = mItemCount;
5240
5241        if (mChoiceMode != CHOICE_MODE_NONE && mAdapter != null && mAdapter.hasStableIds()) {
5242            confirmCheckedPositionsById();
5243        }
5244
5245        // TODO: In the future we can recycle these views based on stable ID instead.
5246        mRecycler.clearTransientStateViews();
5247
5248        if (count > 0) {
5249            int newPos;
5250            int selectablePos;
5251
5252            // Find the row we are supposed to sync to
5253            if (mNeedSync) {
5254                // Update this first, since setNextSelectedPositionInt inspects it
5255                mNeedSync = false;
5256                mPendingSync = null;
5257
5258                if (mTranscriptMode == TRANSCRIPT_MODE_ALWAYS_SCROLL) {
5259                    mLayoutMode = LAYOUT_FORCE_BOTTOM;
5260                    return;
5261                } else if (mTranscriptMode == TRANSCRIPT_MODE_NORMAL) {
5262                    if (mForceTranscriptScroll) {
5263                        mForceTranscriptScroll = false;
5264                        mLayoutMode = LAYOUT_FORCE_BOTTOM;
5265                        return;
5266                    }
5267                    final int childCount = getChildCount();
5268                    final int listBottom = getHeight() - getPaddingBottom();
5269                    final View lastChild = getChildAt(childCount - 1);
5270                    final int lastBottom = lastChild != null ? lastChild.getBottom() : listBottom;
5271                    if (mFirstPosition + childCount >= lastHandledItemCount &&
5272                            lastBottom <= listBottom) {
5273                        mLayoutMode = LAYOUT_FORCE_BOTTOM;
5274                        return;
5275                    }
5276                    // Something new came in and we didn't scroll; give the user a clue that
5277                    // there's something new.
5278                    awakenScrollBars();
5279                }
5280
5281                switch (mSyncMode) {
5282                case SYNC_SELECTED_POSITION:
5283                    if (isInTouchMode()) {
5284                        // We saved our state when not in touch mode. (We know this because
5285                        // mSyncMode is SYNC_SELECTED_POSITION.) Now we are trying to
5286                        // restore in touch mode. Just leave mSyncPosition as it is (possibly
5287                        // adjusting if the available range changed) and return.
5288                        mLayoutMode = LAYOUT_SYNC;
5289                        mSyncPosition = Math.min(Math.max(0, mSyncPosition), count - 1);
5290
5291                        return;
5292                    } else {
5293                        // See if we can find a position in the new data with the same
5294                        // id as the old selection. This will change mSyncPosition.
5295                        newPos = findSyncPosition();
5296                        if (newPos >= 0) {
5297                            // Found it. Now verify that new selection is still selectable
5298                            selectablePos = lookForSelectablePosition(newPos, true);
5299                            if (selectablePos == newPos) {
5300                                // Same row id is selected
5301                                mSyncPosition = newPos;
5302
5303                                if (mSyncHeight == getHeight()) {
5304                                    // If we are at the same height as when we saved state, try
5305                                    // to restore the scroll position too.
5306                                    mLayoutMode = LAYOUT_SYNC;
5307                                } else {
5308                                    // We are not the same height as when the selection was saved, so
5309                                    // don't try to restore the exact position
5310                                    mLayoutMode = LAYOUT_SET_SELECTION;
5311                                }
5312
5313                                // Restore selection
5314                                setNextSelectedPositionInt(newPos);
5315                                return;
5316                            }
5317                        }
5318                    }
5319                    break;
5320                case SYNC_FIRST_POSITION:
5321                    // Leave mSyncPosition as it is -- just pin to available range
5322                    mLayoutMode = LAYOUT_SYNC;
5323                    mSyncPosition = Math.min(Math.max(0, mSyncPosition), count - 1);
5324
5325                    return;
5326                }
5327            }
5328
5329            if (!isInTouchMode()) {
5330                // We couldn't find matching data -- try to use the same position
5331                newPos = getSelectedItemPosition();
5332
5333                // Pin position to the available range
5334                if (newPos >= count) {
5335                    newPos = count - 1;
5336                }
5337                if (newPos < 0) {
5338                    newPos = 0;
5339                }
5340
5341                // Make sure we select something selectable -- first look down
5342                selectablePos = lookForSelectablePosition(newPos, true);
5343
5344                if (selectablePos >= 0) {
5345                    setNextSelectedPositionInt(selectablePos);
5346                    return;
5347                } else {
5348                    // Looking down didn't work -- try looking up
5349                    selectablePos = lookForSelectablePosition(newPos, false);
5350                    if (selectablePos >= 0) {
5351                        setNextSelectedPositionInt(selectablePos);
5352                        return;
5353                    }
5354                }
5355            } else {
5356
5357                // We already know where we want to resurrect the selection
5358                if (mResurrectToPosition >= 0) {
5359                    return;
5360                }
5361            }
5362
5363        }
5364
5365        // Nothing is selected. Give up and reset everything.
5366        mLayoutMode = mStackFromBottom ? LAYOUT_FORCE_BOTTOM : LAYOUT_FORCE_TOP;
5367        mSelectedPosition = INVALID_POSITION;
5368        mSelectedRowId = INVALID_ROW_ID;
5369        mNextSelectedPosition = INVALID_POSITION;
5370        mNextSelectedRowId = INVALID_ROW_ID;
5371        mNeedSync = false;
5372        mPendingSync = null;
5373        mSelectorPosition = INVALID_POSITION;
5374        checkSelectionChanged();
5375    }
5376
5377    @Override
5378    protected void onDisplayHint(int hint) {
5379        super.onDisplayHint(hint);
5380        switch (hint) {
5381            case INVISIBLE:
5382                if (mPopup != null && mPopup.isShowing()) {
5383                    dismissPopup();
5384                }
5385                break;
5386            case VISIBLE:
5387                if (mFiltered && mPopup != null && !mPopup.isShowing()) {
5388                    showPopup();
5389                }
5390                break;
5391        }
5392        mPopupHidden = hint == INVISIBLE;
5393    }
5394
5395    /**
5396     * Removes the filter window
5397     */
5398    private void dismissPopup() {
5399        if (mPopup != null) {
5400            mPopup.dismiss();
5401        }
5402    }
5403
5404    /**
5405     * Shows the filter window
5406     */
5407    private void showPopup() {
5408        // Make sure we have a window before showing the popup
5409        if (getWindowVisibility() == View.VISIBLE) {
5410            createTextFilter(true);
5411            positionPopup();
5412            // Make sure we get focus if we are showing the popup
5413            checkFocus();
5414        }
5415    }
5416
5417    private void positionPopup() {
5418        int screenHeight = getResources().getDisplayMetrics().heightPixels;
5419        final int[] xy = new int[2];
5420        getLocationOnScreen(xy);
5421        // TODO: The 20 below should come from the theme
5422        // TODO: And the gravity should be defined in the theme as well
5423        final int bottomGap = screenHeight - xy[1] - getHeight() + (int) (mDensityScale * 20);
5424        if (!mPopup.isShowing()) {
5425            mPopup.showAtLocation(this, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
5426                    xy[0], bottomGap);
5427        } else {
5428            mPopup.update(xy[0], bottomGap, -1, -1);
5429        }
5430    }
5431
5432    /**
5433     * What is the distance between the source and destination rectangles given the direction of
5434     * focus navigation between them? The direction basically helps figure out more quickly what is
5435     * self evident by the relationship between the rects...
5436     *
5437     * @param source the source rectangle
5438     * @param dest the destination rectangle
5439     * @param direction the direction
5440     * @return the distance between the rectangles
5441     */
5442    static int getDistance(Rect source, Rect dest, int direction) {
5443        int sX, sY; // source x, y
5444        int dX, dY; // dest x, y
5445        switch (direction) {
5446        case View.FOCUS_RIGHT:
5447            sX = source.right;
5448            sY = source.top + source.height() / 2;
5449            dX = dest.left;
5450            dY = dest.top + dest.height() / 2;
5451            break;
5452        case View.FOCUS_DOWN:
5453            sX = source.left + source.width() / 2;
5454            sY = source.bottom;
5455            dX = dest.left + dest.width() / 2;
5456            dY = dest.top;
5457            break;
5458        case View.FOCUS_LEFT:
5459            sX = source.left;
5460            sY = source.top + source.height() / 2;
5461            dX = dest.right;
5462            dY = dest.top + dest.height() / 2;
5463            break;
5464        case View.FOCUS_UP:
5465            sX = source.left + source.width() / 2;
5466            sY = source.top;
5467            dX = dest.left + dest.width() / 2;
5468            dY = dest.bottom;
5469            break;
5470        case View.FOCUS_FORWARD:
5471        case View.FOCUS_BACKWARD:
5472            sX = source.right + source.width() / 2;
5473            sY = source.top + source.height() / 2;
5474            dX = dest.left + dest.width() / 2;
5475            dY = dest.top + dest.height() / 2;
5476            break;
5477        default:
5478            throw new IllegalArgumentException("direction must be one of "
5479                    + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, "
5480                    + "FOCUS_FORWARD, FOCUS_BACKWARD}.");
5481        }
5482        int deltaX = dX - sX;
5483        int deltaY = dY - sY;
5484        return deltaY * deltaY + deltaX * deltaX;
5485    }
5486
5487    @Override
5488    protected boolean isInFilterMode() {
5489        return mFiltered;
5490    }
5491
5492    /**
5493     * Sends a key to the text filter window
5494     *
5495     * @param keyCode The keycode for the event
5496     * @param event The actual key event
5497     *
5498     * @return True if the text filter handled the event, false otherwise.
5499     */
5500    boolean sendToTextFilter(int keyCode, int count, KeyEvent event) {
5501        if (!acceptFilter()) {
5502            return false;
5503        }
5504
5505        boolean handled = false;
5506        boolean okToSend = true;
5507        switch (keyCode) {
5508        case KeyEvent.KEYCODE_DPAD_UP:
5509        case KeyEvent.KEYCODE_DPAD_DOWN:
5510        case KeyEvent.KEYCODE_DPAD_LEFT:
5511        case KeyEvent.KEYCODE_DPAD_RIGHT:
5512        case KeyEvent.KEYCODE_DPAD_CENTER:
5513        case KeyEvent.KEYCODE_ENTER:
5514            okToSend = false;
5515            break;
5516        case KeyEvent.KEYCODE_BACK:
5517            if (mFiltered && mPopup != null && mPopup.isShowing()) {
5518                if (event.getAction() == KeyEvent.ACTION_DOWN
5519                        && event.getRepeatCount() == 0) {
5520                    KeyEvent.DispatcherState state = getKeyDispatcherState();
5521                    if (state != null) {
5522                        state.startTracking(event, this);
5523                    }
5524                    handled = true;
5525                } else if (event.getAction() == KeyEvent.ACTION_UP
5526                        && event.isTracking() && !event.isCanceled()) {
5527                    handled = true;
5528                    mTextFilter.setText("");
5529                }
5530            }
5531            okToSend = false;
5532            break;
5533        case KeyEvent.KEYCODE_SPACE:
5534            // Only send spaces once we are filtered
5535            okToSend = mFiltered;
5536            break;
5537        }
5538
5539        if (okToSend) {
5540            createTextFilter(true);
5541
5542            KeyEvent forwardEvent = event;
5543            if (forwardEvent.getRepeatCount() > 0) {
5544                forwardEvent = KeyEvent.changeTimeRepeat(event, event.getEventTime(), 0);
5545            }
5546
5547            int action = event.getAction();
5548            switch (action) {
5549                case KeyEvent.ACTION_DOWN:
5550                    handled = mTextFilter.onKeyDown(keyCode, forwardEvent);
5551                    break;
5552
5553                case KeyEvent.ACTION_UP:
5554                    handled = mTextFilter.onKeyUp(keyCode, forwardEvent);
5555                    break;
5556
5557                case KeyEvent.ACTION_MULTIPLE:
5558                    handled = mTextFilter.onKeyMultiple(keyCode, count, event);
5559                    break;
5560            }
5561        }
5562        return handled;
5563    }
5564
5565    /**
5566     * Return an InputConnection for editing of the filter text.
5567     */
5568    @Override
5569    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5570        if (isTextFilterEnabled()) {
5571            if (mPublicInputConnection == null) {
5572                mDefInputConnection = new BaseInputConnection(this, false);
5573                mPublicInputConnection = new InputConnectionWrapper(outAttrs);
5574            }
5575            outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_FILTER;
5576            outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
5577            return mPublicInputConnection;
5578        }
5579        return null;
5580    }
5581
5582    private class InputConnectionWrapper implements InputConnection {
5583        private final EditorInfo mOutAttrs;
5584        private InputConnection mTarget;
5585
5586        public InputConnectionWrapper(EditorInfo outAttrs) {
5587            mOutAttrs = outAttrs;
5588        }
5589
5590        private InputConnection getTarget() {
5591            if (mTarget == null) {
5592                mTarget = getTextFilterInput().onCreateInputConnection(mOutAttrs);
5593            }
5594            return mTarget;
5595        }
5596
5597        @Override
5598        public boolean reportFullscreenMode(boolean enabled) {
5599            // Use our own input connection, since it is
5600            // the "real" one the IME is talking with.
5601            return mDefInputConnection.reportFullscreenMode(enabled);
5602        }
5603
5604        @Override
5605        public boolean performEditorAction(int editorAction) {
5606            // The editor is off in its own window; we need to be
5607            // the one that does this.
5608            if (editorAction == EditorInfo.IME_ACTION_DONE) {
5609                InputMethodManager imm = (InputMethodManager)
5610                        getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
5611                if (imm != null) {
5612                    imm.hideSoftInputFromWindow(getWindowToken(), 0);
5613                }
5614                return true;
5615            }
5616            return false;
5617        }
5618
5619        @Override
5620        public boolean sendKeyEvent(KeyEvent event) {
5621            // Use our own input connection, since the filter
5622            // text view may not be shown in a window so has
5623            // no ViewAncestor to dispatch events with.
5624            return mDefInputConnection.sendKeyEvent(event);
5625        }
5626
5627        @Override
5628        public CharSequence getTextBeforeCursor(int n, int flags) {
5629            if (mTarget == null) return "";
5630            return mTarget.getTextBeforeCursor(n, flags);
5631        }
5632
5633        @Override
5634        public CharSequence getTextAfterCursor(int n, int flags) {
5635            if (mTarget == null) return "";
5636            return mTarget.getTextAfterCursor(n, flags);
5637        }
5638
5639        @Override
5640        public CharSequence getSelectedText(int flags) {
5641            if (mTarget == null) return "";
5642            return mTarget.getSelectedText(flags);
5643        }
5644
5645        @Override
5646        public int getCursorCapsMode(int reqModes) {
5647            if (mTarget == null) return InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
5648            return mTarget.getCursorCapsMode(reqModes);
5649        }
5650
5651        @Override
5652        public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
5653            return getTarget().getExtractedText(request, flags);
5654        }
5655
5656        @Override
5657        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
5658            return getTarget().deleteSurroundingText(beforeLength, afterLength);
5659        }
5660
5661        @Override
5662        public boolean setComposingText(CharSequence text, int newCursorPosition) {
5663            return getTarget().setComposingText(text, newCursorPosition);
5664        }
5665
5666        @Override
5667        public boolean setComposingRegion(int start, int end) {
5668            return getTarget().setComposingRegion(start, end);
5669        }
5670
5671        @Override
5672        public boolean finishComposingText() {
5673            return mTarget == null || mTarget.finishComposingText();
5674        }
5675
5676        @Override
5677        public boolean commitText(CharSequence text, int newCursorPosition) {
5678            return getTarget().commitText(text, newCursorPosition);
5679        }
5680
5681        @Override
5682        public boolean commitCompletion(CompletionInfo text) {
5683            return getTarget().commitCompletion(text);
5684        }
5685
5686        @Override
5687        public boolean commitCorrection(CorrectionInfo correctionInfo) {
5688            return getTarget().commitCorrection(correctionInfo);
5689        }
5690
5691        @Override
5692        public boolean setSelection(int start, int end) {
5693            return getTarget().setSelection(start, end);
5694        }
5695
5696        @Override
5697        public boolean performContextMenuAction(int id) {
5698            return getTarget().performContextMenuAction(id);
5699        }
5700
5701        @Override
5702        public boolean beginBatchEdit() {
5703            return getTarget().beginBatchEdit();
5704        }
5705
5706        @Override
5707        public boolean endBatchEdit() {
5708            return getTarget().endBatchEdit();
5709        }
5710
5711        @Override
5712        public boolean clearMetaKeyStates(int states) {
5713            return getTarget().clearMetaKeyStates(states);
5714        }
5715
5716        @Override
5717        public boolean performPrivateCommand(String action, Bundle data) {
5718            return getTarget().performPrivateCommand(action, data);
5719        }
5720
5721        @Override
5722        public boolean requestCursorUpdates(int cursorUpdateMode) {
5723            return getTarget().requestCursorUpdates(cursorUpdateMode);
5724        }
5725
5726        /**
5727         * @removed
5728         */
5729        @Override
5730        public boolean requestUpdateCursorAnchorInfo(int cursorUpdateMode) {
5731            return getTarget().requestCursorUpdates(cursorUpdateMode);
5732        }
5733    }
5734
5735    /**
5736     * For filtering we proxy an input connection to an internal text editor,
5737     * and this allows the proxying to happen.
5738     */
5739    @Override
5740    public boolean checkInputConnectionProxy(View view) {
5741        return view == mTextFilter;
5742    }
5743
5744    /**
5745     * Creates the window for the text filter and populates it with an EditText field;
5746     *
5747     * @param animateEntrance true if the window should appear with an animation
5748     */
5749    private void createTextFilter(boolean animateEntrance) {
5750        if (mPopup == null) {
5751            PopupWindow p = new PopupWindow(getContext());
5752            p.setFocusable(false);
5753            p.setTouchable(false);
5754            p.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
5755            p.setContentView(getTextFilterInput());
5756            p.setWidth(LayoutParams.WRAP_CONTENT);
5757            p.setHeight(LayoutParams.WRAP_CONTENT);
5758            p.setBackgroundDrawable(null);
5759            mPopup = p;
5760            getViewTreeObserver().addOnGlobalLayoutListener(this);
5761            mGlobalLayoutListenerAddedFilter = true;
5762        }
5763        if (animateEntrance) {
5764            mPopup.setAnimationStyle(com.android.internal.R.style.Animation_TypingFilter);
5765        } else {
5766            mPopup.setAnimationStyle(com.android.internal.R.style.Animation_TypingFilterRestore);
5767        }
5768    }
5769
5770    private EditText getTextFilterInput() {
5771        if (mTextFilter == null) {
5772            final LayoutInflater layoutInflater = LayoutInflater.from(getContext());
5773            mTextFilter = (EditText) layoutInflater.inflate(
5774                    com.android.internal.R.layout.typing_filter, null);
5775            // For some reason setting this as the "real" input type changes
5776            // the text view in some way that it doesn't work, and I don't
5777            // want to figure out why this is.
5778            mTextFilter.setRawInputType(EditorInfo.TYPE_CLASS_TEXT
5779                    | EditorInfo.TYPE_TEXT_VARIATION_FILTER);
5780            mTextFilter.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
5781            mTextFilter.addTextChangedListener(this);
5782        }
5783        return mTextFilter;
5784    }
5785
5786    /**
5787     * Clear the text filter.
5788     */
5789    public void clearTextFilter() {
5790        if (mFiltered) {
5791            getTextFilterInput().setText("");
5792            mFiltered = false;
5793            if (mPopup != null && mPopup.isShowing()) {
5794                dismissPopup();
5795            }
5796        }
5797    }
5798
5799    /**
5800     * Returns if the ListView currently has a text filter.
5801     */
5802    public boolean hasTextFilter() {
5803        return mFiltered;
5804    }
5805
5806    @Override
5807    public void onGlobalLayout() {
5808        if (isShown()) {
5809            // Show the popup if we are filtered
5810            if (mFiltered && mPopup != null && !mPopup.isShowing() && !mPopupHidden) {
5811                showPopup();
5812            }
5813        } else {
5814            // Hide the popup when we are no longer visible
5815            if (mPopup != null && mPopup.isShowing()) {
5816                dismissPopup();
5817            }
5818        }
5819
5820    }
5821
5822    /**
5823     * For our text watcher that is associated with the text filter.  Does
5824     * nothing.
5825     */
5826    @Override
5827    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
5828    }
5829
5830    /**
5831     * For our text watcher that is associated with the text filter. Performs
5832     * the actual filtering as the text changes, and takes care of hiding and
5833     * showing the popup displaying the currently entered filter text.
5834     */
5835    @Override
5836    public void onTextChanged(CharSequence s, int start, int before, int count) {
5837        if (isTextFilterEnabled()) {
5838            createTextFilter(true);
5839            int length = s.length();
5840            boolean showing = mPopup.isShowing();
5841            if (!showing && length > 0) {
5842                // Show the filter popup if necessary
5843                showPopup();
5844                mFiltered = true;
5845            } else if (showing && length == 0) {
5846                // Remove the filter popup if the user has cleared all text
5847                dismissPopup();
5848                mFiltered = false;
5849            }
5850            if (mAdapter instanceof Filterable) {
5851                Filter f = ((Filterable) mAdapter).getFilter();
5852                // Filter should not be null when we reach this part
5853                if (f != null) {
5854                    f.filter(s, this);
5855                } else {
5856                    throw new IllegalStateException("You cannot call onTextChanged with a non "
5857                            + "filterable adapter");
5858                }
5859            }
5860        }
5861    }
5862
5863    /**
5864     * For our text watcher that is associated with the text filter.  Does
5865     * nothing.
5866     */
5867    @Override
5868    public void afterTextChanged(Editable s) {
5869    }
5870
5871    @Override
5872    public void onFilterComplete(int count) {
5873        if (mSelectedPosition < 0 && count > 0) {
5874            mResurrectToPosition = INVALID_POSITION;
5875            resurrectSelection();
5876        }
5877    }
5878
5879    @Override
5880    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
5881        return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
5882                ViewGroup.LayoutParams.WRAP_CONTENT, 0);
5883    }
5884
5885    @Override
5886    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
5887        return new LayoutParams(p);
5888    }
5889
5890    @Override
5891    public LayoutParams generateLayoutParams(AttributeSet attrs) {
5892        return new AbsListView.LayoutParams(getContext(), attrs);
5893    }
5894
5895    @Override
5896    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
5897        return p instanceof AbsListView.LayoutParams;
5898    }
5899
5900    /**
5901     * Puts the list or grid into transcript mode. In this mode the list or grid will always scroll
5902     * to the bottom to show new items.
5903     *
5904     * @param mode the transcript mode to set
5905     *
5906     * @see #TRANSCRIPT_MODE_DISABLED
5907     * @see #TRANSCRIPT_MODE_NORMAL
5908     * @see #TRANSCRIPT_MODE_ALWAYS_SCROLL
5909     */
5910    public void setTranscriptMode(int mode) {
5911        mTranscriptMode = mode;
5912    }
5913
5914    /**
5915     * Returns the current transcript mode.
5916     *
5917     * @return {@link #TRANSCRIPT_MODE_DISABLED}, {@link #TRANSCRIPT_MODE_NORMAL} or
5918     *         {@link #TRANSCRIPT_MODE_ALWAYS_SCROLL}
5919     */
5920    public int getTranscriptMode() {
5921        return mTranscriptMode;
5922    }
5923
5924    @Override
5925    public int getSolidColor() {
5926        return mCacheColorHint;
5927    }
5928
5929    /**
5930     * When set to a non-zero value, the cache color hint indicates that this list is always drawn
5931     * on top of a solid, single-color, opaque background.
5932     *
5933     * Zero means that what's behind this object is translucent (non solid) or is not made of a
5934     * single color. This hint will not affect any existing background drawable set on this view (
5935     * typically set via {@link #setBackgroundDrawable(Drawable)}).
5936     *
5937     * @param color The background color
5938     */
5939    public void setCacheColorHint(int color) {
5940        if (color != mCacheColorHint) {
5941            mCacheColorHint = color;
5942            int count = getChildCount();
5943            for (int i = 0; i < count; i++) {
5944                getChildAt(i).setDrawingCacheBackgroundColor(color);
5945            }
5946            mRecycler.setCacheColorHint(color);
5947        }
5948    }
5949
5950    /**
5951     * When set to a non-zero value, the cache color hint indicates that this list is always drawn
5952     * on top of a solid, single-color, opaque background
5953     *
5954     * @return The cache color hint
5955     */
5956    @ViewDebug.ExportedProperty(category = "drawing")
5957    public int getCacheColorHint() {
5958        return mCacheColorHint;
5959    }
5960
5961    /**
5962     * Move all views (excluding headers and footers) held by this AbsListView into the supplied
5963     * List. This includes views displayed on the screen as well as views stored in AbsListView's
5964     * internal view recycler.
5965     *
5966     * @param views A list into which to put the reclaimed views
5967     */
5968    public void reclaimViews(List<View> views) {
5969        int childCount = getChildCount();
5970        RecyclerListener listener = mRecycler.mRecyclerListener;
5971
5972        // Reclaim views on screen
5973        for (int i = 0; i < childCount; i++) {
5974            View child = getChildAt(i);
5975            AbsListView.LayoutParams lp = (AbsListView.LayoutParams) child.getLayoutParams();
5976            // Don't reclaim header or footer views, or views that should be ignored
5977            if (lp != null && mRecycler.shouldRecycleViewType(lp.viewType)) {
5978                views.add(child);
5979                child.setAccessibilityDelegate(null);
5980                if (listener != null) {
5981                    // Pretend they went through the scrap heap
5982                    listener.onMovedToScrapHeap(child);
5983                }
5984            }
5985        }
5986        mRecycler.reclaimScrapViews(views);
5987        removeAllViewsInLayout();
5988    }
5989
5990    private void finishGlows() {
5991        if (mEdgeGlowTop != null) {
5992            mEdgeGlowTop.finish();
5993            mEdgeGlowBottom.finish();
5994        }
5995    }
5996
5997    /**
5998     * Sets up this AbsListView to use a remote views adapter which connects to a RemoteViewsService
5999     * through the specified intent.
6000     * @param intent the intent used to identify the RemoteViewsService for the adapter to connect to.
6001     */
6002    public void setRemoteViewsAdapter(Intent intent) {
6003        // Ensure that we don't already have a RemoteViewsAdapter that is bound to an existing
6004        // service handling the specified intent.
6005        if (mRemoteAdapter != null) {
6006            Intent.FilterComparison fcNew = new Intent.FilterComparison(intent);
6007            Intent.FilterComparison fcOld = new Intent.FilterComparison(
6008                    mRemoteAdapter.getRemoteViewsServiceIntent());
6009            if (fcNew.equals(fcOld)) {
6010                return;
6011            }
6012        }
6013        mDeferNotifyDataSetChanged = false;
6014        // Otherwise, create a new RemoteViewsAdapter for binding
6015        mRemoteAdapter = new RemoteViewsAdapter(getContext(), intent, this);
6016        if (mRemoteAdapter.isDataReady()) {
6017            setAdapter(mRemoteAdapter);
6018        }
6019    }
6020
6021    /**
6022     * Sets up the onClickHandler to be used by the RemoteViewsAdapter when inflating RemoteViews
6023     *
6024     * @param handler The OnClickHandler to use when inflating RemoteViews.
6025     *
6026     * @hide
6027     */
6028    public void setRemoteViewsOnClickHandler(OnClickHandler handler) {
6029        // Ensure that we don't already have a RemoteViewsAdapter that is bound to an existing
6030        // service handling the specified intent.
6031        if (mRemoteAdapter != null) {
6032            mRemoteAdapter.setRemoteViewsOnClickHandler(handler);
6033        }
6034    }
6035
6036    /**
6037     * This defers a notifyDataSetChanged on the pending RemoteViewsAdapter if it has not
6038     * connected yet.
6039     */
6040    @Override
6041    public void deferNotifyDataSetChanged() {
6042        mDeferNotifyDataSetChanged = true;
6043    }
6044
6045    /**
6046     * Called back when the adapter connects to the RemoteViewsService.
6047     */
6048    @Override
6049    public boolean onRemoteAdapterConnected() {
6050        if (mRemoteAdapter != mAdapter) {
6051            setAdapter(mRemoteAdapter);
6052            if (mDeferNotifyDataSetChanged) {
6053                mRemoteAdapter.notifyDataSetChanged();
6054                mDeferNotifyDataSetChanged = false;
6055            }
6056            return false;
6057        } else if (mRemoteAdapter != null) {
6058            mRemoteAdapter.superNotifyDataSetChanged();
6059            return true;
6060        }
6061        return false;
6062    }
6063
6064    /**
6065     * Called back when the adapter disconnects from the RemoteViewsService.
6066     */
6067    @Override
6068    public void onRemoteAdapterDisconnected() {
6069        // If the remote adapter disconnects, we keep it around
6070        // since the currently displayed items are still cached.
6071        // Further, we want the service to eventually reconnect
6072        // when necessary, as triggered by this view requesting
6073        // items from the Adapter.
6074    }
6075
6076    /**
6077     * Hints the RemoteViewsAdapter, if it exists, about which views are currently
6078     * being displayed by the AbsListView.
6079     */
6080    void setVisibleRangeHint(int start, int end) {
6081        if (mRemoteAdapter != null) {
6082            mRemoteAdapter.setVisibleRangeHint(start, end);
6083        }
6084    }
6085
6086    /**
6087     * Sets the recycler listener to be notified whenever a View is set aside in
6088     * the recycler for later reuse. This listener can be used to free resources
6089     * associated to the View.
6090     *
6091     * @param listener The recycler listener to be notified of views set aside
6092     *        in the recycler.
6093     *
6094     * @see android.widget.AbsListView.RecycleBin
6095     * @see android.widget.AbsListView.RecyclerListener
6096     */
6097    public void setRecyclerListener(RecyclerListener listener) {
6098        mRecycler.mRecyclerListener = listener;
6099    }
6100
6101    class AdapterDataSetObserver extends AdapterView<ListAdapter>.AdapterDataSetObserver {
6102        @Override
6103        public void onChanged() {
6104            super.onChanged();
6105            if (mFastScroll != null) {
6106                mFastScroll.onSectionsChanged();
6107            }
6108        }
6109
6110        @Override
6111        public void onInvalidated() {
6112            super.onInvalidated();
6113            if (mFastScroll != null) {
6114                mFastScroll.onSectionsChanged();
6115            }
6116        }
6117    }
6118
6119    /**
6120     * A MultiChoiceModeListener receives events for {@link AbsListView#CHOICE_MODE_MULTIPLE_MODAL}.
6121     * It acts as the {@link ActionMode.Callback} for the selection mode and also receives
6122     * {@link #onItemCheckedStateChanged(ActionMode, int, long, boolean)} events when the user
6123     * selects and deselects list items.
6124     */
6125    public interface MultiChoiceModeListener extends ActionMode.Callback {
6126        /**
6127         * Called when an item is checked or unchecked during selection mode.
6128         *
6129         * @param mode The {@link ActionMode} providing the selection mode
6130         * @param position Adapter position of the item that was checked or unchecked
6131         * @param id Adapter ID of the item that was checked or unchecked
6132         * @param checked <code>true</code> if the item is now checked, <code>false</code>
6133         *                if the item is now unchecked.
6134         */
6135        public void onItemCheckedStateChanged(ActionMode mode,
6136                int position, long id, boolean checked);
6137    }
6138
6139    class MultiChoiceModeWrapper implements MultiChoiceModeListener {
6140        private MultiChoiceModeListener mWrapped;
6141
6142        public void setWrapped(MultiChoiceModeListener wrapped) {
6143            mWrapped = wrapped;
6144        }
6145
6146        public boolean hasWrappedCallback() {
6147            return mWrapped != null;
6148        }
6149
6150        @Override
6151        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
6152            if (mWrapped.onCreateActionMode(mode, menu)) {
6153                // Initialize checked graphic state?
6154                setLongClickable(false);
6155                return true;
6156            }
6157            return false;
6158        }
6159
6160        @Override
6161        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
6162            return mWrapped.onPrepareActionMode(mode, menu);
6163        }
6164
6165        @Override
6166        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
6167            return mWrapped.onActionItemClicked(mode, item);
6168        }
6169
6170        @Override
6171        public void onDestroyActionMode(ActionMode mode) {
6172            mWrapped.onDestroyActionMode(mode);
6173            mChoiceActionMode = null;
6174
6175            // Ending selection mode means deselecting everything.
6176            clearChoices();
6177
6178            mDataChanged = true;
6179            rememberSyncState();
6180            requestLayout();
6181
6182            setLongClickable(true);
6183        }
6184
6185        @Override
6186        public void onItemCheckedStateChanged(ActionMode mode,
6187                int position, long id, boolean checked) {
6188            mWrapped.onItemCheckedStateChanged(mode, position, id, checked);
6189
6190            // If there are no items selected we no longer need the selection mode.
6191            if (getCheckedItemCount() == 0) {
6192                mode.finish();
6193            }
6194        }
6195    }
6196
6197    /**
6198     * AbsListView extends LayoutParams to provide a place to hold the view type.
6199     */
6200    public static class LayoutParams extends ViewGroup.LayoutParams {
6201        /**
6202         * View type for this view, as returned by
6203         * {@link android.widget.Adapter#getItemViewType(int) }
6204         */
6205        @ViewDebug.ExportedProperty(category = "list", mapping = {
6206            @ViewDebug.IntToString(from = ITEM_VIEW_TYPE_IGNORE, to = "ITEM_VIEW_TYPE_IGNORE"),
6207            @ViewDebug.IntToString(from = ITEM_VIEW_TYPE_HEADER_OR_FOOTER, to = "ITEM_VIEW_TYPE_HEADER_OR_FOOTER")
6208        })
6209        int viewType;
6210
6211        /**
6212         * When this boolean is set, the view has been added to the AbsListView
6213         * at least once. It is used to know whether headers/footers have already
6214         * been added to the list view and whether they should be treated as
6215         * recycled views or not.
6216         */
6217        @ViewDebug.ExportedProperty(category = "list")
6218        boolean recycledHeaderFooter;
6219
6220        /**
6221         * When an AbsListView is measured with an AT_MOST measure spec, it needs
6222         * to obtain children views to measure itself. When doing so, the children
6223         * are not attached to the window, but put in the recycler which assumes
6224         * they've been attached before. Setting this flag will force the reused
6225         * view to be attached to the window rather than just attached to the
6226         * parent.
6227         */
6228        @ViewDebug.ExportedProperty(category = "list")
6229        boolean forceAdd;
6230
6231        /**
6232         * The position the view was removed from when pulled out of the
6233         * scrap heap.
6234         * @hide
6235         */
6236        int scrappedFromPosition;
6237
6238        /**
6239         * The ID the view represents
6240         */
6241        long itemId = -1;
6242
6243        public LayoutParams(Context c, AttributeSet attrs) {
6244            super(c, attrs);
6245        }
6246
6247        public LayoutParams(int w, int h) {
6248            super(w, h);
6249        }
6250
6251        public LayoutParams(int w, int h, int viewType) {
6252            super(w, h);
6253            this.viewType = viewType;
6254        }
6255
6256        public LayoutParams(ViewGroup.LayoutParams source) {
6257            super(source);
6258        }
6259    }
6260
6261    /**
6262     * A RecyclerListener is used to receive a notification whenever a View is placed
6263     * inside the RecycleBin's scrap heap. This listener is used to free resources
6264     * associated to Views placed in the RecycleBin.
6265     *
6266     * @see android.widget.AbsListView.RecycleBin
6267     * @see android.widget.AbsListView#setRecyclerListener(android.widget.AbsListView.RecyclerListener)
6268     */
6269    public static interface RecyclerListener {
6270        /**
6271         * Indicates that the specified View was moved into the recycler's scrap heap.
6272         * The view is not displayed on screen any more and any expensive resource
6273         * associated with the view should be discarded.
6274         *
6275         * @param view
6276         */
6277        void onMovedToScrapHeap(View view);
6278    }
6279
6280    /**
6281     * The RecycleBin facilitates reuse of views across layouts. The RecycleBin has two levels of
6282     * storage: ActiveViews and ScrapViews. ActiveViews are those views which were onscreen at the
6283     * start of a layout. By construction, they are displaying current information. At the end of
6284     * layout, all views in ActiveViews are demoted to ScrapViews. ScrapViews are old views that
6285     * could potentially be used by the adapter to avoid allocating views unnecessarily.
6286     *
6287     * @see android.widget.AbsListView#setRecyclerListener(android.widget.AbsListView.RecyclerListener)
6288     * @see android.widget.AbsListView.RecyclerListener
6289     */
6290    class RecycleBin {
6291        private RecyclerListener mRecyclerListener;
6292
6293        /**
6294         * The position of the first view stored in mActiveViews.
6295         */
6296        private int mFirstActivePosition;
6297
6298        /**
6299         * Views that were on screen at the start of layout. This array is populated at the start of
6300         * layout, and at the end of layout all view in mActiveViews are moved to mScrapViews.
6301         * Views in mActiveViews represent a contiguous range of Views, with position of the first
6302         * view store in mFirstActivePosition.
6303         */
6304        private View[] mActiveViews = new View[0];
6305
6306        /**
6307         * Unsorted views that can be used by the adapter as a convert view.
6308         */
6309        private ArrayList<View>[] mScrapViews;
6310
6311        private int mViewTypeCount;
6312
6313        private ArrayList<View> mCurrentScrap;
6314
6315        private ArrayList<View> mSkippedScrap;
6316
6317        private SparseArray<View> mTransientStateViews;
6318        private LongSparseArray<View> mTransientStateViewsById;
6319
6320        public void setViewTypeCount(int viewTypeCount) {
6321            if (viewTypeCount < 1) {
6322                throw new IllegalArgumentException("Can't have a viewTypeCount < 1");
6323            }
6324            //noinspection unchecked
6325            ArrayList<View>[] scrapViews = new ArrayList[viewTypeCount];
6326            for (int i = 0; i < viewTypeCount; i++) {
6327                scrapViews[i] = new ArrayList<View>();
6328            }
6329            mViewTypeCount = viewTypeCount;
6330            mCurrentScrap = scrapViews[0];
6331            mScrapViews = scrapViews;
6332        }
6333
6334        public void markChildrenDirty() {
6335            if (mViewTypeCount == 1) {
6336                final ArrayList<View> scrap = mCurrentScrap;
6337                final int scrapCount = scrap.size();
6338                for (int i = 0; i < scrapCount; i++) {
6339                    scrap.get(i).forceLayout();
6340                }
6341            } else {
6342                final int typeCount = mViewTypeCount;
6343                for (int i = 0; i < typeCount; i++) {
6344                    final ArrayList<View> scrap = mScrapViews[i];
6345                    final int scrapCount = scrap.size();
6346                    for (int j = 0; j < scrapCount; j++) {
6347                        scrap.get(j).forceLayout();
6348                    }
6349                }
6350            }
6351            if (mTransientStateViews != null) {
6352                final int count = mTransientStateViews.size();
6353                for (int i = 0; i < count; i++) {
6354                    mTransientStateViews.valueAt(i).forceLayout();
6355                }
6356            }
6357            if (mTransientStateViewsById != null) {
6358                final int count = mTransientStateViewsById.size();
6359                for (int i = 0; i < count; i++) {
6360                    mTransientStateViewsById.valueAt(i).forceLayout();
6361                }
6362            }
6363        }
6364
6365        public boolean shouldRecycleViewType(int viewType) {
6366            return viewType >= 0;
6367        }
6368
6369        /**
6370         * Clears the scrap heap.
6371         */
6372        void clear() {
6373            if (mViewTypeCount == 1) {
6374                final ArrayList<View> scrap = mCurrentScrap;
6375                clearScrap(scrap);
6376            } else {
6377                final int typeCount = mViewTypeCount;
6378                for (int i = 0; i < typeCount; i++) {
6379                    final ArrayList<View> scrap = mScrapViews[i];
6380                    clearScrap(scrap);
6381                }
6382            }
6383
6384            clearTransientStateViews();
6385        }
6386
6387        /**
6388         * Fill ActiveViews with all of the children of the AbsListView.
6389         *
6390         * @param childCount The minimum number of views mActiveViews should hold
6391         * @param firstActivePosition The position of the first view that will be stored in
6392         *        mActiveViews
6393         */
6394        void fillActiveViews(int childCount, int firstActivePosition) {
6395            if (mActiveViews.length < childCount) {
6396                mActiveViews = new View[childCount];
6397            }
6398            mFirstActivePosition = firstActivePosition;
6399
6400            //noinspection MismatchedReadAndWriteOfArray
6401            final View[] activeViews = mActiveViews;
6402            for (int i = 0; i < childCount; i++) {
6403                View child = getChildAt(i);
6404                AbsListView.LayoutParams lp = (AbsListView.LayoutParams) child.getLayoutParams();
6405                // Don't put header or footer views into the scrap heap
6406                if (lp != null && lp.viewType != ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
6407                    // Note:  We do place AdapterView.ITEM_VIEW_TYPE_IGNORE in active views.
6408                    //        However, we will NOT place them into scrap views.
6409                    activeViews[i] = child;
6410                }
6411            }
6412        }
6413
6414        /**
6415         * Get the view corresponding to the specified position. The view will be removed from
6416         * mActiveViews if it is found.
6417         *
6418         * @param position The position to look up in mActiveViews
6419         * @return The view if it is found, null otherwise
6420         */
6421        View getActiveView(int position) {
6422            int index = position - mFirstActivePosition;
6423            final View[] activeViews = mActiveViews;
6424            if (index >=0 && index < activeViews.length) {
6425                final View match = activeViews[index];
6426                activeViews[index] = null;
6427                return match;
6428            }
6429            return null;
6430        }
6431
6432        View getTransientStateView(int position) {
6433            if (mAdapter != null && mAdapterHasStableIds && mTransientStateViewsById != null) {
6434                long id = mAdapter.getItemId(position);
6435                View result = mTransientStateViewsById.get(id);
6436                mTransientStateViewsById.remove(id);
6437                return result;
6438            }
6439            if (mTransientStateViews != null) {
6440                final int index = mTransientStateViews.indexOfKey(position);
6441                if (index >= 0) {
6442                    View result = mTransientStateViews.valueAt(index);
6443                    mTransientStateViews.removeAt(index);
6444                    return result;
6445                }
6446            }
6447            return null;
6448        }
6449
6450        /**
6451         * Dumps and fully detaches any currently saved views with transient
6452         * state.
6453         */
6454        void clearTransientStateViews() {
6455            final SparseArray<View> viewsByPos = mTransientStateViews;
6456            if (viewsByPos != null) {
6457                final int N = viewsByPos.size();
6458                for (int i = 0; i < N; i++) {
6459                    removeDetachedView(viewsByPos.valueAt(i), false);
6460                }
6461                viewsByPos.clear();
6462            }
6463
6464            final LongSparseArray<View> viewsById = mTransientStateViewsById;
6465            if (viewsById != null) {
6466                final int N = viewsById.size();
6467                for (int i = 0; i < N; i++) {
6468                    removeDetachedView(viewsById.valueAt(i), false);
6469                }
6470                viewsById.clear();
6471            }
6472        }
6473
6474        /**
6475         * @return A view from the ScrapViews collection. These are unordered.
6476         */
6477        View getScrapView(int position) {
6478            if (mViewTypeCount == 1) {
6479                return retrieveFromScrap(mCurrentScrap, position);
6480            } else {
6481                final int whichScrap = mAdapter.getItemViewType(position);
6482                if (whichScrap >= 0 && whichScrap < mScrapViews.length) {
6483                    return retrieveFromScrap(mScrapViews[whichScrap], position);
6484                }
6485            }
6486            return null;
6487        }
6488
6489        /**
6490         * Puts a view into the list of scrap views.
6491         * <p>
6492         * If the list data hasn't changed or the adapter has stable IDs, views
6493         * with transient state will be preserved for later retrieval.
6494         *
6495         * @param scrap The view to add
6496         * @param position The view's position within its parent
6497         */
6498        void addScrapView(View scrap, int position) {
6499            final AbsListView.LayoutParams lp = (AbsListView.LayoutParams) scrap.getLayoutParams();
6500            if (lp == null) {
6501                return;
6502            }
6503
6504            lp.scrappedFromPosition = position;
6505
6506            // Remove but don't scrap header or footer views, or views that
6507            // should otherwise not be recycled.
6508            final int viewType = lp.viewType;
6509            if (!shouldRecycleViewType(viewType)) {
6510                return;
6511            }
6512
6513            scrap.dispatchStartTemporaryDetach();
6514
6515            // The the accessibility state of the view may change while temporary
6516            // detached and we do not allow detached views to fire accessibility
6517            // events. So we are announcing that the subtree changed giving a chance
6518            // to clients holding on to a view in this subtree to refresh it.
6519            notifyViewAccessibilityStateChangedIfNeeded(
6520                    AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
6521
6522            // Don't scrap views that have transient state.
6523            final boolean scrapHasTransientState = scrap.hasTransientState();
6524            if (scrapHasTransientState) {
6525                if (mAdapter != null && mAdapterHasStableIds) {
6526                    // If the adapter has stable IDs, we can reuse the view for
6527                    // the same data.
6528                    if (mTransientStateViewsById == null) {
6529                        mTransientStateViewsById = new LongSparseArray<View>();
6530                    }
6531                    mTransientStateViewsById.put(lp.itemId, scrap);
6532                } else if (!mDataChanged) {
6533                    // If the data hasn't changed, we can reuse the views at
6534                    // their old positions.
6535                    if (mTransientStateViews == null) {
6536                        mTransientStateViews = new SparseArray<View>();
6537                    }
6538                    mTransientStateViews.put(position, scrap);
6539                } else {
6540                    // Otherwise, we'll have to remove the view and start over.
6541                    if (mSkippedScrap == null) {
6542                        mSkippedScrap = new ArrayList<View>();
6543                    }
6544                    mSkippedScrap.add(scrap);
6545                }
6546            } else {
6547                if (mViewTypeCount == 1) {
6548                    mCurrentScrap.add(scrap);
6549                } else {
6550                    mScrapViews[viewType].add(scrap);
6551                }
6552
6553                if (mRecyclerListener != null) {
6554                    mRecyclerListener.onMovedToScrapHeap(scrap);
6555                }
6556            }
6557        }
6558
6559        /**
6560         * Finish the removal of any views that skipped the scrap heap.
6561         */
6562        void removeSkippedScrap() {
6563            if (mSkippedScrap == null) {
6564                return;
6565            }
6566            final int count = mSkippedScrap.size();
6567            for (int i = 0; i < count; i++) {
6568                removeDetachedView(mSkippedScrap.get(i), false);
6569            }
6570            mSkippedScrap.clear();
6571        }
6572
6573        /**
6574         * Move all views remaining in mActiveViews to mScrapViews.
6575         */
6576        void scrapActiveViews() {
6577            final View[] activeViews = mActiveViews;
6578            final boolean hasListener = mRecyclerListener != null;
6579            final boolean multipleScraps = mViewTypeCount > 1;
6580
6581            ArrayList<View> scrapViews = mCurrentScrap;
6582            final int count = activeViews.length;
6583            for (int i = count - 1; i >= 0; i--) {
6584                final View victim = activeViews[i];
6585                if (victim != null) {
6586                    final AbsListView.LayoutParams lp
6587                            = (AbsListView.LayoutParams) victim.getLayoutParams();
6588                    final int whichScrap = lp.viewType;
6589
6590                    activeViews[i] = null;
6591
6592                    if (victim.hasTransientState()) {
6593                        // Store views with transient state for later use.
6594                        victim.dispatchStartTemporaryDetach();
6595
6596                        if (mAdapter != null && mAdapterHasStableIds) {
6597                            if (mTransientStateViewsById == null) {
6598                                mTransientStateViewsById = new LongSparseArray<View>();
6599                            }
6600                            long id = mAdapter.getItemId(mFirstActivePosition + i);
6601                            mTransientStateViewsById.put(id, victim);
6602                        } else if (!mDataChanged) {
6603                            if (mTransientStateViews == null) {
6604                                mTransientStateViews = new SparseArray<View>();
6605                            }
6606                            mTransientStateViews.put(mFirstActivePosition + i, victim);
6607                        } else if (whichScrap != ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
6608                            // The data has changed, we can't keep this view.
6609                            removeDetachedView(victim, false);
6610                        }
6611                    } else if (!shouldRecycleViewType(whichScrap)) {
6612                        // Discard non-recyclable views except headers/footers.
6613                        if (whichScrap != ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
6614                            removeDetachedView(victim, false);
6615                        }
6616                    } else {
6617                        // Store everything else on the appropriate scrap heap.
6618                        if (multipleScraps) {
6619                            scrapViews = mScrapViews[whichScrap];
6620                        }
6621
6622                        victim.dispatchStartTemporaryDetach();
6623                        lp.scrappedFromPosition = mFirstActivePosition + i;
6624                        scrapViews.add(victim);
6625
6626                        if (hasListener) {
6627                            mRecyclerListener.onMovedToScrapHeap(victim);
6628                        }
6629                    }
6630                }
6631            }
6632
6633            pruneScrapViews();
6634        }
6635
6636        /**
6637         * Makes sure that the size of mScrapViews does not exceed the size of
6638         * mActiveViews, which can happen if an adapter does not recycle its
6639         * views. Removes cached transient state views that no longer have
6640         * transient state.
6641         */
6642        private void pruneScrapViews() {
6643            final int maxViews = mActiveViews.length;
6644            final int viewTypeCount = mViewTypeCount;
6645            final ArrayList<View>[] scrapViews = mScrapViews;
6646            for (int i = 0; i < viewTypeCount; ++i) {
6647                final ArrayList<View> scrapPile = scrapViews[i];
6648                int size = scrapPile.size();
6649                final int extras = size - maxViews;
6650                size--;
6651                for (int j = 0; j < extras; j++) {
6652                    removeDetachedView(scrapPile.remove(size--), false);
6653                }
6654            }
6655
6656            final SparseArray<View> transViewsByPos = mTransientStateViews;
6657            if (transViewsByPos != null) {
6658                for (int i = 0; i < transViewsByPos.size(); i++) {
6659                    final View v = transViewsByPos.valueAt(i);
6660                    if (!v.hasTransientState()) {
6661                        removeDetachedView(v, false);
6662                        transViewsByPos.removeAt(i);
6663                        i--;
6664                    }
6665                }
6666            }
6667
6668            final LongSparseArray<View> transViewsById = mTransientStateViewsById;
6669            if (transViewsById != null) {
6670                for (int i = 0; i < transViewsById.size(); i++) {
6671                    final View v = transViewsById.valueAt(i);
6672                    if (!v.hasTransientState()) {
6673                        removeDetachedView(v, false);
6674                        transViewsById.removeAt(i);
6675                        i--;
6676                    }
6677                }
6678            }
6679        }
6680
6681        /**
6682         * Puts all views in the scrap heap into the supplied list.
6683         */
6684        void reclaimScrapViews(List<View> views) {
6685            if (mViewTypeCount == 1) {
6686                views.addAll(mCurrentScrap);
6687            } else {
6688                final int viewTypeCount = mViewTypeCount;
6689                final ArrayList<View>[] scrapViews = mScrapViews;
6690                for (int i = 0; i < viewTypeCount; ++i) {
6691                    final ArrayList<View> scrapPile = scrapViews[i];
6692                    views.addAll(scrapPile);
6693                }
6694            }
6695        }
6696
6697        /**
6698         * Updates the cache color hint of all known views.
6699         *
6700         * @param color The new cache color hint.
6701         */
6702        void setCacheColorHint(int color) {
6703            if (mViewTypeCount == 1) {
6704                final ArrayList<View> scrap = mCurrentScrap;
6705                final int scrapCount = scrap.size();
6706                for (int i = 0; i < scrapCount; i++) {
6707                    scrap.get(i).setDrawingCacheBackgroundColor(color);
6708                }
6709            } else {
6710                final int typeCount = mViewTypeCount;
6711                for (int i = 0; i < typeCount; i++) {
6712                    final ArrayList<View> scrap = mScrapViews[i];
6713                    final int scrapCount = scrap.size();
6714                    for (int j = 0; j < scrapCount; j++) {
6715                        scrap.get(j).setDrawingCacheBackgroundColor(color);
6716                    }
6717                }
6718            }
6719            // Just in case this is called during a layout pass
6720            final View[] activeViews = mActiveViews;
6721            final int count = activeViews.length;
6722            for (int i = 0; i < count; ++i) {
6723                final View victim = activeViews[i];
6724                if (victim != null) {
6725                    victim.setDrawingCacheBackgroundColor(color);
6726                }
6727            }
6728        }
6729
6730        private View retrieveFromScrap(ArrayList<View> scrapViews, int position) {
6731            final int size = scrapViews.size();
6732            if (size > 0) {
6733                // See if we still have a view for this position or ID.
6734                for (int i = 0; i < size; i++) {
6735                    final View view = scrapViews.get(i);
6736                    final AbsListView.LayoutParams params =
6737                            (AbsListView.LayoutParams) view.getLayoutParams();
6738
6739                    if (mAdapterHasStableIds) {
6740                        final long id = mAdapter.getItemId(position);
6741                        if (id == params.itemId) {
6742                            return scrapViews.remove(i);
6743                        }
6744                    } else if (params.scrappedFromPosition == position) {
6745                        final View scrap = scrapViews.remove(i);
6746                        clearAccessibilityFromScrap(scrap);
6747                        return scrap;
6748                    }
6749                }
6750                final View scrap = scrapViews.remove(size - 1);
6751                clearAccessibilityFromScrap(scrap);
6752                return scrap;
6753            } else {
6754                return null;
6755            }
6756        }
6757
6758        private void clearScrap(final ArrayList<View> scrap) {
6759            final int scrapCount = scrap.size();
6760            for (int j = 0; j < scrapCount; j++) {
6761                removeDetachedView(scrap.remove(scrapCount - 1 - j), false);
6762            }
6763        }
6764
6765        private void clearAccessibilityFromScrap(View view) {
6766            if (view.isAccessibilityFocused()) {
6767                view.clearAccessibilityFocus();
6768            }
6769            view.setAccessibilityDelegate(null);
6770        }
6771
6772        private void removeDetachedView(View child, boolean animate) {
6773            child.setAccessibilityDelegate(null);
6774            AbsListView.this.removeDetachedView(child, animate);
6775        }
6776    }
6777
6778    /**
6779     * Returns the height of the view for the specified position.
6780     *
6781     * @param position the item position
6782     * @return view height in pixels
6783     */
6784    int getHeightForPosition(int position) {
6785        final int firstVisiblePosition = getFirstVisiblePosition();
6786        final int childCount = getChildCount();
6787        final int index = position - firstVisiblePosition;
6788        if (index >= 0 && index < childCount) {
6789            // Position is on-screen, use existing view.
6790            final View view = getChildAt(index);
6791            return view.getHeight();
6792        } else {
6793            // Position is off-screen, obtain & recycle view.
6794            final View view = obtainView(position, mIsScrap);
6795            view.measure(mWidthMeasureSpec, MeasureSpec.UNSPECIFIED);
6796            final int height = view.getMeasuredHeight();
6797            mRecycler.addScrapView(view, position);
6798            return height;
6799        }
6800    }
6801
6802    /**
6803     * Sets the selected item and positions the selection y pixels from the top edge
6804     * of the ListView. (If in touch mode, the item will not be selected but it will
6805     * still be positioned appropriately.)
6806     *
6807     * @param position Index (starting at 0) of the data item to be selected.
6808     * @param y The distance from the top edge of the ListView (plus padding) that the
6809     *        item will be positioned.
6810     */
6811    public void setSelectionFromTop(int position, int y) {
6812        if (mAdapter == null) {
6813            return;
6814        }
6815
6816        if (!isInTouchMode()) {
6817            position = lookForSelectablePosition(position, true);
6818            if (position >= 0) {
6819                setNextSelectedPositionInt(position);
6820            }
6821        } else {
6822            mResurrectToPosition = position;
6823        }
6824
6825        if (position >= 0) {
6826            mLayoutMode = LAYOUT_SPECIFIC;
6827            mSpecificTop = mListPadding.top + y;
6828
6829            if (mNeedSync) {
6830                mSyncPosition = position;
6831                mSyncRowId = mAdapter.getItemId(position);
6832            }
6833
6834            if (mPositionScroller != null) {
6835                mPositionScroller.stop();
6836            }
6837            requestLayout();
6838        }
6839    }
6840
6841    /**
6842     * Abstract positon scroller used to handle smooth scrolling.
6843     */
6844    static abstract class AbsPositionScroller {
6845        public abstract void start(int position);
6846        public abstract void start(int position, int boundPosition);
6847        public abstract void startWithOffset(int position, int offset);
6848        public abstract void startWithOffset(int position, int offset, int duration);
6849        public abstract void stop();
6850    }
6851
6852    /**
6853     * Default position scroller that simulates a fling.
6854     */
6855    class PositionScroller extends AbsPositionScroller implements Runnable {
6856        private static final int SCROLL_DURATION = 200;
6857
6858        private static final int MOVE_DOWN_POS = 1;
6859        private static final int MOVE_UP_POS = 2;
6860        private static final int MOVE_DOWN_BOUND = 3;
6861        private static final int MOVE_UP_BOUND = 4;
6862        private static final int MOVE_OFFSET = 5;
6863
6864        private int mMode;
6865        private int mTargetPos;
6866        private int mBoundPos;
6867        private int mLastSeenPos;
6868        private int mScrollDuration;
6869        private final int mExtraScroll;
6870
6871        private int mOffsetFromTop;
6872
6873        PositionScroller() {
6874            mExtraScroll = ViewConfiguration.get(mContext).getScaledFadingEdgeLength();
6875        }
6876
6877        @Override
6878        public void start(final int position) {
6879            stop();
6880
6881            if (mDataChanged) {
6882                // Wait until we're back in a stable state to try this.
6883                mPositionScrollAfterLayout = new Runnable() {
6884                    @Override public void run() {
6885                        start(position);
6886                    }
6887                };
6888                return;
6889            }
6890
6891            final int childCount = getChildCount();
6892            if (childCount == 0) {
6893                // Can't scroll without children.
6894                return;
6895            }
6896
6897            final int firstPos = mFirstPosition;
6898            final int lastPos = firstPos + childCount - 1;
6899
6900            int viewTravelCount;
6901            int clampedPosition = Math.max(0, Math.min(getCount() - 1, position));
6902            if (clampedPosition < firstPos) {
6903                viewTravelCount = firstPos - clampedPosition + 1;
6904                mMode = MOVE_UP_POS;
6905            } else if (clampedPosition > lastPos) {
6906                viewTravelCount = clampedPosition - lastPos + 1;
6907                mMode = MOVE_DOWN_POS;
6908            } else {
6909                scrollToVisible(clampedPosition, INVALID_POSITION, SCROLL_DURATION);
6910                return;
6911            }
6912
6913            if (viewTravelCount > 0) {
6914                mScrollDuration = SCROLL_DURATION / viewTravelCount;
6915            } else {
6916                mScrollDuration = SCROLL_DURATION;
6917            }
6918            mTargetPos = clampedPosition;
6919            mBoundPos = INVALID_POSITION;
6920            mLastSeenPos = INVALID_POSITION;
6921
6922            postOnAnimation(this);
6923        }
6924
6925        @Override
6926        public void start(final int position, final int boundPosition) {
6927            stop();
6928
6929            if (boundPosition == INVALID_POSITION) {
6930                start(position);
6931                return;
6932            }
6933
6934            if (mDataChanged) {
6935                // Wait until we're back in a stable state to try this.
6936                mPositionScrollAfterLayout = new Runnable() {
6937                    @Override public void run() {
6938                        start(position, boundPosition);
6939                    }
6940                };
6941                return;
6942            }
6943
6944            final int childCount = getChildCount();
6945            if (childCount == 0) {
6946                // Can't scroll without children.
6947                return;
6948            }
6949
6950            final int firstPos = mFirstPosition;
6951            final int lastPos = firstPos + childCount - 1;
6952
6953            int viewTravelCount;
6954            int clampedPosition = Math.max(0, Math.min(getCount() - 1, position));
6955            if (clampedPosition < firstPos) {
6956                final int boundPosFromLast = lastPos - boundPosition;
6957                if (boundPosFromLast < 1) {
6958                    // Moving would shift our bound position off the screen. Abort.
6959                    return;
6960                }
6961
6962                final int posTravel = firstPos - clampedPosition + 1;
6963                final int boundTravel = boundPosFromLast - 1;
6964                if (boundTravel < posTravel) {
6965                    viewTravelCount = boundTravel;
6966                    mMode = MOVE_UP_BOUND;
6967                } else {
6968                    viewTravelCount = posTravel;
6969                    mMode = MOVE_UP_POS;
6970                }
6971            } else if (clampedPosition > lastPos) {
6972                final int boundPosFromFirst = boundPosition - firstPos;
6973                if (boundPosFromFirst < 1) {
6974                    // Moving would shift our bound position off the screen. Abort.
6975                    return;
6976                }
6977
6978                final int posTravel = clampedPosition - lastPos + 1;
6979                final int boundTravel = boundPosFromFirst - 1;
6980                if (boundTravel < posTravel) {
6981                    viewTravelCount = boundTravel;
6982                    mMode = MOVE_DOWN_BOUND;
6983                } else {
6984                    viewTravelCount = posTravel;
6985                    mMode = MOVE_DOWN_POS;
6986                }
6987            } else {
6988                scrollToVisible(clampedPosition, boundPosition, SCROLL_DURATION);
6989                return;
6990            }
6991
6992            if (viewTravelCount > 0) {
6993                mScrollDuration = SCROLL_DURATION / viewTravelCount;
6994            } else {
6995                mScrollDuration = SCROLL_DURATION;
6996            }
6997            mTargetPos = clampedPosition;
6998            mBoundPos = boundPosition;
6999            mLastSeenPos = INVALID_POSITION;
7000
7001            postOnAnimation(this);
7002        }
7003
7004        @Override
7005        public void startWithOffset(int position, int offset) {
7006            startWithOffset(position, offset, SCROLL_DURATION);
7007        }
7008
7009        @Override
7010        public void startWithOffset(final int position, int offset, final int duration) {
7011            stop();
7012
7013            if (mDataChanged) {
7014                // Wait until we're back in a stable state to try this.
7015                final int postOffset = offset;
7016                mPositionScrollAfterLayout = new Runnable() {
7017                    @Override public void run() {
7018                        startWithOffset(position, postOffset, duration);
7019                    }
7020                };
7021                return;
7022            }
7023
7024            final int childCount = getChildCount();
7025            if (childCount == 0) {
7026                // Can't scroll without children.
7027                return;
7028            }
7029
7030            offset += getPaddingTop();
7031
7032            mTargetPos = Math.max(0, Math.min(getCount() - 1, position));
7033            mOffsetFromTop = offset;
7034            mBoundPos = INVALID_POSITION;
7035            mLastSeenPos = INVALID_POSITION;
7036            mMode = MOVE_OFFSET;
7037
7038            final int firstPos = mFirstPosition;
7039            final int lastPos = firstPos + childCount - 1;
7040
7041            int viewTravelCount;
7042            if (mTargetPos < firstPos) {
7043                viewTravelCount = firstPos - mTargetPos;
7044            } else if (mTargetPos > lastPos) {
7045                viewTravelCount = mTargetPos - lastPos;
7046            } else {
7047                // On-screen, just scroll.
7048                final int targetTop = getChildAt(mTargetPos - firstPos).getTop();
7049                smoothScrollBy(targetTop - offset, duration, true);
7050                return;
7051            }
7052
7053            // Estimate how many screens we should travel
7054            final float screenTravelCount = (float) viewTravelCount / childCount;
7055            mScrollDuration = screenTravelCount < 1 ?
7056                    duration : (int) (duration / screenTravelCount);
7057            mLastSeenPos = INVALID_POSITION;
7058
7059            postOnAnimation(this);
7060        }
7061
7062        /**
7063         * Scroll such that targetPos is in the visible padded region without scrolling
7064         * boundPos out of view. Assumes targetPos is onscreen.
7065         */
7066        private void scrollToVisible(int targetPos, int boundPos, int duration) {
7067            final int firstPos = mFirstPosition;
7068            final int childCount = getChildCount();
7069            final int lastPos = firstPos + childCount - 1;
7070            final int paddedTop = mListPadding.top;
7071            final int paddedBottom = getHeight() - mListPadding.bottom;
7072
7073            if (targetPos < firstPos || targetPos > lastPos) {
7074                Log.w(TAG, "scrollToVisible called with targetPos " + targetPos +
7075                        " not visible [" + firstPos + ", " + lastPos + "]");
7076            }
7077            if (boundPos < firstPos || boundPos > lastPos) {
7078                // boundPos doesn't matter, it's already offscreen.
7079                boundPos = INVALID_POSITION;
7080            }
7081
7082            final View targetChild = getChildAt(targetPos - firstPos);
7083            final int targetTop = targetChild.getTop();
7084            final int targetBottom = targetChild.getBottom();
7085            int scrollBy = 0;
7086
7087            if (targetBottom > paddedBottom) {
7088                scrollBy = targetBottom - paddedBottom;
7089            }
7090            if (targetTop < paddedTop) {
7091                scrollBy = targetTop - paddedTop;
7092            }
7093
7094            if (scrollBy == 0) {
7095                return;
7096            }
7097
7098            if (boundPos >= 0) {
7099                final View boundChild = getChildAt(boundPos - firstPos);
7100                final int boundTop = boundChild.getTop();
7101                final int boundBottom = boundChild.getBottom();
7102                final int absScroll = Math.abs(scrollBy);
7103
7104                if (scrollBy < 0 && boundBottom + absScroll > paddedBottom) {
7105                    // Don't scroll the bound view off the bottom of the screen.
7106                    scrollBy = Math.max(0, boundBottom - paddedBottom);
7107                } else if (scrollBy > 0 && boundTop - absScroll < paddedTop) {
7108                    // Don't scroll the bound view off the top of the screen.
7109                    scrollBy = Math.min(0, boundTop - paddedTop);
7110                }
7111            }
7112
7113            smoothScrollBy(scrollBy, duration);
7114        }
7115
7116        @Override
7117        public void stop() {
7118            removeCallbacks(this);
7119        }
7120
7121        @Override
7122        public void run() {
7123            final int listHeight = getHeight();
7124            final int firstPos = mFirstPosition;
7125
7126            switch (mMode) {
7127            case MOVE_DOWN_POS: {
7128                final int lastViewIndex = getChildCount() - 1;
7129                final int lastPos = firstPos + lastViewIndex;
7130
7131                if (lastViewIndex < 0) {
7132                    return;
7133                }
7134
7135                if (lastPos == mLastSeenPos) {
7136                    // No new views, let things keep going.
7137                    postOnAnimation(this);
7138                    return;
7139                }
7140
7141                final View lastView = getChildAt(lastViewIndex);
7142                final int lastViewHeight = lastView.getHeight();
7143                final int lastViewTop = lastView.getTop();
7144                final int lastViewPixelsShowing = listHeight - lastViewTop;
7145                final int extraScroll = lastPos < mItemCount - 1 ?
7146                        Math.max(mListPadding.bottom, mExtraScroll) : mListPadding.bottom;
7147
7148                final int scrollBy = lastViewHeight - lastViewPixelsShowing + extraScroll;
7149                smoothScrollBy(scrollBy, mScrollDuration, true);
7150
7151                mLastSeenPos = lastPos;
7152                if (lastPos < mTargetPos) {
7153                    postOnAnimation(this);
7154                }
7155                break;
7156            }
7157
7158            case MOVE_DOWN_BOUND: {
7159                final int nextViewIndex = 1;
7160                final int childCount = getChildCount();
7161
7162                if (firstPos == mBoundPos || childCount <= nextViewIndex
7163                        || firstPos + childCount >= mItemCount) {
7164                    return;
7165                }
7166                final int nextPos = firstPos + nextViewIndex;
7167
7168                if (nextPos == mLastSeenPos) {
7169                    // No new views, let things keep going.
7170                    postOnAnimation(this);
7171                    return;
7172                }
7173
7174                final View nextView = getChildAt(nextViewIndex);
7175                final int nextViewHeight = nextView.getHeight();
7176                final int nextViewTop = nextView.getTop();
7177                final int extraScroll = Math.max(mListPadding.bottom, mExtraScroll);
7178                if (nextPos < mBoundPos) {
7179                    smoothScrollBy(Math.max(0, nextViewHeight + nextViewTop - extraScroll),
7180                            mScrollDuration, true);
7181
7182                    mLastSeenPos = nextPos;
7183
7184                    postOnAnimation(this);
7185                } else  {
7186                    if (nextViewTop > extraScroll) {
7187                        smoothScrollBy(nextViewTop - extraScroll, mScrollDuration, true);
7188                    }
7189                }
7190                break;
7191            }
7192
7193            case MOVE_UP_POS: {
7194                if (firstPos == mLastSeenPos) {
7195                    // No new views, let things keep going.
7196                    postOnAnimation(this);
7197                    return;
7198                }
7199
7200                final View firstView = getChildAt(0);
7201                if (firstView == null) {
7202                    return;
7203                }
7204                final int firstViewTop = firstView.getTop();
7205                final int extraScroll = firstPos > 0 ?
7206                        Math.max(mExtraScroll, mListPadding.top) : mListPadding.top;
7207
7208                smoothScrollBy(firstViewTop - extraScroll, mScrollDuration, true);
7209
7210                mLastSeenPos = firstPos;
7211
7212                if (firstPos > mTargetPos) {
7213                    postOnAnimation(this);
7214                }
7215                break;
7216            }
7217
7218            case MOVE_UP_BOUND: {
7219                final int lastViewIndex = getChildCount() - 2;
7220                if (lastViewIndex < 0) {
7221                    return;
7222                }
7223                final int lastPos = firstPos + lastViewIndex;
7224
7225                if (lastPos == mLastSeenPos) {
7226                    // No new views, let things keep going.
7227                    postOnAnimation(this);
7228                    return;
7229                }
7230
7231                final View lastView = getChildAt(lastViewIndex);
7232                final int lastViewHeight = lastView.getHeight();
7233                final int lastViewTop = lastView.getTop();
7234                final int lastViewPixelsShowing = listHeight - lastViewTop;
7235                final int extraScroll = Math.max(mListPadding.top, mExtraScroll);
7236                mLastSeenPos = lastPos;
7237                if (lastPos > mBoundPos) {
7238                    smoothScrollBy(-(lastViewPixelsShowing - extraScroll), mScrollDuration, true);
7239                    postOnAnimation(this);
7240                } else {
7241                    final int bottom = listHeight - extraScroll;
7242                    final int lastViewBottom = lastViewTop + lastViewHeight;
7243                    if (bottom > lastViewBottom) {
7244                        smoothScrollBy(-(bottom - lastViewBottom), mScrollDuration, true);
7245                    }
7246                }
7247                break;
7248            }
7249
7250            case MOVE_OFFSET: {
7251                if (mLastSeenPos == firstPos) {
7252                    // No new views, let things keep going.
7253                    postOnAnimation(this);
7254                    return;
7255                }
7256
7257                mLastSeenPos = firstPos;
7258
7259                final int childCount = getChildCount();
7260                final int position = mTargetPos;
7261                final int lastPos = firstPos + childCount - 1;
7262
7263                int viewTravelCount = 0;
7264                if (position < firstPos) {
7265                    viewTravelCount = firstPos - position + 1;
7266                } else if (position > lastPos) {
7267                    viewTravelCount = position - lastPos;
7268                }
7269
7270                // Estimate how many screens we should travel
7271                final float screenTravelCount = (float) viewTravelCount / childCount;
7272
7273                final float modifier = Math.min(Math.abs(screenTravelCount), 1.f);
7274                if (position < firstPos) {
7275                    final int distance = (int) (-getHeight() * modifier);
7276                    final int duration = (int) (mScrollDuration * modifier);
7277                    smoothScrollBy(distance, duration, true);
7278                    postOnAnimation(this);
7279                } else if (position > lastPos) {
7280                    final int distance = (int) (getHeight() * modifier);
7281                    final int duration = (int) (mScrollDuration * modifier);
7282                    smoothScrollBy(distance, duration, true);
7283                    postOnAnimation(this);
7284                } else {
7285                    // On-screen, just scroll.
7286                    final int targetTop = getChildAt(position - firstPos).getTop();
7287                    final int distance = targetTop - mOffsetFromTop;
7288                    final int duration = (int) (mScrollDuration *
7289                            ((float) Math.abs(distance) / getHeight()));
7290                    smoothScrollBy(distance, duration, true);
7291                }
7292                break;
7293            }
7294
7295            default:
7296                break;
7297            }
7298        }
7299    }
7300}
7301