blob: d906a16fbd2e8446314d2daa7c9f2a9d65c79de6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Chet Haase21cd1382010-09-01 17:42:29 -070019import android.animation.LayoutTransition;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.TypedArray;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
Adam Powell6e346362010-07-23 10:18:23 -070025import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Paint;
Christopher Tatea53146c2010-09-07 11:57:52 -070027import android.graphics.PointF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.RectF;
svetoslavganov75986cf2009-05-14 22:28:01 -070030import android.graphics.Region;
Jeff Brown995e7742010-12-22 16:59:36 -080031import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Parcelable;
33import android.os.SystemClock;
34import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.Log;
36import android.util.SparseArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070037import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070038import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.view.animation.Animation;
40import android.view.animation.AnimationUtils;
41import android.view.animation.LayoutAnimationController;
42import android.view.animation.Transformation;
Doug Feltcb379122011-07-07 11:57:48 -070043
Romain Guy0211a0a2011-02-14 16:34:59 -080044import com.android.internal.R;
45import com.android.internal.util.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47import java.util.ArrayList;
Christopher Tate86cab1b2011-01-13 20:28:55 -080048import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
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>
Romain Guyd6a463a2009-05-21 23:10:10 -070062 *
Joe Fernandez558459f2011-10-13 16:47:36 -070063 * <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 *
Romain Guyd6a463a2009-05-21 23:10:10 -070069 * @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
Chet Haase13cc1202010-09-03 15:39:20 -070077 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 */
79public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Chet Haase21cd1382010-09-01 17:42:29 -070080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private static final boolean DBG = false;
Gilles Debunnecea45132011-11-24 02:19:27 +010082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
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
Chet Haase48460322010-06-11 14:22:25 -0700102 /**
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 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 private RectF mInvalidateRegion;
112
Chet Haase48460322010-06-11 14:22:25 -0700113 /**
114 * A Transformation used to calculate a correct
115 * invalidation area when the application is autoscaled.
116 */
117 private Transformation mInvalidationTransformation;
118
Christopher Tatea53146c2010-09-07 11:57:52 -0700119 // View currently under an ongoing drag
120 private View mCurrentDragView;
121
Christopher Tate86cab1b2011-01-13 20:28:55 -0800122 // Metadata about the ongoing drag
123 private DragEvent mCurrentDrag;
124 private HashSet<View> mDragNotifiedChildren;
125
Christopher Tatea53146c2010-09-07 11:57:52 -0700126 // 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 // Layout animation
133 private LayoutAnimationController mLayoutAnimationController;
134 private Animation.AnimationListener mAnimationListener;
135
Jeff Brown20e987b2010-08-23 12:01:02 -0700136 // First touch target in the linked list of touch targets.
137 private TouchTarget mFirstTouchTarget;
138
Joe Onorato03ab0c72011-01-06 15:46:27 -0800139 // For debugging only. You can see these in hierarchyviewer.
Romain Guye95003e2011-01-09 13:53:06 -0800140 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800141 @ViewDebug.ExportedProperty(category = "events")
142 private long mLastTouchDownTime;
143 @ViewDebug.ExportedProperty(category = "events")
144 private int mLastTouchDownIndex = -1;
Romain Guye95003e2011-01-09 13:53:06 -0800145 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800146 @ViewDebug.ExportedProperty(category = "events")
147 private float mLastTouchDownX;
Romain Guye95003e2011-01-09 13:53:06 -0800148 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800149 @ViewDebug.ExportedProperty(category = "events")
150 private float mLastTouchDownY;
151
Jeff Brown87b7f802011-06-21 18:35:45 -0700152 // 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;
Jeff Browna032cc02011-03-07 16:56:21 -0800158
Jeff Brown10b62902011-06-20 16:40:37 -0700159 // 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /**
164 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700165 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 * 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.
Romain Guy293451e2009-11-04 13:59:48 -0800216 *
217 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
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}.
Romain Guy8506ab42009-06-11 17:35:47 -0700229 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * {@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.
Adam Powell110486f2010-06-22 17:14:44 -0700288 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 */
Adam Powell110486f2010-06-22 17:14:44 -0700290 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700293 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
294 */
Adam Powellf37df072010-09-17 16:22:49 -0700295 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700296
297 /**
Adam Powell4b867882011-09-16 12:59:46 -0700298 * 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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * 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
Chet Haase9c087442011-01-12 16:20:16 -0800346
347 private boolean mLayoutSuppressed = false;
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 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
Dianne Hackborn0500b3c2011-11-01 15:28:43 -0700355 private Paint mCachePaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356
Chet Haase21cd1382010-09-01 17:42:29 -0700357 // 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
Chet Haase5e25c2c2010-09-16 11:15:56 -0700365 // 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
Romain Guy849d0a32011-02-01 17:20:48 -0800369 // Indicates whether this container will use its children layers to draw
370 @ViewDebug.ExportedProperty(category = "drawing")
371 private boolean mDrawLayers = true;
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 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
Jeff Brown995e7742010-12-22 16:59:36 -0800399 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
400 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
401 }
402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
404
405 mChildren = new View[ARRAY_INITIAL_CAPACITY];
406 mChildrenCount = 0;
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 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;
Adam Powell2b342f02010-08-18 18:14:13 -0700446 case R.styleable.ViewGroup_splitMotionEvents:
447 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
448 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700449 case R.styleable.ViewGroup_animateLayoutChanges:
450 boolean animateLayoutChanges = a.getBoolean(attr, false);
451 if (animateLayoutChanges) {
452 setLayoutTransition(new LayoutTransition());
453 }
454 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
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 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700470 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 @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
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700554 // an ancestor of v; this will get checked for at ViewAncestor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 && !(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 /**
Adam Powell6e346362010-07-23 10:18:23 -0700568 * {@inheritDoc}
569 */
570 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
571 return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null;
572 }
573
574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 * 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 */
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700604 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);
Romain Guy02739a82011-05-16 11:43:18 -0700610 //noinspection SimplifiableIfStatement
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700611 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.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700620 * <p>
Adam Powell2fcbbd02011-09-28 18:56:43 -0700621 * 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)}
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700624 * is responsible for handling this call.
625 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700626 *
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) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700634 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) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700647 return true;
648 }
649
650 /**
651 * {@inheritDoc}
652 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 @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) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700778 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) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 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) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700797 child.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
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())) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700810 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 }
812 }
813
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700814 @Override
Svetoslav Ganovea515ae2011-09-14 18:15:32 -0700815 public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
816 super.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700817 final int childrenCount = mChildrenCount;
818 final View[] children = mChildren;
819 for (int i = 0; i < childrenCount; i++) {
820 View child = children[i];
Svetoslav Ganovea515ae2011-09-14 18:15:32 -0700821 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
822 && (child.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
823 child.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700824 }
825 }
826 }
827
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -0700828 @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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 /**
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 }
Romain Guy43c9cdf2010-01-27 13:53:55 -0800876
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889
890 /**
Chet Haase0d299362012-01-26 10:51:48 -0800891 * Called when a view's visibility has changed. Notify the parent to take any appropriate
892 * action.
893 *
894 * @param child The view whose visibility has changed
895 * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
896 * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
Chet Haase5e25c2c2010-09-16 11:15:56 -0700897 * @hide
Chet Haase5e25c2c2010-09-16 11:15:56 -0700898 */
Chet Haase0d299362012-01-26 10:51:48 -0800899 protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
Chet Haase5e25c2c2010-09-16 11:15:56 -0700900 if (mTransition != null) {
Chet Haase0d299362012-01-26 10:51:48 -0800901 if (newVisibility == VISIBLE) {
902 mTransition.showChild(this, child, oldVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -0700903 } else {
Chet Haase0d299362012-01-26 10:51:48 -0800904 mTransition.hideChild(this, child, newVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -0700905 // Only track this on disappearing views - appearing views are already visible
906 // and don't need special handling during drawChild()
907 if (mVisibilityChangingChildren == null) {
908 mVisibilityChangingChildren = new ArrayList<View>();
909 }
910 mVisibilityChangingChildren.add(child);
911 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
912 addDisappearingView(child);
913 }
914 }
915 }
Christopher Tate86cab1b2011-01-13 20:28:55 -0800916
917 // in all cases, for drags
918 if (mCurrentDrag != null) {
Chet Haase0d299362012-01-26 10:51:48 -0800919 if (newVisibility == VISIBLE) {
Christopher Tate86cab1b2011-01-13 20:28:55 -0800920 notifyChildOfDrag(child);
921 }
922 }
Chet Haase5e25c2c2010-09-16 11:15:56 -0700923 }
924
925 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 * {@inheritDoc}
927 */
928 @Override
Adam Powell326d8082009-12-09 15:10:07 -0800929 protected void dispatchVisibilityChanged(View changedView, int visibility) {
930 super.dispatchVisibilityChanged(changedView, visibility);
931 final int count = mChildrenCount;
932 final View[] children = mChildren;
933 for (int i = 0; i < count; i++) {
934 children[i].dispatchVisibilityChanged(changedView, visibility);
935 }
936 }
937
938 /**
939 * {@inheritDoc}
940 */
941 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 public void dispatchWindowVisibilityChanged(int visibility) {
943 super.dispatchWindowVisibilityChanged(visibility);
944 final int count = mChildrenCount;
945 final View[] children = mChildren;
946 for (int i = 0; i < count; i++) {
947 children[i].dispatchWindowVisibilityChanged(visibility);
948 }
949 }
950
951 /**
952 * {@inheritDoc}
953 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800954 @Override
955 public void dispatchConfigurationChanged(Configuration newConfig) {
956 super.dispatchConfigurationChanged(newConfig);
957 final int count = mChildrenCount;
958 final View[] children = mChildren;
959 for (int i = 0; i < count; i++) {
960 children[i].dispatchConfigurationChanged(newConfig);
961 }
962 }
963
964 /**
965 * {@inheritDoc}
966 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -0800968 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
969 ViewParent parent = mParent;
970 if (parent != null) parent.recomputeViewAttributes(this);
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
Romain Guy8506ab42009-06-11 17:35:47 -0700973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 @Override
975 void dispatchCollectViewAttributes(int visibility) {
976 visibility |= mViewFlags&VISIBILITY_MASK;
977 super.dispatchCollectViewAttributes(visibility);
978 final int count = mChildrenCount;
979 final View[] children = mChildren;
980 for (int i = 0; i < count; i++) {
981 children[i].dispatchCollectViewAttributes(visibility);
982 }
983 }
984
985 /**
986 * {@inheritDoc}
987 */
988 public void bringChildToFront(View child) {
989 int index = indexOfChild(child);
990 if (index >= 0) {
991 removeFromArray(index);
992 addInArray(child, mChildrenCount);
993 child.mParent = this;
994 }
995 }
996
997 /**
998 * {@inheritDoc}
Christopher Tatea53146c2010-09-07 11:57:52 -0700999 *
1000 * !!! TODO: write real docs
1001 */
1002 @Override
1003 public boolean dispatchDragEvent(DragEvent event) {
1004 boolean retval = false;
1005 final float tx = event.mX;
1006 final float ty = event.mY;
1007
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07001008 ViewRootImpl root = getViewRootImpl();
Christopher Tatea53146c2010-09-07 11:57:52 -07001009
1010 // Dispatch down the view hierarchy
1011 switch (event.mAction) {
1012 case DragEvent.ACTION_DRAG_STARTED: {
1013 // clear state to recalculate which views we drag over
Chris Tate9d1ab882010-11-02 15:55:39 -07001014 mCurrentDragView = null;
Christopher Tatea53146c2010-09-07 11:57:52 -07001015
Christopher Tate86cab1b2011-01-13 20:28:55 -08001016 // Set up our tracking of drag-started notifications
1017 mCurrentDrag = DragEvent.obtain(event);
1018 if (mDragNotifiedChildren == null) {
1019 mDragNotifiedChildren = new HashSet<View>();
1020 } else {
1021 mDragNotifiedChildren.clear();
1022 }
1023
Christopher Tatea53146c2010-09-07 11:57:52 -07001024 // Now dispatch down to our children, caching the responses
1025 mChildAcceptsDrag = false;
1026 final int count = mChildrenCount;
1027 final View[] children = mChildren;
1028 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001029 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001030 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001031 if (child.getVisibility() == VISIBLE) {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001032 final boolean handled = notifyChildOfDrag(children[i]);
Christopher Tate2c095f32010-10-04 14:13:40 -07001033 if (handled) {
1034 mChildAcceptsDrag = true;
1035 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001036 }
1037 }
1038
1039 // Return HANDLED if one of our children can accept the drag
1040 if (mChildAcceptsDrag) {
1041 retval = true;
1042 }
1043 } break;
1044
1045 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001046 // Release the bookkeeping now that the drag lifecycle has ended
Christopher Tate1fc014f2011-01-19 12:56:26 -08001047 if (mDragNotifiedChildren != null) {
1048 for (View child : mDragNotifiedChildren) {
1049 // If a child was notified about an ongoing drag, it's told that it's over
1050 child.dispatchDragEvent(event);
Christopher Tate3d4bf172011-03-28 16:16:46 -07001051 child.mPrivateFlags2 &= ~View.DRAG_MASK;
1052 child.refreshDrawableState();
Christopher Tate1fc014f2011-01-19 12:56:26 -08001053 }
1054
1055 mDragNotifiedChildren.clear();
1056 mCurrentDrag.recycle();
1057 mCurrentDrag = null;
1058 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001059
Christopher Tatea53146c2010-09-07 11:57:52 -07001060 // We consider drag-ended to have been handled if one of our children
1061 // had offered to handle the drag.
1062 if (mChildAcceptsDrag) {
1063 retval = true;
1064 }
1065 } break;
1066
1067 case DragEvent.ACTION_DRAG_LOCATION: {
1068 // Find the [possibly new] drag target
1069 final View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
1070
1071 // If we've changed apparent drag target, tell the view root which view
Chris Tate9d1ab882010-11-02 15:55:39 -07001072 // we're over now [for purposes of the eventual drag-recipient-changed
1073 // notifications to the framework] and tell the new target that the drag
1074 // has entered its bounds. The root will see setDragFocus() calls all
1075 // the way down to the final leaf view that is handling the LOCATION event
1076 // before reporting the new potential recipient to the framework.
Christopher Tatea53146c2010-09-07 11:57:52 -07001077 if (mCurrentDragView != target) {
Chris Tate9d1ab882010-11-02 15:55:39 -07001078 root.setDragFocus(target);
1079
1080 final int action = event.mAction;
1081 // If we've dragged off of a child view, send it the EXITED message
1082 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001083 final View view = mCurrentDragView;
Chris Tate9d1ab882010-11-02 15:55:39 -07001084 event.mAction = DragEvent.ACTION_DRAG_EXITED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001085 view.dispatchDragEvent(event);
1086 view.mPrivateFlags2 &= ~View.DRAG_HOVERED;
1087 view.refreshDrawableState();
Chris Tate9d1ab882010-11-02 15:55:39 -07001088 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001089 mCurrentDragView = target;
Chris Tate9d1ab882010-11-02 15:55:39 -07001090
1091 // If we've dragged over a new child view, send it the ENTERED message
1092 if (target != null) {
1093 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
1094 target.dispatchDragEvent(event);
Christopher Tate3d4bf172011-03-28 16:16:46 -07001095 target.mPrivateFlags2 |= View.DRAG_HOVERED;
1096 target.refreshDrawableState();
Chris Tate9d1ab882010-11-02 15:55:39 -07001097 }
1098 event.mAction = action; // restore the event's original state
Christopher Tatea53146c2010-09-07 11:57:52 -07001099 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001100
Christopher Tatea53146c2010-09-07 11:57:52 -07001101 // Dispatch the actual drag location notice, localized into its coordinates
1102 if (target != null) {
1103 event.mX = mLocalPoint.x;
1104 event.mY = mLocalPoint.y;
1105
1106 retval = target.dispatchDragEvent(event);
1107
1108 event.mX = tx;
1109 event.mY = ty;
1110 }
1111 } break;
1112
Chris Tate9d1ab882010-11-02 15:55:39 -07001113 /* Entered / exited dispatch
1114 *
1115 * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is
1116 * that we're about to get the corresponding LOCATION event, which we will use to
1117 * determine which of our children is the new target; at that point we will
1118 * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
1119 *
1120 * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1121 * drag has left this ViewGroup, we know by definition that every contained subview
1122 * is also no longer under the drag point.
1123 */
1124
1125 case DragEvent.ACTION_DRAG_EXITED: {
1126 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001127 final View view = mCurrentDragView;
1128 view.dispatchDragEvent(event);
1129 view.mPrivateFlags2 &= ~View.DRAG_HOVERED;
1130 view.refreshDrawableState();
1131
Chris Tate9d1ab882010-11-02 15:55:39 -07001132 mCurrentDragView = null;
1133 }
1134 } break;
1135
Christopher Tatea53146c2010-09-07 11:57:52 -07001136 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -07001137 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001138 View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
1139 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001140 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Christopher Tatea53146c2010-09-07 11:57:52 -07001141 event.mX = mLocalPoint.x;
1142 event.mY = mLocalPoint.y;
1143 retval = target.dispatchDragEvent(event);
1144 event.mX = tx;
1145 event.mY = ty;
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001146 } else {
1147 if (ViewDebug.DEBUG_DRAG) {
1148 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
1149 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001150 }
1151 } break;
1152 }
1153
1154 // If none of our children could handle the event, try here
1155 if (!retval) {
Chris Tate32affef2010-10-18 15:29:21 -07001156 // Call up to the View implementation that dispatches to installed listeners
1157 retval = super.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001158 }
1159 return retval;
1160 }
1161
1162 // Find the frontmost child view that lies under the given point, and calculate
1163 // the position within its own local coordinate system.
1164 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001165 final int count = mChildrenCount;
1166 final View[] children = mChildren;
1167 for (int i = count - 1; i >= 0; i--) {
1168 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001169 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001170 continue;
1171 }
1172
Christopher Tate2c095f32010-10-04 14:13:40 -07001173 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001174 return child;
1175 }
1176 }
1177 return null;
1178 }
1179
Christopher Tate86cab1b2011-01-13 20:28:55 -08001180 boolean notifyChildOfDrag(View child) {
1181 if (ViewDebug.DEBUG_DRAG) {
1182 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1183 }
1184
Christopher Tate3d4bf172011-03-28 16:16:46 -07001185 boolean canAccept = false;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001186 if (! mDragNotifiedChildren.contains(child)) {
1187 mDragNotifiedChildren.add(child);
Christopher Tate3d4bf172011-03-28 16:16:46 -07001188 canAccept = child.dispatchDragEvent(mCurrentDrag);
1189 if (canAccept && !child.canAcceptDrag()) {
1190 child.mPrivateFlags2 |= View.DRAG_CAN_ACCEPT;
1191 child.refreshDrawableState();
1192 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001193 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001194 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001195 }
1196
Joe Onorato664644d2011-01-23 17:53:23 -08001197 @Override
1198 public void dispatchSystemUiVisibilityChanged(int visible) {
1199 super.dispatchSystemUiVisibilityChanged(visible);
1200
1201 final int count = mChildrenCount;
1202 final View[] children = mChildren;
1203 for (int i=0; i <count; i++) {
1204 final View child = children[i];
1205 child.dispatchSystemUiVisibilityChanged(visible);
1206 }
1207 }
1208
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001209 @Override
1210 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
1211 super.updateLocalSystemUiVisibility(localValue, localChanges);
1212
1213 final int count = mChildrenCount;
1214 final View[] children = mChildren;
1215 for (int i=0; i <count; i++) {
1216 final View child = children[i];
1217 child.updateLocalSystemUiVisibility(localValue, localChanges);
1218 }
1219 }
1220
Christopher Tatea53146c2010-09-07 11:57:52 -07001221 /**
1222 * {@inheritDoc}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 */
1224 @Override
1225 public boolean dispatchKeyEventPreIme(KeyEvent event) {
1226 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1227 return super.dispatchKeyEventPreIme(event);
1228 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1229 return mFocused.dispatchKeyEventPreIme(event);
1230 }
1231 return false;
1232 }
1233
1234 /**
1235 * {@inheritDoc}
1236 */
1237 @Override
1238 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001239 if (mInputEventConsistencyVerifier != null) {
1240 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1241 }
1242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001244 if (super.dispatchKeyEvent(event)) {
1245 return true;
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001248 if (mFocused.dispatchKeyEvent(event)) {
1249 return true;
1250 }
1251 }
1252
1253 if (mInputEventConsistencyVerifier != null) {
1254 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256 return false;
1257 }
1258
1259 /**
1260 * {@inheritDoc}
1261 */
1262 @Override
1263 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
1264 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1265 return super.dispatchKeyShortcutEvent(event);
1266 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1267 return mFocused.dispatchKeyShortcutEvent(event);
1268 }
1269 return false;
1270 }
1271
1272 /**
1273 * {@inheritDoc}
1274 */
1275 @Override
1276 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001277 if (mInputEventConsistencyVerifier != null) {
1278 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1279 }
1280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001282 if (super.dispatchTrackballEvent(event)) {
1283 return true;
1284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001286 if (mFocused.dispatchTrackballEvent(event)) {
1287 return true;
1288 }
1289 }
1290
1291 if (mInputEventConsistencyVerifier != null) {
1292 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294 return false;
1295 }
1296
Jeff Brown10b62902011-06-20 16:40:37 -07001297 /**
1298 * {@inheritDoc}
1299 */
Romain Guya9489272011-06-22 20:58:11 -07001300 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08001302 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08001303 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08001304
Jeff Brown10b62902011-06-20 16:40:37 -07001305 // First check whether the view group wants to intercept the hover event.
1306 final boolean interceptHover = onInterceptHoverEvent(event);
1307 event.setAction(action); // restore action in case it was changed
1308
Jeff Brown87b7f802011-06-21 18:35:45 -07001309 MotionEvent eventNoHistory = event;
1310 boolean handled = false;
1311
1312 // Send events to the hovered children and build a new list of hover targets until
1313 // one is found that handles the event.
1314 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1315 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07001316 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08001317 final float x = event.getX();
1318 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001319 final int childrenCount = mChildrenCount;
1320 if (childrenCount != 0) {
1321 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07001322 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001323 for (int i = childrenCount - 1; i >= 0; i--) {
1324 final View child = children[i];
Jeff Brown87b7f802011-06-21 18:35:45 -07001325 if (!canViewReceivePointerEvents(child)
1326 || !isTransformedTouchPointInView(x, y, child, null)) {
1327 continue;
1328 }
1329
1330 // Obtain a hover target for this child. Dequeue it from the
1331 // old hover target list if the child was previously hovered.
1332 HoverTarget hoverTarget = firstOldHoverTarget;
1333 final boolean wasHovered;
1334 for (HoverTarget predecessor = null; ;) {
1335 if (hoverTarget == null) {
1336 hoverTarget = HoverTarget.obtain(child);
1337 wasHovered = false;
1338 break;
1339 }
1340
1341 if (hoverTarget.child == child) {
1342 if (predecessor != null) {
1343 predecessor.next = hoverTarget.next;
1344 } else {
1345 firstOldHoverTarget = hoverTarget.next;
1346 }
1347 hoverTarget.next = null;
1348 wasHovered = true;
1349 break;
1350 }
1351
1352 predecessor = hoverTarget;
1353 hoverTarget = hoverTarget.next;
1354 }
1355
1356 // Enqueue the hover target onto the new hover target list.
1357 if (lastHoverTarget != null) {
1358 lastHoverTarget.next = hoverTarget;
1359 } else {
1360 lastHoverTarget = hoverTarget;
1361 mFirstHoverTarget = hoverTarget;
1362 }
1363
1364 // Dispatch the event to the child.
1365 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1366 if (!wasHovered) {
1367 // Send the enter as is.
1368 handled |= dispatchTransformedGenericPointerEvent(
1369 event, child); // enter
1370 }
1371 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1372 if (!wasHovered) {
1373 // Synthesize an enter from a move.
1374 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1375 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1376 handled |= dispatchTransformedGenericPointerEvent(
1377 eventNoHistory, child); // enter
1378 eventNoHistory.setAction(action);
1379
1380 handled |= dispatchTransformedGenericPointerEvent(
1381 eventNoHistory, child); // move
1382 } else {
1383 // Send the move as is.
1384 handled |= dispatchTransformedGenericPointerEvent(event, child);
1385 }
1386 }
1387 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07001388 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001389 }
Jeff Brown10b62902011-06-20 16:40:37 -07001390 }
1391 }
1392 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001393
Jeff Brown87b7f802011-06-21 18:35:45 -07001394 // Send exit events to all previously hovered children that are no longer hovered.
1395 while (firstOldHoverTarget != null) {
1396 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07001397
Jeff Brown87b7f802011-06-21 18:35:45 -07001398 // Exit the old hovered child.
1399 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1400 // Send the exit as is.
1401 handled |= dispatchTransformedGenericPointerEvent(
1402 event, child); // exit
1403 } else {
1404 // Synthesize an exit from a move or enter.
1405 // Ignore the result because hover focus has moved to a different view.
1406 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brown10b62902011-06-20 16:40:37 -07001407 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07001408 event, child); // move
Jeff Brown10b62902011-06-20 16:40:37 -07001409 }
Jeff Brown87b7f802011-06-21 18:35:45 -07001410 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1411 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1412 dispatchTransformedGenericPointerEvent(
1413 eventNoHistory, child); // exit
1414 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07001415 }
1416
Jeff Brown87b7f802011-06-21 18:35:45 -07001417 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1418 firstOldHoverTarget.recycle();
1419 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07001420 }
1421
Jeff Brown87b7f802011-06-21 18:35:45 -07001422 // Send events to the view group itself if no children have handled it.
Jeff Brown10b62902011-06-20 16:40:37 -07001423 boolean newHoveredSelf = !handled;
1424 if (newHoveredSelf == mHoveredSelf) {
1425 if (newHoveredSelf) {
1426 // Send event to the view group as before.
1427 handled |= super.dispatchHoverEvent(event);
1428 }
1429 } else {
1430 if (mHoveredSelf) {
1431 // Exit the view group.
1432 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1433 // Send the exit as is.
1434 handled |= super.dispatchHoverEvent(event); // exit
1435 } else {
1436 // Synthesize an exit from a move or enter.
1437 // Ignore the result because hover focus is moving to a different view.
1438 if (action == MotionEvent.ACTION_HOVER_MOVE) {
1439 super.dispatchHoverEvent(event); // move
1440 }
1441 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1442 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1443 super.dispatchHoverEvent(eventNoHistory); // exit
1444 eventNoHistory.setAction(action);
1445 }
1446 mHoveredSelf = false;
1447 }
1448
1449 if (newHoveredSelf) {
1450 // Enter the view group.
1451 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1452 // Send the enter as is.
1453 handled |= super.dispatchHoverEvent(event); // enter
1454 mHoveredSelf = true;
1455 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1456 // Synthesize an enter from a move.
1457 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1458 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1459 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1460 eventNoHistory.setAction(action);
1461
1462 handled |= super.dispatchHoverEvent(eventNoHistory); // move
1463 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001464 }
1465 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001466 }
1467
Jeff Browna032cc02011-03-07 16:56:21 -08001468 // Recycle the copy of the event that we made.
1469 if (eventNoHistory != event) {
1470 eventNoHistory.recycle();
1471 }
1472
Jeff Browna032cc02011-03-07 16:56:21 -08001473 // Done.
1474 return handled;
1475 }
1476
Jeff Brown87b7f802011-06-21 18:35:45 -07001477 /** @hide */
1478 @Override
1479 protected boolean hasHoveredChild() {
1480 return mFirstHoverTarget != null;
1481 }
1482
Jeff Brown10b62902011-06-20 16:40:37 -07001483 /**
1484 * Implement this method to intercept hover events before they are handled
1485 * by child views.
1486 * <p>
1487 * This method is called before dispatching a hover event to a child of
1488 * the view group or to the view group's own {@link #onHoverEvent} to allow
1489 * the view group a chance to intercept the hover event.
1490 * This method can also be used to watch all pointer motions that occur within
1491 * the bounds of the view group even when the pointer is hovering over
1492 * a child of the view group rather than over the view group itself.
1493 * </p><p>
1494 * The view group can prevent its children from receiving hover events by
1495 * implementing this method and returning <code>true</code> to indicate
1496 * that it would like to intercept hover events. The view group must
1497 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
1498 * for as long as it wishes to continue intercepting hover events from
1499 * its children.
1500 * </p><p>
1501 * Interception preserves the invariant that at most one view can be
1502 * hovered at a time by transferring hover focus from the currently hovered
1503 * child to the view group or vice-versa as needed.
1504 * </p><p>
1505 * If this method returns <code>true</code> and a child is already hovered, then the
1506 * child view will first receive a hover exit event and then the view group
1507 * itself will receive a hover enter event in {@link #onHoverEvent}.
1508 * Likewise, if this method had previously returned <code>true</code> to intercept hover
1509 * events and instead returns <code>false</code> while the pointer is hovering
1510 * within the bounds of one of a child, then the view group will first receive a
1511 * hover exit event in {@link #onHoverEvent} and then the hovered child will
1512 * receive a hover enter event.
1513 * </p><p>
1514 * The default implementation always returns false.
1515 * </p>
1516 *
1517 * @param event The motion event that describes the hover.
1518 * @return True if the view group would like to intercept the hover event
1519 * and prevent its children from receiving it.
1520 */
1521 public boolean onInterceptHoverEvent(MotionEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001522 return false;
1523 }
1524
Jeff Browna032cc02011-03-07 16:56:21 -08001525 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
1526 if (event.getHistorySize() == 0) {
1527 return event;
1528 }
1529 return MotionEvent.obtainNoHistory(event);
1530 }
1531
Jeff Brown10b62902011-06-20 16:40:37 -07001532 /**
1533 * {@inheritDoc}
1534 */
Jeff Browna032cc02011-03-07 16:56:21 -08001535 @Override
1536 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
1537 // Send the event to the child under the pointer.
1538 final int childrenCount = mChildrenCount;
1539 if (childrenCount != 0) {
1540 final View[] children = mChildren;
1541 final float x = event.getX();
1542 final float y = event.getY();
1543
1544 for (int i = childrenCount - 1; i >= 0; i--) {
1545 final View child = children[i];
1546 if (!canViewReceivePointerEvents(child)
1547 || !isTransformedTouchPointInView(x, y, child, null)) {
1548 continue;
1549 }
1550
1551 if (dispatchTransformedGenericPointerEvent(event, child)) {
1552 return true;
1553 }
1554 }
1555 }
1556
1557 // No child handled the event. Send it to this view group.
1558 return super.dispatchGenericPointerEvent(event);
1559 }
1560
Jeff Brown10b62902011-06-20 16:40:37 -07001561 /**
1562 * {@inheritDoc}
1563 */
Jeff Browna032cc02011-03-07 16:56:21 -08001564 @Override
1565 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001566 // Send the event to the focused child or to this view group if it has focus.
Jeff Browncb1404e2011-01-15 18:14:15 -08001567 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08001568 return super.dispatchGenericFocusedEvent(event);
Jeff Browncb1404e2011-01-15 18:14:15 -08001569 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1570 return mFocused.dispatchGenericMotionEvent(event);
1571 }
1572 return false;
1573 }
1574
1575 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001576 * Dispatches a generic pointer event to a child, taking into account
1577 * transformations that apply to the child.
1578 *
1579 * @param event The event to send.
1580 * @param child The view to send the event to.
1581 * @return {@code true} if the child handled the event.
1582 */
1583 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
1584 final float offsetX = mScrollX - child.mLeft;
1585 final float offsetY = mScrollY - child.mTop;
1586
1587 boolean handled;
1588 if (!child.hasIdentityMatrix()) {
1589 MotionEvent transformedEvent = MotionEvent.obtain(event);
1590 transformedEvent.offsetLocation(offsetX, offsetY);
1591 transformedEvent.transform(child.getInverseMatrix());
1592 handled = child.dispatchGenericMotionEvent(transformedEvent);
1593 transformedEvent.recycle();
1594 } else {
1595 event.offsetLocation(offsetX, offsetY);
1596 handled = child.dispatchGenericMotionEvent(event);
1597 event.offsetLocation(-offsetX, -offsetY);
1598 }
1599 return handled;
1600 }
1601
1602 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08001603 * {@inheritDoc}
1604 */
1605 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001607 if (mInputEventConsistencyVerifier != null) {
1608 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
1609 }
1610
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001611 boolean handled = false;
1612 if (onFilterTouchEventForSecurity(ev)) {
1613 final int action = ev.getAction();
1614 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07001615
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001616 // Handle an initial down.
1617 if (actionMasked == MotionEvent.ACTION_DOWN) {
1618 // Throw away all previous state when starting a new touch gesture.
1619 // The framework may have dropped the up or cancel event for the previous gesture
1620 // due to an app switch, ANR, or some other state change.
1621 cancelAndClearTouchTargets(ev);
1622 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
Adam Powellb08013c2010-09-16 16:28:11 -07001624
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001625 // Check for interception.
1626 final boolean intercepted;
Jeff Brown20e987b2010-08-23 12:01:02 -07001627 if (actionMasked == MotionEvent.ACTION_DOWN
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001628 || mFirstTouchTarget != null) {
1629 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
1630 if (!disallowIntercept) {
1631 intercepted = onInterceptTouchEvent(ev);
1632 ev.setAction(action); // restore action in case it was changed
1633 } else {
1634 intercepted = false;
1635 }
1636 } else {
1637 // There are no touch targets and this action is not an initial down
1638 // so this view group continues to intercept touches.
1639 intercepted = true;
1640 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001641
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001642 // Check for cancelation.
1643 final boolean canceled = resetCancelNextUpFlag(this)
1644 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07001645
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001646 // Update list of touch targets for pointer down, if needed.
1647 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
1648 TouchTarget newTouchTarget = null;
1649 boolean alreadyDispatchedToNewTouchTarget = false;
1650 if (!canceled && !intercepted) {
1651 if (actionMasked == MotionEvent.ACTION_DOWN
1652 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
1653 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
1654 final int actionIndex = ev.getActionIndex(); // always 0 for down
1655 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
1656 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07001657
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001658 // Clean up earlier touch targets for this pointer id in case they
1659 // have become out of sync.
1660 removePointersFromTouchTargets(idBitsToAssign);
1661
1662 final int childrenCount = mChildrenCount;
1663 if (childrenCount != 0) {
1664 // Find a child that can receive the event.
1665 // Scan children from front to back.
1666 final View[] children = mChildren;
1667 final float x = ev.getX(actionIndex);
1668 final float y = ev.getY(actionIndex);
1669
1670 for (int i = childrenCount - 1; i >= 0; i--) {
1671 final View child = children[i];
1672 if (!canViewReceivePointerEvents(child)
1673 || !isTransformedTouchPointInView(x, y, child, null)) {
1674 continue;
1675 }
1676
1677 newTouchTarget = getTouchTarget(child);
1678 if (newTouchTarget != null) {
1679 // Child is already receiving touch within its bounds.
1680 // Give it the new pointer in addition to the ones it is handling.
1681 newTouchTarget.pointerIdBits |= idBitsToAssign;
1682 break;
1683 }
1684
1685 resetCancelNextUpFlag(child);
1686 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
1687 // Child wants to receive touch within its bounds.
1688 mLastTouchDownTime = ev.getDownTime();
1689 mLastTouchDownIndex = i;
1690 mLastTouchDownX = ev.getX();
1691 mLastTouchDownY = ev.getY();
1692 newTouchTarget = addTouchTarget(child, idBitsToAssign);
1693 alreadyDispatchedToNewTouchTarget = true;
1694 break;
1695 }
1696 }
1697 }
1698
1699 if (newTouchTarget == null && mFirstTouchTarget != null) {
1700 // Did not find a child to receive the event.
1701 // Assign the pointer to the least recently added target.
1702 newTouchTarget = mFirstTouchTarget;
1703 while (newTouchTarget.next != null) {
1704 newTouchTarget = newTouchTarget.next;
1705 }
1706 newTouchTarget.pointerIdBits |= idBitsToAssign;
1707 }
1708 }
1709 }
1710
1711 // Dispatch to touch targets.
1712 if (mFirstTouchTarget == null) {
1713 // No touch targets so treat this as an ordinary view.
1714 handled = dispatchTransformedTouchEvent(ev, canceled, null,
1715 TouchTarget.ALL_POINTER_IDS);
1716 } else {
1717 // Dispatch to touch targets, excluding the new touch target if we already
1718 // dispatched to it. Cancel touch targets if necessary.
1719 TouchTarget predecessor = null;
1720 TouchTarget target = mFirstTouchTarget;
1721 while (target != null) {
1722 final TouchTarget next = target.next;
1723 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
1724 handled = true;
1725 } else {
1726 final boolean cancelChild = resetCancelNextUpFlag(target.child)
1727 || intercepted;
1728 if (dispatchTransformedTouchEvent(ev, cancelChild,
1729 target.child, target.pointerIdBits)) {
1730 handled = true;
1731 }
1732 if (cancelChild) {
1733 if (predecessor == null) {
1734 mFirstTouchTarget = next;
1735 } else {
1736 predecessor.next = next;
1737 }
1738 target.recycle();
1739 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07001740 continue;
1741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001743 predecessor = target;
1744 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001746 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001747
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001748 // Update list of touch targets for pointer up or cancel, if needed.
1749 if (canceled
1750 || actionMasked == MotionEvent.ACTION_UP
1751 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
1752 resetTouchState();
1753 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
1754 final int actionIndex = ev.getActionIndex();
1755 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
1756 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758 }
Romain Guy8506ab42009-06-11 17:35:47 -07001759
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001760 if (!handled && mInputEventConsistencyVerifier != null) {
1761 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07001762 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001763 return handled;
1764 }
1765
Romain Guy469b1db2010-10-05 11:49:57 -07001766 /**
1767 * Resets all touch state in preparation for a new cycle.
1768 */
1769 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07001770 clearTouchTargets();
1771 resetCancelNextUpFlag(this);
1772 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
1773 }
1774
Romain Guy469b1db2010-10-05 11:49:57 -07001775 /**
1776 * Resets the cancel next up flag.
1777 * Returns true if the flag was previously set.
1778 */
1779 private boolean resetCancelNextUpFlag(View view) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001780 if ((view.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) {
1781 view.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
1782 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07001783 }
1784 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786
Romain Guy469b1db2010-10-05 11:49:57 -07001787 /**
1788 * Clears all touch targets.
1789 */
1790 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07001791 TouchTarget target = mFirstTouchTarget;
1792 if (target != null) {
1793 do {
1794 TouchTarget next = target.next;
1795 target.recycle();
1796 target = next;
1797 } while (target != null);
1798 mFirstTouchTarget = null;
1799 }
1800 }
1801
Romain Guy469b1db2010-10-05 11:49:57 -07001802 /**
1803 * Cancels and clears all touch targets.
1804 */
1805 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001806 if (mFirstTouchTarget != null) {
1807 boolean syntheticEvent = false;
1808 if (event == null) {
1809 final long now = SystemClock.uptimeMillis();
1810 event = MotionEvent.obtain(now, now,
1811 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07001812 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07001813 syntheticEvent = true;
1814 }
1815
1816 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
1817 resetCancelNextUpFlag(target.child);
1818 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
1819 }
1820 clearTouchTargets();
1821
1822 if (syntheticEvent) {
1823 event.recycle();
1824 }
1825 }
1826 }
1827
Romain Guy469b1db2010-10-05 11:49:57 -07001828 /**
1829 * Gets the touch target for specified child view.
1830 * Returns null if not found.
1831 */
1832 private TouchTarget getTouchTarget(View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001833 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
1834 if (target.child == child) {
1835 return target;
1836 }
1837 }
1838 return null;
1839 }
1840
Romain Guy469b1db2010-10-05 11:49:57 -07001841 /**
1842 * Adds a touch target for specified child to the beginning of the list.
1843 * Assumes the target child is not already present.
1844 */
1845 private TouchTarget addTouchTarget(View child, int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001846 TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
1847 target.next = mFirstTouchTarget;
1848 mFirstTouchTarget = target;
1849 return target;
1850 }
1851
Romain Guy469b1db2010-10-05 11:49:57 -07001852 /**
1853 * Removes the pointer ids from consideration.
1854 */
1855 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001856 TouchTarget predecessor = null;
1857 TouchTarget target = mFirstTouchTarget;
1858 while (target != null) {
1859 final TouchTarget next = target.next;
1860 if ((target.pointerIdBits & pointerIdBits) != 0) {
1861 target.pointerIdBits &= ~pointerIdBits;
1862 if (target.pointerIdBits == 0) {
1863 if (predecessor == null) {
1864 mFirstTouchTarget = next;
1865 } else {
1866 predecessor.next = next;
1867 }
1868 target.recycle();
1869 target = next;
1870 continue;
1871 }
1872 }
1873 predecessor = target;
1874 target = next;
1875 }
1876 }
1877
Romain Guy469b1db2010-10-05 11:49:57 -07001878 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001879 * Returns true if a child view can receive pointer events.
1880 * @hide
1881 */
1882 private static boolean canViewReceivePointerEvents(View child) {
1883 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
1884 || child.getAnimation() != null;
1885 }
1886
1887 /**
Romain Guy469b1db2010-10-05 11:49:57 -07001888 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07001889 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07001890 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07001891 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07001892 */
Adam Cohena32edd42010-10-26 10:35:01 -07001893 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07001894 PointF outLocalPoint) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001895 float localX = x + mScrollX - child.mLeft;
1896 float localY = y + mScrollY - child.mTop;
1897 if (! child.hasIdentityMatrix() && mAttachInfo != null) {
Adam Powell2b342f02010-08-18 18:14:13 -07001898 final float[] localXY = mAttachInfo.mTmpTransformLocation;
1899 localXY[0] = localX;
1900 localXY[1] = localY;
1901 child.getInverseMatrix().mapPoints(localXY);
1902 localX = localXY[0];
1903 localY = localXY[1];
1904 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001905 final boolean isInView = child.pointInView(localX, localY);
1906 if (isInView && outLocalPoint != null) {
1907 outLocalPoint.set(localX, localY);
1908 }
1909 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07001910 }
1911
Romain Guy469b1db2010-10-05 11:49:57 -07001912 /**
1913 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07001914 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07001915 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
1916 */
1917 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07001918 View child, int desiredPointerIdBits) {
1919 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07001920
Jeff Brown20e987b2010-08-23 12:01:02 -07001921 // Canceling motions is a special case. We don't need to perform any transformations
1922 // or filtering. The important part is the action, not the contents.
1923 final int oldAction = event.getAction();
1924 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
1925 event.setAction(MotionEvent.ACTION_CANCEL);
1926 if (child == null) {
1927 handled = super.dispatchTouchEvent(event);
1928 } else {
1929 handled = child.dispatchTouchEvent(event);
1930 }
1931 event.setAction(oldAction);
1932 return handled;
1933 }
Adam Powell2b342f02010-08-18 18:14:13 -07001934
Jeff Brown20e987b2010-08-23 12:01:02 -07001935 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001936 final int oldPointerIdBits = event.getPointerIdBits();
1937 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07001938
Jeff Brown20e987b2010-08-23 12:01:02 -07001939 // If for some reason we ended up in an inconsistent state where it looks like we
1940 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001941 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001942 return false;
1943 }
Adam Powell2b342f02010-08-18 18:14:13 -07001944
Jeff Brown20e987b2010-08-23 12:01:02 -07001945 // If the number of pointers is the same and we don't need to perform any fancy
1946 // irreversible transformations, then we can reuse the motion event for this
1947 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001948 // Otherwise we need to make a copy.
1949 final MotionEvent transformedEvent;
1950 if (newPointerIdBits == oldPointerIdBits) {
1951 if (child == null || child.hasIdentityMatrix()) {
1952 if (child == null) {
1953 handled = super.dispatchTouchEvent(event);
1954 } else {
1955 final float offsetX = mScrollX - child.mLeft;
1956 final float offsetY = mScrollY - child.mTop;
1957 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07001958
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001959 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07001960
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001961 event.offsetLocation(-offsetX, -offsetY);
1962 }
1963 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07001964 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001965 transformedEvent = MotionEvent.obtain(event);
1966 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001967 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07001968 }
1969
Jeff Brown20e987b2010-08-23 12:01:02 -07001970 // Perform any necessary transformations and dispatch.
1971 if (child == null) {
1972 handled = super.dispatchTouchEvent(transformedEvent);
1973 } else {
1974 final float offsetX = mScrollX - child.mLeft;
1975 final float offsetY = mScrollY - child.mTop;
1976 transformedEvent.offsetLocation(offsetX, offsetY);
1977 if (! child.hasIdentityMatrix()) {
1978 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07001979 }
1980
Jeff Brown20e987b2010-08-23 12:01:02 -07001981 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07001982 }
1983
Jeff Brown20e987b2010-08-23 12:01:02 -07001984 // Done.
1985 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07001986 return handled;
1987 }
1988
Romain Guy469b1db2010-10-05 11:49:57 -07001989 /**
Adam Powell2b342f02010-08-18 18:14:13 -07001990 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08001991 * dispatch. This behavior is enabled by default for applications that target an
1992 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07001993 *
1994 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
1995 * views depending on where each pointer initially went down. This allows for user interactions
1996 * such as scrolling two panes of content independently, chording of buttons, and performing
1997 * independent gestures on different pieces of content.
1998 *
1999 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2000 * child views. <code>false</code> to only allow one child view to be the target of
2001 * any MotionEvent received by this ViewGroup.
2002 */
2003 public void setMotionEventSplittingEnabled(boolean split) {
2004 // TODO Applications really shouldn't change this setting mid-touch event,
2005 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2006 // with gestures in progress when this is changed.
2007 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07002008 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2009 } else {
2010 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07002011 }
2012 }
2013
2014 /**
Jeff Brown995e7742010-12-22 16:59:36 -08002015 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07002016 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2017 */
2018 public boolean isMotionEventSplittingEnabled() {
2019 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2020 }
2021
2022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 * {@inheritDoc}
2024 */
2025 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07002026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2028 // We're already in this state, assume our ancestors are too
2029 return;
2030 }
Romain Guy8506ab42009-06-11 17:35:47 -07002031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if (disallowIntercept) {
2033 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2034 } else {
2035 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2036 }
Romain Guy8506ab42009-06-11 17:35:47 -07002037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 // Pass it up to our parent
2039 if (mParent != null) {
2040 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2041 }
2042 }
2043
2044 /**
2045 * Implement this method to intercept all touch screen motion events. This
2046 * allows you to watch events as they are dispatched to your children, and
2047 * take ownership of the current gesture at any point.
2048 *
2049 * <p>Using this function takes some care, as it has a fairly complicated
2050 * interaction with {@link View#onTouchEvent(MotionEvent)
2051 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2052 * that method as well as this one in the correct way. Events will be
2053 * received in the following order:
2054 *
2055 * <ol>
2056 * <li> You will receive the down event here.
2057 * <li> The down event will be handled either by a child of this view
2058 * group, or given to your own onTouchEvent() method to handle; this means
2059 * you should implement onTouchEvent() to return true, so you will
2060 * continue to see the rest of the gesture (instead of looking for
2061 * a parent view to handle it). Also, by returning true from
2062 * onTouchEvent(), you will not receive any following
2063 * events in onInterceptTouchEvent() and all touch processing must
2064 * happen in onTouchEvent() like normal.
2065 * <li> For as long as you return false from this function, each following
2066 * event (up to and including the final up) will be delivered first here
2067 * and then to the target's onTouchEvent().
2068 * <li> If you return true from here, you will not receive any
2069 * following events: the target view will receive the same event but
2070 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2071 * events will be delivered to your onTouchEvent() method and no longer
2072 * appear here.
2073 * </ol>
2074 *
2075 * @param ev The motion event being dispatched down the hierarchy.
2076 * @return Return true to steal motion events from the children and have
2077 * them dispatched to this ViewGroup through onTouchEvent().
2078 * The current target will receive an ACTION_CANCEL event, and no further
2079 * messages will be delivered here.
2080 */
2081 public boolean onInterceptTouchEvent(MotionEvent ev) {
2082 return false;
2083 }
2084
2085 /**
2086 * {@inheritDoc}
2087 *
2088 * Looks for a view to give focus to respecting the setting specified by
2089 * {@link #getDescendantFocusability()}.
2090 *
2091 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2092 * find focus within the children of this group when appropriate.
2093 *
2094 * @see #FOCUS_BEFORE_DESCENDANTS
2095 * @see #FOCUS_AFTER_DESCENDANTS
2096 * @see #FOCUS_BLOCK_DESCENDANTS
Romain Guy02739a82011-05-16 11:43:18 -07002097 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 */
2099 @Override
2100 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2101 if (DBG) {
2102 System.out.println(this + " ViewGroup.requestFocus direction="
2103 + direction);
2104 }
2105 int descendantFocusability = getDescendantFocusability();
2106
2107 switch (descendantFocusability) {
2108 case FOCUS_BLOCK_DESCENDANTS:
2109 return super.requestFocus(direction, previouslyFocusedRect);
2110 case FOCUS_BEFORE_DESCENDANTS: {
2111 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2112 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2113 }
2114 case FOCUS_AFTER_DESCENDANTS: {
2115 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2116 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2117 }
2118 default:
2119 throw new IllegalStateException("descendant focusability must be "
2120 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2121 + "but is " + descendantFocusability);
2122 }
2123 }
2124
2125 /**
2126 * Look for a descendant to call {@link View#requestFocus} on.
2127 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2128 * when it wants to request focus within its children. Override this to
2129 * customize how your {@link ViewGroup} requests focus within its children.
2130 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2131 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2132 * to give a finer grained hint about where focus is coming from. May be null
2133 * if there is no hint.
2134 * @return Whether focus was taken.
2135 */
2136 @SuppressWarnings({"ConstantConditions"})
2137 protected boolean onRequestFocusInDescendants(int direction,
2138 Rect previouslyFocusedRect) {
2139 int index;
2140 int increment;
2141 int end;
2142 int count = mChildrenCount;
2143 if ((direction & FOCUS_FORWARD) != 0) {
2144 index = 0;
2145 increment = 1;
2146 end = count;
2147 } else {
2148 index = count - 1;
2149 increment = -1;
2150 end = -1;
2151 }
2152 final View[] children = mChildren;
2153 for (int i = index; i != end; i += increment) {
2154 View child = children[i];
2155 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2156 if (child.requestFocus(direction, previouslyFocusedRect)) {
2157 return true;
2158 }
2159 }
2160 }
2161 return false;
2162 }
Chet Haase5c13d892010-10-08 08:37:55 -07002163
Romain Guya440b002010-02-24 15:57:54 -08002164 /**
2165 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002166 *
Romain Guydcc490f2010-02-24 17:59:35 -08002167 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002168 */
2169 @Override
2170 public void dispatchStartTemporaryDetach() {
2171 super.dispatchStartTemporaryDetach();
2172 final int count = mChildrenCount;
2173 final View[] children = mChildren;
2174 for (int i = 0; i < count; i++) {
2175 children[i].dispatchStartTemporaryDetach();
2176 }
2177 }
Chet Haase5c13d892010-10-08 08:37:55 -07002178
Romain Guya440b002010-02-24 15:57:54 -08002179 /**
2180 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002181 *
Romain Guydcc490f2010-02-24 17:59:35 -08002182 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002183 */
2184 @Override
2185 public void dispatchFinishTemporaryDetach() {
2186 super.dispatchFinishTemporaryDetach();
2187 final int count = mChildrenCount;
2188 final View[] children = mChildren;
2189 for (int i = 0; i < count; i++) {
2190 children[i].dispatchFinishTemporaryDetach();
2191 }
2192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193
2194 /**
2195 * {@inheritDoc}
2196 */
2197 @Override
2198 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07002199 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07002201 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 visibility |= mViewFlags & VISIBILITY_MASK;
Adam Powell4b867882011-09-16 12:59:46 -07002204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 final int count = mChildrenCount;
2206 final View[] children = mChildren;
2207 for (int i = 0; i < count; i++) {
2208 children[i].dispatchAttachedToWindow(info, visibility);
2209 }
2210 }
2211
svetoslavganov75986cf2009-05-14 22:28:01 -07002212 @Override
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002213 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002214 boolean handled = super.dispatchPopulateAccessibilityEventInternal(event);
2215 if (handled) {
2216 return handled;
2217 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002218 // Let our children have a shot in populating the event.
svetoslavganov75986cf2009-05-14 22:28:01 -07002219 for (int i = 0, count = getChildCount(); i < count; i++) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002220 View child = getChildAt(i);
2221 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002222 handled = getChildAt(i).dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002223 if (handled) {
2224 return handled;
2225 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002226 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002227 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002228 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07002229 }
2230
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002231 @Override
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002232 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
2233 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08002234 info.setClassName(ViewGroup.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002235 for (int i = 0, count = mChildrenCount; i < count; i++) {
2236 View child = mChildren[i];
Svetoslav Ganov57f3b562011-10-31 17:59:14 -07002237 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2238 && (child.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07002239 info.addChild(child);
2240 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002241 }
2242 }
2243
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08002244 @Override
2245 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
2246 super.onInitializeAccessibilityEventInternal(event);
2247 event.setClassName(ViewGroup.class.getName());
2248 }
2249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 /**
2251 * {@inheritDoc}
2252 */
2253 @Override
2254 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002255 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 // dispatching motion events to a child; we need to get rid of that
2257 // child to avoid dispatching events to it after the window is torn
2258 // down. To make sure we keep the child in a consistent state, we
2259 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07002260 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261
Chet Haase9c087442011-01-12 16:20:16 -08002262 // In case view is detached while transition is running
2263 mLayoutSuppressed = false;
2264
Christopher Tate86cab1b2011-01-13 20:28:55 -08002265 // Tear down our drag tracking
2266 mDragNotifiedChildren = null;
2267 if (mCurrentDrag != null) {
2268 mCurrentDrag.recycle();
2269 mCurrentDrag = null;
2270 }
2271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 final int count = mChildrenCount;
2273 final View[] children = mChildren;
2274 for (int i = 0; i < count; i++) {
2275 children[i].dispatchDetachedFromWindow();
2276 }
2277 super.dispatchDetachedFromWindow();
2278 }
2279
2280 /**
2281 * {@inheritDoc}
2282 */
2283 @Override
2284 public void setPadding(int left, int top, int right, int bottom) {
2285 super.setPadding(left, top, right, bottom);
2286
Romain Guy13f35f32011-03-24 12:03:17 -07002287 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 mGroupFlags |= FLAG_PADDING_NOT_NULL;
2289 } else {
2290 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
2291 }
2292 }
2293
2294 /**
2295 * {@inheritDoc}
2296 */
2297 @Override
2298 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
2299 super.dispatchSaveInstanceState(container);
2300 final int count = mChildrenCount;
2301 final View[] children = mChildren;
2302 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002303 View c = children[i];
2304 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
2305 c.dispatchSaveInstanceState(container);
2306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308 }
2309
2310 /**
Romain Guy9fc27812011-04-27 14:21:41 -07002311 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
2312 * to only this view, not to its children. For use when overriding
2313 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
2314 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 *
2316 * @param container the container
2317 */
2318 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
2319 super.dispatchSaveInstanceState(container);
2320 }
2321
2322 /**
2323 * {@inheritDoc}
2324 */
2325 @Override
2326 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
2327 super.dispatchRestoreInstanceState(container);
2328 final int count = mChildrenCount;
2329 final View[] children = mChildren;
2330 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002331 View c = children[i];
2332 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
2333 c.dispatchRestoreInstanceState(container);
2334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 }
2336 }
2337
2338 /**
Romain Guy02739a82011-05-16 11:43:18 -07002339 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
2340 * to only this view, not to its children. For use when overriding
2341 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
2342 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 *
2344 * @param container the container
2345 */
2346 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
2347 super.dispatchRestoreInstanceState(container);
2348 }
2349
2350 /**
2351 * Enables or disables the drawing cache for each child of this view group.
2352 *
2353 * @param enabled true to enable the cache, false to dispose of it
2354 */
2355 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
2356 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
2357 final View[] children = mChildren;
2358 final int count = mChildrenCount;
2359 for (int i = 0; i < count; i++) {
2360 children[i].setDrawingCacheEnabled(enabled);
2361 }
2362 }
2363 }
2364
2365 @Override
2366 protected void onAnimationStart() {
2367 super.onAnimationStart();
2368
2369 // When this ViewGroup's animation starts, build the cache for the children
2370 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2371 final int count = mChildrenCount;
2372 final View[] children = mChildren;
Romain Guy0d9275e2010-10-26 14:22:30 -07002373 final boolean buildCache = !isHardwareAccelerated();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374
2375 for (int i = 0; i < count; i++) {
2376 final View child = children[i];
2377 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2378 child.setDrawingCacheEnabled(true);
Romain Guy0d9275e2010-10-26 14:22:30 -07002379 if (buildCache) {
2380 child.buildDrawingCache(true);
2381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
2383 }
2384
2385 mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
2386 }
2387 }
2388
2389 @Override
2390 protected void onAnimationEnd() {
2391 super.onAnimationEnd();
2392
2393 // When this ViewGroup's animation ends, destroy the cache of the children
2394 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2395 mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
2396
2397 if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
2398 setChildrenDrawingCacheEnabled(false);
2399 }
2400 }
2401 }
2402
Romain Guy223ff5c2010-03-02 17:07:47 -08002403 @Override
2404 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07002405 int count = mChildrenCount;
2406 int[] visibilities = null;
2407
Romain Guy223ff5c2010-03-02 17:07:47 -08002408 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07002409 visibilities = new int[count];
2410 for (int i = 0; i < count; i++) {
2411 View child = getChildAt(i);
2412 visibilities[i] = child.getVisibility();
2413 if (visibilities[i] == View.VISIBLE) {
2414 child.setVisibility(INVISIBLE);
2415 }
2416 }
Romain Guy223ff5c2010-03-02 17:07:47 -08002417 }
2418
2419 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07002420
2421 if (skipChildren) {
2422 for (int i = 0; i < count; i++) {
2423 getChildAt(i).setVisibility(visibilities[i]);
Chet Haase5c13d892010-10-08 08:37:55 -07002424 }
Romain Guy65554f22010-03-22 18:58:21 -07002425 }
Romain Guy223ff5c2010-03-02 17:07:47 -08002426
2427 return b;
2428 }
2429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 /**
2431 * {@inheritDoc}
2432 */
2433 @Override
2434 protected void dispatchDraw(Canvas canvas) {
2435 final int count = mChildrenCount;
2436 final View[] children = mChildren;
2437 int flags = mGroupFlags;
2438
2439 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
2440 final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
2441
Romain Guy0d9275e2010-10-26 14:22:30 -07002442 final boolean buildCache = !isHardwareAccelerated();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 for (int i = 0; i < count; i++) {
2444 final View child = children[i];
2445 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2446 final LayoutParams params = child.getLayoutParams();
2447 attachLayoutAnimationParameters(child, params, i, count);
2448 bindLayoutAnimation(child);
2449 if (cache) {
2450 child.setDrawingCacheEnabled(true);
Romain Guy0d9275e2010-10-26 14:22:30 -07002451 if (buildCache) {
2452 child.buildDrawingCache(true);
2453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 }
2455 }
2456 }
2457
2458 final LayoutAnimationController controller = mLayoutAnimationController;
2459 if (controller.willOverlap()) {
2460 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
2461 }
2462
2463 controller.start();
2464
2465 mGroupFlags &= ~FLAG_RUN_ANIMATION;
2466 mGroupFlags &= ~FLAG_ANIMATION_DONE;
2467
2468 if (cache) {
2469 mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
2470 }
2471
2472 if (mAnimationListener != null) {
2473 mAnimationListener.onAnimationStart(controller.getAnimation());
2474 }
2475 }
2476
2477 int saveCount = 0;
2478 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
2479 if (clipToPadding) {
2480 saveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07002481 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
2482 mScrollX + mRight - mLeft - mPaddingRight,
2483 mScrollY + mBottom - mTop - mPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484
2485 }
2486
2487 // We will draw our child's animation, let's reset the flag
2488 mPrivateFlags &= ~DRAW_ANIMATION;
2489 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
2490
2491 boolean more = false;
2492 final long drawingTime = getDrawingTime();
2493
2494 if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
2495 for (int i = 0; i < count; i++) {
2496 final View child = children[i];
2497 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
2498 more |= drawChild(canvas, child, drawingTime);
2499 }
2500 }
2501 } else {
2502 for (int i = 0; i < count; i++) {
2503 final View child = children[getChildDrawingOrder(count, i)];
2504 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
2505 more |= drawChild(canvas, child, drawingTime);
2506 }
2507 }
2508 }
2509
2510 // Draw any disappearing views that have animations
2511 if (mDisappearingChildren != null) {
2512 final ArrayList<View> disappearingChildren = mDisappearingChildren;
2513 final int disappearingCount = disappearingChildren.size() - 1;
2514 // Go backwards -- we may delete as animations finish
2515 for (int i = disappearingCount; i >= 0; i--) {
2516 final View child = disappearingChildren.get(i);
2517 more |= drawChild(canvas, child, drawingTime);
2518 }
2519 }
2520
2521 if (clipToPadding) {
2522 canvas.restoreToCount(saveCount);
2523 }
2524
2525 // mGroupFlags might have been updated by drawChild()
2526 flags = mGroupFlags;
2527
2528 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08002529 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 }
2531
2532 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
2533 mLayoutAnimationController.isDone() && !more) {
2534 // We want to erase the drawing cache and notify the listener after the
2535 // next frame is drawn because one extra invalidate() is caused by
2536 // drawChild() after the animation is over
2537 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
2538 final Runnable end = new Runnable() {
2539 public void run() {
2540 notifyAnimationListener();
2541 }
2542 };
2543 post(end);
2544 }
2545 }
Romain Guy8506ab42009-06-11 17:35:47 -07002546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 /**
2548 * Returns the index of the child to draw for this iteration. Override this
2549 * if you want to change the drawing order of children. By default, it
2550 * returns i.
2551 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08002552 * NOTE: In order for this method to be called, you must enable child ordering
2553 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07002554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 * @param i The current iteration.
2556 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07002557 *
Romain Guy293451e2009-11-04 13:59:48 -08002558 * @see #setChildrenDrawingOrderEnabled(boolean)
2559 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 */
2561 protected int getChildDrawingOrder(int childCount, int i) {
2562 return i;
2563 }
Romain Guy8506ab42009-06-11 17:35:47 -07002564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 private void notifyAnimationListener() {
2566 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
2567 mGroupFlags |= FLAG_ANIMATION_DONE;
2568
2569 if (mAnimationListener != null) {
2570 final Runnable end = new Runnable() {
2571 public void run() {
2572 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
2573 }
2574 };
2575 post(end);
2576 }
2577
2578 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2579 mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
2580 if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
2581 setChildrenDrawingCacheEnabled(false);
2582 }
2583 }
2584
Romain Guy849d0a32011-02-01 17:20:48 -08002585 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587
2588 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08002589 * This method is used to cause children of this ViewGroup to restore or recreate their
2590 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
2591 * to recreate its own display list, which would happen if it went through the normal
2592 * draw/dispatchDraw mechanisms.
2593 *
2594 * @hide
2595 */
2596 @Override
2597 protected void dispatchGetDisplayList() {
2598 final int count = mChildrenCount;
2599 final View[] children = mChildren;
2600 for (int i = 0; i < count; i++) {
2601 final View child = children[i];
Romain Guy59c7f802011-09-29 17:21:45 -07002602 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) &&
2603 child.hasStaticLayer()) {
Romain Guy2f57ba52011-02-03 18:03:29 -08002604 child.mRecreateDisplayList = (child.mPrivateFlags & INVALIDATED) == INVALIDATED;
2605 child.mPrivateFlags &= ~INVALIDATED;
2606 child.getDisplayList();
2607 child.mRecreateDisplayList = false;
2608 }
Chet Haasedaf98e92011-01-10 14:10:36 -08002609 }
2610 }
2611
2612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 * Draw one child of this View Group. This method is responsible for getting
2614 * the canvas in the right state. This includes clipping, translating so
2615 * that the child's scrolled origin is at 0, 0, and applying any animation
2616 * transformations.
2617 *
2618 * @param canvas The canvas on which to draw the child
2619 * @param child Who to draw
2620 * @param drawingTime The time at which draw is occuring
2621 * @return True if an invalidate() was issued
2622 */
2623 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
2624 boolean more = false;
2625
2626 final int cl = child.mLeft;
2627 final int ct = child.mTop;
2628 final int cr = child.mRight;
2629 final int cb = child.mBottom;
2630
Chet Haase70d4ba12010-10-06 09:46:45 -07002631 final boolean childHasIdentityMatrix = child.hasIdentityMatrix();
2632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 final int flags = mGroupFlags;
2634
2635 if ((flags & FLAG_CLEAR_TRANSFORMATION) == FLAG_CLEAR_TRANSFORMATION) {
Chet Haase48460322010-06-11 14:22:25 -07002636 mChildTransformation.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 mGroupFlags &= ~FLAG_CLEAR_TRANSFORMATION;
2638 }
2639
2640 Transformation transformToApply = null;
Chet Haase48460322010-06-11 14:22:25 -07002641 Transformation invalidationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 final Animation a = child.getAnimation();
2643 boolean concatMatrix = false;
2644
Chet Haase48460322010-06-11 14:22:25 -07002645 boolean scalingRequired = false;
Romain Guy171c5922011-01-06 10:04:23 -08002646 boolean caching;
Romain Guy849d0a32011-02-01 17:20:48 -08002647 int layerType = mDrawLayers ? child.getLayerType() : LAYER_TYPE_NONE;
Chet Haasee38ba4a2011-01-27 01:10:35 -08002648
2649 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Romain Guyb051e892010-09-28 19:09:36 -07002650 if ((flags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE ||
Chet Haase48460322010-06-11 14:22:25 -07002651 (flags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE) {
2652 caching = true;
2653 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
Romain Guy171c5922011-01-06 10:04:23 -08002654 } else {
Chet Haasee38ba4a2011-01-27 01:10:35 -08002655 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
Chet Haase48460322010-06-11 14:22:25 -07002656 }
2657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 if (a != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 final boolean initialized = a.isInitialized();
2660 if (!initialized) {
Romain Guy8f2d94f2009-03-25 18:04:42 -07002661 a.initialize(cr - cl, cb - ct, getWidth(), getHeight());
2662 a.initializeInvalidateRegion(0, 0, cr - cl, cb - ct);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 child.onAnimationStart();
2664 }
2665
Chet Haase53f2d552011-12-19 14:01:01 -08002666 more = a.getTransformation(drawingTime, mChildTransformation, 1f);
Chet Haase48460322010-06-11 14:22:25 -07002667 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
2668 if (mInvalidationTransformation == null) {
2669 mInvalidationTransformation = new Transformation();
2670 }
2671 invalidationTransform = mInvalidationTransformation;
2672 a.getTransformation(drawingTime, invalidationTransform, 1f);
2673 } else {
2674 invalidationTransform = mChildTransformation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 transformToApply = mChildTransformation;
2677
2678 concatMatrix = a.willChangeTransformationMatrix();
2679
2680 if (more) {
2681 if (!a.willChangeBounds()) {
2682 if ((flags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) ==
2683 FLAG_OPTIMIZE_INVALIDATE) {
2684 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
2685 } else if ((flags & FLAG_INVALIDATE_REQUIRED) == 0) {
2686 // The child need to draw an animation, potentially offscreen, so
2687 // make sure we do not cancel invalidate requests
2688 mPrivateFlags |= DRAW_ANIMATION;
2689 invalidate(cl, ct, cr, cb);
2690 }
2691 } else {
Chet Haase48460322010-06-11 14:22:25 -07002692 if (mInvalidateRegion == null) {
2693 mInvalidateRegion = new RectF();
2694 }
2695 final RectF region = mInvalidateRegion;
2696 a.getInvalidateRegion(0, 0, cr - cl, cb - ct, region, invalidationTransform);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697
2698 // The child need to draw an animation, potentially offscreen, so
2699 // make sure we do not cancel invalidate requests
2700 mPrivateFlags |= DRAW_ANIMATION;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002701
2702 final int left = cl + (int) region.left;
2703 final int top = ct + (int) region.top;
Chet Haase530b22a2011-08-23 14:36:55 -07002704 invalidate(left, top, left + (int) (region.width() + .5f),
2705 top + (int) (region.height() + .5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707 }
2708 } else if ((flags & FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
2709 FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 final boolean hasTransform = getChildStaticTransformation(child, mChildTransformation);
2711 if (hasTransform) {
2712 final int transformType = mChildTransformation.getTransformationType();
2713 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
2714 mChildTransformation : null;
2715 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
2716 }
2717 }
2718
Chet Haase70d4ba12010-10-06 09:46:45 -07002719 concatMatrix |= !childHasIdentityMatrix;
Chet Haasedf030d22010-07-30 17:22:38 -07002720
Romain Guy5bcdff42009-05-14 21:27:18 -07002721 // Sets the flag as early as possible to allow draw() implementations
Romain Guy986003d2009-03-25 17:42:35 -07002722 // to call invalidate() successfully when doing animations
Romain Guy5bcdff42009-05-14 21:27:18 -07002723 child.mPrivateFlags |= DRAWN;
Romain Guy986003d2009-03-25 17:42:35 -07002724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 if (!concatMatrix && canvas.quickReject(cl, ct, cr, cb, Canvas.EdgeType.BW) &&
2726 (child.mPrivateFlags & DRAW_ANIMATION) == 0) {
2727 return more;
2728 }
Chet Haase5c13d892010-10-08 08:37:55 -07002729
Chet Haasee38ba4a2011-01-27 01:10:35 -08002730 if (hardwareAccelerated) {
Chet Haasedaf98e92011-01-10 14:10:36 -08002731 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
2732 // retain the flag's value temporarily in the mRecreateDisplayList flag
2733 child.mRecreateDisplayList = (child.mPrivateFlags & INVALIDATED) == INVALIDATED;
2734 child.mPrivateFlags &= ~INVALIDATED;
2735 }
2736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 child.computeScroll();
2738
2739 final int sx = child.mScrollX;
2740 final int sy = child.mScrollY;
2741
Romain Guyb051e892010-09-28 19:09:36 -07002742 DisplayList displayList = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 Bitmap cache = null;
Chet Haase678e0ad2011-01-25 09:37:18 -08002744 boolean hasDisplayList = false;
Chet Haase48460322010-06-11 14:22:25 -07002745 if (caching) {
Chet Haasee38ba4a2011-01-27 01:10:35 -08002746 if (!hardwareAccelerated) {
Romain Guy171c5922011-01-06 10:04:23 -08002747 if (layerType != LAYER_TYPE_NONE) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002748 layerType = LAYER_TYPE_SOFTWARE;
Romain Guy171c5922011-01-06 10:04:23 -08002749 child.buildDrawingCache(true);
2750 }
Romain Guyb051e892010-09-28 19:09:36 -07002751 cache = child.getDrawingCache(true);
2752 } else {
Romain Guyd643bb52011-03-01 14:55:21 -08002753 switch (layerType) {
2754 case LAYER_TYPE_SOFTWARE:
2755 child.buildDrawingCache(true);
2756 cache = child.getDrawingCache(true);
2757 break;
2758 case LAYER_TYPE_NONE:
2759 // Delay getting the display list until animation-driven alpha values are
2760 // set up and possibly passed on to the view
2761 hasDisplayList = child.canHaveDisplayList();
2762 break;
Romain Guy171c5922011-01-06 10:04:23 -08002763 }
Romain Guyb051e892010-09-28 19:09:36 -07002764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
2766
Romain Guyb051e892010-09-28 19:09:36 -07002767 final boolean hasNoCache = cache == null || hasDisplayList;
Romain Guyd643bb52011-03-01 14:55:21 -08002768 final boolean offsetForScroll = cache == null && !hasDisplayList &&
2769 layerType != LAYER_TYPE_HARDWARE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770
2771 final int restoreTo = canvas.save();
Romain Guyd643bb52011-03-01 14:55:21 -08002772 if (offsetForScroll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 canvas.translate(cl - sx, ct - sy);
2774 } else {
2775 canvas.translate(cl, ct);
Romain Guy8506ab42009-06-11 17:35:47 -07002776 if (scalingRequired) {
Romain Guycafdea62009-06-12 10:51:36 -07002777 // mAttachInfo cannot be null, otherwise scalingRequired == false
Romain Guy8506ab42009-06-11 17:35:47 -07002778 final float scale = 1.0f / mAttachInfo.mApplicationScale;
2779 canvas.scale(scale, scale);
2780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 }
2782
Romain Guyc7ee3ca2011-12-07 19:06:28 -08002783 float alpha = child.getAlpha();
Romain Guy33e72ae2010-07-17 12:40:29 -07002784 if (transformToApply != null || alpha < 1.0f || !child.hasIdentityMatrix()) {
Chet Haase70d4ba12010-10-06 09:46:45 -07002785 if (transformToApply != null || !childHasIdentityMatrix) {
2786 int transX = 0;
2787 int transY = 0;
Romain Guy33e72ae2010-07-17 12:40:29 -07002788
Romain Guyd643bb52011-03-01 14:55:21 -08002789 if (offsetForScroll) {
Chet Haase70d4ba12010-10-06 09:46:45 -07002790 transX = -sx;
2791 transY = -sy;
2792 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002793
Chet Haase70d4ba12010-10-06 09:46:45 -07002794 if (transformToApply != null) {
2795 if (concatMatrix) {
2796 // Undo the scroll translation, apply the transformation matrix,
2797 // then redo the scroll translate to get the correct result.
2798 canvas.translate(-transX, -transY);
2799 canvas.concat(transformToApply.getMatrix());
2800 canvas.translate(transX, transY);
2801 mGroupFlags |= FLAG_CLEAR_TRANSFORMATION;
2802 }
2803
2804 float transformAlpha = transformToApply.getAlpha();
2805 if (transformAlpha < 1.0f) {
2806 alpha *= transformToApply.getAlpha();
2807 mGroupFlags |= FLAG_CLEAR_TRANSFORMATION;
2808 }
2809 }
2810
2811 if (!childHasIdentityMatrix) {
Chet Haasec3aa3612010-06-17 08:50:37 -07002812 canvas.translate(-transX, -transY);
Chet Haase70d4ba12010-10-06 09:46:45 -07002813 canvas.concat(child.getMatrix());
Chet Haasec3aa3612010-06-17 08:50:37 -07002814 canvas.translate(transX, transY);
Chet Haasec3aa3612010-06-17 08:50:37 -07002815 }
Chet Haasec3aa3612010-06-17 08:50:37 -07002816 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 if (alpha < 1.0f) {
2819 mGroupFlags |= FLAG_CLEAR_TRANSFORMATION;
Romain Guy33e72ae2010-07-17 12:40:29 -07002820 if (hasNoCache) {
2821 final int multipliedAlpha = (int) (255 * alpha);
2822 if (!child.onSetAlpha(multipliedAlpha)) {
Romain Guyfd880422010-09-23 16:16:04 -07002823 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Romain Guy171c5922011-01-06 10:04:23 -08002824 if ((flags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN ||
2825 layerType != LAYER_TYPE_NONE) {
Romain Guyfd880422010-09-23 16:16:04 -07002826 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
2827 }
Romain Guy54229ee2011-02-01 13:05:16 -08002828 if (layerType == LAYER_TYPE_NONE) {
Romain Guya7dabcd2011-03-01 15:44:21 -08002829 final int scrollX = hasDisplayList ? 0 : sx;
2830 final int scrollY = hasDisplayList ? 0 : sy;
2831 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + cr - cl,
2832 scrollY + cb - ct, multipliedAlpha, layerFlags);
Romain Guy171c5922011-01-06 10:04:23 -08002833 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002834 } else {
Romain Guy171c5922011-01-06 10:04:23 -08002835 // Alpha is handled by the child directly, clobber the layer's alpha
Romain Guy33e72ae2010-07-17 12:40:29 -07002836 child.mPrivateFlags |= ALPHA_SET;
2837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 }
2839 }
2840 } else if ((child.mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
2841 child.onSetAlpha(255);
Romain Guy9b34d452010-09-02 11:45:04 -07002842 child.mPrivateFlags &= ~ALPHA_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 }
2844
2845 if ((flags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
Romain Guyd643bb52011-03-01 14:55:21 -08002846 if (offsetForScroll) {
Romain Guy8f2d94f2009-03-25 18:04:42 -07002847 canvas.clipRect(sx, sy, sx + (cr - cl), sy + (cb - ct));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 } else {
Chet Haasedaf98e92011-01-10 14:10:36 -08002849 if (!scalingRequired || cache == null) {
Romain Guy8506ab42009-06-11 17:35:47 -07002850 canvas.clipRect(0, 0, cr - cl, cb - ct);
2851 } else {
2852 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
2853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 }
2855 }
2856
Chet Haase678e0ad2011-01-25 09:37:18 -08002857 if (hasDisplayList) {
2858 displayList = child.getDisplayList();
Chet Haase6e6db612011-09-26 13:49:33 -07002859 if (!displayList.isValid()) {
2860 // Uncommon, but possible. If a view is removed from the hierarchy during the call
2861 // to getDisplayList(), the display list will be marked invalid and we should not
2862 // try to use it again.
2863 displayList = null;
2864 hasDisplayList = false;
2865 }
Chet Haase678e0ad2011-01-25 09:37:18 -08002866 }
2867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 if (hasNoCache) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002869 boolean layerRendered = false;
Romain Guyd6cd5722011-01-17 14:42:41 -08002870 if (layerType == LAYER_TYPE_HARDWARE) {
Romain Guy5e7f7662011-01-24 22:35:56 -08002871 final HardwareLayer layer = child.getHardwareLayer();
Romain Guy6c319ca2011-01-11 14:29:25 -08002872 if (layer != null && layer.isValid()) {
Romain Guy54229ee2011-02-01 13:05:16 -08002873 child.mLayerPaint.setAlpha((int) (alpha * 255));
Romain Guyada830f2011-01-13 12:13:20 -08002874 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, child.mLayerPaint);
Romain Guy6c319ca2011-01-11 14:29:25 -08002875 layerRendered = true;
Romain Guyb051e892010-09-28 19:09:36 -07002876 } else {
Romain Guya7dabcd2011-03-01 15:44:21 -08002877 final int scrollX = hasDisplayList ? 0 : sx;
2878 final int scrollY = hasDisplayList ? 0 : sy;
2879 canvas.saveLayer(scrollX, scrollY,
2880 scrollX + cr - cl, scrollY + cb - ct, child.mLayerPaint,
Romain Guy6c319ca2011-01-11 14:29:25 -08002881 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
Romain Guy6c319ca2011-01-11 14:29:25 -08002883 }
2884
2885 if (!layerRendered) {
2886 if (!hasDisplayList) {
2887 // Fast path for layouts with no backgrounds
2888 if ((child.mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
2889 if (ViewDebug.TRACE_HIERARCHY) {
2890 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
2891 }
2892 child.mPrivateFlags &= ~DIRTY_MASK;
2893 child.dispatchDraw(canvas);
2894 } else {
2895 child.draw(canvas);
2896 }
2897 } else {
2898 child.mPrivateFlags &= ~DIRTY_MASK;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002899 ((HardwareCanvas) canvas).drawDisplayList(displayList, cr - cl, cb - ct, null);
Romain Guy6c319ca2011-01-11 14:29:25 -08002900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Romain Guyb051e892010-09-28 19:09:36 -07002902 } else if (cache != null) {
Chet Haasef2f7d8f2010-12-03 14:08:14 -08002903 child.mPrivateFlags &= ~DIRTY_MASK;
Romain Guy171c5922011-01-06 10:04:23 -08002904 Paint cachePaint;
2905
Romain Guyd6cd5722011-01-17 14:42:41 -08002906 if (layerType == LAYER_TYPE_NONE) {
Romain Guy171c5922011-01-06 10:04:23 -08002907 cachePaint = mCachePaint;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002908 if (cachePaint == null) {
2909 cachePaint = new Paint();
2910 cachePaint.setDither(false);
2911 mCachePaint = cachePaint;
2912 }
Romain Guy171c5922011-01-06 10:04:23 -08002913 if (alpha < 1.0f) {
2914 cachePaint.setAlpha((int) (alpha * 255));
2915 mGroupFlags |= FLAG_ALPHA_LOWER_THAN_ONE;
2916 } else if ((flags & FLAG_ALPHA_LOWER_THAN_ONE) == FLAG_ALPHA_LOWER_THAN_ONE) {
2917 cachePaint.setAlpha(255);
2918 mGroupFlags &= ~FLAG_ALPHA_LOWER_THAN_ONE;
2919 }
2920 } else {
2921 cachePaint = child.mLayerPaint;
Romain Guyd6cd5722011-01-17 14:42:41 -08002922 cachePaint.setAlpha((int) (alpha * 255));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
2925 }
2926
2927 canvas.restoreToCount(restoreTo);
2928
2929 if (a != null && !more) {
Chet Haasee38ba4a2011-01-27 01:10:35 -08002930 if (!hardwareAccelerated && !a.getFillAfter()) {
Chet Haase678e0ad2011-01-25 09:37:18 -08002931 child.onSetAlpha(255);
2932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 finishAnimatingView(child, a);
2934 }
2935
Chet Haasee38ba4a2011-01-27 01:10:35 -08002936 if (more && hardwareAccelerated) {
Chet Haasedaf98e92011-01-10 14:10:36 -08002937 // invalidation is the trigger to recreate display lists, so if we're using
2938 // display lists to render, force an invalidate to allow the animation to
2939 // continue drawing another frame
Romain Guy849d0a32011-02-01 17:20:48 -08002940 invalidate(true);
Romain Guy29d23ec2011-07-25 14:42:24 -07002941 if (a.hasAlpha() && (child.mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
Chet Haase678e0ad2011-01-25 09:37:18 -08002942 // alpha animations should cause the child to recreate its display list
Romain Guy849d0a32011-02-01 17:20:48 -08002943 child.invalidate(true);
Chet Haase678e0ad2011-01-25 09:37:18 -08002944 }
Chet Haasedaf98e92011-01-10 14:10:36 -08002945 }
2946
2947 child.mRecreateDisplayList = false;
2948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 return more;
2950 }
2951
2952 /**
Romain Guy849d0a32011-02-01 17:20:48 -08002953 *
2954 * @param enabled True if children should be drawn with layers, false otherwise.
2955 *
2956 * @hide
2957 */
2958 public void setChildrenLayersEnabled(boolean enabled) {
Romain Guy9d18f2d2011-02-03 11:25:51 -08002959 if (enabled != mDrawLayers) {
2960 mDrawLayers = enabled;
2961 invalidate(true);
2962
Romain Guy9c4b79a2011-11-10 19:23:58 -08002963 boolean flushLayers = !enabled;
2964 AttachInfo info = mAttachInfo;
2965 if (info != null && info.mHardwareRenderer != null &&
2966 info.mHardwareRenderer.isEnabled()) {
2967 if (!info.mHardwareRenderer.validate()) {
2968 flushLayers = false;
2969 }
2970 } else {
2971 flushLayers = false;
2972 }
2973
Romain Guy9d18f2d2011-02-03 11:25:51 -08002974 // We need to invalidate any child with a layer. For instance,
2975 // if a child is backed by a hardware layer and we disable layers
2976 // the child is marked as not dirty (flags cleared the last time
2977 // the child was drawn inside its layer.) However, that child might
2978 // never have created its own display list or have an obsolete
2979 // display list. By invalidating the child we ensure the display
2980 // list is in sync with the content of the hardware layer.
2981 for (int i = 0; i < mChildrenCount; i++) {
2982 View child = mChildren[i];
2983 if (child.mLayerType != LAYER_TYPE_NONE) {
Romain Guy9c4b79a2011-11-10 19:23:58 -08002984 if (flushLayers) child.flushLayer();
Romain Guy9d18f2d2011-02-03 11:25:51 -08002985 child.invalidate(true);
2986 }
2987 }
2988 }
Romain Guy849d0a32011-02-01 17:20:48 -08002989 }
2990
2991 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 * By default, children are clipped to their bounds before drawing. This
2993 * allows view groups to override this behavior for animations, etc.
2994 *
2995 * @param clipChildren true to clip children to their bounds,
2996 * false otherwise
2997 * @attr ref android.R.styleable#ViewGroup_clipChildren
2998 */
2999 public void setClipChildren(boolean clipChildren) {
3000 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
3001 }
3002
3003 /**
3004 * By default, children are clipped to the padding of the ViewGroup. This
3005 * allows view groups to override this behavior
3006 *
3007 * @param clipToPadding true to clip children to the padding of the
3008 * group, false otherwise
3009 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3010 */
3011 public void setClipToPadding(boolean clipToPadding) {
3012 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
3013 }
3014
3015 /**
3016 * {@inheritDoc}
3017 */
3018 @Override
3019 public void dispatchSetSelected(boolean selected) {
3020 final View[] children = mChildren;
3021 final int count = mChildrenCount;
3022 for (int i = 0; i < count; i++) {
3023 children[i].setSelected(selected);
3024 }
3025 }
Romain Guy8506ab42009-06-11 17:35:47 -07003026
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003027 /**
3028 * {@inheritDoc}
3029 */
3030 @Override
3031 public void dispatchSetActivated(boolean activated) {
3032 final View[] children = mChildren;
3033 final int count = mChildrenCount;
3034 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003035 children[i].setActivated(activated);
3036 }
3037 }
3038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 @Override
3040 protected void dispatchSetPressed(boolean pressed) {
3041 final View[] children = mChildren;
3042 final int count = mChildrenCount;
3043 for (int i = 0; i < count; i++) {
3044 children[i].setPressed(pressed);
3045 }
3046 }
3047
3048 /**
3049 * When this property is set to true, this ViewGroup supports static transformations on
3050 * children; this causes
3051 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3052 * invoked when a child is drawn.
3053 *
3054 * Any subclass overriding
3055 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3056 * set this property to true.
3057 *
3058 * @param enabled True to enable static transformations on children, false otherwise.
3059 *
3060 * @see #FLAG_SUPPORT_STATIC_TRANSFORMATIONS
3061 */
3062 protected void setStaticTransformationsEnabled(boolean enabled) {
3063 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3064 }
3065
3066 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08003067 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
3068 * boolean to indicate whether a static transform was set. The default implementation
3069 * simply returns <code>false</code>; subclasses may override this method for different
3070 * behavior.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08003072 * @param child The child view whose static transform is being requested
3073 * @param t The Transformation which will hold the result
3074 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07003075 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 */
3077 protected boolean getChildStaticTransformation(View child, Transformation t) {
3078 return false;
3079 }
3080
3081 /**
3082 * {@hide}
3083 */
3084 @Override
3085 protected View findViewTraversal(int id) {
3086 if (id == mID) {
3087 return this;
3088 }
3089
3090 final View[] where = mChildren;
3091 final int len = mChildrenCount;
3092
3093 for (int i = 0; i < len; i++) {
3094 View v = where[i];
3095
3096 if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
3097 v = v.findViewById(id);
3098
3099 if (v != null) {
3100 return v;
3101 }
3102 }
3103 }
3104
3105 return null;
3106 }
3107
3108 /**
3109 * {@hide}
3110 */
3111 @Override
3112 protected View findViewWithTagTraversal(Object tag) {
3113 if (tag != null && tag.equals(mTag)) {
3114 return this;
3115 }
3116
3117 final View[] where = mChildren;
3118 final int len = mChildrenCount;
3119
3120 for (int i = 0; i < len; i++) {
3121 View v = where[i];
3122
3123 if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
3124 v = v.findViewWithTag(tag);
3125
3126 if (v != null) {
3127 return v;
3128 }
3129 }
3130 }
3131
3132 return null;
3133 }
3134
3135 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003136 * {@hide}
3137 */
3138 @Override
Jeff Brown4dfbec22011-08-15 14:55:37 -07003139 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003140 if (predicate.apply(this)) {
3141 return this;
3142 }
3143
3144 final View[] where = mChildren;
3145 final int len = mChildrenCount;
3146
3147 for (int i = 0; i < len; i++) {
3148 View v = where[i];
3149
Jeff Brown4dfbec22011-08-15 14:55:37 -07003150 if (v != childToSkip && (v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003151 v = v.findViewByPredicate(predicate);
3152
3153 if (v != null) {
3154 return v;
3155 }
3156 }
3157 }
3158
3159 return null;
3160 }
3161
3162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 * Adds a child view. If no layout parameters are already set on the child, the
3164 * default parameters for this ViewGroup are set on the child.
3165 *
3166 * @param child the child view to add
3167 *
3168 * @see #generateDefaultLayoutParams()
3169 */
3170 public void addView(View child) {
3171 addView(child, -1);
3172 }
3173
3174 /**
3175 * Adds a child view. If no layout parameters are already set on the child, the
3176 * default parameters for this ViewGroup are set on the child.
3177 *
3178 * @param child the child view to add
3179 * @param index the position at which to add the child
3180 *
3181 * @see #generateDefaultLayoutParams()
3182 */
3183 public void addView(View child, int index) {
3184 LayoutParams params = child.getLayoutParams();
3185 if (params == null) {
3186 params = generateDefaultLayoutParams();
3187 if (params == null) {
3188 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
3189 }
3190 }
3191 addView(child, index, params);
3192 }
3193
3194 /**
3195 * Adds a child view with this ViewGroup's default layout parameters and the
3196 * specified width and height.
3197 *
3198 * @param child the child view to add
3199 */
3200 public void addView(View child, int width, int height) {
3201 final LayoutParams params = generateDefaultLayoutParams();
3202 params.width = width;
3203 params.height = height;
3204 addView(child, -1, params);
3205 }
3206
3207 /**
3208 * Adds a child view with the specified layout parameters.
3209 *
3210 * @param child the child view to add
3211 * @param params the layout parameters to set on the child
3212 */
3213 public void addView(View child, LayoutParams params) {
3214 addView(child, -1, params);
3215 }
3216
3217 /**
3218 * Adds a child view with the specified layout parameters.
3219 *
3220 * @param child the child view to add
3221 * @param index the position at which to add the child
3222 * @param params the layout parameters to set on the child
3223 */
3224 public void addView(View child, int index, LayoutParams params) {
3225 if (DBG) {
3226 System.out.println(this + " addView");
3227 }
3228
3229 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
3230 // therefore, we call requestLayout() on ourselves before, so that the child's request
3231 // will be blocked at our level
3232 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003233 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 addViewInner(child, index, params, false);
3235 }
3236
3237 /**
3238 * {@inheritDoc}
3239 */
3240 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
3241 if (!checkLayoutParams(params)) {
3242 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
3243 }
3244 if (view.mParent != this) {
3245 throw new IllegalArgumentException("Given view not a child of " + this);
3246 }
3247 view.setLayoutParams(params);
3248 }
3249
3250 /**
3251 * {@inheritDoc}
3252 */
3253 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3254 return p != null;
3255 }
3256
3257 /**
3258 * Interface definition for a callback to be invoked when the hierarchy
3259 * within this view changed. The hierarchy changes whenever a child is added
3260 * to or removed from this view.
3261 */
3262 public interface OnHierarchyChangeListener {
3263 /**
3264 * Called when a new child is added to a parent view.
3265 *
3266 * @param parent the view in which a child was added
3267 * @param child the new child view added in the hierarchy
3268 */
3269 void onChildViewAdded(View parent, View child);
3270
3271 /**
3272 * Called when a child is removed from a parent view.
3273 *
3274 * @param parent the view from which the child was removed
3275 * @param child the child removed from the hierarchy
3276 */
3277 void onChildViewRemoved(View parent, View child);
3278 }
3279
3280 /**
3281 * Register a callback to be invoked when a child is added to or removed
3282 * from this view.
3283 *
3284 * @param listener the callback to invoke on hierarchy change
3285 */
3286 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
3287 mOnHierarchyChangeListener = listener;
3288 }
3289
3290 /**
Philip Milnef51d91c2011-07-18 16:12:19 -07003291 * @hide
3292 */
3293 protected void onViewAdded(View child) {
3294 if (mOnHierarchyChangeListener != null) {
3295 mOnHierarchyChangeListener.onChildViewAdded(this, child);
3296 }
3297 }
3298
3299 /**
3300 * @hide
3301 */
3302 protected void onViewRemoved(View child) {
3303 if (mOnHierarchyChangeListener != null) {
3304 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
3305 }
3306 }
3307
3308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 * Adds a view during layout. This is useful if in your onLayout() method,
3310 * you need to add more views (as does the list view for example).
3311 *
3312 * If index is negative, it means put it at the end of the list.
3313 *
3314 * @param child the view to add to the group
3315 * @param index the index at which the child must be added
3316 * @param params the layout parameters to associate with the child
3317 * @return true if the child was added, false otherwise
3318 */
3319 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
3320 return addViewInLayout(child, index, params, false);
3321 }
3322
3323 /**
3324 * Adds a view during layout. This is useful if in your onLayout() method,
3325 * you need to add more views (as does the list view for example).
3326 *
3327 * If index is negative, it means put it at the end of the list.
3328 *
3329 * @param child the view to add to the group
3330 * @param index the index at which the child must be added
3331 * @param params the layout parameters to associate with the child
3332 * @param preventRequestLayout if true, calling this method will not trigger a
3333 * layout request on child
3334 * @return true if the child was added, false otherwise
3335 */
3336 protected boolean addViewInLayout(View child, int index, LayoutParams params,
3337 boolean preventRequestLayout) {
3338 child.mParent = null;
3339 addViewInner(child, index, params, preventRequestLayout);
Romain Guy24443ea2009-05-11 11:56:30 -07003340 child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK) | DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 return true;
3342 }
3343
3344 /**
3345 * Prevents the specified child to be laid out during the next layout pass.
3346 *
3347 * @param child the child on which to perform the cleanup
3348 */
3349 protected void cleanupLayoutState(View child) {
3350 child.mPrivateFlags &= ~View.FORCE_LAYOUT;
3351 }
3352
3353 private void addViewInner(View child, int index, LayoutParams params,
3354 boolean preventRequestLayout) {
3355
Chet Haasee8e45d32011-03-02 17:07:35 -08003356 if (mTransition != null) {
3357 // Don't prevent other add transitions from completing, but cancel remove
3358 // transitions to let them complete the process before we add to the container
3359 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08003360 }
3361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 if (child.getParent() != null) {
3363 throw new IllegalStateException("The specified child already has a parent. " +
3364 "You must call removeView() on the child's parent first.");
3365 }
3366
Chet Haase21cd1382010-09-01 17:42:29 -07003367 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003368 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07003369 }
3370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 if (!checkLayoutParams(params)) {
3372 params = generateLayoutParams(params);
3373 }
3374
3375 if (preventRequestLayout) {
3376 child.mLayoutParams = params;
3377 } else {
3378 child.setLayoutParams(params);
3379 }
3380
3381 if (index < 0) {
3382 index = mChildrenCount;
3383 }
3384
3385 addInArray(child, index);
3386
3387 // tell our children
3388 if (preventRequestLayout) {
3389 child.assignParent(this);
3390 } else {
3391 child.mParent = this;
3392 }
3393
3394 if (child.hasFocus()) {
3395 requestChildFocus(child, child.findFocus());
3396 }
Romain Guy8506ab42009-06-11 17:35:47 -07003397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07003399 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07003400 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 ai.mKeepScreenOn = false;
3402 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
3403 if (ai.mKeepScreenOn) {
3404 needGlobalAttributesUpdate(true);
3405 }
3406 ai.mKeepScreenOn = lastKeepOn;
3407 }
3408
Philip Milnef51d91c2011-07-18 16:12:19 -07003409 onViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410
3411 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
3412 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
3413 }
3414 }
3415
3416 private void addInArray(View child, int index) {
3417 View[] children = mChildren;
3418 final int count = mChildrenCount;
3419 final int size = children.length;
3420 if (index == count) {
3421 if (size == count) {
3422 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
3423 System.arraycopy(children, 0, mChildren, 0, size);
3424 children = mChildren;
3425 }
3426 children[mChildrenCount++] = child;
3427 } else if (index < count) {
3428 if (size == count) {
3429 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
3430 System.arraycopy(children, 0, mChildren, 0, index);
3431 System.arraycopy(children, index, mChildren, index + 1, count - index);
3432 children = mChildren;
3433 } else {
3434 System.arraycopy(children, index, children, index + 1, count - index);
3435 }
3436 children[index] = child;
3437 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08003438 if (mLastTouchDownIndex >= index) {
3439 mLastTouchDownIndex++;
3440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 } else {
3442 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
3443 }
3444 }
3445
3446 // This method also sets the child's mParent to null
3447 private void removeFromArray(int index) {
3448 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07003449 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
3450 children[index].mParent = null;
3451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 final int count = mChildrenCount;
3453 if (index == count - 1) {
3454 children[--mChildrenCount] = null;
3455 } else if (index >= 0 && index < count) {
3456 System.arraycopy(children, index + 1, children, index, count - index - 1);
3457 children[--mChildrenCount] = null;
3458 } else {
3459 throw new IndexOutOfBoundsException();
3460 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08003461 if (mLastTouchDownIndex == index) {
3462 mLastTouchDownTime = 0;
3463 mLastTouchDownIndex = -1;
3464 } else if (mLastTouchDownIndex > index) {
3465 mLastTouchDownIndex--;
3466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 }
3468
3469 // This method also sets the children's mParent to null
3470 private void removeFromArray(int start, int count) {
3471 final View[] children = mChildren;
3472 final int childrenCount = mChildrenCount;
3473
3474 start = Math.max(0, start);
3475 final int end = Math.min(childrenCount, start + count);
3476
3477 if (start == end) {
3478 return;
3479 }
3480
3481 if (end == childrenCount) {
3482 for (int i = start; i < end; i++) {
3483 children[i].mParent = null;
3484 children[i] = null;
3485 }
3486 } else {
3487 for (int i = start; i < end; i++) {
3488 children[i].mParent = null;
3489 }
3490
3491 // Since we're looping above, we might as well do the copy, but is arraycopy()
3492 // faster than the extra 2 bounds checks we would do in the loop?
3493 System.arraycopy(children, end, children, start, childrenCount - end);
3494
3495 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
3496 children[i] = null;
3497 }
3498 }
3499
3500 mChildrenCount -= (end - start);
3501 }
3502
3503 private void bindLayoutAnimation(View child) {
3504 Animation a = mLayoutAnimationController.getAnimationForView(child);
3505 child.setAnimation(a);
3506 }
3507
3508 /**
3509 * Subclasses should override this method to set layout animation
3510 * parameters on the supplied child.
3511 *
3512 * @param child the child to associate with animation parameters
3513 * @param params the child's layout parameters which hold the animation
3514 * parameters
3515 * @param index the index of the child in the view group
3516 * @param count the number of children in the view group
3517 */
3518 protected void attachLayoutAnimationParameters(View child,
3519 LayoutParams params, int index, int count) {
3520 LayoutAnimationController.AnimationParameters animationParams =
3521 params.layoutAnimationParameters;
3522 if (animationParams == null) {
3523 animationParams = new LayoutAnimationController.AnimationParameters();
3524 params.layoutAnimationParameters = animationParams;
3525 }
3526
3527 animationParams.count = count;
3528 animationParams.index = index;
3529 }
3530
3531 /**
3532 * {@inheritDoc}
3533 */
3534 public void removeView(View view) {
3535 removeViewInternal(view);
3536 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003537 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 }
3539
3540 /**
3541 * Removes a view during layout. This is useful if in your onLayout() method,
3542 * you need to remove more views.
3543 *
3544 * @param view the view to remove from the group
3545 */
3546 public void removeViewInLayout(View view) {
3547 removeViewInternal(view);
3548 }
3549
3550 /**
3551 * Removes a range of views during layout. This is useful if in your onLayout() method,
3552 * you need to remove more views.
3553 *
3554 * @param start the index of the first view to remove from the group
3555 * @param count the number of views to remove from the group
3556 */
3557 public void removeViewsInLayout(int start, int count) {
3558 removeViewsInternal(start, count);
3559 }
3560
3561 /**
3562 * Removes the view at the specified position in the group.
3563 *
3564 * @param index the position in the group of the view to remove
3565 */
3566 public void removeViewAt(int index) {
3567 removeViewInternal(index, getChildAt(index));
3568 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003569 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571
3572 /**
3573 * Removes the specified range of views from the group.
3574 *
3575 * @param start the first position in the group of the range of views to remove
3576 * @param count the number of views to remove
3577 */
3578 public void removeViews(int start, int count) {
3579 removeViewsInternal(start, count);
3580 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003581 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 }
3583
3584 private void removeViewInternal(View view) {
3585 final int index = indexOfChild(view);
3586 if (index >= 0) {
3587 removeViewInternal(index, view);
3588 }
3589 }
3590
3591 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07003592
3593 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003594 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003595 }
3596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 boolean clearChildFocus = false;
3598 if (view == mFocused) {
3599 view.clearFocusForRemoval();
3600 clearChildFocus = true;
3601 }
3602
Chet Haase21cd1382010-09-01 17:42:29 -07003603 if (view.getAnimation() != null ||
3604 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 addDisappearingView(view);
3606 } else if (view.mAttachInfo != null) {
3607 view.dispatchDetachedFromWindow();
3608 }
3609
Philip Milnef51d91c2011-07-18 16:12:19 -07003610 onViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611
3612 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 removeFromArray(index);
3615
3616 if (clearChildFocus) {
3617 clearChildFocus(view);
3618 }
3619 }
3620
Chet Haase21cd1382010-09-01 17:42:29 -07003621 /**
3622 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
3623 * not null, changes in layout which occur because of children being added to or removed from
3624 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
3625 * object. By default, the transition object is null (so layout changes are not animated).
3626 *
3627 * @param transition The LayoutTransition object that will animated changes in layout. A value
3628 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07003629 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07003630 */
3631 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07003632 if (mTransition != null) {
3633 mTransition.removeTransitionListener(mLayoutTransitionListener);
3634 }
Chet Haase21cd1382010-09-01 17:42:29 -07003635 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07003636 if (mTransition != null) {
3637 mTransition.addTransitionListener(mLayoutTransitionListener);
3638 }
3639 }
3640
3641 /**
3642 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
3643 * not null, changes in layout which occur because of children being added to or removed from
3644 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
3645 * object. By default, the transition object is null (so layout changes are not animated).
3646 *
3647 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
3648 * A value of <code>null</code> means no transition will run on layout changes.
3649 */
3650 public LayoutTransition getLayoutTransition() {
3651 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07003652 }
3653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 private void removeViewsInternal(int start, int count) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 final View focused = mFocused;
3656 final boolean detach = mAttachInfo != null;
3657 View clearChildFocus = null;
3658
3659 final View[] children = mChildren;
3660 final int end = start + count;
3661
3662 for (int i = start; i < end; i++) {
3663 final View view = children[i];
3664
Chet Haase21cd1382010-09-01 17:42:29 -07003665 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003666 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003667 }
3668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 if (view == focused) {
3670 view.clearFocusForRemoval();
3671 clearChildFocus = view;
3672 }
3673
Chet Haase21cd1382010-09-01 17:42:29 -07003674 if (view.getAnimation() != null ||
3675 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 addDisappearingView(view);
3677 } else if (detach) {
3678 view.dispatchDetachedFromWindow();
3679 }
3680
3681 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003682
Philip Milnef51d91c2011-07-18 16:12:19 -07003683 onViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 }
3685
3686 removeFromArray(start, count);
3687
3688 if (clearChildFocus != null) {
3689 clearChildFocus(clearChildFocus);
3690 }
3691 }
3692
3693 /**
3694 * Call this method to remove all child views from the
3695 * ViewGroup.
3696 */
3697 public void removeAllViews() {
3698 removeAllViewsInLayout();
3699 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003700 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 }
3702
3703 /**
3704 * Called by a ViewGroup subclass to remove child views from itself,
3705 * when it must first know its size on screen before it can calculate how many
3706 * child views it will render. An example is a Gallery or a ListView, which
3707 * may "have" 50 children, but actually only render the number of children
3708 * that can currently fit inside the object on screen. Do not call
3709 * this method unless you are extending ViewGroup and understand the
3710 * view measuring and layout pipeline.
3711 */
3712 public void removeAllViewsInLayout() {
3713 final int count = mChildrenCount;
3714 if (count <= 0) {
3715 return;
3716 }
3717
3718 final View[] children = mChildren;
3719 mChildrenCount = 0;
3720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 final View focused = mFocused;
3722 final boolean detach = mAttachInfo != null;
3723 View clearChildFocus = null;
3724
3725 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 for (int i = count - 1; i >= 0; i--) {
3728 final View view = children[i];
3729
Chet Haase21cd1382010-09-01 17:42:29 -07003730 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003731 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003732 }
3733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 if (view == focused) {
3735 view.clearFocusForRemoval();
3736 clearChildFocus = view;
3737 }
3738
Chet Haase21cd1382010-09-01 17:42:29 -07003739 if (view.getAnimation() != null ||
3740 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 addDisappearingView(view);
3742 } else if (detach) {
3743 view.dispatchDetachedFromWindow();
3744 }
3745
Philip Milnef51d91c2011-07-18 16:12:19 -07003746 onViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747
3748 view.mParent = null;
3749 children[i] = null;
3750 }
3751
3752 if (clearChildFocus != null) {
3753 clearChildFocus(clearChildFocus);
3754 }
3755 }
3756
3757 /**
3758 * Finishes the removal of a detached view. This method will dispatch the detached from
3759 * window event and notify the hierarchy change listener.
3760 *
3761 * @param child the child to be definitely removed from the view hierarchy
3762 * @param animate if true and the view has an animation, the view is placed in the
3763 * disappearing views list, otherwise, it is detached from the window
3764 *
3765 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3766 * @see #detachAllViewsFromParent()
3767 * @see #detachViewFromParent(View)
3768 * @see #detachViewFromParent(int)
3769 */
3770 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07003771 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003772 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07003773 }
3774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 if (child == mFocused) {
3776 child.clearFocus();
3777 }
Romain Guy8506ab42009-06-11 17:35:47 -07003778
Chet Haase21cd1382010-09-01 17:42:29 -07003779 if ((animate && child.getAnimation() != null) ||
3780 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 addDisappearingView(child);
3782 } else if (child.mAttachInfo != null) {
3783 child.dispatchDetachedFromWindow();
3784 }
3785
Philip Milnef51d91c2011-07-18 16:12:19 -07003786 onViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 }
3788
3789 /**
3790 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
3791 * sets the layout parameters and puts the view in the list of children so it can be retrieved
3792 * by calling {@link #getChildAt(int)}.
3793 *
3794 * This method should be called only for view which were detached from their parent.
3795 *
3796 * @param child the child to attach
3797 * @param index the index at which the child should be attached
3798 * @param params the layout parameters of the child
3799 *
3800 * @see #removeDetachedView(View, boolean)
3801 * @see #detachAllViewsFromParent()
3802 * @see #detachViewFromParent(View)
3803 * @see #detachViewFromParent(int)
3804 */
3805 protected void attachViewToParent(View child, int index, LayoutParams params) {
3806 child.mLayoutParams = params;
3807
3808 if (index < 0) {
3809 index = mChildrenCount;
3810 }
3811
3812 addInArray(child, index);
3813
3814 child.mParent = this;
Chet Haase3b2b0fc2011-01-24 17:53:52 -08003815 child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK & ~DRAWING_CACHE_VALID) |
3816 DRAWN | INVALIDATED;
3817 this.mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818
3819 if (child.hasFocus()) {
3820 requestChildFocus(child, child.findFocus());
3821 }
3822 }
3823
3824 /**
3825 * Detaches a view from its parent. Detaching a view should be temporary and followed
3826 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3827 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3828 * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3829 *
3830 * @param child the child to detach
3831 *
3832 * @see #detachViewFromParent(int)
3833 * @see #detachViewsFromParent(int, int)
3834 * @see #detachAllViewsFromParent()
3835 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3836 * @see #removeDetachedView(View, boolean)
3837 */
3838 protected void detachViewFromParent(View child) {
3839 removeFromArray(indexOfChild(child));
3840 }
3841
3842 /**
3843 * Detaches a view from its parent. Detaching a view should be temporary and followed
3844 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3845 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3846 * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3847 *
3848 * @param index the index of the child to detach
3849 *
3850 * @see #detachViewFromParent(View)
3851 * @see #detachAllViewsFromParent()
3852 * @see #detachViewsFromParent(int, int)
3853 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3854 * @see #removeDetachedView(View, boolean)
3855 */
3856 protected void detachViewFromParent(int index) {
3857 removeFromArray(index);
3858 }
3859
3860 /**
3861 * Detaches a range of view from their parent. Detaching a view should be temporary and followed
3862 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3863 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, its
3864 * parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3865 *
3866 * @param start the first index of the childrend range to detach
3867 * @param count the number of children to detach
3868 *
3869 * @see #detachViewFromParent(View)
3870 * @see #detachViewFromParent(int)
3871 * @see #detachAllViewsFromParent()
3872 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3873 * @see #removeDetachedView(View, boolean)
3874 */
3875 protected void detachViewsFromParent(int start, int count) {
3876 removeFromArray(start, count);
3877 }
3878
3879 /**
3880 * Detaches all views from the parent. Detaching a view should be temporary and followed
3881 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3882 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3883 * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3884 *
3885 * @see #detachViewFromParent(View)
3886 * @see #detachViewFromParent(int)
3887 * @see #detachViewsFromParent(int, int)
3888 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3889 * @see #removeDetachedView(View, boolean)
3890 */
3891 protected void detachAllViewsFromParent() {
3892 final int count = mChildrenCount;
3893 if (count <= 0) {
3894 return;
3895 }
3896
3897 final View[] children = mChildren;
3898 mChildrenCount = 0;
3899
3900 for (int i = count - 1; i >= 0; i--) {
3901 children[i].mParent = null;
3902 children[i] = null;
3903 }
3904 }
3905
3906 /**
3907 * Don't call or override this method. It is used for the implementation of
3908 * the view hierarchy.
3909 */
3910 public final void invalidateChild(View child, final Rect dirty) {
3911 if (ViewDebug.TRACE_HIERARCHY) {
3912 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
3913 }
3914
3915 ViewParent parent = this;
3916
3917 final AttachInfo attachInfo = mAttachInfo;
3918 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 // If the child is drawing an animation, we want to copy this flag onto
3920 // ourselves and the parent to make sure the invalidate request goes
3921 // through
3922 final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07003923
Chet Haase70d4ba12010-10-06 09:46:45 -07003924 if (dirty == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08003925 if (child.mLayerType != LAYER_TYPE_NONE) {
3926 mPrivateFlags |= INVALIDATED;
3927 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy3a3133d2011-02-01 22:59:58 -08003928 child.mLocalDirtyRect.setEmpty();
Chet Haasedaf98e92011-01-10 14:10:36 -08003929 }
Chet Haase70d4ba12010-10-06 09:46:45 -07003930 do {
3931 View view = null;
3932 if (parent instanceof View) {
3933 view = (View) parent;
Romain Guy3a3133d2011-02-01 22:59:58 -08003934 if (view.mLayerType != LAYER_TYPE_NONE) {
3935 view.mLocalDirtyRect.setEmpty();
3936 if (view.getParent() instanceof View) {
3937 final View grandParent = (View) view.getParent();
3938 grandParent.mPrivateFlags |= INVALIDATED;
3939 grandParent.mPrivateFlags &= ~DRAWING_CACHE_VALID;
3940 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003941 }
Chet Haase70d4ba12010-10-06 09:46:45 -07003942 if ((view.mPrivateFlags & DIRTY_MASK) != 0) {
3943 // already marked dirty - we're done
3944 break;
3945 }
3946 }
3947
3948 if (drawAnimation) {
3949 if (view != null) {
3950 view.mPrivateFlags |= DRAW_ANIMATION;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07003951 } else if (parent instanceof ViewRootImpl) {
3952 ((ViewRootImpl) parent).mIsAnimating = true;
Chet Haase70d4ba12010-10-06 09:46:45 -07003953 }
3954 }
3955
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07003956 if (parent instanceof ViewRootImpl) {
3957 ((ViewRootImpl) parent).invalidate();
Chet Haase70d4ba12010-10-06 09:46:45 -07003958 parent = null;
3959 } else if (view != null) {
Chet Haase77785f92011-01-25 23:22:09 -08003960 if ((view.mPrivateFlags & DRAWN) == DRAWN ||
3961 (view.mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
Chet Haase70d4ba12010-10-06 09:46:45 -07003962 view.mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haase0d200832010-11-05 15:36:16 -07003963 view.mPrivateFlags |= DIRTY;
Chet Haase70d4ba12010-10-06 09:46:45 -07003964 parent = view.mParent;
3965 } else {
3966 parent = null;
3967 }
3968 }
3969 } while (parent != null);
3970 } else {
Chet Haase0d200832010-11-05 15:36:16 -07003971 // Check whether the child that requests the invalidate is fully opaque
Chet Haase8c39def2011-11-29 13:40:39 -08003972 // Views being animated or transformed are not considered opaque because we may
3973 // be invalidating their old position and need the parent to paint behind them.
3974 Matrix childMatrix = child.getMatrix();
Chet Haase0d200832010-11-05 15:36:16 -07003975 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
Chet Haase8c39def2011-11-29 13:40:39 -08003976 child.getAnimation() == null && childMatrix.isIdentity();
Chet Haase0d200832010-11-05 15:36:16 -07003977 // Mark the child as dirty, using the appropriate flag
3978 // Make sure we do not set both flags at the same time
Romain Guy7e68efb2011-01-07 14:50:27 -08003979 int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
Chet Haase0d200832010-11-05 15:36:16 -07003980
Romain Guybeff8d82011-02-01 23:53:34 -08003981 if (child.mLayerType != LAYER_TYPE_NONE) {
3982 mPrivateFlags |= INVALIDATED;
3983 mPrivateFlags &= ~DRAWING_CACHE_VALID;
3984 child.mLocalDirtyRect.union(dirty);
3985 }
3986
Chet Haase70d4ba12010-10-06 09:46:45 -07003987 final int[] location = attachInfo.mInvalidateChildLocation;
3988 location[CHILD_LEFT_INDEX] = child.mLeft;
3989 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase70d4ba12010-10-06 09:46:45 -07003990 if (!childMatrix.isIdentity()) {
3991 RectF boundingRect = attachInfo.mTmpTransformRect;
3992 boundingRect.set(dirty);
Romain Guya9489272011-06-22 20:58:11 -07003993 //boundingRect.inset(-0.5f, -0.5f);
Chet Haase70d4ba12010-10-06 09:46:45 -07003994 childMatrix.mapRect(boundingRect);
Romain Guya9489272011-06-22 20:58:11 -07003995 dirty.set((int) (boundingRect.left - 0.5f),
3996 (int) (boundingRect.top - 0.5f),
Chet Haase70d4ba12010-10-06 09:46:45 -07003997 (int) (boundingRect.right + 0.5f),
3998 (int) (boundingRect.bottom + 0.5f));
Romain Guy24443ea2009-05-11 11:56:30 -07003999 }
4000
Chet Haase70d4ba12010-10-06 09:46:45 -07004001 do {
4002 View view = null;
4003 if (parent instanceof View) {
4004 view = (View) parent;
Romain Guy7d7b5492011-01-24 16:33:45 -08004005 if (view.mLayerType != LAYER_TYPE_NONE &&
4006 view.getParent() instanceof View) {
4007 final View grandParent = (View) view.getParent();
4008 grandParent.mPrivateFlags |= INVALIDATED;
4009 grandParent.mPrivateFlags &= ~DRAWING_CACHE_VALID;
4010 }
Chet Haase70d4ba12010-10-06 09:46:45 -07004011 }
4012
4013 if (drawAnimation) {
4014 if (view != null) {
4015 view.mPrivateFlags |= DRAW_ANIMATION;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004016 } else if (parent instanceof ViewRootImpl) {
4017 ((ViewRootImpl) parent).mIsAnimating = true;
Chet Haase70d4ba12010-10-06 09:46:45 -07004018 }
4019 }
4020
4021 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
4022 // flag coming from the child that initiated the invalidate
Romain Guy7e68efb2011-01-07 14:50:27 -08004023 if (view != null) {
4024 if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
Romain Guy2243e552011-03-08 11:46:28 -08004025 view.getSolidColor() == 0) {
Romain Guy7e68efb2011-01-07 14:50:27 -08004026 opaqueFlag = DIRTY;
4027 }
4028 if ((view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
4029 view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
4030 }
Chet Haase70d4ba12010-10-06 09:46:45 -07004031 }
4032
4033 parent = parent.invalidateChildInParent(location, dirty);
Romain Guy24443ea2009-05-11 11:56:30 -07004034 if (view != null) {
Chet Haase70d4ba12010-10-06 09:46:45 -07004035 // Account for transform on current parent
4036 Matrix m = view.getMatrix();
4037 if (!m.isIdentity()) {
4038 RectF boundingRect = attachInfo.mTmpTransformRect;
4039 boundingRect.set(dirty);
4040 m.mapRect(boundingRect);
4041 dirty.set((int) boundingRect.left, (int) boundingRect.top,
4042 (int) (boundingRect.right + 0.5f),
4043 (int) (boundingRect.bottom + 0.5f));
4044 }
Romain Guybb93d552009-03-24 21:04:15 -07004045 }
Chet Haase70d4ba12010-10-06 09:46:45 -07004046 } while (parent != null);
4047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
4049 }
4050
4051 /**
4052 * Don't call or override this method. It is used for the implementation of
4053 * the view hierarchy.
4054 *
4055 * This implementation returns null if this ViewGroup does not have a parent,
4056 * if this ViewGroup is already fully invalidated or if the dirty rectangle
4057 * does not intersect with this ViewGroup's bounds.
4058 */
4059 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
4060 if (ViewDebug.TRACE_HIERARCHY) {
4061 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
4062 }
4063
Chet Haase77785f92011-01-25 23:22:09 -08004064 if ((mPrivateFlags & DRAWN) == DRAWN ||
4065 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
4067 FLAG_OPTIMIZE_INVALIDATE) {
4068 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
4069 location[CHILD_TOP_INDEX] - mScrollY);
4070
4071 final int left = mLeft;
4072 final int top = mTop;
4073
Chet Haasea3db8662011-07-19 10:36:05 -07004074 if ((mGroupFlags & FLAG_CLIP_CHILDREN) != FLAG_CLIP_CHILDREN ||
4075 dirty.intersect(0, 0, mRight - left, mBottom - top) ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
4077 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4078
4079 location[CHILD_LEFT_INDEX] = left;
4080 location[CHILD_TOP_INDEX] = top;
4081
Romain Guy3a3133d2011-02-01 22:59:58 -08004082 if (mLayerType != LAYER_TYPE_NONE) {
4083 mLocalDirtyRect.union(dirty);
4084 }
Romain Guybeff8d82011-02-01 23:53:34 -08004085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 return mParent;
4087 }
4088 } else {
4089 mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
4090
4091 location[CHILD_LEFT_INDEX] = mLeft;
4092 location[CHILD_TOP_INDEX] = mTop;
Chet Haasea3db8662011-07-19 10:36:05 -07004093 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
4094 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
4095 } else {
4096 // in case the dirty rect extends outside the bounds of this container
4097 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
4098 }
Romain Guy3a3133d2011-02-01 22:59:58 -08004099
4100 if (mLayerType != LAYER_TYPE_NONE) {
4101 mLocalDirtyRect.union(dirty);
4102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103
4104 return mParent;
4105 }
4106 }
4107
4108 return null;
4109 }
4110
4111 /**
4112 * Offset a rectangle that is in a descendant's coordinate
4113 * space into our coordinate space.
4114 * @param descendant A descendant of this view
4115 * @param rect A rectangle defined in descendant's coordinate space.
4116 */
4117 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
4118 offsetRectBetweenParentAndChild(descendant, rect, true, false);
4119 }
4120
4121 /**
4122 * Offset a rectangle that is in our coordinate space into an ancestor's
4123 * coordinate space.
4124 * @param descendant A descendant of this view
4125 * @param rect A rectangle defined in descendant's coordinate space.
4126 */
4127 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
4128 offsetRectBetweenParentAndChild(descendant, rect, false, false);
4129 }
4130
4131 /**
4132 * Helper method that offsets a rect either from parent to descendant or
4133 * descendant to parent.
4134 */
4135 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
4136 boolean offsetFromChildToParent, boolean clipToBounds) {
4137
4138 // already in the same coord system :)
4139 if (descendant == this) {
4140 return;
4141 }
4142
4143 ViewParent theParent = descendant.mParent;
4144
4145 // search and offset up to the parent
4146 while ((theParent != null)
4147 && (theParent instanceof View)
4148 && (theParent != this)) {
4149
4150 if (offsetFromChildToParent) {
4151 rect.offset(descendant.mLeft - descendant.mScrollX,
4152 descendant.mTop - descendant.mScrollY);
4153 if (clipToBounds) {
4154 View p = (View) theParent;
4155 rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
4156 }
4157 } else {
4158 if (clipToBounds) {
4159 View p = (View) theParent;
4160 rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
4161 }
4162 rect.offset(descendant.mScrollX - descendant.mLeft,
4163 descendant.mScrollY - descendant.mTop);
4164 }
4165
4166 descendant = (View) theParent;
4167 theParent = descendant.mParent;
4168 }
4169
4170 // now that we are up to this view, need to offset one more time
4171 // to get into our coordinate space
4172 if (theParent == this) {
4173 if (offsetFromChildToParent) {
4174 rect.offset(descendant.mLeft - descendant.mScrollX,
4175 descendant.mTop - descendant.mScrollY);
4176 } else {
4177 rect.offset(descendant.mScrollX - descendant.mLeft,
4178 descendant.mScrollY - descendant.mTop);
4179 }
4180 } else {
4181 throw new IllegalArgumentException("parameter must be a descendant of this view");
4182 }
4183 }
4184
4185 /**
4186 * Offset the vertical location of all children of this view by the specified number of pixels.
4187 *
4188 * @param offset the number of pixels to offset
4189 *
4190 * @hide
4191 */
4192 public void offsetChildrenTopAndBottom(int offset) {
4193 final int count = mChildrenCount;
4194 final View[] children = mChildren;
4195
4196 for (int i = 0; i < count; i++) {
4197 final View v = children[i];
4198 v.mTop += offset;
4199 v.mBottom += offset;
4200 }
4201 }
4202
4203 /**
4204 * {@inheritDoc}
4205 */
4206 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08004207 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
4208 // but for some simple tests it can be useful. If we don't have attach info this
4209 // will allocate memory.
4210 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01004211 rect.set(r);
4212
4213 if (!child.hasIdentityMatrix()) {
4214 child.getMatrix().mapRect(rect);
4215 }
4216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 int dx = child.mLeft - mScrollX;
4218 int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01004219
4220 rect.offset(dx, dy);
4221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01004223 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08004224 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
4225 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01004226 position[0] = offset.x;
4227 position[1] = offset.y;
4228 child.getMatrix().mapPoints(position);
4229 offset.x = (int) (position[0] + 0.5f);
4230 offset.y = (int) (position[1] + 0.5f);
4231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 offset.x += dx;
4233 offset.y += dy;
4234 }
Gilles Debunnecea45132011-11-24 02:19:27 +01004235
4236 if (rect.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
4237 if (mParent == null) return true;
4238 r.set((int) (rect.left + 0.5f), (int) (rect.top + 0.5f),
4239 (int) (rect.right + 0.5f), (int) (rect.bottom + 0.5f));
4240 return mParent.getChildVisibleRect(this, r, offset);
4241 }
4242
4243 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 }
4245
4246 /**
4247 * {@inheritDoc}
4248 */
4249 @Override
Chet Haase9c087442011-01-12 16:20:16 -08004250 public final void layout(int l, int t, int r, int b) {
4251 if (mTransition == null || !mTransition.isChangingLayout()) {
4252 super.layout(l, t, r, b);
4253 } else {
4254 // record the fact that we noop'd it; request layout when transition finishes
4255 mLayoutSuppressed = true;
4256 }
4257 }
4258
4259 /**
4260 * {@inheritDoc}
4261 */
4262 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 protected abstract void onLayout(boolean changed,
4264 int l, int t, int r, int b);
4265
4266 /**
4267 * Indicates whether the view group has the ability to animate its children
4268 * after the first layout.
4269 *
4270 * @return true if the children can be animated, false otherwise
4271 */
4272 protected boolean canAnimate() {
4273 return mLayoutAnimationController != null;
4274 }
4275
4276 /**
4277 * Runs the layout animation. Calling this method triggers a relayout of
4278 * this view group.
4279 */
4280 public void startLayoutAnimation() {
4281 if (mLayoutAnimationController != null) {
4282 mGroupFlags |= FLAG_RUN_ANIMATION;
4283 requestLayout();
4284 }
4285 }
4286
4287 /**
4288 * Schedules the layout animation to be played after the next layout pass
4289 * of this view group. This can be used to restart the layout animation
4290 * when the content of the view group changes or when the activity is
4291 * paused and resumed.
4292 */
4293 public void scheduleLayoutAnimation() {
4294 mGroupFlags |= FLAG_RUN_ANIMATION;
4295 }
4296
4297 /**
4298 * Sets the layout animation controller used to animate the group's
4299 * children after the first layout.
4300 *
4301 * @param controller the animation controller
4302 */
4303 public void setLayoutAnimation(LayoutAnimationController controller) {
4304 mLayoutAnimationController = controller;
4305 if (mLayoutAnimationController != null) {
4306 mGroupFlags |= FLAG_RUN_ANIMATION;
4307 }
4308 }
4309
4310 /**
4311 * Returns the layout animation controller used to animate the group's
4312 * children.
4313 *
4314 * @return the current animation controller
4315 */
4316 public LayoutAnimationController getLayoutAnimation() {
4317 return mLayoutAnimationController;
4318 }
4319
4320 /**
4321 * Indicates whether the children's drawing cache is used during a layout
4322 * animation. By default, the drawing cache is enabled but this will prevent
4323 * nested layout animations from working. To nest animations, you must disable
4324 * the cache.
4325 *
4326 * @return true if the animation cache is enabled, false otherwise
4327 *
4328 * @see #setAnimationCacheEnabled(boolean)
4329 * @see View#setDrawingCacheEnabled(boolean)
4330 */
4331 @ViewDebug.ExportedProperty
4332 public boolean isAnimationCacheEnabled() {
4333 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
4334 }
4335
4336 /**
4337 * Enables or disables the children's drawing cache during a layout animation.
4338 * By default, the drawing cache is enabled but this will prevent nested
4339 * layout animations from working. To nest animations, you must disable the
4340 * cache.
4341 *
4342 * @param enabled true to enable the animation cache, false otherwise
4343 *
4344 * @see #isAnimationCacheEnabled()
4345 * @see View#setDrawingCacheEnabled(boolean)
4346 */
4347 public void setAnimationCacheEnabled(boolean enabled) {
4348 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
4349 }
4350
4351 /**
4352 * Indicates whether this ViewGroup will always try to draw its children using their
4353 * drawing cache. By default this property is enabled.
4354 *
4355 * @return true if the animation cache is enabled, false otherwise
4356 *
4357 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4358 * @see #setChildrenDrawnWithCacheEnabled(boolean)
4359 * @see View#setDrawingCacheEnabled(boolean)
4360 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004361 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 public boolean isAlwaysDrawnWithCacheEnabled() {
4363 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
4364 }
4365
4366 /**
4367 * Indicates whether this ViewGroup will always try to draw its children using their
4368 * drawing cache. This property can be set to true when the cache rendering is
4369 * slightly different from the children's normal rendering. Renderings can be different,
4370 * for instance, when the cache's quality is set to low.
4371 *
4372 * When this property is disabled, the ViewGroup will use the drawing cache of its
4373 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
4374 * when to start using the drawing cache and when to stop using it.
4375 *
4376 * @param always true to always draw with the drawing cache, false otherwise
4377 *
4378 * @see #isAlwaysDrawnWithCacheEnabled()
4379 * @see #setChildrenDrawnWithCacheEnabled(boolean)
4380 * @see View#setDrawingCacheEnabled(boolean)
4381 * @see View#setDrawingCacheQuality(int)
4382 */
4383 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
4384 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
4385 }
4386
4387 /**
4388 * Indicates whether the ViewGroup is currently drawing its children using
4389 * their drawing cache.
4390 *
4391 * @return true if children should be drawn with their cache, false otherwise
4392 *
4393 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4394 * @see #setChildrenDrawnWithCacheEnabled(boolean)
4395 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004396 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 protected boolean isChildrenDrawnWithCacheEnabled() {
4398 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
4399 }
4400
4401 /**
4402 * Tells the ViewGroup to draw its children using their drawing cache. This property
4403 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
4404 * will be used only if it has been enabled.
4405 *
4406 * Subclasses should call this method to start and stop using the drawing cache when
4407 * they perform performance sensitive operations, like scrolling or animating.
4408 *
4409 * @param enabled true if children should be drawn with their cache, false otherwise
4410 *
4411 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4412 * @see #isChildrenDrawnWithCacheEnabled()
4413 */
4414 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
4415 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
4416 }
4417
Romain Guy293451e2009-11-04 13:59:48 -08004418 /**
4419 * Indicates whether the ViewGroup is drawing its children in the order defined by
4420 * {@link #getChildDrawingOrder(int, int)}.
4421 *
4422 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
4423 * false otherwise
4424 *
4425 * @see #setChildrenDrawingOrderEnabled(boolean)
4426 * @see #getChildDrawingOrder(int, int)
4427 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004428 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08004429 protected boolean isChildrenDrawingOrderEnabled() {
4430 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
4431 }
4432
4433 /**
4434 * Tells the ViewGroup whether to draw its children in the order defined by the method
4435 * {@link #getChildDrawingOrder(int, int)}.
4436 *
4437 * @param enabled true if the order of the children when drawing is determined by
4438 * {@link #getChildDrawingOrder(int, int)}, false otherwise
4439 *
4440 * @see #isChildrenDrawingOrderEnabled()
4441 * @see #getChildDrawingOrder(int, int)
4442 */
4443 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
4444 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
4445 }
4446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 private void setBooleanFlag(int flag, boolean value) {
4448 if (value) {
4449 mGroupFlags |= flag;
4450 } else {
4451 mGroupFlags &= ~flag;
4452 }
4453 }
4454
4455 /**
4456 * Returns an integer indicating what types of drawing caches are kept in memory.
4457 *
4458 * @see #setPersistentDrawingCache(int)
4459 * @see #setAnimationCacheEnabled(boolean)
4460 *
4461 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
4462 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
4463 * and {@link #PERSISTENT_ALL_CACHES}
4464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004465 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07004467 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
4469 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
4470 })
4471 public int getPersistentDrawingCache() {
4472 return mPersistentDrawingCache;
4473 }
4474
4475 /**
4476 * Indicates what types of drawing caches should be kept in memory after
4477 * they have been created.
4478 *
4479 * @see #getPersistentDrawingCache()
4480 * @see #setAnimationCacheEnabled(boolean)
4481 *
4482 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
4483 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
4484 * and {@link #PERSISTENT_ALL_CACHES}
4485 */
4486 public void setPersistentDrawingCache(int drawingCacheToKeep) {
4487 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
4488 }
4489
4490 /**
4491 * Returns a new set of layout parameters based on the supplied attributes set.
4492 *
4493 * @param attrs the attributes to build the layout parameters from
4494 *
4495 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
4496 * of its descendants
4497 */
4498 public LayoutParams generateLayoutParams(AttributeSet attrs) {
4499 return new LayoutParams(getContext(), attrs);
4500 }
4501
4502 /**
4503 * Returns a safe set of layout parameters based on the supplied layout params.
4504 * When a ViewGroup is passed a View whose layout params do not pass the test of
4505 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
4506 * is invoked. This method should return a new set of layout params suitable for
4507 * this ViewGroup, possibly by copying the appropriate attributes from the
4508 * specified set of layout params.
4509 *
4510 * @param p The layout parameters to convert into a suitable set of layout parameters
4511 * for this ViewGroup.
4512 *
4513 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
4514 * of its descendants
4515 */
4516 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
4517 return p;
4518 }
4519
4520 /**
4521 * Returns a set of default layout parameters. These parameters are requested
4522 * when the View passed to {@link #addView(View)} has no layout parameters
4523 * already set. If null is returned, an exception is thrown from addView.
4524 *
4525 * @return a set of default layout parameters or null
4526 */
4527 protected LayoutParams generateDefaultLayoutParams() {
4528 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
4529 }
4530
4531 /**
Romain Guy13922e02009-05-12 17:56:14 -07004532 * @hide
4533 */
4534 @Override
4535 protected boolean dispatchConsistencyCheck(int consistency) {
4536 boolean result = super.dispatchConsistencyCheck(consistency);
4537
4538 final int count = mChildrenCount;
4539 final View[] children = mChildren;
4540 for (int i = 0; i < count; i++) {
4541 if (!children[i].dispatchConsistencyCheck(consistency)) result = false;
4542 }
4543
4544 return result;
4545 }
4546
4547 /**
4548 * @hide
4549 */
4550 @Override
4551 protected boolean onConsistencyCheck(int consistency) {
4552 boolean result = super.onConsistencyCheck(consistency);
4553
4554 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
4555 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
4556
4557 if (checkLayout) {
4558 final int count = mChildrenCount;
4559 final View[] children = mChildren;
4560 for (int i = 0; i < count; i++) {
4561 if (children[i].getParent() != this) {
4562 result = false;
4563 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
4564 "View " + children[i] + " has no parent/a parent that is not " + this);
4565 }
4566 }
4567 }
4568
4569 if (checkDrawing) {
4570 // If this group is dirty, check that the parent is dirty as well
4571 if ((mPrivateFlags & DIRTY_MASK) != 0) {
4572 final ViewParent parent = getParent();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004573 if (parent != null && !(parent instanceof ViewRootImpl)) {
Romain Guy13922e02009-05-12 17:56:14 -07004574 if ((((View) parent).mPrivateFlags & DIRTY_MASK) == 0) {
4575 result = false;
4576 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
4577 "ViewGroup " + this + " is dirty but its parent is not: " + this);
4578 }
4579 }
4580 }
4581 }
4582
4583 return result;
4584 }
4585
4586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 * {@inheritDoc}
4588 */
4589 @Override
4590 protected void debug(int depth) {
4591 super.debug(depth);
4592 String output;
4593
4594 if (mFocused != null) {
4595 output = debugIndent(depth);
4596 output += "mFocused";
4597 Log.d(VIEW_LOG_TAG, output);
4598 }
4599 if (mChildrenCount != 0) {
4600 output = debugIndent(depth);
4601 output += "{";
4602 Log.d(VIEW_LOG_TAG, output);
4603 }
4604 int count = mChildrenCount;
4605 for (int i = 0; i < count; i++) {
4606 View child = mChildren[i];
4607 child.debug(depth + 1);
4608 }
4609
4610 if (mChildrenCount != 0) {
4611 output = debugIndent(depth);
4612 output += "}";
4613 Log.d(VIEW_LOG_TAG, output);
4614 }
4615 }
4616
4617 /**
4618 * Returns the position in the group of the specified child view.
4619 *
4620 * @param child the view for which to get the position
4621 * @return a positive integer representing the position of the view in the
4622 * group, or -1 if the view does not exist in the group
4623 */
4624 public int indexOfChild(View child) {
4625 final int count = mChildrenCount;
4626 final View[] children = mChildren;
4627 for (int i = 0; i < count; i++) {
4628 if (children[i] == child) {
4629 return i;
4630 }
4631 }
4632 return -1;
4633 }
4634
4635 /**
4636 * Returns the number of children in the group.
4637 *
4638 * @return a positive integer representing the number of children in
4639 * the group
4640 */
4641 public int getChildCount() {
4642 return mChildrenCount;
4643 }
4644
4645 /**
4646 * Returns the view at the specified position in the group.
4647 *
4648 * @param index the position at which to get the view from
4649 * @return the view at the specified position or null if the position
4650 * does not exist within the group
4651 */
4652 public View getChildAt(int index) {
Adam Powell3ba8f5d2011-03-07 15:36:33 -08004653 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 return null;
4655 }
Adam Powell3ba8f5d2011-03-07 15:36:33 -08004656 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 }
4658
4659 /**
4660 * Ask all of the children of this view to measure themselves, taking into
4661 * account both the MeasureSpec requirements for this view and its padding.
4662 * We skip children that are in the GONE state The heavy lifting is done in
4663 * getChildMeasureSpec.
4664 *
4665 * @param widthMeasureSpec The width requirements for this view
4666 * @param heightMeasureSpec The height requirements for this view
4667 */
4668 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
4669 final int size = mChildrenCount;
4670 final View[] children = mChildren;
4671 for (int i = 0; i < size; ++i) {
4672 final View child = children[i];
4673 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
4674 measureChild(child, widthMeasureSpec, heightMeasureSpec);
4675 }
4676 }
4677 }
4678
4679 /**
4680 * Ask one of the children of this view to measure itself, taking into
4681 * account both the MeasureSpec requirements for this view and its padding.
4682 * The heavy lifting is done in getChildMeasureSpec.
4683 *
4684 * @param child The child to measure
4685 * @param parentWidthMeasureSpec The width requirements for this view
4686 * @param parentHeightMeasureSpec The height requirements for this view
4687 */
4688 protected void measureChild(View child, int parentWidthMeasureSpec,
4689 int parentHeightMeasureSpec) {
4690 final LayoutParams lp = child.getLayoutParams();
4691
4692 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
4693 mPaddingLeft + mPaddingRight, lp.width);
4694 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
4695 mPaddingTop + mPaddingBottom, lp.height);
4696
4697 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
4698 }
4699
4700 /**
4701 * Ask one of the children of this view to measure itself, taking into
4702 * account both the MeasureSpec requirements for this view and its padding
4703 * and margins. The child must have MarginLayoutParams The heavy lifting is
4704 * done in getChildMeasureSpec.
4705 *
4706 * @param child The child to measure
4707 * @param parentWidthMeasureSpec The width requirements for this view
4708 * @param widthUsed Extra space that has been used up by the parent
4709 * horizontally (possibly by other children of the parent)
4710 * @param parentHeightMeasureSpec The height requirements for this view
4711 * @param heightUsed Extra space that has been used up by the parent
4712 * vertically (possibly by other children of the parent)
4713 */
4714 protected void measureChildWithMargins(View child,
4715 int parentWidthMeasureSpec, int widthUsed,
4716 int parentHeightMeasureSpec, int heightUsed) {
4717 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
4718
4719 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
4720 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
4721 + widthUsed, lp.width);
4722 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
4723 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
4724 + heightUsed, lp.height);
4725
4726 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
4727 }
4728
4729 /**
4730 * Does the hard part of measureChildren: figuring out the MeasureSpec to
4731 * pass to a particular child. This method figures out the right MeasureSpec
4732 * for one dimension (height or width) of one child view.
4733 *
4734 * The goal is to combine information from our MeasureSpec with the
4735 * LayoutParams of the child to get the best possible results. For example,
4736 * if the this view knows its size (because its MeasureSpec has a mode of
4737 * EXACTLY), and the child has indicated in its LayoutParams that it wants
4738 * to be the same size as the parent, the parent should ask the child to
4739 * layout given an exact size.
4740 *
4741 * @param spec The requirements for this view
4742 * @param padding The padding of this view for the current dimension and
4743 * margins, if applicable
4744 * @param childDimension How big the child wants to be in the current
4745 * dimension
4746 * @return a MeasureSpec integer for the child
4747 */
4748 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
4749 int specMode = MeasureSpec.getMode(spec);
4750 int specSize = MeasureSpec.getSize(spec);
4751
4752 int size = Math.max(0, specSize - padding);
4753
4754 int resultSize = 0;
4755 int resultMode = 0;
4756
4757 switch (specMode) {
4758 // Parent has imposed an exact size on us
4759 case MeasureSpec.EXACTLY:
4760 if (childDimension >= 0) {
4761 resultSize = childDimension;
4762 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08004763 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 // Child wants to be our size. So be it.
4765 resultSize = size;
4766 resultMode = MeasureSpec.EXACTLY;
4767 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
4768 // Child wants to determine its own size. It can't be
4769 // bigger than us.
4770 resultSize = size;
4771 resultMode = MeasureSpec.AT_MOST;
4772 }
4773 break;
4774
4775 // Parent has imposed a maximum size on us
4776 case MeasureSpec.AT_MOST:
4777 if (childDimension >= 0) {
4778 // Child wants a specific size... so be it
4779 resultSize = childDimension;
4780 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08004781 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 // Child wants to be our size, but our size is not fixed.
4783 // Constrain child to not be bigger than us.
4784 resultSize = size;
4785 resultMode = MeasureSpec.AT_MOST;
4786 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
4787 // Child wants to determine its own size. It can't be
4788 // bigger than us.
4789 resultSize = size;
4790 resultMode = MeasureSpec.AT_MOST;
4791 }
4792 break;
4793
4794 // Parent asked to see how big we want to be
4795 case MeasureSpec.UNSPECIFIED:
4796 if (childDimension >= 0) {
4797 // Child wants a specific size... let him have it
4798 resultSize = childDimension;
4799 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08004800 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 // Child wants to be our size... find out how big it should
4802 // be
4803 resultSize = 0;
4804 resultMode = MeasureSpec.UNSPECIFIED;
4805 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
4806 // Child wants to determine its own size.... find out how
4807 // big it should be
4808 resultSize = 0;
4809 resultMode = MeasureSpec.UNSPECIFIED;
4810 }
4811 break;
4812 }
4813 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
4814 }
4815
4816
4817 /**
4818 * Removes any pending animations for views that have been removed. Call
4819 * this if you don't want animations for exiting views to stack up.
4820 */
4821 public void clearDisappearingChildren() {
4822 if (mDisappearingChildren != null) {
4823 mDisappearingChildren.clear();
4824 }
4825 }
4826
4827 /**
4828 * Add a view which is removed from mChildren but still needs animation
4829 *
4830 * @param v View to add
4831 */
4832 private void addDisappearingView(View v) {
4833 ArrayList<View> disappearingChildren = mDisappearingChildren;
4834
4835 if (disappearingChildren == null) {
4836 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
4837 }
4838
4839 disappearingChildren.add(v);
4840 }
4841
4842 /**
4843 * Cleanup a view when its animation is done. This may mean removing it from
4844 * the list of disappearing views.
4845 *
4846 * @param view The view whose animation has finished
4847 * @param animation The animation, cannot be null
4848 */
4849 private void finishAnimatingView(final View view, Animation animation) {
4850 final ArrayList<View> disappearingChildren = mDisappearingChildren;
4851 if (disappearingChildren != null) {
4852 if (disappearingChildren.contains(view)) {
4853 disappearingChildren.remove(view);
4854
4855 if (view.mAttachInfo != null) {
4856 view.dispatchDetachedFromWindow();
4857 }
4858
4859 view.clearAnimation();
4860 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4861 }
4862 }
4863
4864 if (animation != null && !animation.getFillAfter()) {
4865 view.clearAnimation();
4866 }
4867
4868 if ((view.mPrivateFlags & ANIMATION_STARTED) == ANIMATION_STARTED) {
4869 view.onAnimationEnd();
4870 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
4871 // so we'd rather be safe than sorry
4872 view.mPrivateFlags &= ~ANIMATION_STARTED;
4873 // Draw one more frame after the animation is done
4874 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4875 }
4876 }
4877
Chet Haaseb20db3e2010-09-10 13:07:30 -07004878 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07004879 * Utility function called by View during invalidation to determine whether a view that
4880 * is invisible or gone should still be invalidated because it is being transitioned (and
4881 * therefore still needs to be drawn).
4882 */
4883 boolean isViewTransitioning(View view) {
4884 return (mTransitioningViews != null && mTransitioningViews.contains(view));
4885 }
4886
4887 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07004888 * This method tells the ViewGroup that the given View object, which should have this
4889 * ViewGroup as its parent,
4890 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
4891 * is removed from its parent. This allows animations, such as those used by
4892 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
4893 * the removal of views. A call to this method should always be accompanied by a later call
4894 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
4895 * so that the View finally gets removed.
4896 *
4897 * @param view The View object to be kept visible even if it gets removed from its parent.
4898 */
4899 public void startViewTransition(View view) {
4900 if (view.mParent == this) {
4901 if (mTransitioningViews == null) {
4902 mTransitioningViews = new ArrayList<View>();
4903 }
4904 mTransitioningViews.add(view);
4905 }
4906 }
4907
4908 /**
4909 * This method should always be called following an earlier call to
4910 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
4911 * and will no longer be displayed. Note that this method does not perform the functionality
4912 * of removing a view from its parent; it just discontinues the display of a View that
4913 * has previously been removed.
4914 *
4915 * @return view The View object that has been removed but is being kept around in the visible
4916 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
4917 */
4918 public void endViewTransition(View view) {
4919 if (mTransitioningViews != null) {
4920 mTransitioningViews.remove(view);
4921 final ArrayList<View> disappearingChildren = mDisappearingChildren;
4922 if (disappearingChildren != null && disappearingChildren.contains(view)) {
4923 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07004924 if (mVisibilityChangingChildren != null &&
4925 mVisibilityChangingChildren.contains(view)) {
4926 mVisibilityChangingChildren.remove(view);
4927 } else {
4928 if (view.mAttachInfo != null) {
4929 view.dispatchDetachedFromWindow();
4930 }
4931 if (view.mParent != null) {
4932 view.mParent = null;
4933 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07004934 }
4935 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4936 }
4937 }
4938 }
4939
Chet Haase21cd1382010-09-01 17:42:29 -07004940 private LayoutTransition.TransitionListener mLayoutTransitionListener =
4941 new LayoutTransition.TransitionListener() {
4942 @Override
4943 public void startTransition(LayoutTransition transition, ViewGroup container,
4944 View view, int transitionType) {
4945 // We only care about disappearing items, since we need special logic to keep
4946 // those items visible after they've been 'removed'
4947 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004948 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07004949 }
4950 }
4951
4952 @Override
4953 public void endTransition(LayoutTransition transition, ViewGroup container,
4954 View view, int transitionType) {
Chet Haase9c087442011-01-12 16:20:16 -08004955 if (mLayoutSuppressed && !transition.isChangingLayout()) {
4956 requestLayout();
4957 mLayoutSuppressed = false;
4958 }
Chet Haase21cd1382010-09-01 17:42:29 -07004959 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004960 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07004961 }
4962 }
4963 };
4964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 /**
4966 * {@inheritDoc}
4967 */
4968 @Override
4969 public boolean gatherTransparentRegion(Region region) {
4970 // If no transparent regions requested, we are always opaque.
4971 final boolean meOpaque = (mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) == 0;
4972 if (meOpaque && region == null) {
4973 // The caller doesn't care about the region, so stop now.
4974 return true;
4975 }
4976 super.gatherTransparentRegion(region);
4977 final View[] children = mChildren;
4978 final int count = mChildrenCount;
4979 boolean noneOfTheChildrenAreTransparent = true;
4980 for (int i = 0; i < count; i++) {
4981 final View child = children[i];
Mathias Agopiane3381152010-12-02 15:19:36 -08004982 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 if (!child.gatherTransparentRegion(region)) {
4984 noneOfTheChildrenAreTransparent = false;
4985 }
4986 }
4987 }
4988 return meOpaque || noneOfTheChildrenAreTransparent;
4989 }
4990
4991 /**
4992 * {@inheritDoc}
4993 */
4994 public void requestTransparentRegion(View child) {
4995 if (child != null) {
4996 child.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
4997 if (mParent != null) {
4998 mParent.requestTransparentRegion(this);
4999 }
5000 }
5001 }
Romain Guy8506ab42009-06-11 17:35:47 -07005002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003
5004 @Override
5005 protected boolean fitSystemWindows(Rect insets) {
5006 boolean done = super.fitSystemWindows(insets);
5007 if (!done) {
5008 final int count = mChildrenCount;
5009 final View[] children = mChildren;
5010 for (int i = 0; i < count; i++) {
5011 done = children[i].fitSystemWindows(insets);
5012 if (done) {
5013 break;
5014 }
5015 }
5016 }
5017 return done;
5018 }
5019
5020 /**
5021 * Returns the animation listener to which layout animation events are
5022 * sent.
5023 *
5024 * @return an {@link android.view.animation.Animation.AnimationListener}
5025 */
5026 public Animation.AnimationListener getLayoutAnimationListener() {
5027 return mAnimationListener;
5028 }
5029
5030 @Override
5031 protected void drawableStateChanged() {
5032 super.drawableStateChanged();
5033
5034 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
5035 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
5036 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
5037 + " child has duplicateParentState set to true");
5038 }
5039
5040 final View[] children = mChildren;
5041 final int count = mChildrenCount;
5042
5043 for (int i = 0; i < count; i++) {
5044 final View child = children[i];
5045 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
5046 child.refreshDrawableState();
5047 }
5048 }
5049 }
5050 }
5051
5052 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07005053 public void jumpDrawablesToCurrentState() {
5054 super.jumpDrawablesToCurrentState();
5055 final View[] children = mChildren;
5056 final int count = mChildrenCount;
5057 for (int i = 0; i < count; i++) {
5058 children[i].jumpDrawablesToCurrentState();
5059 }
5060 }
5061
5062 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 protected int[] onCreateDrawableState(int extraSpace) {
5064 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
5065 return super.onCreateDrawableState(extraSpace);
5066 }
5067
5068 int need = 0;
5069 int n = getChildCount();
5070 for (int i = 0; i < n; i++) {
5071 int[] childState = getChildAt(i).getDrawableState();
5072
5073 if (childState != null) {
5074 need += childState.length;
5075 }
5076 }
5077
5078 int[] state = super.onCreateDrawableState(extraSpace + need);
5079
5080 for (int i = 0; i < n; i++) {
5081 int[] childState = getChildAt(i).getDrawableState();
5082
5083 if (childState != null) {
5084 state = mergeDrawableStates(state, childState);
5085 }
5086 }
5087
5088 return state;
5089 }
5090
5091 /**
5092 * Sets whether this ViewGroup's drawable states also include
5093 * its children's drawable states. This is used, for example, to
5094 * make a group appear to be focused when its child EditText or button
5095 * is focused.
5096 */
5097 public void setAddStatesFromChildren(boolean addsStates) {
5098 if (addsStates) {
5099 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
5100 } else {
5101 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
5102 }
5103
5104 refreshDrawableState();
5105 }
5106
5107 /**
5108 * Returns whether this ViewGroup's drawable states also include
5109 * its children's drawable states. This is used, for example, to
5110 * make a group appear to be focused when its child EditText or button
5111 * is focused.
5112 */
5113 public boolean addStatesFromChildren() {
5114 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
5115 }
5116
5117 /**
5118 * If {link #addStatesFromChildren} is true, refreshes this group's
5119 * drawable state (to include the states from its children).
5120 */
5121 public void childDrawableStateChanged(View child) {
5122 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
5123 refreshDrawableState();
5124 }
5125 }
5126
5127 /**
5128 * Specifies the animation listener to which layout animation events must
5129 * be sent. Only
5130 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
5131 * and
5132 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
5133 * are invoked.
5134 *
5135 * @param animationListener the layout animation listener
5136 */
5137 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
5138 mAnimationListener = animationListener;
5139 }
5140
5141 /**
Chet Haasecca2c982011-05-20 14:34:18 -07005142 * This method is called by LayoutTransition when there are 'changing' animations that need
5143 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
5144 * starts all pending transitions prior to the drawing phase in the current traversal.
5145 *
5146 * @param transition The LayoutTransition to be started on the next traversal.
5147 *
5148 * @hide
5149 */
5150 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005151 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07005152 if (viewAncestor != null) {
5153 viewAncestor.requestTransitionStart(transition);
5154 }
Chet Haasecca2c982011-05-20 14:34:18 -07005155 }
5156
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005157 @Override
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005158 protected void resetResolvedLayoutDirection() {
5159 super.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005160
5161 // Take care of resetting the children resolution too
5162 final int count = getChildCount();
5163 for (int i = 0; i < count; i++) {
5164 final View child = getChildAt(i);
5165 if (child.getLayoutDirection() == LAYOUT_DIRECTION_INHERIT) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005166 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005167 }
5168 }
5169 }
5170
Fabrice Di Meglio22268862011-06-27 18:13:18 -07005171 @Override
5172 protected void resetResolvedTextDirection() {
5173 super.resetResolvedTextDirection();
5174
5175 // Take care of resetting the children resolution too
5176 final int count = getChildCount();
5177 for (int i = 0; i < count; i++) {
5178 final View child = getChildAt(i);
5179 if (child.getTextDirection() == TEXT_DIRECTION_INHERIT) {
5180 child.resetResolvedTextDirection();
5181 }
5182 }
5183 }
5184
5185 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005186 * Return true if the pressed state should be delayed for children or descendants of this
5187 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
5188 * This prevents the pressed state from appearing when the user is actually trying to scroll
5189 * the content.
5190 *
5191 * The default implementation returns true for compatibility reasons. Subclasses that do
5192 * not scroll should generally override this method and return false.
5193 */
5194 public boolean shouldDelayChildPressedState() {
5195 return true;
5196 }
5197
5198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 * LayoutParams are used by views to tell their parents how they want to be
5200 * laid out. See
5201 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
5202 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07005203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 * <p>
5205 * The base LayoutParams class just describes how big the view wants to be
5206 * for both width and height. For each dimension, it can specify one of:
5207 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005208 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
5209 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
5211 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005212 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 * </ul>
5214 * There are subclasses of LayoutParams for different subclasses of
5215 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07005216 * LayoutParams which adds an X and Y value.</p>
5217 *
5218 * <div class="special reference">
5219 * <h3>Developer Guides</h3>
5220 * <p>For more information about creating user interface layouts, read the
5221 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
5222 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 *
5224 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
5225 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
5226 */
5227 public static class LayoutParams {
5228 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005229 * Special value for the height or width requested by a View.
5230 * FILL_PARENT means that the view wants to be as big as its parent,
5231 * minus the parent's padding, if any. This value is deprecated
5232 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 */
Romain Guy980a9382010-01-08 15:06:28 -08005234 @SuppressWarnings({"UnusedDeclaration"})
5235 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 public static final int FILL_PARENT = -1;
5237
5238 /**
5239 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08005240 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005241 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08005242 */
5243 public static final int MATCH_PARENT = -1;
5244
5245 /**
5246 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 * WRAP_CONTENT means that the view wants to be just large enough to fit
5248 * its own internal content, taking its own padding into account.
5249 */
5250 public static final int WRAP_CONTENT = -2;
5251
5252 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005253 * Information about how wide the view wants to be. Can be one of the
5254 * constants FILL_PARENT (replaced by MATCH_PARENT ,
5255 * in API Level 8) or WRAP_CONTENT. or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005257 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08005258 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
5260 })
5261 public int width;
5262
5263 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005264 * Information about how tall the view wants to be. Can be one of the
5265 * constants FILL_PARENT (replaced by MATCH_PARENT ,
5266 * in API Level 8) or WRAP_CONTENT. or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005268 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08005269 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
5271 })
5272 public int height;
5273
5274 /**
5275 * Used to animate layouts.
5276 */
5277 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
5278
5279 /**
5280 * Creates a new set of layout parameters. The values are extracted from
5281 * the supplied attributes set and context. The XML attributes mapped
5282 * to this set of layout parameters are:
5283 *
5284 * <ul>
5285 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005286 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
5287 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005289 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
5290 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 * </ul>
5292 *
5293 * @param c the application environment
5294 * @param attrs the set of attributes from which to extract the layout
5295 * parameters' values
5296 */
5297 public LayoutParams(Context c, AttributeSet attrs) {
5298 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
5299 setBaseAttributes(a,
5300 R.styleable.ViewGroup_Layout_layout_width,
5301 R.styleable.ViewGroup_Layout_layout_height);
5302 a.recycle();
5303 }
5304
5305 /**
5306 * Creates a new set of layout parameters with the specified width
5307 * and height.
5308 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005309 * @param width the width, either {@link #WRAP_CONTENT},
5310 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
5311 * API Level 8), or a fixed size in pixels
5312 * @param height the height, either {@link #WRAP_CONTENT},
5313 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
5314 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 */
5316 public LayoutParams(int width, int height) {
5317 this.width = width;
5318 this.height = height;
5319 }
5320
5321 /**
5322 * Copy constructor. Clones the width and height values of the source.
5323 *
5324 * @param source The layout params to copy from.
5325 */
5326 public LayoutParams(LayoutParams source) {
5327 this.width = source.width;
5328 this.height = source.height;
5329 }
5330
5331 /**
5332 * Used internally by MarginLayoutParams.
5333 * @hide
5334 */
5335 LayoutParams() {
5336 }
5337
5338 /**
5339 * Extracts the layout parameters from the supplied attributes.
5340 *
5341 * @param a the style attributes to extract the parameters from
5342 * @param widthAttr the identifier of the width attribute
5343 * @param heightAttr the identifier of the height attribute
5344 */
5345 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
5346 width = a.getLayoutDimension(widthAttr, "layout_width");
5347 height = a.getLayoutDimension(heightAttr, "layout_height");
5348 }
5349
5350 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005351 * Resolve layout parameters depending on the layout direction. Subclasses that care about
5352 * layoutDirection changes should override this method. The default implementation does
5353 * nothing.
5354 *
5355 * @param layoutDirection the direction of the layout
5356 *
5357 * {@link View#LAYOUT_DIRECTION_LTR}
5358 * {@link View#LAYOUT_DIRECTION_RTL}
5359 *
5360 * @hide
5361 */
5362 protected void resolveWithDirection(int layoutDirection) {
5363 }
5364
5365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 * Returns a String representation of this set of layout parameters.
5367 *
5368 * @param output the String to prepend to the internal representation
5369 * @return a String with the following format: output +
5370 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07005371 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 * @hide
5373 */
5374 public String debug(String output) {
5375 return output + "ViewGroup.LayoutParams={ width="
5376 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
5377 }
5378
5379 /**
5380 * Converts the specified size to a readable String.
5381 *
5382 * @param size the size to convert
5383 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07005384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 * @hide
5386 */
5387 protected static String sizeToString(int size) {
5388 if (size == WRAP_CONTENT) {
5389 return "wrap-content";
5390 }
Romain Guy980a9382010-01-08 15:06:28 -08005391 if (size == MATCH_PARENT) {
5392 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 }
5394 return String.valueOf(size);
5395 }
5396 }
5397
5398 /**
5399 * Per-child layout information for layouts that support margins.
5400 * See
5401 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
5402 * for a list of all child view attributes that this class supports.
5403 */
5404 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
5405 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005406 * The left margin in pixels of the child. Whenever this value is changed, a call to
5407 * {@link android.view.View#requestLayout()} needs to be done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005409 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 public int leftMargin;
5411
5412 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005413 * The top margin in pixels of the child. Whenever this value is changed, a call to
5414 * {@link android.view.View#requestLayout()} needs to be done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005416 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 public int topMargin;
5418
5419 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005420 * The right margin in pixels of the child. Whenever this value is changed, a call to
5421 * {@link android.view.View#requestLayout()} needs to be done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005423 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 public int rightMargin;
5425
5426 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005427 * The bottom margin in pixels of the child. Whenever this value is changed, a call to
5428 * {@link android.view.View#requestLayout()} needs to be done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005430 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 public int bottomMargin;
5432
5433 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005434 * The start margin in pixels of the child.
5435 *
5436 * @hide
5437 *
5438 */
5439 @ViewDebug.ExportedProperty(category = "layout")
5440 protected int startMargin = DEFAULT_RELATIVE;
5441
5442 /**
5443 * The end margin in pixels of the child.
5444 *
5445 * @hide
5446 */
5447 @ViewDebug.ExportedProperty(category = "layout")
5448 protected int endMargin = DEFAULT_RELATIVE;
5449
5450 /**
5451 * The default start and end margin.
5452 */
5453 static private final int DEFAULT_RELATIVE = Integer.MIN_VALUE;
5454
5455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 * Creates a new set of layout parameters. The values are extracted from
5457 * the supplied attributes set and context.
5458 *
5459 * @param c the application environment
5460 * @param attrs the set of attributes from which to extract the layout
5461 * parameters' values
5462 */
5463 public MarginLayoutParams(Context c, AttributeSet attrs) {
5464 super();
5465
5466 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
5467 setBaseAttributes(a,
5468 R.styleable.ViewGroup_MarginLayout_layout_width,
5469 R.styleable.ViewGroup_MarginLayout_layout_height);
5470
5471 int margin = a.getDimensionPixelSize(
5472 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
5473 if (margin >= 0) {
5474 leftMargin = margin;
5475 topMargin = margin;
5476 rightMargin= margin;
5477 bottomMargin = margin;
5478 } else {
5479 leftMargin = a.getDimensionPixelSize(
5480 R.styleable.ViewGroup_MarginLayout_layout_marginLeft, 0);
5481 topMargin = a.getDimensionPixelSize(
5482 R.styleable.ViewGroup_MarginLayout_layout_marginTop, 0);
5483 rightMargin = a.getDimensionPixelSize(
5484 R.styleable.ViewGroup_MarginLayout_layout_marginRight, 0);
5485 bottomMargin = a.getDimensionPixelSize(
5486 R.styleable.ViewGroup_MarginLayout_layout_marginBottom, 0);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005487 startMargin = a.getDimensionPixelSize(
5488 R.styleable.ViewGroup_MarginLayout_layout_marginStart, DEFAULT_RELATIVE);
5489 endMargin = a.getDimensionPixelSize(
5490 R.styleable.ViewGroup_MarginLayout_layout_marginEnd, DEFAULT_RELATIVE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 }
5492
5493 a.recycle();
5494 }
5495
5496 /**
5497 * {@inheritDoc}
5498 */
5499 public MarginLayoutParams(int width, int height) {
5500 super(width, height);
5501 }
5502
5503 /**
5504 * Copy constructor. Clones the width, height and margin values of the source.
5505 *
5506 * @param source The layout params to copy from.
5507 */
5508 public MarginLayoutParams(MarginLayoutParams source) {
5509 this.width = source.width;
5510 this.height = source.height;
5511
5512 this.leftMargin = source.leftMargin;
5513 this.topMargin = source.topMargin;
5514 this.rightMargin = source.rightMargin;
5515 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005516 this.startMargin = source.startMargin;
5517 this.endMargin = source.endMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 }
5519
5520 /**
5521 * {@inheritDoc}
5522 */
5523 public MarginLayoutParams(LayoutParams source) {
5524 super(source);
5525 }
5526
5527 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005528 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
5529 * to be done so that the new margins are taken into account. Left and right margins may be
5530 * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 *
5532 * @param left the left margin size
5533 * @param top the top margin size
5534 * @param right the right margin size
5535 * @param bottom the bottom margin size
5536 *
5537 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
5538 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
5539 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
5540 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
5541 */
5542 public void setMargins(int left, int top, int right, int bottom) {
5543 leftMargin = left;
5544 topMargin = top;
5545 rightMargin = right;
5546 bottomMargin = bottom;
5547 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005548
5549 /**
5550 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
5551 * needs to be done so that the new relative margins are taken into account. Left and right
5552 * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
5553 * direction.
5554 *
5555 * @param start the start margin size
5556 * @param top the top margin size
5557 * @param end the right margin size
5558 * @param bottom the bottom margin size
5559 *
5560 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
5561 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
5562 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
5563 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
5564 *
5565 * @hide
5566 */
5567 public void setMarginsRelative(int start, int top, int end, int bottom) {
5568 startMargin = start;
5569 topMargin = top;
5570 endMargin = end;
5571 bottomMargin = bottom;
5572 }
5573
5574 /**
5575 * Returns the start margin in pixels.
5576 *
5577 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
5578 *
5579 * @return the start margin in pixels.
5580 *
5581 * @hide
5582 */
5583 public int getMarginStart() {
5584 return startMargin;
5585 }
5586
5587 /**
5588 * Returns the end margin in pixels.
5589 *
5590 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
5591 *
5592 * @return the end margin in pixels.
5593 *
5594 * @hide
5595 */
5596 public int getMarginEnd() {
5597 return endMargin;
5598 }
5599
5600 /**
5601 * Check if margins are relative.
5602 *
5603 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
5604 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
5605 *
5606 * @return true if either marginStart or marginEnd has been set
5607 *
5608 * @hide
5609 */
5610 public boolean isMarginRelative() {
5611 return (startMargin != DEFAULT_RELATIVE) || (endMargin != DEFAULT_RELATIVE);
5612 }
5613
5614 /**
5615 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
5616 * maybe overriden depending on layout direction.
5617 *
5618 * @hide
5619 */
5620 @Override
5621 protected void resolveWithDirection(int layoutDirection) {
5622 switch(layoutDirection) {
5623 case View.LAYOUT_DIRECTION_RTL:
5624 leftMargin = (endMargin > DEFAULT_RELATIVE) ? endMargin : leftMargin;
5625 rightMargin = (startMargin > DEFAULT_RELATIVE) ? startMargin : rightMargin;
5626 break;
5627 case View.LAYOUT_DIRECTION_LTR:
5628 default:
5629 leftMargin = (startMargin > DEFAULT_RELATIVE) ? startMargin : leftMargin;
5630 rightMargin = (endMargin > DEFAULT_RELATIVE) ? endMargin : rightMargin;
5631 break;
5632 }
5633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 }
Adam Powell2b342f02010-08-18 18:14:13 -07005635
Jeff Brown20e987b2010-08-23 12:01:02 -07005636 /* Describes a touched view and the ids of the pointers that it has captured.
5637 *
5638 * This code assumes that pointer ids are always in the range 0..31 such that
5639 * it can use a bitfield to track which pointer ids are present.
5640 * As it happens, the lower layers of the input dispatch pipeline also use the
5641 * same trick so the assumption should be safe here...
5642 */
5643 private static final class TouchTarget {
5644 private static final int MAX_RECYCLED = 32;
5645 private static final Object sRecycleLock = new Object();
5646 private static TouchTarget sRecycleBin;
5647 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07005648
Jeff Brown20e987b2010-08-23 12:01:02 -07005649 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07005650
Jeff Brown20e987b2010-08-23 12:01:02 -07005651 // The touched child view.
5652 public View child;
5653
5654 // The combined bit mask of pointer ids for all pointers captured by the target.
5655 public int pointerIdBits;
5656
5657 // The next target in the target list.
5658 public TouchTarget next;
5659
5660 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07005661 }
5662
Jeff Brown20e987b2010-08-23 12:01:02 -07005663 public static TouchTarget obtain(View child, int pointerIdBits) {
5664 final TouchTarget target;
5665 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07005666 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07005667 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07005668 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07005669 target = sRecycleBin;
5670 sRecycleBin = target.next;
5671 sRecycledCount--;
5672 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07005673 }
Adam Powell816c3be2010-08-23 18:00:05 -07005674 }
Jeff Brown20e987b2010-08-23 12:01:02 -07005675 target.child = child;
5676 target.pointerIdBits = pointerIdBits;
5677 return target;
5678 }
Adam Powell816c3be2010-08-23 18:00:05 -07005679
Jeff Brown20e987b2010-08-23 12:01:02 -07005680 public void recycle() {
5681 synchronized (sRecycleLock) {
5682 if (sRecycledCount < MAX_RECYCLED) {
5683 next = sRecycleBin;
5684 sRecycleBin = this;
5685 sRecycledCount += 1;
Patrick Dubroyfb0547d2010-10-19 17:36:18 -07005686 } else {
5687 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07005688 }
Patrick Dubroyfb0547d2010-10-19 17:36:18 -07005689 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07005690 }
5691 }
Adam Powell2b342f02010-08-18 18:14:13 -07005692 }
Jeff Brown87b7f802011-06-21 18:35:45 -07005693
5694 /* Describes a hovered view. */
5695 private static final class HoverTarget {
5696 private static final int MAX_RECYCLED = 32;
5697 private static final Object sRecycleLock = new Object();
5698 private static HoverTarget sRecycleBin;
5699 private static int sRecycledCount;
5700
5701 // The hovered child view.
5702 public View child;
5703
5704 // The next target in the target list.
5705 public HoverTarget next;
5706
5707 private HoverTarget() {
5708 }
5709
5710 public static HoverTarget obtain(View child) {
5711 final HoverTarget target;
5712 synchronized (sRecycleLock) {
5713 if (sRecycleBin == null) {
5714 target = new HoverTarget();
5715 } else {
5716 target = sRecycleBin;
5717 sRecycleBin = target.next;
5718 sRecycledCount--;
5719 target.next = null;
5720 }
5721 }
5722 target.child = child;
5723 return target;
5724 }
5725
5726 public void recycle() {
5727 synchronized (sRecycleLock) {
5728 if (sRecycledCount < MAX_RECYCLED) {
5729 next = sRecycleBin;
5730 sRecycleBin = this;
5731 sRecycledCount += 1;
5732 } else {
5733 next = null;
5734 }
5735 child = null;
5736 }
5737 }
5738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739}