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