blob: 5b3a091d05458d97c670a6658abf9e8277b02c5f [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 com.android.internal.R;
21
22import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080023import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.res.TypedArray;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
Adam Powell6e346362010-07-23 10:18:23 -070027import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.Paint;
Christopher Tatea53146c2010-09-07 11:57:52 -070029import android.graphics.PointF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.graphics.RectF;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Region;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Parcelable;
34import android.os.SystemClock;
35import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Log;
37import android.util.SparseArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070038import android.view.accessibility.AccessibilityEvent;
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;
43
44import java.util.ArrayList;
45
46/**
47 * <p>
48 * A <code>ViewGroup</code> is a special view that can contain other views
49 * (called children.) The view group is the base class for layouts and views
50 * containers. This class also defines the
51 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
52 * class for layouts parameters.
53 * </p>
54 *
55 * <p>
56 * Also see {@link LayoutParams} for layout attributes.
57 * </p>
Romain Guyd6a463a2009-05-21 23:10:10 -070058 *
59 * @attr ref android.R.styleable#ViewGroup_clipChildren
60 * @attr ref android.R.styleable#ViewGroup_clipToPadding
61 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
62 * @attr ref android.R.styleable#ViewGroup_animationCache
63 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
64 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
65 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
66 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
Chet Haase13cc1202010-09-03 15:39:20 -070067 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 */
69public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Chet Haase21cd1382010-09-01 17:42:29 -070070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 private static final boolean DBG = false;
72
73 /**
74 * Views which have been hidden or removed which need to be animated on
75 * their way out.
76 * This field should be made private, so it is hidden from the SDK.
77 * {@hide}
78 */
79 protected ArrayList<View> mDisappearingChildren;
80
81 /**
82 * Listener used to propagate events indicating when children are added
83 * and/or removed from a view group.
84 * This field should be made private, so it is hidden from the SDK.
85 * {@hide}
86 */
87 protected OnHierarchyChangeListener mOnHierarchyChangeListener;
88
89 // The view contained within this ViewGroup that has or contains focus.
90 private View mFocused;
91
Chet Haase48460322010-06-11 14:22:25 -070092 /**
93 * A Transformation used when drawing children, to
94 * apply on the child being drawn.
95 */
96 private final Transformation mChildTransformation = new Transformation();
97
98 /**
99 * Used to track the current invalidation region.
100 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private RectF mInvalidateRegion;
102
Chet Haase48460322010-06-11 14:22:25 -0700103 /**
104 * A Transformation used to calculate a correct
105 * invalidation area when the application is autoscaled.
106 */
107 private Transformation mInvalidationTransformation;
108
Christopher Tatea53146c2010-09-07 11:57:52 -0700109 // View currently under an ongoing drag
110 private View mCurrentDragView;
111
112 // Does this group have a child that can accept the current drag payload?
113 private boolean mChildAcceptsDrag;
114
115 // Used during drag dispatch
116 private final PointF mLocalPoint = new PointF();
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 // Layout animation
119 private LayoutAnimationController mLayoutAnimationController;
120 private Animation.AnimationListener mAnimationListener;
121
Jeff Brown20e987b2010-08-23 12:01:02 -0700122 // First touch target in the linked list of touch targets.
123 private TouchTarget mFirstTouchTarget;
124
125 // Temporary arrays for splitting pointers.
126 private int[] mTmpPointerIndexMap;
127 private int[] mTmpPointerIds;
128 private MotionEvent.PointerCoords[] mTmpPointerCoords;
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 /**
131 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * This field should be made private, so it is hidden from the SDK.
134 * {@hide}
135 */
136 protected int mGroupFlags;
137
138 // When set, ViewGroup invalidates only the child's rectangle
139 // Set by default
140 private static final int FLAG_CLIP_CHILDREN = 0x1;
141
142 // When set, ViewGroup excludes the padding area from the invalidate rectangle
143 // Set by default
144 private static final int FLAG_CLIP_TO_PADDING = 0x2;
145
146 // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
147 // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
148 private static final int FLAG_INVALIDATE_REQUIRED = 0x4;
149
150 // When set, dispatchDraw() will run the layout animation and unset the flag
151 private static final int FLAG_RUN_ANIMATION = 0x8;
152
153 // When set, there is either no layout animation on the ViewGroup or the layout
154 // animation is over
155 // Set by default
156 private static final int FLAG_ANIMATION_DONE = 0x10;
157
158 // If set, this ViewGroup has padding; if unset there is no padding and we don't need
159 // to clip it, even if FLAG_CLIP_TO_PADDING is set
160 private static final int FLAG_PADDING_NOT_NULL = 0x20;
161
162 // When set, this ViewGroup caches its children in a Bitmap before starting a layout animation
163 // Set by default
164 private static final int FLAG_ANIMATION_CACHE = 0x40;
165
166 // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
167 // layout animation; this avoid clobbering the hierarchy
168 // Automatically set when the layout animation starts, depending on the animation's
169 // characteristics
170 private static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
171
172 // When set, the next call to drawChild() will clear mChildTransformation's matrix
173 private static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
174
175 // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
176 // the children's Bitmap caches if necessary
177 // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
178 private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
179
180 /**
181 * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
182 * to get the index of the child to draw for that iteration.
Romain Guy293451e2009-11-04 13:59:48 -0800183 *
184 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 */
186 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /**
189 * When set, this ViewGroup supports static transformations on children; this causes
190 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
191 * invoked when a child is drawn.
192 *
193 * Any subclass overriding
194 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
195 * set this flags in {@link #mGroupFlags}.
Romain Guy8506ab42009-06-11 17:35:47 -0700196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * {@hide}
198 */
199 protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
200
201 // When the previous drawChild() invocation used an alpha value that was lower than
202 // 1.0 and set it in mCachePaint
203 private static final int FLAG_ALPHA_LOWER_THAN_ONE = 0x1000;
204
205 /**
206 * When set, this ViewGroup's drawable states also include those
207 * of its children.
208 */
209 private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
210
211 /**
212 * When set, this ViewGroup tries to always draw its children using their drawing cache.
213 */
214 private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
215
216 /**
217 * When set, and if FLAG_ALWAYS_DRAWN_WITH_CACHE is not set, this ViewGroup will try to
218 * draw its children with their drawing cache.
219 */
220 private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
221
222 /**
223 * When set, this group will go through its list of children to notify them of
224 * any drawable state change.
225 */
226 private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
227
228 private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
229
230 /**
231 * This view will get focus before any of its descendants.
232 */
233 public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
234
235 /**
236 * This view will get focus only if none of its descendants want it.
237 */
238 public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
239
240 /**
241 * This view will block any of its descendants from getting focus, even
242 * if they are focusable.
243 */
244 public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
245
246 /**
247 * Used to map between enum in attrubutes and flag values.
248 */
249 private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
250 {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
251 FOCUS_BLOCK_DESCENDANTS};
252
253 /**
254 * When set, this ViewGroup should not intercept touch events.
Adam Powell110486f2010-06-22 17:14:44 -0700255 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 */
Adam Powell110486f2010-06-22 17:14:44 -0700257 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700260 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
261 */
Adam Powellf37df072010-09-17 16:22:49 -0700262 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700263
264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 * Indicates which types of drawing caches are to be kept in memory.
266 * This field should be made private, so it is hidden from the SDK.
267 * {@hide}
268 */
269 protected int mPersistentDrawingCache;
270
271 /**
272 * Used to indicate that no drawing cache should be kept in memory.
273 */
274 public static final int PERSISTENT_NO_CACHE = 0x0;
275
276 /**
277 * Used to indicate that the animation drawing cache should be kept in memory.
278 */
279 public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
280
281 /**
282 * Used to indicate that the scrolling drawing cache should be kept in memory.
283 */
284 public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
285
286 /**
287 * Used to indicate that all drawing caches should be kept in memory.
288 */
289 public static final int PERSISTENT_ALL_CACHES = 0x3;
290
291 /**
292 * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
293 * are set at the same time.
294 */
295 protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
296
297 // Index of the child's left position in the mLocation array
298 private static final int CHILD_LEFT_INDEX = 0;
299 // Index of the child's top position in the mLocation array
300 private static final int CHILD_TOP_INDEX = 1;
301
302 // Child views of this ViewGroup
303 private View[] mChildren;
304 // Number of valid children in the mChildren array, the rest should be null or not
305 // considered as children
306 private int mChildrenCount;
307
308 private static final int ARRAY_INITIAL_CAPACITY = 12;
309 private static final int ARRAY_CAPACITY_INCREMENT = 12;
310
311 // Used to draw cached views
312 private final Paint mCachePaint = new Paint();
313
Chet Haase21cd1382010-09-01 17:42:29 -0700314 // Used to animate add/remove changes in layout
315 private LayoutTransition mTransition;
316
317 // The set of views that are currently being transitioned. This list is used to track views
318 // being removed that should not actually be removed from the parent yet because they are
319 // being animated.
320 private ArrayList<View> mTransitioningViews;
321
Chet Haase5e25c2c2010-09-16 11:15:56 -0700322 // List of children changing visibility. This is used to potentially keep rendering
323 // views during a transition when they otherwise would have become gone/invisible
324 private ArrayList<View> mVisibilityChangingChildren;
325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 public ViewGroup(Context context) {
327 super(context);
328 initViewGroup();
329 }
330
331 public ViewGroup(Context context, AttributeSet attrs) {
332 super(context, attrs);
333 initViewGroup();
334 initFromAttributes(context, attrs);
335 }
336
337 public ViewGroup(Context context, AttributeSet attrs, int defStyle) {
338 super(context, attrs, defStyle);
339 initViewGroup();
340 initFromAttributes(context, attrs);
341 }
342
343 private void initViewGroup() {
344 // ViewGroup doesn't draw by default
345 setFlags(WILL_NOT_DRAW, DRAW_MASK);
346 mGroupFlags |= FLAG_CLIP_CHILDREN;
347 mGroupFlags |= FLAG_CLIP_TO_PADDING;
348 mGroupFlags |= FLAG_ANIMATION_DONE;
349 mGroupFlags |= FLAG_ANIMATION_CACHE;
350 mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
351
352 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
353
354 mChildren = new View[ARRAY_INITIAL_CAPACITY];
355 mChildrenCount = 0;
356
357 mCachePaint.setDither(false);
358
359 mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
360 }
361
362 private void initFromAttributes(Context context, AttributeSet attrs) {
363 TypedArray a = context.obtainStyledAttributes(attrs,
364 R.styleable.ViewGroup);
365
366 final int N = a.getIndexCount();
367 for (int i = 0; i < N; i++) {
368 int attr = a.getIndex(i);
369 switch (attr) {
370 case R.styleable.ViewGroup_clipChildren:
371 setClipChildren(a.getBoolean(attr, true));
372 break;
373 case R.styleable.ViewGroup_clipToPadding:
374 setClipToPadding(a.getBoolean(attr, true));
375 break;
376 case R.styleable.ViewGroup_animationCache:
377 setAnimationCacheEnabled(a.getBoolean(attr, true));
378 break;
379 case R.styleable.ViewGroup_persistentDrawingCache:
380 setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
381 break;
382 case R.styleable.ViewGroup_addStatesFromChildren:
383 setAddStatesFromChildren(a.getBoolean(attr, false));
384 break;
385 case R.styleable.ViewGroup_alwaysDrawnWithCache:
386 setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
387 break;
388 case R.styleable.ViewGroup_layoutAnimation:
389 int id = a.getResourceId(attr, -1);
390 if (id > 0) {
391 setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
392 }
393 break;
394 case R.styleable.ViewGroup_descendantFocusability:
395 setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
396 break;
Adam Powell2b342f02010-08-18 18:14:13 -0700397 case R.styleable.ViewGroup_splitMotionEvents:
398 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
399 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700400 case R.styleable.ViewGroup_animateLayoutChanges:
401 boolean animateLayoutChanges = a.getBoolean(attr, false);
402 if (animateLayoutChanges) {
403 setLayoutTransition(new LayoutTransition());
404 }
405 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407 }
408
409 a.recycle();
410 }
411
412 /**
413 * Gets the descendant focusability of this view group. The descendant
414 * focusability defines the relationship between this view group and its
415 * descendants when looking for a view to take focus in
416 * {@link #requestFocus(int, android.graphics.Rect)}.
417 *
418 * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
419 * {@link #FOCUS_BLOCK_DESCENDANTS}.
420 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700421 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
423 @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
424 @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
425 })
426 public int getDescendantFocusability() {
427 return mGroupFlags & FLAG_MASK_FOCUSABILITY;
428 }
429
430 /**
431 * Set the descendant focusability of this view group. This defines the relationship
432 * between this view group and its descendants when looking for a view to
433 * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
434 *
435 * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
436 * {@link #FOCUS_BLOCK_DESCENDANTS}.
437 */
438 public void setDescendantFocusability(int focusability) {
439 switch (focusability) {
440 case FOCUS_BEFORE_DESCENDANTS:
441 case FOCUS_AFTER_DESCENDANTS:
442 case FOCUS_BLOCK_DESCENDANTS:
443 break;
444 default:
445 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
446 + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
447 }
448 mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
449 mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
450 }
451
452 /**
453 * {@inheritDoc}
454 */
455 @Override
456 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
457 if (mFocused != null) {
458 mFocused.unFocus();
459 mFocused = null;
460 }
461 super.handleFocusGainInternal(direction, previouslyFocusedRect);
462 }
463
464 /**
465 * {@inheritDoc}
466 */
467 public void requestChildFocus(View child, View focused) {
468 if (DBG) {
469 System.out.println(this + " requestChildFocus()");
470 }
471 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
472 return;
473 }
474
475 // Unfocus us, if necessary
476 super.unFocus();
477
478 // We had a previous notion of who had focus. Clear it.
479 if (mFocused != child) {
480 if (mFocused != null) {
481 mFocused.unFocus();
482 }
483
484 mFocused = child;
485 }
486 if (mParent != null) {
487 mParent.requestChildFocus(this, focused);
488 }
489 }
490
491 /**
492 * {@inheritDoc}
493 */
494 public void focusableViewAvailable(View v) {
495 if (mParent != null
496 // shortcut: don't report a new focusable view if we block our descendants from
497 // getting focus
498 && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS)
499 // shortcut: don't report a new focusable view if we already are focused
500 // (and we don't prefer our descendants)
501 //
502 // note: knowing that mFocused is non-null is not a good enough reason
503 // to break the traversal since in that case we'd actually have to find
504 // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and
505 // an ancestor of v; this will get checked for at ViewRoot
506 && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) {
507 mParent.focusableViewAvailable(v);
508 }
509 }
510
511 /**
512 * {@inheritDoc}
513 */
514 public boolean showContextMenuForChild(View originalView) {
515 return mParent != null && mParent.showContextMenuForChild(originalView);
516 }
517
518 /**
Adam Powell6e346362010-07-23 10:18:23 -0700519 * {@inheritDoc}
520 */
521 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
522 return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null;
523 }
524
525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * Find the nearest view in the specified direction that wants to take
527 * focus.
528 *
529 * @param focused The view that currently has focus
530 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
531 * FOCUS_RIGHT, or 0 for not applicable.
532 */
533 public View focusSearch(View focused, int direction) {
534 if (isRootNamespace()) {
535 // root namespace means we should consider ourselves the top of the
536 // tree for focus searching; otherwise we could be focus searching
537 // into other tabs. see LocalActivityManager and TabHost for more info
538 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
539 } else if (mParent != null) {
540 return mParent.focusSearch(focused, direction);
541 }
542 return null;
543 }
544
545 /**
546 * {@inheritDoc}
547 */
548 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
549 return false;
550 }
551
552 /**
553 * {@inheritDoc}
554 */
555 @Override
556 public boolean dispatchUnhandledMove(View focused, int direction) {
557 return mFocused != null &&
558 mFocused.dispatchUnhandledMove(focused, direction);
559 }
560
561 /**
562 * {@inheritDoc}
563 */
564 public void clearChildFocus(View child) {
565 if (DBG) {
566 System.out.println(this + " clearChildFocus()");
567 }
568
569 mFocused = null;
570 if (mParent != null) {
571 mParent.clearChildFocus(this);
572 }
573 }
574
575 /**
576 * {@inheritDoc}
577 */
578 @Override
579 public void clearFocus() {
580 super.clearFocus();
581
582 // clear any child focus if it exists
583 if (mFocused != null) {
584 mFocused.clearFocus();
585 }
586 }
587
588 /**
589 * {@inheritDoc}
590 */
591 @Override
592 void unFocus() {
593 if (DBG) {
594 System.out.println(this + " unFocus()");
595 }
596
597 super.unFocus();
598 if (mFocused != null) {
599 mFocused.unFocus();
600 }
601 mFocused = null;
602 }
603
604 /**
605 * Returns the focused child of this view, if any. The child may have focus
606 * or contain focus.
607 *
608 * @return the focused child or null.
609 */
610 public View getFocusedChild() {
611 return mFocused;
612 }
613
614 /**
615 * Returns true if this view has or contains focus
616 *
617 * @return true if this view has or contains focus
618 */
619 @Override
620 public boolean hasFocus() {
621 return (mPrivateFlags & FOCUSED) != 0 || mFocused != null;
622 }
623
624 /*
625 * (non-Javadoc)
626 *
627 * @see android.view.View#findFocus()
628 */
629 @Override
630 public View findFocus() {
631 if (DBG) {
632 System.out.println("Find focus in " + this + ": flags="
633 + isFocused() + ", child=" + mFocused);
634 }
635
636 if (isFocused()) {
637 return this;
638 }
639
640 if (mFocused != null) {
641 return mFocused.findFocus();
642 }
643 return null;
644 }
645
646 /**
647 * {@inheritDoc}
648 */
649 @Override
650 public boolean hasFocusable() {
651 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
652 return false;
653 }
654
655 if (isFocusable()) {
656 return true;
657 }
658
659 final int descendantFocusability = getDescendantFocusability();
660 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
661 final int count = mChildrenCount;
662 final View[] children = mChildren;
663
664 for (int i = 0; i < count; i++) {
665 final View child = children[i];
666 if (child.hasFocusable()) {
667 return true;
668 }
669 }
670 }
671
672 return false;
673 }
674
675 /**
676 * {@inheritDoc}
677 */
678 @Override
679 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700680 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
681 }
682
683 /**
684 * {@inheritDoc}
685 */
686 @Override
687 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 final int focusableCount = views.size();
689
690 final int descendantFocusability = getDescendantFocusability();
691
692 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
693 final int count = mChildrenCount;
694 final View[] children = mChildren;
695
696 for (int i = 0; i < count; i++) {
697 final View child = children[i];
698 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700699 child.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701 }
702 }
703
704 // we add ourselves (if focusable) in all cases except for when we are
705 // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is
706 // to avoid the focus search finding layouts when a more precise search
707 // among the focusable children would be more interesting.
708 if (
709 descendantFocusability != FOCUS_AFTER_DESCENDANTS ||
710 // No focusable descendants
711 (focusableCount == views.size())) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700712 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714 }
715
716 /**
717 * {@inheritDoc}
718 */
719 @Override
720 public void dispatchWindowFocusChanged(boolean hasFocus) {
721 super.dispatchWindowFocusChanged(hasFocus);
722 final int count = mChildrenCount;
723 final View[] children = mChildren;
724 for (int i = 0; i < count; i++) {
725 children[i].dispatchWindowFocusChanged(hasFocus);
726 }
727 }
728
729 /**
730 * {@inheritDoc}
731 */
732 @Override
733 public void addTouchables(ArrayList<View> views) {
734 super.addTouchables(views);
735
736 final int count = mChildrenCount;
737 final View[] children = mChildren;
738
739 for (int i = 0; i < count; i++) {
740 final View child = children[i];
741 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
742 child.addTouchables(views);
743 }
744 }
745 }
Romain Guy43c9cdf2010-01-27 13:53:55 -0800746
747 /**
748 * {@inheritDoc}
749 */
750 @Override
751 public void dispatchDisplayHint(int hint) {
752 super.dispatchDisplayHint(hint);
753 final int count = mChildrenCount;
754 final View[] children = mChildren;
755 for (int i = 0; i < count; i++) {
756 children[i].dispatchDisplayHint(hint);
757 }
758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759
760 /**
Chet Haase5e25c2c2010-09-16 11:15:56 -0700761 * @hide
762 * @param child
763 * @param visibility
764 */
765 void onChildVisibilityChanged(View child, int visibility) {
766 if (mTransition != null) {
767 if (visibility == VISIBLE) {
768 mTransition.showChild(this, child);
769 } else {
770 mTransition.hideChild(this, child);
771 }
772 if (visibility != VISIBLE) {
773 // Only track this on disappearing views - appearing views are already visible
774 // and don't need special handling during drawChild()
775 if (mVisibilityChangingChildren == null) {
776 mVisibilityChangingChildren = new ArrayList<View>();
777 }
778 mVisibilityChangingChildren.add(child);
779 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
780 addDisappearingView(child);
781 }
782 }
783 }
784 }
785
786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 * {@inheritDoc}
788 */
789 @Override
Adam Powell326d8082009-12-09 15:10:07 -0800790 protected void dispatchVisibilityChanged(View changedView, int visibility) {
791 super.dispatchVisibilityChanged(changedView, visibility);
792 final int count = mChildrenCount;
793 final View[] children = mChildren;
794 for (int i = 0; i < count; i++) {
795 children[i].dispatchVisibilityChanged(changedView, visibility);
796 }
797 }
798
799 /**
800 * {@inheritDoc}
801 */
802 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 public void dispatchWindowVisibilityChanged(int visibility) {
804 super.dispatchWindowVisibilityChanged(visibility);
805 final int count = mChildrenCount;
806 final View[] children = mChildren;
807 for (int i = 0; i < count; i++) {
808 children[i].dispatchWindowVisibilityChanged(visibility);
809 }
810 }
811
812 /**
813 * {@inheritDoc}
814 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800815 @Override
816 public void dispatchConfigurationChanged(Configuration newConfig) {
817 super.dispatchConfigurationChanged(newConfig);
818 final int count = mChildrenCount;
819 final View[] children = mChildren;
820 for (int i = 0; i < count; i++) {
821 children[i].dispatchConfigurationChanged(newConfig);
822 }
823 }
824
825 /**
826 * {@inheritDoc}
827 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void recomputeViewAttributes(View child) {
829 ViewParent parent = mParent;
830 if (parent != null) parent.recomputeViewAttributes(this);
831 }
Romain Guy8506ab42009-06-11 17:35:47 -0700832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 @Override
834 void dispatchCollectViewAttributes(int visibility) {
835 visibility |= mViewFlags&VISIBILITY_MASK;
836 super.dispatchCollectViewAttributes(visibility);
837 final int count = mChildrenCount;
838 final View[] children = mChildren;
839 for (int i = 0; i < count; i++) {
840 children[i].dispatchCollectViewAttributes(visibility);
841 }
842 }
843
844 /**
845 * {@inheritDoc}
846 */
847 public void bringChildToFront(View child) {
848 int index = indexOfChild(child);
849 if (index >= 0) {
850 removeFromArray(index);
851 addInArray(child, mChildrenCount);
852 child.mParent = this;
853 }
854 }
855
856 /**
857 * {@inheritDoc}
Christopher Tatea53146c2010-09-07 11:57:52 -0700858 *
859 * !!! TODO: write real docs
860 */
861 @Override
862 public boolean dispatchDragEvent(DragEvent event) {
863 boolean retval = false;
864 final float tx = event.mX;
865 final float ty = event.mY;
866
Christopher Tate2c095f32010-10-04 14:13:40 -0700867 ViewRoot root = getViewRoot();
Christopher Tatea53146c2010-09-07 11:57:52 -0700868
869 // Dispatch down the view hierarchy
870 switch (event.mAction) {
871 case DragEvent.ACTION_DRAG_STARTED: {
872 // clear state to recalculate which views we drag over
873 root.setDragFocus(event, null);
874
875 // Now dispatch down to our children, caching the responses
876 mChildAcceptsDrag = false;
877 final int count = mChildrenCount;
878 final View[] children = mChildren;
879 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -0700880 final View child = children[i];
881 if (child.getVisibility() == VISIBLE) {
882 final boolean handled = children[i].dispatchDragEvent(event);
883 children[i].mCanAcceptDrop = handled;
884 if (handled) {
885 mChildAcceptsDrag = true;
886 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700887 }
888 }
889
890 // Return HANDLED if one of our children can accept the drag
891 if (mChildAcceptsDrag) {
892 retval = true;
893 }
894 } break;
895
896 case DragEvent.ACTION_DRAG_ENDED: {
897 // Notify all of our children that the drag is over
898 final int count = mChildrenCount;
899 final View[] children = mChildren;
900 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -0700901 final View child = children[i];
902 if (child.getVisibility() == VISIBLE) {
903 child.dispatchDragEvent(event);
904 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700905 }
906 // We consider drag-ended to have been handled if one of our children
907 // had offered to handle the drag.
908 if (mChildAcceptsDrag) {
909 retval = true;
910 }
911 } break;
912
913 case DragEvent.ACTION_DRAG_LOCATION: {
914 // Find the [possibly new] drag target
915 final View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
916
917 // If we've changed apparent drag target, tell the view root which view
918 // we're over now. This will in turn send out DRAG_ENTERED / DRAG_EXITED
919 // notifications as appropriate.
920 if (mCurrentDragView != target) {
921 root.setDragFocus(event, target);
922 mCurrentDragView = target;
923 }
Christopher Tate2c095f32010-10-04 14:13:40 -0700924
Christopher Tatea53146c2010-09-07 11:57:52 -0700925 // Dispatch the actual drag location notice, localized into its coordinates
926 if (target != null) {
927 event.mX = mLocalPoint.x;
928 event.mY = mLocalPoint.y;
929
930 retval = target.dispatchDragEvent(event);
931
932 event.mX = tx;
933 event.mY = ty;
934 }
935 } break;
936
937 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -0700938 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Christopher Tatea53146c2010-09-07 11:57:52 -0700939 View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
940 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -0700941 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Christopher Tatea53146c2010-09-07 11:57:52 -0700942 event.mX = mLocalPoint.x;
943 event.mY = mLocalPoint.y;
944 retval = target.dispatchDragEvent(event);
945 event.mX = tx;
946 event.mY = ty;
Christopher Tate5ada6cb2010-10-05 14:15:29 -0700947 } else {
948 if (ViewDebug.DEBUG_DRAG) {
949 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
950 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700951 }
952 } break;
953 }
954
955 // If none of our children could handle the event, try here
956 if (!retval) {
957 retval = onDragEvent(event);
958 }
959 return retval;
960 }
961
962 // Find the frontmost child view that lies under the given point, and calculate
963 // the position within its own local coordinate system.
964 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -0700965 final int count = mChildrenCount;
966 final View[] children = mChildren;
967 for (int i = count - 1; i >= 0; i--) {
968 final View child = children[i];
Romain Guy469b1db2010-10-05 11:49:57 -0700969 if (!child.mCanAcceptDrop) {
Christopher Tatea53146c2010-09-07 11:57:52 -0700970 continue;
971 }
972
Christopher Tate2c095f32010-10-04 14:13:40 -0700973 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -0700974 return child;
975 }
976 }
977 return null;
978 }
979
980 /**
981 * {@inheritDoc}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 */
983 @Override
984 public boolean dispatchKeyEventPreIme(KeyEvent event) {
985 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
986 return super.dispatchKeyEventPreIme(event);
987 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
988 return mFocused.dispatchKeyEventPreIme(event);
989 }
990 return false;
991 }
992
993 /**
994 * {@inheritDoc}
995 */
996 @Override
997 public boolean dispatchKeyEvent(KeyEvent event) {
998 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
999 return super.dispatchKeyEvent(event);
1000 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1001 return mFocused.dispatchKeyEvent(event);
1002 }
1003 return false;
1004 }
1005
1006 /**
1007 * {@inheritDoc}
1008 */
1009 @Override
1010 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
1011 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1012 return super.dispatchKeyShortcutEvent(event);
1013 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1014 return mFocused.dispatchKeyShortcutEvent(event);
1015 }
1016 return false;
1017 }
1018
1019 /**
1020 * {@inheritDoc}
1021 */
1022 @Override
1023 public boolean dispatchTrackballEvent(MotionEvent event) {
1024 if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {
1025 return super.dispatchTrackballEvent(event);
1026 } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
1027 return mFocused.dispatchTrackballEvent(event);
1028 }
1029 return false;
1030 }
1031
1032 /**
1033 * {@inheritDoc}
1034 */
1035 @Override
1036 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown85a31762010-09-01 17:01:00 -07001037 if (!onFilterTouchEventForSecurity(ev)) {
1038 return false;
1039 }
1040
Jeff Brown20e987b2010-08-23 12:01:02 -07001041 final int action = ev.getAction();
1042 final int actionMasked = action & MotionEvent.ACTION_MASK;
1043
1044 // Handle an initial down.
1045 if (actionMasked == MotionEvent.ACTION_DOWN) {
1046 // Throw away all previous state when starting a new touch gesture.
1047 // The framework may have dropped the up or cancel event for the previous gesture
1048 // due to an app switch, ANR, or some other state change.
1049 cancelAndClearTouchTargets(ev);
1050 resetTouchState();
Adam Powell167bc822010-09-13 18:11:08 -07001051 }
Adam Powell2b342f02010-08-18 18:14:13 -07001052
Jeff Brown20e987b2010-08-23 12:01:02 -07001053 // Check for interception.
1054 final boolean intercepted;
1055 if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) {
1056 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
1057 if (!disallowIntercept) {
1058 intercepted = onInterceptTouchEvent(ev);
1059 ev.setAction(action); // restore action in case onInterceptTouchEvent() changed it
1060 } else {
1061 intercepted = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001063 } else {
1064 intercepted = true;
1065 }
Adam Powellb08013c2010-09-16 16:28:11 -07001066
Jeff Brown20e987b2010-08-23 12:01:02 -07001067 // Check for cancelation.
1068 final boolean canceled = resetCancelNextUpFlag(this)
1069 || actionMasked == MotionEvent.ACTION_CANCEL;
1070
1071 // Update list of touch targets for pointer down, if needed.
1072 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
1073 TouchTarget newTouchTarget = null;
1074 boolean alreadyDispatchedToNewTouchTarget = false;
1075 if (!canceled && !intercepted) {
1076 if (actionMasked == MotionEvent.ACTION_DOWN
1077 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)) {
1078 final int actionIndex = ev.getActionIndex(); // always 0 for down
1079 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
1080 : TouchTarget.ALL_POINTER_IDS;
1081
1082 // Clean up earlier touch targets for this pointer id in case they
1083 // have become out of sync.
1084 removePointersFromTouchTargets(idBitsToAssign);
1085
1086 final int childrenCount = mChildrenCount;
1087 if (childrenCount != 0) {
1088 // Find a child that can receive the event. Scan children from front to back.
1089 final View[] children = mChildren;
1090 final float x = ev.getX(actionIndex);
1091 final float y = ev.getY(actionIndex);
1092
1093 for (int i = childrenCount - 1; i >= 0; i--) {
1094 final View child = children[i];
1095 if ((child.mViewFlags & VISIBILITY_MASK) != VISIBLE
1096 && child.getAnimation() == null) {
1097 // Skip invisible child unless it is animating.
1098 continue;
1099 }
1100
Christopher Tate2c095f32010-10-04 14:13:40 -07001101 if (!isTransformedTouchPointInView(x, y, child, null)) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001102 // New pointer is out of child's bounds.
1103 continue;
1104 }
1105
1106 newTouchTarget = getTouchTarget(child);
1107 if (newTouchTarget != null) {
1108 // Child is already receiving touch within its bounds.
1109 // Give it the new pointer in addition to the ones it is handling.
1110 newTouchTarget.pointerIdBits |= idBitsToAssign;
1111 break;
1112 }
1113
1114 resetCancelNextUpFlag(child);
1115 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
1116 // Child wants to receive touch within its bounds.
1117 newTouchTarget = addTouchTarget(child, idBitsToAssign);
1118 alreadyDispatchedToNewTouchTarget = true;
1119 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
1122 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001123
1124 if (newTouchTarget == null && mFirstTouchTarget != null) {
1125 // Did not find a child to receive the event.
1126 // Assign the pointer to the least recently added target.
1127 newTouchTarget = mFirstTouchTarget;
1128 while (newTouchTarget.next != null) {
1129 newTouchTarget = newTouchTarget.next;
1130 }
1131 newTouchTarget.pointerIdBits |= idBitsToAssign;
1132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 }
Romain Guy8506ab42009-06-11 17:35:47 -07001135
Jeff Brown20e987b2010-08-23 12:01:02 -07001136 // Dispatch to touch targets.
1137 boolean handled = false;
1138 if (mFirstTouchTarget == null) {
1139 // No touch targets so treat this as an ordinary view.
1140 handled = dispatchTransformedTouchEvent(ev, canceled, null,
1141 TouchTarget.ALL_POINTER_IDS);
Adam Cohen9b073942010-08-19 16:49:52 -07001142 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07001143 // Dispatch to touch targets, excluding the new touch target if we already
1144 // dispatched to it. Cancel touch targets if necessary.
1145 TouchTarget predecessor = null;
1146 TouchTarget target = mFirstTouchTarget;
1147 while (target != null) {
1148 final TouchTarget next = target.next;
1149 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
1150 handled = true;
1151 } else {
1152 final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted;
1153 if (dispatchTransformedTouchEvent(ev, cancelChild,
1154 target.child, target.pointerIdBits)) {
1155 handled = true;
1156 }
1157 if (cancelChild) {
1158 if (predecessor == null) {
1159 mFirstTouchTarget = next;
1160 } else {
1161 predecessor.next = next;
1162 }
1163 target.recycle();
1164 target = next;
1165 continue;
1166 }
1167 }
1168 predecessor = target;
1169 target = next;
1170 }
1171 }
1172
1173 // Update list of touch targets for pointer up or cancel, if needed.
1174 if (canceled || actionMasked == MotionEvent.ACTION_UP) {
1175 resetTouchState();
1176 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
1177 final int actionIndex = ev.getActionIndex();
1178 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
1179 removePointersFromTouchTargets(idBitsToRemove);
1180 }
1181
1182 return handled;
1183 }
1184
Romain Guy469b1db2010-10-05 11:49:57 -07001185 /**
1186 * Resets all touch state in preparation for a new cycle.
1187 */
1188 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07001189 clearTouchTargets();
1190 resetCancelNextUpFlag(this);
1191 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
1192 }
1193
Romain Guy469b1db2010-10-05 11:49:57 -07001194 /**
1195 * Resets the cancel next up flag.
1196 * Returns true if the flag was previously set.
1197 */
1198 private boolean resetCancelNextUpFlag(View view) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001199 if ((view.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) {
1200 view.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
1201 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07001202 }
1203 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205
Romain Guy469b1db2010-10-05 11:49:57 -07001206 /**
1207 * Clears all touch targets.
1208 */
1209 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07001210 TouchTarget target = mFirstTouchTarget;
1211 if (target != null) {
1212 do {
1213 TouchTarget next = target.next;
1214 target.recycle();
1215 target = next;
1216 } while (target != null);
1217 mFirstTouchTarget = null;
1218 }
1219 }
1220
Romain Guy469b1db2010-10-05 11:49:57 -07001221 /**
1222 * Cancels and clears all touch targets.
1223 */
1224 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001225 if (mFirstTouchTarget != null) {
1226 boolean syntheticEvent = false;
1227 if (event == null) {
1228 final long now = SystemClock.uptimeMillis();
1229 event = MotionEvent.obtain(now, now,
1230 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
1231 syntheticEvent = true;
1232 }
1233
1234 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
1235 resetCancelNextUpFlag(target.child);
1236 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
1237 }
1238 clearTouchTargets();
1239
1240 if (syntheticEvent) {
1241 event.recycle();
1242 }
1243 }
1244 }
1245
Romain Guy469b1db2010-10-05 11:49:57 -07001246 /**
1247 * Gets the touch target for specified child view.
1248 * Returns null if not found.
1249 */
1250 private TouchTarget getTouchTarget(View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001251 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
1252 if (target.child == child) {
1253 return target;
1254 }
1255 }
1256 return null;
1257 }
1258
Romain Guy469b1db2010-10-05 11:49:57 -07001259 /**
1260 * Adds a touch target for specified child to the beginning of the list.
1261 * Assumes the target child is not already present.
1262 */
1263 private TouchTarget addTouchTarget(View child, int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001264 TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
1265 target.next = mFirstTouchTarget;
1266 mFirstTouchTarget = target;
1267 return target;
1268 }
1269
Romain Guy469b1db2010-10-05 11:49:57 -07001270 /**
1271 * Removes the pointer ids from consideration.
1272 */
1273 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001274 TouchTarget predecessor = null;
1275 TouchTarget target = mFirstTouchTarget;
1276 while (target != null) {
1277 final TouchTarget next = target.next;
1278 if ((target.pointerIdBits & pointerIdBits) != 0) {
1279 target.pointerIdBits &= ~pointerIdBits;
1280 if (target.pointerIdBits == 0) {
1281 if (predecessor == null) {
1282 mFirstTouchTarget = next;
1283 } else {
1284 predecessor.next = next;
1285 }
1286 target.recycle();
1287 target = next;
1288 continue;
1289 }
1290 }
1291 predecessor = target;
1292 target = next;
1293 }
1294 }
1295
Romain Guy469b1db2010-10-05 11:49:57 -07001296 /**
1297 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07001298 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07001299 * Child must not be null.
1300 */
1301 private boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07001302 PointF outLocalPoint) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001303 float localX = x + mScrollX - child.mLeft;
1304 float localY = y + mScrollY - child.mTop;
1305 if (! child.hasIdentityMatrix() && mAttachInfo != null) {
Adam Powell2b342f02010-08-18 18:14:13 -07001306 final float[] localXY = mAttachInfo.mTmpTransformLocation;
1307 localXY[0] = localX;
1308 localXY[1] = localY;
1309 child.getInverseMatrix().mapPoints(localXY);
1310 localX = localXY[0];
1311 localY = localXY[1];
1312 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001313 final boolean isInView = child.pointInView(localX, localY);
1314 if (isInView && outLocalPoint != null) {
1315 outLocalPoint.set(localX, localY);
1316 }
1317 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07001318 }
1319
Romain Guy469b1db2010-10-05 11:49:57 -07001320 /**
1321 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07001322 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07001323 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
1324 */
1325 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07001326 View child, int desiredPointerIdBits) {
1327 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07001328
Jeff Brown20e987b2010-08-23 12:01:02 -07001329 // Canceling motions is a special case. We don't need to perform any transformations
1330 // or filtering. The important part is the action, not the contents.
1331 final int oldAction = event.getAction();
1332 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
1333 event.setAction(MotionEvent.ACTION_CANCEL);
1334 if (child == null) {
1335 handled = super.dispatchTouchEvent(event);
1336 } else {
1337 handled = child.dispatchTouchEvent(event);
1338 }
1339 event.setAction(oldAction);
1340 return handled;
1341 }
Adam Powell2b342f02010-08-18 18:14:13 -07001342
Jeff Brown20e987b2010-08-23 12:01:02 -07001343 // Calculate the number of pointers to deliver.
1344 final int oldPointerCount = event.getPointerCount();
1345 int newPointerCount = 0;
1346 if (desiredPointerIdBits == TouchTarget.ALL_POINTER_IDS) {
1347 newPointerCount = oldPointerCount;
1348 } else {
1349 for (int i = 0; i < oldPointerCount; i++) {
1350 final int pointerId = event.getPointerId(i);
1351 final int pointerIdBit = 1 << pointerId;
1352 if ((pointerIdBit & desiredPointerIdBits) != 0) {
1353 newPointerCount += 1;
1354 }
1355 }
1356 }
Adam Powell2b342f02010-08-18 18:14:13 -07001357
Jeff Brown20e987b2010-08-23 12:01:02 -07001358 // If for some reason we ended up in an inconsistent state where it looks like we
1359 // might produce a motion event with no pointers in it, then drop the event.
1360 if (newPointerCount == 0) {
1361 return false;
1362 }
Adam Powell2b342f02010-08-18 18:14:13 -07001363
Jeff Brown20e987b2010-08-23 12:01:02 -07001364 // If the number of pointers is the same and we don't need to perform any fancy
1365 // irreversible transformations, then we can reuse the motion event for this
1366 // dispatch as long as we are careful to revert any changes we make.
1367 final boolean reuse = newPointerCount == oldPointerCount
1368 && (child == null || child.hasIdentityMatrix());
1369 if (reuse) {
1370 if (child == null) {
1371 handled = super.dispatchTouchEvent(event);
1372 } else {
1373 final float offsetX = mScrollX - child.mLeft;
1374 final float offsetY = mScrollY - child.mTop;
1375 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07001376
Jeff Brown20e987b2010-08-23 12:01:02 -07001377 handled = child.dispatchTouchEvent(event);
1378
1379 event.offsetLocation(-offsetX, -offsetY);
1380 }
1381 return handled;
1382 }
1383
1384 // Make a copy of the event.
1385 // If the number of pointers is different, then we need to filter out irrelevant pointers
1386 // as we make a copy of the motion event.
1387 MotionEvent transformedEvent;
1388 if (newPointerCount == oldPointerCount) {
1389 transformedEvent = MotionEvent.obtain(event);
1390 } else {
1391 growTmpPointerArrays(newPointerCount);
1392 final int[] newPointerIndexMap = mTmpPointerIndexMap;
1393 final int[] newPointerIds = mTmpPointerIds;
1394 final MotionEvent.PointerCoords[] newPointerCoords = mTmpPointerCoords;
1395
1396 int newPointerIndex = 0;
1397 int oldPointerIndex = 0;
1398 while (newPointerIndex < newPointerCount) {
1399 final int pointerId = event.getPointerId(oldPointerIndex);
1400 final int pointerIdBits = 1 << pointerId;
1401 if ((pointerIdBits & desiredPointerIdBits) != 0) {
1402 newPointerIndexMap[newPointerIndex] = oldPointerIndex;
1403 newPointerIds[newPointerIndex] = pointerId;
1404 if (newPointerCoords[newPointerIndex] == null) {
1405 newPointerCoords[newPointerIndex] = new MotionEvent.PointerCoords();
1406 }
1407
1408 newPointerIndex += 1;
1409 }
1410 oldPointerIndex += 1;
1411 }
1412
1413 final int newAction;
1414 if (cancel) {
1415 newAction = MotionEvent.ACTION_CANCEL;
1416 } else {
1417 final int oldMaskedAction = oldAction & MotionEvent.ACTION_MASK;
1418 if (oldMaskedAction == MotionEvent.ACTION_POINTER_DOWN
1419 || oldMaskedAction == MotionEvent.ACTION_POINTER_UP) {
1420 final int changedPointerId = event.getPointerId(
1421 (oldAction & MotionEvent.ACTION_POINTER_INDEX_MASK)
1422 >> MotionEvent.ACTION_POINTER_INDEX_SHIFT);
1423 final int changedPointerIdBits = 1 << changedPointerId;
1424 if ((changedPointerIdBits & desiredPointerIdBits) != 0) {
1425 if (newPointerCount == 1) {
1426 // The first/last pointer went down/up.
1427 newAction = oldMaskedAction == MotionEvent.ACTION_POINTER_DOWN
1428 ? MotionEvent.ACTION_DOWN : MotionEvent.ACTION_UP;
1429 } else {
1430 // A secondary pointer went down/up.
1431 int newChangedPointerIndex = 0;
1432 while (newPointerIds[newChangedPointerIndex] != changedPointerId) {
1433 newChangedPointerIndex += 1;
Adam Powell2b342f02010-08-18 18:14:13 -07001434 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001435 newAction = oldMaskedAction | (newChangedPointerIndex
1436 << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
Adam Powell2b342f02010-08-18 18:14:13 -07001437 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001438 } else {
1439 // An unrelated pointer changed.
1440 newAction = MotionEvent.ACTION_MOVE;
1441 }
1442 } else {
1443 // Simple up/down/cancel/move motion action.
1444 newAction = oldMaskedAction;
1445 }
1446 }
1447
1448 transformedEvent = null;
1449 final int historySize = event.getHistorySize();
1450 for (int historyIndex = 0; historyIndex <= historySize; historyIndex++) {
1451 for (newPointerIndex = 0; newPointerIndex < newPointerCount; newPointerIndex++) {
1452 final MotionEvent.PointerCoords c = newPointerCoords[newPointerIndex];
1453 oldPointerIndex = newPointerIndexMap[newPointerIndex];
1454 if (historyIndex != historySize) {
1455 event.getHistoricalPointerCoords(oldPointerIndex, historyIndex, c);
1456 } else {
1457 event.getPointerCoords(oldPointerIndex, c);
Adam Powell2b342f02010-08-18 18:14:13 -07001458 }
1459 }
1460
Jeff Brown20e987b2010-08-23 12:01:02 -07001461 final long eventTime;
1462 if (historyIndex != historySize) {
1463 eventTime = event.getHistoricalEventTime(historyIndex);
1464 } else {
1465 eventTime = event.getEventTime();
1466 }
1467
1468 if (transformedEvent == null) {
1469 transformedEvent = MotionEvent.obtain(
1470 event.getDownTime(), eventTime, newAction,
1471 newPointerCount, newPointerIds, newPointerCoords,
1472 event.getMetaState(), event.getXPrecision(), event.getYPrecision(),
1473 event.getDeviceId(), event.getEdgeFlags(), event.getSource(),
1474 event.getFlags());
1475 } else {
1476 transformedEvent.addBatch(eventTime, newPointerCoords, 0);
Adam Powell2b342f02010-08-18 18:14:13 -07001477 }
1478 }
1479 }
1480
Jeff Brown20e987b2010-08-23 12:01:02 -07001481 // Perform any necessary transformations and dispatch.
1482 if (child == null) {
1483 handled = super.dispatchTouchEvent(transformedEvent);
1484 } else {
1485 final float offsetX = mScrollX - child.mLeft;
1486 final float offsetY = mScrollY - child.mTop;
1487 transformedEvent.offsetLocation(offsetX, offsetY);
1488 if (! child.hasIdentityMatrix()) {
1489 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07001490 }
1491
Jeff Brown20e987b2010-08-23 12:01:02 -07001492 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07001493 }
1494
Jeff Brown20e987b2010-08-23 12:01:02 -07001495 // Done.
1496 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07001497 return handled;
1498 }
1499
Romain Guy469b1db2010-10-05 11:49:57 -07001500 /**
1501 * Enlarge the temporary pointer arrays for splitting pointers.
1502 * May discard contents (but keeps PointerCoords objects to avoid reallocating them).
1503 */
1504 private void growTmpPointerArrays(int desiredCapacity) {
Jeff Brown20e987b2010-08-23 12:01:02 -07001505 final MotionEvent.PointerCoords[] oldTmpPointerCoords = mTmpPointerCoords;
1506 int capacity;
1507 if (oldTmpPointerCoords != null) {
1508 capacity = oldTmpPointerCoords.length;
1509 if (desiredCapacity <= capacity) {
1510 return;
1511 }
1512 } else {
1513 capacity = 4;
1514 }
1515
1516 while (capacity < desiredCapacity) {
1517 capacity *= 2;
1518 }
1519
1520 mTmpPointerIndexMap = new int[capacity];
1521 mTmpPointerIds = new int[capacity];
1522 mTmpPointerCoords = new MotionEvent.PointerCoords[capacity];
1523
1524 if (oldTmpPointerCoords != null) {
1525 System.arraycopy(oldTmpPointerCoords, 0, mTmpPointerCoords, 0,
1526 oldTmpPointerCoords.length);
1527 }
1528 }
1529
Adam Powell2b342f02010-08-18 18:14:13 -07001530 /**
1531 * Enable or disable the splitting of MotionEvents to multiple children during touch event
1532 * dispatch. This behavior is disabled by default.
1533 *
1534 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
1535 * views depending on where each pointer initially went down. This allows for user interactions
1536 * such as scrolling two panes of content independently, chording of buttons, and performing
1537 * independent gestures on different pieces of content.
1538 *
1539 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
1540 * child views. <code>false</code> to only allow one child view to be the target of
1541 * any MotionEvent received by this ViewGroup.
1542 */
1543 public void setMotionEventSplittingEnabled(boolean split) {
1544 // TODO Applications really shouldn't change this setting mid-touch event,
1545 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
1546 // with gestures in progress when this is changed.
1547 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07001548 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
1549 } else {
1550 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07001551 }
1552 }
1553
1554 /**
1555 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
1556 */
1557 public boolean isMotionEventSplittingEnabled() {
1558 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
1559 }
1560
1561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * {@inheritDoc}
1563 */
1564 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07001565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
1567 // We're already in this state, assume our ancestors are too
1568 return;
1569 }
Romain Guy8506ab42009-06-11 17:35:47 -07001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 if (disallowIntercept) {
1572 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
1573 } else {
1574 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
1575 }
Romain Guy8506ab42009-06-11 17:35:47 -07001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 // Pass it up to our parent
1578 if (mParent != null) {
1579 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
1580 }
1581 }
1582
1583 /**
1584 * Implement this method to intercept all touch screen motion events. This
1585 * allows you to watch events as they are dispatched to your children, and
1586 * take ownership of the current gesture at any point.
1587 *
1588 * <p>Using this function takes some care, as it has a fairly complicated
1589 * interaction with {@link View#onTouchEvent(MotionEvent)
1590 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
1591 * that method as well as this one in the correct way. Events will be
1592 * received in the following order:
1593 *
1594 * <ol>
1595 * <li> You will receive the down event here.
1596 * <li> The down event will be handled either by a child of this view
1597 * group, or given to your own onTouchEvent() method to handle; this means
1598 * you should implement onTouchEvent() to return true, so you will
1599 * continue to see the rest of the gesture (instead of looking for
1600 * a parent view to handle it). Also, by returning true from
1601 * onTouchEvent(), you will not receive any following
1602 * events in onInterceptTouchEvent() and all touch processing must
1603 * happen in onTouchEvent() like normal.
1604 * <li> For as long as you return false from this function, each following
1605 * event (up to and including the final up) will be delivered first here
1606 * and then to the target's onTouchEvent().
1607 * <li> If you return true from here, you will not receive any
1608 * following events: the target view will receive the same event but
1609 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
1610 * events will be delivered to your onTouchEvent() method and no longer
1611 * appear here.
1612 * </ol>
1613 *
1614 * @param ev The motion event being dispatched down the hierarchy.
1615 * @return Return true to steal motion events from the children and have
1616 * them dispatched to this ViewGroup through onTouchEvent().
1617 * The current target will receive an ACTION_CANCEL event, and no further
1618 * messages will be delivered here.
1619 */
1620 public boolean onInterceptTouchEvent(MotionEvent ev) {
1621 return false;
1622 }
1623
1624 /**
1625 * {@inheritDoc}
1626 *
1627 * Looks for a view to give focus to respecting the setting specified by
1628 * {@link #getDescendantFocusability()}.
1629 *
1630 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
1631 * find focus within the children of this group when appropriate.
1632 *
1633 * @see #FOCUS_BEFORE_DESCENDANTS
1634 * @see #FOCUS_AFTER_DESCENDANTS
1635 * @see #FOCUS_BLOCK_DESCENDANTS
1636 * @see #onRequestFocusInDescendants
1637 */
1638 @Override
1639 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
1640 if (DBG) {
1641 System.out.println(this + " ViewGroup.requestFocus direction="
1642 + direction);
1643 }
1644 int descendantFocusability = getDescendantFocusability();
1645
1646 switch (descendantFocusability) {
1647 case FOCUS_BLOCK_DESCENDANTS:
1648 return super.requestFocus(direction, previouslyFocusedRect);
1649 case FOCUS_BEFORE_DESCENDANTS: {
1650 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
1651 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
1652 }
1653 case FOCUS_AFTER_DESCENDANTS: {
1654 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
1655 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
1656 }
1657 default:
1658 throw new IllegalStateException("descendant focusability must be "
1659 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
1660 + "but is " + descendantFocusability);
1661 }
1662 }
1663
1664 /**
1665 * Look for a descendant to call {@link View#requestFocus} on.
1666 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
1667 * when it wants to request focus within its children. Override this to
1668 * customize how your {@link ViewGroup} requests focus within its children.
1669 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
1670 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
1671 * to give a finer grained hint about where focus is coming from. May be null
1672 * if there is no hint.
1673 * @return Whether focus was taken.
1674 */
1675 @SuppressWarnings({"ConstantConditions"})
1676 protected boolean onRequestFocusInDescendants(int direction,
1677 Rect previouslyFocusedRect) {
1678 int index;
1679 int increment;
1680 int end;
1681 int count = mChildrenCount;
1682 if ((direction & FOCUS_FORWARD) != 0) {
1683 index = 0;
1684 increment = 1;
1685 end = count;
1686 } else {
1687 index = count - 1;
1688 increment = -1;
1689 end = -1;
1690 }
1691 final View[] children = mChildren;
1692 for (int i = index; i != end; i += increment) {
1693 View child = children[i];
1694 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1695 if (child.requestFocus(direction, previouslyFocusedRect)) {
1696 return true;
1697 }
1698 }
1699 }
1700 return false;
1701 }
Romain Guya440b002010-02-24 15:57:54 -08001702
1703 /**
1704 * {@inheritDoc}
Romain Guydcc490f2010-02-24 17:59:35 -08001705 *
1706 * @hide
Romain Guya440b002010-02-24 15:57:54 -08001707 */
1708 @Override
1709 public void dispatchStartTemporaryDetach() {
1710 super.dispatchStartTemporaryDetach();
1711 final int count = mChildrenCount;
1712 final View[] children = mChildren;
1713 for (int i = 0; i < count; i++) {
1714 children[i].dispatchStartTemporaryDetach();
1715 }
1716 }
1717
1718 /**
1719 * {@inheritDoc}
Romain Guydcc490f2010-02-24 17:59:35 -08001720 *
1721 * @hide
Romain Guya440b002010-02-24 15:57:54 -08001722 */
1723 @Override
1724 public void dispatchFinishTemporaryDetach() {
1725 super.dispatchFinishTemporaryDetach();
1726 final int count = mChildrenCount;
1727 final View[] children = mChildren;
1728 for (int i = 0; i < count; i++) {
1729 children[i].dispatchFinishTemporaryDetach();
1730 }
1731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732
1733 /**
1734 * {@inheritDoc}
1735 */
1736 @Override
1737 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
1738 super.dispatchAttachedToWindow(info, visibility);
1739 visibility |= mViewFlags & VISIBILITY_MASK;
1740 final int count = mChildrenCount;
1741 final View[] children = mChildren;
1742 for (int i = 0; i < count; i++) {
1743 children[i].dispatchAttachedToWindow(info, visibility);
1744 }
1745 }
1746
svetoslavganov75986cf2009-05-14 22:28:01 -07001747 @Override
1748 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1749 boolean populated = false;
1750 for (int i = 0, count = getChildCount(); i < count; i++) {
1751 populated |= getChildAt(i).dispatchPopulateAccessibilityEvent(event);
1752 }
1753 return populated;
1754 }
1755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 /**
1757 * {@inheritDoc}
1758 */
1759 @Override
1760 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07001761 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 // dispatching motion events to a child; we need to get rid of that
1763 // child to avoid dispatching events to it after the window is torn
1764 // down. To make sure we keep the child in a consistent state, we
1765 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07001766 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767
1768 final int count = mChildrenCount;
1769 final View[] children = mChildren;
1770 for (int i = 0; i < count; i++) {
1771 children[i].dispatchDetachedFromWindow();
1772 }
1773 super.dispatchDetachedFromWindow();
1774 }
1775
1776 /**
1777 * {@inheritDoc}
1778 */
1779 @Override
1780 public void setPadding(int left, int top, int right, int bottom) {
1781 super.setPadding(left, top, right, bottom);
1782
1783 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingRight) != 0) {
1784 mGroupFlags |= FLAG_PADDING_NOT_NULL;
1785 } else {
1786 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
1787 }
1788 }
1789
1790 /**
1791 * {@inheritDoc}
1792 */
1793 @Override
1794 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
1795 super.dispatchSaveInstanceState(container);
1796 final int count = mChildrenCount;
1797 final View[] children = mChildren;
1798 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001799 View c = children[i];
1800 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
1801 c.dispatchSaveInstanceState(container);
1802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804 }
1805
1806 /**
1807 * Perform dispatching of a {@link #saveHierarchyState freeze()} to only this view,
1808 * not to its children. For use when overriding
1809 * {@link #dispatchSaveInstanceState dispatchFreeze()} to allow subclasses to freeze
1810 * their own state but not the state of their children.
1811 *
1812 * @param container the container
1813 */
1814 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
1815 super.dispatchSaveInstanceState(container);
1816 }
1817
1818 /**
1819 * {@inheritDoc}
1820 */
1821 @Override
1822 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
1823 super.dispatchRestoreInstanceState(container);
1824 final int count = mChildrenCount;
1825 final View[] children = mChildren;
1826 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001827 View c = children[i];
1828 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
1829 c.dispatchRestoreInstanceState(container);
1830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
1832 }
1833
1834 /**
1835 * Perform dispatching of a {@link #restoreHierarchyState thaw()} to only this view,
1836 * not to its children. For use when overriding
1837 * {@link #dispatchRestoreInstanceState dispatchThaw()} to allow subclasses to thaw
1838 * their own state but not the state of their children.
1839 *
1840 * @param container the container
1841 */
1842 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
1843 super.dispatchRestoreInstanceState(container);
1844 }
1845
1846 /**
1847 * Enables or disables the drawing cache for each child of this view group.
1848 *
1849 * @param enabled true to enable the cache, false to dispose of it
1850 */
1851 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
1852 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
1853 final View[] children = mChildren;
1854 final int count = mChildrenCount;
1855 for (int i = 0; i < count; i++) {
1856 children[i].setDrawingCacheEnabled(enabled);
1857 }
1858 }
1859 }
1860
1861 @Override
1862 protected void onAnimationStart() {
1863 super.onAnimationStart();
1864
1865 // When this ViewGroup's animation starts, build the cache for the children
1866 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
1867 final int count = mChildrenCount;
1868 final View[] children = mChildren;
1869
1870 for (int i = 0; i < count; i++) {
1871 final View child = children[i];
1872 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1873 child.setDrawingCacheEnabled(true);
Romain Guyfbd8f692009-06-26 14:51:58 -07001874 child.buildDrawingCache(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876 }
1877
1878 mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
1879 }
1880 }
1881
1882 @Override
1883 protected void onAnimationEnd() {
1884 super.onAnimationEnd();
1885
1886 // When this ViewGroup's animation ends, destroy the cache of the children
1887 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
1888 mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
1889
1890 if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
1891 setChildrenDrawingCacheEnabled(false);
1892 }
1893 }
1894 }
1895
Romain Guy223ff5c2010-03-02 17:07:47 -08001896 @Override
1897 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07001898 int count = mChildrenCount;
1899 int[] visibilities = null;
1900
Romain Guy223ff5c2010-03-02 17:07:47 -08001901 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07001902 visibilities = new int[count];
1903 for (int i = 0; i < count; i++) {
1904 View child = getChildAt(i);
1905 visibilities[i] = child.getVisibility();
1906 if (visibilities[i] == View.VISIBLE) {
1907 child.setVisibility(INVISIBLE);
1908 }
1909 }
Romain Guy223ff5c2010-03-02 17:07:47 -08001910 }
1911
1912 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07001913
1914 if (skipChildren) {
1915 for (int i = 0; i < count; i++) {
1916 getChildAt(i).setVisibility(visibilities[i]);
1917 }
1918 }
Romain Guy223ff5c2010-03-02 17:07:47 -08001919
1920 return b;
1921 }
1922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 /**
1924 * {@inheritDoc}
1925 */
1926 @Override
1927 protected void dispatchDraw(Canvas canvas) {
1928 final int count = mChildrenCount;
1929 final View[] children = mChildren;
1930 int flags = mGroupFlags;
1931
1932 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
1933 final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
1934
1935 for (int i = 0; i < count; i++) {
1936 final View child = children[i];
1937 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1938 final LayoutParams params = child.getLayoutParams();
1939 attachLayoutAnimationParameters(child, params, i, count);
1940 bindLayoutAnimation(child);
1941 if (cache) {
1942 child.setDrawingCacheEnabled(true);
Romain Guyfbd8f692009-06-26 14:51:58 -07001943 child.buildDrawingCache(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 }
1945 }
1946 }
1947
1948 final LayoutAnimationController controller = mLayoutAnimationController;
1949 if (controller.willOverlap()) {
1950 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
1951 }
1952
1953 controller.start();
1954
1955 mGroupFlags &= ~FLAG_RUN_ANIMATION;
1956 mGroupFlags &= ~FLAG_ANIMATION_DONE;
1957
1958 if (cache) {
1959 mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
1960 }
1961
1962 if (mAnimationListener != null) {
1963 mAnimationListener.onAnimationStart(controller.getAnimation());
1964 }
1965 }
1966
1967 int saveCount = 0;
1968 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
1969 if (clipToPadding) {
1970 saveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07001971 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
1972 mScrollX + mRight - mLeft - mPaddingRight,
1973 mScrollY + mBottom - mTop - mPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974
1975 }
1976
1977 // We will draw our child's animation, let's reset the flag
1978 mPrivateFlags &= ~DRAW_ANIMATION;
1979 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
1980
1981 boolean more = false;
1982 final long drawingTime = getDrawingTime();
1983
1984 if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
1985 for (int i = 0; i < count; i++) {
1986 final View child = children[i];
1987 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
1988 more |= drawChild(canvas, child, drawingTime);
1989 }
1990 }
1991 } else {
1992 for (int i = 0; i < count; i++) {
1993 final View child = children[getChildDrawingOrder(count, i)];
1994 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
1995 more |= drawChild(canvas, child, drawingTime);
1996 }
1997 }
1998 }
1999
2000 // Draw any disappearing views that have animations
2001 if (mDisappearingChildren != null) {
2002 final ArrayList<View> disappearingChildren = mDisappearingChildren;
2003 final int disappearingCount = disappearingChildren.size() - 1;
2004 // Go backwards -- we may delete as animations finish
2005 for (int i = disappearingCount; i >= 0; i--) {
2006 final View child = disappearingChildren.get(i);
2007 more |= drawChild(canvas, child, drawingTime);
2008 }
2009 }
2010
2011 if (clipToPadding) {
2012 canvas.restoreToCount(saveCount);
2013 }
2014
2015 // mGroupFlags might have been updated by drawChild()
2016 flags = mGroupFlags;
2017
2018 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
2019 invalidate();
2020 }
2021
2022 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
2023 mLayoutAnimationController.isDone() && !more) {
2024 // We want to erase the drawing cache and notify the listener after the
2025 // next frame is drawn because one extra invalidate() is caused by
2026 // drawChild() after the animation is over
2027 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
2028 final Runnable end = new Runnable() {
2029 public void run() {
2030 notifyAnimationListener();
2031 }
2032 };
2033 post(end);
2034 }
2035 }
Romain Guy8506ab42009-06-11 17:35:47 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 /**
2038 * Returns the index of the child to draw for this iteration. Override this
2039 * if you want to change the drawing order of children. By default, it
2040 * returns i.
2041 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08002042 * NOTE: In order for this method to be called, you must enable child ordering
2043 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07002044 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 * @param i The current iteration.
2046 * @return The index of the child to draw this iteration.
Romain Guy293451e2009-11-04 13:59:48 -08002047 *
2048 * @see #setChildrenDrawingOrderEnabled(boolean)
2049 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 */
2051 protected int getChildDrawingOrder(int childCount, int i) {
2052 return i;
2053 }
Romain Guy8506ab42009-06-11 17:35:47 -07002054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 private void notifyAnimationListener() {
2056 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
2057 mGroupFlags |= FLAG_ANIMATION_DONE;
2058
2059 if (mAnimationListener != null) {
2060 final Runnable end = new Runnable() {
2061 public void run() {
2062 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
2063 }
2064 };
2065 post(end);
2066 }
2067
2068 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2069 mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
2070 if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
2071 setChildrenDrawingCacheEnabled(false);
2072 }
2073 }
2074
2075 invalidate();
2076 }
2077
2078 /**
2079 * Draw one child of this View Group. This method is responsible for getting
2080 * the canvas in the right state. This includes clipping, translating so
2081 * that the child's scrolled origin is at 0, 0, and applying any animation
2082 * transformations.
2083 *
2084 * @param canvas The canvas on which to draw the child
2085 * @param child Who to draw
2086 * @param drawingTime The time at which draw is occuring
2087 * @return True if an invalidate() was issued
2088 */
2089 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
2090 boolean more = false;
2091
2092 final int cl = child.mLeft;
2093 final int ct = child.mTop;
2094 final int cr = child.mRight;
2095 final int cb = child.mBottom;
2096
2097 final int flags = mGroupFlags;
2098
2099 if ((flags & FLAG_CLEAR_TRANSFORMATION) == FLAG_CLEAR_TRANSFORMATION) {
Chet Haase48460322010-06-11 14:22:25 -07002100 mChildTransformation.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 mGroupFlags &= ~FLAG_CLEAR_TRANSFORMATION;
2102 }
2103
2104 Transformation transformToApply = null;
Chet Haase48460322010-06-11 14:22:25 -07002105 Transformation invalidationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 final Animation a = child.getAnimation();
2107 boolean concatMatrix = false;
2108
Chet Haase48460322010-06-11 14:22:25 -07002109 boolean scalingRequired = false;
2110 boolean caching = false;
Romain Guyb051e892010-09-28 19:09:36 -07002111 if ((flags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE ||
Chet Haase48460322010-06-11 14:22:25 -07002112 (flags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE) {
2113 caching = true;
2114 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
2115 }
2116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 if (a != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 final boolean initialized = a.isInitialized();
2119 if (!initialized) {
Romain Guy8f2d94f2009-03-25 18:04:42 -07002120 a.initialize(cr - cl, cb - ct, getWidth(), getHeight());
2121 a.initializeInvalidateRegion(0, 0, cr - cl, cb - ct);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 child.onAnimationStart();
2123 }
2124
Chet Haase48460322010-06-11 14:22:25 -07002125 more = a.getTransformation(drawingTime, mChildTransformation,
2126 scalingRequired ? mAttachInfo.mApplicationScale : 1f);
2127 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
2128 if (mInvalidationTransformation == null) {
2129 mInvalidationTransformation = new Transformation();
2130 }
2131 invalidationTransform = mInvalidationTransformation;
2132 a.getTransformation(drawingTime, invalidationTransform, 1f);
2133 } else {
2134 invalidationTransform = mChildTransformation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 transformToApply = mChildTransformation;
2137
2138 concatMatrix = a.willChangeTransformationMatrix();
2139
2140 if (more) {
2141 if (!a.willChangeBounds()) {
2142 if ((flags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) ==
2143 FLAG_OPTIMIZE_INVALIDATE) {
2144 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
2145 } else if ((flags & FLAG_INVALIDATE_REQUIRED) == 0) {
2146 // The child need to draw an animation, potentially offscreen, so
2147 // make sure we do not cancel invalidate requests
2148 mPrivateFlags |= DRAW_ANIMATION;
2149 invalidate(cl, ct, cr, cb);
2150 }
2151 } else {
Chet Haase48460322010-06-11 14:22:25 -07002152 if (mInvalidateRegion == null) {
2153 mInvalidateRegion = new RectF();
2154 }
2155 final RectF region = mInvalidateRegion;
2156 a.getInvalidateRegion(0, 0, cr - cl, cb - ct, region, invalidationTransform);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157
2158 // The child need to draw an animation, potentially offscreen, so
2159 // make sure we do not cancel invalidate requests
2160 mPrivateFlags |= DRAW_ANIMATION;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002161
2162 final int left = cl + (int) region.left;
2163 final int top = ct + (int) region.top;
2164 invalidate(left, top, left + (int) region.width(), top + (int) region.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166 }
2167 } else if ((flags & FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
2168 FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 final boolean hasTransform = getChildStaticTransformation(child, mChildTransformation);
2170 if (hasTransform) {
2171 final int transformType = mChildTransformation.getTransformationType();
2172 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
2173 mChildTransformation : null;
2174 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
2175 }
2176 }
2177
Chet Haasedf030d22010-07-30 17:22:38 -07002178 concatMatrix |= !child.hasIdentityMatrix();
2179
Romain Guy5bcdff42009-05-14 21:27:18 -07002180 // Sets the flag as early as possible to allow draw() implementations
Romain Guy986003d2009-03-25 17:42:35 -07002181 // to call invalidate() successfully when doing animations
Romain Guy5bcdff42009-05-14 21:27:18 -07002182 child.mPrivateFlags |= DRAWN;
Romain Guy986003d2009-03-25 17:42:35 -07002183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 if (!concatMatrix && canvas.quickReject(cl, ct, cr, cb, Canvas.EdgeType.BW) &&
2185 (child.mPrivateFlags & DRAW_ANIMATION) == 0) {
2186 return more;
2187 }
2188
2189 child.computeScroll();
2190
2191 final int sx = child.mScrollX;
2192 final int sy = child.mScrollY;
2193
Romain Guyb051e892010-09-28 19:09:36 -07002194 DisplayList displayList = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 Bitmap cache = null;
Chet Haase48460322010-06-11 14:22:25 -07002196 if (caching) {
Romain Guyb051e892010-09-28 19:09:36 -07002197 if (!canvas.isHardwareAccelerated()) {
2198 cache = child.getDrawingCache(true);
2199 } else {
Romain Guy469b1db2010-10-05 11:49:57 -07002200 // TODO: bring back
2201 // displayList = child.getDisplayList();
Romain Guyb051e892010-09-28 19:09:36 -07002202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 }
2204
Romain Guyb051e892010-09-28 19:09:36 -07002205 final boolean hasDisplayList = displayList != null && displayList.isReady();
2206 final boolean hasNoCache = cache == null || hasDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207
2208 final int restoreTo = canvas.save();
2209 if (hasNoCache) {
2210 canvas.translate(cl - sx, ct - sy);
2211 } else {
2212 canvas.translate(cl, ct);
Romain Guy8506ab42009-06-11 17:35:47 -07002213 if (scalingRequired) {
Romain Guycafdea62009-06-12 10:51:36 -07002214 // mAttachInfo cannot be null, otherwise scalingRequired == false
Romain Guy8506ab42009-06-11 17:35:47 -07002215 final float scale = 1.0f / mAttachInfo.mApplicationScale;
2216 canvas.scale(scale, scale);
2217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
2219
Chet Haasec3aa3612010-06-17 08:50:37 -07002220 float alpha = child.getAlpha();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221
Romain Guy33e72ae2010-07-17 12:40:29 -07002222 if (transformToApply != null || alpha < 1.0f || !child.hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07002223 int transX = 0;
2224 int transY = 0;
Romain Guy33e72ae2010-07-17 12:40:29 -07002225
Chet Haasec3aa3612010-06-17 08:50:37 -07002226 if (hasNoCache) {
2227 transX = -sx;
2228 transY = -sy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002230
Chet Haasec3aa3612010-06-17 08:50:37 -07002231 if (transformToApply != null) {
2232 if (concatMatrix) {
2233 // Undo the scroll translation, apply the transformation matrix,
2234 // then redo the scroll translate to get the correct result.
2235 canvas.translate(-transX, -transY);
2236 canvas.concat(transformToApply.getMatrix());
2237 canvas.translate(transX, transY);
2238 mGroupFlags |= FLAG_CLEAR_TRANSFORMATION;
2239 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002240
Chet Haasec3aa3612010-06-17 08:50:37 -07002241 float transformAlpha = transformToApply.getAlpha();
2242 if (transformAlpha < 1.0f) {
2243 alpha *= transformToApply.getAlpha();
2244 mGroupFlags |= FLAG_CLEAR_TRANSFORMATION;
2245 }
2246 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002247
2248 if (!child.hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07002249 canvas.translate(-transX, -transY);
2250 canvas.concat(child.getMatrix());
2251 canvas.translate(transX, transY);
2252 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 if (alpha < 1.0f) {
2255 mGroupFlags |= FLAG_CLEAR_TRANSFORMATION;
Romain Guy33e72ae2010-07-17 12:40:29 -07002256
2257 if (hasNoCache) {
2258 final int multipliedAlpha = (int) (255 * alpha);
2259 if (!child.onSetAlpha(multipliedAlpha)) {
Romain Guyfd880422010-09-23 16:16:04 -07002260 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
2261 if ((flags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
2262 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
2263 }
Romain Guy33e72ae2010-07-17 12:40:29 -07002264 canvas.saveLayerAlpha(sx, sy, sx + cr - cl, sy + cb - ct, multipliedAlpha,
Romain Guyfd880422010-09-23 16:16:04 -07002265 layerFlags);
Romain Guy33e72ae2010-07-17 12:40:29 -07002266 } else {
2267 child.mPrivateFlags |= ALPHA_SET;
2268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 }
2270 }
2271 } else if ((child.mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
2272 child.onSetAlpha(255);
Romain Guy9b34d452010-09-02 11:45:04 -07002273 child.mPrivateFlags &= ~ALPHA_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 }
2275
2276 if ((flags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
2277 if (hasNoCache) {
Romain Guy8f2d94f2009-03-25 18:04:42 -07002278 canvas.clipRect(sx, sy, sx + (cr - cl), sy + (cb - ct));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 } else {
Romain Guy8506ab42009-06-11 17:35:47 -07002280 if (!scalingRequired) {
2281 canvas.clipRect(0, 0, cr - cl, cb - ct);
2282 } else {
2283 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
2284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
2286 }
2287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 if (hasNoCache) {
Romain Guyb051e892010-09-28 19:09:36 -07002289 if (!hasDisplayList) {
2290 // Fast path for layouts with no backgrounds
2291 if ((child.mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
2292 if (ViewDebug.TRACE_HIERARCHY) {
2293 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
2294 }
2295 child.mPrivateFlags &= ~DIRTY_MASK;
2296 child.dispatchDraw(canvas);
2297 } else {
2298 child.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 } else {
Romain Guyb051e892010-09-28 19:09:36 -07002301 ((HardwareCanvas) canvas).drawDisplayList(displayList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 }
Romain Guyb051e892010-09-28 19:09:36 -07002303 } else if (cache != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 final Paint cachePaint = mCachePaint;
2305 if (alpha < 1.0f) {
2306 cachePaint.setAlpha((int) (alpha * 255));
2307 mGroupFlags |= FLAG_ALPHA_LOWER_THAN_ONE;
2308 } else if ((flags & FLAG_ALPHA_LOWER_THAN_ONE) == FLAG_ALPHA_LOWER_THAN_ONE) {
2309 cachePaint.setAlpha(255);
2310 mGroupFlags &= ~FLAG_ALPHA_LOWER_THAN_ONE;
2311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
2313 }
2314
2315 canvas.restoreToCount(restoreTo);
2316
2317 if (a != null && !more) {
2318 child.onSetAlpha(255);
2319 finishAnimatingView(child, a);
2320 }
2321
2322 return more;
2323 }
2324
2325 /**
2326 * By default, children are clipped to their bounds before drawing. This
2327 * allows view groups to override this behavior for animations, etc.
2328 *
2329 * @param clipChildren true to clip children to their bounds,
2330 * false otherwise
2331 * @attr ref android.R.styleable#ViewGroup_clipChildren
2332 */
2333 public void setClipChildren(boolean clipChildren) {
2334 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
2335 }
2336
2337 /**
2338 * By default, children are clipped to the padding of the ViewGroup. This
2339 * allows view groups to override this behavior
2340 *
2341 * @param clipToPadding true to clip children to the padding of the
2342 * group, false otherwise
2343 * @attr ref android.R.styleable#ViewGroup_clipToPadding
2344 */
2345 public void setClipToPadding(boolean clipToPadding) {
2346 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
2347 }
2348
2349 /**
2350 * {@inheritDoc}
2351 */
2352 @Override
2353 public void dispatchSetSelected(boolean selected) {
2354 final View[] children = mChildren;
2355 final int count = mChildrenCount;
2356 for (int i = 0; i < count; i++) {
Chet Haasedf030d22010-07-30 17:22:38 -07002357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 children[i].setSelected(selected);
2359 }
2360 }
Romain Guy8506ab42009-06-11 17:35:47 -07002361
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07002362 /**
2363 * {@inheritDoc}
2364 */
2365 @Override
2366 public void dispatchSetActivated(boolean activated) {
2367 final View[] children = mChildren;
2368 final int count = mChildrenCount;
2369 for (int i = 0; i < count; i++) {
2370
2371 children[i].setActivated(activated);
2372 }
2373 }
2374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 @Override
2376 protected void dispatchSetPressed(boolean pressed) {
2377 final View[] children = mChildren;
2378 final int count = mChildrenCount;
2379 for (int i = 0; i < count; i++) {
2380 children[i].setPressed(pressed);
2381 }
2382 }
2383
2384 /**
2385 * When this property is set to true, this ViewGroup supports static transformations on
2386 * children; this causes
2387 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
2388 * invoked when a child is drawn.
2389 *
2390 * Any subclass overriding
2391 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
2392 * set this property to true.
2393 *
2394 * @param enabled True to enable static transformations on children, false otherwise.
2395 *
2396 * @see #FLAG_SUPPORT_STATIC_TRANSFORMATIONS
2397 */
2398 protected void setStaticTransformationsEnabled(boolean enabled) {
2399 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
2400 }
2401
2402 /**
2403 * {@inheritDoc}
2404 *
Romain Guy8506ab42009-06-11 17:35:47 -07002405 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 */
2407 protected boolean getChildStaticTransformation(View child, Transformation t) {
2408 return false;
2409 }
2410
2411 /**
2412 * {@hide}
2413 */
2414 @Override
2415 protected View findViewTraversal(int id) {
2416 if (id == mID) {
2417 return this;
2418 }
2419
2420 final View[] where = mChildren;
2421 final int len = mChildrenCount;
2422
2423 for (int i = 0; i < len; i++) {
2424 View v = where[i];
2425
2426 if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
2427 v = v.findViewById(id);
2428
2429 if (v != null) {
2430 return v;
2431 }
2432 }
2433 }
2434
2435 return null;
2436 }
2437
2438 /**
2439 * {@hide}
2440 */
2441 @Override
2442 protected View findViewWithTagTraversal(Object tag) {
2443 if (tag != null && tag.equals(mTag)) {
2444 return this;
2445 }
2446
2447 final View[] where = mChildren;
2448 final int len = mChildrenCount;
2449
2450 for (int i = 0; i < len; i++) {
2451 View v = where[i];
2452
2453 if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
2454 v = v.findViewWithTag(tag);
2455
2456 if (v != null) {
2457 return v;
2458 }
2459 }
2460 }
2461
2462 return null;
2463 }
2464
2465 /**
2466 * Adds a child view. If no layout parameters are already set on the child, the
2467 * default parameters for this ViewGroup are set on the child.
2468 *
2469 * @param child the child view to add
2470 *
2471 * @see #generateDefaultLayoutParams()
2472 */
2473 public void addView(View child) {
2474 addView(child, -1);
2475 }
2476
2477 /**
2478 * Adds a child view. If no layout parameters are already set on the child, the
2479 * default parameters for this ViewGroup are set on the child.
2480 *
2481 * @param child the child view to add
2482 * @param index the position at which to add the child
2483 *
2484 * @see #generateDefaultLayoutParams()
2485 */
2486 public void addView(View child, int index) {
2487 LayoutParams params = child.getLayoutParams();
2488 if (params == null) {
2489 params = generateDefaultLayoutParams();
2490 if (params == null) {
2491 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
2492 }
2493 }
2494 addView(child, index, params);
2495 }
2496
2497 /**
2498 * Adds a child view with this ViewGroup's default layout parameters and the
2499 * specified width and height.
2500 *
2501 * @param child the child view to add
2502 */
2503 public void addView(View child, int width, int height) {
2504 final LayoutParams params = generateDefaultLayoutParams();
2505 params.width = width;
2506 params.height = height;
2507 addView(child, -1, params);
2508 }
2509
2510 /**
2511 * Adds a child view with the specified layout parameters.
2512 *
2513 * @param child the child view to add
2514 * @param params the layout parameters to set on the child
2515 */
2516 public void addView(View child, LayoutParams params) {
2517 addView(child, -1, params);
2518 }
2519
2520 /**
2521 * Adds a child view with the specified layout parameters.
2522 *
2523 * @param child the child view to add
2524 * @param index the position at which to add the child
2525 * @param params the layout parameters to set on the child
2526 */
2527 public void addView(View child, int index, LayoutParams params) {
2528 if (DBG) {
2529 System.out.println(this + " addView");
2530 }
2531
2532 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
2533 // therefore, we call requestLayout() on ourselves before, so that the child's request
2534 // will be blocked at our level
2535 requestLayout();
2536 invalidate();
2537 addViewInner(child, index, params, false);
2538 }
2539
2540 /**
2541 * {@inheritDoc}
2542 */
2543 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
2544 if (!checkLayoutParams(params)) {
2545 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
2546 }
2547 if (view.mParent != this) {
2548 throw new IllegalArgumentException("Given view not a child of " + this);
2549 }
2550 view.setLayoutParams(params);
2551 }
2552
2553 /**
2554 * {@inheritDoc}
2555 */
2556 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2557 return p != null;
2558 }
2559
2560 /**
2561 * Interface definition for a callback to be invoked when the hierarchy
2562 * within this view changed. The hierarchy changes whenever a child is added
2563 * to or removed from this view.
2564 */
2565 public interface OnHierarchyChangeListener {
2566 /**
2567 * Called when a new child is added to a parent view.
2568 *
2569 * @param parent the view in which a child was added
2570 * @param child the new child view added in the hierarchy
2571 */
2572 void onChildViewAdded(View parent, View child);
2573
2574 /**
2575 * Called when a child is removed from a parent view.
2576 *
2577 * @param parent the view from which the child was removed
2578 * @param child the child removed from the hierarchy
2579 */
2580 void onChildViewRemoved(View parent, View child);
2581 }
2582
2583 /**
2584 * Register a callback to be invoked when a child is added to or removed
2585 * from this view.
2586 *
2587 * @param listener the callback to invoke on hierarchy change
2588 */
2589 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
2590 mOnHierarchyChangeListener = listener;
2591 }
2592
2593 /**
2594 * Adds a view during layout. This is useful if in your onLayout() method,
2595 * you need to add more views (as does the list view for example).
2596 *
2597 * If index is negative, it means put it at the end of the list.
2598 *
2599 * @param child the view to add to the group
2600 * @param index the index at which the child must be added
2601 * @param params the layout parameters to associate with the child
2602 * @return true if the child was added, false otherwise
2603 */
2604 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
2605 return addViewInLayout(child, index, params, false);
2606 }
2607
2608 /**
2609 * Adds a view during layout. This is useful if in your onLayout() method,
2610 * you need to add more views (as does the list view for example).
2611 *
2612 * If index is negative, it means put it at the end of the list.
2613 *
2614 * @param child the view to add to the group
2615 * @param index the index at which the child must be added
2616 * @param params the layout parameters to associate with the child
2617 * @param preventRequestLayout if true, calling this method will not trigger a
2618 * layout request on child
2619 * @return true if the child was added, false otherwise
2620 */
2621 protected boolean addViewInLayout(View child, int index, LayoutParams params,
2622 boolean preventRequestLayout) {
2623 child.mParent = null;
2624 addViewInner(child, index, params, preventRequestLayout);
Romain Guy24443ea2009-05-11 11:56:30 -07002625 child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK) | DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 return true;
2627 }
2628
2629 /**
2630 * Prevents the specified child to be laid out during the next layout pass.
2631 *
2632 * @param child the child on which to perform the cleanup
2633 */
2634 protected void cleanupLayoutState(View child) {
2635 child.mPrivateFlags &= ~View.FORCE_LAYOUT;
2636 }
2637
2638 private void addViewInner(View child, int index, LayoutParams params,
2639 boolean preventRequestLayout) {
2640
2641 if (child.getParent() != null) {
2642 throw new IllegalStateException("The specified child already has a parent. " +
2643 "You must call removeView() on the child's parent first.");
2644 }
2645
Chet Haase21cd1382010-09-01 17:42:29 -07002646 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07002647 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07002648 }
2649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 if (!checkLayoutParams(params)) {
2651 params = generateLayoutParams(params);
2652 }
2653
2654 if (preventRequestLayout) {
2655 child.mLayoutParams = params;
2656 } else {
2657 child.setLayoutParams(params);
2658 }
2659
2660 if (index < 0) {
2661 index = mChildrenCount;
2662 }
2663
2664 addInArray(child, index);
2665
2666 // tell our children
2667 if (preventRequestLayout) {
2668 child.assignParent(this);
2669 } else {
2670 child.mParent = this;
2671 }
2672
2673 if (child.hasFocus()) {
2674 requestChildFocus(child, child.findFocus());
2675 }
Romain Guy8506ab42009-06-11 17:35:47 -07002676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 AttachInfo ai = mAttachInfo;
2678 if (ai != null) {
Romain Guy8506ab42009-06-11 17:35:47 -07002679 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 ai.mKeepScreenOn = false;
2681 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
2682 if (ai.mKeepScreenOn) {
2683 needGlobalAttributesUpdate(true);
2684 }
2685 ai.mKeepScreenOn = lastKeepOn;
2686 }
2687
2688 if (mOnHierarchyChangeListener != null) {
2689 mOnHierarchyChangeListener.onChildViewAdded(this, child);
2690 }
2691
2692 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
2693 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
2694 }
2695 }
2696
2697 private void addInArray(View child, int index) {
2698 View[] children = mChildren;
2699 final int count = mChildrenCount;
2700 final int size = children.length;
2701 if (index == count) {
2702 if (size == count) {
2703 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
2704 System.arraycopy(children, 0, mChildren, 0, size);
2705 children = mChildren;
2706 }
2707 children[mChildrenCount++] = child;
2708 } else if (index < count) {
2709 if (size == count) {
2710 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
2711 System.arraycopy(children, 0, mChildren, 0, index);
2712 System.arraycopy(children, index, mChildren, index + 1, count - index);
2713 children = mChildren;
2714 } else {
2715 System.arraycopy(children, index, children, index + 1, count - index);
2716 }
2717 children[index] = child;
2718 mChildrenCount++;
2719 } else {
2720 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
2721 }
2722 }
2723
2724 // This method also sets the child's mParent to null
2725 private void removeFromArray(int index) {
2726 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07002727 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
2728 children[index].mParent = null;
2729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 final int count = mChildrenCount;
2731 if (index == count - 1) {
2732 children[--mChildrenCount] = null;
2733 } else if (index >= 0 && index < count) {
2734 System.arraycopy(children, index + 1, children, index, count - index - 1);
2735 children[--mChildrenCount] = null;
2736 } else {
2737 throw new IndexOutOfBoundsException();
2738 }
2739 }
2740
2741 // This method also sets the children's mParent to null
2742 private void removeFromArray(int start, int count) {
2743 final View[] children = mChildren;
2744 final int childrenCount = mChildrenCount;
2745
2746 start = Math.max(0, start);
2747 final int end = Math.min(childrenCount, start + count);
2748
2749 if (start == end) {
2750 return;
2751 }
2752
2753 if (end == childrenCount) {
2754 for (int i = start; i < end; i++) {
2755 children[i].mParent = null;
2756 children[i] = null;
2757 }
2758 } else {
2759 for (int i = start; i < end; i++) {
2760 children[i].mParent = null;
2761 }
2762
2763 // Since we're looping above, we might as well do the copy, but is arraycopy()
2764 // faster than the extra 2 bounds checks we would do in the loop?
2765 System.arraycopy(children, end, children, start, childrenCount - end);
2766
2767 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
2768 children[i] = null;
2769 }
2770 }
2771
2772 mChildrenCount -= (end - start);
2773 }
2774
2775 private void bindLayoutAnimation(View child) {
2776 Animation a = mLayoutAnimationController.getAnimationForView(child);
2777 child.setAnimation(a);
2778 }
2779
2780 /**
2781 * Subclasses should override this method to set layout animation
2782 * parameters on the supplied child.
2783 *
2784 * @param child the child to associate with animation parameters
2785 * @param params the child's layout parameters which hold the animation
2786 * parameters
2787 * @param index the index of the child in the view group
2788 * @param count the number of children in the view group
2789 */
2790 protected void attachLayoutAnimationParameters(View child,
2791 LayoutParams params, int index, int count) {
2792 LayoutAnimationController.AnimationParameters animationParams =
2793 params.layoutAnimationParameters;
2794 if (animationParams == null) {
2795 animationParams = new LayoutAnimationController.AnimationParameters();
2796 params.layoutAnimationParameters = animationParams;
2797 }
2798
2799 animationParams.count = count;
2800 animationParams.index = index;
2801 }
2802
2803 /**
2804 * {@inheritDoc}
2805 */
2806 public void removeView(View view) {
2807 removeViewInternal(view);
2808 requestLayout();
2809 invalidate();
2810 }
2811
2812 /**
2813 * Removes a view during layout. This is useful if in your onLayout() method,
2814 * you need to remove more views.
2815 *
2816 * @param view the view to remove from the group
2817 */
2818 public void removeViewInLayout(View view) {
2819 removeViewInternal(view);
2820 }
2821
2822 /**
2823 * Removes a range of views during layout. This is useful if in your onLayout() method,
2824 * you need to remove more views.
2825 *
2826 * @param start the index of the first view to remove from the group
2827 * @param count the number of views to remove from the group
2828 */
2829 public void removeViewsInLayout(int start, int count) {
2830 removeViewsInternal(start, count);
2831 }
2832
2833 /**
2834 * Removes the view at the specified position in the group.
2835 *
2836 * @param index the position in the group of the view to remove
2837 */
2838 public void removeViewAt(int index) {
2839 removeViewInternal(index, getChildAt(index));
2840 requestLayout();
2841 invalidate();
2842 }
2843
2844 /**
2845 * Removes the specified range of views from the group.
2846 *
2847 * @param start the first position in the group of the range of views to remove
2848 * @param count the number of views to remove
2849 */
2850 public void removeViews(int start, int count) {
2851 removeViewsInternal(start, count);
2852 requestLayout();
2853 invalidate();
2854 }
2855
2856 private void removeViewInternal(View view) {
2857 final int index = indexOfChild(view);
2858 if (index >= 0) {
2859 removeViewInternal(index, view);
2860 }
2861 }
2862
2863 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07002864
2865 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07002866 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07002867 }
2868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 boolean clearChildFocus = false;
2870 if (view == mFocused) {
2871 view.clearFocusForRemoval();
2872 clearChildFocus = true;
2873 }
2874
Chet Haase21cd1382010-09-01 17:42:29 -07002875 if (view.getAnimation() != null ||
2876 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 addDisappearingView(view);
2878 } else if (view.mAttachInfo != null) {
2879 view.dispatchDetachedFromWindow();
2880 }
2881
2882 if (mOnHierarchyChangeListener != null) {
2883 mOnHierarchyChangeListener.onChildViewRemoved(this, view);
2884 }
2885
2886 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07002887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 removeFromArray(index);
2889
2890 if (clearChildFocus) {
2891 clearChildFocus(view);
2892 }
2893 }
2894
Chet Haase21cd1382010-09-01 17:42:29 -07002895 /**
2896 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
2897 * not null, changes in layout which occur because of children being added to or removed from
2898 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
2899 * object. By default, the transition object is null (so layout changes are not animated).
2900 *
2901 * @param transition The LayoutTransition object that will animated changes in layout. A value
2902 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07002903 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07002904 */
2905 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07002906 if (mTransition != null) {
2907 mTransition.removeTransitionListener(mLayoutTransitionListener);
2908 }
Chet Haase21cd1382010-09-01 17:42:29 -07002909 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07002910 if (mTransition != null) {
2911 mTransition.addTransitionListener(mLayoutTransitionListener);
2912 }
2913 }
2914
2915 /**
2916 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
2917 * not null, changes in layout which occur because of children being added to or removed from
2918 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
2919 * object. By default, the transition object is null (so layout changes are not animated).
2920 *
2921 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
2922 * A value of <code>null</code> means no transition will run on layout changes.
2923 */
2924 public LayoutTransition getLayoutTransition() {
2925 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07002926 }
2927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 private void removeViewsInternal(int start, int count) {
2929 final OnHierarchyChangeListener onHierarchyChangeListener = mOnHierarchyChangeListener;
2930 final boolean notifyListener = onHierarchyChangeListener != null;
2931 final View focused = mFocused;
2932 final boolean detach = mAttachInfo != null;
2933 View clearChildFocus = null;
2934
2935 final View[] children = mChildren;
2936 final int end = start + count;
2937
2938 for (int i = start; i < end; i++) {
2939 final View view = children[i];
2940
Chet Haase21cd1382010-09-01 17:42:29 -07002941 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07002942 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07002943 }
2944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 if (view == focused) {
2946 view.clearFocusForRemoval();
2947 clearChildFocus = view;
2948 }
2949
Chet Haase21cd1382010-09-01 17:42:29 -07002950 if (view.getAnimation() != null ||
2951 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 addDisappearingView(view);
2953 } else if (detach) {
2954 view.dispatchDetachedFromWindow();
2955 }
2956
2957 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07002958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 if (notifyListener) {
2960 onHierarchyChangeListener.onChildViewRemoved(this, view);
2961 }
2962 }
2963
2964 removeFromArray(start, count);
2965
2966 if (clearChildFocus != null) {
2967 clearChildFocus(clearChildFocus);
2968 }
2969 }
2970
2971 /**
2972 * Call this method to remove all child views from the
2973 * ViewGroup.
2974 */
2975 public void removeAllViews() {
2976 removeAllViewsInLayout();
2977 requestLayout();
2978 invalidate();
2979 }
2980
2981 /**
2982 * Called by a ViewGroup subclass to remove child views from itself,
2983 * when it must first know its size on screen before it can calculate how many
2984 * child views it will render. An example is a Gallery or a ListView, which
2985 * may "have" 50 children, but actually only render the number of children
2986 * that can currently fit inside the object on screen. Do not call
2987 * this method unless you are extending ViewGroup and understand the
2988 * view measuring and layout pipeline.
2989 */
2990 public void removeAllViewsInLayout() {
2991 final int count = mChildrenCount;
2992 if (count <= 0) {
2993 return;
2994 }
2995
2996 final View[] children = mChildren;
2997 mChildrenCount = 0;
2998
2999 final OnHierarchyChangeListener listener = mOnHierarchyChangeListener;
3000 final boolean notify = listener != null;
3001 final View focused = mFocused;
3002 final boolean detach = mAttachInfo != null;
3003 View clearChildFocus = null;
3004
3005 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 for (int i = count - 1; i >= 0; i--) {
3008 final View view = children[i];
3009
Chet Haase21cd1382010-09-01 17:42:29 -07003010 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003011 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003012 }
3013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 if (view == focused) {
3015 view.clearFocusForRemoval();
3016 clearChildFocus = view;
3017 }
3018
Chet Haase21cd1382010-09-01 17:42:29 -07003019 if (view.getAnimation() != null ||
3020 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 addDisappearingView(view);
3022 } else if (detach) {
3023 view.dispatchDetachedFromWindow();
3024 }
3025
3026 if (notify) {
3027 listener.onChildViewRemoved(this, view);
3028 }
3029
3030 view.mParent = null;
3031 children[i] = null;
3032 }
3033
3034 if (clearChildFocus != null) {
3035 clearChildFocus(clearChildFocus);
3036 }
3037 }
3038
3039 /**
3040 * Finishes the removal of a detached view. This method will dispatch the detached from
3041 * window event and notify the hierarchy change listener.
3042 *
3043 * @param child the child to be definitely removed from the view hierarchy
3044 * @param animate if true and the view has an animation, the view is placed in the
3045 * disappearing views list, otherwise, it is detached from the window
3046 *
3047 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3048 * @see #detachAllViewsFromParent()
3049 * @see #detachViewFromParent(View)
3050 * @see #detachViewFromParent(int)
3051 */
3052 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07003053 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003054 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07003055 }
3056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 if (child == mFocused) {
3058 child.clearFocus();
3059 }
Romain Guy8506ab42009-06-11 17:35:47 -07003060
Chet Haase21cd1382010-09-01 17:42:29 -07003061 if ((animate && child.getAnimation() != null) ||
3062 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 addDisappearingView(child);
3064 } else if (child.mAttachInfo != null) {
3065 child.dispatchDetachedFromWindow();
3066 }
3067
3068 if (mOnHierarchyChangeListener != null) {
3069 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
3070 }
3071 }
3072
3073 /**
3074 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
3075 * sets the layout parameters and puts the view in the list of children so it can be retrieved
3076 * by calling {@link #getChildAt(int)}.
3077 *
3078 * This method should be called only for view which were detached from their parent.
3079 *
3080 * @param child the child to attach
3081 * @param index the index at which the child should be attached
3082 * @param params the layout parameters of the child
3083 *
3084 * @see #removeDetachedView(View, boolean)
3085 * @see #detachAllViewsFromParent()
3086 * @see #detachViewFromParent(View)
3087 * @see #detachViewFromParent(int)
3088 */
3089 protected void attachViewToParent(View child, int index, LayoutParams params) {
3090 child.mLayoutParams = params;
3091
3092 if (index < 0) {
3093 index = mChildrenCount;
3094 }
3095
3096 addInArray(child, index);
3097
3098 child.mParent = this;
Romain Guybc9fdc92010-01-19 13:27:48 -08003099 child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK & ~DRAWING_CACHE_VALID) | DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100
3101 if (child.hasFocus()) {
3102 requestChildFocus(child, child.findFocus());
3103 }
3104 }
3105
3106 /**
3107 * Detaches a view from its parent. Detaching a view should be temporary and followed
3108 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3109 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3110 * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3111 *
3112 * @param child the child to detach
3113 *
3114 * @see #detachViewFromParent(int)
3115 * @see #detachViewsFromParent(int, int)
3116 * @see #detachAllViewsFromParent()
3117 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3118 * @see #removeDetachedView(View, boolean)
3119 */
3120 protected void detachViewFromParent(View child) {
3121 removeFromArray(indexOfChild(child));
3122 }
3123
3124 /**
3125 * Detaches a view from its parent. Detaching a view should be temporary and followed
3126 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3127 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3128 * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3129 *
3130 * @param index the index of the child to detach
3131 *
3132 * @see #detachViewFromParent(View)
3133 * @see #detachAllViewsFromParent()
3134 * @see #detachViewsFromParent(int, int)
3135 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3136 * @see #removeDetachedView(View, boolean)
3137 */
3138 protected void detachViewFromParent(int index) {
3139 removeFromArray(index);
3140 }
3141
3142 /**
3143 * Detaches a range of view from their parent. Detaching a view should be temporary and followed
3144 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3145 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, its
3146 * parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3147 *
3148 * @param start the first index of the childrend range to detach
3149 * @param count the number of children to detach
3150 *
3151 * @see #detachViewFromParent(View)
3152 * @see #detachViewFromParent(int)
3153 * @see #detachAllViewsFromParent()
3154 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3155 * @see #removeDetachedView(View, boolean)
3156 */
3157 protected void detachViewsFromParent(int start, int count) {
3158 removeFromArray(start, count);
3159 }
3160
3161 /**
3162 * Detaches all views from the parent. Detaching a view should be temporary and followed
3163 * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
3164 * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached,
3165 * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}.
3166 *
3167 * @see #detachViewFromParent(View)
3168 * @see #detachViewFromParent(int)
3169 * @see #detachViewsFromParent(int, int)
3170 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3171 * @see #removeDetachedView(View, boolean)
3172 */
3173 protected void detachAllViewsFromParent() {
3174 final int count = mChildrenCount;
3175 if (count <= 0) {
3176 return;
3177 }
3178
3179 final View[] children = mChildren;
3180 mChildrenCount = 0;
3181
3182 for (int i = count - 1; i >= 0; i--) {
3183 children[i].mParent = null;
3184 children[i] = null;
3185 }
3186 }
3187
3188 /**
3189 * Don't call or override this method. It is used for the implementation of
3190 * the view hierarchy.
3191 */
3192 public final void invalidateChild(View child, final Rect dirty) {
3193 if (ViewDebug.TRACE_HIERARCHY) {
3194 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
3195 }
3196
3197 ViewParent parent = this;
3198
3199 final AttachInfo attachInfo = mAttachInfo;
3200 if (attachInfo != null) {
3201 final int[] location = attachInfo.mInvalidateChildLocation;
3202 location[CHILD_LEFT_INDEX] = child.mLeft;
3203 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haasec3aa3612010-06-17 08:50:37 -07003204 Matrix childMatrix = child.getMatrix();
3205 if (!childMatrix.isIdentity()) {
Chet Haase9bc829c2010-07-15 17:09:23 -07003206 RectF boundingRect = attachInfo.mTmpTransformRect;
3207 boundingRect.set(dirty);
3208 childMatrix.mapRect(boundingRect);
3209 dirty.set((int) boundingRect.left, (int) boundingRect.top,
3210 (int) (boundingRect.right + 0.5f),
3211 (int) (boundingRect.bottom + 0.5f));
Chet Haasec3aa3612010-06-17 08:50:37 -07003212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213
3214 // If the child is drawing an animation, we want to copy this flag onto
3215 // ourselves and the parent to make sure the invalidate request goes
3216 // through
3217 final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07003218
3219 // Check whether the child that requests the invalidate is fully opaque
Romain Guyec25df92009-05-25 04:39:37 -07003220 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
3221 child.getAnimation() != null;
Romain Guy24443ea2009-05-11 11:56:30 -07003222 // Mark the child as dirty, using the appropriate flag
3223 // Make sure we do not set both flags at the same time
3224 final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
3225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 do {
Romain Guy24443ea2009-05-11 11:56:30 -07003227 View view = null;
3228 if (parent instanceof View) {
3229 view = (View) parent;
3230 }
3231
Romain Guybb93d552009-03-24 21:04:15 -07003232 if (drawAnimation) {
Romain Guy24443ea2009-05-11 11:56:30 -07003233 if (view != null) {
3234 view.mPrivateFlags |= DRAW_ANIMATION;
Romain Guybb93d552009-03-24 21:04:15 -07003235 } else if (parent instanceof ViewRoot) {
3236 ((ViewRoot) parent).mIsAnimating = true;
3237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
Romain Guy24443ea2009-05-11 11:56:30 -07003239
3240 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
3241 // flag coming from the child that initiated the invalidate
3242 if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
3243 view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
3244 }
3245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 parent = parent.invalidateChildInParent(location, dirty);
Chet Haase9bc829c2010-07-15 17:09:23 -07003247 if (view != null) {
3248 // Account for transform on current parent
3249 Matrix m = view.getMatrix();
3250 if (!m.isIdentity()) {
3251 RectF boundingRect = attachInfo.mTmpTransformRect;
3252 boundingRect.set(dirty);
3253 m.mapRect(boundingRect);
3254 dirty.set((int) boundingRect.left, (int) boundingRect.top,
3255 (int) (boundingRect.right + 0.5f),
3256 (int) (boundingRect.bottom + 0.5f));
Chet Haasec3aa3612010-06-17 08:50:37 -07003257 }
Chet Haasec3aa3612010-06-17 08:50:37 -07003258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 } while (parent != null);
3260 }
3261 }
3262
3263 /**
3264 * Don't call or override this method. It is used for the implementation of
3265 * the view hierarchy.
3266 *
3267 * This implementation returns null if this ViewGroup does not have a parent,
3268 * if this ViewGroup is already fully invalidated or if the dirty rectangle
3269 * does not intersect with this ViewGroup's bounds.
3270 */
3271 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
3272 if (ViewDebug.TRACE_HIERARCHY) {
3273 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
3274 }
3275
3276 if ((mPrivateFlags & DRAWN) == DRAWN) {
3277 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
3278 FLAG_OPTIMIZE_INVALIDATE) {
3279 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
3280 location[CHILD_TOP_INDEX] - mScrollY);
3281
3282 final int left = mLeft;
3283 final int top = mTop;
3284
Adam Powell879fb6b2010-09-20 11:23:56 -07003285 if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
3287 mPrivateFlags &= ~DRAWING_CACHE_VALID;
3288
3289 location[CHILD_LEFT_INDEX] = left;
3290 location[CHILD_TOP_INDEX] = top;
3291
3292 return mParent;
3293 }
3294 } else {
3295 mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
3296
3297 location[CHILD_LEFT_INDEX] = mLeft;
3298 location[CHILD_TOP_INDEX] = mTop;
3299
3300 dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
3301 mBottom - location[CHILD_TOP_INDEX]);
3302
3303 return mParent;
3304 }
3305 }
3306
3307 return null;
3308 }
3309
3310 /**
3311 * Offset a rectangle that is in a descendant's coordinate
3312 * space into our coordinate space.
3313 * @param descendant A descendant of this view
3314 * @param rect A rectangle defined in descendant's coordinate space.
3315 */
3316 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
3317 offsetRectBetweenParentAndChild(descendant, rect, true, false);
3318 }
3319
3320 /**
3321 * Offset a rectangle that is in our coordinate space into an ancestor's
3322 * coordinate space.
3323 * @param descendant A descendant of this view
3324 * @param rect A rectangle defined in descendant's coordinate space.
3325 */
3326 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
3327 offsetRectBetweenParentAndChild(descendant, rect, false, false);
3328 }
3329
3330 /**
3331 * Helper method that offsets a rect either from parent to descendant or
3332 * descendant to parent.
3333 */
3334 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
3335 boolean offsetFromChildToParent, boolean clipToBounds) {
3336
3337 // already in the same coord system :)
3338 if (descendant == this) {
3339 return;
3340 }
3341
3342 ViewParent theParent = descendant.mParent;
3343
3344 // search and offset up to the parent
3345 while ((theParent != null)
3346 && (theParent instanceof View)
3347 && (theParent != this)) {
3348
3349 if (offsetFromChildToParent) {
3350 rect.offset(descendant.mLeft - descendant.mScrollX,
3351 descendant.mTop - descendant.mScrollY);
3352 if (clipToBounds) {
3353 View p = (View) theParent;
3354 rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
3355 }
3356 } else {
3357 if (clipToBounds) {
3358 View p = (View) theParent;
3359 rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
3360 }
3361 rect.offset(descendant.mScrollX - descendant.mLeft,
3362 descendant.mScrollY - descendant.mTop);
3363 }
3364
3365 descendant = (View) theParent;
3366 theParent = descendant.mParent;
3367 }
3368
3369 // now that we are up to this view, need to offset one more time
3370 // to get into our coordinate space
3371 if (theParent == this) {
3372 if (offsetFromChildToParent) {
3373 rect.offset(descendant.mLeft - descendant.mScrollX,
3374 descendant.mTop - descendant.mScrollY);
3375 } else {
3376 rect.offset(descendant.mScrollX - descendant.mLeft,
3377 descendant.mScrollY - descendant.mTop);
3378 }
3379 } else {
3380 throw new IllegalArgumentException("parameter must be a descendant of this view");
3381 }
3382 }
3383
3384 /**
3385 * Offset the vertical location of all children of this view by the specified number of pixels.
3386 *
3387 * @param offset the number of pixels to offset
3388 *
3389 * @hide
3390 */
3391 public void offsetChildrenTopAndBottom(int offset) {
3392 final int count = mChildrenCount;
3393 final View[] children = mChildren;
3394
3395 for (int i = 0; i < count; i++) {
3396 final View v = children[i];
3397 v.mTop += offset;
3398 v.mBottom += offset;
3399 }
3400 }
3401
3402 /**
3403 * {@inheritDoc}
3404 */
3405 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
3406 int dx = child.mLeft - mScrollX;
3407 int dy = child.mTop - mScrollY;
3408 if (offset != null) {
3409 offset.x += dx;
3410 offset.y += dy;
3411 }
3412 r.offset(dx, dy);
3413 return r.intersect(0, 0, mRight - mLeft, mBottom - mTop) &&
3414 (mParent == null || mParent.getChildVisibleRect(this, r, offset));
3415 }
3416
3417 /**
3418 * {@inheritDoc}
3419 */
3420 @Override
3421 protected abstract void onLayout(boolean changed,
3422 int l, int t, int r, int b);
3423
3424 /**
3425 * Indicates whether the view group has the ability to animate its children
3426 * after the first layout.
3427 *
3428 * @return true if the children can be animated, false otherwise
3429 */
3430 protected boolean canAnimate() {
3431 return mLayoutAnimationController != null;
3432 }
3433
3434 /**
3435 * Runs the layout animation. Calling this method triggers a relayout of
3436 * this view group.
3437 */
3438 public void startLayoutAnimation() {
3439 if (mLayoutAnimationController != null) {
3440 mGroupFlags |= FLAG_RUN_ANIMATION;
3441 requestLayout();
3442 }
3443 }
3444
3445 /**
3446 * Schedules the layout animation to be played after the next layout pass
3447 * of this view group. This can be used to restart the layout animation
3448 * when the content of the view group changes or when the activity is
3449 * paused and resumed.
3450 */
3451 public void scheduleLayoutAnimation() {
3452 mGroupFlags |= FLAG_RUN_ANIMATION;
3453 }
3454
3455 /**
3456 * Sets the layout animation controller used to animate the group's
3457 * children after the first layout.
3458 *
3459 * @param controller the animation controller
3460 */
3461 public void setLayoutAnimation(LayoutAnimationController controller) {
3462 mLayoutAnimationController = controller;
3463 if (mLayoutAnimationController != null) {
3464 mGroupFlags |= FLAG_RUN_ANIMATION;
3465 }
3466 }
3467
3468 /**
3469 * Returns the layout animation controller used to animate the group's
3470 * children.
3471 *
3472 * @return the current animation controller
3473 */
3474 public LayoutAnimationController getLayoutAnimation() {
3475 return mLayoutAnimationController;
3476 }
3477
3478 /**
3479 * Indicates whether the children's drawing cache is used during a layout
3480 * animation. By default, the drawing cache is enabled but this will prevent
3481 * nested layout animations from working. To nest animations, you must disable
3482 * the cache.
3483 *
3484 * @return true if the animation cache is enabled, false otherwise
3485 *
3486 * @see #setAnimationCacheEnabled(boolean)
3487 * @see View#setDrawingCacheEnabled(boolean)
3488 */
3489 @ViewDebug.ExportedProperty
3490 public boolean isAnimationCacheEnabled() {
3491 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
3492 }
3493
3494 /**
3495 * Enables or disables the children's drawing cache during a layout animation.
3496 * By default, the drawing cache is enabled but this will prevent nested
3497 * layout animations from working. To nest animations, you must disable the
3498 * cache.
3499 *
3500 * @param enabled true to enable the animation cache, false otherwise
3501 *
3502 * @see #isAnimationCacheEnabled()
3503 * @see View#setDrawingCacheEnabled(boolean)
3504 */
3505 public void setAnimationCacheEnabled(boolean enabled) {
3506 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
3507 }
3508
3509 /**
3510 * Indicates whether this ViewGroup will always try to draw its children using their
3511 * drawing cache. By default this property is enabled.
3512 *
3513 * @return true if the animation cache is enabled, false otherwise
3514 *
3515 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
3516 * @see #setChildrenDrawnWithCacheEnabled(boolean)
3517 * @see View#setDrawingCacheEnabled(boolean)
3518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003519 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 public boolean isAlwaysDrawnWithCacheEnabled() {
3521 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
3522 }
3523
3524 /**
3525 * Indicates whether this ViewGroup will always try to draw its children using their
3526 * drawing cache. This property can be set to true when the cache rendering is
3527 * slightly different from the children's normal rendering. Renderings can be different,
3528 * for instance, when the cache's quality is set to low.
3529 *
3530 * When this property is disabled, the ViewGroup will use the drawing cache of its
3531 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
3532 * when to start using the drawing cache and when to stop using it.
3533 *
3534 * @param always true to always draw with the drawing cache, false otherwise
3535 *
3536 * @see #isAlwaysDrawnWithCacheEnabled()
3537 * @see #setChildrenDrawnWithCacheEnabled(boolean)
3538 * @see View#setDrawingCacheEnabled(boolean)
3539 * @see View#setDrawingCacheQuality(int)
3540 */
3541 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
3542 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
3543 }
3544
3545 /**
3546 * Indicates whether the ViewGroup is currently drawing its children using
3547 * their drawing cache.
3548 *
3549 * @return true if children should be drawn with their cache, false otherwise
3550 *
3551 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
3552 * @see #setChildrenDrawnWithCacheEnabled(boolean)
3553 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003554 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 protected boolean isChildrenDrawnWithCacheEnabled() {
3556 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
3557 }
3558
3559 /**
3560 * Tells the ViewGroup to draw its children using their drawing cache. This property
3561 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
3562 * will be used only if it has been enabled.
3563 *
3564 * Subclasses should call this method to start and stop using the drawing cache when
3565 * they perform performance sensitive operations, like scrolling or animating.
3566 *
3567 * @param enabled true if children should be drawn with their cache, false otherwise
3568 *
3569 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
3570 * @see #isChildrenDrawnWithCacheEnabled()
3571 */
3572 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
3573 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
3574 }
3575
Romain Guy293451e2009-11-04 13:59:48 -08003576 /**
3577 * Indicates whether the ViewGroup is drawing its children in the order defined by
3578 * {@link #getChildDrawingOrder(int, int)}.
3579 *
3580 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
3581 * false otherwise
3582 *
3583 * @see #setChildrenDrawingOrderEnabled(boolean)
3584 * @see #getChildDrawingOrder(int, int)
3585 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003586 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08003587 protected boolean isChildrenDrawingOrderEnabled() {
3588 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
3589 }
3590
3591 /**
3592 * Tells the ViewGroup whether to draw its children in the order defined by the method
3593 * {@link #getChildDrawingOrder(int, int)}.
3594 *
3595 * @param enabled true if the order of the children when drawing is determined by
3596 * {@link #getChildDrawingOrder(int, int)}, false otherwise
3597 *
3598 * @see #isChildrenDrawingOrderEnabled()
3599 * @see #getChildDrawingOrder(int, int)
3600 */
3601 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
3602 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
3603 }
3604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 private void setBooleanFlag(int flag, boolean value) {
3606 if (value) {
3607 mGroupFlags |= flag;
3608 } else {
3609 mGroupFlags &= ~flag;
3610 }
3611 }
3612
3613 /**
3614 * Returns an integer indicating what types of drawing caches are kept in memory.
3615 *
3616 * @see #setPersistentDrawingCache(int)
3617 * @see #setAnimationCacheEnabled(boolean)
3618 *
3619 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
3620 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
3621 * and {@link #PERSISTENT_ALL_CACHES}
3622 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003623 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07003625 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
3627 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
3628 })
3629 public int getPersistentDrawingCache() {
3630 return mPersistentDrawingCache;
3631 }
3632
3633 /**
3634 * Indicates what types of drawing caches should be kept in memory after
3635 * they have been created.
3636 *
3637 * @see #getPersistentDrawingCache()
3638 * @see #setAnimationCacheEnabled(boolean)
3639 *
3640 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
3641 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
3642 * and {@link #PERSISTENT_ALL_CACHES}
3643 */
3644 public void setPersistentDrawingCache(int drawingCacheToKeep) {
3645 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
3646 }
3647
3648 /**
3649 * Returns a new set of layout parameters based on the supplied attributes set.
3650 *
3651 * @param attrs the attributes to build the layout parameters from
3652 *
3653 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
3654 * of its descendants
3655 */
3656 public LayoutParams generateLayoutParams(AttributeSet attrs) {
3657 return new LayoutParams(getContext(), attrs);
3658 }
3659
3660 /**
3661 * Returns a safe set of layout parameters based on the supplied layout params.
3662 * When a ViewGroup is passed a View whose layout params do not pass the test of
3663 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
3664 * is invoked. This method should return a new set of layout params suitable for
3665 * this ViewGroup, possibly by copying the appropriate attributes from the
3666 * specified set of layout params.
3667 *
3668 * @param p The layout parameters to convert into a suitable set of layout parameters
3669 * for this ViewGroup.
3670 *
3671 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
3672 * of its descendants
3673 */
3674 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3675 return p;
3676 }
3677
3678 /**
3679 * Returns a set of default layout parameters. These parameters are requested
3680 * when the View passed to {@link #addView(View)} has no layout parameters
3681 * already set. If null is returned, an exception is thrown from addView.
3682 *
3683 * @return a set of default layout parameters or null
3684 */
3685 protected LayoutParams generateDefaultLayoutParams() {
3686 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
3687 }
3688
3689 /**
Romain Guy13922e02009-05-12 17:56:14 -07003690 * @hide
3691 */
3692 @Override
3693 protected boolean dispatchConsistencyCheck(int consistency) {
3694 boolean result = super.dispatchConsistencyCheck(consistency);
3695
3696 final int count = mChildrenCount;
3697 final View[] children = mChildren;
3698 for (int i = 0; i < count; i++) {
3699 if (!children[i].dispatchConsistencyCheck(consistency)) result = false;
3700 }
3701
3702 return result;
3703 }
3704
3705 /**
3706 * @hide
3707 */
3708 @Override
3709 protected boolean onConsistencyCheck(int consistency) {
3710 boolean result = super.onConsistencyCheck(consistency);
3711
3712 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
3713 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
3714
3715 if (checkLayout) {
3716 final int count = mChildrenCount;
3717 final View[] children = mChildren;
3718 for (int i = 0; i < count; i++) {
3719 if (children[i].getParent() != this) {
3720 result = false;
3721 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
3722 "View " + children[i] + " has no parent/a parent that is not " + this);
3723 }
3724 }
3725 }
3726
3727 if (checkDrawing) {
3728 // If this group is dirty, check that the parent is dirty as well
3729 if ((mPrivateFlags & DIRTY_MASK) != 0) {
3730 final ViewParent parent = getParent();
3731 if (parent != null && !(parent instanceof ViewRoot)) {
3732 if ((((View) parent).mPrivateFlags & DIRTY_MASK) == 0) {
3733 result = false;
3734 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
3735 "ViewGroup " + this + " is dirty but its parent is not: " + this);
3736 }
3737 }
3738 }
3739 }
3740
3741 return result;
3742 }
3743
3744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 * {@inheritDoc}
3746 */
3747 @Override
3748 protected void debug(int depth) {
3749 super.debug(depth);
3750 String output;
3751
3752 if (mFocused != null) {
3753 output = debugIndent(depth);
3754 output += "mFocused";
3755 Log.d(VIEW_LOG_TAG, output);
3756 }
3757 if (mChildrenCount != 0) {
3758 output = debugIndent(depth);
3759 output += "{";
3760 Log.d(VIEW_LOG_TAG, output);
3761 }
3762 int count = mChildrenCount;
3763 for (int i = 0; i < count; i++) {
3764 View child = mChildren[i];
3765 child.debug(depth + 1);
3766 }
3767
3768 if (mChildrenCount != 0) {
3769 output = debugIndent(depth);
3770 output += "}";
3771 Log.d(VIEW_LOG_TAG, output);
3772 }
3773 }
3774
3775 /**
3776 * Returns the position in the group of the specified child view.
3777 *
3778 * @param child the view for which to get the position
3779 * @return a positive integer representing the position of the view in the
3780 * group, or -1 if the view does not exist in the group
3781 */
3782 public int indexOfChild(View child) {
3783 final int count = mChildrenCount;
3784 final View[] children = mChildren;
3785 for (int i = 0; i < count; i++) {
3786 if (children[i] == child) {
3787 return i;
3788 }
3789 }
3790 return -1;
3791 }
3792
3793 /**
3794 * Returns the number of children in the group.
3795 *
3796 * @return a positive integer representing the number of children in
3797 * the group
3798 */
3799 public int getChildCount() {
3800 return mChildrenCount;
3801 }
3802
3803 /**
3804 * Returns the view at the specified position in the group.
3805 *
3806 * @param index the position at which to get the view from
3807 * @return the view at the specified position or null if the position
3808 * does not exist within the group
3809 */
3810 public View getChildAt(int index) {
3811 try {
3812 return mChildren[index];
3813 } catch (IndexOutOfBoundsException ex) {
3814 return null;
3815 }
3816 }
3817
3818 /**
3819 * Ask all of the children of this view to measure themselves, taking into
3820 * account both the MeasureSpec requirements for this view and its padding.
3821 * We skip children that are in the GONE state The heavy lifting is done in
3822 * getChildMeasureSpec.
3823 *
3824 * @param widthMeasureSpec The width requirements for this view
3825 * @param heightMeasureSpec The height requirements for this view
3826 */
3827 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
3828 final int size = mChildrenCount;
3829 final View[] children = mChildren;
3830 for (int i = 0; i < size; ++i) {
3831 final View child = children[i];
3832 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
3833 measureChild(child, widthMeasureSpec, heightMeasureSpec);
3834 }
3835 }
3836 }
3837
3838 /**
3839 * Ask one of the children of this view to measure itself, taking into
3840 * account both the MeasureSpec requirements for this view and its padding.
3841 * The heavy lifting is done in getChildMeasureSpec.
3842 *
3843 * @param child The child to measure
3844 * @param parentWidthMeasureSpec The width requirements for this view
3845 * @param parentHeightMeasureSpec The height requirements for this view
3846 */
3847 protected void measureChild(View child, int parentWidthMeasureSpec,
3848 int parentHeightMeasureSpec) {
3849 final LayoutParams lp = child.getLayoutParams();
3850
3851 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
3852 mPaddingLeft + mPaddingRight, lp.width);
3853 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
3854 mPaddingTop + mPaddingBottom, lp.height);
3855
3856 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
3857 }
3858
3859 /**
3860 * Ask one of the children of this view to measure itself, taking into
3861 * account both the MeasureSpec requirements for this view and its padding
3862 * and margins. The child must have MarginLayoutParams The heavy lifting is
3863 * done in getChildMeasureSpec.
3864 *
3865 * @param child The child to measure
3866 * @param parentWidthMeasureSpec The width requirements for this view
3867 * @param widthUsed Extra space that has been used up by the parent
3868 * horizontally (possibly by other children of the parent)
3869 * @param parentHeightMeasureSpec The height requirements for this view
3870 * @param heightUsed Extra space that has been used up by the parent
3871 * vertically (possibly by other children of the parent)
3872 */
3873 protected void measureChildWithMargins(View child,
3874 int parentWidthMeasureSpec, int widthUsed,
3875 int parentHeightMeasureSpec, int heightUsed) {
3876 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
3877
3878 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
3879 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
3880 + widthUsed, lp.width);
3881 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
3882 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
3883 + heightUsed, lp.height);
3884
3885 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
3886 }
3887
3888 /**
3889 * Does the hard part of measureChildren: figuring out the MeasureSpec to
3890 * pass to a particular child. This method figures out the right MeasureSpec
3891 * for one dimension (height or width) of one child view.
3892 *
3893 * The goal is to combine information from our MeasureSpec with the
3894 * LayoutParams of the child to get the best possible results. For example,
3895 * if the this view knows its size (because its MeasureSpec has a mode of
3896 * EXACTLY), and the child has indicated in its LayoutParams that it wants
3897 * to be the same size as the parent, the parent should ask the child to
3898 * layout given an exact size.
3899 *
3900 * @param spec The requirements for this view
3901 * @param padding The padding of this view for the current dimension and
3902 * margins, if applicable
3903 * @param childDimension How big the child wants to be in the current
3904 * dimension
3905 * @return a MeasureSpec integer for the child
3906 */
3907 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
3908 int specMode = MeasureSpec.getMode(spec);
3909 int specSize = MeasureSpec.getSize(spec);
3910
3911 int size = Math.max(0, specSize - padding);
3912
3913 int resultSize = 0;
3914 int resultMode = 0;
3915
3916 switch (specMode) {
3917 // Parent has imposed an exact size on us
3918 case MeasureSpec.EXACTLY:
3919 if (childDimension >= 0) {
3920 resultSize = childDimension;
3921 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08003922 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 // Child wants to be our size. So be it.
3924 resultSize = size;
3925 resultMode = MeasureSpec.EXACTLY;
3926 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
3927 // Child wants to determine its own size. It can't be
3928 // bigger than us.
3929 resultSize = size;
3930 resultMode = MeasureSpec.AT_MOST;
3931 }
3932 break;
3933
3934 // Parent has imposed a maximum size on us
3935 case MeasureSpec.AT_MOST:
3936 if (childDimension >= 0) {
3937 // Child wants a specific size... so be it
3938 resultSize = childDimension;
3939 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08003940 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 // Child wants to be our size, but our size is not fixed.
3942 // Constrain child to not be bigger than us.
3943 resultSize = size;
3944 resultMode = MeasureSpec.AT_MOST;
3945 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
3946 // Child wants to determine its own size. It can't be
3947 // bigger than us.
3948 resultSize = size;
3949 resultMode = MeasureSpec.AT_MOST;
3950 }
3951 break;
3952
3953 // Parent asked to see how big we want to be
3954 case MeasureSpec.UNSPECIFIED:
3955 if (childDimension >= 0) {
3956 // Child wants a specific size... let him have it
3957 resultSize = childDimension;
3958 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08003959 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 // Child wants to be our size... find out how big it should
3961 // be
3962 resultSize = 0;
3963 resultMode = MeasureSpec.UNSPECIFIED;
3964 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
3965 // Child wants to determine its own size.... find out how
3966 // big it should be
3967 resultSize = 0;
3968 resultMode = MeasureSpec.UNSPECIFIED;
3969 }
3970 break;
3971 }
3972 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
3973 }
3974
3975
3976 /**
3977 * Removes any pending animations for views that have been removed. Call
3978 * this if you don't want animations for exiting views to stack up.
3979 */
3980 public void clearDisappearingChildren() {
3981 if (mDisappearingChildren != null) {
3982 mDisappearingChildren.clear();
3983 }
3984 }
3985
3986 /**
3987 * Add a view which is removed from mChildren but still needs animation
3988 *
3989 * @param v View to add
3990 */
3991 private void addDisappearingView(View v) {
3992 ArrayList<View> disappearingChildren = mDisappearingChildren;
3993
3994 if (disappearingChildren == null) {
3995 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
3996 }
3997
3998 disappearingChildren.add(v);
3999 }
4000
4001 /**
4002 * Cleanup a view when its animation is done. This may mean removing it from
4003 * the list of disappearing views.
4004 *
4005 * @param view The view whose animation has finished
4006 * @param animation The animation, cannot be null
4007 */
4008 private void finishAnimatingView(final View view, Animation animation) {
4009 final ArrayList<View> disappearingChildren = mDisappearingChildren;
4010 if (disappearingChildren != null) {
4011 if (disappearingChildren.contains(view)) {
4012 disappearingChildren.remove(view);
4013
4014 if (view.mAttachInfo != null) {
4015 view.dispatchDetachedFromWindow();
4016 }
4017
4018 view.clearAnimation();
4019 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4020 }
4021 }
4022
4023 if (animation != null && !animation.getFillAfter()) {
4024 view.clearAnimation();
4025 }
4026
4027 if ((view.mPrivateFlags & ANIMATION_STARTED) == ANIMATION_STARTED) {
4028 view.onAnimationEnd();
4029 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
4030 // so we'd rather be safe than sorry
4031 view.mPrivateFlags &= ~ANIMATION_STARTED;
4032 // Draw one more frame after the animation is done
4033 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4034 }
4035 }
4036
Chet Haaseb20db3e2010-09-10 13:07:30 -07004037 /**
4038 * This method tells the ViewGroup that the given View object, which should have this
4039 * ViewGroup as its parent,
4040 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
4041 * is removed from its parent. This allows animations, such as those used by
4042 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
4043 * the removal of views. A call to this method should always be accompanied by a later call
4044 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
4045 * so that the View finally gets removed.
4046 *
4047 * @param view The View object to be kept visible even if it gets removed from its parent.
4048 */
4049 public void startViewTransition(View view) {
4050 if (view.mParent == this) {
4051 if (mTransitioningViews == null) {
4052 mTransitioningViews = new ArrayList<View>();
4053 }
4054 mTransitioningViews.add(view);
4055 }
4056 }
4057
4058 /**
4059 * This method should always be called following an earlier call to
4060 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
4061 * and will no longer be displayed. Note that this method does not perform the functionality
4062 * of removing a view from its parent; it just discontinues the display of a View that
4063 * has previously been removed.
4064 *
4065 * @return view The View object that has been removed but is being kept around in the visible
4066 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
4067 */
4068 public void endViewTransition(View view) {
4069 if (mTransitioningViews != null) {
4070 mTransitioningViews.remove(view);
4071 final ArrayList<View> disappearingChildren = mDisappearingChildren;
4072 if (disappearingChildren != null && disappearingChildren.contains(view)) {
4073 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07004074 if (mVisibilityChangingChildren != null &&
4075 mVisibilityChangingChildren.contains(view)) {
4076 mVisibilityChangingChildren.remove(view);
4077 } else {
4078 if (view.mAttachInfo != null) {
4079 view.dispatchDetachedFromWindow();
4080 }
4081 if (view.mParent != null) {
4082 view.mParent = null;
4083 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07004084 }
4085 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
4086 }
4087 }
4088 }
4089
Chet Haase21cd1382010-09-01 17:42:29 -07004090 private LayoutTransition.TransitionListener mLayoutTransitionListener =
4091 new LayoutTransition.TransitionListener() {
4092 @Override
4093 public void startTransition(LayoutTransition transition, ViewGroup container,
4094 View view, int transitionType) {
4095 // We only care about disappearing items, since we need special logic to keep
4096 // those items visible after they've been 'removed'
4097 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004098 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07004099 }
4100 }
4101
4102 @Override
4103 public void endTransition(LayoutTransition transition, ViewGroup container,
4104 View view, int transitionType) {
4105 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004106 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07004107 }
4108 }
4109 };
4110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 /**
4112 * {@inheritDoc}
4113 */
4114 @Override
4115 public boolean gatherTransparentRegion(Region region) {
4116 // If no transparent regions requested, we are always opaque.
4117 final boolean meOpaque = (mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) == 0;
4118 if (meOpaque && region == null) {
4119 // The caller doesn't care about the region, so stop now.
4120 return true;
4121 }
4122 super.gatherTransparentRegion(region);
4123 final View[] children = mChildren;
4124 final int count = mChildrenCount;
4125 boolean noneOfTheChildrenAreTransparent = true;
4126 for (int i = 0; i < count; i++) {
4127 final View child = children[i];
4128 if ((child.mViewFlags & VISIBILITY_MASK) != GONE || child.getAnimation() != null) {
4129 if (!child.gatherTransparentRegion(region)) {
4130 noneOfTheChildrenAreTransparent = false;
4131 }
4132 }
4133 }
4134 return meOpaque || noneOfTheChildrenAreTransparent;
4135 }
4136
4137 /**
4138 * {@inheritDoc}
4139 */
4140 public void requestTransparentRegion(View child) {
4141 if (child != null) {
4142 child.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
4143 if (mParent != null) {
4144 mParent.requestTransparentRegion(this);
4145 }
4146 }
4147 }
Romain Guy8506ab42009-06-11 17:35:47 -07004148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149
4150 @Override
4151 protected boolean fitSystemWindows(Rect insets) {
4152 boolean done = super.fitSystemWindows(insets);
4153 if (!done) {
4154 final int count = mChildrenCount;
4155 final View[] children = mChildren;
4156 for (int i = 0; i < count; i++) {
4157 done = children[i].fitSystemWindows(insets);
4158 if (done) {
4159 break;
4160 }
4161 }
4162 }
4163 return done;
4164 }
4165
4166 /**
4167 * Returns the animation listener to which layout animation events are
4168 * sent.
4169 *
4170 * @return an {@link android.view.animation.Animation.AnimationListener}
4171 */
4172 public Animation.AnimationListener getLayoutAnimationListener() {
4173 return mAnimationListener;
4174 }
4175
4176 @Override
4177 protected void drawableStateChanged() {
4178 super.drawableStateChanged();
4179
4180 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
4181 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
4182 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
4183 + " child has duplicateParentState set to true");
4184 }
4185
4186 final View[] children = mChildren;
4187 final int count = mChildrenCount;
4188
4189 for (int i = 0; i < count; i++) {
4190 final View child = children[i];
4191 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
4192 child.refreshDrawableState();
4193 }
4194 }
4195 }
4196 }
4197
4198 @Override
4199 protected int[] onCreateDrawableState(int extraSpace) {
4200 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
4201 return super.onCreateDrawableState(extraSpace);
4202 }
4203
4204 int need = 0;
4205 int n = getChildCount();
4206 for (int i = 0; i < n; i++) {
4207 int[] childState = getChildAt(i).getDrawableState();
4208
4209 if (childState != null) {
4210 need += childState.length;
4211 }
4212 }
4213
4214 int[] state = super.onCreateDrawableState(extraSpace + need);
4215
4216 for (int i = 0; i < n; i++) {
4217 int[] childState = getChildAt(i).getDrawableState();
4218
4219 if (childState != null) {
4220 state = mergeDrawableStates(state, childState);
4221 }
4222 }
4223
4224 return state;
4225 }
4226
4227 /**
4228 * Sets whether this ViewGroup's drawable states also include
4229 * its children's drawable states. This is used, for example, to
4230 * make a group appear to be focused when its child EditText or button
4231 * is focused.
4232 */
4233 public void setAddStatesFromChildren(boolean addsStates) {
4234 if (addsStates) {
4235 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
4236 } else {
4237 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
4238 }
4239
4240 refreshDrawableState();
4241 }
4242
4243 /**
4244 * Returns whether this ViewGroup's drawable states also include
4245 * its children's drawable states. This is used, for example, to
4246 * make a group appear to be focused when its child EditText or button
4247 * is focused.
4248 */
4249 public boolean addStatesFromChildren() {
4250 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
4251 }
4252
4253 /**
4254 * If {link #addStatesFromChildren} is true, refreshes this group's
4255 * drawable state (to include the states from its children).
4256 */
4257 public void childDrawableStateChanged(View child) {
4258 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
4259 refreshDrawableState();
4260 }
4261 }
4262
4263 /**
4264 * Specifies the animation listener to which layout animation events must
4265 * be sent. Only
4266 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
4267 * and
4268 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
4269 * are invoked.
4270 *
4271 * @param animationListener the layout animation listener
4272 */
4273 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
4274 mAnimationListener = animationListener;
4275 }
4276
4277 /**
4278 * LayoutParams are used by views to tell their parents how they want to be
4279 * laid out. See
4280 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
4281 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07004282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 * <p>
4284 * The base LayoutParams class just describes how big the view wants to be
4285 * for both width and height. For each dimension, it can specify one of:
4286 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004287 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
4288 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
4290 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004291 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 * </ul>
4293 * There are subclasses of LayoutParams for different subclasses of
4294 * ViewGroup. For example, AbsoluteLayout has its own subclass of
4295 * LayoutParams which adds an X and Y value.
4296 *
4297 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
4298 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
4299 */
4300 public static class LayoutParams {
4301 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004302 * Special value for the height or width requested by a View.
4303 * FILL_PARENT means that the view wants to be as big as its parent,
4304 * minus the parent's padding, if any. This value is deprecated
4305 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 */
Romain Guy980a9382010-01-08 15:06:28 -08004307 @SuppressWarnings({"UnusedDeclaration"})
4308 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 public static final int FILL_PARENT = -1;
4310
4311 /**
4312 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08004313 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004314 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08004315 */
4316 public static final int MATCH_PARENT = -1;
4317
4318 /**
4319 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 * WRAP_CONTENT means that the view wants to be just large enough to fit
4321 * its own internal content, taking its own padding into account.
4322 */
4323 public static final int WRAP_CONTENT = -2;
4324
4325 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004326 * Information about how wide the view wants to be. Can be one of the
4327 * constants FILL_PARENT (replaced by MATCH_PARENT ,
4328 * in API Level 8) or WRAP_CONTENT. or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004330 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08004331 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
4333 })
4334 public int width;
4335
4336 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004337 * Information about how tall the view wants to be. Can be one of the
4338 * constants FILL_PARENT (replaced by MATCH_PARENT ,
4339 * in API Level 8) or WRAP_CONTENT. or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004341 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08004342 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
4344 })
4345 public int height;
4346
4347 /**
4348 * Used to animate layouts.
4349 */
4350 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
4351
4352 /**
4353 * Creates a new set of layout parameters. The values are extracted from
4354 * the supplied attributes set and context. The XML attributes mapped
4355 * to this set of layout parameters are:
4356 *
4357 * <ul>
4358 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004359 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
4360 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004362 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
4363 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 * </ul>
4365 *
4366 * @param c the application environment
4367 * @param attrs the set of attributes from which to extract the layout
4368 * parameters' values
4369 */
4370 public LayoutParams(Context c, AttributeSet attrs) {
4371 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
4372 setBaseAttributes(a,
4373 R.styleable.ViewGroup_Layout_layout_width,
4374 R.styleable.ViewGroup_Layout_layout_height);
4375 a.recycle();
4376 }
4377
4378 /**
4379 * Creates a new set of layout parameters with the specified width
4380 * and height.
4381 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07004382 * @param width the width, either {@link #WRAP_CONTENT},
4383 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
4384 * API Level 8), or a fixed size in pixels
4385 * @param height the height, either {@link #WRAP_CONTENT},
4386 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
4387 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 */
4389 public LayoutParams(int width, int height) {
4390 this.width = width;
4391 this.height = height;
4392 }
4393
4394 /**
4395 * Copy constructor. Clones the width and height values of the source.
4396 *
4397 * @param source The layout params to copy from.
4398 */
4399 public LayoutParams(LayoutParams source) {
4400 this.width = source.width;
4401 this.height = source.height;
4402 }
4403
4404 /**
4405 * Used internally by MarginLayoutParams.
4406 * @hide
4407 */
4408 LayoutParams() {
4409 }
4410
4411 /**
4412 * Extracts the layout parameters from the supplied attributes.
4413 *
4414 * @param a the style attributes to extract the parameters from
4415 * @param widthAttr the identifier of the width attribute
4416 * @param heightAttr the identifier of the height attribute
4417 */
4418 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
4419 width = a.getLayoutDimension(widthAttr, "layout_width");
4420 height = a.getLayoutDimension(heightAttr, "layout_height");
4421 }
4422
4423 /**
4424 * Returns a String representation of this set of layout parameters.
4425 *
4426 * @param output the String to prepend to the internal representation
4427 * @return a String with the following format: output +
4428 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07004429 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 * @hide
4431 */
4432 public String debug(String output) {
4433 return output + "ViewGroup.LayoutParams={ width="
4434 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
4435 }
4436
4437 /**
4438 * Converts the specified size to a readable String.
4439 *
4440 * @param size the size to convert
4441 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07004442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 * @hide
4444 */
4445 protected static String sizeToString(int size) {
4446 if (size == WRAP_CONTENT) {
4447 return "wrap-content";
4448 }
Romain Guy980a9382010-01-08 15:06:28 -08004449 if (size == MATCH_PARENT) {
4450 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 }
4452 return String.valueOf(size);
4453 }
4454 }
4455
4456 /**
4457 * Per-child layout information for layouts that support margins.
4458 * See
4459 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
4460 * for a list of all child view attributes that this class supports.
4461 */
4462 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
4463 /**
4464 * The left margin in pixels of the child.
4465 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004466 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 public int leftMargin;
4468
4469 /**
4470 * The top margin in pixels of the child.
4471 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004472 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 public int topMargin;
4474
4475 /**
4476 * The right margin in pixels of the child.
4477 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004478 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 public int rightMargin;
4480
4481 /**
4482 * The bottom margin in pixels of the child.
4483 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004484 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 public int bottomMargin;
4486
4487 /**
4488 * Creates a new set of layout parameters. The values are extracted from
4489 * the supplied attributes set and context.
4490 *
4491 * @param c the application environment
4492 * @param attrs the set of attributes from which to extract the layout
4493 * parameters' values
4494 */
4495 public MarginLayoutParams(Context c, AttributeSet attrs) {
4496 super();
4497
4498 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
4499 setBaseAttributes(a,
4500 R.styleable.ViewGroup_MarginLayout_layout_width,
4501 R.styleable.ViewGroup_MarginLayout_layout_height);
4502
4503 int margin = a.getDimensionPixelSize(
4504 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
4505 if (margin >= 0) {
4506 leftMargin = margin;
4507 topMargin = margin;
4508 rightMargin= margin;
4509 bottomMargin = margin;
4510 } else {
4511 leftMargin = a.getDimensionPixelSize(
4512 R.styleable.ViewGroup_MarginLayout_layout_marginLeft, 0);
4513 topMargin = a.getDimensionPixelSize(
4514 R.styleable.ViewGroup_MarginLayout_layout_marginTop, 0);
4515 rightMargin = a.getDimensionPixelSize(
4516 R.styleable.ViewGroup_MarginLayout_layout_marginRight, 0);
4517 bottomMargin = a.getDimensionPixelSize(
4518 R.styleable.ViewGroup_MarginLayout_layout_marginBottom, 0);
4519 }
4520
4521 a.recycle();
4522 }
4523
4524 /**
4525 * {@inheritDoc}
4526 */
4527 public MarginLayoutParams(int width, int height) {
4528 super(width, height);
4529 }
4530
4531 /**
4532 * Copy constructor. Clones the width, height and margin values of the source.
4533 *
4534 * @param source The layout params to copy from.
4535 */
4536 public MarginLayoutParams(MarginLayoutParams source) {
4537 this.width = source.width;
4538 this.height = source.height;
4539
4540 this.leftMargin = source.leftMargin;
4541 this.topMargin = source.topMargin;
4542 this.rightMargin = source.rightMargin;
4543 this.bottomMargin = source.bottomMargin;
4544 }
4545
4546 /**
4547 * {@inheritDoc}
4548 */
4549 public MarginLayoutParams(LayoutParams source) {
4550 super(source);
4551 }
4552
4553 /**
4554 * Sets the margins, in pixels.
4555 *
4556 * @param left the left margin size
4557 * @param top the top margin size
4558 * @param right the right margin size
4559 * @param bottom the bottom margin size
4560 *
4561 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
4562 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
4563 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
4564 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
4565 */
4566 public void setMargins(int left, int top, int right, int bottom) {
4567 leftMargin = left;
4568 topMargin = top;
4569 rightMargin = right;
4570 bottomMargin = bottom;
4571 }
4572 }
Adam Powell2b342f02010-08-18 18:14:13 -07004573
Jeff Brown20e987b2010-08-23 12:01:02 -07004574 /* Describes a touched view and the ids of the pointers that it has captured.
4575 *
4576 * This code assumes that pointer ids are always in the range 0..31 such that
4577 * it can use a bitfield to track which pointer ids are present.
4578 * As it happens, the lower layers of the input dispatch pipeline also use the
4579 * same trick so the assumption should be safe here...
4580 */
4581 private static final class TouchTarget {
4582 private static final int MAX_RECYCLED = 32;
4583 private static final Object sRecycleLock = new Object();
4584 private static TouchTarget sRecycleBin;
4585 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07004586
Jeff Brown20e987b2010-08-23 12:01:02 -07004587 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07004588
Jeff Brown20e987b2010-08-23 12:01:02 -07004589 // The touched child view.
4590 public View child;
4591
4592 // The combined bit mask of pointer ids for all pointers captured by the target.
4593 public int pointerIdBits;
4594
4595 // The next target in the target list.
4596 public TouchTarget next;
4597
4598 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07004599 }
4600
Jeff Brown20e987b2010-08-23 12:01:02 -07004601 public static TouchTarget obtain(View child, int pointerIdBits) {
4602 final TouchTarget target;
4603 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07004604 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07004605 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07004606 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07004607 target = sRecycleBin;
4608 sRecycleBin = target.next;
4609 sRecycledCount--;
4610 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07004611 }
Adam Powell816c3be2010-08-23 18:00:05 -07004612 }
Jeff Brown20e987b2010-08-23 12:01:02 -07004613 target.child = child;
4614 target.pointerIdBits = pointerIdBits;
4615 return target;
4616 }
Adam Powell816c3be2010-08-23 18:00:05 -07004617
Jeff Brown20e987b2010-08-23 12:01:02 -07004618 public void recycle() {
4619 synchronized (sRecycleLock) {
4620 if (sRecycledCount < MAX_RECYCLED) {
4621 next = sRecycleBin;
4622 sRecycleBin = this;
4623 sRecycledCount += 1;
Adam Powell816c3be2010-08-23 18:00:05 -07004624 }
Adam Powell816c3be2010-08-23 18:00:05 -07004625 }
4626 }
Adam Powell2b342f02010-08-18 18:14:13 -07004627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628}