ViewGroup.java revision 3ed259a43cd6532c9cf835c925fc89c2f4f75bc1
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.view;
18
19import android.animation.LayoutTransition;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.content.res.TypedArray;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Color;
26import android.graphics.Insets;
27import android.graphics.Matrix;
28import android.graphics.Paint;
29import android.graphics.PointF;
30import android.graphics.Rect;
31import android.graphics.RectF;
32import android.graphics.Region;
33import android.os.Build;
34import android.os.Parcelable;
35import android.os.SystemClock;
36import android.util.AttributeSet;
37import android.util.Log;
38import android.util.SparseArray;
39import android.view.accessibility.AccessibilityEvent;
40import android.view.accessibility.AccessibilityNodeInfo;
41import android.view.animation.Animation;
42import android.view.animation.AnimationUtils;
43import android.view.animation.LayoutAnimationController;
44import android.view.animation.Transformation;
45
46import com.android.internal.R;
47import com.android.internal.util.Predicate;
48
49import java.util.ArrayList;
50import java.util.Collections;
51import java.util.HashSet;
52
53/**
54 * <p>
55 * A <code>ViewGroup</code> is a special view that can contain other views
56 * (called children.) The view group is the base class for layouts and views
57 * containers. This class also defines the
58 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
59 * class for layouts parameters.
60 * </p>
61 *
62 * <p>
63 * Also see {@link LayoutParams} for layout attributes.
64 * </p>
65 *
66 * <div class="special reference">
67 * <h3>Developer Guides</h3>
68 * <p>For more information about creating user interface layouts, read the
69 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
70 * guide.</p></div>
71 *
72 * @attr ref android.R.styleable#ViewGroup_clipChildren
73 * @attr ref android.R.styleable#ViewGroup_clipToPadding
74 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
75 * @attr ref android.R.styleable#ViewGroup_animationCache
76 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
77 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
78 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
79 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
80 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
81 */
82public abstract class ViewGroup extends View implements ViewParent, ViewManager {
83    private static final String TAG = "ViewGroup";
84
85    private static final boolean DBG = false;
86
87    /**
88     * Views which have been hidden or removed which need to be animated on
89     * their way out.
90     * This field should be made private, so it is hidden from the SDK.
91     * {@hide}
92     */
93    protected ArrayList<View> mDisappearingChildren;
94
95    /**
96     * Listener used to propagate events indicating when children are added
97     * and/or removed from a view group.
98     * This field should be made private, so it is hidden from the SDK.
99     * {@hide}
100     */
101    protected OnHierarchyChangeListener mOnHierarchyChangeListener;
102
103    // The view contained within this ViewGroup that has or contains focus.
104    private View mFocused;
105
106    /**
107     * A Transformation used when drawing children, to
108     * apply on the child being drawn.
109     */
110    final Transformation mChildTransformation = new Transformation();
111
112    /**
113     * Used to track the current invalidation region.
114     */
115    RectF mInvalidateRegion;
116
117    /**
118     * A Transformation used to calculate a correct
119     * invalidation area when the application is autoscaled.
120     */
121    Transformation mInvalidationTransformation;
122
123    // View currently under an ongoing drag
124    private View mCurrentDragView;
125
126    // Metadata about the ongoing drag
127    private DragEvent mCurrentDrag;
128    private HashSet<View> mDragNotifiedChildren;
129
130    // Does this group have a child that can accept the current drag payload?
131    private boolean mChildAcceptsDrag;
132
133    // Used during drag dispatch
134    private final PointF mLocalPoint = new PointF();
135
136    // Layout animation
137    private LayoutAnimationController mLayoutAnimationController;
138    private Animation.AnimationListener mAnimationListener;
139
140    // First touch target in the linked list of touch targets.
141    private TouchTarget mFirstTouchTarget;
142
143    // For debugging only.  You can see these in hierarchyviewer.
144    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
145    @ViewDebug.ExportedProperty(category = "events")
146    private long mLastTouchDownTime;
147    @ViewDebug.ExportedProperty(category = "events")
148    private int mLastTouchDownIndex = -1;
149    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
150    @ViewDebug.ExportedProperty(category = "events")
151    private float mLastTouchDownX;
152    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
153    @ViewDebug.ExportedProperty(category = "events")
154    private float mLastTouchDownY;
155
156    // First hover target in the linked list of hover targets.
157    // The hover targets are children which have received ACTION_HOVER_ENTER.
158    // They might not have actually handled the hover event, but we will
159    // continue sending hover events to them as long as the pointer remains over
160    // their bounds and the view group does not intercept hover.
161    private HoverTarget mFirstHoverTarget;
162
163    // True if the view group itself received a hover event.
164    // It might not have actually handled the hover event.
165    private boolean mHoveredSelf;
166
167    /**
168     * Internal flags.
169     *
170     * This field should be made private, so it is hidden from the SDK.
171     * {@hide}
172     */
173    protected int mGroupFlags;
174
175    /*
176     * The layout mode: either {@link #CLIP_BOUNDS} or {@link #OPTICAL_BOUNDS}
177     */
178    private int mLayoutMode = CLIP_BOUNDS;
179
180    /**
181     * NOTE: If you change the flags below make sure to reflect the changes
182     *       the DisplayList class
183     */
184
185    // When set, ViewGroup invalidates only the child's rectangle
186    // Set by default
187    static final int FLAG_CLIP_CHILDREN = 0x1;
188
189    // When set, ViewGroup excludes the padding area from the invalidate rectangle
190    // Set by default
191    private static final int FLAG_CLIP_TO_PADDING = 0x2;
192
193    // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
194    // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
195    static final int FLAG_INVALIDATE_REQUIRED  = 0x4;
196
197    // When set, dispatchDraw() will run the layout animation and unset the flag
198    private static final int FLAG_RUN_ANIMATION = 0x8;
199
200    // When set, there is either no layout animation on the ViewGroup or the layout
201    // animation is over
202    // Set by default
203    static final int FLAG_ANIMATION_DONE = 0x10;
204
205    // If set, this ViewGroup has padding; if unset there is no padding and we don't need
206    // to clip it, even if FLAG_CLIP_TO_PADDING is set
207    private static final int FLAG_PADDING_NOT_NULL = 0x20;
208
209    // When set, this ViewGroup caches its children in a Bitmap before starting a layout animation
210    // Set by default
211    private static final int FLAG_ANIMATION_CACHE = 0x40;
212
213    // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
214    // layout animation; this avoid clobbering the hierarchy
215    // Automatically set when the layout animation starts, depending on the animation's
216    // characteristics
217    static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
218
219    // When set, the next call to drawChild() will clear mChildTransformation's matrix
220    static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
221
222    // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
223    // the children's Bitmap caches if necessary
224    // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
225    private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
226
227    /**
228     * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
229     * to get the index of the child to draw for that iteration.
230     *
231     * @hide
232     */
233    protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
234
235    /**
236     * When set, this ViewGroup supports static transformations on children; this causes
237     * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
238     * invoked when a child is drawn.
239     *
240     * Any subclass overriding
241     * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
242     * set this flags in {@link #mGroupFlags}.
243     *
244     * {@hide}
245     */
246    protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
247
248    // When the previous drawChild() invocation used an alpha value that was lower than
249    // 1.0 and set it in mCachePaint
250    static final int FLAG_ALPHA_LOWER_THAN_ONE = 0x1000;
251
252    /**
253     * When set, this ViewGroup's drawable states also include those
254     * of its children.
255     */
256    private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
257
258    /**
259     * When set, this ViewGroup tries to always draw its children using their drawing cache.
260     */
261    static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
262
263    /**
264     * When set, and if FLAG_ALWAYS_DRAWN_WITH_CACHE is not set, this ViewGroup will try to
265     * draw its children with their drawing cache.
266     */
267    static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
268
269    /**
270     * When set, this group will go through its list of children to notify them of
271     * any drawable state change.
272     */
273    private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
274
275    private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
276
277    /**
278     * This view will get focus before any of its descendants.
279     */
280    public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
281
282    /**
283     * This view will get focus only if none of its descendants want it.
284     */
285    public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
286
287    /**
288     * This view will block any of its descendants from getting focus, even
289     * if they are focusable.
290     */
291    public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
292
293    /**
294     * Used to map between enum in attrubutes and flag values.
295     */
296    private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
297            {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
298                    FOCUS_BLOCK_DESCENDANTS};
299
300    /**
301     * When set, this ViewGroup should not intercept touch events.
302     * {@hide}
303     */
304    protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
305
306    /**
307     * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
308     */
309    private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
310
311    /**
312     * When set, this ViewGroup will not dispatch onAttachedToWindow calls
313     * to children when adding new views. This is used to prevent multiple
314     * onAttached calls when a ViewGroup adds children in its own onAttached method.
315     */
316    private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000;
317
318    /**
319     * Indicates which types of drawing caches are to be kept in memory.
320     * This field should be made private, so it is hidden from the SDK.
321     * {@hide}
322     */
323    protected int mPersistentDrawingCache;
324
325    /**
326     * Used to indicate that no drawing cache should be kept in memory.
327     */
328    public static final int PERSISTENT_NO_CACHE = 0x0;
329
330    /**
331     * Used to indicate that the animation drawing cache should be kept in memory.
332     */
333    public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
334
335    /**
336     * Used to indicate that the scrolling drawing cache should be kept in memory.
337     */
338    public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
339
340    /**
341     * Used to indicate that all drawing caches should be kept in memory.
342     */
343    public static final int PERSISTENT_ALL_CACHES = 0x3;
344
345    // Layout Modes
346
347    /**
348     * This constant is a {@link #setLayoutMode(int) layoutMode}.
349     * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
350     * {@link #getRight() right} and {@link #getBottom() bottom}.
351     *
352     * @hide
353     */
354    public static final int CLIP_BOUNDS = 0;
355
356    /**
357     * This constant is a {@link #setLayoutMode(int) layoutMode}.
358     * Optical bounds describe where a widget appears to be. They sit inside the clip
359     * bounds which need to cover a larger area to allow other effects,
360     * such as shadows and glows, to be drawn.
361     *
362     * @hide
363     */
364    public static final int OPTICAL_BOUNDS = 1;
365
366    /**
367     * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
368     * are set at the same time.
369     */
370    protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
371
372    // Index of the child's left position in the mLocation array
373    private static final int CHILD_LEFT_INDEX = 0;
374    // Index of the child's top position in the mLocation array
375    private static final int CHILD_TOP_INDEX = 1;
376
377    // Child views of this ViewGroup
378    private View[] mChildren;
379    // Number of valid children in the mChildren array, the rest should be null or not
380    // considered as children
381
382    private boolean mLayoutSuppressed = false;
383
384    private int mChildrenCount;
385
386    private static final int ARRAY_INITIAL_CAPACITY = 12;
387    private static final int ARRAY_CAPACITY_INCREMENT = 12;
388
389    private static Paint sDebugPaint;
390    private static float[] sDebugLines;
391
392    // Used to draw cached views
393    Paint mCachePaint;
394
395    // Used to animate add/remove changes in layout
396    private LayoutTransition mTransition;
397
398    // The set of views that are currently being transitioned. This list is used to track views
399    // being removed that should not actually be removed from the parent yet because they are
400    // being animated.
401    private ArrayList<View> mTransitioningViews;
402
403    // List of children changing visibility. This is used to potentially keep rendering
404    // views during a transition when they otherwise would have become gone/invisible
405    private ArrayList<View> mVisibilityChangingChildren;
406
407    // Indicates whether this container will use its children layers to draw
408    @ViewDebug.ExportedProperty(category = "drawing")
409    boolean mDrawLayers = true;
410
411    // Indicates how many of this container's child subtrees contain transient state
412    @ViewDebug.ExportedProperty(category = "layout")
413    private int mChildCountWithTransientState = 0;
414
415    public ViewGroup(Context context) {
416        super(context);
417        initViewGroup();
418    }
419
420    public ViewGroup(Context context, AttributeSet attrs) {
421        super(context, attrs);
422        initViewGroup();
423        initFromAttributes(context, attrs);
424    }
425
426    public ViewGroup(Context context, AttributeSet attrs, int defStyle) {
427        super(context, attrs, defStyle);
428        initViewGroup();
429        initFromAttributes(context, attrs);
430    }
431
432    private boolean debugDraw() {
433        return mAttachInfo != null && mAttachInfo.mDebugLayout;
434    }
435
436    private void initViewGroup() {
437        // ViewGroup doesn't draw by default
438        if (!debugDraw()) {
439            setFlags(WILL_NOT_DRAW, DRAW_MASK);
440        }
441        mGroupFlags |= FLAG_CLIP_CHILDREN;
442        mGroupFlags |= FLAG_CLIP_TO_PADDING;
443        mGroupFlags |= FLAG_ANIMATION_DONE;
444        mGroupFlags |= FLAG_ANIMATION_CACHE;
445        mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
446
447        if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
448            mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
449        }
450
451        setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
452
453        mChildren = new View[ARRAY_INITIAL_CAPACITY];
454        mChildrenCount = 0;
455
456        mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
457    }
458
459    private void initFromAttributes(Context context, AttributeSet attrs) {
460        TypedArray a = context.obtainStyledAttributes(attrs,
461                R.styleable.ViewGroup);
462
463        final int N = a.getIndexCount();
464        for (int i = 0; i < N; i++) {
465            int attr = a.getIndex(i);
466            switch (attr) {
467                case R.styleable.ViewGroup_clipChildren:
468                    setClipChildren(a.getBoolean(attr, true));
469                    break;
470                case R.styleable.ViewGroup_clipToPadding:
471                    setClipToPadding(a.getBoolean(attr, true));
472                    break;
473                case R.styleable.ViewGroup_animationCache:
474                    setAnimationCacheEnabled(a.getBoolean(attr, true));
475                    break;
476                case R.styleable.ViewGroup_persistentDrawingCache:
477                    setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
478                    break;
479                case R.styleable.ViewGroup_addStatesFromChildren:
480                    setAddStatesFromChildren(a.getBoolean(attr, false));
481                    break;
482                case R.styleable.ViewGroup_alwaysDrawnWithCache:
483                    setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
484                    break;
485                case R.styleable.ViewGroup_layoutAnimation:
486                    int id = a.getResourceId(attr, -1);
487                    if (id > 0) {
488                        setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
489                    }
490                    break;
491                case R.styleable.ViewGroup_descendantFocusability:
492                    setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
493                    break;
494                case R.styleable.ViewGroup_splitMotionEvents:
495                    setMotionEventSplittingEnabled(a.getBoolean(attr, false));
496                    break;
497                case R.styleable.ViewGroup_animateLayoutChanges:
498                    boolean animateLayoutChanges = a.getBoolean(attr, false);
499                    if (animateLayoutChanges) {
500                        setLayoutTransition(new LayoutTransition());
501                    }
502                    break;
503            }
504        }
505
506        a.recycle();
507    }
508
509    /**
510     * Gets the descendant focusability of this view group.  The descendant
511     * focusability defines the relationship between this view group and its
512     * descendants when looking for a view to take focus in
513     * {@link #requestFocus(int, android.graphics.Rect)}.
514     *
515     * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
516     *   {@link #FOCUS_BLOCK_DESCENDANTS}.
517     */
518    @ViewDebug.ExportedProperty(category = "focus", mapping = {
519        @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
520        @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
521        @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
522    })
523    public int getDescendantFocusability() {
524        return mGroupFlags & FLAG_MASK_FOCUSABILITY;
525    }
526
527    /**
528     * Set the descendant focusability of this view group. This defines the relationship
529     * between this view group and its descendants when looking for a view to
530     * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
531     *
532     * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
533     *   {@link #FOCUS_BLOCK_DESCENDANTS}.
534     */
535    public void setDescendantFocusability(int focusability) {
536        switch (focusability) {
537            case FOCUS_BEFORE_DESCENDANTS:
538            case FOCUS_AFTER_DESCENDANTS:
539            case FOCUS_BLOCK_DESCENDANTS:
540                break;
541            default:
542                throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
543                        + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
544        }
545        mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
546        mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
547    }
548
549    /**
550     * {@inheritDoc}
551     */
552    @Override
553    void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
554        if (mFocused != null) {
555            mFocused.unFocus();
556            mFocused = null;
557        }
558        super.handleFocusGainInternal(direction, previouslyFocusedRect);
559    }
560
561    /**
562     * {@inheritDoc}
563     */
564    public void requestChildFocus(View child, View focused) {
565        if (DBG) {
566            System.out.println(this + " requestChildFocus()");
567        }
568        if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
569            return;
570        }
571
572        // Unfocus us, if necessary
573        super.unFocus();
574
575        // We had a previous notion of who had focus. Clear it.
576        if (mFocused != child) {
577            if (mFocused != null) {
578                mFocused.unFocus();
579            }
580
581            mFocused = child;
582        }
583        if (mParent != null) {
584            mParent.requestChildFocus(this, focused);
585        }
586    }
587
588    /**
589     * {@inheritDoc}
590     */
591    public void focusableViewAvailable(View v) {
592        if (mParent != null
593                // shortcut: don't report a new focusable view if we block our descendants from
594                // getting focus
595                && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS)
596                // shortcut: don't report a new focusable view if we already are focused
597                // (and we don't prefer our descendants)
598                //
599                // note: knowing that mFocused is non-null is not a good enough reason
600                // to break the traversal since in that case we'd actually have to find
601                // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and
602                // an ancestor of v; this will get checked for at ViewAncestor
603                && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) {
604            mParent.focusableViewAvailable(v);
605        }
606    }
607
608    /**
609     * {@inheritDoc}
610     */
611    public boolean showContextMenuForChild(View originalView) {
612        return mParent != null && mParent.showContextMenuForChild(originalView);
613    }
614
615    /**
616     * {@inheritDoc}
617     */
618    public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
619        return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null;
620    }
621
622    /**
623     * Find the nearest view in the specified direction that wants to take
624     * focus.
625     *
626     * @param focused The view that currently has focus
627     * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
628     *        FOCUS_RIGHT, or 0 for not applicable.
629     */
630    public View focusSearch(View focused, int direction) {
631        if (isRootNamespace()) {
632            // root namespace means we should consider ourselves the top of the
633            // tree for focus searching; otherwise we could be focus searching
634            // into other tabs.  see LocalActivityManager and TabHost for more info
635            return FocusFinder.getInstance().findNextFocus(this, focused, direction);
636        } else if (mParent != null) {
637            return mParent.focusSearch(focused, direction);
638        }
639        return null;
640    }
641
642    /**
643     * {@inheritDoc}
644     */
645    public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
646        return false;
647    }
648
649    /**
650     * {@inheritDoc}
651     */
652    @Override
653    public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
654        ViewParent parent = mParent;
655        if (parent == null) {
656            return false;
657        }
658        final boolean propagate = onRequestSendAccessibilityEvent(child, event);
659        if (!propagate) {
660            return false;
661        }
662        return parent.requestSendAccessibilityEvent(this, event);
663    }
664
665    /**
666     * Called when a child has requested sending an {@link AccessibilityEvent} and
667     * gives an opportunity to its parent to augment the event.
668     * <p>
669     * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling
670     * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its
671     * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)}
672     * is responsible for handling this call.
673     * </p>
674     *
675     * @param child The child which requests sending the event.
676     * @param event The event to be sent.
677     * @return True if the event should be sent.
678     *
679     * @see #requestSendAccessibilityEvent(View, AccessibilityEvent)
680     */
681    public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
682        if (mAccessibilityDelegate != null) {
683            return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event);
684        } else {
685            return onRequestSendAccessibilityEventInternal(child, event);
686        }
687    }
688
689    /**
690     * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent)
691     *
692     * Note: Called from the default {@link View.AccessibilityDelegate}.
693     */
694    boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
695        return true;
696    }
697
698    /**
699     * Called when a child view has changed whether or not it is tracking transient state.
700     *
701     * @hide
702     */
703    public void childHasTransientStateChanged(View child, boolean childHasTransientState) {
704        final boolean oldHasTransientState = hasTransientState();
705        if (childHasTransientState) {
706            mChildCountWithTransientState++;
707        } else {
708            mChildCountWithTransientState--;
709        }
710
711        final boolean newHasTransientState = hasTransientState();
712        if (mParent != null && oldHasTransientState != newHasTransientState) {
713            try {
714                mParent.childHasTransientStateChanged(this, newHasTransientState);
715            } catch (AbstractMethodError e) {
716                Log.e(TAG, mParent.getClass().getSimpleName() +
717                        " does not fully implement ViewParent", e);
718            }
719        }
720    }
721
722    /**
723     * @hide
724     */
725    @Override
726    public boolean hasTransientState() {
727        return mChildCountWithTransientState > 0 || super.hasTransientState();
728    }
729
730    /**
731     * {@inheritDoc}
732     */
733    @Override
734    public boolean dispatchUnhandledMove(View focused, int direction) {
735        return mFocused != null &&
736                mFocused.dispatchUnhandledMove(focused, direction);
737    }
738
739    /**
740     * {@inheritDoc}
741     */
742    public void clearChildFocus(View child) {
743        if (DBG) {
744            System.out.println(this + " clearChildFocus()");
745        }
746
747        mFocused = null;
748        if (mParent != null) {
749            mParent.clearChildFocus(this);
750        }
751    }
752
753    /**
754     * {@inheritDoc}
755     */
756    @Override
757    public void clearFocus() {
758        if (DBG) {
759            System.out.println(this + " clearFocus()");
760        }
761        if (mFocused == null) {
762            super.clearFocus();
763        } else {
764            mFocused.clearFocus();
765            mFocused = null;
766        }
767    }
768
769    /**
770     * {@inheritDoc}
771     */
772    @Override
773    void unFocus() {
774        if (DBG) {
775            System.out.println(this + " unFocus()");
776        }
777        if (mFocused == null) {
778            super.unFocus();
779        } else {
780            mFocused.unFocus();
781            mFocused = null;
782        }
783    }
784
785    /**
786     * Returns the focused child of this view, if any. The child may have focus
787     * or contain focus.
788     *
789     * @return the focused child or null.
790     */
791    public View getFocusedChild() {
792        return mFocused;
793    }
794
795    /**
796     * Returns true if this view has or contains focus
797     *
798     * @return true if this view has or contains focus
799     */
800    @Override
801    public boolean hasFocus() {
802        return (mPrivateFlags & FOCUSED) != 0 || mFocused != null;
803    }
804
805    /*
806     * (non-Javadoc)
807     *
808     * @see android.view.View#findFocus()
809     */
810    @Override
811    public View findFocus() {
812        if (DBG) {
813            System.out.println("Find focus in " + this + ": flags="
814                    + isFocused() + ", child=" + mFocused);
815        }
816
817        if (isFocused()) {
818            return this;
819        }
820
821        if (mFocused != null) {
822            return mFocused.findFocus();
823        }
824        return null;
825    }
826
827    /**
828     * {@inheritDoc}
829     */
830    @Override
831    public boolean hasFocusable() {
832        if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
833            return false;
834        }
835
836        if (isFocusable()) {
837            return true;
838        }
839
840        final int descendantFocusability = getDescendantFocusability();
841        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
842            final int count = mChildrenCount;
843            final View[] children = mChildren;
844
845            for (int i = 0; i < count; i++) {
846                final View child = children[i];
847                if (child.hasFocusable()) {
848                    return true;
849                }
850            }
851        }
852
853        return false;
854    }
855
856    /**
857     * {@inheritDoc}
858     */
859    @Override
860    public void addFocusables(ArrayList<View> views, int direction) {
861        addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
862    }
863
864    /**
865     * {@inheritDoc}
866     */
867    @Override
868    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
869        final int focusableCount = views.size();
870
871        final int descendantFocusability = getDescendantFocusability();
872
873        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
874            final int count = mChildrenCount;
875            final View[] children = mChildren;
876
877            for (int i = 0; i < count; i++) {
878                final View child = children[i];
879                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
880                    child.addFocusables(views, direction, focusableMode);
881                }
882            }
883        }
884
885        // we add ourselves (if focusable) in all cases except for when we are
886        // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable.  this is
887        // to avoid the focus search finding layouts when a more precise search
888        // among the focusable children would be more interesting.
889        if (
890            descendantFocusability != FOCUS_AFTER_DESCENDANTS ||
891                // No focusable descendants
892                (focusableCount == views.size())) {
893            super.addFocusables(views, direction, focusableMode);
894        }
895    }
896
897    @Override
898    public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
899        super.findViewsWithText(outViews, text, flags);
900        final int childrenCount = mChildrenCount;
901        final View[] children = mChildren;
902        for (int i = 0; i < childrenCount; i++) {
903            View child = children[i];
904            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
905                    && (child.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
906                child.findViewsWithText(outViews, text, flags);
907            }
908        }
909    }
910
911    @Override
912    View findViewByAccessibilityIdTraversal(int accessibilityId) {
913        View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId);
914        if (foundView != null) {
915            return foundView;
916        }
917        final int childrenCount = mChildrenCount;
918        final View[] children = mChildren;
919        for (int i = 0; i < childrenCount; i++) {
920            View child = children[i];
921            foundView = child.findViewByAccessibilityIdTraversal(accessibilityId);
922            if (foundView != null) {
923                return foundView;
924            }
925        }
926        return null;
927    }
928
929    /**
930     * {@inheritDoc}
931     */
932    @Override
933    public void dispatchWindowFocusChanged(boolean hasFocus) {
934        super.dispatchWindowFocusChanged(hasFocus);
935        final int count = mChildrenCount;
936        final View[] children = mChildren;
937        for (int i = 0; i < count; i++) {
938            children[i].dispatchWindowFocusChanged(hasFocus);
939        }
940    }
941
942    /**
943     * {@inheritDoc}
944     */
945    @Override
946    public void addTouchables(ArrayList<View> views) {
947        super.addTouchables(views);
948
949        final int count = mChildrenCount;
950        final View[] children = mChildren;
951
952        for (int i = 0; i < count; i++) {
953            final View child = children[i];
954            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
955                child.addTouchables(views);
956            }
957        }
958    }
959
960    /**
961     * @hide
962     */
963    @Override
964    public void makeOptionalFitsSystemWindows() {
965        super.makeOptionalFitsSystemWindows();
966        final int count = mChildrenCount;
967        final View[] children = mChildren;
968        for (int i = 0; i < count; i++) {
969            children[i].makeOptionalFitsSystemWindows();
970        }
971    }
972
973    /**
974     * {@inheritDoc}
975     */
976    @Override
977    public void dispatchDisplayHint(int hint) {
978        super.dispatchDisplayHint(hint);
979        final int count = mChildrenCount;
980        final View[] children = mChildren;
981        for (int i = 0; i < count; i++) {
982            children[i].dispatchDisplayHint(hint);
983        }
984    }
985
986    /**
987     * Called when a view's visibility has changed. Notify the parent to take any appropriate
988     * action.
989     *
990     * @param child The view whose visibility has changed
991     * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
992     * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
993     * @hide
994     */
995    protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
996        if (mTransition != null) {
997            if (newVisibility == VISIBLE) {
998                mTransition.showChild(this, child, oldVisibility);
999            } else {
1000                mTransition.hideChild(this, child, newVisibility);
1001                // Only track this on disappearing views - appearing views are already visible
1002                // and don't need special handling during drawChild()
1003                if (mVisibilityChangingChildren == null) {
1004                    mVisibilityChangingChildren = new ArrayList<View>();
1005                }
1006                mVisibilityChangingChildren.add(child);
1007                if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
1008                    addDisappearingView(child);
1009                }
1010            }
1011        }
1012
1013        // in all cases, for drags
1014        if (mCurrentDrag != null) {
1015            if (newVisibility == VISIBLE) {
1016                notifyChildOfDrag(child);
1017            }
1018        }
1019    }
1020
1021    /**
1022     * {@inheritDoc}
1023     */
1024    @Override
1025    protected void dispatchVisibilityChanged(View changedView, int visibility) {
1026        super.dispatchVisibilityChanged(changedView, visibility);
1027        final int count = mChildrenCount;
1028        final View[] children = mChildren;
1029        for (int i = 0; i < count; i++) {
1030            children[i].dispatchVisibilityChanged(changedView, visibility);
1031        }
1032    }
1033
1034    /**
1035     * {@inheritDoc}
1036     */
1037    @Override
1038    public void dispatchWindowVisibilityChanged(int visibility) {
1039        super.dispatchWindowVisibilityChanged(visibility);
1040        final int count = mChildrenCount;
1041        final View[] children = mChildren;
1042        for (int i = 0; i < count; i++) {
1043            children[i].dispatchWindowVisibilityChanged(visibility);
1044        }
1045    }
1046
1047    /**
1048     * {@inheritDoc}
1049     */
1050    @Override
1051    public void dispatchConfigurationChanged(Configuration newConfig) {
1052        super.dispatchConfigurationChanged(newConfig);
1053        final int count = mChildrenCount;
1054        final View[] children = mChildren;
1055        for (int i = 0; i < count; i++) {
1056            children[i].dispatchConfigurationChanged(newConfig);
1057        }
1058    }
1059
1060    /**
1061     * {@inheritDoc}
1062     */
1063    public void recomputeViewAttributes(View child) {
1064        if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1065            ViewParent parent = mParent;
1066            if (parent != null) parent.recomputeViewAttributes(this);
1067        }
1068    }
1069
1070    @Override
1071    void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1072        if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1073            super.dispatchCollectViewAttributes(attachInfo, visibility);
1074            final int count = mChildrenCount;
1075            final View[] children = mChildren;
1076            for (int i = 0; i < count; i++) {
1077                final View child = children[i];
1078                child.dispatchCollectViewAttributes(attachInfo,
1079                        visibility | (child.mViewFlags&VISIBILITY_MASK));
1080            }
1081        }
1082    }
1083
1084    /**
1085     * {@inheritDoc}
1086     */
1087    public void bringChildToFront(View child) {
1088        int index = indexOfChild(child);
1089        if (index >= 0) {
1090            removeFromArray(index);
1091            addInArray(child, mChildrenCount);
1092            child.mParent = this;
1093        }
1094    }
1095
1096    /**
1097     * {@inheritDoc}
1098     */
1099    // TODO: Write real docs
1100    @Override
1101    public boolean dispatchDragEvent(DragEvent event) {
1102        boolean retval = false;
1103        final float tx = event.mX;
1104        final float ty = event.mY;
1105
1106        ViewRootImpl root = getViewRootImpl();
1107
1108        // Dispatch down the view hierarchy
1109        switch (event.mAction) {
1110        case DragEvent.ACTION_DRAG_STARTED: {
1111            // clear state to recalculate which views we drag over
1112            mCurrentDragView = null;
1113
1114            // Set up our tracking of drag-started notifications
1115            mCurrentDrag = DragEvent.obtain(event);
1116            if (mDragNotifiedChildren == null) {
1117                mDragNotifiedChildren = new HashSet<View>();
1118            } else {
1119                mDragNotifiedChildren.clear();
1120            }
1121
1122            // Now dispatch down to our children, caching the responses
1123            mChildAcceptsDrag = false;
1124            final int count = mChildrenCount;
1125            final View[] children = mChildren;
1126            for (int i = 0; i < count; i++) {
1127                final View child = children[i];
1128                child.mPrivateFlags2 &= ~View.DRAG_MASK;
1129                if (child.getVisibility() == VISIBLE) {
1130                    final boolean handled = notifyChildOfDrag(children[i]);
1131                    if (handled) {
1132                        mChildAcceptsDrag = true;
1133                    }
1134                }
1135            }
1136
1137            // Return HANDLED if one of our children can accept the drag
1138            if (mChildAcceptsDrag) {
1139                retval = true;
1140            }
1141        } break;
1142
1143        case DragEvent.ACTION_DRAG_ENDED: {
1144            // Release the bookkeeping now that the drag lifecycle has ended
1145            if (mDragNotifiedChildren != null) {
1146                for (View child : mDragNotifiedChildren) {
1147                    // If a child was notified about an ongoing drag, it's told that it's over
1148                    child.dispatchDragEvent(event);
1149                    child.mPrivateFlags2 &= ~View.DRAG_MASK;
1150                    child.refreshDrawableState();
1151                }
1152
1153                mDragNotifiedChildren.clear();
1154                mCurrentDrag.recycle();
1155                mCurrentDrag = null;
1156            }
1157
1158            // We consider drag-ended to have been handled if one of our children
1159            // had offered to handle the drag.
1160            if (mChildAcceptsDrag) {
1161                retval = true;
1162            }
1163        } break;
1164
1165        case DragEvent.ACTION_DRAG_LOCATION: {
1166            // Find the [possibly new] drag target
1167            final View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
1168
1169            // If we've changed apparent drag target, tell the view root which view
1170            // we're over now [for purposes of the eventual drag-recipient-changed
1171            // notifications to the framework] and tell the new target that the drag
1172            // has entered its bounds.  The root will see setDragFocus() calls all
1173            // the way down to the final leaf view that is handling the LOCATION event
1174            // before reporting the new potential recipient to the framework.
1175            if (mCurrentDragView != target) {
1176                root.setDragFocus(target);
1177
1178                final int action = event.mAction;
1179                // If we've dragged off of a child view, send it the EXITED message
1180                if (mCurrentDragView != null) {
1181                    final View view = mCurrentDragView;
1182                    event.mAction = DragEvent.ACTION_DRAG_EXITED;
1183                    view.dispatchDragEvent(event);
1184                    view.mPrivateFlags2 &= ~View.DRAG_HOVERED;
1185                    view.refreshDrawableState();
1186                }
1187                mCurrentDragView = target;
1188
1189                // If we've dragged over a new child view, send it the ENTERED message
1190                if (target != null) {
1191                    event.mAction = DragEvent.ACTION_DRAG_ENTERED;
1192                    target.dispatchDragEvent(event);
1193                    target.mPrivateFlags2 |= View.DRAG_HOVERED;
1194                    target.refreshDrawableState();
1195                }
1196                event.mAction = action;  // restore the event's original state
1197            }
1198
1199            // Dispatch the actual drag location notice, localized into its coordinates
1200            if (target != null) {
1201                event.mX = mLocalPoint.x;
1202                event.mY = mLocalPoint.y;
1203
1204                retval = target.dispatchDragEvent(event);
1205
1206                event.mX = tx;
1207                event.mY = ty;
1208            }
1209        } break;
1210
1211        /* Entered / exited dispatch
1212         *
1213         * DRAG_ENTERED is not dispatched downwards from ViewGroup.  The reason for this is
1214         * that we're about to get the corresponding LOCATION event, which we will use to
1215         * determine which of our children is the new target; at that point we will
1216         * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
1217         *
1218         * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1219         * drag has left this ViewGroup, we know by definition that every contained subview
1220         * is also no longer under the drag point.
1221         */
1222
1223        case DragEvent.ACTION_DRAG_EXITED: {
1224            if (mCurrentDragView != null) {
1225                final View view = mCurrentDragView;
1226                view.dispatchDragEvent(event);
1227                view.mPrivateFlags2 &= ~View.DRAG_HOVERED;
1228                view.refreshDrawableState();
1229
1230                mCurrentDragView = null;
1231            }
1232        } break;
1233
1234        case DragEvent.ACTION_DROP: {
1235            if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
1236            View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
1237            if (target != null) {
1238                if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "   dispatch drop to " + target);
1239                event.mX = mLocalPoint.x;
1240                event.mY = mLocalPoint.y;
1241                retval = target.dispatchDragEvent(event);
1242                event.mX = tx;
1243                event.mY = ty;
1244            } else {
1245                if (ViewDebug.DEBUG_DRAG) {
1246                    Log.d(View.VIEW_LOG_TAG, "   not dropped on an accepting view");
1247                }
1248            }
1249        } break;
1250        }
1251
1252        // If none of our children could handle the event, try here
1253        if (!retval) {
1254            // Call up to the View implementation that dispatches to installed listeners
1255            retval = super.dispatchDragEvent(event);
1256        }
1257        return retval;
1258    }
1259
1260    // Find the frontmost child view that lies under the given point, and calculate
1261    // the position within its own local coordinate system.
1262    View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
1263        final int count = mChildrenCount;
1264        final View[] children = mChildren;
1265        for (int i = count - 1; i >= 0; i--) {
1266            final View child = children[i];
1267            if (!child.canAcceptDrag()) {
1268                continue;
1269            }
1270
1271            if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
1272                return child;
1273            }
1274        }
1275        return null;
1276    }
1277
1278    boolean notifyChildOfDrag(View child) {
1279        if (ViewDebug.DEBUG_DRAG) {
1280            Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1281        }
1282
1283        boolean canAccept = false;
1284        if (! mDragNotifiedChildren.contains(child)) {
1285            mDragNotifiedChildren.add(child);
1286            canAccept = child.dispatchDragEvent(mCurrentDrag);
1287            if (canAccept && !child.canAcceptDrag()) {
1288                child.mPrivateFlags2 |= View.DRAG_CAN_ACCEPT;
1289                child.refreshDrawableState();
1290            }
1291        }
1292        return canAccept;
1293    }
1294
1295    @Override
1296    public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1297        super.dispatchWindowSystemUiVisiblityChanged(visible);
1298
1299        final int count = mChildrenCount;
1300        final View[] children = mChildren;
1301        for (int i=0; i <count; i++) {
1302            final View child = children[i];
1303            child.dispatchWindowSystemUiVisiblityChanged(visible);
1304        }
1305    }
1306
1307    @Override
1308    public void dispatchSystemUiVisibilityChanged(int visible) {
1309        super.dispatchSystemUiVisibilityChanged(visible);
1310
1311        final int count = mChildrenCount;
1312        final View[] children = mChildren;
1313        for (int i=0; i <count; i++) {
1314            final View child = children[i];
1315            child.dispatchSystemUiVisibilityChanged(visible);
1316        }
1317    }
1318
1319    @Override
1320    void updateLocalSystemUiVisibility(int localValue, int localChanges) {
1321        super.updateLocalSystemUiVisibility(localValue, localChanges);
1322
1323        final int count = mChildrenCount;
1324        final View[] children = mChildren;
1325        for (int i=0; i <count; i++) {
1326            final View child = children[i];
1327            child.updateLocalSystemUiVisibility(localValue, localChanges);
1328        }
1329    }
1330
1331    /**
1332     * {@inheritDoc}
1333     */
1334    @Override
1335    public boolean dispatchKeyEventPreIme(KeyEvent event) {
1336        if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1337            return super.dispatchKeyEventPreIme(event);
1338        } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1339            return mFocused.dispatchKeyEventPreIme(event);
1340        }
1341        return false;
1342    }
1343
1344    /**
1345     * {@inheritDoc}
1346     */
1347    @Override
1348    public boolean dispatchKeyEvent(KeyEvent event) {
1349        if (mInputEventConsistencyVerifier != null) {
1350            mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1351        }
1352
1353        if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1354            if (super.dispatchKeyEvent(event)) {
1355                return true;
1356            }
1357        } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1358            if (mFocused.dispatchKeyEvent(event)) {
1359                return true;
1360            }
1361        }
1362
1363        if (mInputEventConsistencyVerifier != null) {
1364            mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
1365        }
1366        return false;
1367    }
1368
1369    /**
1370     * {@inheritDoc}
1371     */
1372    @Override
1373    public boolean dispatchKeyShortcutEvent(KeyEvent event) {
1374        if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1375            return super.dispatchKeyShortcutEvent(event);
1376        } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1377            return mFocused.dispatchKeyShortcutEvent(event);
1378        }
1379        return false;
1380    }
1381
1382    /**
1383     * {@inheritDoc}
1384     */
1385    @Override
1386    public boolean dispatchTrackballEvent(MotionEvent event) {
1387        if (mInputEventConsistencyVerifier != null) {
1388            mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1389        }
1390
1391        if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1392            if (super.dispatchTrackballEvent(event)) {
1393                return true;
1394            }
1395        } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1396            if (mFocused.dispatchTrackballEvent(event)) {
1397                return true;
1398            }
1399        }
1400
1401        if (mInputEventConsistencyVerifier != null) {
1402            mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
1403        }
1404        return false;
1405    }
1406
1407    /**
1408     * {@inheritDoc}
1409     */
1410    @SuppressWarnings({"ConstantConditions"})
1411    @Override
1412    protected boolean dispatchHoverEvent(MotionEvent event) {
1413        final int action = event.getAction();
1414
1415        // First check whether the view group wants to intercept the hover event.
1416        final boolean interceptHover = onInterceptHoverEvent(event);
1417        event.setAction(action); // restore action in case it was changed
1418
1419        MotionEvent eventNoHistory = event;
1420        boolean handled = false;
1421
1422        // Send events to the hovered children and build a new list of hover targets until
1423        // one is found that handles the event.
1424        HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1425        mFirstHoverTarget = null;
1426        if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
1427            final float x = event.getX();
1428            final float y = event.getY();
1429            final int childrenCount = mChildrenCount;
1430            if (childrenCount != 0) {
1431                final View[] children = mChildren;
1432                HoverTarget lastHoverTarget = null;
1433                for (int i = childrenCount - 1; i >= 0; i--) {
1434                    final View child = children[i];
1435                    if (!canViewReceivePointerEvents(child)
1436                            || !isTransformedTouchPointInView(x, y, child, null)) {
1437                        continue;
1438                    }
1439
1440                    // Obtain a hover target for this child.  Dequeue it from the
1441                    // old hover target list if the child was previously hovered.
1442                    HoverTarget hoverTarget = firstOldHoverTarget;
1443                    final boolean wasHovered;
1444                    for (HoverTarget predecessor = null; ;) {
1445                        if (hoverTarget == null) {
1446                            hoverTarget = HoverTarget.obtain(child);
1447                            wasHovered = false;
1448                            break;
1449                        }
1450
1451                        if (hoverTarget.child == child) {
1452                            if (predecessor != null) {
1453                                predecessor.next = hoverTarget.next;
1454                            } else {
1455                                firstOldHoverTarget = hoverTarget.next;
1456                            }
1457                            hoverTarget.next = null;
1458                            wasHovered = true;
1459                            break;
1460                        }
1461
1462                        predecessor = hoverTarget;
1463                        hoverTarget = hoverTarget.next;
1464                    }
1465
1466                    // Enqueue the hover target onto the new hover target list.
1467                    if (lastHoverTarget != null) {
1468                        lastHoverTarget.next = hoverTarget;
1469                    } else {
1470                        lastHoverTarget = hoverTarget;
1471                        mFirstHoverTarget = hoverTarget;
1472                    }
1473
1474                    // Dispatch the event to the child.
1475                    if (action == MotionEvent.ACTION_HOVER_ENTER) {
1476                        if (!wasHovered) {
1477                            // Send the enter as is.
1478                            handled |= dispatchTransformedGenericPointerEvent(
1479                                    event, child); // enter
1480                        }
1481                    } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1482                        if (!wasHovered) {
1483                            // Synthesize an enter from a move.
1484                            eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1485                            eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1486                            handled |= dispatchTransformedGenericPointerEvent(
1487                                    eventNoHistory, child); // enter
1488                            eventNoHistory.setAction(action);
1489
1490                            handled |= dispatchTransformedGenericPointerEvent(
1491                                    eventNoHistory, child); // move
1492                        } else {
1493                            // Send the move as is.
1494                            handled |= dispatchTransformedGenericPointerEvent(event, child);
1495                        }
1496                    }
1497                    if (handled) {
1498                        break;
1499                    }
1500                }
1501            }
1502        }
1503
1504        // Send exit events to all previously hovered children that are no longer hovered.
1505        while (firstOldHoverTarget != null) {
1506            final View child = firstOldHoverTarget.child;
1507
1508            // Exit the old hovered child.
1509            if (action == MotionEvent.ACTION_HOVER_EXIT) {
1510                // Send the exit as is.
1511                handled |= dispatchTransformedGenericPointerEvent(
1512                        event, child); // exit
1513            } else {
1514                // Synthesize an exit from a move or enter.
1515                // Ignore the result because hover focus has moved to a different view.
1516                if (action == MotionEvent.ACTION_HOVER_MOVE) {
1517                    dispatchTransformedGenericPointerEvent(
1518                            event, child); // move
1519                }
1520                eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1521                eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1522                dispatchTransformedGenericPointerEvent(
1523                        eventNoHistory, child); // exit
1524                eventNoHistory.setAction(action);
1525            }
1526
1527            final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1528            firstOldHoverTarget.recycle();
1529            firstOldHoverTarget = nextOldHoverTarget;
1530        }
1531
1532        // Send events to the view group itself if no children have handled it.
1533        boolean newHoveredSelf = !handled;
1534        if (newHoveredSelf == mHoveredSelf) {
1535            if (newHoveredSelf) {
1536                // Send event to the view group as before.
1537                handled |= super.dispatchHoverEvent(event);
1538            }
1539        } else {
1540            if (mHoveredSelf) {
1541                // Exit the view group.
1542                if (action == MotionEvent.ACTION_HOVER_EXIT) {
1543                    // Send the exit as is.
1544                    handled |= super.dispatchHoverEvent(event); // exit
1545                } else {
1546                    // Synthesize an exit from a move or enter.
1547                    // Ignore the result because hover focus is moving to a different view.
1548                    if (action == MotionEvent.ACTION_HOVER_MOVE) {
1549                        super.dispatchHoverEvent(event); // move
1550                    }
1551                    eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1552                    eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1553                    super.dispatchHoverEvent(eventNoHistory); // exit
1554                    eventNoHistory.setAction(action);
1555                }
1556                mHoveredSelf = false;
1557            }
1558
1559            if (newHoveredSelf) {
1560                // Enter the view group.
1561                if (action == MotionEvent.ACTION_HOVER_ENTER) {
1562                    // Send the enter as is.
1563                    handled |= super.dispatchHoverEvent(event); // enter
1564                    mHoveredSelf = true;
1565                } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1566                    // Synthesize an enter from a move.
1567                    eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1568                    eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1569                    handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1570                    eventNoHistory.setAction(action);
1571
1572                    handled |= super.dispatchHoverEvent(eventNoHistory); // move
1573                    mHoveredSelf = true;
1574                }
1575            }
1576        }
1577
1578        // Recycle the copy of the event that we made.
1579        if (eventNoHistory != event) {
1580            eventNoHistory.recycle();
1581        }
1582
1583        // Done.
1584        return handled;
1585    }
1586
1587    private void exitHoverTargets() {
1588        if (mHoveredSelf || mFirstHoverTarget != null) {
1589            final long now = SystemClock.uptimeMillis();
1590            MotionEvent event = MotionEvent.obtain(now, now,
1591                    MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1592            event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1593            dispatchHoverEvent(event);
1594            event.recycle();
1595        }
1596    }
1597
1598    private void cancelHoverTarget(View view) {
1599        HoverTarget predecessor = null;
1600        HoverTarget target = mFirstHoverTarget;
1601        while (target != null) {
1602            final HoverTarget next = target.next;
1603            if (target.child == view) {
1604                if (predecessor == null) {
1605                    mFirstHoverTarget = next;
1606                } else {
1607                    predecessor.next = next;
1608                }
1609                target.recycle();
1610
1611                final long now = SystemClock.uptimeMillis();
1612                MotionEvent event = MotionEvent.obtain(now, now,
1613                        MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1614                event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1615                view.dispatchHoverEvent(event);
1616                event.recycle();
1617                return;
1618            }
1619            predecessor = target;
1620            target = next;
1621        }
1622    }
1623
1624    /** @hide */
1625    @Override
1626    protected boolean hasHoveredChild() {
1627        return mFirstHoverTarget != null;
1628    }
1629
1630    @Override
1631    public void addChildrenForAccessibility(ArrayList<View> childrenForAccessibility) {
1632        ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
1633        try {
1634            final int childrenCount = children.getChildCount();
1635            for (int i = 0; i < childrenCount; i++) {
1636                View child = children.getChildAt(i);
1637                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
1638                        && (child.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
1639                    if (child.includeForAccessibility()) {
1640                        childrenForAccessibility.add(child);
1641                    } else {
1642                        child.addChildrenForAccessibility(childrenForAccessibility);
1643                    }
1644                }
1645            }
1646        } finally {
1647            children.recycle();
1648        }
1649    }
1650
1651    /**
1652     * @hide
1653     */
1654    @Override
1655    public void childAccessibilityStateChanged(View child) {
1656        if (mParent != null) {
1657            mParent.childAccessibilityStateChanged(child);
1658        }
1659    }
1660
1661    /**
1662     * Implement this method to intercept hover events before they are handled
1663     * by child views.
1664     * <p>
1665     * This method is called before dispatching a hover event to a child of
1666     * the view group or to the view group's own {@link #onHoverEvent} to allow
1667     * the view group a chance to intercept the hover event.
1668     * This method can also be used to watch all pointer motions that occur within
1669     * the bounds of the view group even when the pointer is hovering over
1670     * a child of the view group rather than over the view group itself.
1671     * </p><p>
1672     * The view group can prevent its children from receiving hover events by
1673     * implementing this method and returning <code>true</code> to indicate
1674     * that it would like to intercept hover events.  The view group must
1675     * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
1676     * for as long as it wishes to continue intercepting hover events from
1677     * its children.
1678     * </p><p>
1679     * Interception preserves the invariant that at most one view can be
1680     * hovered at a time by transferring hover focus from the currently hovered
1681     * child to the view group or vice-versa as needed.
1682     * </p><p>
1683     * If this method returns <code>true</code> and a child is already hovered, then the
1684     * child view will first receive a hover exit event and then the view group
1685     * itself will receive a hover enter event in {@link #onHoverEvent}.
1686     * Likewise, if this method had previously returned <code>true</code> to intercept hover
1687     * events and instead returns <code>false</code> while the pointer is hovering
1688     * within the bounds of one of a child, then the view group will first receive a
1689     * hover exit event in {@link #onHoverEvent} and then the hovered child will
1690     * receive a hover enter event.
1691     * </p><p>
1692     * The default implementation always returns false.
1693     * </p>
1694     *
1695     * @param event The motion event that describes the hover.
1696     * @return True if the view group would like to intercept the hover event
1697     * and prevent its children from receiving it.
1698     */
1699    public boolean onInterceptHoverEvent(MotionEvent event) {
1700        return false;
1701    }
1702
1703    private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
1704        if (event.getHistorySize() == 0) {
1705            return event;
1706        }
1707        return MotionEvent.obtainNoHistory(event);
1708    }
1709
1710    /**
1711     * {@inheritDoc}
1712     */
1713    @Override
1714    protected boolean dispatchGenericPointerEvent(MotionEvent event) {
1715        // Send the event to the child under the pointer.
1716        final int childrenCount = mChildrenCount;
1717        if (childrenCount != 0) {
1718            final View[] children = mChildren;
1719            final float x = event.getX();
1720            final float y = event.getY();
1721
1722            for (int i = childrenCount - 1; i >= 0; i--) {
1723                final View child = children[i];
1724                if (!canViewReceivePointerEvents(child)
1725                        || !isTransformedTouchPointInView(x, y, child, null)) {
1726                    continue;
1727                }
1728
1729                if (dispatchTransformedGenericPointerEvent(event, child)) {
1730                    return true;
1731                }
1732            }
1733        }
1734
1735        // No child handled the event.  Send it to this view group.
1736        return super.dispatchGenericPointerEvent(event);
1737    }
1738
1739    /**
1740     * {@inheritDoc}
1741     */
1742    @Override
1743    protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
1744        // Send the event to the focused child or to this view group if it has focus.
1745        if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1746            return super.dispatchGenericFocusedEvent(event);
1747        } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1748            return mFocused.dispatchGenericMotionEvent(event);
1749        }
1750        return false;
1751    }
1752
1753    /**
1754     * Dispatches a generic pointer event to a child, taking into account
1755     * transformations that apply to the child.
1756     *
1757     * @param event The event to send.
1758     * @param child The view to send the event to.
1759     * @return {@code true} if the child handled the event.
1760     */
1761    private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
1762        final float offsetX = mScrollX - child.mLeft;
1763        final float offsetY = mScrollY - child.mTop;
1764
1765        boolean handled;
1766        if (!child.hasIdentityMatrix()) {
1767            MotionEvent transformedEvent = MotionEvent.obtain(event);
1768            transformedEvent.offsetLocation(offsetX, offsetY);
1769            transformedEvent.transform(child.getInverseMatrix());
1770            handled = child.dispatchGenericMotionEvent(transformedEvent);
1771            transformedEvent.recycle();
1772        } else {
1773            event.offsetLocation(offsetX, offsetY);
1774            handled = child.dispatchGenericMotionEvent(event);
1775            event.offsetLocation(-offsetX, -offsetY);
1776        }
1777        return handled;
1778    }
1779
1780    /**
1781     * {@inheritDoc}
1782     */
1783    @Override
1784    public boolean dispatchTouchEvent(MotionEvent ev) {
1785        if (mInputEventConsistencyVerifier != null) {
1786            mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
1787        }
1788
1789        boolean handled = false;
1790        if (onFilterTouchEventForSecurity(ev)) {
1791            final int action = ev.getAction();
1792            final int actionMasked = action & MotionEvent.ACTION_MASK;
1793
1794            // Handle an initial down.
1795            if (actionMasked == MotionEvent.ACTION_DOWN) {
1796                // Throw away all previous state when starting a new touch gesture.
1797                // The framework may have dropped the up or cancel event for the previous gesture
1798                // due to an app switch, ANR, or some other state change.
1799                cancelAndClearTouchTargets(ev);
1800                resetTouchState();
1801            }
1802
1803            // Check for interception.
1804            final boolean intercepted;
1805            if (actionMasked == MotionEvent.ACTION_DOWN
1806                    || mFirstTouchTarget != null) {
1807                final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
1808                if (!disallowIntercept) {
1809                    intercepted = onInterceptTouchEvent(ev);
1810                    ev.setAction(action); // restore action in case it was changed
1811                } else {
1812                    intercepted = false;
1813                }
1814            } else {
1815                // There are no touch targets and this action is not an initial down
1816                // so this view group continues to intercept touches.
1817                intercepted = true;
1818            }
1819
1820            // Check for cancelation.
1821            final boolean canceled = resetCancelNextUpFlag(this)
1822                    || actionMasked == MotionEvent.ACTION_CANCEL;
1823
1824            // Update list of touch targets for pointer down, if needed.
1825            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
1826            TouchTarget newTouchTarget = null;
1827            boolean alreadyDispatchedToNewTouchTarget = false;
1828            if (!canceled && !intercepted) {
1829                if (actionMasked == MotionEvent.ACTION_DOWN
1830                        || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
1831                        || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
1832                    final int actionIndex = ev.getActionIndex(); // always 0 for down
1833                    final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
1834                            : TouchTarget.ALL_POINTER_IDS;
1835
1836                    // Clean up earlier touch targets for this pointer id in case they
1837                    // have become out of sync.
1838                    removePointersFromTouchTargets(idBitsToAssign);
1839
1840                    final int childrenCount = mChildrenCount;
1841                    if (childrenCount != 0) {
1842                        // Find a child that can receive the event.
1843                        // Scan children from front to back.
1844                        final View[] children = mChildren;
1845                        final float x = ev.getX(actionIndex);
1846                        final float y = ev.getY(actionIndex);
1847
1848                        for (int i = childrenCount - 1; i >= 0; i--) {
1849                            final View child = children[i];
1850                            if (!canViewReceivePointerEvents(child)
1851                                    || !isTransformedTouchPointInView(x, y, child, null)) {
1852                                continue;
1853                            }
1854
1855                            newTouchTarget = getTouchTarget(child);
1856                            if (newTouchTarget != null) {
1857                                // Child is already receiving touch within its bounds.
1858                                // Give it the new pointer in addition to the ones it is handling.
1859                                newTouchTarget.pointerIdBits |= idBitsToAssign;
1860                                break;
1861                            }
1862
1863                            resetCancelNextUpFlag(child);
1864                            if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
1865                                // Child wants to receive touch within its bounds.
1866                                mLastTouchDownTime = ev.getDownTime();
1867                                mLastTouchDownIndex = i;
1868                                mLastTouchDownX = ev.getX();
1869                                mLastTouchDownY = ev.getY();
1870                                newTouchTarget = addTouchTarget(child, idBitsToAssign);
1871                                alreadyDispatchedToNewTouchTarget = true;
1872                                break;
1873                            }
1874                        }
1875                    }
1876
1877                    if (newTouchTarget == null && mFirstTouchTarget != null) {
1878                        // Did not find a child to receive the event.
1879                        // Assign the pointer to the least recently added target.
1880                        newTouchTarget = mFirstTouchTarget;
1881                        while (newTouchTarget.next != null) {
1882                            newTouchTarget = newTouchTarget.next;
1883                        }
1884                        newTouchTarget.pointerIdBits |= idBitsToAssign;
1885                    }
1886                }
1887            }
1888
1889            // Dispatch to touch targets.
1890            if (mFirstTouchTarget == null) {
1891                // No touch targets so treat this as an ordinary view.
1892                handled = dispatchTransformedTouchEvent(ev, canceled, null,
1893                        TouchTarget.ALL_POINTER_IDS);
1894            } else {
1895                // Dispatch to touch targets, excluding the new touch target if we already
1896                // dispatched to it.  Cancel touch targets if necessary.
1897                TouchTarget predecessor = null;
1898                TouchTarget target = mFirstTouchTarget;
1899                while (target != null) {
1900                    final TouchTarget next = target.next;
1901                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
1902                        handled = true;
1903                    } else {
1904                        final boolean cancelChild = resetCancelNextUpFlag(target.child)
1905                        || intercepted;
1906                        if (dispatchTransformedTouchEvent(ev, cancelChild,
1907                                target.child, target.pointerIdBits)) {
1908                            handled = true;
1909                        }
1910                        if (cancelChild) {
1911                            if (predecessor == null) {
1912                                mFirstTouchTarget = next;
1913                            } else {
1914                                predecessor.next = next;
1915                            }
1916                            target.recycle();
1917                            target = next;
1918                            continue;
1919                        }
1920                    }
1921                    predecessor = target;
1922                    target = next;
1923                }
1924            }
1925
1926            // Update list of touch targets for pointer up or cancel, if needed.
1927            if (canceled
1928                    || actionMasked == MotionEvent.ACTION_UP
1929                    || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
1930                resetTouchState();
1931            } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
1932                final int actionIndex = ev.getActionIndex();
1933                final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
1934                removePointersFromTouchTargets(idBitsToRemove);
1935            }
1936        }
1937
1938        if (!handled && mInputEventConsistencyVerifier != null) {
1939            mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
1940        }
1941        return handled;
1942    }
1943
1944    /**
1945     * Resets all touch state in preparation for a new cycle.
1946     */
1947    private void resetTouchState() {
1948        clearTouchTargets();
1949        resetCancelNextUpFlag(this);
1950        mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
1951    }
1952
1953    /**
1954     * Resets the cancel next up flag.
1955     * Returns true if the flag was previously set.
1956     */
1957    private static boolean resetCancelNextUpFlag(View view) {
1958        if ((view.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) {
1959            view.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
1960            return true;
1961        }
1962        return false;
1963    }
1964
1965    /**
1966     * Clears all touch targets.
1967     */
1968    private void clearTouchTargets() {
1969        TouchTarget target = mFirstTouchTarget;
1970        if (target != null) {
1971            do {
1972                TouchTarget next = target.next;
1973                target.recycle();
1974                target = next;
1975            } while (target != null);
1976            mFirstTouchTarget = null;
1977        }
1978    }
1979
1980    /**
1981     * Cancels and clears all touch targets.
1982     */
1983    private void cancelAndClearTouchTargets(MotionEvent event) {
1984        if (mFirstTouchTarget != null) {
1985            boolean syntheticEvent = false;
1986            if (event == null) {
1987                final long now = SystemClock.uptimeMillis();
1988                event = MotionEvent.obtain(now, now,
1989                        MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
1990                event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1991                syntheticEvent = true;
1992            }
1993
1994            for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
1995                resetCancelNextUpFlag(target.child);
1996                dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
1997            }
1998            clearTouchTargets();
1999
2000            if (syntheticEvent) {
2001                event.recycle();
2002            }
2003        }
2004    }
2005
2006    /**
2007     * Gets the touch target for specified child view.
2008     * Returns null if not found.
2009     */
2010    private TouchTarget getTouchTarget(View child) {
2011        for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2012            if (target.child == child) {
2013                return target;
2014            }
2015        }
2016        return null;
2017    }
2018
2019    /**
2020     * Adds a touch target for specified child to the beginning of the list.
2021     * Assumes the target child is not already present.
2022     */
2023    private TouchTarget addTouchTarget(View child, int pointerIdBits) {
2024        TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
2025        target.next = mFirstTouchTarget;
2026        mFirstTouchTarget = target;
2027        return target;
2028    }
2029
2030    /**
2031     * Removes the pointer ids from consideration.
2032     */
2033    private void removePointersFromTouchTargets(int pointerIdBits) {
2034        TouchTarget predecessor = null;
2035        TouchTarget target = mFirstTouchTarget;
2036        while (target != null) {
2037            final TouchTarget next = target.next;
2038            if ((target.pointerIdBits & pointerIdBits) != 0) {
2039                target.pointerIdBits &= ~pointerIdBits;
2040                if (target.pointerIdBits == 0) {
2041                    if (predecessor == null) {
2042                        mFirstTouchTarget = next;
2043                    } else {
2044                        predecessor.next = next;
2045                    }
2046                    target.recycle();
2047                    target = next;
2048                    continue;
2049                }
2050            }
2051            predecessor = target;
2052            target = next;
2053        }
2054    }
2055
2056    private void cancelTouchTarget(View view) {
2057        TouchTarget predecessor = null;
2058        TouchTarget target = mFirstTouchTarget;
2059        while (target != null) {
2060            final TouchTarget next = target.next;
2061            if (target.child == view) {
2062                if (predecessor == null) {
2063                    mFirstTouchTarget = next;
2064                } else {
2065                    predecessor.next = next;
2066                }
2067                target.recycle();
2068
2069                final long now = SystemClock.uptimeMillis();
2070                MotionEvent event = MotionEvent.obtain(now, now,
2071                        MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2072                event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2073                view.dispatchTouchEvent(event);
2074                event.recycle();
2075                return;
2076            }
2077            predecessor = target;
2078            target = next;
2079        }
2080    }
2081
2082    /**
2083     * Returns true if a child view can receive pointer events.
2084     * @hide
2085     */
2086    private static boolean canViewReceivePointerEvents(View child) {
2087        return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2088                || child.getAnimation() != null;
2089    }
2090
2091    /**
2092     * Returns true if a child view contains the specified point when transformed
2093     * into its coordinate space.
2094     * Child must not be null.
2095     * @hide
2096     */
2097    protected boolean isTransformedTouchPointInView(float x, float y, View child,
2098            PointF outLocalPoint) {
2099        float localX = x + mScrollX - child.mLeft;
2100        float localY = y + mScrollY - child.mTop;
2101        if (! child.hasIdentityMatrix() && mAttachInfo != null) {
2102            final float[] localXY = mAttachInfo.mTmpTransformLocation;
2103            localXY[0] = localX;
2104            localXY[1] = localY;
2105            child.getInverseMatrix().mapPoints(localXY);
2106            localX = localXY[0];
2107            localY = localXY[1];
2108        }
2109        final boolean isInView = child.pointInView(localX, localY);
2110        if (isInView && outLocalPoint != null) {
2111            outLocalPoint.set(localX, localY);
2112        }
2113        return isInView;
2114    }
2115
2116    /**
2117     * Transforms a motion event into the coordinate space of a particular child view,
2118     * filters out irrelevant pointer ids, and overrides its action if necessary.
2119     * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
2120     */
2121    private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
2122            View child, int desiredPointerIdBits) {
2123        final boolean handled;
2124
2125        // Canceling motions is a special case.  We don't need to perform any transformations
2126        // or filtering.  The important part is the action, not the contents.
2127        final int oldAction = event.getAction();
2128        if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
2129            event.setAction(MotionEvent.ACTION_CANCEL);
2130            if (child == null) {
2131                handled = super.dispatchTouchEvent(event);
2132            } else {
2133                handled = child.dispatchTouchEvent(event);
2134            }
2135            event.setAction(oldAction);
2136            return handled;
2137        }
2138
2139        // Calculate the number of pointers to deliver.
2140        final int oldPointerIdBits = event.getPointerIdBits();
2141        final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
2142
2143        // If for some reason we ended up in an inconsistent state where it looks like we
2144        // might produce a motion event with no pointers in it, then drop the event.
2145        if (newPointerIdBits == 0) {
2146            return false;
2147        }
2148
2149        // If the number of pointers is the same and we don't need to perform any fancy
2150        // irreversible transformations, then we can reuse the motion event for this
2151        // dispatch as long as we are careful to revert any changes we make.
2152        // Otherwise we need to make a copy.
2153        final MotionEvent transformedEvent;
2154        if (newPointerIdBits == oldPointerIdBits) {
2155            if (child == null || child.hasIdentityMatrix()) {
2156                if (child == null) {
2157                    handled = super.dispatchTouchEvent(event);
2158                } else {
2159                    final float offsetX = mScrollX - child.mLeft;
2160                    final float offsetY = mScrollY - child.mTop;
2161                    event.offsetLocation(offsetX, offsetY);
2162
2163                    handled = child.dispatchTouchEvent(event);
2164
2165                    event.offsetLocation(-offsetX, -offsetY);
2166                }
2167                return handled;
2168            }
2169            transformedEvent = MotionEvent.obtain(event);
2170        } else {
2171            transformedEvent = event.split(newPointerIdBits);
2172        }
2173
2174        // Perform any necessary transformations and dispatch.
2175        if (child == null) {
2176            handled = super.dispatchTouchEvent(transformedEvent);
2177        } else {
2178            final float offsetX = mScrollX - child.mLeft;
2179            final float offsetY = mScrollY - child.mTop;
2180            transformedEvent.offsetLocation(offsetX, offsetY);
2181            if (! child.hasIdentityMatrix()) {
2182                transformedEvent.transform(child.getInverseMatrix());
2183            }
2184
2185            handled = child.dispatchTouchEvent(transformedEvent);
2186        }
2187
2188        // Done.
2189        transformedEvent.recycle();
2190        return handled;
2191    }
2192
2193    /**
2194     * Enable or disable the splitting of MotionEvents to multiple children during touch event
2195     * dispatch. This behavior is enabled by default for applications that target an
2196     * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
2197     *
2198     * <p>When this option is enabled MotionEvents may be split and dispatched to different child
2199     * views depending on where each pointer initially went down. This allows for user interactions
2200     * such as scrolling two panes of content independently, chording of buttons, and performing
2201     * independent gestures on different pieces of content.
2202     *
2203     * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2204     *              child views. <code>false</code> to only allow one child view to be the target of
2205     *              any MotionEvent received by this ViewGroup.
2206     */
2207    public void setMotionEventSplittingEnabled(boolean split) {
2208        // TODO Applications really shouldn't change this setting mid-touch event,
2209        // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2210        // with gestures in progress when this is changed.
2211        if (split) {
2212            mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2213        } else {
2214            mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
2215        }
2216    }
2217
2218    /**
2219     * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2220     * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2221     */
2222    public boolean isMotionEventSplittingEnabled() {
2223        return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2224    }
2225
2226    /**
2227     * {@inheritDoc}
2228     */
2229    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2230
2231        if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2232            // We're already in this state, assume our ancestors are too
2233            return;
2234        }
2235
2236        if (disallowIntercept) {
2237            mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2238        } else {
2239            mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2240        }
2241
2242        // Pass it up to our parent
2243        if (mParent != null) {
2244            mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2245        }
2246    }
2247
2248    /**
2249     * Implement this method to intercept all touch screen motion events.  This
2250     * allows you to watch events as they are dispatched to your children, and
2251     * take ownership of the current gesture at any point.
2252     *
2253     * <p>Using this function takes some care, as it has a fairly complicated
2254     * interaction with {@link View#onTouchEvent(MotionEvent)
2255     * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2256     * that method as well as this one in the correct way.  Events will be
2257     * received in the following order:
2258     *
2259     * <ol>
2260     * <li> You will receive the down event here.
2261     * <li> The down event will be handled either by a child of this view
2262     * group, or given to your own onTouchEvent() method to handle; this means
2263     * you should implement onTouchEvent() to return true, so you will
2264     * continue to see the rest of the gesture (instead of looking for
2265     * a parent view to handle it).  Also, by returning true from
2266     * onTouchEvent(), you will not receive any following
2267     * events in onInterceptTouchEvent() and all touch processing must
2268     * happen in onTouchEvent() like normal.
2269     * <li> For as long as you return false from this function, each following
2270     * event (up to and including the final up) will be delivered first here
2271     * and then to the target's onTouchEvent().
2272     * <li> If you return true from here, you will not receive any
2273     * following events: the target view will receive the same event but
2274     * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2275     * events will be delivered to your onTouchEvent() method and no longer
2276     * appear here.
2277     * </ol>
2278     *
2279     * @param ev The motion event being dispatched down the hierarchy.
2280     * @return Return true to steal motion events from the children and have
2281     * them dispatched to this ViewGroup through onTouchEvent().
2282     * The current target will receive an ACTION_CANCEL event, and no further
2283     * messages will be delivered here.
2284     */
2285    public boolean onInterceptTouchEvent(MotionEvent ev) {
2286        return false;
2287    }
2288
2289    /**
2290     * {@inheritDoc}
2291     *
2292     * Looks for a view to give focus to respecting the setting specified by
2293     * {@link #getDescendantFocusability()}.
2294     *
2295     * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2296     * find focus within the children of this group when appropriate.
2297     *
2298     * @see #FOCUS_BEFORE_DESCENDANTS
2299     * @see #FOCUS_AFTER_DESCENDANTS
2300     * @see #FOCUS_BLOCK_DESCENDANTS
2301     * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
2302     */
2303    @Override
2304    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2305        if (DBG) {
2306            System.out.println(this + " ViewGroup.requestFocus direction="
2307                    + direction);
2308        }
2309        int descendantFocusability = getDescendantFocusability();
2310
2311        switch (descendantFocusability) {
2312            case FOCUS_BLOCK_DESCENDANTS:
2313                return super.requestFocus(direction, previouslyFocusedRect);
2314            case FOCUS_BEFORE_DESCENDANTS: {
2315                final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2316                return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2317            }
2318            case FOCUS_AFTER_DESCENDANTS: {
2319                final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2320                return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2321            }
2322            default:
2323                throw new IllegalStateException("descendant focusability must be "
2324                        + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2325                        + "but is " + descendantFocusability);
2326        }
2327    }
2328
2329    /**
2330     * Look for a descendant to call {@link View#requestFocus} on.
2331     * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2332     * when it wants to request focus within its children.  Override this to
2333     * customize how your {@link ViewGroup} requests focus within its children.
2334     * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2335     * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2336     *        to give a finer grained hint about where focus is coming from.  May be null
2337     *        if there is no hint.
2338     * @return Whether focus was taken.
2339     */
2340    @SuppressWarnings({"ConstantConditions"})
2341    protected boolean onRequestFocusInDescendants(int direction,
2342            Rect previouslyFocusedRect) {
2343        int index;
2344        int increment;
2345        int end;
2346        int count = mChildrenCount;
2347        if ((direction & FOCUS_FORWARD) != 0) {
2348            index = 0;
2349            increment = 1;
2350            end = count;
2351        } else {
2352            index = count - 1;
2353            increment = -1;
2354            end = -1;
2355        }
2356        final View[] children = mChildren;
2357        for (int i = index; i != end; i += increment) {
2358            View child = children[i];
2359            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2360                if (child.requestFocus(direction, previouslyFocusedRect)) {
2361                    return true;
2362                }
2363            }
2364        }
2365        return false;
2366    }
2367
2368    /**
2369     * {@inheritDoc}
2370     *
2371     * @hide
2372     */
2373    @Override
2374    public void dispatchStartTemporaryDetach() {
2375        super.dispatchStartTemporaryDetach();
2376        final int count = mChildrenCount;
2377        final View[] children = mChildren;
2378        for (int i = 0; i < count; i++) {
2379            children[i].dispatchStartTemporaryDetach();
2380        }
2381    }
2382
2383    /**
2384     * {@inheritDoc}
2385     *
2386     * @hide
2387     */
2388    @Override
2389    public void dispatchFinishTemporaryDetach() {
2390        super.dispatchFinishTemporaryDetach();
2391        final int count = mChildrenCount;
2392        final View[] children = mChildren;
2393        for (int i = 0; i < count; i++) {
2394            children[i].dispatchFinishTemporaryDetach();
2395        }
2396    }
2397
2398    /**
2399     * {@inheritDoc}
2400     */
2401    @Override
2402    void dispatchAttachedToWindow(AttachInfo info, int visibility) {
2403        mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2404        super.dispatchAttachedToWindow(info, visibility);
2405        mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2406
2407        final int count = mChildrenCount;
2408        final View[] children = mChildren;
2409        for (int i = 0; i < count; i++) {
2410            final View child = children[i];
2411            child.dispatchAttachedToWindow(info,
2412                    visibility | (child.mViewFlags&VISIBILITY_MASK));
2413        }
2414    }
2415
2416    @Override
2417    void dispatchScreenStateChanged(int screenState) {
2418        super.dispatchScreenStateChanged(screenState);
2419
2420        final int count = mChildrenCount;
2421        final View[] children = mChildren;
2422        for (int i = 0; i < count; i++) {
2423            children[i].dispatchScreenStateChanged(screenState);
2424        }
2425    }
2426
2427    @Override
2428    boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
2429        boolean handled = false;
2430        if (includeForAccessibility()) {
2431            handled = super.dispatchPopulateAccessibilityEventInternal(event);
2432            if (handled) {
2433                return handled;
2434            }
2435        }
2436        // Let our children have a shot in populating the event.
2437        ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
2438        try {
2439            final int childCount = children.getChildCount();
2440            for (int i = 0; i < childCount; i++) {
2441                View child = children.getChildAt(i);
2442                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2443                    handled = child.dispatchPopulateAccessibilityEvent(event);
2444                    if (handled) {
2445                        return handled;
2446                    }
2447                }
2448            }
2449        } finally {
2450            children.recycle();
2451        }
2452        return false;
2453    }
2454
2455    @Override
2456    void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
2457        super.onInitializeAccessibilityNodeInfoInternal(info);
2458        if (mAttachInfo != null) {
2459            ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
2460            childrenForAccessibility.clear();
2461            addChildrenForAccessibility(childrenForAccessibility);
2462            final int childrenForAccessibilityCount = childrenForAccessibility.size();
2463            for (int i = 0; i < childrenForAccessibilityCount; i++) {
2464                View child = childrenForAccessibility.get(i);
2465                info.addChild(child);
2466            }
2467            childrenForAccessibility.clear();
2468        }
2469    }
2470
2471    @Override
2472    void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
2473        super.onInitializeAccessibilityEventInternal(event);
2474        event.setClassName(ViewGroup.class.getName());
2475    }
2476
2477    /**
2478     * @hide
2479     */
2480    @Override
2481    public void resetAccessibilityStateChanged() {
2482        super.resetAccessibilityStateChanged();
2483        View[] children = mChildren;
2484        final int childCount = mChildrenCount;
2485        for (int i = 0; i < childCount; i++) {
2486            View child = children[i];
2487            child.resetAccessibilityStateChanged();
2488        }
2489    }
2490
2491    /**
2492     * {@inheritDoc}
2493     */
2494    @Override
2495    void dispatchDetachedFromWindow() {
2496        // If we still have a touch target, we are still in the process of
2497        // dispatching motion events to a child; we need to get rid of that
2498        // child to avoid dispatching events to it after the window is torn
2499        // down. To make sure we keep the child in a consistent state, we
2500        // first send it an ACTION_CANCEL motion event.
2501        cancelAndClearTouchTargets(null);
2502
2503        // Similarly, set ACTION_EXIT to all hover targets and clear them.
2504        exitHoverTargets();
2505
2506        // In case view is detached while transition is running
2507        mLayoutSuppressed = false;
2508
2509        // Tear down our drag tracking
2510        mDragNotifiedChildren = null;
2511        if (mCurrentDrag != null) {
2512            mCurrentDrag.recycle();
2513            mCurrentDrag = null;
2514        }
2515
2516        final int count = mChildrenCount;
2517        final View[] children = mChildren;
2518        for (int i = 0; i < count; i++) {
2519            children[i].dispatchDetachedFromWindow();
2520        }
2521        super.dispatchDetachedFromWindow();
2522    }
2523
2524    /**
2525     * {@inheritDoc}
2526     */
2527    @Override
2528    public void setPadding(int left, int top, int right, int bottom) {
2529        super.setPadding(left, top, right, bottom);
2530
2531        if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
2532            mGroupFlags |= FLAG_PADDING_NOT_NULL;
2533        } else {
2534            mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
2535        }
2536    }
2537
2538    /**
2539     * {@inheritDoc}
2540     */
2541    @Override
2542    protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
2543        super.dispatchSaveInstanceState(container);
2544        final int count = mChildrenCount;
2545        final View[] children = mChildren;
2546        for (int i = 0; i < count; i++) {
2547            View c = children[i];
2548            if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
2549                c.dispatchSaveInstanceState(container);
2550            }
2551        }
2552    }
2553
2554    /**
2555     * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)}  freeze()}
2556     * to only this view, not to its children.  For use when overriding
2557     * {@link #dispatchSaveInstanceState(android.util.SparseArray)}  dispatchFreeze()} to allow
2558     * subclasses to freeze their own state but not the state of their children.
2559     *
2560     * @param container the container
2561     */
2562    protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
2563        super.dispatchSaveInstanceState(container);
2564    }
2565
2566    /**
2567     * {@inheritDoc}
2568     */
2569    @Override
2570    protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
2571        super.dispatchRestoreInstanceState(container);
2572        final int count = mChildrenCount;
2573        final View[] children = mChildren;
2574        for (int i = 0; i < count; i++) {
2575            View c = children[i];
2576            if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
2577                c.dispatchRestoreInstanceState(container);
2578            }
2579        }
2580    }
2581
2582    /**
2583     * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
2584     * to only this view, not to its children.  For use when overriding
2585     * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
2586     * subclasses to thaw their own state but not the state of their children.
2587     *
2588     * @param container the container
2589     */
2590    protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
2591        super.dispatchRestoreInstanceState(container);
2592    }
2593
2594    /**
2595     * Enables or disables the drawing cache for each child of this view group.
2596     *
2597     * @param enabled true to enable the cache, false to dispose of it
2598     */
2599    protected void setChildrenDrawingCacheEnabled(boolean enabled) {
2600        if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
2601            final View[] children = mChildren;
2602            final int count = mChildrenCount;
2603            for (int i = 0; i < count; i++) {
2604                children[i].setDrawingCacheEnabled(enabled);
2605            }
2606        }
2607    }
2608
2609    @Override
2610    protected void onAnimationStart() {
2611        super.onAnimationStart();
2612
2613        // When this ViewGroup's animation starts, build the cache for the children
2614        if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2615            final int count = mChildrenCount;
2616            final View[] children = mChildren;
2617            final boolean buildCache = !isHardwareAccelerated();
2618
2619            for (int i = 0; i < count; i++) {
2620                final View child = children[i];
2621                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2622                    child.setDrawingCacheEnabled(true);
2623                    if (buildCache) {
2624                        child.buildDrawingCache(true);
2625                    }
2626                }
2627            }
2628
2629            mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
2630        }
2631    }
2632
2633    @Override
2634    protected void onAnimationEnd() {
2635        super.onAnimationEnd();
2636
2637        // When this ViewGroup's animation ends, destroy the cache of the children
2638        if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2639            mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
2640
2641            if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
2642                setChildrenDrawingCacheEnabled(false);
2643            }
2644        }
2645    }
2646
2647    @Override
2648    Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
2649        int count = mChildrenCount;
2650        int[] visibilities = null;
2651
2652        if (skipChildren) {
2653            visibilities = new int[count];
2654            for (int i = 0; i < count; i++) {
2655                View child = getChildAt(i);
2656                visibilities[i] = child.getVisibility();
2657                if (visibilities[i] == View.VISIBLE) {
2658                    child.setVisibility(INVISIBLE);
2659                }
2660            }
2661        }
2662
2663        Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
2664
2665        if (skipChildren) {
2666            for (int i = 0; i < count; i++) {
2667                getChildAt(i).setVisibility(visibilities[i]);
2668            }
2669        }
2670
2671        return b;
2672    }
2673
2674    private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, int color) {
2675        Paint paint = getDebugPaint();
2676        paint.setColor(color);
2677
2678        canvas.drawLines(getDebugLines(x1, y1, x2, y2), paint);
2679    }
2680
2681    /**
2682     * @hide
2683     */
2684    protected void onDebugDrawMargins(Canvas canvas) {
2685        for (int i = 0; i < getChildCount(); i++) {
2686            View c = getChildAt(i);
2687            c.getLayoutParams().onDebugDraw(c, canvas);
2688        }
2689    }
2690
2691    /**
2692     * @hide
2693     */
2694    protected void onDebugDraw(Canvas canvas) {
2695        // Draw optical bounds
2696        if (getLayoutMode() == OPTICAL_BOUNDS) {
2697            for (int i = 0; i < getChildCount(); i++) {
2698                View c = getChildAt(i);
2699                Insets insets = c.getOpticalInsets();
2700                drawRect(canvas,
2701                        c.getLeft() + insets.left,
2702                        c.getTop() + insets.top,
2703                        c.getRight() - insets.right,
2704                        c.getBottom() - insets.bottom, Color.RED);
2705            }
2706        }
2707
2708        // Draw margins
2709        onDebugDrawMargins(canvas);
2710
2711        // Draw bounds
2712        for (int i = 0; i < getChildCount(); i++) {
2713            View c = getChildAt(i);
2714            drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), Color.BLUE);
2715        }
2716    }
2717
2718    /**
2719     * {@inheritDoc}
2720     */
2721    @Override
2722    protected void dispatchDraw(Canvas canvas) {
2723        final int count = mChildrenCount;
2724        final View[] children = mChildren;
2725        int flags = mGroupFlags;
2726
2727        if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
2728            final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
2729
2730            final boolean buildCache = !isHardwareAccelerated();
2731            for (int i = 0; i < count; i++) {
2732                final View child = children[i];
2733                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2734                    final LayoutParams params = child.getLayoutParams();
2735                    attachLayoutAnimationParameters(child, params, i, count);
2736                    bindLayoutAnimation(child);
2737                    if (cache) {
2738                        child.setDrawingCacheEnabled(true);
2739                        if (buildCache) {
2740                            child.buildDrawingCache(true);
2741                        }
2742                    }
2743                }
2744            }
2745
2746            final LayoutAnimationController controller = mLayoutAnimationController;
2747            if (controller.willOverlap()) {
2748                mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
2749            }
2750
2751            controller.start();
2752
2753            mGroupFlags &= ~FLAG_RUN_ANIMATION;
2754            mGroupFlags &= ~FLAG_ANIMATION_DONE;
2755
2756            if (cache) {
2757                mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
2758            }
2759
2760            if (mAnimationListener != null) {
2761                mAnimationListener.onAnimationStart(controller.getAnimation());
2762            }
2763        }
2764
2765        int saveCount = 0;
2766        final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
2767        if (clipToPadding) {
2768            saveCount = canvas.save();
2769            canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
2770                    mScrollX + mRight - mLeft - mPaddingRight,
2771                    mScrollY + mBottom - mTop - mPaddingBottom);
2772
2773        }
2774
2775        // We will draw our child's animation, let's reset the flag
2776        mPrivateFlags &= ~DRAW_ANIMATION;
2777        mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
2778
2779        boolean more = false;
2780        final long drawingTime = getDrawingTime();
2781
2782        if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
2783            for (int i = 0; i < count; i++) {
2784                final View child = children[i];
2785                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
2786                    more |= drawChild(canvas, child, drawingTime);
2787                }
2788            }
2789        } else {
2790            for (int i = 0; i < count; i++) {
2791                final View child = children[getChildDrawingOrder(count, i)];
2792                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
2793                    more |= drawChild(canvas, child, drawingTime);
2794                }
2795            }
2796        }
2797
2798        // Draw any disappearing views that have animations
2799        if (mDisappearingChildren != null) {
2800            final ArrayList<View> disappearingChildren = mDisappearingChildren;
2801            final int disappearingCount = disappearingChildren.size() - 1;
2802            // Go backwards -- we may delete as animations finish
2803            for (int i = disappearingCount; i >= 0; i--) {
2804                final View child = disappearingChildren.get(i);
2805                more |= drawChild(canvas, child, drawingTime);
2806            }
2807        }
2808
2809        if (debugDraw()) {
2810            onDebugDraw(canvas);
2811        }
2812
2813        if (clipToPadding) {
2814            canvas.restoreToCount(saveCount);
2815        }
2816
2817        // mGroupFlags might have been updated by drawChild()
2818        flags = mGroupFlags;
2819
2820        if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
2821            invalidate(true);
2822        }
2823
2824        if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
2825                mLayoutAnimationController.isDone() && !more) {
2826            // We want to erase the drawing cache and notify the listener after the
2827            // next frame is drawn because one extra invalidate() is caused by
2828            // drawChild() after the animation is over
2829            mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
2830            final Runnable end = new Runnable() {
2831               public void run() {
2832                   notifyAnimationListener();
2833               }
2834            };
2835            post(end);
2836        }
2837    }
2838
2839    /**
2840     * Returns the index of the child to draw for this iteration. Override this
2841     * if you want to change the drawing order of children. By default, it
2842     * returns i.
2843     * <p>
2844     * NOTE: In order for this method to be called, you must enable child ordering
2845     * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
2846     *
2847     * @param i The current iteration.
2848     * @return The index of the child to draw this iteration.
2849     *
2850     * @see #setChildrenDrawingOrderEnabled(boolean)
2851     * @see #isChildrenDrawingOrderEnabled()
2852     */
2853    protected int getChildDrawingOrder(int childCount, int i) {
2854        return i;
2855    }
2856
2857    private void notifyAnimationListener() {
2858        mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
2859        mGroupFlags |= FLAG_ANIMATION_DONE;
2860
2861        if (mAnimationListener != null) {
2862           final Runnable end = new Runnable() {
2863               public void run() {
2864                   mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
2865               }
2866           };
2867           post(end);
2868        }
2869
2870        if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2871            mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
2872            if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
2873                setChildrenDrawingCacheEnabled(false);
2874            }
2875        }
2876
2877        invalidate(true);
2878    }
2879
2880    /**
2881     * This method is used to cause children of this ViewGroup to restore or recreate their
2882     * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
2883     * to recreate its own display list, which would happen if it went through the normal
2884     * draw/dispatchDraw mechanisms.
2885     *
2886     * @hide
2887     */
2888    @Override
2889    protected void dispatchGetDisplayList() {
2890        final int count = mChildrenCount;
2891        final View[] children = mChildren;
2892        for (int i = 0; i < count; i++) {
2893            final View child = children[i];
2894            if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) &&
2895                    child.hasStaticLayer()) {
2896                child.mRecreateDisplayList = (child.mPrivateFlags & INVALIDATED) == INVALIDATED;
2897                child.mPrivateFlags &= ~INVALIDATED;
2898                child.getDisplayList();
2899                child.mRecreateDisplayList = false;
2900            }
2901        }
2902    }
2903
2904    /**
2905     * Draw one child of this View Group. This method is responsible for getting
2906     * the canvas in the right state. This includes clipping, translating so
2907     * that the child's scrolled origin is at 0, 0, and applying any animation
2908     * transformations.
2909     *
2910     * @param canvas The canvas on which to draw the child
2911     * @param child Who to draw
2912     * @param drawingTime The time at which draw is occurring
2913     * @return True if an invalidate() was issued
2914     */
2915    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
2916        return child.draw(canvas, this, drawingTime);
2917    }
2918
2919    /**
2920     *
2921     * @param enabled True if children should be drawn with layers, false otherwise.
2922     *
2923     * @hide
2924     */
2925    public void setChildrenLayersEnabled(boolean enabled) {
2926        if (enabled != mDrawLayers) {
2927            mDrawLayers = enabled;
2928            invalidate(true);
2929
2930            boolean flushLayers = !enabled;
2931            AttachInfo info = mAttachInfo;
2932            if (info != null && info.mHardwareRenderer != null &&
2933                    info.mHardwareRenderer.isEnabled()) {
2934                if (!info.mHardwareRenderer.validate()) {
2935                    flushLayers = false;
2936                }
2937            } else {
2938                flushLayers = false;
2939            }
2940
2941            // We need to invalidate any child with a layer. For instance,
2942            // if a child is backed by a hardware layer and we disable layers
2943            // the child is marked as not dirty (flags cleared the last time
2944            // the child was drawn inside its layer.) However, that child might
2945            // never have created its own display list or have an obsolete
2946            // display list. By invalidating the child we ensure the display
2947            // list is in sync with the content of the hardware layer.
2948            for (int i = 0; i < mChildrenCount; i++) {
2949                View child = mChildren[i];
2950                if (child.mLayerType != LAYER_TYPE_NONE) {
2951                    if (flushLayers) child.flushLayer();
2952                    child.invalidate(true);
2953                }
2954            }
2955        }
2956    }
2957
2958    /**
2959     * By default, children are clipped to their bounds before drawing. This
2960     * allows view groups to override this behavior for animations, etc.
2961     *
2962     * @param clipChildren true to clip children to their bounds,
2963     *        false otherwise
2964     * @attr ref android.R.styleable#ViewGroup_clipChildren
2965     */
2966    public void setClipChildren(boolean clipChildren) {
2967        boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
2968        if (clipChildren != previousValue) {
2969            setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
2970            for (int i = 0; i < mChildrenCount; ++i) {
2971                View child = getChildAt(i);
2972                if (child.mDisplayList != null) {
2973                    child.mDisplayList.setClipChildren(clipChildren);
2974                }
2975            }
2976        }
2977    }
2978
2979    /**
2980     * By default, children are clipped to the padding of the ViewGroup. This
2981     * allows view groups to override this behavior
2982     *
2983     * @param clipToPadding true to clip children to the padding of the
2984     *        group, false otherwise
2985     * @attr ref android.R.styleable#ViewGroup_clipToPadding
2986     */
2987    public void setClipToPadding(boolean clipToPadding) {
2988        setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
2989    }
2990
2991    /**
2992     * {@inheritDoc}
2993     */
2994    @Override
2995    public void dispatchSetSelected(boolean selected) {
2996        final View[] children = mChildren;
2997        final int count = mChildrenCount;
2998        for (int i = 0; i < count; i++) {
2999            children[i].setSelected(selected);
3000        }
3001    }
3002
3003    /**
3004     * {@inheritDoc}
3005     */
3006    @Override
3007    public void dispatchSetActivated(boolean activated) {
3008        final View[] children = mChildren;
3009        final int count = mChildrenCount;
3010        for (int i = 0; i < count; i++) {
3011            children[i].setActivated(activated);
3012        }
3013    }
3014
3015    @Override
3016    protected void dispatchSetPressed(boolean pressed) {
3017        final View[] children = mChildren;
3018        final int count = mChildrenCount;
3019        for (int i = 0; i < count; i++) {
3020            final View child = children[i];
3021            // Children that are clickable on their own should not
3022            // show a pressed state when their parent view does.
3023            // Clearing a pressed state always propagates.
3024            if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
3025                child.setPressed(pressed);
3026            }
3027        }
3028    }
3029
3030    /**
3031     * When this property is set to true, this ViewGroup supports static transformations on
3032     * children; this causes
3033     * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3034     * invoked when a child is drawn.
3035     *
3036     * Any subclass overriding
3037     * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3038     * set this property to true.
3039     *
3040     * @param enabled True to enable static transformations on children, false otherwise.
3041     *
3042     * @see #FLAG_SUPPORT_STATIC_TRANSFORMATIONS
3043     */
3044    protected void setStaticTransformationsEnabled(boolean enabled) {
3045        setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3046    }
3047
3048    /**
3049     * Sets  <code>t</code> to be the static transformation of the child, if set, returning a
3050     * boolean to indicate whether a static transform was set. The default implementation
3051     * simply returns <code>false</code>; subclasses may override this method for different
3052     * behavior.
3053     *
3054     * @param child The child view whose static transform is being requested
3055     * @param t The Transformation which will hold the result
3056     * @return true if the transformation was set, false otherwise
3057     * @see #setStaticTransformationsEnabled(boolean)
3058     */
3059    protected boolean getChildStaticTransformation(View child, Transformation t) {
3060        return false;
3061    }
3062
3063    /**
3064     * {@hide}
3065     */
3066    @Override
3067    protected View findViewTraversal(int id) {
3068        if (id == mID) {
3069            return this;
3070        }
3071
3072        final View[] where = mChildren;
3073        final int len = mChildrenCount;
3074
3075        for (int i = 0; i < len; i++) {
3076            View v = where[i];
3077
3078            if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
3079                v = v.findViewById(id);
3080
3081                if (v != null) {
3082                    return v;
3083                }
3084            }
3085        }
3086
3087        return null;
3088    }
3089
3090    /**
3091     * {@hide}
3092     */
3093    @Override
3094    protected View findViewWithTagTraversal(Object tag) {
3095        if (tag != null && tag.equals(mTag)) {
3096            return this;
3097        }
3098
3099        final View[] where = mChildren;
3100        final int len = mChildrenCount;
3101
3102        for (int i = 0; i < len; i++) {
3103            View v = where[i];
3104
3105            if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
3106                v = v.findViewWithTag(tag);
3107
3108                if (v != null) {
3109                    return v;
3110                }
3111            }
3112        }
3113
3114        return null;
3115    }
3116
3117    /**
3118     * {@hide}
3119     */
3120    @Override
3121    protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
3122        if (predicate.apply(this)) {
3123            return this;
3124        }
3125
3126        final View[] where = mChildren;
3127        final int len = mChildrenCount;
3128
3129        for (int i = 0; i < len; i++) {
3130            View v = where[i];
3131
3132            if (v != childToSkip && (v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
3133                v = v.findViewByPredicate(predicate);
3134
3135                if (v != null) {
3136                    return v;
3137                }
3138            }
3139        }
3140
3141        return null;
3142    }
3143
3144    /**
3145     * Adds a child view. If no layout parameters are already set on the child, the
3146     * default parameters for this ViewGroup are set on the child.
3147     *
3148     * @param child the child view to add
3149     *
3150     * @see #generateDefaultLayoutParams()
3151     */
3152    public void addView(View child) {
3153        addView(child, -1);
3154    }
3155
3156    /**
3157     * Adds a child view. If no layout parameters are already set on the child, the
3158     * default parameters for this ViewGroup are set on the child.
3159     *
3160     * @param child the child view to add
3161     * @param index the position at which to add the child
3162     *
3163     * @see #generateDefaultLayoutParams()
3164     */
3165    public void addView(View child, int index) {
3166        LayoutParams params = child.getLayoutParams();
3167        if (params == null) {
3168            params = generateDefaultLayoutParams();
3169            if (params == null) {
3170                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
3171            }
3172        }
3173        addView(child, index, params);
3174    }
3175
3176    /**
3177     * Adds a child view with this ViewGroup's default layout parameters and the
3178     * specified width and height.
3179     *
3180     * @param child the child view to add
3181     */
3182    public void addView(View child, int width, int height) {
3183        final LayoutParams params = generateDefaultLayoutParams();
3184        params.width = width;
3185        params.height = height;
3186        addView(child, -1, params);
3187    }
3188
3189    /**
3190     * Adds a child view with the specified layout parameters.
3191     *
3192     * @param child the child view to add
3193     * @param params the layout parameters to set on the child
3194     */
3195    public void addView(View child, LayoutParams params) {
3196        addView(child, -1, params);
3197    }
3198
3199    /**
3200     * Adds a child view with the specified layout parameters.
3201     *
3202     * @param child the child view to add
3203     * @param index the position at which to add the child
3204     * @param params the layout parameters to set on the child
3205     */
3206    public void addView(View child, int index, LayoutParams params) {
3207        if (DBG) {
3208            System.out.println(this + " addView");
3209        }
3210
3211        // addViewInner() will call child.requestLayout() when setting the new LayoutParams
3212        // therefore, we call requestLayout() on ourselves before, so that the child's request
3213        // will be blocked at our level
3214        requestLayout();
3215        invalidate(true);
3216        addViewInner(child, index, params, false);
3217    }
3218
3219    /**
3220     * {@inheritDoc}
3221     */
3222    public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
3223        if (!checkLayoutParams(params)) {
3224            throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
3225        }
3226        if (view.mParent != this) {
3227            throw new IllegalArgumentException("Given view not a child of " + this);
3228        }
3229        view.setLayoutParams(params);
3230    }
3231
3232    /**
3233     * {@inheritDoc}
3234     */
3235    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3236        return  p != null;
3237    }
3238
3239    /**
3240     * Interface definition for a callback to be invoked when the hierarchy
3241     * within this view changed. The hierarchy changes whenever a child is added
3242     * to or removed from this view.
3243     */
3244    public interface OnHierarchyChangeListener {
3245        /**
3246         * Called when a new child is added to a parent view.
3247         *
3248         * @param parent the view in which a child was added
3249         * @param child the new child view added in the hierarchy
3250         */
3251        void onChildViewAdded(View parent, View child);
3252
3253        /**
3254         * Called when a child is removed from a parent view.
3255         *
3256         * @param parent the view from which the child was removed
3257         * @param child the child removed from the hierarchy
3258         */
3259        void onChildViewRemoved(View parent, View child);
3260    }
3261
3262    /**
3263     * Register a callback to be invoked when a child is added to or removed
3264     * from this view.
3265     *
3266     * @param listener the callback to invoke on hierarchy change
3267     */
3268    public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
3269        mOnHierarchyChangeListener = listener;
3270    }
3271
3272    /**
3273     * @hide
3274     */
3275    protected void onViewAdded(View child) {
3276        if (mOnHierarchyChangeListener != null) {
3277            mOnHierarchyChangeListener.onChildViewAdded(this, child);
3278        }
3279    }
3280
3281    /**
3282     * @hide
3283     */
3284    protected void onViewRemoved(View child) {
3285        if (mOnHierarchyChangeListener != null) {
3286            mOnHierarchyChangeListener.onChildViewRemoved(this, child);
3287        }
3288    }
3289
3290    /**
3291     * Adds a view during layout. This is useful if in your onLayout() method,
3292     * you need to add more views (as does the list view for example).
3293     *
3294     * If index is negative, it means put it at the end of the list.
3295     *
3296     * @param child the view to add to the group
3297     * @param index the index at which the child must be added
3298     * @param params the layout parameters to associate with the child
3299     * @return true if the child was added, false otherwise
3300     */
3301    protected boolean addViewInLayout(View child, int index, LayoutParams params) {
3302        return addViewInLayout(child, index, params, false);
3303    }
3304
3305    /**
3306     * Adds a view during layout. This is useful if in your onLayout() method,
3307     * you need to add more views (as does the list view for example).
3308     *
3309     * If index is negative, it means put it at the end of the list.
3310     *
3311     * @param child the view to add to the group
3312     * @param index the index at which the child must be added
3313     * @param params the layout parameters to associate with the child
3314     * @param preventRequestLayout if true, calling this method will not trigger a
3315     *        layout request on child
3316     * @return true if the child was added, false otherwise
3317     */
3318    protected boolean addViewInLayout(View child, int index, LayoutParams params,
3319            boolean preventRequestLayout) {
3320        child.mParent = null;
3321        addViewInner(child, index, params, preventRequestLayout);
3322        child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK) | DRAWN;
3323        return true;
3324    }
3325
3326    /**
3327     * Prevents the specified child to be laid out during the next layout pass.
3328     *
3329     * @param child the child on which to perform the cleanup
3330     */
3331    protected void cleanupLayoutState(View child) {
3332        child.mPrivateFlags &= ~View.FORCE_LAYOUT;
3333    }
3334
3335    private void addViewInner(View child, int index, LayoutParams params,
3336            boolean preventRequestLayout) {
3337
3338        if (mTransition != null) {
3339            // Don't prevent other add transitions from completing, but cancel remove
3340            // transitions to let them complete the process before we add to the container
3341            mTransition.cancel(LayoutTransition.DISAPPEARING);
3342        }
3343
3344        if (child.getParent() != null) {
3345            throw new IllegalStateException("The specified child already has a parent. " +
3346                    "You must call removeView() on the child's parent first.");
3347        }
3348
3349        if (mTransition != null) {
3350            mTransition.addChild(this, child);
3351        }
3352
3353        if (!checkLayoutParams(params)) {
3354            params = generateLayoutParams(params);
3355        }
3356
3357        if (preventRequestLayout) {
3358            child.mLayoutParams = params;
3359        } else {
3360            child.setLayoutParams(params);
3361        }
3362
3363        if (index < 0) {
3364            index = mChildrenCount;
3365        }
3366
3367        addInArray(child, index);
3368
3369        // tell our children
3370        if (preventRequestLayout) {
3371            child.assignParent(this);
3372        } else {
3373            child.mParent = this;
3374        }
3375
3376        if (child.hasFocus()) {
3377            requestChildFocus(child, child.findFocus());
3378        }
3379
3380        AttachInfo ai = mAttachInfo;
3381        if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
3382            boolean lastKeepOn = ai.mKeepScreenOn;
3383            ai.mKeepScreenOn = false;
3384            child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
3385            if (ai.mKeepScreenOn) {
3386                needGlobalAttributesUpdate(true);
3387            }
3388            ai.mKeepScreenOn = lastKeepOn;
3389        }
3390
3391        onViewAdded(child);
3392
3393        if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
3394            mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
3395        }
3396
3397        if (child.hasTransientState()) {
3398            childHasTransientStateChanged(child, true);
3399        }
3400    }
3401
3402    private void addInArray(View child, int index) {
3403        View[] children = mChildren;
3404        final int count = mChildrenCount;
3405        final int size = children.length;
3406        if (index == count) {
3407            if (size == count) {
3408                mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
3409                System.arraycopy(children, 0, mChildren, 0, size);
3410                children = mChildren;
3411            }
3412            children[mChildrenCount++] = child;
3413        } else if (index < count) {
3414            if (size == count) {
3415                mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
3416                System.arraycopy(children, 0, mChildren, 0, index);
3417                System.arraycopy(children, index, mChildren, index + 1, count - index);
3418                children = mChildren;
3419            } else {
3420                System.arraycopy(children, index, children, index + 1, count - index);
3421            }
3422            children[index] = child;
3423            mChildrenCount++;
3424            if (mLastTouchDownIndex >= index) {
3425                mLastTouchDownIndex++;
3426            }
3427        } else {
3428            throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
3429        }
3430    }
3431
3432    // This method also sets the child's mParent to null
3433    private void removeFromArray(int index) {
3434        final View[] children = mChildren;
3435        if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
3436            children[index].mParent = null;
3437        }
3438        final int count = mChildrenCount;
3439        if (index == count - 1) {
3440            children[--mChildrenCount] = null;
3441        } else if (index >= 0 && index < count) {
3442            System.arraycopy(children, index + 1, children, index, count - index - 1);
3443            children[--mChildrenCount] = null;
3444        } else {
3445            throw new IndexOutOfBoundsException();
3446        }
3447        if (mLastTouchDownIndex == index) {
3448            mLastTouchDownTime = 0;
3449            mLastTouchDownIndex = -1;
3450        } else if (mLastTouchDownIndex > index) {
3451            mLastTouchDownIndex--;
3452        }
3453    }
3454
3455    // This method also sets the children's mParent to null
3456    private void removeFromArray(int start, int count) {
3457        final View[] children = mChildren;
3458        final int childrenCount = mChildrenCount;
3459
3460        start = Math.max(0, start);
3461        final int end = Math.min(childrenCount, start + count);
3462
3463        if (start == end) {
3464            return;
3465        }
3466
3467        if (end == childrenCount) {
3468            for (int i = start; i < end; i++) {
3469                children[i].mParent = null;
3470                children[i] = null;
3471            }
3472        } else {
3473            for (int i = start; i < end; i++) {
3474                children[i].mParent = null;
3475            }
3476
3477            // Since we're looping above, we might as well do the copy, but is arraycopy()
3478            // faster than the extra 2 bounds checks we would do in the loop?
3479            System.arraycopy(children, end, children, start, childrenCount - end);
3480
3481            for (int i = childrenCount - (end - start); i < childrenCount; i++) {
3482                children[i] = null;
3483            }
3484        }
3485
3486        mChildrenCount -= (end - start);
3487    }
3488
3489    private void bindLayoutAnimation(View child) {
3490        Animation a = mLayoutAnimationController.getAnimationForView(child);
3491        child.setAnimation(a);
3492    }
3493
3494    /**
3495     * Subclasses should override this method to set layout animation
3496     * parameters on the supplied child.
3497     *
3498     * @param child the child to associate with animation parameters
3499     * @param params the child's layout parameters which hold the animation
3500     *        parameters
3501     * @param index the index of the child in the view group
3502     * @param count the number of children in the view group
3503     */
3504    protected void attachLayoutAnimationParameters(View child,
3505            LayoutParams params, int index, int count) {
3506        LayoutAnimationController.AnimationParameters animationParams =
3507                    params.layoutAnimationParameters;
3508        if (animationParams == null) {
3509            animationParams = new LayoutAnimationController.AnimationParameters();
3510            params.layoutAnimationParameters = animationParams;
3511        }
3512
3513        animationParams.count = count;
3514        animationParams.index = index;
3515    }
3516
3517    /**
3518     * {@inheritDoc}
3519     */
3520    public void removeView(View view) {
3521        removeViewInternal(view);
3522        requestLayout();
3523        invalidate(true);
3524    }
3525
3526    /**
3527     * Removes a view during layout. This is useful if in your onLayout() method,
3528     * you need to remove more views.
3529     *
3530     * @param view the view to remove from the group
3531     */
3532    public void removeViewInLayout(View view) {
3533        removeViewInternal(view);
3534    }
3535
3536    /**
3537     * Removes a range of views during layout. This is useful if in your onLayout() method,
3538     * you need to remove more views.
3539     *
3540     * @param start the index of the first view to remove from the group
3541     * @param count the number of views to remove from the group
3542     */
3543    public void removeViewsInLayout(int start, int count) {
3544        removeViewsInternal(start, count);
3545    }
3546
3547    /**
3548     * Removes the view at the specified position in the group.
3549     *
3550     * @param index the position in the group of the view to remove
3551     */
3552    public void removeViewAt(int index) {
3553        removeViewInternal(index, getChildAt(index));
3554        requestLayout();
3555        invalidate(true);
3556    }
3557
3558    /**
3559     * Removes the specified range of views from the group.
3560     *
3561     * @param start the first position in the group of the range of views to remove
3562     * @param count the number of views to remove
3563     */
3564    public void removeViews(int start, int count) {
3565        removeViewsInternal(start, count);
3566        requestLayout();
3567        invalidate(true);
3568    }
3569
3570    private void removeViewInternal(View view) {
3571        final int index = indexOfChild(view);
3572        if (index >= 0) {
3573            removeViewInternal(index, view);
3574        }
3575    }
3576
3577    private void removeViewInternal(int index, View view) {
3578
3579        if (mTransition != null) {
3580            mTransition.removeChild(this, view);
3581        }
3582
3583        boolean clearChildFocus = false;
3584        if (view == mFocused) {
3585            view.unFocus();
3586            clearChildFocus = true;
3587        }
3588
3589        cancelTouchTarget(view);
3590        cancelHoverTarget(view);
3591
3592        if (view.getAnimation() != null ||
3593                (mTransitioningViews != null && mTransitioningViews.contains(view))) {
3594            addDisappearingView(view);
3595        } else if (view.mAttachInfo != null) {
3596           view.dispatchDetachedFromWindow();
3597        }
3598
3599        if (view.hasTransientState()) {
3600            childHasTransientStateChanged(view, false);
3601        }
3602
3603        onViewRemoved(view);
3604
3605        needGlobalAttributesUpdate(false);
3606
3607        removeFromArray(index);
3608
3609        if (clearChildFocus) {
3610            clearChildFocus(view);
3611            ensureInputFocusOnFirstFocusable();
3612        }
3613
3614        if (view.isAccessibilityFocused()) {
3615            view.clearAccessibilityFocus();
3616        }
3617    }
3618
3619    /**
3620     * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
3621     * not null, changes in layout which occur because of children being added to or removed from
3622     * the ViewGroup will be animated according to the animations defined in that LayoutTransition
3623     * object. By default, the transition object is null (so layout changes are not animated).
3624     *
3625     * @param transition The LayoutTransition object that will animated changes in layout. A value
3626     * of <code>null</code> means no transition will run on layout changes.
3627     * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
3628     */
3629    public void setLayoutTransition(LayoutTransition transition) {
3630        if (mTransition != null) {
3631            mTransition.removeTransitionListener(mLayoutTransitionListener);
3632        }
3633        mTransition = transition;
3634        if (mTransition != null) {
3635            mTransition.addTransitionListener(mLayoutTransitionListener);
3636        }
3637    }
3638
3639    /**
3640     * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
3641     * not null, changes in layout which occur because of children being added to or removed from
3642     * the ViewGroup will be animated according to the animations defined in that LayoutTransition
3643     * object. By default, the transition object is null (so layout changes are not animated).
3644     *
3645     * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
3646     * A value of <code>null</code> means no transition will run on layout changes.
3647     */
3648    public LayoutTransition getLayoutTransition() {
3649        return mTransition;
3650    }
3651
3652    private void removeViewsInternal(int start, int count) {
3653        final View focused = mFocused;
3654        final boolean detach = mAttachInfo != null;
3655        View clearChildFocus = null;
3656
3657        final View[] children = mChildren;
3658        final int end = start + count;
3659
3660        for (int i = start; i < end; i++) {
3661            final View view = children[i];
3662
3663            if (mTransition != null) {
3664                mTransition.removeChild(this, view);
3665            }
3666
3667            if (view == focused) {
3668                view.unFocus();
3669                clearChildFocus = view;
3670            }
3671
3672            cancelTouchTarget(view);
3673            cancelHoverTarget(view);
3674
3675            if (view.getAnimation() != null ||
3676                (mTransitioningViews != null && mTransitioningViews.contains(view))) {
3677                addDisappearingView(view);
3678            } else if (detach) {
3679               view.dispatchDetachedFromWindow();
3680            }
3681
3682            if (view.hasTransientState()) {
3683                childHasTransientStateChanged(view, false);
3684            }
3685
3686            needGlobalAttributesUpdate(false);
3687
3688            onViewRemoved(view);
3689        }
3690
3691        removeFromArray(start, count);
3692
3693        if (clearChildFocus != null) {
3694            clearChildFocus(clearChildFocus);
3695            ensureInputFocusOnFirstFocusable();
3696        }
3697    }
3698
3699    /**
3700     * Call this method to remove all child views from the
3701     * ViewGroup.
3702     */
3703    public void removeAllViews() {
3704        removeAllViewsInLayout();
3705        requestLayout();
3706        invalidate(true);
3707    }
3708
3709    /**
3710     * Called by a ViewGroup subclass to remove child views from itself,
3711     * when it must first know its size on screen before it can calculate how many
3712     * child views it will render. An example is a Gallery or a ListView, which
3713     * may "have" 50 children, but actually only render the number of children
3714     * that can currently fit inside the object on screen. Do not call
3715     * this method unless you are extending ViewGroup and understand the
3716     * view measuring and layout pipeline.
3717     */
3718    public void removeAllViewsInLayout() {
3719        final int count = mChildrenCount;
3720        if (count <= 0) {
3721            return;
3722        }
3723
3724        final View[] children = mChildren;
3725        mChildrenCount = 0;
3726
3727        final View focused = mFocused;
3728        final boolean detach = mAttachInfo != null;
3729        View clearChildFocus = null;
3730
3731        needGlobalAttributesUpdate(false);
3732
3733        for (int i = count - 1; i >= 0; i--) {
3734            final View view = children[i];
3735
3736            if (mTransition != null) {
3737                mTransition.removeChild(this, view);
3738            }
3739
3740            if (view == focused) {
3741                view.unFocus();
3742                clearChildFocus = view;
3743            }
3744
3745            cancelTouchTarget(view);
3746            cancelHoverTarget(view);
3747
3748            if (view.getAnimation() != null ||
3749                    (mTransitioningViews != null && mTransitioningViews.contains(view))) {
3750                addDisappearingView(view);
3751            } else if (detach) {
3752               view.dispatchDetachedFromWindow();
3753            }
3754
3755            if (view.hasTransientState()) {
3756                childHasTransientStateChanged(view, false);
3757            }
3758
3759            onViewRemoved(view);
3760
3761            view.mParent = null;
3762            children[i] = null;
3763        }
3764
3765        if (clearChildFocus != null) {
3766            clearChildFocus(clearChildFocus);
3767            ensureInputFocusOnFirstFocusable();
3768        }
3769    }
3770
3771    /**
3772     * Finishes the removal of a detached view. This method will dispatch the detached from
3773     * window event and notify the hierarchy change listener.
3774     *
3775     * @param child the child to be definitely removed from the view hierarchy
3776     * @param animate if true and the view has an animation, the view is placed in the
3777     *                disappearing views list, otherwise, it is detached from the window
3778     *
3779     * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3780     * @see #detachAllViewsFromParent()
3781     * @see #detachViewFromParent(View)
3782     * @see #detachViewFromParent(int)
3783     */
3784    protected void removeDetachedView(View child, boolean animate) {
3785        if (mTransition != null) {
3786            mTransition.removeChild(this, child);
3787        }
3788
3789        if (child == mFocused) {
3790            child.clearFocus();
3791        }
3792
3793        cancelTouchTarget(child);
3794        cancelHoverTarget(child);
3795
3796        if ((animate && child.getAnimation() != null) ||
3797                (mTransitioningViews != null && mTransitioningViews.contains(child))) {
3798            addDisappearingView(child);
3799        } else if (child.mAttachInfo != null) {
3800            child.dispatchDetachedFromWindow();
3801        }
3802
3803        if (child.hasTransientState()) {
3804            childHasTransientStateChanged(child, false);
3805        }
3806
3807        onViewRemoved(child);
3808    }
3809
3810    /**
3811     * Attaches a view to this view group. Attaching a view assigns this group as the parent,
3812     * sets the layout parameters and puts the view in the list of children so it can be retrieved
3813     * by calling {@link #getChildAt(int)}.
3814     *
3815     * This method should be called only for view which were detached from their parent.
3816     *
3817     * @param child the child to attach
3818     * @param index the index at which the child should be attached
3819     * @param params the layout parameters of the child
3820     *
3821     * @see #removeDetachedView(View, boolean)
3822     * @see #detachAllViewsFromParent()
3823     * @see #detachViewFromParent(View)
3824     * @see #detachViewFromParent(int)
3825     */
3826    protected void attachViewToParent(View child, int index, LayoutParams params) {
3827        child.mLayoutParams = params;
3828
3829        if (index < 0) {
3830            index = mChildrenCount;
3831        }
3832
3833        addInArray(child, index);
3834
3835        child.mParent = this;
3836        child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK & ~DRAWING_CACHE_VALID) |
3837                DRAWN | INVALIDATED;
3838        this.mPrivateFlags |= INVALIDATED;
3839
3840        if (child.hasFocus()) {
3841            requestChildFocus(child, child.findFocus());
3842        }
3843    }
3844
3845    /**
3846     * Detaches a view from its parent. Detaching a view should be temporary and followed
3847     * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3848     * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3849     * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3850     *
3851     * @param child the child to detach
3852     *
3853     * @see #detachViewFromParent(int)
3854     * @see #detachViewsFromParent(int, int)
3855     * @see #detachAllViewsFromParent()
3856     * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3857     * @see #removeDetachedView(View, boolean)
3858     */
3859    protected void detachViewFromParent(View child) {
3860        removeFromArray(indexOfChild(child));
3861    }
3862
3863    /**
3864     * Detaches a view from its parent. Detaching a view should be temporary and followed
3865     * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3866     * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3867     * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3868     *
3869     * @param index the index of the child to detach
3870     *
3871     * @see #detachViewFromParent(View)
3872     * @see #detachAllViewsFromParent()
3873     * @see #detachViewsFromParent(int, int)
3874     * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3875     * @see #removeDetachedView(View, boolean)
3876     */
3877    protected void detachViewFromParent(int index) {
3878        removeFromArray(index);
3879    }
3880
3881    /**
3882     * Detaches a range of view from their parent. Detaching a view should be temporary and followed
3883     * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3884     * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, its
3885     * parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3886     *
3887     * @param start the first index of the childrend range to detach
3888     * @param count the number of children to detach
3889     *
3890     * @see #detachViewFromParent(View)
3891     * @see #detachViewFromParent(int)
3892     * @see #detachAllViewsFromParent()
3893     * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3894     * @see #removeDetachedView(View, boolean)
3895     */
3896    protected void detachViewsFromParent(int start, int count) {
3897        removeFromArray(start, count);
3898    }
3899
3900    /**
3901     * Detaches all views from the parent. Detaching a view should be temporary and followed
3902     * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3903     * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3904     * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3905     *
3906     * @see #detachViewFromParent(View)
3907     * @see #detachViewFromParent(int)
3908     * @see #detachViewsFromParent(int, int)
3909     * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3910     * @see #removeDetachedView(View, boolean)
3911     */
3912    protected void detachAllViewsFromParent() {
3913        final int count = mChildrenCount;
3914        if (count <= 0) {
3915            return;
3916        }
3917
3918        final View[] children = mChildren;
3919        mChildrenCount = 0;
3920
3921        for (int i = count - 1; i >= 0; i--) {
3922            children[i].mParent = null;
3923            children[i] = null;
3924        }
3925    }
3926
3927    /**
3928     * Don't call or override this method. It is used for the implementation of
3929     * the view hierarchy.
3930     */
3931    public final void invalidateChild(View child, final Rect dirty) {
3932        if (ViewDebug.TRACE_HIERARCHY) {
3933            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
3934        }
3935
3936        ViewParent parent = this;
3937
3938        final AttachInfo attachInfo = mAttachInfo;
3939        if (attachInfo != null) {
3940            // If the child is drawing an animation, we want to copy this flag onto
3941            // ourselves and the parent to make sure the invalidate request goes
3942            // through
3943            final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
3944
3945            // Check whether the child that requests the invalidate is fully opaque
3946            // Views being animated or transformed are not considered opaque because we may
3947            // be invalidating their old position and need the parent to paint behind them.
3948            Matrix childMatrix = child.getMatrix();
3949            final boolean isOpaque = child.isOpaque() && !drawAnimation &&
3950                    child.getAnimation() == null && childMatrix.isIdentity();
3951            // Mark the child as dirty, using the appropriate flag
3952            // Make sure we do not set both flags at the same time
3953            int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
3954
3955            if (child.mLayerType != LAYER_TYPE_NONE) {
3956                mPrivateFlags |= INVALIDATED;
3957                mPrivateFlags &= ~DRAWING_CACHE_VALID;
3958                child.mLocalDirtyRect.union(dirty);
3959            }
3960
3961            final int[] location = attachInfo.mInvalidateChildLocation;
3962            location[CHILD_LEFT_INDEX] = child.mLeft;
3963            location[CHILD_TOP_INDEX] = child.mTop;
3964            if (!childMatrix.isIdentity()) {
3965                RectF boundingRect = attachInfo.mTmpTransformRect;
3966                boundingRect.set(dirty);
3967                //boundingRect.inset(-0.5f, -0.5f);
3968                childMatrix.mapRect(boundingRect);
3969                dirty.set((int) (boundingRect.left - 0.5f),
3970                        (int) (boundingRect.top - 0.5f),
3971                        (int) (boundingRect.right + 0.5f),
3972                        (int) (boundingRect.bottom + 0.5f));
3973            }
3974
3975            do {
3976                View view = null;
3977                if (parent instanceof View) {
3978                    view = (View) parent;
3979                }
3980
3981                if (drawAnimation) {
3982                    if (view != null) {
3983                        view.mPrivateFlags |= DRAW_ANIMATION;
3984                    } else if (parent instanceof ViewRootImpl) {
3985                        ((ViewRootImpl) parent).mIsAnimating = true;
3986                    }
3987                }
3988
3989                // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
3990                // flag coming from the child that initiated the invalidate
3991                if (view != null) {
3992                    if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
3993                            view.getSolidColor() == 0) {
3994                        opaqueFlag = DIRTY;
3995                    }
3996                    if ((view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
3997                        view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
3998                    }
3999                }
4000
4001                parent = parent.invalidateChildInParent(location, dirty);
4002                if (view != null) {
4003                    // Account for transform on current parent
4004                    Matrix m = view.getMatrix();
4005                    if (!m.isIdentity()) {
4006                        RectF boundingRect = attachInfo.mTmpTransformRect;
4007                        boundingRect.set(dirty);
4008                        m.mapRect(boundingRect);
4009                        dirty.set((int) (boundingRect.left - 0.5f),
4010                                (int) (boundingRect.top - 0.5f),
4011                                (int) (boundingRect.right + 0.5f),
4012                                (int) (boundingRect.bottom + 0.5f));
4013                    }
4014                }
4015            } while (parent != null);
4016        }
4017    }
4018
4019    /**
4020     * Don't call or override this method. It is used for the implementation of
4021     * the view hierarchy.
4022     *
4023     * This implementation returns null if this ViewGroup does not have a parent,
4024     * if this ViewGroup is already fully invalidated or if the dirty rectangle
4025     * does not intersect with this ViewGroup's bounds.
4026     */
4027    public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
4028        if (ViewDebug.TRACE_HIERARCHY) {
4029            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
4030        }
4031
4032        if ((mPrivateFlags & DRAWN) == DRAWN ||
4033                (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
4034            if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
4035                        FLAG_OPTIMIZE_INVALIDATE) {
4036                dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
4037                        location[CHILD_TOP_INDEX] - mScrollY);
4038
4039                final int left = mLeft;
4040                final int top = mTop;
4041
4042                if ((mGroupFlags & FLAG_CLIP_CHILDREN) != FLAG_CLIP_CHILDREN ||
4043                        dirty.intersect(0, 0, mRight - left, mBottom - top) ||
4044                        (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
4045                    mPrivateFlags &= ~DRAWING_CACHE_VALID;
4046
4047                    location[CHILD_LEFT_INDEX] = left;
4048                    location[CHILD_TOP_INDEX] = top;
4049
4050                    if (mLayerType != LAYER_TYPE_NONE) {
4051                        mPrivateFlags |= INVALIDATED;
4052                        mLocalDirtyRect.union(dirty);
4053                    }
4054
4055                    return mParent;
4056                }
4057            } else {
4058                mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
4059
4060                location[CHILD_LEFT_INDEX] = mLeft;
4061                location[CHILD_TOP_INDEX] = mTop;
4062                if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
4063                    dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
4064                } else {
4065                    // in case the dirty rect extends outside the bounds of this container
4066                    dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
4067                }
4068
4069                if (mLayerType != LAYER_TYPE_NONE) {
4070                    mPrivateFlags |= INVALIDATED;
4071                    mLocalDirtyRect.union(dirty);
4072                }
4073
4074                return mParent;
4075            }
4076        }
4077
4078        return null;
4079    }
4080
4081    /**
4082     * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the
4083     * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods
4084     * do; all we want to do here is schedule a traversal with the appropriate dirty rect.
4085     *
4086     * @hide
4087     */
4088    public void invalidateChildFast(View child, final Rect dirty) {
4089        ViewParent parent = this;
4090
4091        final AttachInfo attachInfo = mAttachInfo;
4092        if (attachInfo != null) {
4093            if (child.mLayerType != LAYER_TYPE_NONE) {
4094                child.mLocalDirtyRect.union(dirty);
4095            }
4096
4097            int left = child.mLeft;
4098            int top = child.mTop;
4099            if (!child.getMatrix().isIdentity()) {
4100                child.transformRect(dirty);
4101            }
4102
4103            do {
4104                if (parent instanceof ViewGroup) {
4105                    ViewGroup parentVG = (ViewGroup) parent;
4106                    if (parentVG.mLayerType != LAYER_TYPE_NONE) {
4107                        // Layered parents should be recreated, not just re-issued
4108                        parentVG.invalidate();
4109                        parent = null;
4110                    } else {
4111                        parent = parentVG.invalidateChildInParentFast(left, top, dirty);
4112                        left = parentVG.mLeft;
4113                        top = parentVG.mTop;
4114                    }
4115                } else {
4116                    // Reached the top; this calls into the usual invalidate method in
4117                    // ViewRootImpl, which schedules a traversal
4118                    final int[] location = attachInfo.mInvalidateChildLocation;
4119                    location[0] = left;
4120                    location[1] = top;
4121                    parent = parent.invalidateChildInParent(location, dirty);
4122                }
4123            } while (parent != null);
4124        }
4125    }
4126
4127    /**
4128     * Quick invalidation method that simply transforms the dirty rect into the parent's
4129     * coordinate system, pruning the invalidation if the parent has already been invalidated.
4130     */
4131    private ViewParent invalidateChildInParentFast(int left, int top, final Rect dirty) {
4132        if ((mPrivateFlags & DRAWN) == DRAWN ||
4133                (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
4134            dirty.offset(left - mScrollX, top - mScrollY);
4135
4136            if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 ||
4137                    dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
4138
4139                if (mLayerType != LAYER_TYPE_NONE) {
4140                    mLocalDirtyRect.union(dirty);
4141                }
4142                if (!getMatrix().isIdentity()) {
4143                    transformRect(dirty);
4144                }
4145
4146                return mParent;
4147            }
4148        }
4149
4150        return null;
4151    }
4152
4153    /**
4154     * Offset a rectangle that is in a descendant's coordinate
4155     * space into our coordinate space.
4156     * @param descendant A descendant of this view
4157     * @param rect A rectangle defined in descendant's coordinate space.
4158     */
4159    public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
4160        offsetRectBetweenParentAndChild(descendant, rect, true, false);
4161    }
4162
4163    /**
4164     * Offset a rectangle that is in our coordinate space into an ancestor's
4165     * coordinate space.
4166     * @param descendant A descendant of this view
4167     * @param rect A rectangle defined in descendant's coordinate space.
4168     */
4169    public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
4170        offsetRectBetweenParentAndChild(descendant, rect, false, false);
4171    }
4172
4173    /**
4174     * Helper method that offsets a rect either from parent to descendant or
4175     * descendant to parent.
4176     */
4177    void offsetRectBetweenParentAndChild(View descendant, Rect rect,
4178            boolean offsetFromChildToParent, boolean clipToBounds) {
4179
4180        // already in the same coord system :)
4181        if (descendant == this) {
4182            return;
4183        }
4184
4185        ViewParent theParent = descendant.mParent;
4186
4187        // search and offset up to the parent
4188        while ((theParent != null)
4189                && (theParent instanceof View)
4190                && (theParent != this)) {
4191
4192            if (offsetFromChildToParent) {
4193                rect.offset(descendant.mLeft - descendant.mScrollX,
4194                        descendant.mTop - descendant.mScrollY);
4195                if (clipToBounds) {
4196                    View p = (View) theParent;
4197                    rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
4198                }
4199            } else {
4200                if (clipToBounds) {
4201                    View p = (View) theParent;
4202                    rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
4203                }
4204                rect.offset(descendant.mScrollX - descendant.mLeft,
4205                        descendant.mScrollY - descendant.mTop);
4206            }
4207
4208            descendant = (View) theParent;
4209            theParent = descendant.mParent;
4210        }
4211
4212        // now that we are up to this view, need to offset one more time
4213        // to get into our coordinate space
4214        if (theParent == this) {
4215            if (offsetFromChildToParent) {
4216                rect.offset(descendant.mLeft - descendant.mScrollX,
4217                        descendant.mTop - descendant.mScrollY);
4218            } else {
4219                rect.offset(descendant.mScrollX - descendant.mLeft,
4220                        descendant.mScrollY - descendant.mTop);
4221            }
4222        } else {
4223            throw new IllegalArgumentException("parameter must be a descendant of this view");
4224        }
4225    }
4226
4227    /**
4228     * Offset the vertical location of all children of this view by the specified number of pixels.
4229     *
4230     * @param offset the number of pixels to offset
4231     *
4232     * @hide
4233     */
4234    public void offsetChildrenTopAndBottom(int offset) {
4235        final int count = mChildrenCount;
4236        final View[] children = mChildren;
4237
4238        for (int i = 0; i < count; i++) {
4239            final View v = children[i];
4240            v.mTop += offset;
4241            v.mBottom += offset;
4242            if (v.mDisplayList != null) {
4243                v.mDisplayList.offsetTopBottom(offset);
4244                invalidateViewProperty(false, false);
4245            }
4246        }
4247    }
4248
4249    /**
4250     * {@inheritDoc}
4251     */
4252    public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
4253        // It doesn't make a whole lot of sense to call this on a view that isn't attached,
4254        // but for some simple tests it can be useful. If we don't have attach info this
4255        // will allocate memory.
4256        final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
4257        rect.set(r);
4258
4259        if (!child.hasIdentityMatrix()) {
4260           child.getMatrix().mapRect(rect);
4261        }
4262
4263        int dx = child.mLeft - mScrollX;
4264        int dy = child.mTop - mScrollY;
4265
4266        rect.offset(dx, dy);
4267
4268        if (offset != null) {
4269            if (!child.hasIdentityMatrix()) {
4270                float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
4271                        : new float[2];
4272                position[0] = offset.x;
4273                position[1] = offset.y;
4274                child.getMatrix().mapPoints(position);
4275                offset.x = (int) (position[0] + 0.5f);
4276                offset.y = (int) (position[1] + 0.5f);
4277            }
4278            offset.x += dx;
4279            offset.y += dy;
4280        }
4281
4282        if (rect.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
4283            if (mParent == null) return true;
4284            r.set((int) (rect.left + 0.5f), (int) (rect.top + 0.5f),
4285                    (int) (rect.right + 0.5f), (int) (rect.bottom + 0.5f));
4286            return mParent.getChildVisibleRect(this, r, offset);
4287        }
4288
4289        return false;
4290    }
4291
4292    /**
4293     * {@inheritDoc}
4294     */
4295    @Override
4296    public final void layout(int l, int t, int r, int b) {
4297        if (mTransition == null || !mTransition.isChangingLayout()) {
4298            if (mTransition != null) {
4299                mTransition.layoutChange(this);
4300            }
4301            super.layout(l, t, r, b);
4302        } else {
4303            // record the fact that we noop'd it; request layout when transition finishes
4304            mLayoutSuppressed = true;
4305        }
4306    }
4307
4308    /**
4309     * {@inheritDoc}
4310     */
4311    @Override
4312    protected abstract void onLayout(boolean changed,
4313            int l, int t, int r, int b);
4314
4315    /**
4316     * Indicates whether the view group has the ability to animate its children
4317     * after the first layout.
4318     *
4319     * @return true if the children can be animated, false otherwise
4320     */
4321    protected boolean canAnimate() {
4322        return mLayoutAnimationController != null;
4323    }
4324
4325    /**
4326     * Runs the layout animation. Calling this method triggers a relayout of
4327     * this view group.
4328     */
4329    public void startLayoutAnimation() {
4330        if (mLayoutAnimationController != null) {
4331            mGroupFlags |= FLAG_RUN_ANIMATION;
4332            requestLayout();
4333        }
4334    }
4335
4336    /**
4337     * Schedules the layout animation to be played after the next layout pass
4338     * of this view group. This can be used to restart the layout animation
4339     * when the content of the view group changes or when the activity is
4340     * paused and resumed.
4341     */
4342    public void scheduleLayoutAnimation() {
4343        mGroupFlags |= FLAG_RUN_ANIMATION;
4344    }
4345
4346    /**
4347     * Sets the layout animation controller used to animate the group's
4348     * children after the first layout.
4349     *
4350     * @param controller the animation controller
4351     */
4352    public void setLayoutAnimation(LayoutAnimationController controller) {
4353        mLayoutAnimationController = controller;
4354        if (mLayoutAnimationController != null) {
4355            mGroupFlags |= FLAG_RUN_ANIMATION;
4356        }
4357    }
4358
4359    /**
4360     * Returns the layout animation controller used to animate the group's
4361     * children.
4362     *
4363     * @return the current animation controller
4364     */
4365    public LayoutAnimationController getLayoutAnimation() {
4366        return mLayoutAnimationController;
4367    }
4368
4369    /**
4370     * Indicates whether the children's drawing cache is used during a layout
4371     * animation. By default, the drawing cache is enabled but this will prevent
4372     * nested layout animations from working. To nest animations, you must disable
4373     * the cache.
4374     *
4375     * @return true if the animation cache is enabled, false otherwise
4376     *
4377     * @see #setAnimationCacheEnabled(boolean)
4378     * @see View#setDrawingCacheEnabled(boolean)
4379     */
4380    @ViewDebug.ExportedProperty
4381    public boolean isAnimationCacheEnabled() {
4382        return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
4383    }
4384
4385    /**
4386     * Enables or disables the children's drawing cache during a layout animation.
4387     * By default, the drawing cache is enabled but this will prevent nested
4388     * layout animations from working. To nest animations, you must disable the
4389     * cache.
4390     *
4391     * @param enabled true to enable the animation cache, false otherwise
4392     *
4393     * @see #isAnimationCacheEnabled()
4394     * @see View#setDrawingCacheEnabled(boolean)
4395     */
4396    public void setAnimationCacheEnabled(boolean enabled) {
4397        setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
4398    }
4399
4400    /**
4401     * Indicates whether this ViewGroup will always try to draw its children using their
4402     * drawing cache. By default this property is enabled.
4403     *
4404     * @return true if the animation cache is enabled, false otherwise
4405     *
4406     * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4407     * @see #setChildrenDrawnWithCacheEnabled(boolean)
4408     * @see View#setDrawingCacheEnabled(boolean)
4409     */
4410    @ViewDebug.ExportedProperty(category = "drawing")
4411    public boolean isAlwaysDrawnWithCacheEnabled() {
4412        return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
4413    }
4414
4415    /**
4416     * Indicates whether this ViewGroup will always try to draw its children using their
4417     * drawing cache. This property can be set to true when the cache rendering is
4418     * slightly different from the children's normal rendering. Renderings can be different,
4419     * for instance, when the cache's quality is set to low.
4420     *
4421     * When this property is disabled, the ViewGroup will use the drawing cache of its
4422     * children only when asked to. It's usually the task of subclasses to tell ViewGroup
4423     * when to start using the drawing cache and when to stop using it.
4424     *
4425     * @param always true to always draw with the drawing cache, false otherwise
4426     *
4427     * @see #isAlwaysDrawnWithCacheEnabled()
4428     * @see #setChildrenDrawnWithCacheEnabled(boolean)
4429     * @see View#setDrawingCacheEnabled(boolean)
4430     * @see View#setDrawingCacheQuality(int)
4431     */
4432    public void setAlwaysDrawnWithCacheEnabled(boolean always) {
4433        setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
4434    }
4435
4436    /**
4437     * Indicates whether the ViewGroup is currently drawing its children using
4438     * their drawing cache.
4439     *
4440     * @return true if children should be drawn with their cache, false otherwise
4441     *
4442     * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4443     * @see #setChildrenDrawnWithCacheEnabled(boolean)
4444     */
4445    @ViewDebug.ExportedProperty(category = "drawing")
4446    protected boolean isChildrenDrawnWithCacheEnabled() {
4447        return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
4448    }
4449
4450    /**
4451     * Tells the ViewGroup to draw its children using their drawing cache. This property
4452     * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
4453     * will be used only if it has been enabled.
4454     *
4455     * Subclasses should call this method to start and stop using the drawing cache when
4456     * they perform performance sensitive operations, like scrolling or animating.
4457     *
4458     * @param enabled true if children should be drawn with their cache, false otherwise
4459     *
4460     * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4461     * @see #isChildrenDrawnWithCacheEnabled()
4462     */
4463    protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
4464        setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
4465    }
4466
4467    /**
4468     * Indicates whether the ViewGroup is drawing its children in the order defined by
4469     * {@link #getChildDrawingOrder(int, int)}.
4470     *
4471     * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
4472     *         false otherwise
4473     *
4474     * @see #setChildrenDrawingOrderEnabled(boolean)
4475     * @see #getChildDrawingOrder(int, int)
4476     */
4477    @ViewDebug.ExportedProperty(category = "drawing")
4478    protected boolean isChildrenDrawingOrderEnabled() {
4479        return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
4480    }
4481
4482    /**
4483     * Tells the ViewGroup whether to draw its children in the order defined by the method
4484     * {@link #getChildDrawingOrder(int, int)}.
4485     *
4486     * @param enabled true if the order of the children when drawing is determined by
4487     *        {@link #getChildDrawingOrder(int, int)}, false otherwise
4488     *
4489     * @see #isChildrenDrawingOrderEnabled()
4490     * @see #getChildDrawingOrder(int, int)
4491     */
4492    protected void setChildrenDrawingOrderEnabled(boolean enabled) {
4493        setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
4494    }
4495
4496    private void setBooleanFlag(int flag, boolean value) {
4497        if (value) {
4498            mGroupFlags |= flag;
4499        } else {
4500            mGroupFlags &= ~flag;
4501        }
4502    }
4503
4504    /**
4505     * Returns an integer indicating what types of drawing caches are kept in memory.
4506     *
4507     * @see #setPersistentDrawingCache(int)
4508     * @see #setAnimationCacheEnabled(boolean)
4509     *
4510     * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
4511     *         {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
4512     *         and {@link #PERSISTENT_ALL_CACHES}
4513     */
4514    @ViewDebug.ExportedProperty(category = "drawing", mapping = {
4515        @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE,        to = "NONE"),
4516        @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
4517        @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
4518        @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES,      to = "ALL")
4519    })
4520    public int getPersistentDrawingCache() {
4521        return mPersistentDrawingCache;
4522    }
4523
4524    /**
4525     * Indicates what types of drawing caches should be kept in memory after
4526     * they have been created.
4527     *
4528     * @see #getPersistentDrawingCache()
4529     * @see #setAnimationCacheEnabled(boolean)
4530     *
4531     * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
4532     *        {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
4533     *        and {@link #PERSISTENT_ALL_CACHES}
4534     */
4535    public void setPersistentDrawingCache(int drawingCacheToKeep) {
4536        mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
4537    }
4538
4539    /**
4540     * Returns the basis of alignment during layout operations on this view group:
4541     * either {@link #CLIP_BOUNDS} or {@link #OPTICAL_BOUNDS}.
4542     *
4543     * @return the layout mode to use during layout operations
4544     *
4545     * @see #setLayoutMode(int)
4546     *
4547     * @hide
4548     */
4549    public int getLayoutMode() {
4550        return mLayoutMode;
4551    }
4552
4553    /**
4554     * Sets the basis of alignment during the layout of this view group.
4555     * Valid values are either {@link #CLIP_BOUNDS} or {@link #OPTICAL_BOUNDS}.
4556     * <p>
4557     * The default is {@link #CLIP_BOUNDS}.
4558     *
4559     * @param layoutMode the layout mode to use during layout operations
4560     *
4561     * @see #getLayoutMode()
4562     *
4563     * @hide
4564     */
4565    public void setLayoutMode(int layoutMode) {
4566        if (mLayoutMode != layoutMode) {
4567            mLayoutMode = layoutMode;
4568            requestLayout();
4569        }
4570    }
4571
4572    /**
4573     * Returns a new set of layout parameters based on the supplied attributes set.
4574     *
4575     * @param attrs the attributes to build the layout parameters from
4576     *
4577     * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
4578     *         of its descendants
4579     */
4580    public LayoutParams generateLayoutParams(AttributeSet attrs) {
4581        return new LayoutParams(getContext(), attrs);
4582    }
4583
4584    /**
4585     * Returns a safe set of layout parameters based on the supplied layout params.
4586     * When a ViewGroup is passed a View whose layout params do not pass the test of
4587     * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
4588     * is invoked. This method should return a new set of layout params suitable for
4589     * this ViewGroup, possibly by copying the appropriate attributes from the
4590     * specified set of layout params.
4591     *
4592     * @param p The layout parameters to convert into a suitable set of layout parameters
4593     *          for this ViewGroup.
4594     *
4595     * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
4596     *         of its descendants
4597     */
4598    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
4599        return p;
4600    }
4601
4602    /**
4603     * Returns a set of default layout parameters. These parameters are requested
4604     * when the View passed to {@link #addView(View)} has no layout parameters
4605     * already set. If null is returned, an exception is thrown from addView.
4606     *
4607     * @return a set of default layout parameters or null
4608     */
4609    protected LayoutParams generateDefaultLayoutParams() {
4610        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
4611    }
4612
4613    /**
4614     * @hide
4615     */
4616    @Override
4617    protected boolean dispatchConsistencyCheck(int consistency) {
4618        boolean result = super.dispatchConsistencyCheck(consistency);
4619
4620        final int count = mChildrenCount;
4621        final View[] children = mChildren;
4622        for (int i = 0; i < count; i++) {
4623            if (!children[i].dispatchConsistencyCheck(consistency)) result = false;
4624        }
4625
4626        return result;
4627    }
4628
4629    /**
4630     * @hide
4631     */
4632    @Override
4633    protected boolean onConsistencyCheck(int consistency) {
4634        boolean result = super.onConsistencyCheck(consistency);
4635
4636        final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
4637        final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
4638
4639        if (checkLayout) {
4640            final int count = mChildrenCount;
4641            final View[] children = mChildren;
4642            for (int i = 0; i < count; i++) {
4643                if (children[i].getParent() != this) {
4644                    result = false;
4645                    android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
4646                            "View " + children[i] + " has no parent/a parent that is not " + this);
4647                }
4648            }
4649        }
4650
4651        if (checkDrawing) {
4652            // If this group is dirty, check that the parent is dirty as well
4653            if ((mPrivateFlags & DIRTY_MASK) != 0) {
4654                final ViewParent parent = getParent();
4655                if (parent != null && !(parent instanceof ViewRootImpl)) {
4656                    if ((((View) parent).mPrivateFlags & DIRTY_MASK) == 0) {
4657                        result = false;
4658                        android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
4659                                "ViewGroup " + this + " is dirty but its parent is not: " + this);
4660                    }
4661                }
4662            }
4663        }
4664
4665        return result;
4666    }
4667
4668    /**
4669     * {@inheritDoc}
4670     */
4671    @Override
4672    protected void debug(int depth) {
4673        super.debug(depth);
4674        String output;
4675
4676        if (mFocused != null) {
4677            output = debugIndent(depth);
4678            output += "mFocused";
4679            Log.d(VIEW_LOG_TAG, output);
4680        }
4681        if (mChildrenCount != 0) {
4682            output = debugIndent(depth);
4683            output += "{";
4684            Log.d(VIEW_LOG_TAG, output);
4685        }
4686        int count = mChildrenCount;
4687        for (int i = 0; i < count; i++) {
4688            View child = mChildren[i];
4689            child.debug(depth + 1);
4690        }
4691
4692        if (mChildrenCount != 0) {
4693            output = debugIndent(depth);
4694            output += "}";
4695            Log.d(VIEW_LOG_TAG, output);
4696        }
4697    }
4698
4699    /**
4700     * Returns the position in the group of the specified child view.
4701     *
4702     * @param child the view for which to get the position
4703     * @return a positive integer representing the position of the view in the
4704     *         group, or -1 if the view does not exist in the group
4705     */
4706    public int indexOfChild(View child) {
4707        final int count = mChildrenCount;
4708        final View[] children = mChildren;
4709        for (int i = 0; i < count; i++) {
4710            if (children[i] == child) {
4711                return i;
4712            }
4713        }
4714        return -1;
4715    }
4716
4717    /**
4718     * Returns the number of children in the group.
4719     *
4720     * @return a positive integer representing the number of children in
4721     *         the group
4722     */
4723    public int getChildCount() {
4724        return mChildrenCount;
4725    }
4726
4727    /**
4728     * Returns the view at the specified position in the group.
4729     *
4730     * @param index the position at which to get the view from
4731     * @return the view at the specified position or null if the position
4732     *         does not exist within the group
4733     */
4734    public View getChildAt(int index) {
4735        if (index < 0 || index >= mChildrenCount) {
4736            return null;
4737        }
4738        return mChildren[index];
4739    }
4740
4741    /**
4742     * Ask all of the children of this view to measure themselves, taking into
4743     * account both the MeasureSpec requirements for this view and its padding.
4744     * We skip children that are in the GONE state The heavy lifting is done in
4745     * getChildMeasureSpec.
4746     *
4747     * @param widthMeasureSpec The width requirements for this view
4748     * @param heightMeasureSpec The height requirements for this view
4749     */
4750    protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
4751        final int size = mChildrenCount;
4752        final View[] children = mChildren;
4753        for (int i = 0; i < size; ++i) {
4754            final View child = children[i];
4755            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
4756                measureChild(child, widthMeasureSpec, heightMeasureSpec);
4757            }
4758        }
4759    }
4760
4761    /**
4762     * Ask one of the children of this view to measure itself, taking into
4763     * account both the MeasureSpec requirements for this view and its padding.
4764     * The heavy lifting is done in getChildMeasureSpec.
4765     *
4766     * @param child The child to measure
4767     * @param parentWidthMeasureSpec The width requirements for this view
4768     * @param parentHeightMeasureSpec The height requirements for this view
4769     */
4770    protected void measureChild(View child, int parentWidthMeasureSpec,
4771            int parentHeightMeasureSpec) {
4772        final LayoutParams lp = child.getLayoutParams();
4773
4774        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
4775                mPaddingLeft + mPaddingRight, lp.width);
4776        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
4777                mPaddingTop + mPaddingBottom, lp.height);
4778
4779        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
4780    }
4781
4782    /**
4783     * Ask one of the children of this view to measure itself, taking into
4784     * account both the MeasureSpec requirements for this view and its padding
4785     * and margins. The child must have MarginLayoutParams The heavy lifting is
4786     * done in getChildMeasureSpec.
4787     *
4788     * @param child The child to measure
4789     * @param parentWidthMeasureSpec The width requirements for this view
4790     * @param widthUsed Extra space that has been used up by the parent
4791     *        horizontally (possibly by other children of the parent)
4792     * @param parentHeightMeasureSpec The height requirements for this view
4793     * @param heightUsed Extra space that has been used up by the parent
4794     *        vertically (possibly by other children of the parent)
4795     */
4796    protected void measureChildWithMargins(View child,
4797            int parentWidthMeasureSpec, int widthUsed,
4798            int parentHeightMeasureSpec, int heightUsed) {
4799        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
4800
4801        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
4802                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
4803                        + widthUsed, lp.width);
4804        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
4805                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
4806                        + heightUsed, lp.height);
4807
4808        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
4809    }
4810
4811    /**
4812     * Does the hard part of measureChildren: figuring out the MeasureSpec to
4813     * pass to a particular child. This method figures out the right MeasureSpec
4814     * for one dimension (height or width) of one child view.
4815     *
4816     * The goal is to combine information from our MeasureSpec with the
4817     * LayoutParams of the child to get the best possible results. For example,
4818     * if the this view knows its size (because its MeasureSpec has a mode of
4819     * EXACTLY), and the child has indicated in its LayoutParams that it wants
4820     * to be the same size as the parent, the parent should ask the child to
4821     * layout given an exact size.
4822     *
4823     * @param spec The requirements for this view
4824     * @param padding The padding of this view for the current dimension and
4825     *        margins, if applicable
4826     * @param childDimension How big the child wants to be in the current
4827     *        dimension
4828     * @return a MeasureSpec integer for the child
4829     */
4830    public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
4831        int specMode = MeasureSpec.getMode(spec);
4832        int specSize = MeasureSpec.getSize(spec);
4833
4834        int size = Math.max(0, specSize - padding);
4835
4836        int resultSize = 0;
4837        int resultMode = 0;
4838
4839        switch (specMode) {
4840        // Parent has imposed an exact size on us
4841        case MeasureSpec.EXACTLY:
4842            if (childDimension >= 0) {
4843                resultSize = childDimension;
4844                resultMode = MeasureSpec.EXACTLY;
4845            } else if (childDimension == LayoutParams.MATCH_PARENT) {
4846                // Child wants to be our size. So be it.
4847                resultSize = size;
4848                resultMode = MeasureSpec.EXACTLY;
4849            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
4850                // Child wants to determine its own size. It can't be
4851                // bigger than us.
4852                resultSize = size;
4853                resultMode = MeasureSpec.AT_MOST;
4854            }
4855            break;
4856
4857        // Parent has imposed a maximum size on us
4858        case MeasureSpec.AT_MOST:
4859            if (childDimension >= 0) {
4860                // Child wants a specific size... so be it
4861                resultSize = childDimension;
4862                resultMode = MeasureSpec.EXACTLY;
4863            } else if (childDimension == LayoutParams.MATCH_PARENT) {
4864                // Child wants to be our size, but our size is not fixed.
4865                // Constrain child to not be bigger than us.
4866                resultSize = size;
4867                resultMode = MeasureSpec.AT_MOST;
4868            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
4869                // Child wants to determine its own size. It can't be
4870                // bigger than us.
4871                resultSize = size;
4872                resultMode = MeasureSpec.AT_MOST;
4873            }
4874            break;
4875
4876        // Parent asked to see how big we want to be
4877        case MeasureSpec.UNSPECIFIED:
4878            if (childDimension >= 0) {
4879                // Child wants a specific size... let him have it
4880                resultSize = childDimension;
4881                resultMode = MeasureSpec.EXACTLY;
4882            } else if (childDimension == LayoutParams.MATCH_PARENT) {
4883                // Child wants to be our size... find out how big it should
4884                // be
4885                resultSize = 0;
4886                resultMode = MeasureSpec.UNSPECIFIED;
4887            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
4888                // Child wants to determine its own size.... find out how
4889                // big it should be
4890                resultSize = 0;
4891                resultMode = MeasureSpec.UNSPECIFIED;
4892            }
4893            break;
4894        }
4895        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
4896    }
4897
4898
4899    /**
4900     * Removes any pending animations for views that have been removed. Call
4901     * this if you don't want animations for exiting views to stack up.
4902     */
4903    public void clearDisappearingChildren() {
4904        if (mDisappearingChildren != null) {
4905            mDisappearingChildren.clear();
4906            invalidate();
4907        }
4908    }
4909
4910    /**
4911     * Add a view which is removed from mChildren but still needs animation
4912     *
4913     * @param v View to add
4914     */
4915    private void addDisappearingView(View v) {
4916        ArrayList<View> disappearingChildren = mDisappearingChildren;
4917
4918        if (disappearingChildren == null) {
4919            disappearingChildren = mDisappearingChildren = new ArrayList<View>();
4920        }
4921
4922        disappearingChildren.add(v);
4923    }
4924
4925    /**
4926     * Cleanup a view when its animation is done. This may mean removing it from
4927     * the list of disappearing views.
4928     *
4929     * @param view The view whose animation has finished
4930     * @param animation The animation, cannot be null
4931     */
4932    void finishAnimatingView(final View view, Animation animation) {
4933        final ArrayList<View> disappearingChildren = mDisappearingChildren;
4934        if (disappearingChildren != null) {
4935            if (disappearingChildren.contains(view)) {
4936                disappearingChildren.remove(view);
4937
4938                if (view.mAttachInfo != null) {
4939                    view.dispatchDetachedFromWindow();
4940                }
4941
4942                view.clearAnimation();
4943                mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4944            }
4945        }
4946
4947        if (animation != null && !animation.getFillAfter()) {
4948            view.clearAnimation();
4949        }
4950
4951        if ((view.mPrivateFlags & ANIMATION_STARTED) == ANIMATION_STARTED) {
4952            view.onAnimationEnd();
4953            // Should be performed by onAnimationEnd() but this avoid an infinite loop,
4954            // so we'd rather be safe than sorry
4955            view.mPrivateFlags &= ~ANIMATION_STARTED;
4956            // Draw one more frame after the animation is done
4957            mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4958        }
4959    }
4960
4961    /**
4962     * Utility function called by View during invalidation to determine whether a view that
4963     * is invisible or gone should still be invalidated because it is being transitioned (and
4964     * therefore still needs to be drawn).
4965     */
4966    boolean isViewTransitioning(View view) {
4967        return (mTransitioningViews != null && mTransitioningViews.contains(view));
4968    }
4969
4970    /**
4971     * This method tells the ViewGroup that the given View object, which should have this
4972     * ViewGroup as its parent,
4973     * should be kept around  (re-displayed when the ViewGroup draws its children) even if it
4974     * is removed from its parent. This allows animations, such as those used by
4975     * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
4976     * the removal of views. A call to this method should always be accompanied by a later call
4977     * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
4978     * so that the View finally gets removed.
4979     *
4980     * @param view The View object to be kept visible even if it gets removed from its parent.
4981     */
4982    public void startViewTransition(View view) {
4983        if (view.mParent == this) {
4984            if (mTransitioningViews == null) {
4985                mTransitioningViews = new ArrayList<View>();
4986            }
4987            mTransitioningViews.add(view);
4988        }
4989    }
4990
4991    /**
4992     * This method should always be called following an earlier call to
4993     * {@link #startViewTransition(View)}. The given View is finally removed from its parent
4994     * and will no longer be displayed. Note that this method does not perform the functionality
4995     * of removing a view from its parent; it just discontinues the display of a View that
4996     * has previously been removed.
4997     *
4998     * @return view The View object that has been removed but is being kept around in the visible
4999     * hierarchy by an earlier call to {@link #startViewTransition(View)}.
5000     */
5001    public void endViewTransition(View view) {
5002        if (mTransitioningViews != null) {
5003            mTransitioningViews.remove(view);
5004            final ArrayList<View> disappearingChildren = mDisappearingChildren;
5005            if (disappearingChildren != null && disappearingChildren.contains(view)) {
5006                disappearingChildren.remove(view);
5007                if (mVisibilityChangingChildren != null &&
5008                        mVisibilityChangingChildren.contains(view)) {
5009                    mVisibilityChangingChildren.remove(view);
5010                } else {
5011                    if (view.mAttachInfo != null) {
5012                        view.dispatchDetachedFromWindow();
5013                    }
5014                    if (view.mParent != null) {
5015                        view.mParent = null;
5016                    }
5017                }
5018                invalidate();
5019            }
5020        }
5021    }
5022
5023    private LayoutTransition.TransitionListener mLayoutTransitionListener =
5024            new LayoutTransition.TransitionListener() {
5025        @Override
5026        public void startTransition(LayoutTransition transition, ViewGroup container,
5027                View view, int transitionType) {
5028            // We only care about disappearing items, since we need special logic to keep
5029            // those items visible after they've been 'removed'
5030            if (transitionType == LayoutTransition.DISAPPEARING) {
5031                startViewTransition(view);
5032            }
5033        }
5034
5035        @Override
5036        public void endTransition(LayoutTransition transition, ViewGroup container,
5037                View view, int transitionType) {
5038            if (mLayoutSuppressed && !transition.isChangingLayout()) {
5039                requestLayout();
5040                mLayoutSuppressed = false;
5041            }
5042            if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
5043                endViewTransition(view);
5044            }
5045        }
5046    };
5047
5048    /**
5049     * {@inheritDoc}
5050     */
5051    @Override
5052    public boolean gatherTransparentRegion(Region region) {
5053        // If no transparent regions requested, we are always opaque.
5054        final boolean meOpaque = (mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) == 0;
5055        if (meOpaque && region == null) {
5056            // The caller doesn't care about the region, so stop now.
5057            return true;
5058        }
5059        super.gatherTransparentRegion(region);
5060        final View[] children = mChildren;
5061        final int count = mChildrenCount;
5062        boolean noneOfTheChildrenAreTransparent = true;
5063        for (int i = 0; i < count; i++) {
5064            final View child = children[i];
5065            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
5066                if (!child.gatherTransparentRegion(region)) {
5067                    noneOfTheChildrenAreTransparent = false;
5068                }
5069            }
5070        }
5071        return meOpaque || noneOfTheChildrenAreTransparent;
5072    }
5073
5074    /**
5075     * {@inheritDoc}
5076     */
5077    public void requestTransparentRegion(View child) {
5078        if (child != null) {
5079            child.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
5080            if (mParent != null) {
5081                mParent.requestTransparentRegion(this);
5082            }
5083        }
5084    }
5085
5086
5087    @Override
5088    protected boolean fitSystemWindows(Rect insets) {
5089        boolean done = super.fitSystemWindows(insets);
5090        if (!done) {
5091            final int count = mChildrenCount;
5092            final View[] children = mChildren;
5093            for (int i = 0; i < count; i++) {
5094                done = children[i].fitSystemWindows(insets);
5095                if (done) {
5096                    break;
5097                }
5098            }
5099        }
5100        return done;
5101    }
5102
5103    /**
5104     * Returns the animation listener to which layout animation events are
5105     * sent.
5106     *
5107     * @return an {@link android.view.animation.Animation.AnimationListener}
5108     */
5109    public Animation.AnimationListener getLayoutAnimationListener() {
5110        return mAnimationListener;
5111    }
5112
5113    @Override
5114    protected void drawableStateChanged() {
5115        super.drawableStateChanged();
5116
5117        if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
5118            if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
5119                throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
5120                        + " child has duplicateParentState set to true");
5121            }
5122
5123            final View[] children = mChildren;
5124            final int count = mChildrenCount;
5125
5126            for (int i = 0; i < count; i++) {
5127                final View child = children[i];
5128                if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
5129                    child.refreshDrawableState();
5130                }
5131            }
5132        }
5133    }
5134
5135    @Override
5136    public void jumpDrawablesToCurrentState() {
5137        super.jumpDrawablesToCurrentState();
5138        final View[] children = mChildren;
5139        final int count = mChildrenCount;
5140        for (int i = 0; i < count; i++) {
5141            children[i].jumpDrawablesToCurrentState();
5142        }
5143    }
5144
5145    @Override
5146    protected int[] onCreateDrawableState(int extraSpace) {
5147        if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
5148            return super.onCreateDrawableState(extraSpace);
5149        }
5150
5151        int need = 0;
5152        int n = getChildCount();
5153        for (int i = 0; i < n; i++) {
5154            int[] childState = getChildAt(i).getDrawableState();
5155
5156            if (childState != null) {
5157                need += childState.length;
5158            }
5159        }
5160
5161        int[] state = super.onCreateDrawableState(extraSpace + need);
5162
5163        for (int i = 0; i < n; i++) {
5164            int[] childState = getChildAt(i).getDrawableState();
5165
5166            if (childState != null) {
5167                state = mergeDrawableStates(state, childState);
5168            }
5169        }
5170
5171        return state;
5172    }
5173
5174    /**
5175     * Sets whether this ViewGroup's drawable states also include
5176     * its children's drawable states.  This is used, for example, to
5177     * make a group appear to be focused when its child EditText or button
5178     * is focused.
5179     */
5180    public void setAddStatesFromChildren(boolean addsStates) {
5181        if (addsStates) {
5182            mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
5183        } else {
5184            mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
5185        }
5186
5187        refreshDrawableState();
5188    }
5189
5190    /**
5191     * Returns whether this ViewGroup's drawable states also include
5192     * its children's drawable states.  This is used, for example, to
5193     * make a group appear to be focused when its child EditText or button
5194     * is focused.
5195     */
5196    public boolean addStatesFromChildren() {
5197        return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
5198    }
5199
5200    /**
5201     * If {link #addStatesFromChildren} is true, refreshes this group's
5202     * drawable state (to include the states from its children).
5203     */
5204    public void childDrawableStateChanged(View child) {
5205        if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
5206            refreshDrawableState();
5207        }
5208    }
5209
5210    /**
5211     * Specifies the animation listener to which layout animation events must
5212     * be sent. Only
5213     * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
5214     * and
5215     * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
5216     * are invoked.
5217     *
5218     * @param animationListener the layout animation listener
5219     */
5220    public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
5221        mAnimationListener = animationListener;
5222    }
5223
5224    /**
5225     * This method is called by LayoutTransition when there are 'changing' animations that need
5226     * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
5227     * starts all pending transitions prior to the drawing phase in the current traversal.
5228     *
5229     * @param transition The LayoutTransition to be started on the next traversal.
5230     *
5231     * @hide
5232     */
5233    public void requestTransitionStart(LayoutTransition transition) {
5234        ViewRootImpl viewAncestor = getViewRootImpl();
5235        if (viewAncestor != null) {
5236            viewAncestor.requestTransitionStart(transition);
5237        }
5238    }
5239
5240    @Override
5241    public void onResolvedLayoutDirectionReset() {
5242        // Take care of resetting the children resolution too
5243        final int count = getChildCount();
5244        for (int i = 0; i < count; i++) {
5245            final View child = getChildAt(i);
5246            if (child.getLayoutDirection() == LAYOUT_DIRECTION_INHERIT) {
5247                child.resetResolvedLayoutDirection();
5248            }
5249        }
5250    }
5251
5252    @Override
5253    public void onResolvedTextDirectionReset() {
5254        // Take care of resetting the children resolution too
5255        final int count = getChildCount();
5256        for (int i = 0; i < count; i++) {
5257            final View child = getChildAt(i);
5258            if (child.getTextDirection() == TEXT_DIRECTION_INHERIT) {
5259                child.resetResolvedTextDirection();
5260            }
5261        }
5262    }
5263
5264    @Override
5265    public void onResolvedTextAlignmentReset() {
5266        // Take care of resetting the children resolution too
5267        final int count = getChildCount();
5268        for (int i = 0; i < count; i++) {
5269            final View child = getChildAt(i);
5270            if (child.getTextAlignment() == TEXT_ALIGNMENT_INHERIT) {
5271                child.resetResolvedTextAlignment();
5272            }
5273        }
5274    }
5275
5276    /**
5277     * Return true if the pressed state should be delayed for children or descendants of this
5278     * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
5279     * This prevents the pressed state from appearing when the user is actually trying to scroll
5280     * the content.
5281     *
5282     * The default implementation returns true for compatibility reasons. Subclasses that do
5283     * not scroll should generally override this method and return false.
5284     */
5285    public boolean shouldDelayChildPressedState() {
5286        return true;
5287    }
5288
5289    /** @hide */
5290    protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
5291    }
5292
5293    /**
5294     * LayoutParams are used by views to tell their parents how they want to be
5295     * laid out. See
5296     * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
5297     * for a list of all child view attributes that this class supports.
5298     *
5299     * <p>
5300     * The base LayoutParams class just describes how big the view wants to be
5301     * for both width and height. For each dimension, it can specify one of:
5302     * <ul>
5303     * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
5304     * means that the view wants to be as big as its parent (minus padding)
5305     * <li> WRAP_CONTENT, which means that the view wants to be just big enough
5306     * to enclose its content (plus padding)
5307     * <li> an exact number
5308     * </ul>
5309     * There are subclasses of LayoutParams for different subclasses of
5310     * ViewGroup. For example, AbsoluteLayout has its own subclass of
5311     * LayoutParams which adds an X and Y value.</p>
5312     *
5313     * <div class="special reference">
5314     * <h3>Developer Guides</h3>
5315     * <p>For more information about creating user interface layouts, read the
5316     * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
5317     * guide.</p></div>
5318     *
5319     * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
5320     * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
5321     */
5322    public static class LayoutParams {
5323        /**
5324         * Special value for the height or width requested by a View.
5325         * FILL_PARENT means that the view wants to be as big as its parent,
5326         * minus the parent's padding, if any. This value is deprecated
5327         * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
5328         */
5329        @SuppressWarnings({"UnusedDeclaration"})
5330        @Deprecated
5331        public static final int FILL_PARENT = -1;
5332
5333        /**
5334         * Special value for the height or width requested by a View.
5335         * MATCH_PARENT means that the view wants to be as big as its parent,
5336         * minus the parent's padding, if any. Introduced in API Level 8.
5337         */
5338        public static final int MATCH_PARENT = -1;
5339
5340        /**
5341         * Special value for the height or width requested by a View.
5342         * WRAP_CONTENT means that the view wants to be just large enough to fit
5343         * its own internal content, taking its own padding into account.
5344         */
5345        public static final int WRAP_CONTENT = -2;
5346
5347        /**
5348         * Information about how wide the view wants to be. Can be one of the
5349         * constants FILL_PARENT (replaced by MATCH_PARENT ,
5350         * in API Level 8) or WRAP_CONTENT. or an exact size.
5351         */
5352        @ViewDebug.ExportedProperty(category = "layout", mapping = {
5353            @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
5354            @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
5355        })
5356        public int width;
5357
5358        /**
5359         * Information about how tall the view wants to be. Can be one of the
5360         * constants FILL_PARENT (replaced by MATCH_PARENT ,
5361         * in API Level 8) or WRAP_CONTENT. or an exact size.
5362         */
5363        @ViewDebug.ExportedProperty(category = "layout", mapping = {
5364            @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
5365            @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
5366        })
5367        public int height;
5368
5369        /**
5370         * Used to animate layouts.
5371         */
5372        public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
5373
5374        /**
5375         * Creates a new set of layout parameters. The values are extracted from
5376         * the supplied attributes set and context. The XML attributes mapped
5377         * to this set of layout parameters are:
5378         *
5379         * <ul>
5380         *   <li><code>layout_width</code>: the width, either an exact value,
5381         *   {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
5382         *   {@link #MATCH_PARENT} in API Level 8)</li>
5383         *   <li><code>layout_height</code>: the height, either an exact value,
5384         *   {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
5385         *   {@link #MATCH_PARENT} in API Level 8)</li>
5386         * </ul>
5387         *
5388         * @param c the application environment
5389         * @param attrs the set of attributes from which to extract the layout
5390         *              parameters' values
5391         */
5392        public LayoutParams(Context c, AttributeSet attrs) {
5393            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
5394            setBaseAttributes(a,
5395                    R.styleable.ViewGroup_Layout_layout_width,
5396                    R.styleable.ViewGroup_Layout_layout_height);
5397            a.recycle();
5398        }
5399
5400        /**
5401         * Creates a new set of layout parameters with the specified width
5402         * and height.
5403         *
5404         * @param width the width, either {@link #WRAP_CONTENT},
5405         *        {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
5406         *        API Level 8), or a fixed size in pixels
5407         * @param height the height, either {@link #WRAP_CONTENT},
5408         *        {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
5409         *        API Level 8), or a fixed size in pixels
5410         */
5411        public LayoutParams(int width, int height) {
5412            this.width = width;
5413            this.height = height;
5414        }
5415
5416        /**
5417         * Copy constructor. Clones the width and height values of the source.
5418         *
5419         * @param source The layout params to copy from.
5420         */
5421        public LayoutParams(LayoutParams source) {
5422            this.width = source.width;
5423            this.height = source.height;
5424        }
5425
5426        /**
5427         * Used internally by MarginLayoutParams.
5428         * @hide
5429         */
5430        LayoutParams() {
5431        }
5432
5433        /**
5434         * Extracts the layout parameters from the supplied attributes.
5435         *
5436         * @param a the style attributes to extract the parameters from
5437         * @param widthAttr the identifier of the width attribute
5438         * @param heightAttr the identifier of the height attribute
5439         */
5440        protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
5441            width = a.getLayoutDimension(widthAttr, "layout_width");
5442            height = a.getLayoutDimension(heightAttr, "layout_height");
5443        }
5444
5445        /**
5446         * Resolve layout parameters depending on the layout direction. Subclasses that care about
5447         * layoutDirection changes should override this method. The default implementation does
5448         * nothing.
5449         *
5450         * @param layoutDirection the direction of the layout
5451         *
5452         * {@link View#LAYOUT_DIRECTION_LTR}
5453         * {@link View#LAYOUT_DIRECTION_RTL}
5454         */
5455        public void onResolveLayoutDirection(int layoutDirection) {
5456        }
5457
5458        /**
5459         * Returns a String representation of this set of layout parameters.
5460         *
5461         * @param output the String to prepend to the internal representation
5462         * @return a String with the following format: output +
5463         *         "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
5464         *
5465         * @hide
5466         */
5467        public String debug(String output) {
5468            return output + "ViewGroup.LayoutParams={ width="
5469                    + sizeToString(width) + ", height=" + sizeToString(height) + " }";
5470        }
5471
5472        /**
5473         * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
5474         *
5475         * @param view the view that contains these layout parameters
5476         * @param canvas the canvas on which to draw
5477         *
5478         * @hide
5479         */
5480        public void onDebugDraw(View view, Canvas canvas) {
5481        }
5482
5483        /**
5484         * Converts the specified size to a readable String.
5485         *
5486         * @param size the size to convert
5487         * @return a String instance representing the supplied size
5488         *
5489         * @hide
5490         */
5491        protected static String sizeToString(int size) {
5492            if (size == WRAP_CONTENT) {
5493                return "wrap-content";
5494            }
5495            if (size == MATCH_PARENT) {
5496                return "match-parent";
5497            }
5498            return String.valueOf(size);
5499        }
5500    }
5501
5502    /**
5503     * Per-child layout information for layouts that support margins.
5504     * See
5505     * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
5506     * for a list of all child view attributes that this class supports.
5507     */
5508    public static class MarginLayoutParams extends ViewGroup.LayoutParams {
5509        /**
5510         * The left margin in pixels of the child.
5511         * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5512         * to this field.
5513         */
5514        @ViewDebug.ExportedProperty(category = "layout")
5515        public int leftMargin;
5516
5517        /**
5518         * The top margin in pixels of the child.
5519         * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5520         * to this field.
5521         */
5522        @ViewDebug.ExportedProperty(category = "layout")
5523        public int topMargin;
5524
5525        /**
5526         * The right margin in pixels of the child.
5527         * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5528         * to this field.
5529         */
5530        @ViewDebug.ExportedProperty(category = "layout")
5531        public int rightMargin;
5532
5533        /**
5534         * The bottom margin in pixels of the child.
5535         * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5536         * to this field.
5537         */
5538        @ViewDebug.ExportedProperty(category = "layout")
5539        public int bottomMargin;
5540
5541        /**
5542         * The start margin in pixels of the child.
5543         * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5544         * to this field.
5545         */
5546        @ViewDebug.ExportedProperty(category = "layout")
5547        public int startMargin = DEFAULT_RELATIVE;
5548
5549        /**
5550         * The end margin in pixels of the child.
5551         * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5552         * to this field.
5553         */
5554        @ViewDebug.ExportedProperty(category = "layout")
5555        public int endMargin = DEFAULT_RELATIVE;
5556
5557        /**
5558         * The default start and end margin.
5559         */
5560        static private final int DEFAULT_RELATIVE = Integer.MIN_VALUE;
5561
5562        /**
5563         * Creates a new set of layout parameters. The values are extracted from
5564         * the supplied attributes set and context.
5565         *
5566         * @param c the application environment
5567         * @param attrs the set of attributes from which to extract the layout
5568         *              parameters' values
5569         */
5570        public MarginLayoutParams(Context c, AttributeSet attrs) {
5571            super();
5572
5573            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
5574            setBaseAttributes(a,
5575                    R.styleable.ViewGroup_MarginLayout_layout_width,
5576                    R.styleable.ViewGroup_MarginLayout_layout_height);
5577
5578            int margin = a.getDimensionPixelSize(
5579                    com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
5580            if (margin >= 0) {
5581                leftMargin = margin;
5582                topMargin = margin;
5583                rightMargin= margin;
5584                bottomMargin = margin;
5585            } else {
5586                leftMargin = a.getDimensionPixelSize(
5587                        R.styleable.ViewGroup_MarginLayout_layout_marginLeft, 0);
5588                topMargin = a.getDimensionPixelSize(
5589                        R.styleable.ViewGroup_MarginLayout_layout_marginTop, 0);
5590                rightMargin = a.getDimensionPixelSize(
5591                        R.styleable.ViewGroup_MarginLayout_layout_marginRight, 0);
5592                bottomMargin = a.getDimensionPixelSize(
5593                        R.styleable.ViewGroup_MarginLayout_layout_marginBottom, 0);
5594                startMargin = a.getDimensionPixelSize(
5595                        R.styleable.ViewGroup_MarginLayout_layout_marginStart, DEFAULT_RELATIVE);
5596                endMargin = a.getDimensionPixelSize(
5597                        R.styleable.ViewGroup_MarginLayout_layout_marginEnd, DEFAULT_RELATIVE);
5598            }
5599
5600            a.recycle();
5601        }
5602
5603        /**
5604         * {@inheritDoc}
5605         */
5606        public MarginLayoutParams(int width, int height) {
5607            super(width, height);
5608        }
5609
5610        /**
5611         * Copy constructor. Clones the width, height and margin values of the source.
5612         *
5613         * @param source The layout params to copy from.
5614         */
5615        public MarginLayoutParams(MarginLayoutParams source) {
5616            this.width = source.width;
5617            this.height = source.height;
5618
5619            this.leftMargin = source.leftMargin;
5620            this.topMargin = source.topMargin;
5621            this.rightMargin = source.rightMargin;
5622            this.bottomMargin = source.bottomMargin;
5623            this.startMargin = source.startMargin;
5624            this.endMargin = source.endMargin;
5625        }
5626
5627        /**
5628         * {@inheritDoc}
5629         */
5630        public MarginLayoutParams(LayoutParams source) {
5631            super(source);
5632        }
5633
5634        /**
5635         * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
5636         * to be done so that the new margins are taken into account. Left and right margins may be
5637         * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
5638         *
5639         * @param left the left margin size
5640         * @param top the top margin size
5641         * @param right the right margin size
5642         * @param bottom the bottom margin size
5643         *
5644         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
5645         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
5646         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
5647         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
5648         */
5649        public void setMargins(int left, int top, int right, int bottom) {
5650            leftMargin = left;
5651            topMargin = top;
5652            rightMargin = right;
5653            bottomMargin = bottom;
5654        }
5655
5656        /**
5657         * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
5658         * needs to be done so that the new relative margins are taken into account. Left and right
5659         * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
5660         * direction.
5661         *
5662         * @param start the start margin size
5663         * @param top the top margin size
5664         * @param end the right margin size
5665         * @param bottom the bottom margin size
5666         *
5667         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
5668         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
5669         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
5670         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
5671         *
5672         * @hide
5673         */
5674        public void setMarginsRelative(int start, int top, int end, int bottom) {
5675            startMargin = start;
5676            topMargin = top;
5677            endMargin = end;
5678            bottomMargin = bottom;
5679        }
5680
5681        /**
5682         * Returns the start margin in pixels.
5683         *
5684         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
5685         *
5686         * @return the start margin in pixels.
5687         */
5688        public int getMarginStart() {
5689            return startMargin;
5690        }
5691
5692        /**
5693         * Returns the end margin in pixels.
5694         *
5695         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
5696         *
5697         * @return the end margin in pixels.
5698         */
5699        public int getMarginEnd() {
5700            return endMargin;
5701        }
5702
5703        /**
5704         * Check if margins are relative.
5705         *
5706         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
5707         * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
5708         *
5709         * @return true if either marginStart or marginEnd has been set
5710         */
5711        public boolean isMarginRelative() {
5712            return (startMargin != DEFAULT_RELATIVE) || (endMargin != DEFAULT_RELATIVE);
5713        }
5714
5715        /**
5716         * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
5717         * may be overridden depending on layout direction.
5718         */
5719        @Override
5720        public void onResolveLayoutDirection(int layoutDirection) {
5721            switch(layoutDirection) {
5722                case View.LAYOUT_DIRECTION_RTL:
5723                    leftMargin = (endMargin > DEFAULT_RELATIVE) ? endMargin : leftMargin;
5724                    rightMargin = (startMargin > DEFAULT_RELATIVE) ? startMargin : rightMargin;
5725                    break;
5726                case View.LAYOUT_DIRECTION_LTR:
5727                default:
5728                    leftMargin = (startMargin > DEFAULT_RELATIVE) ? startMargin : leftMargin;
5729                    rightMargin = (endMargin > DEFAULT_RELATIVE) ? endMargin : rightMargin;
5730                    break;
5731            }
5732        }
5733
5734        /**
5735         * @hide
5736         */
5737        @Override
5738        public void onDebugDraw(View view, Canvas canvas) {
5739            drawRect(canvas,
5740                    view.getLeft() - leftMargin,
5741                    view.getTop() - topMargin,
5742                    view.getRight() + rightMargin,
5743                    view.getBottom() + bottomMargin, Color.MAGENTA);
5744        }
5745    }
5746
5747    /* Describes a touched view and the ids of the pointers that it has captured.
5748     *
5749     * This code assumes that pointer ids are always in the range 0..31 such that
5750     * it can use a bitfield to track which pointer ids are present.
5751     * As it happens, the lower layers of the input dispatch pipeline also use the
5752     * same trick so the assumption should be safe here...
5753     */
5754    private static final class TouchTarget {
5755        private static final int MAX_RECYCLED = 32;
5756        private static final Object sRecycleLock = new Object();
5757        private static TouchTarget sRecycleBin;
5758        private static int sRecycledCount;
5759
5760        public static final int ALL_POINTER_IDS = -1; // all ones
5761
5762        // The touched child view.
5763        public View child;
5764
5765        // The combined bit mask of pointer ids for all pointers captured by the target.
5766        public int pointerIdBits;
5767
5768        // The next target in the target list.
5769        public TouchTarget next;
5770
5771        private TouchTarget() {
5772        }
5773
5774        public static TouchTarget obtain(View child, int pointerIdBits) {
5775            final TouchTarget target;
5776            synchronized (sRecycleLock) {
5777                if (sRecycleBin == null) {
5778                    target = new TouchTarget();
5779                } else {
5780                    target = sRecycleBin;
5781                    sRecycleBin = target.next;
5782                     sRecycledCount--;
5783                    target.next = null;
5784                }
5785            }
5786            target.child = child;
5787            target.pointerIdBits = pointerIdBits;
5788            return target;
5789        }
5790
5791        public void recycle() {
5792            synchronized (sRecycleLock) {
5793                if (sRecycledCount < MAX_RECYCLED) {
5794                    next = sRecycleBin;
5795                    sRecycleBin = this;
5796                    sRecycledCount += 1;
5797                } else {
5798                    next = null;
5799                }
5800                child = null;
5801            }
5802        }
5803    }
5804
5805    /* Describes a hovered view. */
5806    private static final class HoverTarget {
5807        private static final int MAX_RECYCLED = 32;
5808        private static final Object sRecycleLock = new Object();
5809        private static HoverTarget sRecycleBin;
5810        private static int sRecycledCount;
5811
5812        // The hovered child view.
5813        public View child;
5814
5815        // The next target in the target list.
5816        public HoverTarget next;
5817
5818        private HoverTarget() {
5819        }
5820
5821        public static HoverTarget obtain(View child) {
5822            final HoverTarget target;
5823            synchronized (sRecycleLock) {
5824                if (sRecycleBin == null) {
5825                    target = new HoverTarget();
5826                } else {
5827                    target = sRecycleBin;
5828                    sRecycleBin = target.next;
5829                     sRecycledCount--;
5830                    target.next = null;
5831                }
5832            }
5833            target.child = child;
5834            return target;
5835        }
5836
5837        public void recycle() {
5838            synchronized (sRecycleLock) {
5839                if (sRecycledCount < MAX_RECYCLED) {
5840                    next = sRecycleBin;
5841                    sRecycleBin = this;
5842                    sRecycledCount += 1;
5843                } else {
5844                    next = null;
5845                }
5846                child = null;
5847            }
5848        }
5849    }
5850
5851    /**
5852     * Pooled class that orderes the children of a ViewGroup from start
5853     * to end based on how they are laid out and the layout direction.
5854     */
5855    static class ChildListForAccessibility {
5856
5857        private static final int MAX_POOL_SIZE = 32;
5858
5859        private static final Object sPoolLock = new Object();
5860
5861        private static ChildListForAccessibility sPool;
5862
5863        private static int sPoolSize;
5864
5865        private boolean mIsPooled;
5866
5867        private ChildListForAccessibility mNext;
5868
5869        private final ArrayList<View> mChildren = new ArrayList<View>();
5870
5871        private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
5872
5873        public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
5874            ChildListForAccessibility list = null;
5875            synchronized (sPoolLock) {
5876                if (sPool != null) {
5877                    list = sPool;
5878                    sPool = list.mNext;
5879                    list.mNext = null;
5880                    list.mIsPooled = false;
5881                    sPoolSize--;
5882                } else {
5883                    list = new ChildListForAccessibility();
5884                }
5885                list.init(parent, sort);
5886                return list;
5887            }
5888        }
5889
5890        public void recycle() {
5891            if (mIsPooled) {
5892                throw new IllegalStateException("Instance already recycled.");
5893            }
5894            clear();
5895            synchronized (sPoolLock) {
5896                if (sPoolSize < MAX_POOL_SIZE) {
5897                    mNext = sPool;
5898                    mIsPooled = true;
5899                    sPool = this;
5900                    sPoolSize++;
5901                }
5902            }
5903        }
5904
5905        public int getChildCount() {
5906            return mChildren.size();
5907        }
5908
5909        public View getChildAt(int index) {
5910            return mChildren.get(index);
5911        }
5912
5913        public int getChildIndex(View child) {
5914            return mChildren.indexOf(child);
5915        }
5916
5917        private void init(ViewGroup parent, boolean sort) {
5918            ArrayList<View> children = mChildren;
5919            final int childCount = parent.getChildCount();
5920            for (int i = 0; i < childCount; i++) {
5921                View child = parent.getChildAt(i);
5922                children.add(child);
5923            }
5924            if (sort) {
5925                ArrayList<ViewLocationHolder> holders = mHolders;
5926                for (int i = 0; i < childCount; i++) {
5927                    View child = children.get(i);
5928                    ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
5929                    holders.add(holder);
5930                }
5931                Collections.sort(holders);
5932                for (int i = 0; i < childCount; i++) {
5933                    ViewLocationHolder holder = holders.get(i);
5934                    children.set(i, holder.mView);
5935                    holder.recycle();
5936                }
5937                holders.clear();
5938            }
5939        }
5940
5941        private void clear() {
5942            mChildren.clear();
5943        }
5944    }
5945
5946    /**
5947     * Pooled class that holds a View and its location with respect to
5948     * a specified root. This enables sorting of views based on their
5949     * coordinates without recomputing the position relative to the root
5950     * on every comparison.
5951     */
5952    static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
5953
5954        private static final int MAX_POOL_SIZE = 32;
5955
5956        private static final Object sPoolLock = new Object();
5957
5958        private static ViewLocationHolder sPool;
5959
5960        private static int sPoolSize;
5961
5962        private boolean mIsPooled;
5963
5964        private ViewLocationHolder mNext;
5965
5966        private final Rect mLocation = new Rect();
5967
5968        public View mView;
5969
5970        private int mLayoutDirection;
5971
5972        public static ViewLocationHolder obtain(ViewGroup root, View view) {
5973            ViewLocationHolder holder = null;
5974            synchronized (sPoolLock) {
5975                if (sPool != null) {
5976                    holder = sPool;
5977                    sPool = holder.mNext;
5978                    holder.mNext = null;
5979                    holder.mIsPooled = false;
5980                    sPoolSize--;
5981                } else {
5982                    holder = new ViewLocationHolder();
5983                }
5984                holder.init(root, view);
5985                return holder;
5986            }
5987        }
5988
5989        public void recycle() {
5990            if (mIsPooled) {
5991                throw new IllegalStateException("Instance already recycled.");
5992            }
5993            clear();
5994            synchronized (sPoolLock) {
5995                if (sPoolSize < MAX_POOL_SIZE) {
5996                    mNext = sPool;
5997                    mIsPooled = true;
5998                    sPool = this;
5999                    sPoolSize++;
6000                }
6001            }
6002        }
6003
6004        @Override
6005        public int compareTo(ViewLocationHolder another) {
6006            // This instance is greater than an invalid argument.
6007            if (another == null) {
6008                return 1;
6009            }
6010            if (getClass() != another.getClass()) {
6011                return 1;
6012            }
6013            // First is above second.
6014            if (mLocation.bottom - another.mLocation.top <= 0) {
6015                return -1;
6016            }
6017            // First is below second.
6018            if (mLocation.top - another.mLocation.bottom >= 0) {
6019                return 1;
6020            }
6021            // LTR
6022            if (mLayoutDirection == LAYOUT_DIRECTION_LTR) {
6023                final int leftDifference = mLocation.left - another.mLocation.left;
6024                // First more to the left than second.
6025                if (leftDifference != 0) {
6026                    return leftDifference;
6027                }
6028            } else { // RTL
6029                final int rightDifference = mLocation.right - another.mLocation.right;
6030                // First more to the right than second.
6031                if (rightDifference != 0) {
6032                    return -rightDifference;
6033                }
6034            }
6035            // Break tie by top.
6036            final int topDiference = mLocation.top - another.mLocation.top;
6037            if (topDiference != 0) {
6038                return topDiference;
6039            }
6040            // Break tie by height.
6041            final int heightDiference = mLocation.height() - another.mLocation.height();
6042            if (heightDiference != 0) {
6043                return -heightDiference;
6044            }
6045            // Break tie by width.
6046            final int widthDiference = mLocation.width() - another.mLocation.width();
6047            if (widthDiference != 0) {
6048                return -widthDiference;
6049            }
6050            // Just break the tie somehow. The accessibliity ids are unique
6051            // and stable, hence this is deterministic tie breaking.
6052            return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
6053        }
6054
6055        private void init(ViewGroup root, View view) {
6056            Rect viewLocation = mLocation;
6057            view.getDrawingRect(viewLocation);
6058            root.offsetDescendantRectToMyCoords(view, viewLocation);
6059            mView = view;
6060            mLayoutDirection = root.getResolvedLayoutDirection();
6061        }
6062
6063        private void clear() {
6064            mView = null;
6065            mLocation.set(0, 0, 0, 0);
6066        }
6067    }
6068
6069    private static Paint getDebugPaint() {
6070        if (sDebugPaint == null) {
6071            sDebugPaint = new Paint();
6072            sDebugPaint.setAntiAlias(false);
6073        }
6074        return sDebugPaint;
6075    }
6076
6077    private static float[] getDebugLines(int x1, int y1, int x2, int y2) {
6078        if (sDebugLines== null) {
6079            sDebugLines = new float[16];
6080        }
6081
6082        x2--;
6083        y2--;
6084
6085        sDebugLines[0] = x1;
6086        sDebugLines[1] = y1;
6087        sDebugLines[2] = x2;
6088        sDebugLines[3] = y1;
6089
6090        sDebugLines[4] = x2;
6091        sDebugLines[5] = y1;
6092        sDebugLines[6] = x2;
6093        sDebugLines[7] = y2 + 1;
6094
6095        sDebugLines[8] = x2 + 1;
6096        sDebugLines[9] = y2;
6097        sDebugLines[10] = x1;
6098        sDebugLines[11] = y2;
6099
6100        sDebugLines[12]  = x1;
6101        sDebugLines[13]  = y2;
6102        sDebugLines[14] = x1;
6103        sDebugLines[15] = y1;
6104
6105        return sDebugLines;
6106    }
6107}
6108