blob: 3aa4cfb34374ca01eb2c91be50375068888ff8e2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Chet Haase21cd1382010-09-01 17:42:29 -070019import android.animation.LayoutTransition;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.TypedArray;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
Philip Milne10ca24a2012-04-23 15:38:27 -070025import android.graphics.Color;
26import android.graphics.Insets;
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;
Jeff Brown995e7742010-12-22 16:59:36 -080033import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Parcelable;
35import android.os.SystemClock;
36import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.Log;
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -080038import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.util.SparseArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070040import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070041import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.view.animation.Animation;
43import android.view.animation.AnimationUtils;
44import android.view.animation.LayoutAnimationController;
45import android.view.animation.Transformation;
Doug Feltcb3791202011-07-07 11:57:48 -070046
Romain Guy0211a0a2011-02-14 16:34:59 -080047import com.android.internal.R;
48import com.android.internal.util.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50import java.util.ArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -070051import java.util.Collections;
Christopher Tate86cab1b2011-01-13 20:28:55 -080052import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Fabrice Di Meglio0072f642013-03-26 15:50:24 -070054import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056/**
57 * <p>
58 * A <code>ViewGroup</code> is a special view that can contain other views
59 * (called children.) The view group is the base class for layouts and views
60 * containers. This class also defines the
61 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
62 * class for layouts parameters.
63 * </p>
64 *
65 * <p>
66 * Also see {@link LayoutParams} for layout attributes.
67 * </p>
Romain Guyd6a463a2009-05-21 23:10:10 -070068 *
Joe Fernandez558459f2011-10-13 16:47:36 -070069 * <div class="special reference">
70 * <h3>Developer Guides</h3>
71 * <p>For more information about creating user interface layouts, read the
72 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
73 * guide.</p></div>
74 *
Dianne Hackborn7caab0f2013-03-06 13:47:04 -080075 * <p>Here is a complete implementation of a custom ViewGroup that implements
76 * a simple {@link android.widget.FrameLayout} along with the ability to stack
77 * children in left and right gutters.</p>
78 *
79 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java
80 * Complete}
81 *
82 * <p>If you are implementing XML layout attributes as shown in the example, this is the
83 * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p>
84 *
85 * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout}
86 *
87 * <p>Finally the layout manager can be used in an XML layout like so:</p>
88 *
89 * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete}
90 *
Romain Guyd6a463a2009-05-21 23:10:10 -070091 * @attr ref android.R.styleable#ViewGroup_clipChildren
92 * @attr ref android.R.styleable#ViewGroup_clipToPadding
93 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
94 * @attr ref android.R.styleable#ViewGroup_animationCache
95 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
96 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
97 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
98 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
Chet Haase13cc1202010-09-03 15:39:20 -070099 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Scott Main27a85082013-06-10 10:39:48 -0700100 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
101 * @attr ref android.R.styleable#ViewGroup_layoutMode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 */
103public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Adam Powell539ee872012-02-03 19:00:49 -0800104 private static final String TAG = "ViewGroup";
Chet Haase21cd1382010-09-01 17:42:29 -0700105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private static final boolean DBG = false;
Philip Milne7b757812012-09-19 18:13:44 -0700107 /** @hide */
108 public static boolean DEBUG_DRAW = false;
Gilles Debunnecea45132011-11-24 02:19:27 +0100109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 /**
111 * Views which have been hidden or removed which need to be animated on
112 * their way out.
113 * This field should be made private, so it is hidden from the SDK.
114 * {@hide}
115 */
116 protected ArrayList<View> mDisappearingChildren;
117
118 /**
119 * Listener used to propagate events indicating when children are added
120 * and/or removed from a view group.
121 * This field should be made private, so it is hidden from the SDK.
122 * {@hide}
123 */
124 protected OnHierarchyChangeListener mOnHierarchyChangeListener;
125
126 // The view contained within this ViewGroup that has or contains focus.
127 private View mFocused;
128
Chet Haase48460322010-06-11 14:22:25 -0700129 /**
130 * A Transformation used when drawing children, to
131 * apply on the child being drawn.
132 */
Chet Haase64a48c12012-02-13 16:33:29 -0800133 final Transformation mChildTransformation = new Transformation();
Chet Haase48460322010-06-11 14:22:25 -0700134
135 /**
136 * Used to track the current invalidation region.
137 */
Chet Haase64a48c12012-02-13 16:33:29 -0800138 RectF mInvalidateRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Chet Haase48460322010-06-11 14:22:25 -0700140 /**
141 * A Transformation used to calculate a correct
142 * invalidation area when the application is autoscaled.
143 */
Chet Haase64a48c12012-02-13 16:33:29 -0800144 Transformation mInvalidationTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700145
Christopher Tatea53146c2010-09-07 11:57:52 -0700146 // View currently under an ongoing drag
147 private View mCurrentDragView;
148
Christopher Tate86cab1b2011-01-13 20:28:55 -0800149 // Metadata about the ongoing drag
150 private DragEvent mCurrentDrag;
151 private HashSet<View> mDragNotifiedChildren;
152
Christopher Tatea53146c2010-09-07 11:57:52 -0700153 // Does this group have a child that can accept the current drag payload?
154 private boolean mChildAcceptsDrag;
155
156 // Used during drag dispatch
157 private final PointF mLocalPoint = new PointF();
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // Layout animation
160 private LayoutAnimationController mLayoutAnimationController;
161 private Animation.AnimationListener mAnimationListener;
162
Jeff Brown20e987b2010-08-23 12:01:02 -0700163 // First touch target in the linked list of touch targets.
164 private TouchTarget mFirstTouchTarget;
165
Joe Onorato03ab0c72011-01-06 15:46:27 -0800166 // For debugging only. You can see these in hierarchyviewer.
Romain Guye95003e2011-01-09 13:53:06 -0800167 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800168 @ViewDebug.ExportedProperty(category = "events")
169 private long mLastTouchDownTime;
170 @ViewDebug.ExportedProperty(category = "events")
171 private int mLastTouchDownIndex = -1;
Romain Guye95003e2011-01-09 13:53:06 -0800172 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800173 @ViewDebug.ExportedProperty(category = "events")
174 private float mLastTouchDownX;
Romain Guye95003e2011-01-09 13:53:06 -0800175 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800176 @ViewDebug.ExportedProperty(category = "events")
177 private float mLastTouchDownY;
178
Jeff Brown87b7f802011-06-21 18:35:45 -0700179 // First hover target in the linked list of hover targets.
180 // The hover targets are children which have received ACTION_HOVER_ENTER.
181 // They might not have actually handled the hover event, but we will
182 // continue sending hover events to them as long as the pointer remains over
183 // their bounds and the view group does not intercept hover.
184 private HoverTarget mFirstHoverTarget;
Jeff Browna032cc02011-03-07 16:56:21 -0800185
Jeff Brown10b62902011-06-20 16:40:37 -0700186 // True if the view group itself received a hover event.
187 // It might not have actually handled the hover event.
188 private boolean mHoveredSelf;
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
191 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * This field should be made private, so it is hidden from the SDK.
194 * {@hide}
195 */
Romain Guy2440e672012-08-07 14:43:43 -0700196 @ViewDebug.ExportedProperty(flagMapping = {
197 @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN,
198 name = "CLIP_CHILDREN"),
199 @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING,
200 name = "CLIP_TO_PADDING"),
201 @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL,
202 name = "PADDING_NOT_NULL")
203 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 protected int mGroupFlags;
205
Philip Milne7b757812012-09-19 18:13:44 -0700206 /**
207 * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -0700208 */
Philip Milne7b757812012-09-19 18:13:44 -0700209 private int mLayoutMode = DEFAULT_LAYOUT_MODE;
Philip Milne1557fd72012-04-04 23:41:34 -0700210
Romain Guy33f6beb2012-02-16 19:24:51 -0800211 /**
212 * NOTE: If you change the flags below make sure to reflect the changes
213 * the DisplayList class
214 */
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 // When set, ViewGroup invalidates only the child's rectangle
217 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800218 static final int FLAG_CLIP_CHILDREN = 0x1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
220 // When set, ViewGroup excludes the padding area from the invalidate rectangle
221 // Set by default
222 private static final int FLAG_CLIP_TO_PADDING = 0x2;
223
224 // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
225 // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
Chet Haase64a48c12012-02-13 16:33:29 -0800226 static final int FLAG_INVALIDATE_REQUIRED = 0x4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
228 // When set, dispatchDraw() will run the layout animation and unset the flag
229 private static final int FLAG_RUN_ANIMATION = 0x8;
230
231 // When set, there is either no layout animation on the ViewGroup or the layout
232 // animation is over
233 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800234 static final int FLAG_ANIMATION_DONE = 0x10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
236 // If set, this ViewGroup has padding; if unset there is no padding and we don't need
237 // to clip it, even if FLAG_CLIP_TO_PADDING is set
238 private static final int FLAG_PADDING_NOT_NULL = 0x20;
239
240 // When set, this ViewGroup caches its children in a Bitmap before starting a layout animation
241 // Set by default
242 private static final int FLAG_ANIMATION_CACHE = 0x40;
243
244 // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
245 // layout animation; this avoid clobbering the hierarchy
246 // Automatically set when the layout animation starts, depending on the animation's
247 // characteristics
Chet Haase64a48c12012-02-13 16:33:29 -0800248 static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250 // When set, the next call to drawChild() will clear mChildTransformation's matrix
Chet Haase64a48c12012-02-13 16:33:29 -0800251 static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
254 // the children's Bitmap caches if necessary
255 // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
256 private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
257
258 /**
259 * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
260 * to get the index of the child to draw for that iteration.
Romain Guy293451e2009-11-04 13:59:48 -0800261 *
262 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
264 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * When set, this ViewGroup supports static transformations on children; this causes
268 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
269 * invoked when a child is drawn.
270 *
271 * Any subclass overriding
272 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
273 * set this flags in {@link #mGroupFlags}.
Romain Guy8506ab42009-06-11 17:35:47 -0700274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * {@hide}
276 */
277 protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
278
279 // When the previous drawChild() invocation used an alpha value that was lower than
280 // 1.0 and set it in mCachePaint
Chet Haase64a48c12012-02-13 16:33:29 -0800281 static final int FLAG_ALPHA_LOWER_THAN_ONE = 0x1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282
283 /**
284 * When set, this ViewGroup's drawable states also include those
285 * of its children.
286 */
287 private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
288
289 /**
290 * When set, this ViewGroup tries to always draw its children using their drawing cache.
291 */
Chet Haase64a48c12012-02-13 16:33:29 -0800292 static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
294 /**
295 * When set, and if FLAG_ALWAYS_DRAWN_WITH_CACHE is not set, this ViewGroup will try to
296 * draw its children with their drawing cache.
297 */
Chet Haase64a48c12012-02-13 16:33:29 -0800298 static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
300 /**
301 * When set, this group will go through its list of children to notify them of
302 * any drawable state change.
303 */
304 private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
305
306 private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
307
308 /**
309 * This view will get focus before any of its descendants.
310 */
311 public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
312
313 /**
314 * This view will get focus only if none of its descendants want it.
315 */
316 public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
317
318 /**
319 * This view will block any of its descendants from getting focus, even
320 * if they are focusable.
321 */
322 public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
323
324 /**
325 * Used to map between enum in attrubutes and flag values.
326 */
327 private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
328 {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
329 FOCUS_BLOCK_DESCENDANTS};
330
331 /**
332 * When set, this ViewGroup should not intercept touch events.
Adam Powell110486f2010-06-22 17:14:44 -0700333 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
Adam Powell110486f2010-06-22 17:14:44 -0700335 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700338 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
339 */
Adam Powellf37df072010-09-17 16:22:49 -0700340 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700341
342 /**
Adam Powell4b867882011-09-16 12:59:46 -0700343 * When set, this ViewGroup will not dispatch onAttachedToWindow calls
344 * to children when adding new views. This is used to prevent multiple
345 * onAttached calls when a ViewGroup adds children in its own onAttached method.
346 */
347 private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000;
348
349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * Indicates which types of drawing caches are to be kept in memory.
351 * This field should be made private, so it is hidden from the SDK.
352 * {@hide}
353 */
354 protected int mPersistentDrawingCache;
355
356 /**
357 * Used to indicate that no drawing cache should be kept in memory.
358 */
359 public static final int PERSISTENT_NO_CACHE = 0x0;
360
361 /**
362 * Used to indicate that the animation drawing cache should be kept in memory.
363 */
364 public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
365
366 /**
367 * Used to indicate that the scrolling drawing cache should be kept in memory.
368 */
369 public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
370
371 /**
372 * Used to indicate that all drawing caches should be kept in memory.
373 */
374 public static final int PERSISTENT_ALL_CACHES = 0x3;
375
Philip Milne1557fd72012-04-04 23:41:34 -0700376 // Layout Modes
377
Philip Milne1557fd72012-04-04 23:41:34 -0700378 /**
379 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700380 * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
Philip Milne1557fd72012-04-04 23:41:34 -0700381 * {@link #getRight() right} and {@link #getBottom() bottom}.
382 */
Philip Milne7b757812012-09-19 18:13:44 -0700383 public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
Philip Milne1557fd72012-04-04 23:41:34 -0700384
385 /**
386 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700387 * Optical bounds describe where a widget appears to be. They sit inside the clip
388 * bounds which need to cover a larger area to allow other effects,
389 * such as shadows and glows, to be drawn.
Philip Milne1557fd72012-04-04 23:41:34 -0700390 */
Philip Milne7b757812012-09-19 18:13:44 -0700391 public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
392
393 /** @hide */
394 public static int DEFAULT_LAYOUT_MODE = LAYOUT_MODE_CLIP_BOUNDS;
Philip Milne1557fd72012-04-04 23:41:34 -0700395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
398 * are set at the same time.
399 */
400 protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
401
402 // Index of the child's left position in the mLocation array
403 private static final int CHILD_LEFT_INDEX = 0;
404 // Index of the child's top position in the mLocation array
405 private static final int CHILD_TOP_INDEX = 1;
406
407 // Child views of this ViewGroup
408 private View[] mChildren;
409 // Number of valid children in the mChildren array, the rest should be null or not
410 // considered as children
411 private int mChildrenCount;
412
Chet Haaseb9895022013-04-02 15:10:58 -0700413 // Whether layout calls are currently being suppressed, controlled by calls to
414 // suppressLayout()
415 boolean mSuppressLayout = false;
416
417 // Whether any layout calls have actually been suppressed while mSuppressLayout
418 // has been true. This tracks whether we need to issue a requestLayout() when
419 // layout is later re-enabled.
420 private boolean mLayoutCalledWhileSuppressed = false;
421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 private static final int ARRAY_INITIAL_CAPACITY = 12;
423 private static final int ARRAY_CAPACITY_INCREMENT = 12;
424
Romain Guycbc67742012-04-27 16:12:57 -0700425 private static Paint sDebugPaint;
426 private static float[] sDebugLines;
Philip Milne604f4402012-04-24 19:27:11 -0700427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 // Used to draw cached views
Chet Haase64a48c12012-02-13 16:33:29 -0800429 Paint mCachePaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
Chet Haase21cd1382010-09-01 17:42:29 -0700431 // Used to animate add/remove changes in layout
432 private LayoutTransition mTransition;
433
434 // The set of views that are currently being transitioned. This list is used to track views
435 // being removed that should not actually be removed from the parent yet because they are
436 // being animated.
437 private ArrayList<View> mTransitioningViews;
438
Chet Haase5e25c2c2010-09-16 11:15:56 -0700439 // List of children changing visibility. This is used to potentially keep rendering
440 // views during a transition when they otherwise would have become gone/invisible
441 private ArrayList<View> mVisibilityChangingChildren;
442
Adam Powell539ee872012-02-03 19:00:49 -0800443 // Indicates how many of this container's child subtrees contain transient state
444 @ViewDebug.ExportedProperty(category = "layout")
445 private int mChildCountWithTransientState = 0;
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public ViewGroup(Context context) {
448 super(context);
449 initViewGroup();
450 }
451
452 public ViewGroup(Context context, AttributeSet attrs) {
453 super(context, attrs);
454 initViewGroup();
455 initFromAttributes(context, attrs);
456 }
457
458 public ViewGroup(Context context, AttributeSet attrs, int defStyle) {
459 super(context, attrs, defStyle);
460 initViewGroup();
461 initFromAttributes(context, attrs);
462 }
463
Philip Milne10ca24a2012-04-23 15:38:27 -0700464 private boolean debugDraw() {
Philip Milne7b757812012-09-19 18:13:44 -0700465 return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout;
Philip Milne10ca24a2012-04-23 15:38:27 -0700466 }
467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 private void initViewGroup() {
469 // ViewGroup doesn't draw by default
Philip Milne10ca24a2012-04-23 15:38:27 -0700470 if (!debugDraw()) {
471 setFlags(WILL_NOT_DRAW, DRAW_MASK);
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 mGroupFlags |= FLAG_CLIP_CHILDREN;
474 mGroupFlags |= FLAG_CLIP_TO_PADDING;
475 mGroupFlags |= FLAG_ANIMATION_DONE;
476 mGroupFlags |= FLAG_ANIMATION_CACHE;
477 mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
478
Jeff Brown995e7742010-12-22 16:59:36 -0800479 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
480 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
481 }
482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
484
485 mChildren = new View[ARRAY_INITIAL_CAPACITY];
486 mChildrenCount = 0;
487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
489 }
490
491 private void initFromAttributes(Context context, AttributeSet attrs) {
492 TypedArray a = context.obtainStyledAttributes(attrs,
493 R.styleable.ViewGroup);
494
495 final int N = a.getIndexCount();
496 for (int i = 0; i < N; i++) {
497 int attr = a.getIndex(i);
498 switch (attr) {
499 case R.styleable.ViewGroup_clipChildren:
500 setClipChildren(a.getBoolean(attr, true));
501 break;
502 case R.styleable.ViewGroup_clipToPadding:
503 setClipToPadding(a.getBoolean(attr, true));
504 break;
505 case R.styleable.ViewGroup_animationCache:
506 setAnimationCacheEnabled(a.getBoolean(attr, true));
507 break;
508 case R.styleable.ViewGroup_persistentDrawingCache:
509 setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
510 break;
511 case R.styleable.ViewGroup_addStatesFromChildren:
512 setAddStatesFromChildren(a.getBoolean(attr, false));
513 break;
514 case R.styleable.ViewGroup_alwaysDrawnWithCache:
515 setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
516 break;
517 case R.styleable.ViewGroup_layoutAnimation:
518 int id = a.getResourceId(attr, -1);
519 if (id > 0) {
520 setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
521 }
522 break;
523 case R.styleable.ViewGroup_descendantFocusability:
524 setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
525 break;
Adam Powell2b342f02010-08-18 18:14:13 -0700526 case R.styleable.ViewGroup_splitMotionEvents:
527 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
528 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700529 case R.styleable.ViewGroup_animateLayoutChanges:
530 boolean animateLayoutChanges = a.getBoolean(attr, false);
531 if (animateLayoutChanges) {
532 setLayoutTransition(new LayoutTransition());
533 }
534 break;
Philip Milne7b757812012-09-19 18:13:44 -0700535 case R.styleable.ViewGroup_layoutMode:
536 setLayoutMode(a.getInt(attr, DEFAULT_LAYOUT_MODE));
537 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 }
540
541 a.recycle();
542 }
543
544 /**
545 * Gets the descendant focusability of this view group. The descendant
546 * focusability defines the relationship between this view group and its
547 * descendants when looking for a view to take focus in
548 * {@link #requestFocus(int, android.graphics.Rect)}.
549 *
550 * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
551 * {@link #FOCUS_BLOCK_DESCENDANTS}.
552 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700553 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
555 @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
556 @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
557 })
558 public int getDescendantFocusability() {
559 return mGroupFlags & FLAG_MASK_FOCUSABILITY;
560 }
561
562 /**
563 * Set the descendant focusability of this view group. This defines the relationship
564 * between this view group and its descendants when looking for a view to
565 * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
566 *
567 * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
568 * {@link #FOCUS_BLOCK_DESCENDANTS}.
569 */
570 public void setDescendantFocusability(int focusability) {
571 switch (focusability) {
572 case FOCUS_BEFORE_DESCENDANTS:
573 case FOCUS_AFTER_DESCENDANTS:
574 case FOCUS_BLOCK_DESCENDANTS:
575 break;
576 default:
577 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
578 + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
579 }
580 mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
581 mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
582 }
583
584 /**
585 * {@inheritDoc}
586 */
587 @Override
588 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
589 if (mFocused != null) {
590 mFocused.unFocus();
591 mFocused = null;
592 }
593 super.handleFocusGainInternal(direction, previouslyFocusedRect);
594 }
595
596 /**
597 * {@inheritDoc}
598 */
599 public void requestChildFocus(View child, View focused) {
600 if (DBG) {
601 System.out.println(this + " requestChildFocus()");
602 }
603 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
604 return;
605 }
606
607 // Unfocus us, if necessary
608 super.unFocus();
609
610 // We had a previous notion of who had focus. Clear it.
611 if (mFocused != child) {
612 if (mFocused != null) {
613 mFocused.unFocus();
614 }
615
616 mFocused = child;
617 }
618 if (mParent != null) {
619 mParent.requestChildFocus(this, focused);
620 }
621 }
622
623 /**
624 * {@inheritDoc}
625 */
626 public void focusableViewAvailable(View v) {
627 if (mParent != null
628 // shortcut: don't report a new focusable view if we block our descendants from
629 // getting focus
630 && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS)
631 // shortcut: don't report a new focusable view if we already are focused
632 // (and we don't prefer our descendants)
633 //
634 // note: knowing that mFocused is non-null is not a good enough reason
635 // to break the traversal since in that case we'd actually have to find
636 // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700637 // an ancestor of v; this will get checked for at ViewAncestor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) {
639 mParent.focusableViewAvailable(v);
640 }
641 }
642
643 /**
644 * {@inheritDoc}
645 */
646 public boolean showContextMenuForChild(View originalView) {
647 return mParent != null && mParent.showContextMenuForChild(originalView);
648 }
649
650 /**
Adam Powell6e346362010-07-23 10:18:23 -0700651 * {@inheritDoc}
652 */
653 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
654 return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null;
655 }
656
657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 * Find the nearest view in the specified direction that wants to take
659 * focus.
660 *
661 * @param focused The view that currently has focus
662 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
663 * FOCUS_RIGHT, or 0 for not applicable.
664 */
665 public View focusSearch(View focused, int direction) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -0700666 if (isRootNamespace()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 // root namespace means we should consider ourselves the top of the
668 // tree for focus searching; otherwise we could be focus searching
669 // into other tabs. see LocalActivityManager and TabHost for more info
670 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
671 } else if (mParent != null) {
672 return mParent.focusSearch(focused, direction);
673 }
674 return null;
675 }
676
677 /**
678 * {@inheritDoc}
679 */
680 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
681 return false;
682 }
683
684 /**
685 * {@inheritDoc}
686 */
Svetoslav Ganov42138042012-03-20 11:51:39 -0700687 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700688 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -0700689 ViewParent parent = mParent;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700690 if (parent == null) {
691 return false;
692 }
693 final boolean propagate = onRequestSendAccessibilityEvent(child, event);
694 if (!propagate) {
695 return false;
696 }
697 return parent.requestSendAccessibilityEvent(this, event);
698 }
699
700 /**
701 * Called when a child has requested sending an {@link AccessibilityEvent} and
702 * gives an opportunity to its parent to augment the event.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700703 * <p>
Adam Powell2fcbbd02011-09-28 18:56:43 -0700704 * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling
705 * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its
706 * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)}
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700707 * is responsible for handling this call.
708 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700709 *
710 * @param child The child which requests sending the event.
711 * @param event The event to be sent.
712 * @return True if the event should be sent.
713 *
714 * @see #requestSendAccessibilityEvent(View, AccessibilityEvent)
715 */
716 public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700717 if (mAccessibilityDelegate != null) {
718 return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event);
719 } else {
720 return onRequestSendAccessibilityEventInternal(child, event);
721 }
722 }
723
724 /**
725 * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent)
726 *
727 * Note: Called from the default {@link View.AccessibilityDelegate}.
728 */
729 boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700730 return true;
731 }
732
733 /**
Adam Powell539ee872012-02-03 19:00:49 -0800734 * Called when a child view has changed whether or not it is tracking transient state.
735 *
736 * @hide
737 */
738 public void childHasTransientStateChanged(View child, boolean childHasTransientState) {
739 final boolean oldHasTransientState = hasTransientState();
740 if (childHasTransientState) {
741 mChildCountWithTransientState++;
742 } else {
743 mChildCountWithTransientState--;
744 }
745
746 final boolean newHasTransientState = hasTransientState();
747 if (mParent != null && oldHasTransientState != newHasTransientState) {
748 try {
749 mParent.childHasTransientStateChanged(this, newHasTransientState);
750 } catch (AbstractMethodError e) {
751 Log.e(TAG, mParent.getClass().getSimpleName() +
752 " does not fully implement ViewParent", e);
753 }
754 }
755 }
756
757 /**
758 * @hide
759 */
760 @Override
761 public boolean hasTransientState() {
762 return mChildCountWithTransientState > 0 || super.hasTransientState();
763 }
764
765 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700766 * {@inheritDoc}
767 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 @Override
769 public boolean dispatchUnhandledMove(View focused, int direction) {
770 return mFocused != null &&
771 mFocused.dispatchUnhandledMove(focused, direction);
772 }
773
774 /**
775 * {@inheritDoc}
776 */
777 public void clearChildFocus(View child) {
778 if (DBG) {
779 System.out.println(this + " clearChildFocus()");
780 }
781
782 mFocused = null;
783 if (mParent != null) {
784 mParent.clearChildFocus(this);
785 }
786 }
787
788 /**
789 * {@inheritDoc}
790 */
791 @Override
792 public void clearFocus() {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800793 if (DBG) {
794 System.out.println(this + " clearFocus()");
795 }
796 if (mFocused == null) {
797 super.clearFocus();
798 } else {
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700799 View focused = mFocused;
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800800 mFocused = null;
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700801 focused.clearFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803 }
804
805 /**
806 * {@inheritDoc}
807 */
808 @Override
809 void unFocus() {
810 if (DBG) {
811 System.out.println(this + " unFocus()");
812 }
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800813 if (mFocused == null) {
814 super.unFocus();
815 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 mFocused.unFocus();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800817 mFocused = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820
821 /**
822 * Returns the focused child of this view, if any. The child may have focus
823 * or contain focus.
824 *
825 * @return the focused child or null.
826 */
827 public View getFocusedChild() {
828 return mFocused;
829 }
830
831 /**
832 * Returns true if this view has or contains focus
833 *
834 * @return true if this view has or contains focus
835 */
836 @Override
837 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700838 return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
840
841 /*
842 * (non-Javadoc)
843 *
844 * @see android.view.View#findFocus()
845 */
846 @Override
847 public View findFocus() {
848 if (DBG) {
849 System.out.println("Find focus in " + this + ": flags="
850 + isFocused() + ", child=" + mFocused);
851 }
852
853 if (isFocused()) {
854 return this;
855 }
856
857 if (mFocused != null) {
858 return mFocused.findFocus();
859 }
860 return null;
861 }
862
863 /**
864 * {@inheritDoc}
865 */
866 @Override
867 public boolean hasFocusable() {
868 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
869 return false;
870 }
871
872 if (isFocusable()) {
873 return true;
874 }
875
876 final int descendantFocusability = getDescendantFocusability();
877 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
878 final int count = mChildrenCount;
879 final View[] children = mChildren;
880
881 for (int i = 0; i < count; i++) {
882 final View child = children[i];
883 if (child.hasFocusable()) {
884 return true;
885 }
886 }
887 }
888
889 return false;
890 }
891
892 /**
893 * {@inheritDoc}
894 */
895 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -0700896 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 final int focusableCount = views.size();
898
899 final int descendantFocusability = getDescendantFocusability();
900
Svetoslav Ganov27e2da72012-07-02 18:12:00 -0700901 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 final int count = mChildrenCount;
903 final View[] children = mChildren;
904
905 for (int i = 0; i < count; i++) {
906 final View child = children[i];
907 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700908 child.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910 }
911 }
912
913 // we add ourselves (if focusable) in all cases except for when we are
914 // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is
915 // to avoid the focus search finding layouts when a more precise search
916 // among the focusable children would be more interesting.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -0700917 if (descendantFocusability != FOCUS_AFTER_DESCENDANTS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 // No focusable descendants
Svetoslav Ganov27e2da72012-07-02 18:12:00 -0700919 || (focusableCount == views.size())) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700920 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
922 }
923
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700924 @Override
Svetoslav Ganovea515ae2011-09-14 18:15:32 -0700925 public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
926 super.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700927 final int childrenCount = mChildrenCount;
928 final View[] children = mChildren;
929 for (int i = 0; i < childrenCount; i++) {
930 View child = children[i];
Svetoslav Ganovea515ae2011-09-14 18:15:32 -0700931 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
Dianne Hackborn4702a852012-08-17 15:18:29 -0700932 && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -0700933 child.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700934 }
935 }
936 }
937
Svetoslav5b578da2013-05-08 14:23:32 -0700938 /** @hide */
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -0700939 @Override
Svetoslav5b578da2013-05-08 14:23:32 -0700940 public View findViewByAccessibilityIdTraversal(int accessibilityId) {
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -0700941 View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId);
942 if (foundView != null) {
943 return foundView;
944 }
945 final int childrenCount = mChildrenCount;
946 final View[] children = mChildren;
947 for (int i = 0; i < childrenCount; i++) {
948 View child = children[i];
949 foundView = child.findViewByAccessibilityIdTraversal(accessibilityId);
950 if (foundView != null) {
951 return foundView;
952 }
953 }
954 return null;
955 }
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 /**
958 * {@inheritDoc}
959 */
960 @Override
961 public void dispatchWindowFocusChanged(boolean hasFocus) {
962 super.dispatchWindowFocusChanged(hasFocus);
963 final int count = mChildrenCount;
964 final View[] children = mChildren;
965 for (int i = 0; i < count; i++) {
966 children[i].dispatchWindowFocusChanged(hasFocus);
967 }
968 }
969
970 /**
971 * {@inheritDoc}
972 */
973 @Override
974 public void addTouchables(ArrayList<View> views) {
975 super.addTouchables(views);
976
977 final int count = mChildrenCount;
978 final View[] children = mChildren;
979
980 for (int i = 0; i < count; i++) {
981 final View child = children[i];
982 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
983 child.addTouchables(views);
984 }
985 }
986 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700987
988 /**
989 * @hide
990 */
991 @Override
992 public void makeOptionalFitsSystemWindows() {
993 super.makeOptionalFitsSystemWindows();
994 final int count = mChildrenCount;
995 final View[] children = mChildren;
996 for (int i = 0; i < count; i++) {
997 children[i].makeOptionalFitsSystemWindows();
998 }
999 }
1000
Romain Guy43c9cdf2010-01-27 13:53:55 -08001001 /**
1002 * {@inheritDoc}
1003 */
1004 @Override
1005 public void dispatchDisplayHint(int hint) {
1006 super.dispatchDisplayHint(hint);
1007 final int count = mChildrenCount;
1008 final View[] children = mChildren;
1009 for (int i = 0; i < count; i++) {
1010 children[i].dispatchDisplayHint(hint);
1011 }
1012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013
1014 /**
Chet Haase0d299362012-01-26 10:51:48 -08001015 * Called when a view's visibility has changed. Notify the parent to take any appropriate
1016 * action.
1017 *
1018 * @param child The view whose visibility has changed
1019 * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
1020 * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
Chet Haase5e25c2c2010-09-16 11:15:56 -07001021 * @hide
Chet Haase5e25c2c2010-09-16 11:15:56 -07001022 */
Chet Haase0d299362012-01-26 10:51:48 -08001023 protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07001024 if (mTransition != null) {
Chet Haase0d299362012-01-26 10:51:48 -08001025 if (newVisibility == VISIBLE) {
1026 mTransition.showChild(this, child, oldVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001027 } else {
Chet Haase0d299362012-01-26 10:51:48 -08001028 mTransition.hideChild(this, child, newVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001029 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
Chet Haaseddbb3462012-06-19 13:54:29 -07001030 // Only track this on disappearing views - appearing views are already visible
1031 // and don't need special handling during drawChild()
1032 if (mVisibilityChangingChildren == null) {
1033 mVisibilityChangingChildren = new ArrayList<View>();
1034 }
1035 mVisibilityChangingChildren.add(child);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001036 addDisappearingView(child);
1037 }
1038 }
1039 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001040
1041 // in all cases, for drags
1042 if (mCurrentDrag != null) {
Chet Haase0d299362012-01-26 10:51:48 -08001043 if (newVisibility == VISIBLE) {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001044 notifyChildOfDrag(child);
1045 }
1046 }
Chet Haase5e25c2c2010-09-16 11:15:56 -07001047 }
1048
1049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 * {@inheritDoc}
1051 */
1052 @Override
Adam Powell326d8082009-12-09 15:10:07 -08001053 protected void dispatchVisibilityChanged(View changedView, int visibility) {
1054 super.dispatchVisibilityChanged(changedView, visibility);
1055 final int count = mChildrenCount;
1056 final View[] children = mChildren;
1057 for (int i = 0; i < count; i++) {
1058 children[i].dispatchVisibilityChanged(changedView, visibility);
1059 }
1060 }
1061
1062 /**
1063 * {@inheritDoc}
1064 */
1065 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 public void dispatchWindowVisibilityChanged(int visibility) {
1067 super.dispatchWindowVisibilityChanged(visibility);
1068 final int count = mChildrenCount;
1069 final View[] children = mChildren;
1070 for (int i = 0; i < count; i++) {
1071 children[i].dispatchWindowVisibilityChanged(visibility);
1072 }
1073 }
1074
1075 /**
1076 * {@inheritDoc}
1077 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001078 @Override
1079 public void dispatchConfigurationChanged(Configuration newConfig) {
1080 super.dispatchConfigurationChanged(newConfig);
1081 final int count = mChildrenCount;
1082 final View[] children = mChildren;
1083 for (int i = 0; i < count; i++) {
1084 children[i].dispatchConfigurationChanged(newConfig);
1085 }
1086 }
1087
1088 /**
1089 * {@inheritDoc}
1090 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -08001092 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1093 ViewParent parent = mParent;
1094 if (parent != null) parent.recomputeViewAttributes(this);
1095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
Romain Guy8506ab42009-06-11 17:35:47 -07001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001099 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1100 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1101 super.dispatchCollectViewAttributes(attachInfo, visibility);
1102 final int count = mChildrenCount;
1103 final View[] children = mChildren;
1104 for (int i = 0; i < count; i++) {
1105 final View child = children[i];
1106 child.dispatchCollectViewAttributes(attachInfo,
1107 visibility | (child.mViewFlags&VISIBILITY_MASK));
1108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110 }
1111
1112 /**
1113 * {@inheritDoc}
1114 */
1115 public void bringChildToFront(View child) {
1116 int index = indexOfChild(child);
1117 if (index >= 0) {
1118 removeFromArray(index);
1119 addInArray(child, mChildrenCount);
1120 child.mParent = this;
1121 }
1122 }
1123
1124 /**
1125 * {@inheritDoc}
Christopher Tatea53146c2010-09-07 11:57:52 -07001126 */
Steve Block8a7259b2012-03-01 11:24:41 +00001127 // TODO: Write real docs
Christopher Tatea53146c2010-09-07 11:57:52 -07001128 @Override
1129 public boolean dispatchDragEvent(DragEvent event) {
1130 boolean retval = false;
1131 final float tx = event.mX;
1132 final float ty = event.mY;
1133
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07001134 ViewRootImpl root = getViewRootImpl();
Christopher Tatea53146c2010-09-07 11:57:52 -07001135
1136 // Dispatch down the view hierarchy
1137 switch (event.mAction) {
1138 case DragEvent.ACTION_DRAG_STARTED: {
1139 // clear state to recalculate which views we drag over
Chris Tate9d1ab882010-11-02 15:55:39 -07001140 mCurrentDragView = null;
Christopher Tatea53146c2010-09-07 11:57:52 -07001141
Christopher Tate86cab1b2011-01-13 20:28:55 -08001142 // Set up our tracking of drag-started notifications
1143 mCurrentDrag = DragEvent.obtain(event);
1144 if (mDragNotifiedChildren == null) {
1145 mDragNotifiedChildren = new HashSet<View>();
1146 } else {
1147 mDragNotifiedChildren.clear();
1148 }
1149
Christopher Tatea53146c2010-09-07 11:57:52 -07001150 // Now dispatch down to our children, caching the responses
1151 mChildAcceptsDrag = false;
1152 final int count = mChildrenCount;
1153 final View[] children = mChildren;
1154 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001155 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001156 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001157 if (child.getVisibility() == VISIBLE) {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001158 final boolean handled = notifyChildOfDrag(children[i]);
Christopher Tate2c095f32010-10-04 14:13:40 -07001159 if (handled) {
1160 mChildAcceptsDrag = true;
1161 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001162 }
1163 }
1164
1165 // Return HANDLED if one of our children can accept the drag
1166 if (mChildAcceptsDrag) {
1167 retval = true;
1168 }
1169 } break;
1170
1171 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001172 // Release the bookkeeping now that the drag lifecycle has ended
Christopher Tate1fc014f2011-01-19 12:56:26 -08001173 if (mDragNotifiedChildren != null) {
1174 for (View child : mDragNotifiedChildren) {
1175 // If a child was notified about an ongoing drag, it's told that it's over
1176 child.dispatchDragEvent(event);
Christopher Tate3d4bf172011-03-28 16:16:46 -07001177 child.mPrivateFlags2 &= ~View.DRAG_MASK;
1178 child.refreshDrawableState();
Christopher Tate1fc014f2011-01-19 12:56:26 -08001179 }
1180
1181 mDragNotifiedChildren.clear();
Christopher Tate267097b2013-03-04 12:57:23 -08001182 if (mCurrentDrag != null) {
1183 mCurrentDrag.recycle();
1184 mCurrentDrag = null;
1185 }
Christopher Tate1fc014f2011-01-19 12:56:26 -08001186 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001187
Christopher Tatea53146c2010-09-07 11:57:52 -07001188 // We consider drag-ended to have been handled if one of our children
1189 // had offered to handle the drag.
1190 if (mChildAcceptsDrag) {
1191 retval = true;
1192 }
1193 } break;
1194
1195 case DragEvent.ACTION_DRAG_LOCATION: {
1196 // Find the [possibly new] drag target
1197 final View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
1198
1199 // If we've changed apparent drag target, tell the view root which view
Chris Tate9d1ab882010-11-02 15:55:39 -07001200 // we're over now [for purposes of the eventual drag-recipient-changed
1201 // notifications to the framework] and tell the new target that the drag
1202 // has entered its bounds. The root will see setDragFocus() calls all
1203 // the way down to the final leaf view that is handling the LOCATION event
1204 // before reporting the new potential recipient to the framework.
Christopher Tatea53146c2010-09-07 11:57:52 -07001205 if (mCurrentDragView != target) {
Chris Tate9d1ab882010-11-02 15:55:39 -07001206 root.setDragFocus(target);
1207
1208 final int action = event.mAction;
1209 // If we've dragged off of a child view, send it the EXITED message
1210 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001211 final View view = mCurrentDragView;
Chris Tate9d1ab882010-11-02 15:55:39 -07001212 event.mAction = DragEvent.ACTION_DRAG_EXITED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001213 view.dispatchDragEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001214 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001215 view.refreshDrawableState();
Chris Tate9d1ab882010-11-02 15:55:39 -07001216 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001217 mCurrentDragView = target;
Chris Tate9d1ab882010-11-02 15:55:39 -07001218
1219 // If we've dragged over a new child view, send it the ENTERED message
1220 if (target != null) {
1221 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
1222 target.dispatchDragEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001223 target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001224 target.refreshDrawableState();
Chris Tate9d1ab882010-11-02 15:55:39 -07001225 }
1226 event.mAction = action; // restore the event's original state
Christopher Tatea53146c2010-09-07 11:57:52 -07001227 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001228
Christopher Tatea53146c2010-09-07 11:57:52 -07001229 // Dispatch the actual drag location notice, localized into its coordinates
1230 if (target != null) {
1231 event.mX = mLocalPoint.x;
1232 event.mY = mLocalPoint.y;
1233
1234 retval = target.dispatchDragEvent(event);
1235
1236 event.mX = tx;
1237 event.mY = ty;
1238 }
1239 } break;
1240
Chris Tate9d1ab882010-11-02 15:55:39 -07001241 /* Entered / exited dispatch
1242 *
1243 * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is
1244 * that we're about to get the corresponding LOCATION event, which we will use to
1245 * determine which of our children is the new target; at that point we will
1246 * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
1247 *
1248 * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1249 * drag has left this ViewGroup, we know by definition that every contained subview
1250 * is also no longer under the drag point.
1251 */
1252
1253 case DragEvent.ACTION_DRAG_EXITED: {
1254 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001255 final View view = mCurrentDragView;
1256 view.dispatchDragEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001257 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001258 view.refreshDrawableState();
1259
Chris Tate9d1ab882010-11-02 15:55:39 -07001260 mCurrentDragView = null;
1261 }
1262 } break;
1263
Christopher Tatea53146c2010-09-07 11:57:52 -07001264 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -07001265 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001266 View target = findFrontmostDroppableChildAt(event.mX, event.mY, mLocalPoint);
1267 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001268 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Christopher Tatea53146c2010-09-07 11:57:52 -07001269 event.mX = mLocalPoint.x;
1270 event.mY = mLocalPoint.y;
1271 retval = target.dispatchDragEvent(event);
1272 event.mX = tx;
1273 event.mY = ty;
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001274 } else {
1275 if (ViewDebug.DEBUG_DRAG) {
1276 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
1277 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001278 }
1279 } break;
1280 }
1281
1282 // If none of our children could handle the event, try here
1283 if (!retval) {
Chris Tate32affef2010-10-18 15:29:21 -07001284 // Call up to the View implementation that dispatches to installed listeners
1285 retval = super.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001286 }
1287 return retval;
1288 }
1289
1290 // Find the frontmost child view that lies under the given point, and calculate
1291 // the position within its own local coordinate system.
1292 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001293 final int count = mChildrenCount;
1294 final View[] children = mChildren;
1295 for (int i = count - 1; i >= 0; i--) {
1296 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001297 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001298 continue;
1299 }
1300
Christopher Tate2c095f32010-10-04 14:13:40 -07001301 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001302 return child;
1303 }
1304 }
1305 return null;
1306 }
1307
Christopher Tate86cab1b2011-01-13 20:28:55 -08001308 boolean notifyChildOfDrag(View child) {
1309 if (ViewDebug.DEBUG_DRAG) {
1310 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1311 }
1312
Christopher Tate3d4bf172011-03-28 16:16:46 -07001313 boolean canAccept = false;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001314 if (! mDragNotifiedChildren.contains(child)) {
1315 mDragNotifiedChildren.add(child);
Christopher Tate3d4bf172011-03-28 16:16:46 -07001316 canAccept = child.dispatchDragEvent(mCurrentDrag);
1317 if (canAccept && !child.canAcceptDrag()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001318 child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001319 child.refreshDrawableState();
1320 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001321 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001322 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001323 }
1324
Joe Onorato664644d2011-01-23 17:53:23 -08001325 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001326 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1327 super.dispatchWindowSystemUiVisiblityChanged(visible);
1328
1329 final int count = mChildrenCount;
1330 final View[] children = mChildren;
1331 for (int i=0; i <count; i++) {
1332 final View child = children[i];
1333 child.dispatchWindowSystemUiVisiblityChanged(visible);
1334 }
1335 }
1336
1337 @Override
Joe Onorato664644d2011-01-23 17:53:23 -08001338 public void dispatchSystemUiVisibilityChanged(int visible) {
1339 super.dispatchSystemUiVisibilityChanged(visible);
1340
1341 final int count = mChildrenCount;
1342 final View[] children = mChildren;
1343 for (int i=0; i <count; i++) {
1344 final View child = children[i];
1345 child.dispatchSystemUiVisibilityChanged(visible);
1346 }
1347 }
1348
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001349 @Override
Dianne Hackborncf675782012-05-10 15:07:24 -07001350 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
1351 boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001352
1353 final int count = mChildrenCount;
1354 final View[] children = mChildren;
1355 for (int i=0; i <count; i++) {
1356 final View child = children[i];
Dianne Hackborncf675782012-05-10 15:07:24 -07001357 changed |= child.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001358 }
Dianne Hackborncf675782012-05-10 15:07:24 -07001359 return changed;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001360 }
1361
Christopher Tatea53146c2010-09-07 11:57:52 -07001362 /**
1363 * {@inheritDoc}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 */
1365 @Override
1366 public boolean dispatchKeyEventPreIme(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001367 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1368 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 return super.dispatchKeyEventPreIme(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001370 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1371 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 return mFocused.dispatchKeyEventPreIme(event);
1373 }
1374 return false;
1375 }
1376
1377 /**
1378 * {@inheritDoc}
1379 */
1380 @Override
1381 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001382 if (mInputEventConsistencyVerifier != null) {
1383 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1384 }
1385
Dianne Hackborn4702a852012-08-17 15:18:29 -07001386 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1387 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001388 if (super.dispatchKeyEvent(event)) {
1389 return true;
1390 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001391 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1392 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001393 if (mFocused.dispatchKeyEvent(event)) {
1394 return true;
1395 }
1396 }
1397
1398 if (mInputEventConsistencyVerifier != null) {
1399 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401 return false;
1402 }
1403
1404 /**
1405 * {@inheritDoc}
1406 */
1407 @Override
1408 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001409 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1410 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 return super.dispatchKeyShortcutEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001412 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1413 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 return mFocused.dispatchKeyShortcutEvent(event);
1415 }
1416 return false;
1417 }
1418
1419 /**
1420 * {@inheritDoc}
1421 */
1422 @Override
1423 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001424 if (mInputEventConsistencyVerifier != null) {
1425 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1426 }
1427
Dianne Hackborn4702a852012-08-17 15:18:29 -07001428 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1429 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001430 if (super.dispatchTrackballEvent(event)) {
1431 return true;
1432 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001433 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1434 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001435 if (mFocused.dispatchTrackballEvent(event)) {
1436 return true;
1437 }
1438 }
1439
1440 if (mInputEventConsistencyVerifier != null) {
1441 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
1443 return false;
1444 }
1445
Jeff Brown10b62902011-06-20 16:40:37 -07001446 /**
1447 * {@inheritDoc}
1448 */
Romain Guya9489272011-06-22 20:58:11 -07001449 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08001451 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08001452 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08001453
Jeff Brown10b62902011-06-20 16:40:37 -07001454 // First check whether the view group wants to intercept the hover event.
1455 final boolean interceptHover = onInterceptHoverEvent(event);
1456 event.setAction(action); // restore action in case it was changed
1457
Jeff Brown87b7f802011-06-21 18:35:45 -07001458 MotionEvent eventNoHistory = event;
1459 boolean handled = false;
1460
1461 // Send events to the hovered children and build a new list of hover targets until
1462 // one is found that handles the event.
1463 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1464 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07001465 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08001466 final float x = event.getX();
1467 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001468 final int childrenCount = mChildrenCount;
1469 if (childrenCount != 0) {
Svetoslav0e5e9aa2013-04-12 14:13:20 -07001470 final boolean customChildOrder = isChildrenDrawingOrderEnabled();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001471 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07001472 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001473 for (int i = childrenCount - 1; i >= 0; i--) {
Svetoslav0e5e9aa2013-04-12 14:13:20 -07001474 final int childIndex = customChildOrder
1475 ? getChildDrawingOrder(childrenCount, i) : i;
1476 final View child = children[childIndex];
Jeff Brown87b7f802011-06-21 18:35:45 -07001477 if (!canViewReceivePointerEvents(child)
1478 || !isTransformedTouchPointInView(x, y, child, null)) {
1479 continue;
1480 }
1481
1482 // Obtain a hover target for this child. Dequeue it from the
1483 // old hover target list if the child was previously hovered.
1484 HoverTarget hoverTarget = firstOldHoverTarget;
1485 final boolean wasHovered;
1486 for (HoverTarget predecessor = null; ;) {
1487 if (hoverTarget == null) {
1488 hoverTarget = HoverTarget.obtain(child);
1489 wasHovered = false;
1490 break;
1491 }
1492
1493 if (hoverTarget.child == child) {
1494 if (predecessor != null) {
1495 predecessor.next = hoverTarget.next;
1496 } else {
1497 firstOldHoverTarget = hoverTarget.next;
1498 }
1499 hoverTarget.next = null;
1500 wasHovered = true;
1501 break;
1502 }
1503
1504 predecessor = hoverTarget;
1505 hoverTarget = hoverTarget.next;
1506 }
1507
1508 // Enqueue the hover target onto the new hover target list.
1509 if (lastHoverTarget != null) {
1510 lastHoverTarget.next = hoverTarget;
1511 } else {
Jeff Brown87b7f802011-06-21 18:35:45 -07001512 mFirstHoverTarget = hoverTarget;
1513 }
Sangkyu Lee8725f362013-03-13 09:38:45 +09001514 lastHoverTarget = hoverTarget;
Jeff Brown87b7f802011-06-21 18:35:45 -07001515
1516 // Dispatch the event to the child.
1517 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1518 if (!wasHovered) {
1519 // Send the enter as is.
1520 handled |= dispatchTransformedGenericPointerEvent(
1521 event, child); // enter
1522 }
1523 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1524 if (!wasHovered) {
1525 // Synthesize an enter from a move.
1526 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1527 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1528 handled |= dispatchTransformedGenericPointerEvent(
1529 eventNoHistory, child); // enter
1530 eventNoHistory.setAction(action);
1531
1532 handled |= dispatchTransformedGenericPointerEvent(
1533 eventNoHistory, child); // move
1534 } else {
1535 // Send the move as is.
1536 handled |= dispatchTransformedGenericPointerEvent(event, child);
1537 }
1538 }
1539 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07001540 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001541 }
Jeff Brown10b62902011-06-20 16:40:37 -07001542 }
1543 }
1544 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001545
Jeff Brown87b7f802011-06-21 18:35:45 -07001546 // Send exit events to all previously hovered children that are no longer hovered.
1547 while (firstOldHoverTarget != null) {
1548 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07001549
Jeff Brown87b7f802011-06-21 18:35:45 -07001550 // Exit the old hovered child.
1551 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1552 // Send the exit as is.
1553 handled |= dispatchTransformedGenericPointerEvent(
1554 event, child); // exit
1555 } else {
1556 // Synthesize an exit from a move or enter.
1557 // Ignore the result because hover focus has moved to a different view.
1558 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brown10b62902011-06-20 16:40:37 -07001559 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07001560 event, child); // move
Jeff Brown10b62902011-06-20 16:40:37 -07001561 }
Jeff Brown87b7f802011-06-21 18:35:45 -07001562 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1563 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1564 dispatchTransformedGenericPointerEvent(
1565 eventNoHistory, child); // exit
1566 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07001567 }
1568
Jeff Brown87b7f802011-06-21 18:35:45 -07001569 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1570 firstOldHoverTarget.recycle();
1571 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07001572 }
1573
Jeff Brown87b7f802011-06-21 18:35:45 -07001574 // Send events to the view group itself if no children have handled it.
Jeff Brown10b62902011-06-20 16:40:37 -07001575 boolean newHoveredSelf = !handled;
1576 if (newHoveredSelf == mHoveredSelf) {
1577 if (newHoveredSelf) {
1578 // Send event to the view group as before.
1579 handled |= super.dispatchHoverEvent(event);
1580 }
1581 } else {
1582 if (mHoveredSelf) {
1583 // Exit the view group.
1584 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1585 // Send the exit as is.
1586 handled |= super.dispatchHoverEvent(event); // exit
1587 } else {
1588 // Synthesize an exit from a move or enter.
1589 // Ignore the result because hover focus is moving to a different view.
1590 if (action == MotionEvent.ACTION_HOVER_MOVE) {
1591 super.dispatchHoverEvent(event); // move
1592 }
1593 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1594 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1595 super.dispatchHoverEvent(eventNoHistory); // exit
1596 eventNoHistory.setAction(action);
1597 }
1598 mHoveredSelf = false;
1599 }
1600
1601 if (newHoveredSelf) {
1602 // Enter the view group.
1603 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1604 // Send the enter as is.
1605 handled |= super.dispatchHoverEvent(event); // enter
1606 mHoveredSelf = true;
1607 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1608 // Synthesize an enter from a move.
1609 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1610 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1611 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1612 eventNoHistory.setAction(action);
1613
1614 handled |= super.dispatchHoverEvent(eventNoHistory); // move
1615 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001616 }
1617 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001618 }
1619
Jeff Browna032cc02011-03-07 16:56:21 -08001620 // Recycle the copy of the event that we made.
1621 if (eventNoHistory != event) {
1622 eventNoHistory.recycle();
1623 }
1624
Jeff Browna032cc02011-03-07 16:56:21 -08001625 // Done.
1626 return handled;
1627 }
1628
Jeff Brown59a422e2012-04-19 15:19:19 -07001629 private void exitHoverTargets() {
1630 if (mHoveredSelf || mFirstHoverTarget != null) {
1631 final long now = SystemClock.uptimeMillis();
1632 MotionEvent event = MotionEvent.obtain(now, now,
1633 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1634 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1635 dispatchHoverEvent(event);
1636 event.recycle();
1637 }
1638 }
1639
1640 private void cancelHoverTarget(View view) {
1641 HoverTarget predecessor = null;
1642 HoverTarget target = mFirstHoverTarget;
1643 while (target != null) {
1644 final HoverTarget next = target.next;
1645 if (target.child == view) {
1646 if (predecessor == null) {
1647 mFirstHoverTarget = next;
1648 } else {
1649 predecessor.next = next;
1650 }
1651 target.recycle();
1652
1653 final long now = SystemClock.uptimeMillis();
1654 MotionEvent event = MotionEvent.obtain(now, now,
1655 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1656 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1657 view.dispatchHoverEvent(event);
1658 event.recycle();
1659 return;
1660 }
1661 predecessor = target;
1662 target = next;
1663 }
1664 }
1665
Jeff Brown87b7f802011-06-21 18:35:45 -07001666 /** @hide */
1667 @Override
1668 protected boolean hasHoveredChild() {
1669 return mFirstHoverTarget != null;
1670 }
1671
Svetoslav Ganov42138042012-03-20 11:51:39 -07001672 @Override
1673 public void addChildrenForAccessibility(ArrayList<View> childrenForAccessibility) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001674 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
1675 try {
1676 final int childrenCount = children.getChildCount();
1677 for (int i = 0; i < childrenCount; i++) {
1678 View child = children.getChildAt(i);
Svetoslav Ganovc406be92012-05-11 16:12:32 -07001679 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001680 if (child.includeForAccessibility()) {
1681 childrenForAccessibility.add(child);
1682 } else {
1683 child.addChildrenForAccessibility(childrenForAccessibility);
1684 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001685 }
1686 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001687 } finally {
1688 children.recycle();
Svetoslav Ganov42138042012-03-20 11:51:39 -07001689 }
1690 }
1691
1692 /**
1693 * @hide
1694 */
1695 @Override
1696 public void childAccessibilityStateChanged(View child) {
1697 if (mParent != null) {
1698 mParent.childAccessibilityStateChanged(child);
1699 }
1700 }
1701
Jeff Brown10b62902011-06-20 16:40:37 -07001702 /**
1703 * Implement this method to intercept hover events before they are handled
1704 * by child views.
1705 * <p>
1706 * This method is called before dispatching a hover event to a child of
1707 * the view group or to the view group's own {@link #onHoverEvent} to allow
1708 * the view group a chance to intercept the hover event.
1709 * This method can also be used to watch all pointer motions that occur within
1710 * the bounds of the view group even when the pointer is hovering over
1711 * a child of the view group rather than over the view group itself.
1712 * </p><p>
1713 * The view group can prevent its children from receiving hover events by
1714 * implementing this method and returning <code>true</code> to indicate
1715 * that it would like to intercept hover events. The view group must
1716 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
1717 * for as long as it wishes to continue intercepting hover events from
1718 * its children.
1719 * </p><p>
1720 * Interception preserves the invariant that at most one view can be
1721 * hovered at a time by transferring hover focus from the currently hovered
1722 * child to the view group or vice-versa as needed.
1723 * </p><p>
1724 * If this method returns <code>true</code> and a child is already hovered, then the
1725 * child view will first receive a hover exit event and then the view group
1726 * itself will receive a hover enter event in {@link #onHoverEvent}.
1727 * Likewise, if this method had previously returned <code>true</code> to intercept hover
1728 * events and instead returns <code>false</code> while the pointer is hovering
1729 * within the bounds of one of a child, then the view group will first receive a
1730 * hover exit event in {@link #onHoverEvent} and then the hovered child will
1731 * receive a hover enter event.
1732 * </p><p>
1733 * The default implementation always returns false.
1734 * </p>
1735 *
1736 * @param event The motion event that describes the hover.
1737 * @return True if the view group would like to intercept the hover event
1738 * and prevent its children from receiving it.
1739 */
1740 public boolean onInterceptHoverEvent(MotionEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001741 return false;
1742 }
1743
Jeff Browna032cc02011-03-07 16:56:21 -08001744 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
1745 if (event.getHistorySize() == 0) {
1746 return event;
1747 }
1748 return MotionEvent.obtainNoHistory(event);
1749 }
1750
Jeff Brown10b62902011-06-20 16:40:37 -07001751 /**
1752 * {@inheritDoc}
1753 */
Jeff Browna032cc02011-03-07 16:56:21 -08001754 @Override
1755 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
1756 // Send the event to the child under the pointer.
1757 final int childrenCount = mChildrenCount;
1758 if (childrenCount != 0) {
1759 final View[] children = mChildren;
1760 final float x = event.getX();
1761 final float y = event.getY();
1762
Adam Powella6478a32012-08-17 16:40:00 -07001763 final boolean customOrder = isChildrenDrawingOrderEnabled();
Jeff Browna032cc02011-03-07 16:56:21 -08001764 for (int i = childrenCount - 1; i >= 0; i--) {
Adam Powella6478a32012-08-17 16:40:00 -07001765 final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
1766 final View child = children[childIndex];
Jeff Browna032cc02011-03-07 16:56:21 -08001767 if (!canViewReceivePointerEvents(child)
1768 || !isTransformedTouchPointInView(x, y, child, null)) {
1769 continue;
1770 }
1771
1772 if (dispatchTransformedGenericPointerEvent(event, child)) {
1773 return true;
1774 }
1775 }
1776 }
1777
1778 // No child handled the event. Send it to this view group.
1779 return super.dispatchGenericPointerEvent(event);
1780 }
1781
Jeff Brown10b62902011-06-20 16:40:37 -07001782 /**
1783 * {@inheritDoc}
1784 */
Jeff Browna032cc02011-03-07 16:56:21 -08001785 @Override
1786 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001787 // Send the event to the focused child or to this view group if it has focus.
Dianne Hackborn4702a852012-08-17 15:18:29 -07001788 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1789 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08001790 return super.dispatchGenericFocusedEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001791 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1792 == PFLAG_HAS_BOUNDS) {
Jeff Browncb1404e2011-01-15 18:14:15 -08001793 return mFocused.dispatchGenericMotionEvent(event);
1794 }
1795 return false;
1796 }
1797
1798 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001799 * Dispatches a generic pointer event to a child, taking into account
1800 * transformations that apply to the child.
1801 *
1802 * @param event The event to send.
1803 * @param child The view to send the event to.
1804 * @return {@code true} if the child handled the event.
1805 */
1806 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
1807 final float offsetX = mScrollX - child.mLeft;
1808 final float offsetY = mScrollY - child.mTop;
1809
1810 boolean handled;
1811 if (!child.hasIdentityMatrix()) {
1812 MotionEvent transformedEvent = MotionEvent.obtain(event);
1813 transformedEvent.offsetLocation(offsetX, offsetY);
1814 transformedEvent.transform(child.getInverseMatrix());
1815 handled = child.dispatchGenericMotionEvent(transformedEvent);
1816 transformedEvent.recycle();
1817 } else {
1818 event.offsetLocation(offsetX, offsetY);
1819 handled = child.dispatchGenericMotionEvent(event);
1820 event.offsetLocation(-offsetX, -offsetY);
1821 }
1822 return handled;
1823 }
1824
1825 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08001826 * {@inheritDoc}
1827 */
1828 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001830 if (mInputEventConsistencyVerifier != null) {
1831 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
1832 }
1833
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001834 boolean handled = false;
1835 if (onFilterTouchEventForSecurity(ev)) {
1836 final int action = ev.getAction();
1837 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07001838
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001839 // Handle an initial down.
1840 if (actionMasked == MotionEvent.ACTION_DOWN) {
1841 // Throw away all previous state when starting a new touch gesture.
1842 // The framework may have dropped the up or cancel event for the previous gesture
1843 // due to an app switch, ANR, or some other state change.
1844 cancelAndClearTouchTargets(ev);
1845 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
Adam Powellb08013c2010-09-16 16:28:11 -07001847
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001848 // Check for interception.
1849 final boolean intercepted;
Jeff Brown20e987b2010-08-23 12:01:02 -07001850 if (actionMasked == MotionEvent.ACTION_DOWN
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001851 || mFirstTouchTarget != null) {
1852 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
1853 if (!disallowIntercept) {
1854 intercepted = onInterceptTouchEvent(ev);
1855 ev.setAction(action); // restore action in case it was changed
1856 } else {
1857 intercepted = false;
1858 }
1859 } else {
1860 // There are no touch targets and this action is not an initial down
1861 // so this view group continues to intercept touches.
1862 intercepted = true;
1863 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001864
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001865 // Check for cancelation.
1866 final boolean canceled = resetCancelNextUpFlag(this)
1867 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07001868
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001869 // Update list of touch targets for pointer down, if needed.
1870 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
1871 TouchTarget newTouchTarget = null;
1872 boolean alreadyDispatchedToNewTouchTarget = false;
1873 if (!canceled && !intercepted) {
1874 if (actionMasked == MotionEvent.ACTION_DOWN
1875 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
1876 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
1877 final int actionIndex = ev.getActionIndex(); // always 0 for down
1878 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
1879 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07001880
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001881 // Clean up earlier touch targets for this pointer id in case they
1882 // have become out of sync.
1883 removePointersFromTouchTargets(idBitsToAssign);
1884
1885 final int childrenCount = mChildrenCount;
Chet Haase9c17fe62013-03-22 17:05:55 -07001886 if (newTouchTarget == null && childrenCount != 0) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07001887 final float x = ev.getX(actionIndex);
1888 final float y = ev.getY(actionIndex);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001889 // Find a child that can receive the event.
1890 // Scan children from front to back.
1891 final View[] children = mChildren;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001892
Adam Powella6478a32012-08-17 16:40:00 -07001893 final boolean customOrder = isChildrenDrawingOrderEnabled();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001894 for (int i = childrenCount - 1; i >= 0; i--) {
Adam Powella6478a32012-08-17 16:40:00 -07001895 final int childIndex = customOrder ?
1896 getChildDrawingOrder(childrenCount, i) : i;
1897 final View child = children[childIndex];
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001898 if (!canViewReceivePointerEvents(child)
1899 || !isTransformedTouchPointInView(x, y, child, null)) {
1900 continue;
1901 }
1902
1903 newTouchTarget = getTouchTarget(child);
1904 if (newTouchTarget != null) {
1905 // Child is already receiving touch within its bounds.
1906 // Give it the new pointer in addition to the ones it is handling.
1907 newTouchTarget.pointerIdBits |= idBitsToAssign;
1908 break;
1909 }
1910
1911 resetCancelNextUpFlag(child);
1912 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
1913 // Child wants to receive touch within its bounds.
1914 mLastTouchDownTime = ev.getDownTime();
Adam Powella6478a32012-08-17 16:40:00 -07001915 mLastTouchDownIndex = childIndex;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001916 mLastTouchDownX = ev.getX();
1917 mLastTouchDownY = ev.getY();
1918 newTouchTarget = addTouchTarget(child, idBitsToAssign);
1919 alreadyDispatchedToNewTouchTarget = true;
1920 break;
1921 }
1922 }
1923 }
1924
1925 if (newTouchTarget == null && mFirstTouchTarget != null) {
1926 // Did not find a child to receive the event.
1927 // Assign the pointer to the least recently added target.
1928 newTouchTarget = mFirstTouchTarget;
1929 while (newTouchTarget.next != null) {
1930 newTouchTarget = newTouchTarget.next;
1931 }
1932 newTouchTarget.pointerIdBits |= idBitsToAssign;
1933 }
1934 }
1935 }
1936
1937 // Dispatch to touch targets.
1938 if (mFirstTouchTarget == null) {
1939 // No touch targets so treat this as an ordinary view.
1940 handled = dispatchTransformedTouchEvent(ev, canceled, null,
1941 TouchTarget.ALL_POINTER_IDS);
1942 } else {
1943 // Dispatch to touch targets, excluding the new touch target if we already
1944 // dispatched to it. Cancel touch targets if necessary.
1945 TouchTarget predecessor = null;
1946 TouchTarget target = mFirstTouchTarget;
1947 while (target != null) {
1948 final TouchTarget next = target.next;
1949 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
1950 handled = true;
1951 } else {
1952 final boolean cancelChild = resetCancelNextUpFlag(target.child)
Chet Haase9c17fe62013-03-22 17:05:55 -07001953 || intercepted;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001954 if (dispatchTransformedTouchEvent(ev, cancelChild,
1955 target.child, target.pointerIdBits)) {
1956 handled = true;
1957 }
1958 if (cancelChild) {
1959 if (predecessor == null) {
1960 mFirstTouchTarget = next;
1961 } else {
1962 predecessor.next = next;
1963 }
1964 target.recycle();
1965 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07001966 continue;
1967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001969 predecessor = target;
1970 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001972 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001973
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001974 // Update list of touch targets for pointer up or cancel, if needed.
1975 if (canceled
1976 || actionMasked == MotionEvent.ACTION_UP
1977 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
1978 resetTouchState();
1979 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
1980 final int actionIndex = ev.getActionIndex();
1981 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
1982 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984 }
Romain Guy8506ab42009-06-11 17:35:47 -07001985
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001986 if (!handled && mInputEventConsistencyVerifier != null) {
1987 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07001988 }
Jeff Brown20e987b2010-08-23 12:01:02 -07001989 return handled;
1990 }
1991
Romain Guy469b1db2010-10-05 11:49:57 -07001992 /**
1993 * Resets all touch state in preparation for a new cycle.
1994 */
1995 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07001996 clearTouchTargets();
1997 resetCancelNextUpFlag(this);
1998 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
1999 }
2000
Romain Guy469b1db2010-10-05 11:49:57 -07002001 /**
2002 * Resets the cancel next up flag.
2003 * Returns true if the flag was previously set.
2004 */
Romain Guya998dff2012-03-23 18:58:36 -07002005 private static boolean resetCancelNextUpFlag(View view) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002006 if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) {
2007 view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Jeff Brown20e987b2010-08-23 12:01:02 -07002008 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07002009 }
2010 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
2012
Romain Guy469b1db2010-10-05 11:49:57 -07002013 /**
2014 * Clears all touch targets.
2015 */
2016 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002017 TouchTarget target = mFirstTouchTarget;
2018 if (target != null) {
2019 do {
2020 TouchTarget next = target.next;
2021 target.recycle();
2022 target = next;
2023 } while (target != null);
2024 mFirstTouchTarget = null;
2025 }
2026 }
2027
Romain Guy469b1db2010-10-05 11:49:57 -07002028 /**
2029 * Cancels and clears all touch targets.
2030 */
2031 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002032 if (mFirstTouchTarget != null) {
2033 boolean syntheticEvent = false;
2034 if (event == null) {
2035 final long now = SystemClock.uptimeMillis();
2036 event = MotionEvent.obtain(now, now,
2037 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07002038 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07002039 syntheticEvent = true;
2040 }
2041
2042 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2043 resetCancelNextUpFlag(target.child);
2044 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
2045 }
2046 clearTouchTargets();
2047
2048 if (syntheticEvent) {
2049 event.recycle();
2050 }
2051 }
2052 }
2053
Romain Guy469b1db2010-10-05 11:49:57 -07002054 /**
2055 * Gets the touch target for specified child view.
2056 * Returns null if not found.
2057 */
2058 private TouchTarget getTouchTarget(View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002059 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2060 if (target.child == child) {
2061 return target;
2062 }
2063 }
2064 return null;
2065 }
2066
Romain Guy469b1db2010-10-05 11:49:57 -07002067 /**
2068 * Adds a touch target for specified child to the beginning of the list.
2069 * Assumes the target child is not already present.
2070 */
2071 private TouchTarget addTouchTarget(View child, int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002072 TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
2073 target.next = mFirstTouchTarget;
2074 mFirstTouchTarget = target;
2075 return target;
2076 }
2077
Romain Guy469b1db2010-10-05 11:49:57 -07002078 /**
2079 * Removes the pointer ids from consideration.
2080 */
2081 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002082 TouchTarget predecessor = null;
2083 TouchTarget target = mFirstTouchTarget;
2084 while (target != null) {
2085 final TouchTarget next = target.next;
2086 if ((target.pointerIdBits & pointerIdBits) != 0) {
2087 target.pointerIdBits &= ~pointerIdBits;
2088 if (target.pointerIdBits == 0) {
2089 if (predecessor == null) {
2090 mFirstTouchTarget = next;
2091 } else {
2092 predecessor.next = next;
2093 }
2094 target.recycle();
2095 target = next;
2096 continue;
2097 }
2098 }
2099 predecessor = target;
2100 target = next;
2101 }
2102 }
2103
Jeff Brown59a422e2012-04-19 15:19:19 -07002104 private void cancelTouchTarget(View view) {
2105 TouchTarget predecessor = null;
2106 TouchTarget target = mFirstTouchTarget;
2107 while (target != null) {
2108 final TouchTarget next = target.next;
2109 if (target.child == view) {
2110 if (predecessor == null) {
2111 mFirstTouchTarget = next;
2112 } else {
2113 predecessor.next = next;
2114 }
2115 target.recycle();
2116
2117 final long now = SystemClock.uptimeMillis();
2118 MotionEvent event = MotionEvent.obtain(now, now,
2119 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2120 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2121 view.dispatchTouchEvent(event);
2122 event.recycle();
2123 return;
2124 }
2125 predecessor = target;
2126 target = next;
2127 }
2128 }
2129
Romain Guy469b1db2010-10-05 11:49:57 -07002130 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002131 * Returns true if a child view can receive pointer events.
2132 * @hide
2133 */
2134 private static boolean canViewReceivePointerEvents(View child) {
2135 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2136 || child.getAnimation() != null;
2137 }
2138
2139 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002140 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07002141 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07002142 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07002143 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07002144 */
Adam Cohena32edd42010-10-26 10:35:01 -07002145 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07002146 PointF outLocalPoint) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002147 float localX = x + mScrollX - child.mLeft;
2148 float localY = y + mScrollY - child.mTop;
2149 if (! child.hasIdentityMatrix() && mAttachInfo != null) {
Adam Powell2b342f02010-08-18 18:14:13 -07002150 final float[] localXY = mAttachInfo.mTmpTransformLocation;
2151 localXY[0] = localX;
2152 localXY[1] = localY;
2153 child.getInverseMatrix().mapPoints(localXY);
2154 localX = localXY[0];
2155 localY = localXY[1];
2156 }
Christopher Tate2c095f32010-10-04 14:13:40 -07002157 final boolean isInView = child.pointInView(localX, localY);
2158 if (isInView && outLocalPoint != null) {
2159 outLocalPoint.set(localX, localY);
2160 }
2161 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07002162 }
2163
Romain Guy469b1db2010-10-05 11:49:57 -07002164 /**
2165 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07002166 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07002167 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
2168 */
2169 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07002170 View child, int desiredPointerIdBits) {
2171 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07002172
Jeff Brown20e987b2010-08-23 12:01:02 -07002173 // Canceling motions is a special case. We don't need to perform any transformations
2174 // or filtering. The important part is the action, not the contents.
2175 final int oldAction = event.getAction();
2176 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
2177 event.setAction(MotionEvent.ACTION_CANCEL);
2178 if (child == null) {
2179 handled = super.dispatchTouchEvent(event);
2180 } else {
2181 handled = child.dispatchTouchEvent(event);
2182 }
2183 event.setAction(oldAction);
2184 return handled;
2185 }
Adam Powell2b342f02010-08-18 18:14:13 -07002186
Jeff Brown20e987b2010-08-23 12:01:02 -07002187 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002188 final int oldPointerIdBits = event.getPointerIdBits();
2189 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07002190
Jeff Brown20e987b2010-08-23 12:01:02 -07002191 // If for some reason we ended up in an inconsistent state where it looks like we
2192 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002193 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002194 return false;
2195 }
Adam Powell2b342f02010-08-18 18:14:13 -07002196
Jeff Brown20e987b2010-08-23 12:01:02 -07002197 // If the number of pointers is the same and we don't need to perform any fancy
2198 // irreversible transformations, then we can reuse the motion event for this
2199 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002200 // Otherwise we need to make a copy.
2201 final MotionEvent transformedEvent;
2202 if (newPointerIdBits == oldPointerIdBits) {
2203 if (child == null || child.hasIdentityMatrix()) {
2204 if (child == null) {
2205 handled = super.dispatchTouchEvent(event);
2206 } else {
2207 final float offsetX = mScrollX - child.mLeft;
2208 final float offsetY = mScrollY - child.mTop;
2209 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07002210
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002211 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07002212
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002213 event.offsetLocation(-offsetX, -offsetY);
2214 }
2215 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07002216 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002217 transformedEvent = MotionEvent.obtain(event);
2218 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002219 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07002220 }
2221
Jeff Brown20e987b2010-08-23 12:01:02 -07002222 // Perform any necessary transformations and dispatch.
2223 if (child == null) {
2224 handled = super.dispatchTouchEvent(transformedEvent);
2225 } else {
2226 final float offsetX = mScrollX - child.mLeft;
2227 final float offsetY = mScrollY - child.mTop;
2228 transformedEvent.offsetLocation(offsetX, offsetY);
2229 if (! child.hasIdentityMatrix()) {
2230 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07002231 }
2232
Jeff Brown20e987b2010-08-23 12:01:02 -07002233 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07002234 }
2235
Jeff Brown20e987b2010-08-23 12:01:02 -07002236 // Done.
2237 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07002238 return handled;
2239 }
2240
Romain Guy469b1db2010-10-05 11:49:57 -07002241 /**
Adam Powell2b342f02010-08-18 18:14:13 -07002242 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08002243 * dispatch. This behavior is enabled by default for applications that target an
2244 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07002245 *
2246 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
2247 * views depending on where each pointer initially went down. This allows for user interactions
2248 * such as scrolling two panes of content independently, chording of buttons, and performing
2249 * independent gestures on different pieces of content.
2250 *
2251 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2252 * child views. <code>false</code> to only allow one child view to be the target of
2253 * any MotionEvent received by this ViewGroup.
Scott Main27a85082013-06-10 10:39:48 -07002254 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
Adam Powell2b342f02010-08-18 18:14:13 -07002255 */
2256 public void setMotionEventSplittingEnabled(boolean split) {
2257 // TODO Applications really shouldn't change this setting mid-touch event,
2258 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2259 // with gestures in progress when this is changed.
2260 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07002261 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2262 } else {
2263 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07002264 }
2265 }
2266
2267 /**
Jeff Brown995e7742010-12-22 16:59:36 -08002268 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07002269 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2270 */
2271 public boolean isMotionEventSplittingEnabled() {
2272 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2273 }
2274
2275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 * {@inheritDoc}
2277 */
2278 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07002279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2281 // We're already in this state, assume our ancestors are too
2282 return;
2283 }
Romain Guy8506ab42009-06-11 17:35:47 -07002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 if (disallowIntercept) {
2286 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2287 } else {
2288 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2289 }
Romain Guy8506ab42009-06-11 17:35:47 -07002290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 // Pass it up to our parent
2292 if (mParent != null) {
2293 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2294 }
2295 }
2296
2297 /**
2298 * Implement this method to intercept all touch screen motion events. This
2299 * allows you to watch events as they are dispatched to your children, and
2300 * take ownership of the current gesture at any point.
2301 *
2302 * <p>Using this function takes some care, as it has a fairly complicated
2303 * interaction with {@link View#onTouchEvent(MotionEvent)
2304 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2305 * that method as well as this one in the correct way. Events will be
2306 * received in the following order:
2307 *
2308 * <ol>
2309 * <li> You will receive the down event here.
2310 * <li> The down event will be handled either by a child of this view
2311 * group, or given to your own onTouchEvent() method to handle; this means
2312 * you should implement onTouchEvent() to return true, so you will
2313 * continue to see the rest of the gesture (instead of looking for
2314 * a parent view to handle it). Also, by returning true from
2315 * onTouchEvent(), you will not receive any following
2316 * events in onInterceptTouchEvent() and all touch processing must
2317 * happen in onTouchEvent() like normal.
2318 * <li> For as long as you return false from this function, each following
2319 * event (up to and including the final up) will be delivered first here
2320 * and then to the target's onTouchEvent().
2321 * <li> If you return true from here, you will not receive any
2322 * following events: the target view will receive the same event but
2323 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2324 * events will be delivered to your onTouchEvent() method and no longer
2325 * appear here.
2326 * </ol>
2327 *
2328 * @param ev The motion event being dispatched down the hierarchy.
2329 * @return Return true to steal motion events from the children and have
2330 * them dispatched to this ViewGroup through onTouchEvent().
2331 * The current target will receive an ACTION_CANCEL event, and no further
2332 * messages will be delivered here.
2333 */
2334 public boolean onInterceptTouchEvent(MotionEvent ev) {
2335 return false;
2336 }
2337
2338 /**
2339 * {@inheritDoc}
2340 *
2341 * Looks for a view to give focus to respecting the setting specified by
2342 * {@link #getDescendantFocusability()}.
2343 *
2344 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2345 * find focus within the children of this group when appropriate.
2346 *
2347 * @see #FOCUS_BEFORE_DESCENDANTS
2348 * @see #FOCUS_AFTER_DESCENDANTS
2349 * @see #FOCUS_BLOCK_DESCENDANTS
Romain Guy02739a82011-05-16 11:43:18 -07002350 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 */
2352 @Override
2353 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2354 if (DBG) {
2355 System.out.println(this + " ViewGroup.requestFocus direction="
2356 + direction);
2357 }
2358 int descendantFocusability = getDescendantFocusability();
2359
2360 switch (descendantFocusability) {
2361 case FOCUS_BLOCK_DESCENDANTS:
2362 return super.requestFocus(direction, previouslyFocusedRect);
2363 case FOCUS_BEFORE_DESCENDANTS: {
2364 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2365 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2366 }
2367 case FOCUS_AFTER_DESCENDANTS: {
2368 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2369 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2370 }
2371 default:
2372 throw new IllegalStateException("descendant focusability must be "
2373 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2374 + "but is " + descendantFocusability);
2375 }
2376 }
2377
2378 /**
2379 * Look for a descendant to call {@link View#requestFocus} on.
2380 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2381 * when it wants to request focus within its children. Override this to
2382 * customize how your {@link ViewGroup} requests focus within its children.
2383 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2384 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2385 * to give a finer grained hint about where focus is coming from. May be null
2386 * if there is no hint.
2387 * @return Whether focus was taken.
2388 */
2389 @SuppressWarnings({"ConstantConditions"})
2390 protected boolean onRequestFocusInDescendants(int direction,
2391 Rect previouslyFocusedRect) {
2392 int index;
2393 int increment;
2394 int end;
2395 int count = mChildrenCount;
2396 if ((direction & FOCUS_FORWARD) != 0) {
2397 index = 0;
2398 increment = 1;
2399 end = count;
2400 } else {
2401 index = count - 1;
2402 increment = -1;
2403 end = -1;
2404 }
2405 final View[] children = mChildren;
2406 for (int i = index; i != end; i += increment) {
2407 View child = children[i];
2408 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2409 if (child.requestFocus(direction, previouslyFocusedRect)) {
2410 return true;
2411 }
2412 }
2413 }
2414 return false;
2415 }
Chet Haase5c13d892010-10-08 08:37:55 -07002416
Romain Guya440b002010-02-24 15:57:54 -08002417 /**
2418 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002419 *
Romain Guydcc490f2010-02-24 17:59:35 -08002420 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002421 */
2422 @Override
2423 public void dispatchStartTemporaryDetach() {
2424 super.dispatchStartTemporaryDetach();
2425 final int count = mChildrenCount;
2426 final View[] children = mChildren;
2427 for (int i = 0; i < count; i++) {
2428 children[i].dispatchStartTemporaryDetach();
2429 }
2430 }
Chet Haase5c13d892010-10-08 08:37:55 -07002431
Romain Guya440b002010-02-24 15:57:54 -08002432 /**
2433 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002434 *
Romain Guydcc490f2010-02-24 17:59:35 -08002435 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002436 */
2437 @Override
2438 public void dispatchFinishTemporaryDetach() {
2439 super.dispatchFinishTemporaryDetach();
2440 final int count = mChildrenCount;
2441 final View[] children = mChildren;
2442 for (int i = 0; i < count; i++) {
2443 children[i].dispatchFinishTemporaryDetach();
2444 }
2445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446
2447 /**
2448 * {@inheritDoc}
2449 */
2450 @Override
2451 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07002452 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07002454 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 final int count = mChildrenCount;
2457 final View[] children = mChildren;
2458 for (int i = 0; i < count; i++) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002459 final View child = children[i];
2460 child.dispatchAttachedToWindow(info,
Philip Milne7b757812012-09-19 18:13:44 -07002461 visibility | (child.mViewFlags & VISIBILITY_MASK));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 }
2463 }
2464
svetoslavganov75986cf2009-05-14 22:28:01 -07002465 @Override
Romain Guybb9908b2012-03-08 11:14:07 -08002466 void dispatchScreenStateChanged(int screenState) {
2467 super.dispatchScreenStateChanged(screenState);
2468
2469 final int count = mChildrenCount;
2470 final View[] children = mChildren;
2471 for (int i = 0; i < count; i++) {
2472 children[i].dispatchScreenStateChanged(screenState);
2473 }
2474 }
2475
2476 @Override
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002477 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002478 boolean handled = false;
2479 if (includeForAccessibility()) {
2480 handled = super.dispatchPopulateAccessibilityEventInternal(event);
2481 if (handled) {
2482 return handled;
2483 }
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002484 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002485 // Let our children have a shot in populating the event.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002486 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002487 try {
2488 final int childCount = children.getChildCount();
2489 for (int i = 0; i < childCount; i++) {
2490 View child = children.getChildAt(i);
2491 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2492 handled = child.dispatchPopulateAccessibilityEvent(event);
2493 if (handled) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002494 return handled;
2495 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002496 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002497 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002498 } finally {
2499 children.recycle();
svetoslavganov75986cf2009-05-14 22:28:01 -07002500 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002501 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07002502 }
2503
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002504 @Override
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002505 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
2506 super.onInitializeAccessibilityNodeInfoInternal(info);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002507 if (mAttachInfo != null) {
2508 ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
2509 childrenForAccessibility.clear();
2510 addChildrenForAccessibility(childrenForAccessibility);
2511 final int childrenForAccessibilityCount = childrenForAccessibility.size();
2512 for (int i = 0; i < childrenForAccessibilityCount; i++) {
2513 View child = childrenForAccessibility.get(i);
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07002514 info.addChild(child);
2515 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002516 childrenForAccessibility.clear();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002517 }
2518 }
2519
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08002520 @Override
2521 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
2522 super.onInitializeAccessibilityEventInternal(event);
2523 event.setClassName(ViewGroup.class.getName());
2524 }
2525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07002527 * @hide
2528 */
2529 @Override
2530 public void resetAccessibilityStateChanged() {
2531 super.resetAccessibilityStateChanged();
2532 View[] children = mChildren;
2533 final int childCount = mChildrenCount;
2534 for (int i = 0; i < childCount; i++) {
2535 View child = children[i];
2536 child.resetAccessibilityStateChanged();
2537 }
2538 }
2539
2540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 * {@inheritDoc}
2542 */
2543 @Override
2544 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002545 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 // dispatching motion events to a child; we need to get rid of that
2547 // child to avoid dispatching events to it after the window is torn
2548 // down. To make sure we keep the child in a consistent state, we
2549 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07002550 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551
Jeff Brown59a422e2012-04-19 15:19:19 -07002552 // Similarly, set ACTION_EXIT to all hover targets and clear them.
2553 exitHoverTargets();
2554
Chet Haase9c087442011-01-12 16:20:16 -08002555 // In case view is detached while transition is running
Chet Haaseb9895022013-04-02 15:10:58 -07002556 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08002557
Christopher Tate86cab1b2011-01-13 20:28:55 -08002558 // Tear down our drag tracking
2559 mDragNotifiedChildren = null;
2560 if (mCurrentDrag != null) {
2561 mCurrentDrag.recycle();
2562 mCurrentDrag = null;
2563 }
2564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 final int count = mChildrenCount;
2566 final View[] children = mChildren;
2567 for (int i = 0; i < count; i++) {
2568 children[i].dispatchDetachedFromWindow();
2569 }
2570 super.dispatchDetachedFromWindow();
2571 }
2572
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002573 /**
2574 * @hide
2575 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 @Override
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002577 protected void internalSetPadding(int left, int top, int right, int bottom) {
Romain Guy2440e672012-08-07 14:43:43 -07002578 super.internalSetPadding(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579
Romain Guy13f35f32011-03-24 12:03:17 -07002580 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 mGroupFlags |= FLAG_PADDING_NOT_NULL;
2582 } else {
2583 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
2584 }
2585 }
2586
2587 /**
2588 * {@inheritDoc}
2589 */
2590 @Override
2591 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
2592 super.dispatchSaveInstanceState(container);
2593 final int count = mChildrenCount;
2594 final View[] children = mChildren;
2595 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002596 View c = children[i];
2597 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
2598 c.dispatchSaveInstanceState(container);
2599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 }
2601 }
2602
2603 /**
Romain Guy9fc27812011-04-27 14:21:41 -07002604 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
2605 * to only this view, not to its children. For use when overriding
2606 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
2607 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 *
2609 * @param container the container
2610 */
2611 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
2612 super.dispatchSaveInstanceState(container);
2613 }
2614
2615 /**
2616 * {@inheritDoc}
2617 */
2618 @Override
2619 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
2620 super.dispatchRestoreInstanceState(container);
2621 final int count = mChildrenCount;
2622 final View[] children = mChildren;
2623 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002624 View c = children[i];
2625 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
2626 c.dispatchRestoreInstanceState(container);
2627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 }
2629 }
2630
2631 /**
Romain Guy02739a82011-05-16 11:43:18 -07002632 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
2633 * to only this view, not to its children. For use when overriding
2634 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
2635 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 *
2637 * @param container the container
2638 */
2639 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
2640 super.dispatchRestoreInstanceState(container);
2641 }
2642
2643 /**
2644 * Enables or disables the drawing cache for each child of this view group.
2645 *
2646 * @param enabled true to enable the cache, false to dispose of it
2647 */
2648 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
2649 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
2650 final View[] children = mChildren;
2651 final int count = mChildrenCount;
2652 for (int i = 0; i < count; i++) {
2653 children[i].setDrawingCacheEnabled(enabled);
2654 }
2655 }
2656 }
2657
2658 @Override
2659 protected void onAnimationStart() {
2660 super.onAnimationStart();
2661
2662 // When this ViewGroup's animation starts, build the cache for the children
2663 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2664 final int count = mChildrenCount;
2665 final View[] children = mChildren;
Romain Guy0d9275e2010-10-26 14:22:30 -07002666 final boolean buildCache = !isHardwareAccelerated();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667
2668 for (int i = 0; i < count; i++) {
2669 final View child = children[i];
2670 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2671 child.setDrawingCacheEnabled(true);
Romain Guy0d9275e2010-10-26 14:22:30 -07002672 if (buildCache) {
2673 child.buildDrawingCache(true);
2674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
2676 }
2677
2678 mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
2679 }
2680 }
2681
2682 @Override
2683 protected void onAnimationEnd() {
2684 super.onAnimationEnd();
2685
2686 // When this ViewGroup's animation ends, destroy the cache of the children
2687 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
2688 mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
2689
2690 if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
2691 setChildrenDrawingCacheEnabled(false);
2692 }
2693 }
2694 }
2695
Romain Guy223ff5c2010-03-02 17:07:47 -08002696 @Override
2697 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07002698 int count = mChildrenCount;
2699 int[] visibilities = null;
2700
Romain Guy223ff5c2010-03-02 17:07:47 -08002701 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07002702 visibilities = new int[count];
2703 for (int i = 0; i < count; i++) {
2704 View child = getChildAt(i);
2705 visibilities[i] = child.getVisibility();
2706 if (visibilities[i] == View.VISIBLE) {
2707 child.setVisibility(INVISIBLE);
2708 }
2709 }
Romain Guy223ff5c2010-03-02 17:07:47 -08002710 }
2711
2712 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07002713
2714 if (skipChildren) {
2715 for (int i = 0; i < count; i++) {
2716 getChildAt(i).setVisibility(visibilities[i]);
Chet Haase5c13d892010-10-08 08:37:55 -07002717 }
Romain Guy65554f22010-03-22 18:58:21 -07002718 }
Romain Guy223ff5c2010-03-02 17:07:47 -08002719
2720 return b;
2721 }
2722
Philip Milne7b757812012-09-19 18:13:44 -07002723 /** Return true if this ViewGroup is laying out using optical bounds. */
2724 boolean isLayoutModeOptical() {
2725 return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS;
2726 }
Romain Guycbc67742012-04-27 16:12:57 -07002727
Philip Milne7b757812012-09-19 18:13:44 -07002728 Insets computeOpticalInsets() {
2729 if (isLayoutModeOptical()) {
2730 int left = 0;
2731 int top = 0;
2732 int right = 0;
2733 int bottom = 0;
2734 for (int i = 0; i < mChildrenCount; i++) {
2735 View child = getChildAt(i);
2736 if (child.getVisibility() == VISIBLE) {
2737 Insets insets = child.getOpticalInsets();
2738 left = Math.max(left, insets.left);
2739 top = Math.max(top, insets.top);
2740 right = Math.max(right, insets.right);
2741 bottom = Math.max(bottom, insets.bottom);
2742 }
2743 }
2744 return Insets.of(left, top, right, bottom);
2745 } else {
2746 return Insets.NONE;
2747 }
2748 }
2749
2750 private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
2751 if (x1 != x2 && y1 != y2) {
2752 if (x1 > x2) {
2753 int tmp = x1; x1 = x2; x2 = tmp;
2754 }
2755 if (y1 > y2) {
2756 int tmp = y1; y1 = y2; y2 = tmp;
2757 }
2758 canvas.drawRect(x1, y1, x2, y2, paint);
2759 }
2760 }
2761
2762 private static int sign(int x) {
2763 return (x >= 0) ? 1 : -1;
2764 }
2765
2766 private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) {
2767 fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy));
2768 fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
2769 }
2770
2771 private int dipsToPixels(int dips) {
2772 float scale = getContext().getResources().getDisplayMetrics().density;
2773 return (int) (dips * scale + 0.5f);
2774 }
2775
2776 private void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
2777 int lineLength, int lineWidth) {
2778 drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
2779 drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth);
2780 drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth);
2781 drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth);
2782 }
2783
2784 private static void fillDifference(Canvas canvas,
2785 int x2, int y2, int x3, int y3,
2786 int dx1, int dy1, int dx2, int dy2, Paint paint) {
2787 int x1 = x2 - dx1;
2788 int y1 = y2 - dy1;
2789
2790 int x4 = x3 + dx2;
2791 int y4 = y3 + dy2;
2792
2793 fillRect(canvas, paint, x1, y1, x4, y2);
2794 fillRect(canvas, paint, x1, y2, x2, y3);
2795 fillRect(canvas, paint, x3, y2, x4, y3);
2796 fillRect(canvas, paint, x1, y3, x4, y4);
Philip Milne10ca24a2012-04-23 15:38:27 -07002797 }
2798
2799 /**
2800 * @hide
2801 */
Philip Milne7b757812012-09-19 18:13:44 -07002802 protected void onDebugDrawMargins(Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07002803 for (int i = 0; i < getChildCount(); i++) {
2804 View c = getChildAt(i);
Philip Milne7b757812012-09-19 18:13:44 -07002805 c.getLayoutParams().onDebugDraw(c, canvas, paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07002806 }
2807 }
2808
2809 /**
2810 * @hide
2811 */
2812 protected void onDebugDraw(Canvas canvas) {
Philip Milne7b757812012-09-19 18:13:44 -07002813 Paint paint = getDebugPaint();
2814
Philip Milne10ca24a2012-04-23 15:38:27 -07002815 // Draw optical bounds
Philip Milne7b757812012-09-19 18:13:44 -07002816 {
2817 paint.setColor(Color.RED);
2818 paint.setStyle(Paint.Style.STROKE);
Philip Milne7b757812012-09-19 18:13:44 -07002819
Philip Milne10ca24a2012-04-23 15:38:27 -07002820 for (int i = 0; i < getChildCount(); i++) {
2821 View c = getChildAt(i);
Philip Milne7a23b492012-04-24 22:12:36 -07002822 Insets insets = c.getOpticalInsets();
Philip Milne7b757812012-09-19 18:13:44 -07002823
2824 drawRect(canvas, paint,
2825 c.getLeft() + insets.left,
2826 c.getTop() + insets.top,
2827 c.getRight() - insets.right - 1,
2828 c.getBottom() - insets.bottom - 1);
Philip Milne10ca24a2012-04-23 15:38:27 -07002829 }
2830 }
2831
Philip Milne10ca24a2012-04-23 15:38:27 -07002832 // Draw margins
Philip Milne7b757812012-09-19 18:13:44 -07002833 {
2834 paint.setColor(Color.argb(63, 255, 0, 255));
2835 paint.setStyle(Paint.Style.FILL);
Philip Milne604f4402012-04-24 19:27:11 -07002836
Philip Milne7b757812012-09-19 18:13:44 -07002837 onDebugDrawMargins(canvas, paint);
2838 }
2839
2840 // Draw clip bounds
2841 {
2842 paint.setColor(Color.rgb(63, 127, 255));
2843 paint.setStyle(Paint.Style.FILL);
2844
2845 int lineLength = dipsToPixels(8);
2846 int lineWidth = dipsToPixels(1);
2847 for (int i = 0; i < getChildCount(); i++) {
2848 View c = getChildAt(i);
2849 drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(),
2850 paint, lineLength, lineWidth);
2851 }
Philip Milne604f4402012-04-24 19:27:11 -07002852 }
Philip Milne10ca24a2012-04-23 15:38:27 -07002853 }
2854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 /**
2856 * {@inheritDoc}
2857 */
2858 @Override
2859 protected void dispatchDraw(Canvas canvas) {
2860 final int count = mChildrenCount;
2861 final View[] children = mChildren;
2862 int flags = mGroupFlags;
2863
2864 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
2865 final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
2866
Romain Guy0d9275e2010-10-26 14:22:30 -07002867 final boolean buildCache = !isHardwareAccelerated();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 for (int i = 0; i < count; i++) {
2869 final View child = children[i];
2870 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2871 final LayoutParams params = child.getLayoutParams();
2872 attachLayoutAnimationParameters(child, params, i, count);
2873 bindLayoutAnimation(child);
2874 if (cache) {
2875 child.setDrawingCacheEnabled(true);
Romain Guy0d9275e2010-10-26 14:22:30 -07002876 if (buildCache) {
2877 child.buildDrawingCache(true);
2878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
2880 }
2881 }
2882
2883 final LayoutAnimationController controller = mLayoutAnimationController;
2884 if (controller.willOverlap()) {
2885 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
2886 }
2887
2888 controller.start();
2889
2890 mGroupFlags &= ~FLAG_RUN_ANIMATION;
2891 mGroupFlags &= ~FLAG_ANIMATION_DONE;
2892
2893 if (cache) {
2894 mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE;
2895 }
2896
2897 if (mAnimationListener != null) {
2898 mAnimationListener.onAnimationStart(controller.getAnimation());
2899 }
2900 }
2901
2902 int saveCount = 0;
2903 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
2904 if (clipToPadding) {
2905 saveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07002906 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
2907 mScrollX + mRight - mLeft - mPaddingRight,
2908 mScrollY + mBottom - mTop - mPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
2910 }
2911
2912 // We will draw our child's animation, let's reset the flag
Dianne Hackborn4702a852012-08-17 15:18:29 -07002913 mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
2915
2916 boolean more = false;
2917 final long drawingTime = getDrawingTime();
2918
2919 if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
2920 for (int i = 0; i < count; i++) {
2921 final View child = children[i];
2922 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
2923 more |= drawChild(canvas, child, drawingTime);
2924 }
2925 }
2926 } else {
2927 for (int i = 0; i < count; i++) {
2928 final View child = children[getChildDrawingOrder(count, i)];
2929 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
2930 more |= drawChild(canvas, child, drawingTime);
2931 }
2932 }
2933 }
2934
2935 // Draw any disappearing views that have animations
2936 if (mDisappearingChildren != null) {
2937 final ArrayList<View> disappearingChildren = mDisappearingChildren;
2938 final int disappearingCount = disappearingChildren.size() - 1;
2939 // Go backwards -- we may delete as animations finish
2940 for (int i = disappearingCount; i >= 0; i--) {
2941 final View child = disappearingChildren.get(i);
2942 more |= drawChild(canvas, child, drawingTime);
2943 }
2944 }
2945
Philip Milne10ca24a2012-04-23 15:38:27 -07002946 if (debugDraw()) {
2947 onDebugDraw(canvas);
2948 }
2949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 if (clipToPadding) {
2951 canvas.restoreToCount(saveCount);
2952 }
2953
2954 // mGroupFlags might have been updated by drawChild()
2955 flags = mGroupFlags;
2956
2957 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08002958 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 }
2960
2961 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
2962 mLayoutAnimationController.isDone() && !more) {
2963 // We want to erase the drawing cache and notify the listener after the
2964 // next frame is drawn because one extra invalidate() is caused by
2965 // drawChild() after the animation is over
2966 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
2967 final Runnable end = new Runnable() {
2968 public void run() {
2969 notifyAnimationListener();
2970 }
2971 };
2972 post(end);
2973 }
2974 }
Romain Guy8506ab42009-06-11 17:35:47 -07002975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002977 * Returns the ViewGroupOverlay for this view group, creating it if it does
2978 * not yet exist. In addition to {@link ViewOverlay}'s support for drawables,
2979 * {@link ViewGroupOverlay} allows views to be added to the overlay. These
2980 * views, like overlay drawables, are visual-only; they do not receive input
2981 * events and should not be used as anything other than a temporary
2982 * representation of a view in a parent container, such as might be used
2983 * by an animation effect.
2984 *
Chet Haase95399492013-04-08 14:30:31 -07002985 * <p>Note: Overlays do not currently work correctly with {@link
2986 * SurfaceView} or {@link TextureView}; contents in overlays for these
2987 * types of views may not display correctly.</p>
2988 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002989 * @return The ViewGroupOverlay object for this view.
2990 * @see ViewGroupOverlay
2991 */
2992 @Override
2993 public ViewGroupOverlay getOverlay() {
2994 if (mOverlay == null) {
2995 mOverlay = new ViewGroupOverlay(mContext, this);
2996 }
2997 return (ViewGroupOverlay) mOverlay;
2998 }
2999
3000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 * Returns the index of the child to draw for this iteration. Override this
3002 * if you want to change the drawing order of children. By default, it
3003 * returns i.
3004 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08003005 * NOTE: In order for this method to be called, you must enable child ordering
3006 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07003007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 * @param i The current iteration.
3009 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07003010 *
Romain Guy293451e2009-11-04 13:59:48 -08003011 * @see #setChildrenDrawingOrderEnabled(boolean)
3012 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 */
3014 protected int getChildDrawingOrder(int childCount, int i) {
3015 return i;
3016 }
Romain Guy8506ab42009-06-11 17:35:47 -07003017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 private void notifyAnimationListener() {
3019 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
3020 mGroupFlags |= FLAG_ANIMATION_DONE;
3021
3022 if (mAnimationListener != null) {
3023 final Runnable end = new Runnable() {
3024 public void run() {
3025 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
3026 }
3027 };
3028 post(end);
3029 }
3030
3031 if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) {
3032 mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE;
3033 if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) {
3034 setChildrenDrawingCacheEnabled(false);
3035 }
3036 }
3037
Romain Guy849d0a32011-02-01 17:20:48 -08003038 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 }
3040
3041 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08003042 * This method is used to cause children of this ViewGroup to restore or recreate their
3043 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
3044 * to recreate its own display list, which would happen if it went through the normal
3045 * draw/dispatchDraw mechanisms.
3046 *
3047 * @hide
3048 */
3049 @Override
3050 protected void dispatchGetDisplayList() {
3051 final int count = mChildrenCount;
3052 final View[] children = mChildren;
3053 for (int i = 0; i < count; i++) {
3054 final View child = children[i];
Romain Guy59c7f802011-09-29 17:21:45 -07003055 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) &&
3056 child.hasStaticLayer()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003057 child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED)
3058 == PFLAG_INVALIDATED;
3059 child.mPrivateFlags &= ~PFLAG_INVALIDATED;
Romain Guy2f57ba52011-02-03 18:03:29 -08003060 child.getDisplayList();
3061 child.mRecreateDisplayList = false;
3062 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003063 }
Chet Haase91cedf12013-03-11 07:56:30 -07003064 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003065 View overlayView = mOverlay.getOverlayView();
3066 overlayView.mRecreateDisplayList = (overlayView.mPrivateFlags & PFLAG_INVALIDATED)
Chet Haase91cedf12013-03-11 07:56:30 -07003067 == PFLAG_INVALIDATED;
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003068 overlayView.mPrivateFlags &= ~PFLAG_INVALIDATED;
3069 overlayView.getDisplayList();
3070 overlayView.mRecreateDisplayList = false;
Chet Haase91cedf12013-03-11 07:56:30 -07003071 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003072 }
3073
3074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 * Draw one child of this View Group. This method is responsible for getting
3076 * the canvas in the right state. This includes clipping, translating so
3077 * that the child's scrolled origin is at 0, 0, and applying any animation
3078 * transformations.
3079 *
3080 * @param canvas The canvas on which to draw the child
3081 * @param child Who to draw
Chet Haasebcca79a2012-02-14 08:45:14 -08003082 * @param drawingTime The time at which draw is occurring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * @return True if an invalidate() was issued
3084 */
3085 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Chet Haase64a48c12012-02-13 16:33:29 -08003086 return child.draw(canvas, this, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 /**
Chet Haase430742f2013-04-12 11:18:36 -07003090 * Returns whether ths group's children are clipped to their bounds before drawing.
3091 * The default value is true.
3092 * @see #setClipChildren(boolean)
3093 *
3094 * @return True if the group's children will be clipped to their bounds,
3095 * false otherwise.
3096 */
3097 public boolean getClipChildren() {
3098 return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0);
3099 }
3100
3101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 * By default, children are clipped to their bounds before drawing. This
3103 * allows view groups to override this behavior for animations, etc.
3104 *
3105 * @param clipChildren true to clip children to their bounds,
3106 * false otherwise
3107 * @attr ref android.R.styleable#ViewGroup_clipChildren
3108 */
3109 public void setClipChildren(boolean clipChildren) {
Chet Haasea1cff502012-02-21 13:43:44 -08003110 boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
3111 if (clipChildren != previousValue) {
3112 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
Chet Haase1271e2c2012-04-20 09:54:27 -07003113 for (int i = 0; i < mChildrenCount; ++i) {
3114 View child = getChildAt(i);
3115 if (child.mDisplayList != null) {
Chet Haasedd671592013-04-19 14:54:34 -07003116 child.mDisplayList.setClipToBounds(clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -08003117 }
3118 }
3119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
3121
3122 /**
3123 * By default, children are clipped to the padding of the ViewGroup. This
3124 * allows view groups to override this behavior
3125 *
3126 * @param clipToPadding true to clip children to the padding of the
3127 * group, false otherwise
3128 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3129 */
3130 public void setClipToPadding(boolean clipToPadding) {
3131 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
3132 }
3133
3134 /**
3135 * {@inheritDoc}
3136 */
3137 @Override
3138 public void dispatchSetSelected(boolean selected) {
3139 final View[] children = mChildren;
3140 final int count = mChildrenCount;
3141 for (int i = 0; i < count; i++) {
3142 children[i].setSelected(selected);
3143 }
3144 }
Romain Guy8506ab42009-06-11 17:35:47 -07003145
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003146 /**
3147 * {@inheritDoc}
3148 */
3149 @Override
3150 public void dispatchSetActivated(boolean activated) {
3151 final View[] children = mChildren;
3152 final int count = mChildrenCount;
3153 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003154 children[i].setActivated(activated);
3155 }
3156 }
3157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 @Override
3159 protected void dispatchSetPressed(boolean pressed) {
3160 final View[] children = mChildren;
3161 final int count = mChildrenCount;
3162 for (int i = 0; i < count; i++) {
Adam Powell035a1fc2012-02-27 15:23:50 -08003163 final View child = children[i];
3164 // Children that are clickable on their own should not
3165 // show a pressed state when their parent view does.
3166 // Clearing a pressed state always propagates.
3167 if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
3168 child.setPressed(pressed);
3169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
3171 }
3172
3173 /**
3174 * When this property is set to true, this ViewGroup supports static transformations on
3175 * children; this causes
3176 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3177 * invoked when a child is drawn.
3178 *
3179 * Any subclass overriding
3180 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3181 * set this property to true.
3182 *
3183 * @param enabled True to enable static transformations on children, false otherwise.
3184 *
Chet Haase599913d2012-07-23 16:22:05 -07003185 * @see #getChildStaticTransformation(View, android.view.animation.Transformation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 */
3187 protected void setStaticTransformationsEnabled(boolean enabled) {
3188 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3189 }
3190
3191 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08003192 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
3193 * boolean to indicate whether a static transform was set. The default implementation
3194 * simply returns <code>false</code>; subclasses may override this method for different
Chet Haase599913d2012-07-23 16:22:05 -07003195 * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true
3196 * for this method to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08003198 * @param child The child view whose static transform is being requested
3199 * @param t The Transformation which will hold the result
3200 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07003201 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 */
3203 protected boolean getChildStaticTransformation(View child, Transformation t) {
3204 return false;
3205 }
3206
3207 /**
3208 * {@hide}
3209 */
3210 @Override
3211 protected View findViewTraversal(int id) {
3212 if (id == mID) {
3213 return this;
3214 }
3215
3216 final View[] where = mChildren;
3217 final int len = mChildrenCount;
3218
3219 for (int i = 0; i < len; i++) {
3220 View v = where[i];
3221
Dianne Hackborn4702a852012-08-17 15:18:29 -07003222 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 v = v.findViewById(id);
3224
3225 if (v != null) {
3226 return v;
3227 }
3228 }
3229 }
3230
3231 return null;
3232 }
3233
3234 /**
3235 * {@hide}
3236 */
3237 @Override
3238 protected View findViewWithTagTraversal(Object tag) {
3239 if (tag != null && tag.equals(mTag)) {
3240 return this;
3241 }
3242
3243 final View[] where = mChildren;
3244 final int len = mChildrenCount;
3245
3246 for (int i = 0; i < len; i++) {
3247 View v = where[i];
3248
Dianne Hackborn4702a852012-08-17 15:18:29 -07003249 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 v = v.findViewWithTag(tag);
3251
3252 if (v != null) {
3253 return v;
3254 }
3255 }
3256 }
3257
3258 return null;
3259 }
3260
3261 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003262 * {@hide}
3263 */
3264 @Override
Jeff Brown4dfbec22011-08-15 14:55:37 -07003265 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003266 if (predicate.apply(this)) {
3267 return this;
3268 }
3269
3270 final View[] where = mChildren;
3271 final int len = mChildrenCount;
3272
3273 for (int i = 0; i < len; i++) {
3274 View v = where[i];
3275
Dianne Hackborn4702a852012-08-17 15:18:29 -07003276 if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003277 v = v.findViewByPredicate(predicate);
3278
3279 if (v != null) {
3280 return v;
3281 }
3282 }
3283 }
3284
3285 return null;
3286 }
3287
3288 /**
Romain Guy393a52c2012-05-22 20:21:08 -07003289 * <p>Adds a child view. If no layout parameters are already set on the child, the
3290 * default parameters for this ViewGroup are set on the child.</p>
3291 *
3292 * <p><strong>Note:</strong> do not invoke this method from
3293 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3294 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 *
3296 * @param child the child view to add
3297 *
3298 * @see #generateDefaultLayoutParams()
3299 */
3300 public void addView(View child) {
3301 addView(child, -1);
3302 }
3303
3304 /**
3305 * Adds a child view. If no layout parameters are already set on the child, the
3306 * default parameters for this ViewGroup are set on the child.
Romain Guy393a52c2012-05-22 20:21:08 -07003307 *
3308 * <p><strong>Note:</strong> do not invoke this method from
3309 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3310 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 *
3312 * @param child the child view to add
3313 * @param index the position at which to add the child
3314 *
3315 * @see #generateDefaultLayoutParams()
3316 */
3317 public void addView(View child, int index) {
3318 LayoutParams params = child.getLayoutParams();
3319 if (params == null) {
3320 params = generateDefaultLayoutParams();
3321 if (params == null) {
3322 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
3323 }
3324 }
3325 addView(child, index, params);
3326 }
3327
3328 /**
3329 * Adds a child view with this ViewGroup's default layout parameters and the
3330 * specified width and height.
3331 *
Romain Guy393a52c2012-05-22 20:21:08 -07003332 * <p><strong>Note:</strong> do not invoke this method from
3333 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3334 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3335 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 * @param child the child view to add
3337 */
3338 public void addView(View child, int width, int height) {
3339 final LayoutParams params = generateDefaultLayoutParams();
3340 params.width = width;
3341 params.height = height;
3342 addView(child, -1, params);
3343 }
3344
3345 /**
3346 * Adds a child view with the specified layout parameters.
3347 *
Romain Guy393a52c2012-05-22 20:21:08 -07003348 * <p><strong>Note:</strong> do not invoke this method from
3349 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3350 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 * @param child the child view to add
3353 * @param params the layout parameters to set on the child
3354 */
3355 public void addView(View child, LayoutParams params) {
3356 addView(child, -1, params);
3357 }
3358
3359 /**
3360 * Adds a child view with the specified layout parameters.
3361 *
Romain Guy393a52c2012-05-22 20:21:08 -07003362 * <p><strong>Note:</strong> do not invoke this method from
3363 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3364 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 * @param child the child view to add
3367 * @param index the position at which to add the child
3368 * @param params the layout parameters to set on the child
3369 */
3370 public void addView(View child, int index, LayoutParams params) {
3371 if (DBG) {
3372 System.out.println(this + " addView");
3373 }
3374
3375 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
3376 // therefore, we call requestLayout() on ourselves before, so that the child's request
3377 // will be blocked at our level
3378 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003379 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 addViewInner(child, index, params, false);
3381 }
3382
3383 /**
3384 * {@inheritDoc}
3385 */
3386 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
3387 if (!checkLayoutParams(params)) {
3388 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
3389 }
3390 if (view.mParent != this) {
3391 throw new IllegalArgumentException("Given view not a child of " + this);
3392 }
3393 view.setLayoutParams(params);
3394 }
3395
3396 /**
3397 * {@inheritDoc}
3398 */
3399 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3400 return p != null;
3401 }
3402
3403 /**
3404 * Interface definition for a callback to be invoked when the hierarchy
3405 * within this view changed. The hierarchy changes whenever a child is added
3406 * to or removed from this view.
3407 */
3408 public interface OnHierarchyChangeListener {
3409 /**
3410 * Called when a new child is added to a parent view.
3411 *
3412 * @param parent the view in which a child was added
3413 * @param child the new child view added in the hierarchy
3414 */
3415 void onChildViewAdded(View parent, View child);
3416
3417 /**
3418 * Called when a child is removed from a parent view.
3419 *
3420 * @param parent the view from which the child was removed
3421 * @param child the child removed from the hierarchy
3422 */
3423 void onChildViewRemoved(View parent, View child);
3424 }
3425
3426 /**
3427 * Register a callback to be invoked when a child is added to or removed
3428 * from this view.
3429 *
3430 * @param listener the callback to invoke on hierarchy change
3431 */
3432 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
3433 mOnHierarchyChangeListener = listener;
3434 }
3435
3436 /**
Philip Milnef51d91c2011-07-18 16:12:19 -07003437 * @hide
3438 */
3439 protected void onViewAdded(View child) {
3440 if (mOnHierarchyChangeListener != null) {
3441 mOnHierarchyChangeListener.onChildViewAdded(this, child);
3442 }
3443 }
3444
3445 /**
3446 * @hide
3447 */
3448 protected void onViewRemoved(View child) {
3449 if (mOnHierarchyChangeListener != null) {
3450 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
3451 }
3452 }
3453
3454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 * Adds a view during layout. This is useful if in your onLayout() method,
3456 * you need to add more views (as does the list view for example).
3457 *
3458 * If index is negative, it means put it at the end of the list.
3459 *
3460 * @param child the view to add to the group
3461 * @param index the index at which the child must be added
3462 * @param params the layout parameters to associate with the child
3463 * @return true if the child was added, false otherwise
3464 */
3465 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
3466 return addViewInLayout(child, index, params, false);
3467 }
3468
3469 /**
3470 * Adds a view during layout. This is useful if in your onLayout() method,
3471 * you need to add more views (as does the list view for example).
3472 *
3473 * If index is negative, it means put it at the end of the list.
3474 *
3475 * @param child the view to add to the group
3476 * @param index the index at which the child must be added
3477 * @param params the layout parameters to associate with the child
3478 * @param preventRequestLayout if true, calling this method will not trigger a
3479 * layout request on child
3480 * @return true if the child was added, false otherwise
3481 */
3482 protected boolean addViewInLayout(View child, int index, LayoutParams params,
3483 boolean preventRequestLayout) {
3484 child.mParent = null;
3485 addViewInner(child, index, params, preventRequestLayout);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003486 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 return true;
3488 }
3489
3490 /**
3491 * Prevents the specified child to be laid out during the next layout pass.
3492 *
3493 * @param child the child on which to perform the cleanup
3494 */
3495 protected void cleanupLayoutState(View child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003496 child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 }
3498
3499 private void addViewInner(View child, int index, LayoutParams params,
3500 boolean preventRequestLayout) {
3501
Chet Haasee8e45d32011-03-02 17:07:35 -08003502 if (mTransition != null) {
3503 // Don't prevent other add transitions from completing, but cancel remove
3504 // transitions to let them complete the process before we add to the container
3505 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08003506 }
3507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 if (child.getParent() != null) {
3509 throw new IllegalStateException("The specified child already has a parent. " +
3510 "You must call removeView() on the child's parent first.");
3511 }
3512
Chet Haase21cd1382010-09-01 17:42:29 -07003513 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003514 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07003515 }
3516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 if (!checkLayoutParams(params)) {
3518 params = generateLayoutParams(params);
3519 }
3520
3521 if (preventRequestLayout) {
3522 child.mLayoutParams = params;
3523 } else {
3524 child.setLayoutParams(params);
3525 }
3526
3527 if (index < 0) {
3528 index = mChildrenCount;
3529 }
3530
3531 addInArray(child, index);
3532
3533 // tell our children
3534 if (preventRequestLayout) {
3535 child.assignParent(this);
3536 } else {
3537 child.mParent = this;
3538 }
3539
3540 if (child.hasFocus()) {
3541 requestChildFocus(child, child.findFocus());
3542 }
Romain Guy8506ab42009-06-11 17:35:47 -07003543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07003545 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07003546 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 ai.mKeepScreenOn = false;
3548 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
3549 if (ai.mKeepScreenOn) {
3550 needGlobalAttributesUpdate(true);
3551 }
3552 ai.mKeepScreenOn = lastKeepOn;
3553 }
3554
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07003555 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglioa7e0bcd2012-10-16 19:55:01 -07003556 child.resetRtlProperties();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07003557 }
3558
Philip Milnef51d91c2011-07-18 16:12:19 -07003559 onViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560
3561 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
3562 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
3563 }
Adam Powell539ee872012-02-03 19:00:49 -08003564
3565 if (child.hasTransientState()) {
3566 childHasTransientStateChanged(child, true);
3567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
3569
3570 private void addInArray(View child, int index) {
3571 View[] children = mChildren;
3572 final int count = mChildrenCount;
3573 final int size = children.length;
3574 if (index == count) {
3575 if (size == count) {
3576 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
3577 System.arraycopy(children, 0, mChildren, 0, size);
3578 children = mChildren;
3579 }
3580 children[mChildrenCount++] = child;
3581 } else if (index < count) {
3582 if (size == count) {
3583 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
3584 System.arraycopy(children, 0, mChildren, 0, index);
3585 System.arraycopy(children, index, mChildren, index + 1, count - index);
3586 children = mChildren;
3587 } else {
3588 System.arraycopy(children, index, children, index + 1, count - index);
3589 }
3590 children[index] = child;
3591 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08003592 if (mLastTouchDownIndex >= index) {
3593 mLastTouchDownIndex++;
3594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 } else {
3596 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
3597 }
3598 }
3599
3600 // This method also sets the child's mParent to null
3601 private void removeFromArray(int index) {
3602 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07003603 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
3604 children[index].mParent = null;
3605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 final int count = mChildrenCount;
3607 if (index == count - 1) {
3608 children[--mChildrenCount] = null;
3609 } else if (index >= 0 && index < count) {
3610 System.arraycopy(children, index + 1, children, index, count - index - 1);
3611 children[--mChildrenCount] = null;
3612 } else {
3613 throw new IndexOutOfBoundsException();
3614 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08003615 if (mLastTouchDownIndex == index) {
3616 mLastTouchDownTime = 0;
3617 mLastTouchDownIndex = -1;
3618 } else if (mLastTouchDownIndex > index) {
3619 mLastTouchDownIndex--;
3620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 }
3622
3623 // This method also sets the children's mParent to null
3624 private void removeFromArray(int start, int count) {
3625 final View[] children = mChildren;
3626 final int childrenCount = mChildrenCount;
3627
3628 start = Math.max(0, start);
3629 final int end = Math.min(childrenCount, start + count);
3630
3631 if (start == end) {
3632 return;
3633 }
3634
3635 if (end == childrenCount) {
3636 for (int i = start; i < end; i++) {
3637 children[i].mParent = null;
3638 children[i] = null;
3639 }
3640 } else {
3641 for (int i = start; i < end; i++) {
3642 children[i].mParent = null;
3643 }
3644
3645 // Since we're looping above, we might as well do the copy, but is arraycopy()
3646 // faster than the extra 2 bounds checks we would do in the loop?
3647 System.arraycopy(children, end, children, start, childrenCount - end);
3648
3649 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
3650 children[i] = null;
3651 }
3652 }
3653
3654 mChildrenCount -= (end - start);
3655 }
3656
3657 private void bindLayoutAnimation(View child) {
3658 Animation a = mLayoutAnimationController.getAnimationForView(child);
3659 child.setAnimation(a);
3660 }
3661
3662 /**
3663 * Subclasses should override this method to set layout animation
3664 * parameters on the supplied child.
3665 *
3666 * @param child the child to associate with animation parameters
3667 * @param params the child's layout parameters which hold the animation
3668 * parameters
3669 * @param index the index of the child in the view group
3670 * @param count the number of children in the view group
3671 */
3672 protected void attachLayoutAnimationParameters(View child,
3673 LayoutParams params, int index, int count) {
3674 LayoutAnimationController.AnimationParameters animationParams =
3675 params.layoutAnimationParameters;
3676 if (animationParams == null) {
3677 animationParams = new LayoutAnimationController.AnimationParameters();
3678 params.layoutAnimationParameters = animationParams;
3679 }
3680
3681 animationParams.count = count;
3682 animationParams.index = index;
3683 }
3684
3685 /**
3686 * {@inheritDoc}
Romain Guy393a52c2012-05-22 20:21:08 -07003687 *
3688 * <p><strong>Note:</strong> do not invoke this method from
3689 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3690 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 */
3692 public void removeView(View view) {
3693 removeViewInternal(view);
3694 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003695 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 }
3697
3698 /**
3699 * Removes a view during layout. This is useful if in your onLayout() method,
3700 * you need to remove more views.
3701 *
Romain Guy393a52c2012-05-22 20:21:08 -07003702 * <p><strong>Note:</strong> do not invoke this method from
3703 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3704 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3705 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 * @param view the view to remove from the group
3707 */
3708 public void removeViewInLayout(View view) {
3709 removeViewInternal(view);
3710 }
3711
3712 /**
3713 * Removes a range of views during layout. This is useful if in your onLayout() method,
3714 * you need to remove more views.
3715 *
Romain Guy393a52c2012-05-22 20:21:08 -07003716 * <p><strong>Note:</strong> do not invoke this method from
3717 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3718 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 * @param start the index of the first view to remove from the group
3721 * @param count the number of views to remove from the group
3722 */
3723 public void removeViewsInLayout(int start, int count) {
3724 removeViewsInternal(start, count);
3725 }
3726
3727 /**
3728 * Removes the view at the specified position in the group.
3729 *
Romain Guy393a52c2012-05-22 20:21:08 -07003730 * <p><strong>Note:</strong> do not invoke this method from
3731 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3732 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3733 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 * @param index the position in the group of the view to remove
3735 */
3736 public void removeViewAt(int index) {
3737 removeViewInternal(index, getChildAt(index));
3738 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003739 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
3741
3742 /**
3743 * Removes the specified range of views from the group.
3744 *
Romain Guy393a52c2012-05-22 20:21:08 -07003745 * <p><strong>Note:</strong> do not invoke this method from
3746 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3747 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
3748 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 * @param start the first position in the group of the range of views to remove
3750 * @param count the number of views to remove
3751 */
3752 public void removeViews(int start, int count) {
3753 removeViewsInternal(start, count);
3754 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003755 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 }
3757
3758 private void removeViewInternal(View view) {
3759 final int index = indexOfChild(view);
3760 if (index >= 0) {
3761 removeViewInternal(index, view);
3762 }
3763 }
3764
3765 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07003766
3767 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003768 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003769 }
3770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 boolean clearChildFocus = false;
3772 if (view == mFocused) {
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07003773 view.unFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 clearChildFocus = true;
3775 }
3776
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003777 if (view.isAccessibilityFocused()) {
3778 view.clearAccessibilityFocus();
3779 }
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07003780
Jeff Brown59a422e2012-04-19 15:19:19 -07003781 cancelTouchTarget(view);
3782 cancelHoverTarget(view);
3783
Chet Haase21cd1382010-09-01 17:42:29 -07003784 if (view.getAnimation() != null ||
3785 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 addDisappearingView(view);
3787 } else if (view.mAttachInfo != null) {
3788 view.dispatchDetachedFromWindow();
3789 }
3790
Adam Powell539ee872012-02-03 19:00:49 -08003791 if (view.hasTransientState()) {
3792 childHasTransientStateChanged(view, false);
3793 }
3794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 removeFromArray(index);
3798
3799 if (clearChildFocus) {
3800 clearChildFocus(view);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003801 if (!rootViewRequestFocus()) {
3802 notifyGlobalFocusCleared(this);
3803 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003804 }
Romain Guy6fb05632012-11-29 10:50:33 -08003805
3806 onViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 }
3808
Chet Haase21cd1382010-09-01 17:42:29 -07003809 /**
3810 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
3811 * not null, changes in layout which occur because of children being added to or removed from
3812 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
3813 * object. By default, the transition object is null (so layout changes are not animated).
3814 *
3815 * @param transition The LayoutTransition object that will animated changes in layout. A value
3816 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07003817 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07003818 */
3819 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07003820 if (mTransition != null) {
3821 mTransition.removeTransitionListener(mLayoutTransitionListener);
3822 }
Chet Haase21cd1382010-09-01 17:42:29 -07003823 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07003824 if (mTransition != null) {
3825 mTransition.addTransitionListener(mLayoutTransitionListener);
3826 }
3827 }
3828
3829 /**
3830 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
3831 * not null, changes in layout which occur because of children being added to or removed from
3832 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
3833 * object. By default, the transition object is null (so layout changes are not animated).
3834 *
3835 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
3836 * A value of <code>null</code> means no transition will run on layout changes.
3837 */
3838 public LayoutTransition getLayoutTransition() {
3839 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07003840 }
3841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 private void removeViewsInternal(int start, int count) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 final View focused = mFocused;
3844 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003845 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846
3847 final View[] children = mChildren;
3848 final int end = start + count;
3849
3850 for (int i = start; i < end; i++) {
3851 final View view = children[i];
3852
Chet Haase21cd1382010-09-01 17:42:29 -07003853 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003854 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003855 }
3856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 if (view == focused) {
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07003858 view.unFocus();
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003859 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 }
3861
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003862 if (view.isAccessibilityFocused()) {
3863 view.clearAccessibilityFocus();
3864 }
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07003865
Jeff Brown59a422e2012-04-19 15:19:19 -07003866 cancelTouchTarget(view);
3867 cancelHoverTarget(view);
3868
Chet Haase21cd1382010-09-01 17:42:29 -07003869 if (view.getAnimation() != null ||
3870 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 addDisappearingView(view);
3872 } else if (detach) {
3873 view.dispatchDetachedFromWindow();
3874 }
3875
Adam Powell539ee872012-02-03 19:00:49 -08003876 if (view.hasTransientState()) {
3877 childHasTransientStateChanged(view, false);
3878 }
3879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003881
Philip Milnef51d91c2011-07-18 16:12:19 -07003882 onViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 }
3884
3885 removeFromArray(start, count);
3886
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003887 if (clearChildFocus) {
3888 clearChildFocus(focused);
3889 if (!rootViewRequestFocus()) {
3890 notifyGlobalFocusCleared(focused);
3891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 }
3893 }
3894
3895 /**
3896 * Call this method to remove all child views from the
3897 * ViewGroup.
Romain Guy393a52c2012-05-22 20:21:08 -07003898 *
3899 * <p><strong>Note:</strong> do not invoke this method from
3900 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3901 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 */
3903 public void removeAllViews() {
3904 removeAllViewsInLayout();
3905 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08003906 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 }
3908
3909 /**
3910 * Called by a ViewGroup subclass to remove child views from itself,
3911 * when it must first know its size on screen before it can calculate how many
3912 * child views it will render. An example is a Gallery or a ListView, which
3913 * may "have" 50 children, but actually only render the number of children
3914 * that can currently fit inside the object on screen. Do not call
3915 * this method unless you are extending ViewGroup and understand the
3916 * view measuring and layout pipeline.
Romain Guy393a52c2012-05-22 20:21:08 -07003917 *
3918 * <p><strong>Note:</strong> do not invoke this method from
3919 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
3920 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 */
3922 public void removeAllViewsInLayout() {
3923 final int count = mChildrenCount;
3924 if (count <= 0) {
3925 return;
3926 }
3927
3928 final View[] children = mChildren;
3929 mChildrenCount = 0;
3930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 final View focused = mFocused;
3932 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003933 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934
3935 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07003936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 for (int i = count - 1; i >= 0; i--) {
3938 final View view = children[i];
3939
Chet Haase21cd1382010-09-01 17:42:29 -07003940 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07003941 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07003942 }
3943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 if (view == focused) {
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07003945 view.unFocus();
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003946 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 }
3948
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003949 if (view.isAccessibilityFocused()) {
3950 view.clearAccessibilityFocus();
3951 }
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07003952
Jeff Brown59a422e2012-04-19 15:19:19 -07003953 cancelTouchTarget(view);
3954 cancelHoverTarget(view);
3955
Chet Haase21cd1382010-09-01 17:42:29 -07003956 if (view.getAnimation() != null ||
3957 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 addDisappearingView(view);
3959 } else if (detach) {
3960 view.dispatchDetachedFromWindow();
3961 }
3962
Adam Powell539ee872012-02-03 19:00:49 -08003963 if (view.hasTransientState()) {
3964 childHasTransientStateChanged(view, false);
3965 }
3966
Philip Milnef51d91c2011-07-18 16:12:19 -07003967 onViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968
3969 view.mParent = null;
3970 children[i] = null;
3971 }
3972
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003973 if (clearChildFocus) {
3974 clearChildFocus(focused);
3975 if (!rootViewRequestFocus()) {
3976 notifyGlobalFocusCleared(focused);
3977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 }
3979 }
3980
3981 /**
3982 * Finishes the removal of a detached view. This method will dispatch the detached from
3983 * window event and notify the hierarchy change listener.
Chet Haaseca479d42012-08-30 17:20:08 -07003984 * <p>
3985 * This method is intended to be lightweight and makes no assumptions about whether the
3986 * parent or child should be redrawn. Proper use of this method will include also making
3987 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
3988 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
3989 * which performs a {@link #requestLayout()} on the next frame, after all detach/remove
3990 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 *
3992 * @param child the child to be definitely removed from the view hierarchy
3993 * @param animate if true and the view has an animation, the view is placed in the
3994 * disappearing views list, otherwise, it is detached from the window
3995 *
3996 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
3997 * @see #detachAllViewsFromParent()
3998 * @see #detachViewFromParent(View)
3999 * @see #detachViewFromParent(int)
4000 */
4001 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07004002 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004003 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004004 }
4005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 if (child == mFocused) {
4007 child.clearFocus();
4008 }
Romain Guy8506ab42009-06-11 17:35:47 -07004009
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004010 child.clearAccessibilityFocus();
4011
Jeff Brown59a422e2012-04-19 15:19:19 -07004012 cancelTouchTarget(child);
4013 cancelHoverTarget(child);
4014
Chet Haase21cd1382010-09-01 17:42:29 -07004015 if ((animate && child.getAnimation() != null) ||
4016 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 addDisappearingView(child);
4018 } else if (child.mAttachInfo != null) {
4019 child.dispatchDetachedFromWindow();
4020 }
4021
Adam Powell539ee872012-02-03 19:00:49 -08004022 if (child.hasTransientState()) {
4023 childHasTransientStateChanged(child, false);
4024 }
4025
Philip Milnef51d91c2011-07-18 16:12:19 -07004026 onViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
4028
4029 /**
4030 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
Chet Haaseca479d42012-08-30 17:20:08 -07004031 * sets the layout parameters and puts the view in the list of children so that
4032 * it can be retrieved by calling {@link #getChildAt(int)}.
4033 * <p>
4034 * This method is intended to be lightweight and makes no assumptions about whether the
4035 * parent or child should be redrawn. Proper use of this method will include also making
4036 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4037 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4038 * which performs a {@link #requestLayout()} on the next frame, after all detach/attach
4039 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
4040 * <p>
4041 * This method should be called only for views which were detached from their parent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 *
4043 * @param child the child to attach
4044 * @param index the index at which the child should be attached
4045 * @param params the layout parameters of the child
4046 *
4047 * @see #removeDetachedView(View, boolean)
4048 * @see #detachAllViewsFromParent()
4049 * @see #detachViewFromParent(View)
4050 * @see #detachViewFromParent(int)
4051 */
4052 protected void attachViewToParent(View child, int index, LayoutParams params) {
4053 child.mLayoutParams = params;
4054
4055 if (index < 0) {
4056 index = mChildrenCount;
4057 }
4058
4059 addInArray(child, index);
4060
4061 child.mParent = this;
Dianne Hackborn4702a852012-08-17 15:18:29 -07004062 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK
4063 & ~PFLAG_DRAWING_CACHE_VALID)
4064 | PFLAG_DRAWN | PFLAG_INVALIDATED;
4065 this.mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066
4067 if (child.hasFocus()) {
4068 requestChildFocus(child, child.findFocus());
4069 }
4070 }
4071
4072 /**
Chet Haaseca479d42012-08-30 17:20:08 -07004073 * Detaches a view from its parent. Detaching a view should be followed
4074 * either by a call to
4075 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
4076 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
4077 * temporary; reattachment or removal should happen within the same drawing cycle as
4078 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
4079 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 *
4081 * @param child the child to detach
4082 *
4083 * @see #detachViewFromParent(int)
4084 * @see #detachViewsFromParent(int, int)
4085 * @see #detachAllViewsFromParent()
4086 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4087 * @see #removeDetachedView(View, boolean)
4088 */
4089 protected void detachViewFromParent(View child) {
4090 removeFromArray(indexOfChild(child));
4091 }
4092
4093 /**
Chet Haaseca479d42012-08-30 17:20:08 -07004094 * Detaches a view from its parent. Detaching a view should be followed
4095 * either by a call to
4096 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
4097 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
4098 * temporary; reattachment or removal should happen within the same drawing cycle as
4099 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
4100 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 *
4102 * @param index the index of the child to detach
4103 *
4104 * @see #detachViewFromParent(View)
4105 * @see #detachAllViewsFromParent()
4106 * @see #detachViewsFromParent(int, int)
4107 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4108 * @see #removeDetachedView(View, boolean)
4109 */
4110 protected void detachViewFromParent(int index) {
4111 removeFromArray(index);
4112 }
4113
4114 /**
Chet Haaseca479d42012-08-30 17:20:08 -07004115 * Detaches a range of views from their parents. Detaching a view should be followed
4116 * either by a call to
4117 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
4118 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
4119 * temporary; reattachment or removal should happen within the same drawing cycle as
4120 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
4121 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 *
4123 * @param start the first index of the childrend range to detach
4124 * @param count the number of children to detach
4125 *
4126 * @see #detachViewFromParent(View)
4127 * @see #detachViewFromParent(int)
4128 * @see #detachAllViewsFromParent()
4129 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4130 * @see #removeDetachedView(View, boolean)
4131 */
4132 protected void detachViewsFromParent(int start, int count) {
4133 removeFromArray(start, count);
4134 }
4135
4136 /**
Chet Haaseca479d42012-08-30 17:20:08 -07004137 * Detaches all views from the parent. Detaching a view should be followed
4138 * either by a call to
4139 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
4140 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
4141 * temporary; reattachment or removal should happen within the same drawing cycle as
4142 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
4143 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 *
4145 * @see #detachViewFromParent(View)
4146 * @see #detachViewFromParent(int)
4147 * @see #detachViewsFromParent(int, int)
4148 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4149 * @see #removeDetachedView(View, boolean)
4150 */
4151 protected void detachAllViewsFromParent() {
4152 final int count = mChildrenCount;
4153 if (count <= 0) {
4154 return;
4155 }
4156
4157 final View[] children = mChildren;
4158 mChildrenCount = 0;
4159
4160 for (int i = count - 1; i >= 0; i--) {
4161 children[i].mParent = null;
4162 children[i] = null;
4163 }
4164 }
4165
4166 /**
4167 * Don't call or override this method. It is used for the implementation of
4168 * the view hierarchy.
4169 */
4170 public final void invalidateChild(View child, final Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 ViewParent parent = this;
4172
4173 final AttachInfo attachInfo = mAttachInfo;
4174 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 // If the child is drawing an animation, we want to copy this flag onto
4176 // ourselves and the parent to make sure the invalidate request goes
4177 // through
Dianne Hackborn4702a852012-08-17 15:18:29 -07004178 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION)
4179 == PFLAG_DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07004180
Romain Guyfe455af2012-02-15 16:40:20 -08004181 // Check whether the child that requests the invalidate is fully opaque
4182 // Views being animated or transformed are not considered opaque because we may
4183 // be invalidating their old position and need the parent to paint behind them.
4184 Matrix childMatrix = child.getMatrix();
4185 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
4186 child.getAnimation() == null && childMatrix.isIdentity();
4187 // Mark the child as dirty, using the appropriate flag
4188 // Make sure we do not set both flags at the same time
Dianne Hackborn4702a852012-08-17 15:18:29 -07004189 int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08004190
4191 if (child.mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004192 mPrivateFlags |= PFLAG_INVALIDATED;
4193 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guyfe455af2012-02-15 16:40:20 -08004194 child.mLocalDirtyRect.union(dirty);
4195 }
4196
4197 final int[] location = attachInfo.mInvalidateChildLocation;
4198 location[CHILD_LEFT_INDEX] = child.mLeft;
4199 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase599913d2012-07-23 16:22:05 -07004200 if (!childMatrix.isIdentity() ||
4201 (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Romain Guyfe455af2012-02-15 16:40:20 -08004202 RectF boundingRect = attachInfo.mTmpTransformRect;
4203 boundingRect.set(dirty);
Chet Haase599913d2012-07-23 16:22:05 -07004204 Matrix transformMatrix;
4205 if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
4206 Transformation t = attachInfo.mTmpTransformation;
4207 boolean transformed = getChildStaticTransformation(child, t);
4208 if (transformed) {
4209 transformMatrix = attachInfo.mTmpMatrix;
4210 transformMatrix.set(t.getMatrix());
4211 if (!childMatrix.isIdentity()) {
4212 transformMatrix.preConcat(childMatrix);
4213 }
4214 } else {
4215 transformMatrix = childMatrix;
4216 }
4217 } else {
4218 transformMatrix = childMatrix;
4219 }
4220 transformMatrix.mapRect(boundingRect);
Romain Guyfe455af2012-02-15 16:40:20 -08004221 dirty.set((int) (boundingRect.left - 0.5f),
4222 (int) (boundingRect.top - 0.5f),
4223 (int) (boundingRect.right + 0.5f),
4224 (int) (boundingRect.bottom + 0.5f));
4225 }
4226
4227 do {
4228 View view = null;
4229 if (parent instanceof View) {
4230 view = (View) parent;
Romain Guyfe455af2012-02-15 16:40:20 -08004231 }
4232
4233 if (drawAnimation) {
4234 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004235 view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Romain Guyfe455af2012-02-15 16:40:20 -08004236 } else if (parent instanceof ViewRootImpl) {
4237 ((ViewRootImpl) parent).mIsAnimating = true;
4238 }
4239 }
4240
4241 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
4242 // flag coming from the child that initiated the invalidate
4243 if (view != null) {
4244 if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
4245 view.getSolidColor() == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004246 opaqueFlag = PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08004247 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07004248 if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) {
4249 view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag;
Romain Guyfe455af2012-02-15 16:40:20 -08004250 }
4251 }
4252
4253 parent = parent.invalidateChildInParent(location, dirty);
4254 if (view != null) {
4255 // Account for transform on current parent
4256 Matrix m = view.getMatrix();
4257 if (!m.isIdentity()) {
4258 RectF boundingRect = attachInfo.mTmpTransformRect;
4259 boundingRect.set(dirty);
4260 m.mapRect(boundingRect);
Romain Guye8585b12012-02-17 18:28:47 -08004261 dirty.set((int) (boundingRect.left - 0.5f),
4262 (int) (boundingRect.top - 0.5f),
Romain Guyfe455af2012-02-15 16:40:20 -08004263 (int) (boundingRect.right + 0.5f),
4264 (int) (boundingRect.bottom + 0.5f));
4265 }
4266 }
4267 } while (parent != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 }
4269 }
4270
4271 /**
4272 * Don't call or override this method. It is used for the implementation of
4273 * the view hierarchy.
4274 *
4275 * This implementation returns null if this ViewGroup does not have a parent,
4276 * if this ViewGroup is already fully invalidated or if the dirty rectangle
4277 * does not intersect with this ViewGroup's bounds.
4278 */
4279 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004280 if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
4281 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
4283 FLAG_OPTIMIZE_INVALIDATE) {
4284 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
4285 location[CHILD_TOP_INDEX] - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07004286 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
4287 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
4288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289
4290 final int left = mLeft;
4291 final int top = mTop;
4292
Chet Haase05e91ed2012-07-03 14:17:57 -07004293 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
4294 if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) {
4295 dirty.setEmpty();
Romain Guy3a3133d2011-02-01 22:59:58 -08004296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07004298 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haase05e91ed2012-07-03 14:17:57 -07004299
4300 location[CHILD_LEFT_INDEX] = left;
4301 location[CHILD_TOP_INDEX] = top;
4302
4303 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004304 mPrivateFlags |= PFLAG_INVALIDATED;
Chet Haase05e91ed2012-07-03 14:17:57 -07004305 mLocalDirtyRect.union(dirty);
4306 }
4307
4308 return mParent;
4309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004311 mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312
4313 location[CHILD_LEFT_INDEX] = mLeft;
4314 location[CHILD_TOP_INDEX] = mTop;
Chet Haasea3db8662011-07-19 10:36:05 -07004315 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
4316 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
4317 } else {
4318 // in case the dirty rect extends outside the bounds of this container
4319 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
4320 }
Romain Guy3a3133d2011-02-01 22:59:58 -08004321
4322 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004323 mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy3a3133d2011-02-01 22:59:58 -08004324 mLocalDirtyRect.union(dirty);
4325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326
4327 return mParent;
4328 }
4329 }
4330
4331 return null;
4332 }
4333
4334 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07004335 * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the
4336 * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods
4337 * do; all we want to do here is schedule a traversal with the appropriate dirty rect.
4338 *
4339 * @hide
4340 */
4341 public void invalidateChildFast(View child, final Rect dirty) {
4342 ViewParent parent = this;
4343
4344 final AttachInfo attachInfo = mAttachInfo;
4345 if (attachInfo != null) {
4346 if (child.mLayerType != LAYER_TYPE_NONE) {
4347 child.mLocalDirtyRect.union(dirty);
4348 }
4349
4350 int left = child.mLeft;
4351 int top = child.mTop;
4352 if (!child.getMatrix().isIdentity()) {
4353 child.transformRect(dirty);
4354 }
4355
4356 do {
4357 if (parent instanceof ViewGroup) {
4358 ViewGroup parentVG = (ViewGroup) parent;
Chet Haaseb85967b2012-03-26 14:37:51 -07004359 if (parentVG.mLayerType != LAYER_TYPE_NONE) {
4360 // Layered parents should be recreated, not just re-issued
4361 parentVG.invalidate();
4362 parent = null;
4363 } else {
4364 parent = parentVG.invalidateChildInParentFast(left, top, dirty);
4365 left = parentVG.mLeft;
4366 top = parentVG.mTop;
4367 }
Chet Haase9d1992d2012-03-13 11:03:25 -07004368 } else {
4369 // Reached the top; this calls into the usual invalidate method in
4370 // ViewRootImpl, which schedules a traversal
4371 final int[] location = attachInfo.mInvalidateChildLocation;
4372 location[0] = left;
4373 location[1] = top;
4374 parent = parent.invalidateChildInParent(location, dirty);
4375 }
4376 } while (parent != null);
4377 }
4378 }
4379
4380 /**
4381 * Quick invalidation method that simply transforms the dirty rect into the parent's
4382 * coordinate system, pruning the invalidation if the parent has already been invalidated.
4383 */
4384 private ViewParent invalidateChildInParentFast(int left, int top, final Rect dirty) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004385 if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
4386 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
Chet Haase9d1992d2012-03-13 11:03:25 -07004387 dirty.offset(left - mScrollX, top - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07004388 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
4389 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
4390 }
Chet Haase9d1992d2012-03-13 11:03:25 -07004391
4392 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 ||
4393 dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
4394
4395 if (mLayerType != LAYER_TYPE_NONE) {
4396 mLocalDirtyRect.union(dirty);
4397 }
4398 if (!getMatrix().isIdentity()) {
4399 transformRect(dirty);
4400 }
4401
4402 return mParent;
4403 }
4404 }
4405
4406 return null;
4407 }
4408
4409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 * Offset a rectangle that is in a descendant's coordinate
4411 * space into our coordinate space.
4412 * @param descendant A descendant of this view
4413 * @param rect A rectangle defined in descendant's coordinate space.
4414 */
4415 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
4416 offsetRectBetweenParentAndChild(descendant, rect, true, false);
4417 }
4418
4419 /**
4420 * Offset a rectangle that is in our coordinate space into an ancestor's
4421 * coordinate space.
4422 * @param descendant A descendant of this view
4423 * @param rect A rectangle defined in descendant's coordinate space.
4424 */
4425 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
4426 offsetRectBetweenParentAndChild(descendant, rect, false, false);
4427 }
4428
4429 /**
4430 * Helper method that offsets a rect either from parent to descendant or
4431 * descendant to parent.
4432 */
4433 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
4434 boolean offsetFromChildToParent, boolean clipToBounds) {
4435
4436 // already in the same coord system :)
4437 if (descendant == this) {
4438 return;
4439 }
4440
4441 ViewParent theParent = descendant.mParent;
4442
4443 // search and offset up to the parent
4444 while ((theParent != null)
4445 && (theParent instanceof View)
4446 && (theParent != this)) {
4447
4448 if (offsetFromChildToParent) {
4449 rect.offset(descendant.mLeft - descendant.mScrollX,
4450 descendant.mTop - descendant.mScrollY);
4451 if (clipToBounds) {
4452 View p = (View) theParent;
4453 rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
4454 }
4455 } else {
4456 if (clipToBounds) {
4457 View p = (View) theParent;
4458 rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop);
4459 }
4460 rect.offset(descendant.mScrollX - descendant.mLeft,
4461 descendant.mScrollY - descendant.mTop);
4462 }
4463
4464 descendant = (View) theParent;
4465 theParent = descendant.mParent;
4466 }
4467
4468 // now that we are up to this view, need to offset one more time
4469 // to get into our coordinate space
4470 if (theParent == this) {
4471 if (offsetFromChildToParent) {
4472 rect.offset(descendant.mLeft - descendant.mScrollX,
4473 descendant.mTop - descendant.mScrollY);
4474 } else {
4475 rect.offset(descendant.mScrollX - descendant.mLeft,
4476 descendant.mScrollY - descendant.mTop);
4477 }
4478 } else {
4479 throw new IllegalArgumentException("parameter must be a descendant of this view");
4480 }
4481 }
4482
4483 /**
4484 * Offset the vertical location of all children of this view by the specified number of pixels.
4485 *
4486 * @param offset the number of pixels to offset
4487 *
4488 * @hide
4489 */
4490 public void offsetChildrenTopAndBottom(int offset) {
4491 final int count = mChildrenCount;
4492 final View[] children = mChildren;
Romain Guy5549cb52013-05-06 18:42:08 -07004493 boolean invalidate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494
4495 for (int i = 0; i < count; i++) {
4496 final View v = children[i];
4497 v.mTop += offset;
4498 v.mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07004499 if (v.mDisplayList != null) {
Romain Guy5549cb52013-05-06 18:42:08 -07004500 invalidate = true;
Romain Guy52036b12013-02-14 18:03:37 -08004501 v.mDisplayList.offsetTopAndBottom(offset);
Chet Haasea1cff502012-02-21 13:43:44 -08004502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
Romain Guy5549cb52013-05-06 18:42:08 -07004504
4505 if (invalidate) {
4506 invalidateViewProperty(false, false);
4507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 }
4509
4510 /**
4511 * {@inheritDoc}
4512 */
4513 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08004514 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
4515 // but for some simple tests it can be useful. If we don't have attach info this
4516 // will allocate memory.
4517 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01004518 rect.set(r);
4519
4520 if (!child.hasIdentityMatrix()) {
4521 child.getMatrix().mapRect(rect);
4522 }
4523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 int dx = child.mLeft - mScrollX;
4525 int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01004526
4527 rect.offset(dx, dy);
4528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01004530 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08004531 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
4532 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01004533 position[0] = offset.x;
4534 position[1] = offset.y;
4535 child.getMatrix().mapPoints(position);
4536 offset.x = (int) (position[0] + 0.5f);
4537 offset.y = (int) (position[1] + 0.5f);
4538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 offset.x += dx;
4540 offset.y += dy;
4541 }
Gilles Debunnecea45132011-11-24 02:19:27 +01004542
4543 if (rect.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
4544 if (mParent == null) return true;
4545 r.set((int) (rect.left + 0.5f), (int) (rect.top + 0.5f),
4546 (int) (rect.right + 0.5f), (int) (rect.bottom + 0.5f));
4547 return mParent.getChildVisibleRect(this, r, offset);
4548 }
4549
4550 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 }
4552
4553 /**
4554 * {@inheritDoc}
4555 */
4556 @Override
Chet Haase9c087442011-01-12 16:20:16 -08004557 public final void layout(int l, int t, int r, int b) {
Chet Haase430742f2013-04-12 11:18:36 -07004558 if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
Chet Haase7dd4a532012-04-16 13:35:09 -07004559 if (mTransition != null) {
4560 mTransition.layoutChange(this);
4561 }
Chet Haase9c087442011-01-12 16:20:16 -08004562 super.layout(l, t, r, b);
4563 } else {
4564 // record the fact that we noop'd it; request layout when transition finishes
Chet Haaseb9895022013-04-02 15:10:58 -07004565 mLayoutCalledWhileSuppressed = true;
Chet Haase9c087442011-01-12 16:20:16 -08004566 }
4567 }
4568
4569 /**
4570 * {@inheritDoc}
4571 */
4572 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 protected abstract void onLayout(boolean changed,
4574 int l, int t, int r, int b);
4575
4576 /**
4577 * Indicates whether the view group has the ability to animate its children
4578 * after the first layout.
4579 *
4580 * @return true if the children can be animated, false otherwise
4581 */
4582 protected boolean canAnimate() {
4583 return mLayoutAnimationController != null;
4584 }
4585
4586 /**
4587 * Runs the layout animation. Calling this method triggers a relayout of
4588 * this view group.
4589 */
4590 public void startLayoutAnimation() {
4591 if (mLayoutAnimationController != null) {
4592 mGroupFlags |= FLAG_RUN_ANIMATION;
4593 requestLayout();
4594 }
4595 }
4596
4597 /**
4598 * Schedules the layout animation to be played after the next layout pass
4599 * of this view group. This can be used to restart the layout animation
4600 * when the content of the view group changes or when the activity is
4601 * paused and resumed.
4602 */
4603 public void scheduleLayoutAnimation() {
4604 mGroupFlags |= FLAG_RUN_ANIMATION;
4605 }
4606
4607 /**
4608 * Sets the layout animation controller used to animate the group's
4609 * children after the first layout.
4610 *
4611 * @param controller the animation controller
4612 */
4613 public void setLayoutAnimation(LayoutAnimationController controller) {
4614 mLayoutAnimationController = controller;
4615 if (mLayoutAnimationController != null) {
4616 mGroupFlags |= FLAG_RUN_ANIMATION;
4617 }
4618 }
4619
4620 /**
4621 * Returns the layout animation controller used to animate the group's
4622 * children.
4623 *
4624 * @return the current animation controller
4625 */
4626 public LayoutAnimationController getLayoutAnimation() {
4627 return mLayoutAnimationController;
4628 }
4629
4630 /**
4631 * Indicates whether the children's drawing cache is used during a layout
4632 * animation. By default, the drawing cache is enabled but this will prevent
4633 * nested layout animations from working. To nest animations, you must disable
4634 * the cache.
4635 *
4636 * @return true if the animation cache is enabled, false otherwise
4637 *
4638 * @see #setAnimationCacheEnabled(boolean)
4639 * @see View#setDrawingCacheEnabled(boolean)
4640 */
4641 @ViewDebug.ExportedProperty
4642 public boolean isAnimationCacheEnabled() {
4643 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
4644 }
4645
4646 /**
4647 * Enables or disables the children's drawing cache during a layout animation.
4648 * By default, the drawing cache is enabled but this will prevent nested
4649 * layout animations from working. To nest animations, you must disable the
4650 * cache.
4651 *
4652 * @param enabled true to enable the animation cache, false otherwise
4653 *
4654 * @see #isAnimationCacheEnabled()
4655 * @see View#setDrawingCacheEnabled(boolean)
4656 */
4657 public void setAnimationCacheEnabled(boolean enabled) {
4658 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
4659 }
4660
4661 /**
4662 * Indicates whether this ViewGroup will always try to draw its children using their
4663 * drawing cache. By default this property is enabled.
4664 *
4665 * @return true if the animation cache is enabled, false otherwise
4666 *
4667 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4668 * @see #setChildrenDrawnWithCacheEnabled(boolean)
4669 * @see View#setDrawingCacheEnabled(boolean)
4670 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004671 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 public boolean isAlwaysDrawnWithCacheEnabled() {
4673 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
4674 }
4675
4676 /**
4677 * Indicates whether this ViewGroup will always try to draw its children using their
4678 * drawing cache. This property can be set to true when the cache rendering is
4679 * slightly different from the children's normal rendering. Renderings can be different,
4680 * for instance, when the cache's quality is set to low.
4681 *
4682 * When this property is disabled, the ViewGroup will use the drawing cache of its
4683 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
4684 * when to start using the drawing cache and when to stop using it.
4685 *
4686 * @param always true to always draw with the drawing cache, false otherwise
4687 *
4688 * @see #isAlwaysDrawnWithCacheEnabled()
4689 * @see #setChildrenDrawnWithCacheEnabled(boolean)
4690 * @see View#setDrawingCacheEnabled(boolean)
4691 * @see View#setDrawingCacheQuality(int)
4692 */
4693 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
4694 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
4695 }
4696
4697 /**
4698 * Indicates whether the ViewGroup is currently drawing its children using
4699 * their drawing cache.
4700 *
4701 * @return true if children should be drawn with their cache, false otherwise
4702 *
4703 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4704 * @see #setChildrenDrawnWithCacheEnabled(boolean)
4705 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004706 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 protected boolean isChildrenDrawnWithCacheEnabled() {
4708 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
4709 }
4710
4711 /**
4712 * Tells the ViewGroup to draw its children using their drawing cache. This property
4713 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
4714 * will be used only if it has been enabled.
4715 *
4716 * Subclasses should call this method to start and stop using the drawing cache when
4717 * they perform performance sensitive operations, like scrolling or animating.
4718 *
4719 * @param enabled true if children should be drawn with their cache, false otherwise
4720 *
4721 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
4722 * @see #isChildrenDrawnWithCacheEnabled()
4723 */
4724 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
4725 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
4726 }
4727
Romain Guy293451e2009-11-04 13:59:48 -08004728 /**
4729 * Indicates whether the ViewGroup is drawing its children in the order defined by
4730 * {@link #getChildDrawingOrder(int, int)}.
4731 *
4732 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
4733 * false otherwise
4734 *
4735 * @see #setChildrenDrawingOrderEnabled(boolean)
4736 * @see #getChildDrawingOrder(int, int)
4737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004738 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08004739 protected boolean isChildrenDrawingOrderEnabled() {
4740 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
4741 }
4742
4743 /**
4744 * Tells the ViewGroup whether to draw its children in the order defined by the method
4745 * {@link #getChildDrawingOrder(int, int)}.
4746 *
4747 * @param enabled true if the order of the children when drawing is determined by
4748 * {@link #getChildDrawingOrder(int, int)}, false otherwise
4749 *
4750 * @see #isChildrenDrawingOrderEnabled()
4751 * @see #getChildDrawingOrder(int, int)
4752 */
4753 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
4754 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
4755 }
4756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 private void setBooleanFlag(int flag, boolean value) {
4758 if (value) {
4759 mGroupFlags |= flag;
4760 } else {
4761 mGroupFlags &= ~flag;
4762 }
4763 }
4764
4765 /**
4766 * Returns an integer indicating what types of drawing caches are kept in memory.
4767 *
4768 * @see #setPersistentDrawingCache(int)
4769 * @see #setAnimationCacheEnabled(boolean)
4770 *
4771 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
4772 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
4773 * and {@link #PERSISTENT_ALL_CACHES}
4774 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004775 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07004777 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
4779 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
4780 })
4781 public int getPersistentDrawingCache() {
4782 return mPersistentDrawingCache;
4783 }
4784
4785 /**
4786 * Indicates what types of drawing caches should be kept in memory after
4787 * they have been created.
4788 *
4789 * @see #getPersistentDrawingCache()
4790 * @see #setAnimationCacheEnabled(boolean)
4791 *
4792 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
4793 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
4794 * and {@link #PERSISTENT_ALL_CACHES}
4795 */
4796 public void setPersistentDrawingCache(int drawingCacheToKeep) {
4797 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
4798 }
4799
4800 /**
Philip Milne7a23b492012-04-24 22:12:36 -07004801 * Returns the basis of alignment during layout operations on this view group:
Philip Milne7b757812012-09-19 18:13:44 -07004802 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07004803 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07004804 * @return the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07004805 *
4806 * @see #setLayoutMode(int)
4807 */
4808 public int getLayoutMode() {
Philip Milne1557fd72012-04-04 23:41:34 -07004809 return mLayoutMode;
4810 }
4811
4812 /**
Philip Milne7a23b492012-04-24 22:12:36 -07004813 * Sets the basis of alignment during the layout of this view group.
Philip Milne7b757812012-09-19 18:13:44 -07004814 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
4815 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07004816 * <p>
Philip Milne7b757812012-09-19 18:13:44 -07004817 * The default is {@link #LAYOUT_MODE_CLIP_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07004818 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07004819 * @param layoutMode the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07004820 *
4821 * @see #getLayoutMode()
Scott Main27a85082013-06-10 10:39:48 -07004822 * @attr ref android.R.styleable#ViewGroup_layoutMode
Philip Milne1557fd72012-04-04 23:41:34 -07004823 */
4824 public void setLayoutMode(int layoutMode) {
4825 if (mLayoutMode != layoutMode) {
4826 mLayoutMode = layoutMode;
4827 requestLayout();
4828 }
4829 }
4830
4831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 * Returns a new set of layout parameters based on the supplied attributes set.
4833 *
4834 * @param attrs the attributes to build the layout parameters from
4835 *
4836 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
4837 * of its descendants
4838 */
4839 public LayoutParams generateLayoutParams(AttributeSet attrs) {
4840 return new LayoutParams(getContext(), attrs);
4841 }
4842
4843 /**
4844 * Returns a safe set of layout parameters based on the supplied layout params.
4845 * When a ViewGroup is passed a View whose layout params do not pass the test of
4846 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
4847 * is invoked. This method should return a new set of layout params suitable for
4848 * this ViewGroup, possibly by copying the appropriate attributes from the
4849 * specified set of layout params.
4850 *
4851 * @param p The layout parameters to convert into a suitable set of layout parameters
4852 * for this ViewGroup.
4853 *
4854 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
4855 * of its descendants
4856 */
4857 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
4858 return p;
4859 }
4860
4861 /**
4862 * Returns a set of default layout parameters. These parameters are requested
4863 * when the View passed to {@link #addView(View)} has no layout parameters
4864 * already set. If null is returned, an exception is thrown from addView.
4865 *
4866 * @return a set of default layout parameters or null
4867 */
4868 protected LayoutParams generateDefaultLayoutParams() {
4869 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
4870 }
4871
4872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 * {@inheritDoc}
4874 */
4875 @Override
4876 protected void debug(int depth) {
4877 super.debug(depth);
4878 String output;
4879
4880 if (mFocused != null) {
4881 output = debugIndent(depth);
4882 output += "mFocused";
4883 Log.d(VIEW_LOG_TAG, output);
4884 }
4885 if (mChildrenCount != 0) {
4886 output = debugIndent(depth);
4887 output += "{";
4888 Log.d(VIEW_LOG_TAG, output);
4889 }
4890 int count = mChildrenCount;
4891 for (int i = 0; i < count; i++) {
4892 View child = mChildren[i];
4893 child.debug(depth + 1);
4894 }
4895
4896 if (mChildrenCount != 0) {
4897 output = debugIndent(depth);
4898 output += "}";
4899 Log.d(VIEW_LOG_TAG, output);
4900 }
4901 }
4902
4903 /**
4904 * Returns the position in the group of the specified child view.
4905 *
4906 * @param child the view for which to get the position
4907 * @return a positive integer representing the position of the view in the
4908 * group, or -1 if the view does not exist in the group
4909 */
4910 public int indexOfChild(View child) {
4911 final int count = mChildrenCount;
4912 final View[] children = mChildren;
4913 for (int i = 0; i < count; i++) {
4914 if (children[i] == child) {
4915 return i;
4916 }
4917 }
4918 return -1;
4919 }
4920
4921 /**
4922 * Returns the number of children in the group.
4923 *
4924 * @return a positive integer representing the number of children in
4925 * the group
4926 */
4927 public int getChildCount() {
4928 return mChildrenCount;
4929 }
4930
4931 /**
4932 * Returns the view at the specified position in the group.
4933 *
4934 * @param index the position at which to get the view from
4935 * @return the view at the specified position or null if the position
4936 * does not exist within the group
4937 */
4938 public View getChildAt(int index) {
Adam Powell3ba8f5d62011-03-07 15:36:33 -08004939 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 return null;
4941 }
Adam Powell3ba8f5d62011-03-07 15:36:33 -08004942 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 }
4944
4945 /**
4946 * Ask all of the children of this view to measure themselves, taking into
4947 * account both the MeasureSpec requirements for this view and its padding.
4948 * We skip children that are in the GONE state The heavy lifting is done in
4949 * getChildMeasureSpec.
4950 *
4951 * @param widthMeasureSpec The width requirements for this view
4952 * @param heightMeasureSpec The height requirements for this view
4953 */
4954 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
4955 final int size = mChildrenCount;
4956 final View[] children = mChildren;
4957 for (int i = 0; i < size; ++i) {
4958 final View child = children[i];
4959 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
4960 measureChild(child, widthMeasureSpec, heightMeasureSpec);
4961 }
4962 }
4963 }
4964
4965 /**
4966 * Ask one of the children of this view to measure itself, taking into
4967 * account both the MeasureSpec requirements for this view and its padding.
4968 * The heavy lifting is done in getChildMeasureSpec.
4969 *
4970 * @param child The child to measure
4971 * @param parentWidthMeasureSpec The width requirements for this view
4972 * @param parentHeightMeasureSpec The height requirements for this view
4973 */
4974 protected void measureChild(View child, int parentWidthMeasureSpec,
4975 int parentHeightMeasureSpec) {
4976 final LayoutParams lp = child.getLayoutParams();
4977
4978 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
4979 mPaddingLeft + mPaddingRight, lp.width);
4980 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
4981 mPaddingTop + mPaddingBottom, lp.height);
4982
4983 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
4984 }
4985
4986 /**
4987 * Ask one of the children of this view to measure itself, taking into
4988 * account both the MeasureSpec requirements for this view and its padding
4989 * and margins. The child must have MarginLayoutParams The heavy lifting is
4990 * done in getChildMeasureSpec.
4991 *
4992 * @param child The child to measure
4993 * @param parentWidthMeasureSpec The width requirements for this view
4994 * @param widthUsed Extra space that has been used up by the parent
4995 * horizontally (possibly by other children of the parent)
4996 * @param parentHeightMeasureSpec The height requirements for this view
4997 * @param heightUsed Extra space that has been used up by the parent
4998 * vertically (possibly by other children of the parent)
4999 */
5000 protected void measureChildWithMargins(View child,
5001 int parentWidthMeasureSpec, int widthUsed,
5002 int parentHeightMeasureSpec, int heightUsed) {
5003 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
5004
5005 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
5006 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
5007 + widthUsed, lp.width);
5008 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
5009 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
5010 + heightUsed, lp.height);
5011
5012 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
5013 }
5014
5015 /**
5016 * Does the hard part of measureChildren: figuring out the MeasureSpec to
5017 * pass to a particular child. This method figures out the right MeasureSpec
5018 * for one dimension (height or width) of one child view.
5019 *
5020 * The goal is to combine information from our MeasureSpec with the
5021 * LayoutParams of the child to get the best possible results. For example,
5022 * if the this view knows its size (because its MeasureSpec has a mode of
5023 * EXACTLY), and the child has indicated in its LayoutParams that it wants
5024 * to be the same size as the parent, the parent should ask the child to
5025 * layout given an exact size.
5026 *
5027 * @param spec The requirements for this view
5028 * @param padding The padding of this view for the current dimension and
5029 * margins, if applicable
5030 * @param childDimension How big the child wants to be in the current
5031 * dimension
5032 * @return a MeasureSpec integer for the child
5033 */
5034 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
5035 int specMode = MeasureSpec.getMode(spec);
5036 int specSize = MeasureSpec.getSize(spec);
5037
5038 int size = Math.max(0, specSize - padding);
5039
5040 int resultSize = 0;
5041 int resultMode = 0;
5042
5043 switch (specMode) {
5044 // Parent has imposed an exact size on us
5045 case MeasureSpec.EXACTLY:
5046 if (childDimension >= 0) {
5047 resultSize = childDimension;
5048 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08005049 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 // Child wants to be our size. So be it.
5051 resultSize = size;
5052 resultMode = MeasureSpec.EXACTLY;
5053 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
5054 // Child wants to determine its own size. It can't be
5055 // bigger than us.
5056 resultSize = size;
5057 resultMode = MeasureSpec.AT_MOST;
5058 }
5059 break;
5060
5061 // Parent has imposed a maximum size on us
5062 case MeasureSpec.AT_MOST:
5063 if (childDimension >= 0) {
5064 // Child wants a specific size... so be it
5065 resultSize = childDimension;
5066 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08005067 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 // Child wants to be our size, but our size is not fixed.
5069 // Constrain child to not be bigger than us.
5070 resultSize = size;
5071 resultMode = MeasureSpec.AT_MOST;
5072 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
5073 // Child wants to determine its own size. It can't be
5074 // bigger than us.
5075 resultSize = size;
5076 resultMode = MeasureSpec.AT_MOST;
5077 }
5078 break;
5079
5080 // Parent asked to see how big we want to be
5081 case MeasureSpec.UNSPECIFIED:
5082 if (childDimension >= 0) {
5083 // Child wants a specific size... let him have it
5084 resultSize = childDimension;
5085 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08005086 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 // Child wants to be our size... find out how big it should
5088 // be
5089 resultSize = 0;
5090 resultMode = MeasureSpec.UNSPECIFIED;
5091 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
5092 // Child wants to determine its own size.... find out how
5093 // big it should be
5094 resultSize = 0;
5095 resultMode = MeasureSpec.UNSPECIFIED;
5096 }
5097 break;
5098 }
5099 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
5100 }
5101
5102
5103 /**
5104 * Removes any pending animations for views that have been removed. Call
5105 * this if you don't want animations for exiting views to stack up.
5106 */
5107 public void clearDisappearingChildren() {
5108 if (mDisappearingChildren != null) {
5109 mDisappearingChildren.clear();
Chet Haaseb85967b2012-03-26 14:37:51 -07005110 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 }
5112 }
5113
5114 /**
5115 * Add a view which is removed from mChildren but still needs animation
5116 *
5117 * @param v View to add
5118 */
5119 private void addDisappearingView(View v) {
5120 ArrayList<View> disappearingChildren = mDisappearingChildren;
5121
5122 if (disappearingChildren == null) {
5123 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
5124 }
5125
5126 disappearingChildren.add(v);
5127 }
5128
5129 /**
5130 * Cleanup a view when its animation is done. This may mean removing it from
5131 * the list of disappearing views.
5132 *
5133 * @param view The view whose animation has finished
5134 * @param animation The animation, cannot be null
5135 */
Chet Haase64a48c12012-02-13 16:33:29 -08005136 void finishAnimatingView(final View view, Animation animation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137 final ArrayList<View> disappearingChildren = mDisappearingChildren;
5138 if (disappearingChildren != null) {
5139 if (disappearingChildren.contains(view)) {
5140 disappearingChildren.remove(view);
5141
5142 if (view.mAttachInfo != null) {
5143 view.dispatchDetachedFromWindow();
5144 }
5145
5146 view.clearAnimation();
5147 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
5148 }
5149 }
5150
5151 if (animation != null && !animation.getFillAfter()) {
5152 view.clearAnimation();
5153 }
5154
Dianne Hackborn4702a852012-08-17 15:18:29 -07005155 if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 view.onAnimationEnd();
5157 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
5158 // so we'd rather be safe than sorry
Dianne Hackborn4702a852012-08-17 15:18:29 -07005159 view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 // Draw one more frame after the animation is done
5161 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
5162 }
5163 }
5164
Chet Haaseb20db3e2010-09-10 13:07:30 -07005165 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07005166 * Utility function called by View during invalidation to determine whether a view that
5167 * is invisible or gone should still be invalidated because it is being transitioned (and
5168 * therefore still needs to be drawn).
5169 */
5170 boolean isViewTransitioning(View view) {
5171 return (mTransitioningViews != null && mTransitioningViews.contains(view));
5172 }
5173
5174 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07005175 * This method tells the ViewGroup that the given View object, which should have this
5176 * ViewGroup as its parent,
5177 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
5178 * is removed from its parent. This allows animations, such as those used by
5179 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
5180 * the removal of views. A call to this method should always be accompanied by a later call
5181 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
5182 * so that the View finally gets removed.
5183 *
5184 * @param view The View object to be kept visible even if it gets removed from its parent.
5185 */
5186 public void startViewTransition(View view) {
5187 if (view.mParent == this) {
5188 if (mTransitioningViews == null) {
5189 mTransitioningViews = new ArrayList<View>();
5190 }
5191 mTransitioningViews.add(view);
5192 }
5193 }
5194
5195 /**
5196 * This method should always be called following an earlier call to
5197 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
5198 * and will no longer be displayed. Note that this method does not perform the functionality
5199 * of removing a view from its parent; it just discontinues the display of a View that
5200 * has previously been removed.
5201 *
5202 * @return view The View object that has been removed but is being kept around in the visible
5203 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
5204 */
5205 public void endViewTransition(View view) {
5206 if (mTransitioningViews != null) {
5207 mTransitioningViews.remove(view);
5208 final ArrayList<View> disappearingChildren = mDisappearingChildren;
5209 if (disappearingChildren != null && disappearingChildren.contains(view)) {
5210 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005211 if (mVisibilityChangingChildren != null &&
5212 mVisibilityChangingChildren.contains(view)) {
5213 mVisibilityChangingChildren.remove(view);
5214 } else {
5215 if (view.mAttachInfo != null) {
5216 view.dispatchDetachedFromWindow();
5217 }
5218 if (view.mParent != null) {
5219 view.mParent = null;
5220 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07005221 }
Chet Haaseb85967b2012-03-26 14:37:51 -07005222 invalidate();
Chet Haaseb20db3e2010-09-10 13:07:30 -07005223 }
5224 }
5225 }
5226
Chet Haase21cd1382010-09-01 17:42:29 -07005227 private LayoutTransition.TransitionListener mLayoutTransitionListener =
5228 new LayoutTransition.TransitionListener() {
5229 @Override
5230 public void startTransition(LayoutTransition transition, ViewGroup container,
5231 View view, int transitionType) {
5232 // We only care about disappearing items, since we need special logic to keep
5233 // those items visible after they've been 'removed'
5234 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07005235 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07005236 }
5237 }
5238
5239 @Override
5240 public void endTransition(LayoutTransition transition, ViewGroup container,
5241 View view, int transitionType) {
Chet Haaseb9895022013-04-02 15:10:58 -07005242 if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) {
Chet Haase9c087442011-01-12 16:20:16 -08005243 requestLayout();
Chet Haaseb9895022013-04-02 15:10:58 -07005244 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08005245 }
Chet Haase21cd1382010-09-01 17:42:29 -07005246 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07005247 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07005248 }
5249 }
5250 };
5251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 /**
Chet Haaseb9895022013-04-02 15:10:58 -07005253 * Tells this ViewGroup to suppress all layout() calls until layout
5254 * suppression is disabled with a later call to suppressLayout(false).
5255 * When layout suppression is disabled, a requestLayout() call is sent
5256 * if layout() was attempted while layout was being suppressed.
5257 *
5258 * @hide
5259 */
5260 public void suppressLayout(boolean suppress) {
5261 mSuppressLayout = suppress;
5262 if (!suppress) {
5263 if (mLayoutCalledWhileSuppressed) {
5264 requestLayout();
5265 mLayoutCalledWhileSuppressed = false;
5266 }
5267 }
5268 }
5269
5270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 * {@inheritDoc}
5272 */
5273 @Override
5274 public boolean gatherTransparentRegion(Region region) {
5275 // If no transparent regions requested, we are always opaque.
Dianne Hackborn4702a852012-08-17 15:18:29 -07005276 final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 if (meOpaque && region == null) {
5278 // The caller doesn't care about the region, so stop now.
5279 return true;
5280 }
5281 super.gatherTransparentRegion(region);
5282 final View[] children = mChildren;
5283 final int count = mChildrenCount;
5284 boolean noneOfTheChildrenAreTransparent = true;
5285 for (int i = 0; i < count; i++) {
5286 final View child = children[i];
Mathias Agopiane3381152010-12-02 15:19:36 -08005287 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 if (!child.gatherTransparentRegion(region)) {
5289 noneOfTheChildrenAreTransparent = false;
5290 }
5291 }
5292 }
5293 return meOpaque || noneOfTheChildrenAreTransparent;
5294 }
5295
5296 /**
5297 * {@inheritDoc}
5298 */
5299 public void requestTransparentRegion(View child) {
5300 if (child != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005301 child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 if (mParent != null) {
5303 mParent.requestTransparentRegion(this);
5304 }
5305 }
5306 }
Romain Guy8506ab42009-06-11 17:35:47 -07005307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308
5309 @Override
5310 protected boolean fitSystemWindows(Rect insets) {
5311 boolean done = super.fitSystemWindows(insets);
5312 if (!done) {
5313 final int count = mChildrenCount;
5314 final View[] children = mChildren;
5315 for (int i = 0; i < count; i++) {
5316 done = children[i].fitSystemWindows(insets);
5317 if (done) {
5318 break;
5319 }
5320 }
5321 }
5322 return done;
5323 }
5324
5325 /**
5326 * Returns the animation listener to which layout animation events are
5327 * sent.
5328 *
5329 * @return an {@link android.view.animation.Animation.AnimationListener}
5330 */
5331 public Animation.AnimationListener getLayoutAnimationListener() {
5332 return mAnimationListener;
5333 }
5334
5335 @Override
5336 protected void drawableStateChanged() {
5337 super.drawableStateChanged();
5338
5339 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
5340 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
5341 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
5342 + " child has duplicateParentState set to true");
5343 }
5344
5345 final View[] children = mChildren;
5346 final int count = mChildrenCount;
5347
5348 for (int i = 0; i < count; i++) {
5349 final View child = children[i];
5350 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
5351 child.refreshDrawableState();
5352 }
5353 }
5354 }
5355 }
5356
5357 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07005358 public void jumpDrawablesToCurrentState() {
5359 super.jumpDrawablesToCurrentState();
5360 final View[] children = mChildren;
5361 final int count = mChildrenCount;
5362 for (int i = 0; i < count; i++) {
5363 children[i].jumpDrawablesToCurrentState();
5364 }
5365 }
5366
5367 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 protected int[] onCreateDrawableState(int extraSpace) {
5369 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
5370 return super.onCreateDrawableState(extraSpace);
5371 }
5372
5373 int need = 0;
5374 int n = getChildCount();
5375 for (int i = 0; i < n; i++) {
5376 int[] childState = getChildAt(i).getDrawableState();
5377
5378 if (childState != null) {
5379 need += childState.length;
5380 }
5381 }
5382
5383 int[] state = super.onCreateDrawableState(extraSpace + need);
5384
5385 for (int i = 0; i < n; i++) {
5386 int[] childState = getChildAt(i).getDrawableState();
5387
5388 if (childState != null) {
5389 state = mergeDrawableStates(state, childState);
5390 }
5391 }
5392
5393 return state;
5394 }
5395
5396 /**
5397 * Sets whether this ViewGroup's drawable states also include
5398 * its children's drawable states. This is used, for example, to
5399 * make a group appear to be focused when its child EditText or button
5400 * is focused.
5401 */
5402 public void setAddStatesFromChildren(boolean addsStates) {
5403 if (addsStates) {
5404 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
5405 } else {
5406 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
5407 }
5408
5409 refreshDrawableState();
5410 }
5411
5412 /**
5413 * Returns whether this ViewGroup's drawable states also include
5414 * its children's drawable states. This is used, for example, to
5415 * make a group appear to be focused when its child EditText or button
5416 * is focused.
5417 */
5418 public boolean addStatesFromChildren() {
5419 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
5420 }
5421
5422 /**
Jeff Smitha45746e2012-07-19 14:19:24 -05005423 * If {@link #addStatesFromChildren} is true, refreshes this group's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 * drawable state (to include the states from its children).
5425 */
5426 public void childDrawableStateChanged(View child) {
5427 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
5428 refreshDrawableState();
5429 }
5430 }
5431
5432 /**
5433 * Specifies the animation listener to which layout animation events must
5434 * be sent. Only
5435 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
5436 * and
5437 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
5438 * are invoked.
5439 *
5440 * @param animationListener the layout animation listener
5441 */
5442 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
5443 mAnimationListener = animationListener;
5444 }
5445
5446 /**
Chet Haasecca2c982011-05-20 14:34:18 -07005447 * This method is called by LayoutTransition when there are 'changing' animations that need
5448 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
5449 * starts all pending transitions prior to the drawing phase in the current traversal.
5450 *
5451 * @param transition The LayoutTransition to be started on the next traversal.
5452 *
5453 * @hide
5454 */
5455 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005456 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07005457 if (viewAncestor != null) {
5458 viewAncestor.requestTransitionStart(transition);
5459 }
Chet Haasecca2c982011-05-20 14:34:18 -07005460 }
5461
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005462 /**
5463 * @hide
5464 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005465 @Override
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07005466 public boolean resolveRtlPropertiesIfNeeded() {
5467 final boolean result = super.resolveRtlPropertiesIfNeeded();
5468 // We dont need to resolve the children RTL properties if nothing has changed for the parent
5469 if (result) {
5470 int count = getChildCount();
5471 for (int i = 0; i < count; i++) {
5472 final View child = getChildAt(i);
5473 if (child.isLayoutDirectionInherited()) {
5474 child.resolveRtlPropertiesIfNeeded();
5475 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07005476 }
5477 }
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07005478 return result;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07005479 }
5480
5481 /**
5482 * @hide
5483 */
5484 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005485 public boolean resolveLayoutDirection() {
5486 final boolean result = super.resolveLayoutDirection();
5487 if (result) {
5488 int count = getChildCount();
5489 for (int i = 0; i < count; i++) {
5490 final View child = getChildAt(i);
5491 if (child.isLayoutDirectionInherited()) {
5492 child.resolveLayoutDirection();
5493 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005494 }
5495 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005496 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005497 }
5498
5499 /**
5500 * @hide
5501 */
5502 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005503 public boolean resolveTextDirection() {
5504 final boolean result = super.resolveTextDirection();
5505 if (result) {
5506 int count = getChildCount();
5507 for (int i = 0; i < count; i++) {
5508 final View child = getChildAt(i);
5509 if (child.isTextDirectionInherited()) {
5510 child.resolveTextDirection();
5511 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005512 }
5513 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005514 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005515 }
5516
5517 /**
5518 * @hide
5519 */
5520 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005521 public boolean resolveTextAlignment() {
5522 final boolean result = super.resolveTextAlignment();
5523 if (result) {
5524 int count = getChildCount();
5525 for (int i = 0; i < count; i++) {
5526 final View child = getChildAt(i);
5527 if (child.isTextAlignmentInherited()) {
5528 child.resolveTextAlignment();
5529 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005530 }
5531 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005532 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005533 }
5534
5535 /**
5536 * @hide
5537 */
5538 @Override
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07005539 public void resolvePadding() {
5540 super.resolvePadding();
5541 int count = getChildCount();
5542 for (int i = 0; i < count; i++) {
5543 final View child = getChildAt(i);
5544 if (child.isLayoutDirectionInherited()) {
5545 child.resolvePadding();
5546 }
5547 }
5548 }
5549
5550 /**
5551 * @hide
5552 */
5553 @Override
5554 protected void resolveDrawables() {
5555 super.resolveDrawables();
5556 int count = getChildCount();
5557 for (int i = 0; i < count; i++) {
5558 final View child = getChildAt(i);
5559 if (child.isLayoutDirectionInherited()) {
5560 child.resolveDrawables();
5561 }
5562 }
5563 }
5564
Fabrice Di Meglio1e0ed6b2012-10-18 16:06:52 -07005565 /**
5566 * @hide
5567 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -07005568 @Override
5569 public void resolveLayoutParams() {
5570 super.resolveLayoutParams();
5571 int count = getChildCount();
5572 for (int i = 0; i < count; i++) {
5573 final View child = getChildAt(i);
5574 child.resolveLayoutParams();
5575 }
5576 }
5577
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07005578 /**
5579 * @hide
5580 */
5581 @Override
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005582 public void resetResolvedLayoutDirection() {
5583 super.resetResolvedLayoutDirection();
5584
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005585 int count = getChildCount();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005586 for (int i = 0; i < count; i++) {
5587 final View child = getChildAt(i);
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005588 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005589 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005590 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005591 }
5592 }
5593
5594 /**
5595 * @hide
5596 */
5597 @Override
5598 public void resetResolvedTextDirection() {
5599 super.resetResolvedTextDirection();
5600
5601 int count = getChildCount();
5602 for (int i = 0; i < count; i++) {
5603 final View child = getChildAt(i);
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -07005604 if (child.isTextDirectionInherited()) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07005605 child.resetResolvedTextDirection();
5606 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005607 }
5608 }
5609
5610 /**
5611 * @hide
5612 */
5613 @Override
5614 public void resetResolvedTextAlignment() {
5615 super.resetResolvedTextAlignment();
5616
5617 int count = getChildCount();
5618 for (int i = 0; i < count; i++) {
5619 final View child = getChildAt(i);
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -07005620 if (child.isTextAlignmentInherited()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07005621 child.resetResolvedTextAlignment();
5622 }
5623 }
5624 }
5625
Fabrice Di Meglio22268862011-06-27 18:13:18 -07005626 /**
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07005627 * @hide
5628 */
5629 @Override
5630 public void resetResolvedPadding() {
5631 super.resetResolvedPadding();
5632
5633 int count = getChildCount();
5634 for (int i = 0; i < count; i++) {
5635 final View child = getChildAt(i);
5636 if (child.isLayoutDirectionInherited()) {
5637 child.resetResolvedPadding();
5638 }
5639 }
5640 }
5641
5642 /**
5643 * @hide
5644 */
5645 @Override
5646 protected void resetResolvedDrawables() {
5647 super.resetResolvedDrawables();
5648
5649 int count = getChildCount();
5650 for (int i = 0; i < count; i++) {
5651 final View child = getChildAt(i);
5652 if (child.isLayoutDirectionInherited()) {
5653 child.resetResolvedDrawables();
5654 }
5655 }
5656 }
5657
5658 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005659 * Return true if the pressed state should be delayed for children or descendants of this
5660 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
5661 * This prevents the pressed state from appearing when the user is actually trying to scroll
5662 * the content.
5663 *
5664 * The default implementation returns true for compatibility reasons. Subclasses that do
5665 * not scroll should generally override this method and return false.
5666 */
5667 public boolean shouldDelayChildPressedState() {
5668 return true;
5669 }
5670
Philip Milned7dd8902012-01-26 16:55:30 -08005671 /** @hide */
5672 protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
5673 }
5674
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 * LayoutParams are used by views to tell their parents how they want to be
5677 * laid out. See
5678 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
5679 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07005680 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 * <p>
5682 * The base LayoutParams class just describes how big the view wants to be
5683 * for both width and height. For each dimension, it can specify one of:
5684 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005685 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
5686 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
5688 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005689 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 * </ul>
5691 * There are subclasses of LayoutParams for different subclasses of
5692 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07005693 * LayoutParams which adds an X and Y value.</p>
5694 *
5695 * <div class="special reference">
5696 * <h3>Developer Guides</h3>
5697 * <p>For more information about creating user interface layouts, read the
5698 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
5699 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 *
5701 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
5702 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
5703 */
5704 public static class LayoutParams {
5705 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005706 * Special value for the height or width requested by a View.
5707 * FILL_PARENT means that the view wants to be as big as its parent,
5708 * minus the parent's padding, if any. This value is deprecated
5709 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 */
Romain Guy980a9382010-01-08 15:06:28 -08005711 @SuppressWarnings({"UnusedDeclaration"})
5712 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 public static final int FILL_PARENT = -1;
5714
5715 /**
5716 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08005717 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005718 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08005719 */
5720 public static final int MATCH_PARENT = -1;
5721
5722 /**
5723 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 * WRAP_CONTENT means that the view wants to be just large enough to fit
5725 * its own internal content, taking its own padding into account.
5726 */
5727 public static final int WRAP_CONTENT = -2;
5728
5729 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005730 * Information about how wide the view wants to be. Can be one of the
5731 * constants FILL_PARENT (replaced by MATCH_PARENT ,
5732 * in API Level 8) or WRAP_CONTENT. or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005734 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08005735 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
5737 })
5738 public int width;
5739
5740 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005741 * Information about how tall the view wants to be. Can be one of the
5742 * constants FILL_PARENT (replaced by MATCH_PARENT ,
5743 * in API Level 8) or WRAP_CONTENT. or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005745 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08005746 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
5748 })
5749 public int height;
5750
5751 /**
5752 * Used to animate layouts.
5753 */
5754 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
5755
5756 /**
5757 * Creates a new set of layout parameters. The values are extracted from
5758 * the supplied attributes set and context. The XML attributes mapped
5759 * to this set of layout parameters are:
5760 *
5761 * <ul>
5762 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005763 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
5764 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005766 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
5767 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 * </ul>
5769 *
5770 * @param c the application environment
5771 * @param attrs the set of attributes from which to extract the layout
5772 * parameters' values
5773 */
5774 public LayoutParams(Context c, AttributeSet attrs) {
5775 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
5776 setBaseAttributes(a,
5777 R.styleable.ViewGroup_Layout_layout_width,
5778 R.styleable.ViewGroup_Layout_layout_height);
5779 a.recycle();
5780 }
5781
5782 /**
5783 * Creates a new set of layout parameters with the specified width
5784 * and height.
5785 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07005786 * @param width the width, either {@link #WRAP_CONTENT},
5787 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
5788 * API Level 8), or a fixed size in pixels
5789 * @param height the height, either {@link #WRAP_CONTENT},
5790 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
5791 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 */
5793 public LayoutParams(int width, int height) {
5794 this.width = width;
5795 this.height = height;
5796 }
5797
5798 /**
5799 * Copy constructor. Clones the width and height values of the source.
5800 *
5801 * @param source The layout params to copy from.
5802 */
5803 public LayoutParams(LayoutParams source) {
5804 this.width = source.width;
5805 this.height = source.height;
5806 }
5807
5808 /**
5809 * Used internally by MarginLayoutParams.
5810 * @hide
5811 */
5812 LayoutParams() {
5813 }
5814
5815 /**
Dave Burke579e1402012-10-18 20:41:55 -07005816 * Extracts the layout parameters from the supplied attributes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 *
5818 * @param a the style attributes to extract the parameters from
5819 * @param widthAttr the identifier of the width attribute
5820 * @param heightAttr the identifier of the height attribute
5821 */
5822 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
Dave Burke579e1402012-10-18 20:41:55 -07005823 width = a.getLayoutDimension(widthAttr, "layout_width");
5824 height = a.getLayoutDimension(heightAttr, "layout_height");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 }
5826
5827 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005828 * Resolve layout parameters depending on the layout direction. Subclasses that care about
5829 * layoutDirection changes should override this method. The default implementation does
5830 * nothing.
5831 *
5832 * @param layoutDirection the direction of the layout
5833 *
5834 * {@link View#LAYOUT_DIRECTION_LTR}
5835 * {@link View#LAYOUT_DIRECTION_RTL}
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005836 */
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07005837 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005838 }
5839
5840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 * Returns a String representation of this set of layout parameters.
5842 *
5843 * @param output the String to prepend to the internal representation
5844 * @return a String with the following format: output +
5845 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07005846 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 * @hide
5848 */
5849 public String debug(String output) {
5850 return output + "ViewGroup.LayoutParams={ width="
5851 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
5852 }
5853
5854 /**
Philip Milne10ca24a2012-04-23 15:38:27 -07005855 * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
5856 *
5857 * @param view the view that contains these layout parameters
5858 * @param canvas the canvas on which to draw
5859 *
5860 * @hide
5861 */
Philip Milne7b757812012-09-19 18:13:44 -07005862 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07005863 }
5864
5865 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 * Converts the specified size to a readable String.
5867 *
5868 * @param size the size to convert
5869 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07005870 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 * @hide
5872 */
5873 protected static String sizeToString(int size) {
5874 if (size == WRAP_CONTENT) {
5875 return "wrap-content";
5876 }
Romain Guy980a9382010-01-08 15:06:28 -08005877 if (size == MATCH_PARENT) {
5878 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 }
5880 return String.valueOf(size);
5881 }
5882 }
5883
5884 /**
5885 * Per-child layout information for layouts that support margins.
5886 * See
5887 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
5888 * for a list of all child view attributes that this class supports.
5889 */
5890 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
5891 /**
Philip Milned7dd8902012-01-26 16:55:30 -08005892 * The left margin in pixels of the child.
5893 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5894 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005896 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 public int leftMargin;
5898
5899 /**
Philip Milned7dd8902012-01-26 16:55:30 -08005900 * The top margin in pixels of the child.
5901 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5902 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005904 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 public int topMargin;
5906
5907 /**
Philip Milned7dd8902012-01-26 16:55:30 -08005908 * The right margin in pixels of the child.
5909 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5910 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005912 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 public int rightMargin;
5914
5915 /**
Philip Milned7dd8902012-01-26 16:55:30 -08005916 * The bottom margin in pixels of the child.
5917 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5918 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005920 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 public int bottomMargin;
5922
5923 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005924 * The start margin in pixels of the child.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08005925 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5926 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005927 */
5928 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07005929 private int startMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005930
5931 /**
5932 * The end margin in pixels of the child.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08005933 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
5934 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005935 */
5936 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07005937 private int endMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005938
5939 /**
5940 * The default start and end margin.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07005941 * @hide
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005942 */
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07005943 public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005944
Fabrice Di Megliob365f912013-03-27 16:36:21 -07005945 /**
5946 * Bit 0: layout direction
5947 * Bit 1: layout direction
5948 * Bit 2: left margin undefined
5949 * Bit 3: right margin undefined
5950 * Bit 4: is RTL compatibility mode
5951 * Bit 5: need resolution
5952 *
5953 * Bit 6 to 7 not used
5954 *
5955 * @hide
5956 */
5957 @ViewDebug.ExportedProperty(category = "layout", flagMapping = {
5958 @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK,
5959 equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"),
5960 @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK,
5961 equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"),
5962 @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK,
5963 equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"),
5964 @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK,
5965 equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"),
5966 @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK,
5967 equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK")
5968 })
5969 byte mMarginFlags;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07005970
Fabrice Di Megliob365f912013-03-27 16:36:21 -07005971 private static final int LAYOUT_DIRECTION_MASK = 0x00000003;
5972 private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004;
5973 private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008;
5974 private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010;
5975 private static final int NEED_RESOLUTION_MASK = 0x00000020;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07005976
Fabrice Di Megliob365f912013-03-27 16:36:21 -07005977 private static final int DEFAULT_MARGIN_RESOLVED = 0;
5978 private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07005979
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07005980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 * Creates a new set of layout parameters. The values are extracted from
5982 * the supplied attributes set and context.
5983 *
5984 * @param c the application environment
5985 * @param attrs the set of attributes from which to extract the layout
5986 * parameters' values
5987 */
5988 public MarginLayoutParams(Context c, AttributeSet attrs) {
5989 super();
5990
5991 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
5992 setBaseAttributes(a,
5993 R.styleable.ViewGroup_MarginLayout_layout_width,
5994 R.styleable.ViewGroup_MarginLayout_layout_height);
5995
5996 int margin = a.getDimensionPixelSize(
5997 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
5998 if (margin >= 0) {
5999 leftMargin = margin;
6000 topMargin = margin;
6001 rightMargin= margin;
6002 bottomMargin = margin;
6003 } else {
6004 leftMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006005 R.styleable.ViewGroup_MarginLayout_layout_marginLeft,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006006 UNDEFINED_MARGIN);
6007 if (leftMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006008 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006009 leftMargin = DEFAULT_MARGIN_RESOLVED;
6010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 rightMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006012 R.styleable.ViewGroup_MarginLayout_layout_marginRight,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006013 UNDEFINED_MARGIN);
6014 if (rightMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006015 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006016 rightMargin = DEFAULT_MARGIN_RESOLVED;
6017 }
6018
6019 topMargin = a.getDimensionPixelSize(
6020 R.styleable.ViewGroup_MarginLayout_layout_marginTop,
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006021 DEFAULT_MARGIN_RESOLVED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 bottomMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006023 R.styleable.ViewGroup_MarginLayout_layout_marginBottom,
6024 DEFAULT_MARGIN_RESOLVED);
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006025
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006026 startMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006027 R.styleable.ViewGroup_MarginLayout_layout_marginStart,
6028 DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006029 endMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006030 R.styleable.ViewGroup_MarginLayout_layout_marginEnd,
6031 DEFAULT_MARGIN_RELATIVE);
6032
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006033 if (isMarginRelative()) {
6034 mMarginFlags |= NEED_RESOLUTION_MASK;
6035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 }
6037
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006038 final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
6039 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006040 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
6041 mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
6042 }
6043
6044 // Layout direction is LTR by default
6045 mMarginFlags |= LAYOUT_DIRECTION_LTR;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07006046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 a.recycle();
6048 }
6049
6050 /**
6051 * {@inheritDoc}
6052 */
6053 public MarginLayoutParams(int width, int height) {
6054 super(width, height);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006055
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006056 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
6057 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006058
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006059 mMarginFlags &= ~NEED_RESOLUTION_MASK;
6060 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 }
6062
6063 /**
6064 * Copy constructor. Clones the width, height and margin values of the source.
6065 *
6066 * @param source The layout params to copy from.
6067 */
6068 public MarginLayoutParams(MarginLayoutParams source) {
6069 this.width = source.width;
6070 this.height = source.height;
6071
6072 this.leftMargin = source.leftMargin;
6073 this.topMargin = source.topMargin;
6074 this.rightMargin = source.rightMargin;
6075 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006076 this.startMargin = source.startMargin;
6077 this.endMargin = source.endMargin;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07006078
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006079 this.mMarginFlags = source.mMarginFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 }
6081
6082 /**
6083 * {@inheritDoc}
6084 */
6085 public MarginLayoutParams(LayoutParams source) {
6086 super(source);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006087
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006088 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
6089 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006090
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006091 mMarginFlags &= ~NEED_RESOLUTION_MASK;
6092 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 }
6094
6095 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006096 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
6097 * to be done so that the new margins are taken into account. Left and right margins may be
6098 * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 *
6100 * @param left the left margin size
6101 * @param top the top margin size
6102 * @param right the right margin size
6103 * @param bottom the bottom margin size
6104 *
6105 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
6106 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
6107 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
6108 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
6109 */
6110 public void setMargins(int left, int top, int right, int bottom) {
6111 leftMargin = left;
6112 topMargin = top;
6113 rightMargin = right;
6114 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006115 mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
6116 mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
6117 if (isMarginRelative()) {
6118 mMarginFlags |= NEED_RESOLUTION_MASK;
6119 } else {
6120 mMarginFlags &= ~NEED_RESOLUTION_MASK;
6121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006123
6124 /**
6125 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
6126 * needs to be done so that the new relative margins are taken into account. Left and right
6127 * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
6128 * direction.
6129 *
6130 * @param start the start margin size
6131 * @param top the top margin size
6132 * @param end the right margin size
6133 * @param bottom the bottom margin size
6134 *
6135 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
6136 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
6137 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
6138 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
6139 *
6140 * @hide
6141 */
6142 public void setMarginsRelative(int start, int top, int end, int bottom) {
6143 startMargin = start;
6144 topMargin = top;
6145 endMargin = end;
6146 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006147 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006148 }
6149
6150 /**
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07006151 * Sets the relative start margin.
6152 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07006153 * @param start the start margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07006154 *
6155 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
6156 */
6157 public void setMarginStart(int start) {
6158 startMargin = start;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006159 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07006160 }
6161
6162 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006163 * Returns the start margin in pixels.
6164 *
6165 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
6166 *
6167 * @return the start margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006168 */
6169 public int getMarginStart() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006170 if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006171 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006172 doResolveMargins();
6173 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006174 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07006175 case View.LAYOUT_DIRECTION_RTL:
6176 return rightMargin;
6177 case View.LAYOUT_DIRECTION_LTR:
6178 default:
6179 return leftMargin;
6180 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006181 }
6182
6183 /**
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07006184 * Sets the relative end margin.
6185 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07006186 * @param end the end margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07006187 *
6188 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
6189 */
6190 public void setMarginEnd(int end) {
6191 endMargin = end;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006192 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07006193 }
6194
6195 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006196 * Returns the end margin in pixels.
6197 *
6198 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
6199 *
6200 * @return the end margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006201 */
6202 public int getMarginEnd() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006203 if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006204 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006205 doResolveMargins();
6206 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006207 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07006208 case View.LAYOUT_DIRECTION_RTL:
6209 return leftMargin;
6210 case View.LAYOUT_DIRECTION_LTR:
6211 default:
6212 return rightMargin;
6213 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006214 }
6215
6216 /**
6217 * Check if margins are relative.
6218 *
6219 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
6220 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
6221 *
Fabrice Di Megliof443f982012-07-13 20:24:03 -07006222 * @return true if either marginStart or marginEnd has been set.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006223 */
6224 public boolean isMarginRelative() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006225 return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006226 }
6227
6228 /**
Fabrice Di Megliof443f982012-07-13 20:24:03 -07006229 * Set the layout direction
6230 * @param layoutDirection the layout direction.
6231 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
6232 * or {@link View#LAYOUT_DIRECTION_RTL}.
6233 */
6234 public void setLayoutDirection(int layoutDirection) {
6235 if (layoutDirection != View.LAYOUT_DIRECTION_LTR &&
6236 layoutDirection != View.LAYOUT_DIRECTION_RTL) return;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006237 if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) {
6238 mMarginFlags &= ~LAYOUT_DIRECTION_MASK;
6239 mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK);
6240 if (isMarginRelative()) {
6241 mMarginFlags |= NEED_RESOLUTION_MASK;
6242 } else {
6243 mMarginFlags &= ~NEED_RESOLUTION_MASK;
6244 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006245 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07006246 }
6247
6248 /**
6249 * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or
6250 * {@link View#LAYOUT_DIRECTION_RTL}.
6251 *
6252 * @return the layout direction.
6253 */
6254 public int getLayoutDirection() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006255 return (mMarginFlags & LAYOUT_DIRECTION_MASK);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07006256 }
6257
6258 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006259 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08006260 * may be overridden depending on layout direction.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006261 */
6262 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07006263 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07006264 setLayoutDirection(layoutDirection);
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07006265
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006266 // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything
6267 // Will use the left and right margins if no relative margin is defined.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006268 if (!isMarginRelative() ||
6269 (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07006270
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07006271 // Proceed with resolution
6272 doResolveMargins();
6273 }
6274
6275 private void doResolveMargins() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006276 if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006277 // if left or right margins are not defined and if we have some start or end margin
6278 // defined then use those start and end margins.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006279 if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK
6280 && startMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006281 leftMargin = startMargin;
6282 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006283 if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK
6284 && endMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006285 rightMargin = endMargin;
6286 }
6287 } else {
6288 // We have some relative margins (either the start one or the end one or both). So use
6289 // them and override what has been defined for left and right margins. If either start
6290 // or end margin is not defined, just set it to default "0".
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006291 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07006292 case View.LAYOUT_DIRECTION_RTL:
6293 leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
6294 endMargin : DEFAULT_MARGIN_RESOLVED;
6295 rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
6296 startMargin : DEFAULT_MARGIN_RESOLVED;
6297 break;
6298 case View.LAYOUT_DIRECTION_LTR:
6299 default:
6300 leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
6301 startMargin : DEFAULT_MARGIN_RESOLVED;
6302 rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
6303 endMargin : DEFAULT_MARGIN_RESOLVED;
6304 break;
6305 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006306 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006307 mMarginFlags &= ~NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006308 }
Philip Milne10ca24a2012-04-23 15:38:27 -07006309
Fabrice Di Meglio03b8d3a2012-09-27 17:05:27 -07006310 /**
6311 * @hide
6312 */
6313 public boolean isLayoutRtl() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07006314 return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07006315 }
6316
Philip Milne10ca24a2012-04-23 15:38:27 -07006317 /**
6318 * @hide
6319 */
6320 @Override
Philip Milne7b757812012-09-19 18:13:44 -07006321 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
6322 Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE;
6323
6324 fillDifference(canvas,
6325 view.getLeft() + oi.left,
6326 view.getTop() + oi.top,
6327 view.getRight() - oi.right,
6328 view.getBottom() - oi.bottom,
6329 leftMargin,
6330 topMargin,
6331 rightMargin,
6332 bottomMargin,
6333 paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07006334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 }
Adam Powell2b342f02010-08-18 18:14:13 -07006336
Jeff Brown20e987b2010-08-23 12:01:02 -07006337 /* Describes a touched view and the ids of the pointers that it has captured.
6338 *
6339 * This code assumes that pointer ids are always in the range 0..31 such that
6340 * it can use a bitfield to track which pointer ids are present.
6341 * As it happens, the lower layers of the input dispatch pipeline also use the
6342 * same trick so the assumption should be safe here...
6343 */
6344 private static final class TouchTarget {
6345 private static final int MAX_RECYCLED = 32;
6346 private static final Object sRecycleLock = new Object();
6347 private static TouchTarget sRecycleBin;
6348 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07006349
Jeff Brown20e987b2010-08-23 12:01:02 -07006350 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07006351
Jeff Brown20e987b2010-08-23 12:01:02 -07006352 // The touched child view.
6353 public View child;
6354
6355 // The combined bit mask of pointer ids for all pointers captured by the target.
6356 public int pointerIdBits;
6357
6358 // The next target in the target list.
6359 public TouchTarget next;
6360
6361 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07006362 }
6363
Jeff Brown20e987b2010-08-23 12:01:02 -07006364 public static TouchTarget obtain(View child, int pointerIdBits) {
6365 final TouchTarget target;
6366 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07006367 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006368 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07006369 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07006370 target = sRecycleBin;
6371 sRecycleBin = target.next;
6372 sRecycledCount--;
6373 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07006374 }
Adam Powell816c3be2010-08-23 18:00:05 -07006375 }
Jeff Brown20e987b2010-08-23 12:01:02 -07006376 target.child = child;
6377 target.pointerIdBits = pointerIdBits;
6378 return target;
6379 }
Adam Powell816c3be2010-08-23 18:00:05 -07006380
Jeff Brown20e987b2010-08-23 12:01:02 -07006381 public void recycle() {
6382 synchronized (sRecycleLock) {
6383 if (sRecycledCount < MAX_RECYCLED) {
6384 next = sRecycleBin;
6385 sRecycleBin = this;
6386 sRecycledCount += 1;
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07006387 } else {
6388 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07006389 }
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07006390 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07006391 }
6392 }
Adam Powell2b342f02010-08-18 18:14:13 -07006393 }
Jeff Brown87b7f802011-06-21 18:35:45 -07006394
6395 /* Describes a hovered view. */
6396 private static final class HoverTarget {
6397 private static final int MAX_RECYCLED = 32;
6398 private static final Object sRecycleLock = new Object();
6399 private static HoverTarget sRecycleBin;
6400 private static int sRecycledCount;
6401
6402 // The hovered child view.
6403 public View child;
6404
6405 // The next target in the target list.
6406 public HoverTarget next;
6407
6408 private HoverTarget() {
6409 }
6410
6411 public static HoverTarget obtain(View child) {
6412 final HoverTarget target;
6413 synchronized (sRecycleLock) {
6414 if (sRecycleBin == null) {
6415 target = new HoverTarget();
6416 } else {
6417 target = sRecycleBin;
6418 sRecycleBin = target.next;
6419 sRecycledCount--;
6420 target.next = null;
6421 }
6422 }
6423 target.child = child;
6424 return target;
6425 }
6426
6427 public void recycle() {
6428 synchronized (sRecycleLock) {
6429 if (sRecycledCount < MAX_RECYCLED) {
6430 next = sRecycleBin;
6431 sRecycleBin = this;
6432 sRecycledCount += 1;
6433 } else {
6434 next = null;
6435 }
6436 child = null;
6437 }
6438 }
6439 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006440
6441 /**
6442 * Pooled class that orderes the children of a ViewGroup from start
6443 * to end based on how they are laid out and the layout direction.
6444 */
6445 static class ChildListForAccessibility {
6446
6447 private static final int MAX_POOL_SIZE = 32;
6448
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006449 private static final SynchronizedPool<ChildListForAccessibility> sPool =
6450 new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006451
6452 private final ArrayList<View> mChildren = new ArrayList<View>();
6453
6454 private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
6455
6456 public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006457 ChildListForAccessibility list = sPool.acquire();
6458 if (list == null) {
6459 list = new ChildListForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006460 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006461 list.init(parent, sort);
6462 return list;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006463 }
6464
6465 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006466 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006467 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006468 }
6469
6470 public int getChildCount() {
6471 return mChildren.size();
6472 }
6473
6474 public View getChildAt(int index) {
6475 return mChildren.get(index);
6476 }
6477
6478 public int getChildIndex(View child) {
6479 return mChildren.indexOf(child);
6480 }
6481
6482 private void init(ViewGroup parent, boolean sort) {
6483 ArrayList<View> children = mChildren;
6484 final int childCount = parent.getChildCount();
6485 for (int i = 0; i < childCount; i++) {
6486 View child = parent.getChildAt(i);
6487 children.add(child);
6488 }
6489 if (sort) {
6490 ArrayList<ViewLocationHolder> holders = mHolders;
6491 for (int i = 0; i < childCount; i++) {
6492 View child = children.get(i);
6493 ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
6494 holders.add(holder);
6495 }
6496 Collections.sort(holders);
6497 for (int i = 0; i < childCount; i++) {
6498 ViewLocationHolder holder = holders.get(i);
6499 children.set(i, holder.mView);
6500 holder.recycle();
6501 }
6502 holders.clear();
6503 }
6504 }
6505
6506 private void clear() {
6507 mChildren.clear();
6508 }
6509 }
6510
6511 /**
6512 * Pooled class that holds a View and its location with respect to
6513 * a specified root. This enables sorting of views based on their
6514 * coordinates without recomputing the position relative to the root
6515 * on every comparison.
6516 */
6517 static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
6518
6519 private static final int MAX_POOL_SIZE = 32;
6520
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006521 private static final SynchronizedPool<ViewLocationHolder> sPool =
6522 new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006523
6524 private final Rect mLocation = new Rect();
6525
6526 public View mView;
6527
6528 private int mLayoutDirection;
6529
6530 public static ViewLocationHolder obtain(ViewGroup root, View view) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006531 ViewLocationHolder holder = sPool.acquire();
6532 if (holder == null) {
6533 holder = new ViewLocationHolder();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006534 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006535 holder.init(root, view);
6536 return holder;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006537 }
6538
6539 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006540 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08006541 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006542 }
6543
6544 @Override
6545 public int compareTo(ViewLocationHolder another) {
6546 // This instance is greater than an invalid argument.
6547 if (another == null) {
6548 return 1;
6549 }
6550 if (getClass() != another.getClass()) {
6551 return 1;
6552 }
6553 // First is above second.
6554 if (mLocation.bottom - another.mLocation.top <= 0) {
6555 return -1;
6556 }
6557 // First is below second.
6558 if (mLocation.top - another.mLocation.bottom >= 0) {
6559 return 1;
6560 }
6561 // LTR
6562 if (mLayoutDirection == LAYOUT_DIRECTION_LTR) {
6563 final int leftDifference = mLocation.left - another.mLocation.left;
6564 // First more to the left than second.
6565 if (leftDifference != 0) {
6566 return leftDifference;
6567 }
6568 } else { // RTL
6569 final int rightDifference = mLocation.right - another.mLocation.right;
6570 // First more to the right than second.
6571 if (rightDifference != 0) {
6572 return -rightDifference;
6573 }
6574 }
6575 // Break tie by top.
6576 final int topDiference = mLocation.top - another.mLocation.top;
6577 if (topDiference != 0) {
6578 return topDiference;
6579 }
6580 // Break tie by height.
6581 final int heightDiference = mLocation.height() - another.mLocation.height();
6582 if (heightDiference != 0) {
6583 return -heightDiference;
6584 }
6585 // Break tie by width.
6586 final int widthDiference = mLocation.width() - another.mLocation.width();
6587 if (widthDiference != 0) {
6588 return -widthDiference;
6589 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006590 // Just break the tie somehow. The accessibliity ids are unique
6591 // and stable, hence this is deterministic tie breaking.
6592 return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006593 }
6594
6595 private void init(ViewGroup root, View view) {
6596 Rect viewLocation = mLocation;
6597 view.getDrawingRect(viewLocation);
6598 root.offsetDescendantRectToMyCoords(view, viewLocation);
6599 mView = view;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006600 mLayoutDirection = root.getLayoutDirection();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006601 }
6602
6603 private void clear() {
6604 mView = null;
6605 mLocation.set(0, 0, 0, 0);
6606 }
6607 }
Romain Guycbc67742012-04-27 16:12:57 -07006608
6609 private static Paint getDebugPaint() {
6610 if (sDebugPaint == null) {
6611 sDebugPaint = new Paint();
6612 sDebugPaint.setAntiAlias(false);
6613 }
6614 return sDebugPaint;
6615 }
6616
Philip Milne7b757812012-09-19 18:13:44 -07006617 private void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
Romain Guycbc67742012-04-27 16:12:57 -07006618 if (sDebugLines== null) {
6619 sDebugLines = new float[16];
6620 }
6621
Romain Guycbc67742012-04-27 16:12:57 -07006622 sDebugLines[0] = x1;
6623 sDebugLines[1] = y1;
6624 sDebugLines[2] = x2;
6625 sDebugLines[3] = y1;
6626
6627 sDebugLines[4] = x2;
6628 sDebugLines[5] = y1;
6629 sDebugLines[6] = x2;
Philip Milne7b757812012-09-19 18:13:44 -07006630 sDebugLines[7] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07006631
Philip Milne7b757812012-09-19 18:13:44 -07006632 sDebugLines[8] = x2;
Romain Guycbc67742012-04-27 16:12:57 -07006633 sDebugLines[9] = y2;
6634 sDebugLines[10] = x1;
6635 sDebugLines[11] = y2;
6636
Philip Milne7b757812012-09-19 18:13:44 -07006637 sDebugLines[12] = x1;
6638 sDebugLines[13] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07006639 sDebugLines[14] = x1;
6640 sDebugLines[15] = y1;
6641
Philip Milne7b757812012-09-19 18:13:44 -07006642 canvas.drawLines(sDebugLines, paint);
Romain Guycbc67742012-04-27 16:12:57 -07006643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644}