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