blob: c09c332921e1e0a0cab50846d6b25c1acf920939 [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;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.IdRes;
Alan Viverette922e1c62015-05-05 17:18:27 -070021import android.annotation.NonNull;
Tor Norbye83c68962015-03-10 20:55:31 -070022import android.annotation.UiThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +000024import android.content.Intent;
Adam Powellff0d2982014-07-10 20:34:14 -070025import android.content.pm.PackageManager;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080026import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.res.TypedArray;
28import android.graphics.Bitmap;
29import android.graphics.Canvas;
Philip Milne10ca24a2012-04-23 15:38:27 -070030import android.graphics.Color;
31import android.graphics.Insets;
Adam Powell6e346362010-07-23 10:18:23 -070032import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Paint;
Christopher Tatea53146c2010-09-07 11:57:52 -070034import android.graphics.PointF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.graphics.RectF;
svetoslavganov75986cf2009-05-14 22:28:01 -070037import android.graphics.Region;
Jeff Brown995e7742010-12-22 16:59:36 -080038import android.os.Build;
Adam Powellb6ab0982015-01-07 17:00:12 -080039import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.Parcelable;
41import android.os.SystemClock;
42import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.util.Log;
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -080044import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.util.SparseArray;
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -070046import android.util.SparseBooleanArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070047import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070048import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.view.animation.Animation;
50import android.view.animation.AnimationUtils;
51import android.view.animation.LayoutAnimationController;
52import android.view.animation.Transformation;
Doug Feltcb3791202011-07-07 11:57:48 -070053
Romain Guy0211a0a2011-02-14 16:34:59 -080054import com.android.internal.R;
55import com.android.internal.util.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57import java.util.ArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -070058import java.util.Collections;
Christopher Tate86cab1b2011-01-13 20:28:55 -080059import java.util.HashSet;
George Mounte1803372014-02-26 19:00:52 +000060import java.util.List;
61import java.util.Map;
Fabrice Di Meglio0072f642013-03-26 15:50:24 -070062import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064/**
65 * <p>
66 * A <code>ViewGroup</code> is a special view that can contain other views
67 * (called children.) The view group is the base class for layouts and views
68 * containers. This class also defines the
69 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
70 * class for layouts parameters.
71 * </p>
72 *
73 * <p>
74 * Also see {@link LayoutParams} for layout attributes.
75 * </p>
Romain Guyd6a463a2009-05-21 23:10:10 -070076 *
Joe Fernandez558459f2011-10-13 16:47:36 -070077 * <div class="special reference">
78 * <h3>Developer Guides</h3>
79 * <p>For more information about creating user interface layouts, read the
80 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
81 * guide.</p></div>
82 *
Dianne Hackborn7caab0f2013-03-06 13:47:04 -080083 * <p>Here is a complete implementation of a custom ViewGroup that implements
84 * a simple {@link android.widget.FrameLayout} along with the ability to stack
85 * children in left and right gutters.</p>
86 *
87 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java
88 * Complete}
89 *
90 * <p>If you are implementing XML layout attributes as shown in the example, this is the
91 * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p>
92 *
93 * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout}
94 *
95 * <p>Finally the layout manager can be used in an XML layout like so:</p>
96 *
97 * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete}
98 *
Romain Guyd6a463a2009-05-21 23:10:10 -070099 * @attr ref android.R.styleable#ViewGroup_clipChildren
100 * @attr ref android.R.styleable#ViewGroup_clipToPadding
101 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
102 * @attr ref android.R.styleable#ViewGroup_animationCache
103 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
104 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
105 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
106 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
Chet Haase13cc1202010-09-03 15:39:20 -0700107 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Scott Main27a85082013-06-10 10:39:48 -0700108 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
109 * @attr ref android.R.styleable#ViewGroup_layoutMode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 */
Tor Norbye83c68962015-03-10 20:55:31 -0700111@UiThread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Adam Powell539ee872012-02-03 19:00:49 -0800113 private static final String TAG = "ViewGroup";
Chet Haase21cd1382010-09-01 17:42:29 -0700114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private static final boolean DBG = false;
Philip Milne7b757812012-09-19 18:13:44 -0700116 /** @hide */
117 public static boolean DEBUG_DRAW = false;
Gilles Debunnecea45132011-11-24 02:19:27 +0100118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
120 * Views which have been hidden or removed which need to be animated on
121 * their way out.
122 * This field should be made private, so it is hidden from the SDK.
123 * {@hide}
124 */
125 protected ArrayList<View> mDisappearingChildren;
126
127 /**
128 * Listener used to propagate events indicating when children are added
129 * and/or removed from a view group.
130 * This field should be made private, so it is hidden from the SDK.
131 * {@hide}
132 */
133 protected OnHierarchyChangeListener mOnHierarchyChangeListener;
134
135 // The view contained within this ViewGroup that has or contains focus.
136 private View mFocused;
137
Chet Haase48460322010-06-11 14:22:25 -0700138 /**
139 * A Transformation used when drawing children, to
140 * apply on the child being drawn.
141 */
Romain Guyf6991302013-06-05 17:19:01 -0700142 private Transformation mChildTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700143
144 /**
145 * Used to track the current invalidation region.
146 */
Chet Haase64a48c12012-02-13 16:33:29 -0800147 RectF mInvalidateRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Chet Haase48460322010-06-11 14:22:25 -0700149 /**
150 * A Transformation used to calculate a correct
151 * invalidation area when the application is autoscaled.
152 */
Chet Haase64a48c12012-02-13 16:33:29 -0800153 Transformation mInvalidationTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700154
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700155 // View currently under an ongoing drag. Can be null, a child or this window.
Christopher Tatea53146c2010-09-07 11:57:52 -0700156 private View mCurrentDragView;
157
Christopher Tate86cab1b2011-01-13 20:28:55 -0800158 // Metadata about the ongoing drag
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700159 private DragEvent mCurrentDragStartEvent;
160 private boolean mIsInterestedInDrag;
161 private HashSet<View> mChildrenInterestedInDrag;
Christopher Tatea53146c2010-09-07 11:57:52 -0700162
163 // Used during drag dispatch
Romain Guy6410c0a2013-06-17 11:21:58 -0700164 private PointF mLocalPoint;
Christopher Tatea53146c2010-09-07 11:57:52 -0700165
Alan Viveretteb942b6f2014-12-08 10:37:39 -0800166 // Lazily-created holder for point computations.
167 private float[] mTempPoint;
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 // Layout animation
170 private LayoutAnimationController mLayoutAnimationController;
171 private Animation.AnimationListener mAnimationListener;
172
Jeff Brown20e987b2010-08-23 12:01:02 -0700173 // First touch target in the linked list of touch targets.
174 private TouchTarget mFirstTouchTarget;
175
Joe Onorato03ab0c72011-01-06 15:46:27 -0800176 // For debugging only. You can see these in hierarchyviewer.
Romain Guye95003e2011-01-09 13:53:06 -0800177 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800178 @ViewDebug.ExportedProperty(category = "events")
179 private long mLastTouchDownTime;
180 @ViewDebug.ExportedProperty(category = "events")
181 private int mLastTouchDownIndex = -1;
Romain Guye95003e2011-01-09 13:53:06 -0800182 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800183 @ViewDebug.ExportedProperty(category = "events")
184 private float mLastTouchDownX;
Romain Guye95003e2011-01-09 13:53:06 -0800185 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800186 @ViewDebug.ExportedProperty(category = "events")
187 private float mLastTouchDownY;
188
Jeff Brown87b7f802011-06-21 18:35:45 -0700189 // First hover target in the linked list of hover targets.
190 // The hover targets are children which have received ACTION_HOVER_ENTER.
191 // They might not have actually handled the hover event, but we will
192 // continue sending hover events to them as long as the pointer remains over
193 // their bounds and the view group does not intercept hover.
194 private HoverTarget mFirstHoverTarget;
Jeff Browna032cc02011-03-07 16:56:21 -0800195
Jeff Brown10b62902011-06-20 16:40:37 -0700196 // True if the view group itself received a hover event.
197 // It might not have actually handled the hover event.
198 private boolean mHoveredSelf;
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
201 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * This field should be made private, so it is hidden from the SDK.
204 * {@hide}
205 */
Romain Guy2440e672012-08-07 14:43:43 -0700206 @ViewDebug.ExportedProperty(flagMapping = {
207 @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN,
208 name = "CLIP_CHILDREN"),
209 @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING,
210 name = "CLIP_TO_PADDING"),
211 @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL,
212 name = "PADDING_NOT_NULL")
Jon Miranda4597e982014-07-29 07:25:49 -0700213 }, formatToHexString = true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 protected int mGroupFlags;
215
Philip Milne7b757812012-09-19 18:13:44 -0700216 /**
217 * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -0700218 */
Philip Milnecfb631b2012-10-26 10:51:46 -0700219 private int mLayoutMode = LAYOUT_MODE_UNDEFINED;
Philip Milne1557fd72012-04-04 23:41:34 -0700220
Romain Guy33f6beb2012-02-16 19:24:51 -0800221 /**
222 * NOTE: If you change the flags below make sure to reflect the changes
223 * the DisplayList class
224 */
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 // When set, ViewGroup invalidates only the child's rectangle
227 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800228 static final int FLAG_CLIP_CHILDREN = 0x1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
230 // When set, ViewGroup excludes the padding area from the invalidate rectangle
231 // Set by default
232 private static final int FLAG_CLIP_TO_PADDING = 0x2;
233
234 // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
235 // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
Chet Haase64a48c12012-02-13 16:33:29 -0800236 static final int FLAG_INVALIDATE_REQUIRED = 0x4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
238 // When set, dispatchDraw() will run the layout animation and unset the flag
239 private static final int FLAG_RUN_ANIMATION = 0x8;
240
241 // When set, there is either no layout animation on the ViewGroup or the layout
242 // animation is over
243 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800244 static final int FLAG_ANIMATION_DONE = 0x10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 // If set, this ViewGroup has padding; if unset there is no padding and we don't need
247 // to clip it, even if FLAG_CLIP_TO_PADDING is set
248 private static final int FLAG_PADDING_NOT_NULL = 0x20;
249
Chris Craik5a6bbae2015-04-10 17:41:34 -0700250 /** @deprecated - functionality removed */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 private static final int FLAG_ANIMATION_CACHE = 0x40;
252
253 // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
254 // layout animation; this avoid clobbering the hierarchy
255 // Automatically set when the layout animation starts, depending on the animation's
256 // characteristics
Chet Haase64a48c12012-02-13 16:33:29 -0800257 static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
259 // When set, the next call to drawChild() will clear mChildTransformation's matrix
Chet Haase64a48c12012-02-13 16:33:29 -0800260 static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
262 // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
263 // the children's Bitmap caches if necessary
264 // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
265 private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
266
267 /**
268 * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
269 * to get the index of the child to draw for that iteration.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800270 *
Romain Guy293451e2009-11-04 13:59:48 -0800271 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * When set, this ViewGroup supports static transformations on children; this causes
277 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
278 * invoked when a child is drawn.
279 *
280 * Any subclass overriding
281 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
282 * set this flags in {@link #mGroupFlags}.
Romain Guy8506ab42009-06-11 17:35:47 -0700283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * {@hide}
285 */
286 protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
287
John Reckfb5899d2014-08-15 18:51:27 -0700288 // UNUSED FLAG VALUE: 0x1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 /**
291 * When set, this ViewGroup's drawable states also include those
292 * of its children.
293 */
294 private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
295
Chris Craik5a6bbae2015-04-10 17:41:34 -0700296 /** @deprecated functionality removed */
297 private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Chris Craik5a6bbae2015-04-10 17:41:34 -0700299 /** @deprecated functionality removed */
300 private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
302 /**
303 * When set, this group will go through its list of children to notify them of
304 * any drawable state change.
305 */
306 private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
307
308 private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
309
310 /**
311 * This view will get focus before any of its descendants.
312 */
313 public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
314
315 /**
316 * This view will get focus only if none of its descendants want it.
317 */
318 public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
319
320 /**
321 * This view will block any of its descendants from getting focus, even
322 * if they are focusable.
323 */
324 public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
325
326 /**
327 * Used to map between enum in attrubutes and flag values.
328 */
329 private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
330 {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
331 FOCUS_BLOCK_DESCENDANTS};
332
333 /**
334 * When set, this ViewGroup should not intercept touch events.
Adam Powell110486f2010-06-22 17:14:44 -0700335 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 */
Adam Powell110486f2010-06-22 17:14:44 -0700337 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700340 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
341 */
Adam Powellf37df072010-09-17 16:22:49 -0700342 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700343
344 /**
Adam Powell4b867882011-09-16 12:59:46 -0700345 * When set, this ViewGroup will not dispatch onAttachedToWindow calls
346 * to children when adding new views. This is used to prevent multiple
347 * onAttached calls when a ViewGroup adds children in its own onAttached method.
348 */
349 private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000;
350
351 /**
Philip Milnecfb631b2012-10-26 10:51:46 -0700352 * When true, indicates that a layoutMode has been explicitly set, either with
353 * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource.
354 * This distinguishes the situation in which a layout mode was inherited from
355 * one of the ViewGroup's ancestors and cached locally.
356 */
357 private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000;
358
Chris Craikb49f4462014-03-20 12:44:20 -0700359 static final int FLAG_IS_TRANSITION_GROUP = 0x1000000;
Chris Craikd863a102013-12-19 13:31:15 -0800360
Chris Craikb49f4462014-03-20 12:44:20 -0700361 static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000;
George Mount0a778ed2013-12-13 13:35:36 -0800362
Chris Craikd863a102013-12-19 13:31:15 -0800363 /**
Adam Powellff0d2982014-07-10 20:34:14 -0700364 * When set, focus will not be permitted to enter this group if a touchscreen is present.
365 */
366 static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000;
367
368 /**
Clara Bayarri4423d912015-03-02 19:42:48 +0000369 * When true, indicates that a call to startActionModeForChild was made with the type parameter
370 * and should not be ignored. This helps in backwards compatibility with the existing method
371 * without a type.
372 *
373 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
374 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
375 */
376 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000;
377
378 /**
379 * When true, indicates that a call to startActionModeForChild was made without the type
380 * parameter. This helps in backwards compatibility with the existing method
381 * without a type.
382 *
383 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
384 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
385 */
386 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000;
387
388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * Indicates which types of drawing caches are to be kept in memory.
390 * This field should be made private, so it is hidden from the SDK.
391 * {@hide}
392 */
393 protected int mPersistentDrawingCache;
394
395 /**
396 * Used to indicate that no drawing cache should be kept in memory.
397 */
398 public static final int PERSISTENT_NO_CACHE = 0x0;
399
400 /**
401 * Used to indicate that the animation drawing cache should be kept in memory.
402 */
403 public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
404
405 /**
406 * Used to indicate that the scrolling drawing cache should be kept in memory.
407 */
408 public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
409
410 /**
411 * Used to indicate that all drawing caches should be kept in memory.
412 */
413 public static final int PERSISTENT_ALL_CACHES = 0x3;
414
Philip Milne1557fd72012-04-04 23:41:34 -0700415 // Layout Modes
416
Philip Milnecfb631b2012-10-26 10:51:46 -0700417 private static final int LAYOUT_MODE_UNDEFINED = -1;
418
Philip Milne1557fd72012-04-04 23:41:34 -0700419 /**
420 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700421 * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
Philip Milne1557fd72012-04-04 23:41:34 -0700422 * {@link #getRight() right} and {@link #getBottom() bottom}.
423 */
Philip Milne7b757812012-09-19 18:13:44 -0700424 public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
Philip Milne1557fd72012-04-04 23:41:34 -0700425
426 /**
427 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700428 * Optical bounds describe where a widget appears to be. They sit inside the clip
429 * bounds which need to cover a larger area to allow other effects,
430 * such as shadows and glows, to be drawn.
Philip Milne1557fd72012-04-04 23:41:34 -0700431 */
Philip Milne7b757812012-09-19 18:13:44 -0700432 public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
433
434 /** @hide */
Philip Milnecfb631b2012-10-26 10:51:46 -0700435 public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS;
Philip Milne1557fd72012-04-04 23:41:34 -0700436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 /**
438 * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
439 * are set at the same time.
440 */
441 protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
442
443 // Index of the child's left position in the mLocation array
444 private static final int CHILD_LEFT_INDEX = 0;
445 // Index of the child's top position in the mLocation array
446 private static final int CHILD_TOP_INDEX = 1;
447
448 // Child views of this ViewGroup
449 private View[] mChildren;
450 // Number of valid children in the mChildren array, the rest should be null or not
451 // considered as children
452 private int mChildrenCount;
453
Chet Haaseb9895022013-04-02 15:10:58 -0700454 // Whether layout calls are currently being suppressed, controlled by calls to
455 // suppressLayout()
456 boolean mSuppressLayout = false;
457
458 // Whether any layout calls have actually been suppressed while mSuppressLayout
459 // has been true. This tracks whether we need to issue a requestLayout() when
460 // layout is later re-enabled.
461 private boolean mLayoutCalledWhileSuppressed = false;
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 private static final int ARRAY_INITIAL_CAPACITY = 12;
464 private static final int ARRAY_CAPACITY_INCREMENT = 12;
465
Romain Guycbc67742012-04-27 16:12:57 -0700466 private static Paint sDebugPaint;
467 private static float[] sDebugLines;
Philip Milne604f4402012-04-24 19:27:11 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 // Used to draw cached views
Chet Haase64a48c12012-02-13 16:33:29 -0800470 Paint mCachePaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
Chet Haase21cd1382010-09-01 17:42:29 -0700472 // Used to animate add/remove changes in layout
473 private LayoutTransition mTransition;
474
475 // The set of views that are currently being transitioned. This list is used to track views
476 // being removed that should not actually be removed from the parent yet because they are
477 // being animated.
478 private ArrayList<View> mTransitioningViews;
479
Chet Haase5e25c2c2010-09-16 11:15:56 -0700480 // List of children changing visibility. This is used to potentially keep rendering
481 // views during a transition when they otherwise would have become gone/invisible
482 private ArrayList<View> mVisibilityChangingChildren;
483
Chris Craikab008f02014-05-23 17:55:03 -0700484 // Temporary holder of presorted children, only used for
485 // input/software draw dispatch for correctly Z ordering.
486 private ArrayList<View> mPreSortedChildren;
487
Adam Powell539ee872012-02-03 19:00:49 -0800488 // Indicates how many of this container's child subtrees contain transient state
489 @ViewDebug.ExportedProperty(category = "layout")
490 private int mChildCountWithTransientState = 0;
491
Adam Powell10ba2772014-04-15 09:46:51 -0700492 /**
493 * Currently registered axes for nested scrolling. Flag set consisting of
494 * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE}
495 * for null.
496 */
497 private int mNestedScrollAxes;
498
Chet Haasec633d2f2015-04-07 10:29:39 -0700499 // Used to manage the list of transient views, added by addTransientView()
500 private List<Integer> mTransientIndices = null;
501 private List<View> mTransientViews = null;
502
503
Clara Bayarri4423d912015-03-02 19:42:48 +0000504 /**
505 * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild.
506 *
507 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
508 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
509 */
510 private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() {
511 @Override
512 public void setTitle(CharSequence title) {}
513
514 @Override
515 public void setTitle(int resId) {}
516
517 @Override
518 public void setSubtitle(CharSequence subtitle) {}
519
520 @Override
521 public void setSubtitle(int resId) {}
522
523 @Override
524 public void setCustomView(View view) {}
525
526 @Override
527 public void invalidate() {}
528
529 @Override
530 public void finish() {}
531
532 @Override
533 public Menu getMenu() {
534 return null;
535 }
536
537 @Override
538 public CharSequence getTitle() {
539 return null;
540 }
541
542 @Override
543 public CharSequence getSubtitle() {
544 return null;
545 }
546
547 @Override
548 public View getCustomView() {
549 return null;
550 }
551
552 @Override
553 public MenuInflater getMenuInflater() {
554 return null;
555 }
556 };
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 public ViewGroup(Context context) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700559 this(context, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561
562 public ViewGroup(Context context, AttributeSet attrs) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700563 this(context, attrs, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Alan Viverette617feb92013-09-09 18:09:13 -0700566 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700567 this(context, attrs, defStyleAttr, 0);
Alan Viverette617feb92013-09-09 18:09:13 -0700568 }
569
570 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
571 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 initViewGroup();
Alan Viveretted6479ec2013-09-10 17:03:02 -0700573 initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575
Philip Milne10ca24a2012-04-23 15:38:27 -0700576 private boolean debugDraw() {
Philip Milne7b757812012-09-19 18:13:44 -0700577 return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout;
Philip Milne10ca24a2012-04-23 15:38:27 -0700578 }
579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 private void initViewGroup() {
581 // ViewGroup doesn't draw by default
Philip Milne10ca24a2012-04-23 15:38:27 -0700582 if (!debugDraw()) {
583 setFlags(WILL_NOT_DRAW, DRAW_MASK);
584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 mGroupFlags |= FLAG_CLIP_CHILDREN;
586 mGroupFlags |= FLAG_CLIP_TO_PADDING;
587 mGroupFlags |= FLAG_ANIMATION_DONE;
Chris Craikf6ce8fd2015-05-11 15:33:11 -0700588 mGroupFlags |= FLAG_ANIMATION_CACHE;
589 mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590
Jeff Brown995e7742010-12-22 16:59:36 -0800591 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
592 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
593 }
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
596
597 mChildren = new View[ARRAY_INITIAL_CAPACITY];
598 mChildrenCount = 0;
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
601 }
602
Alan Viveretted6479ec2013-09-10 17:03:02 -0700603 private void initFromAttributes(
604 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Filip Gruszczyńskib50cea02014-03-05 17:54:58 -0800605 final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr,
606 defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
608 final int N = a.getIndexCount();
609 for (int i = 0; i < N; i++) {
610 int attr = a.getIndex(i);
611 switch (attr) {
612 case R.styleable.ViewGroup_clipChildren:
613 setClipChildren(a.getBoolean(attr, true));
614 break;
615 case R.styleable.ViewGroup_clipToPadding:
616 setClipToPadding(a.getBoolean(attr, true));
617 break;
618 case R.styleable.ViewGroup_animationCache:
619 setAnimationCacheEnabled(a.getBoolean(attr, true));
620 break;
621 case R.styleable.ViewGroup_persistentDrawingCache:
622 setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
623 break;
624 case R.styleable.ViewGroup_addStatesFromChildren:
625 setAddStatesFromChildren(a.getBoolean(attr, false));
626 break;
627 case R.styleable.ViewGroup_alwaysDrawnWithCache:
628 setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
629 break;
630 case R.styleable.ViewGroup_layoutAnimation:
631 int id = a.getResourceId(attr, -1);
632 if (id > 0) {
633 setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
634 }
635 break;
636 case R.styleable.ViewGroup_descendantFocusability:
637 setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
638 break;
Adam Powell2b342f02010-08-18 18:14:13 -0700639 case R.styleable.ViewGroup_splitMotionEvents:
640 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
641 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700642 case R.styleable.ViewGroup_animateLayoutChanges:
643 boolean animateLayoutChanges = a.getBoolean(attr, false);
644 if (animateLayoutChanges) {
645 setLayoutTransition(new LayoutTransition());
646 }
647 break;
Philip Milne7b757812012-09-19 18:13:44 -0700648 case R.styleable.ViewGroup_layoutMode:
Philip Milnecfb631b2012-10-26 10:51:46 -0700649 setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED));
Philip Milne7b757812012-09-19 18:13:44 -0700650 break;
George Mount0a778ed2013-12-13 13:35:36 -0800651 case R.styleable.ViewGroup_transitionGroup:
652 setTransitionGroup(a.getBoolean(attr, false));
653 break;
Adam Powellff0d2982014-07-10 20:34:14 -0700654 case R.styleable.ViewGroup_touchscreenBlocksFocus:
655 setTouchscreenBlocksFocus(a.getBoolean(attr, false));
656 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 }
658 }
659
660 a.recycle();
661 }
662
663 /**
664 * Gets the descendant focusability of this view group. The descendant
665 * focusability defines the relationship between this view group and its
666 * descendants when looking for a view to take focus in
667 * {@link #requestFocus(int, android.graphics.Rect)}.
668 *
669 * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
670 * {@link #FOCUS_BLOCK_DESCENDANTS}.
671 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700672 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
674 @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
675 @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
676 })
677 public int getDescendantFocusability() {
678 return mGroupFlags & FLAG_MASK_FOCUSABILITY;
679 }
680
681 /**
682 * Set the descendant focusability of this view group. This defines the relationship
683 * between this view group and its descendants when looking for a view to
684 * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
685 *
686 * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
687 * {@link #FOCUS_BLOCK_DESCENDANTS}.
688 */
689 public void setDescendantFocusability(int focusability) {
690 switch (focusability) {
691 case FOCUS_BEFORE_DESCENDANTS:
692 case FOCUS_AFTER_DESCENDANTS:
693 case FOCUS_BLOCK_DESCENDANTS:
694 break;
695 default:
696 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
697 + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
698 }
699 mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
700 mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
701 }
702
703 /**
704 * {@inheritDoc}
705 */
706 @Override
707 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
708 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800709 mFocused.unFocus(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 mFocused = null;
711 }
712 super.handleFocusGainInternal(direction, previouslyFocusedRect);
713 }
714
715 /**
716 * {@inheritDoc}
717 */
718 public void requestChildFocus(View child, View focused) {
719 if (DBG) {
720 System.out.println(this + " requestChildFocus()");
721 }
722 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
723 return;
724 }
725
726 // Unfocus us, if necessary
Alan Viverette223622a2013-12-17 13:29:02 -0800727 super.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728
729 // We had a previous notion of who had focus. Clear it.
730 if (mFocused != child) {
731 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800732 mFocused.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734
735 mFocused = child;
736 }
737 if (mParent != null) {
738 mParent.requestChildFocus(this, focused);
739 }
740 }
741
742 /**
743 * {@inheritDoc}
744 */
745 public void focusableViewAvailable(View v) {
746 if (mParent != null
747 // shortcut: don't report a new focusable view if we block our descendants from
748 // getting focus
749 && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS)
Adam Powell88c11752014-07-21 17:19:16 -0700750 && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 // shortcut: don't report a new focusable view if we already are focused
752 // (and we don't prefer our descendants)
753 //
754 // note: knowing that mFocused is non-null is not a good enough reason
755 // to break the traversal since in that case we'd actually have to find
756 // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700757 // an ancestor of v; this will get checked for at ViewAncestor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) {
759 mParent.focusableViewAvailable(v);
760 }
761 }
762
763 /**
764 * {@inheritDoc}
765 */
766 public boolean showContextMenuForChild(View originalView) {
767 return mParent != null && mParent.showContextMenuForChild(originalView);
768 }
769
Oren Blasberged391262015-09-01 12:12:51 -0700770 @Override
771 public boolean showContextMenuForChild(View originalView, float x, float y) {
772 return mParent != null && mParent.showContextMenuForChild(originalView, x, y);
773 }
774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 /**
Adam Powell6e346362010-07-23 10:18:23 -0700776 * {@inheritDoc}
777 */
Clara Bayarri4423d912015-03-02 19:42:48 +0000778 @Override
Adam Powell6e346362010-07-23 10:18:23 -0700779 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000780 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) {
781 // This is the original call.
782 try {
783 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
784 return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY);
785 } finally {
786 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
787 }
788 } else {
789 // We are being called from the new method with type.
790 return SENTINEL_ACTION_MODE;
791 }
792 }
793
794 /**
795 * {@inheritDoc}
796 */
797 @Override
798 public ActionMode startActionModeForChild(
799 View originalView, ActionMode.Callback callback, int type) {
Adam Powelle9fd6d22015-06-01 11:26:32 -0700800 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0
801 && type == ActionMode.TYPE_PRIMARY) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000802 ActionMode mode;
803 try {
804 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
805 mode = startActionModeForChild(originalView, callback);
806 } finally {
807 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
808 }
809 if (mode != SENTINEL_ACTION_MODE) {
810 return mode;
811 }
812 }
813 if (mParent != null) {
814 try {
815 return mParent.startActionModeForChild(originalView, callback, type);
816 } catch (AbstractMethodError ame) {
817 // Custom view parents might not implement this method.
818 return mParent.startActionModeForChild(originalView, callback);
819 }
820 }
821 return null;
Adam Powell6e346362010-07-23 10:18:23 -0700822 }
823
824 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000825 * @hide
826 */
827 @Override
828 public boolean dispatchActivityResult(
829 String who, int requestCode, int resultCode, Intent data) {
830 if (super.dispatchActivityResult(who, requestCode, resultCode, data)) {
831 return true;
832 }
833 int childCount = getChildCount();
834 for (int i = 0; i < childCount; i++) {
835 View child = getChildAt(i);
836 if (child.dispatchActivityResult(who, requestCode, resultCode, data)) {
837 return true;
838 }
839 }
840 return false;
841 }
842
843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 * Find the nearest view in the specified direction that wants to take
845 * focus.
846 *
847 * @param focused The view that currently has focus
848 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
849 * FOCUS_RIGHT, or 0 for not applicable.
850 */
851 public View focusSearch(View focused, int direction) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -0700852 if (isRootNamespace()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 // root namespace means we should consider ourselves the top of the
854 // tree for focus searching; otherwise we could be focus searching
855 // into other tabs. see LocalActivityManager and TabHost for more info
856 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
857 } else if (mParent != null) {
858 return mParent.focusSearch(focused, direction);
859 }
860 return null;
861 }
862
863 /**
864 * {@inheritDoc}
865 */
866 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
867 return false;
868 }
869
870 /**
871 * {@inheritDoc}
872 */
Svetoslav Ganov42138042012-03-20 11:51:39 -0700873 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700874 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -0700875 ViewParent parent = mParent;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700876 if (parent == null) {
877 return false;
878 }
879 final boolean propagate = onRequestSendAccessibilityEvent(child, event);
880 if (!propagate) {
881 return false;
882 }
883 return parent.requestSendAccessibilityEvent(this, event);
884 }
885
886 /**
887 * Called when a child has requested sending an {@link AccessibilityEvent} and
888 * gives an opportunity to its parent to augment the event.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700889 * <p>
Adam Powell2fcbbd02011-09-28 18:56:43 -0700890 * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling
891 * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its
892 * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)}
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700893 * is responsible for handling this call.
894 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700895 *
896 * @param child The child which requests sending the event.
897 * @param event The event to be sent.
898 * @return True if the event should be sent.
899 *
900 * @see #requestSendAccessibilityEvent(View, AccessibilityEvent)
901 */
902 public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700903 if (mAccessibilityDelegate != null) {
904 return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event);
905 } else {
906 return onRequestSendAccessibilityEventInternal(child, event);
907 }
908 }
909
910 /**
911 * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent)
912 *
913 * Note: Called from the default {@link View.AccessibilityDelegate}.
Alan Viverettea54956a2015-01-07 16:05:02 -0800914 *
915 * @hide
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700916 */
Alan Viverettea54956a2015-01-07 16:05:02 -0800917 public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700918 return true;
919 }
920
921 /**
Adam Powell539ee872012-02-03 19:00:49 -0800922 * Called when a child view has changed whether or not it is tracking transient state.
Adam Powell539ee872012-02-03 19:00:49 -0800923 */
924 public void childHasTransientStateChanged(View child, boolean childHasTransientState) {
925 final boolean oldHasTransientState = hasTransientState();
926 if (childHasTransientState) {
927 mChildCountWithTransientState++;
928 } else {
929 mChildCountWithTransientState--;
930 }
931
932 final boolean newHasTransientState = hasTransientState();
933 if (mParent != null && oldHasTransientState != newHasTransientState) {
934 try {
935 mParent.childHasTransientStateChanged(this, newHasTransientState);
936 } catch (AbstractMethodError e) {
937 Log.e(TAG, mParent.getClass().getSimpleName() +
938 " does not fully implement ViewParent", e);
939 }
940 }
941 }
942
Adam Powell539ee872012-02-03 19:00:49 -0800943 @Override
944 public boolean hasTransientState() {
945 return mChildCountWithTransientState > 0 || super.hasTransientState();
946 }
947
948 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700949 * {@inheritDoc}
950 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 @Override
952 public boolean dispatchUnhandledMove(View focused, int direction) {
953 return mFocused != null &&
954 mFocused.dispatchUnhandledMove(focused, direction);
955 }
956
957 /**
958 * {@inheritDoc}
959 */
960 public void clearChildFocus(View child) {
961 if (DBG) {
962 System.out.println(this + " clearChildFocus()");
963 }
964
965 mFocused = null;
966 if (mParent != null) {
967 mParent.clearChildFocus(this);
968 }
969 }
970
971 /**
972 * {@inheritDoc}
973 */
974 @Override
975 public void clearFocus() {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800976 if (DBG) {
977 System.out.println(this + " clearFocus()");
978 }
979 if (mFocused == null) {
980 super.clearFocus();
981 } else {
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700982 View focused = mFocused;
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800983 mFocused = null;
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700984 focused.clearFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 }
987
988 /**
989 * {@inheritDoc}
990 */
991 @Override
Alan Viverette223622a2013-12-17 13:29:02 -0800992 void unFocus(View focused) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 if (DBG) {
994 System.out.println(this + " unFocus()");
995 }
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800996 if (mFocused == null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800997 super.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800998 } else {
Alan Viverette223622a2013-12-17 13:29:02 -0800999 mFocused.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08001000 mFocused = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003
1004 /**
1005 * Returns the focused child of this view, if any. The child may have focus
1006 * or contain focus.
1007 *
1008 * @return the focused child or null.
1009 */
1010 public View getFocusedChild() {
1011 return mFocused;
1012 }
1013
Adam Powell88c11752014-07-21 17:19:16 -07001014 View getDeepestFocusedChild() {
1015 View v = this;
1016 while (v != null) {
1017 if (v.isFocused()) {
1018 return v;
1019 }
1020 v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null;
1021 }
1022 return null;
1023 }
1024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 /**
1026 * Returns true if this view has or contains focus
1027 *
1028 * @return true if this view has or contains focus
1029 */
1030 @Override
1031 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001032 return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
1035 /*
1036 * (non-Javadoc)
1037 *
1038 * @see android.view.View#findFocus()
1039 */
1040 @Override
1041 public View findFocus() {
1042 if (DBG) {
1043 System.out.println("Find focus in " + this + ": flags="
1044 + isFocused() + ", child=" + mFocused);
1045 }
1046
1047 if (isFocused()) {
1048 return this;
1049 }
1050
1051 if (mFocused != null) {
1052 return mFocused.findFocus();
1053 }
1054 return null;
1055 }
1056
1057 /**
1058 * {@inheritDoc}
1059 */
1060 @Override
1061 public boolean hasFocusable() {
1062 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
1063 return false;
1064 }
1065
1066 if (isFocusable()) {
1067 return true;
1068 }
1069
1070 final int descendantFocusability = getDescendantFocusability();
Adam Powell88c11752014-07-21 17:19:16 -07001071 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 final int count = mChildrenCount;
1073 final View[] children = mChildren;
1074
1075 for (int i = 0; i < count; i++) {
1076 final View child = children[i];
1077 if (child.hasFocusable()) {
1078 return true;
1079 }
1080 }
1081 }
1082
1083 return false;
1084 }
1085
1086 /**
1087 * {@inheritDoc}
1088 */
1089 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001090 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 final int focusableCount = views.size();
1092
1093 final int descendantFocusability = getDescendantFocusability();
1094
Adam Powell88c11752014-07-21 17:19:16 -07001095 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
1096 if (shouldBlockFocusForTouchscreen()) {
1097 focusableMode |= FOCUSABLES_TOUCH_MODE;
1098 }
1099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 final int count = mChildrenCount;
1101 final View[] children = mChildren;
1102
1103 for (int i = 0; i < count; i++) {
1104 final View child = children[i];
1105 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001106 child.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108 }
1109 }
1110
1111 // we add ourselves (if focusable) in all cases except for when we are
1112 // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is
1113 // to avoid the focus search finding layouts when a more precise search
1114 // among the focusable children would be more interesting.
Adam Powellff0d2982014-07-10 20:34:14 -07001115 if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // No focusable descendants
Adam Powell88c11752014-07-21 17:19:16 -07001117 || (focusableCount == views.size())) &&
1118 (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001119 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
1122
Adam Powellff0d2982014-07-10 20:34:14 -07001123 /**
1124 * Set whether this ViewGroup should ignore focus requests for itself and its children.
1125 * If this option is enabled and the ViewGroup or a descendant currently has focus, focus
1126 * will proceed forward.
1127 *
1128 * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen
1129 */
1130 public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {
1131 if (touchscreenBlocksFocus) {
1132 mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1133 if (hasFocus()) {
Adam Powell88c11752014-07-21 17:19:16 -07001134 final View focusedChild = getDeepestFocusedChild();
1135 if (!focusedChild.isFocusableInTouchMode()) {
1136 final View newFocus = focusSearch(FOCUS_FORWARD);
1137 if (newFocus != null) {
1138 newFocus.requestFocus();
1139 }
Adam Powellff0d2982014-07-10 20:34:14 -07001140 }
1141 }
1142 } else {
1143 mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1144 }
1145 }
1146
1147 /**
1148 * Check whether this ViewGroup should ignore focus requests for itself and its children.
1149 */
1150 public boolean getTouchscreenBlocksFocus() {
1151 return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0;
1152 }
1153
1154 boolean shouldBlockFocusForTouchscreen() {
1155 return getTouchscreenBlocksFocus() &&
1156 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
1157 }
1158
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001159 @Override
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001160 public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
1161 super.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001162 final int childrenCount = mChildrenCount;
1163 final View[] children = mChildren;
1164 for (int i = 0; i < childrenCount; i++) {
1165 View child = children[i];
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001166 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
Dianne Hackborn4702a852012-08-17 15:18:29 -07001167 && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001168 child.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001169 }
1170 }
1171 }
1172
Svetoslav5b578da2013-05-08 14:23:32 -07001173 /** @hide */
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001174 @Override
Svetoslav5b578da2013-05-08 14:23:32 -07001175 public View findViewByAccessibilityIdTraversal(int accessibilityId) {
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001176 View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId);
1177 if (foundView != null) {
1178 return foundView;
1179 }
Svetoslav6cc46272015-06-03 11:38:30 -07001180
1181 if (getAccessibilityNodeProvider() != null) {
1182 return null;
1183 }
1184
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001185 final int childrenCount = mChildrenCount;
1186 final View[] children = mChildren;
1187 for (int i = 0; i < childrenCount; i++) {
1188 View child = children[i];
1189 foundView = child.findViewByAccessibilityIdTraversal(accessibilityId);
1190 if (foundView != null) {
1191 return foundView;
1192 }
1193 }
Svetoslav6cc46272015-06-03 11:38:30 -07001194
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001195 return null;
1196 }
1197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * {@inheritDoc}
1200 */
1201 @Override
1202 public void dispatchWindowFocusChanged(boolean hasFocus) {
1203 super.dispatchWindowFocusChanged(hasFocus);
1204 final int count = mChildrenCount;
1205 final View[] children = mChildren;
1206 for (int i = 0; i < count; i++) {
1207 children[i].dispatchWindowFocusChanged(hasFocus);
1208 }
1209 }
1210
1211 /**
1212 * {@inheritDoc}
1213 */
1214 @Override
1215 public void addTouchables(ArrayList<View> views) {
1216 super.addTouchables(views);
1217
1218 final int count = mChildrenCount;
1219 final View[] children = mChildren;
1220
1221 for (int i = 0; i < count; i++) {
1222 final View child = children[i];
1223 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1224 child.addTouchables(views);
1225 }
1226 }
1227 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001228
1229 /**
1230 * @hide
1231 */
1232 @Override
1233 public void makeOptionalFitsSystemWindows() {
1234 super.makeOptionalFitsSystemWindows();
1235 final int count = mChildrenCount;
1236 final View[] children = mChildren;
1237 for (int i = 0; i < count; i++) {
1238 children[i].makeOptionalFitsSystemWindows();
1239 }
1240 }
1241
Romain Guy43c9cdf2010-01-27 13:53:55 -08001242 /**
1243 * {@inheritDoc}
1244 */
1245 @Override
1246 public void dispatchDisplayHint(int hint) {
1247 super.dispatchDisplayHint(hint);
1248 final int count = mChildrenCount;
1249 final View[] children = mChildren;
1250 for (int i = 0; i < count; i++) {
1251 children[i].dispatchDisplayHint(hint);
1252 }
1253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254
1255 /**
Chet Haase0d299362012-01-26 10:51:48 -08001256 * Called when a view's visibility has changed. Notify the parent to take any appropriate
1257 * action.
1258 *
1259 * @param child The view whose visibility has changed
1260 * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
1261 * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
Chet Haase5e25c2c2010-09-16 11:15:56 -07001262 * @hide
Chet Haase5e25c2c2010-09-16 11:15:56 -07001263 */
Chet Haase0d299362012-01-26 10:51:48 -08001264 protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07001265 if (mTransition != null) {
Chet Haase0d299362012-01-26 10:51:48 -08001266 if (newVisibility == VISIBLE) {
1267 mTransition.showChild(this, child, oldVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001268 } else {
Chet Haase0d299362012-01-26 10:51:48 -08001269 mTransition.hideChild(this, child, newVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001270 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
Chet Haaseddbb3462012-06-19 13:54:29 -07001271 // Only track this on disappearing views - appearing views are already visible
1272 // and don't need special handling during drawChild()
1273 if (mVisibilityChangingChildren == null) {
1274 mVisibilityChangingChildren = new ArrayList<View>();
1275 }
1276 mVisibilityChangingChildren.add(child);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001277 addDisappearingView(child);
1278 }
1279 }
1280 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001281
1282 // in all cases, for drags
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001283 if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) {
1284 if (!mChildrenInterestedInDrag.contains(child)) {
1285 notifyChildOfDragStart(child);
Christopher Tate86cab1b2011-01-13 20:28:55 -08001286 }
1287 }
Chet Haase5e25c2c2010-09-16 11:15:56 -07001288 }
1289
1290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 * {@inheritDoc}
1292 */
1293 @Override
Adam Powell326d8082009-12-09 15:10:07 -08001294 protected void dispatchVisibilityChanged(View changedView, int visibility) {
1295 super.dispatchVisibilityChanged(changedView, visibility);
1296 final int count = mChildrenCount;
1297 final View[] children = mChildren;
1298 for (int i = 0; i < count; i++) {
1299 children[i].dispatchVisibilityChanged(changedView, visibility);
1300 }
1301 }
1302
1303 /**
1304 * {@inheritDoc}
1305 */
1306 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 public void dispatchWindowVisibilityChanged(int visibility) {
1308 super.dispatchWindowVisibilityChanged(visibility);
1309 final int count = mChildrenCount;
1310 final View[] children = mChildren;
1311 for (int i = 0; i < count; i++) {
1312 children[i].dispatchWindowVisibilityChanged(visibility);
1313 }
1314 }
1315
1316 /**
1317 * {@inheritDoc}
1318 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001319 @Override
1320 public void dispatchConfigurationChanged(Configuration newConfig) {
1321 super.dispatchConfigurationChanged(newConfig);
1322 final int count = mChildrenCount;
1323 final View[] children = mChildren;
1324 for (int i = 0; i < count; i++) {
1325 children[i].dispatchConfigurationChanged(newConfig);
1326 }
1327 }
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08001328
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001329 /**
1330 * {@inheritDoc}
1331 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -08001333 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1334 ViewParent parent = mParent;
1335 if (parent != null) parent.recomputeViewAttributes(this);
1336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
Romain Guy8506ab42009-06-11 17:35:47 -07001338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001340 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1341 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1342 super.dispatchCollectViewAttributes(attachInfo, visibility);
1343 final int count = mChildrenCount;
1344 final View[] children = mChildren;
1345 for (int i = 0; i < count; i++) {
1346 final View child = children[i];
1347 child.dispatchCollectViewAttributes(attachInfo,
1348 visibility | (child.mViewFlags&VISIBILITY_MASK));
1349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
1352
1353 /**
1354 * {@inheritDoc}
1355 */
1356 public void bringChildToFront(View child) {
Alan Viverette77bb6f12015-02-11 17:24:33 -08001357 final int index = indexOfChild(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 if (index >= 0) {
1359 removeFromArray(index);
1360 addInArray(child, mChildrenCount);
1361 child.mParent = this;
Chet Haasecb96db82013-09-04 10:21:46 -07001362 requestLayout();
1363 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
1366
Romain Guy6410c0a2013-06-17 11:21:58 -07001367 private PointF getLocalPoint() {
1368 if (mLocalPoint == null) mLocalPoint = new PointF();
1369 return mLocalPoint;
1370 }
1371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 /**
1373 * {@inheritDoc}
Christopher Tatea53146c2010-09-07 11:57:52 -07001374 */
Steve Block8a7259b2012-03-01 11:24:41 +00001375 // TODO: Write real docs
Christopher Tatea53146c2010-09-07 11:57:52 -07001376 @Override
1377 public boolean dispatchDragEvent(DragEvent event) {
1378 boolean retval = false;
1379 final float tx = event.mX;
1380 final float ty = event.mY;
1381
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07001382 ViewRootImpl root = getViewRootImpl();
Christopher Tatea53146c2010-09-07 11:57:52 -07001383
1384 // Dispatch down the view hierarchy
Romain Guy6410c0a2013-06-17 11:21:58 -07001385 final PointF localPoint = getLocalPoint();
1386
Christopher Tatea53146c2010-09-07 11:57:52 -07001387 switch (event.mAction) {
1388 case DragEvent.ACTION_DRAG_STARTED: {
1389 // clear state to recalculate which views we drag over
Chris Tate9d1ab882010-11-02 15:55:39 -07001390 mCurrentDragView = null;
Christopher Tatea53146c2010-09-07 11:57:52 -07001391
Christopher Tate86cab1b2011-01-13 20:28:55 -08001392 // Set up our tracking of drag-started notifications
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001393 mCurrentDragStartEvent = DragEvent.obtain(event);
1394 if (mChildrenInterestedInDrag == null) {
1395 mChildrenInterestedInDrag = new HashSet<View>();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001396 } else {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001397 mChildrenInterestedInDrag.clear();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001398 }
1399
Christopher Tatea53146c2010-09-07 11:57:52 -07001400 // Now dispatch down to our children, caching the responses
Christopher Tatea53146c2010-09-07 11:57:52 -07001401 final int count = mChildrenCount;
1402 final View[] children = mChildren;
1403 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001404 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001405 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001406 if (child.getVisibility() == VISIBLE) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001407 if (notifyChildOfDragStart(children[i])) {
1408 retval = true;
Christopher Tate2c095f32010-10-04 14:13:40 -07001409 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001410 }
1411 }
1412
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001413 // Notify itself of the drag start.
1414 mIsInterestedInDrag = super.dispatchDragEvent(event);
1415 if (mIsInterestedInDrag) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001416 retval = true;
1417 }
1418 } break;
1419
1420 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001421 // Release the bookkeeping now that the drag lifecycle has ended
Vadim Tryshev15177892015-12-18 08:01:00 -08001422 final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag;
1423 if (childrenInterestedInDrag != null) {
1424 for (View child : childrenInterestedInDrag) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001425 // If a child was interested in the ongoing drag, it's told that it's over
1426 if (child.dispatchDragEvent(event)) {
1427 retval = true;
1428 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001429 child.mPrivateFlags2 &= ~View.DRAG_MASK;
1430 child.refreshDrawableState();
Christopher Tate1fc014f2011-01-19 12:56:26 -08001431 }
Vadim Tryshev15177892015-12-18 08:01:00 -08001432 childrenInterestedInDrag.clear();
1433 }
1434 if (mCurrentDragStartEvent != null) {
1435 mCurrentDragStartEvent.recycle();
1436 mCurrentDragStartEvent = null;
Christopher Tate1fc014f2011-01-19 12:56:26 -08001437 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001438
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001439 if (mIsInterestedInDrag) {
1440 if (super.dispatchDragEvent(event)) {
1441 retval = true;
1442 }
1443 mIsInterestedInDrag = false;
Christopher Tatea53146c2010-09-07 11:57:52 -07001444 }
1445 } break;
1446
1447 case DragEvent.ACTION_DRAG_LOCATION: {
1448 // Find the [possibly new] drag target
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001449 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
1450 if (target == null && mIsInterestedInDrag) {
1451 target = this;
1452 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001453
1454 // If we've changed apparent drag target, tell the view root which view
Chris Tate9d1ab882010-11-02 15:55:39 -07001455 // we're over now [for purposes of the eventual drag-recipient-changed
1456 // notifications to the framework] and tell the new target that the drag
1457 // has entered its bounds. The root will see setDragFocus() calls all
1458 // the way down to the final leaf view that is handling the LOCATION event
1459 // before reporting the new potential recipient to the framework.
Christopher Tatea53146c2010-09-07 11:57:52 -07001460 if (mCurrentDragView != target) {
Chris Tate9d1ab882010-11-02 15:55:39 -07001461 root.setDragFocus(target);
1462
1463 final int action = event.mAction;
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001464 // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED.
1465 event.mX = 0;
1466 event.mY = 0;
1467
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001468 // If we've dragged off of a child view or this window, send it the EXITED message
Chris Tate9d1ab882010-11-02 15:55:39 -07001469 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001470 final View view = mCurrentDragView;
Chris Tate9d1ab882010-11-02 15:55:39 -07001471 event.mAction = DragEvent.ACTION_DRAG_EXITED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001472 if (view != this) {
1473 view.dispatchDragEvent(event);
1474 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1475 view.refreshDrawableState();
1476 } else {
1477 super.dispatchDragEvent(event);
1478 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001479 }
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001480
Christopher Tatea53146c2010-09-07 11:57:52 -07001481 mCurrentDragView = target;
Chris Tate9d1ab882010-11-02 15:55:39 -07001482
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001483 // If we've dragged over a new child view, send it the ENTERED message, otherwise
1484 // send it to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001485 if (target != null) {
1486 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001487 if (target != this) {
1488 target.dispatchDragEvent(event);
1489 target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
1490 target.refreshDrawableState();
1491 } else {
1492 super.dispatchDragEvent(event);
1493 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001494 }
1495 event.mAction = action; // restore the event's original state
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001496 event.mX = tx;
1497 event.mY = ty;
Christopher Tatea53146c2010-09-07 11:57:52 -07001498 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001499
Christopher Tatea53146c2010-09-07 11:57:52 -07001500 // Dispatch the actual drag location notice, localized into its coordinates
1501 if (target != null) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001502 if (target != this) {
1503 event.mX = localPoint.x;
1504 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001505
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001506 retval = target.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001507
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001508 event.mX = tx;
1509 event.mY = ty;
1510 } else {
1511 retval = super.dispatchDragEvent(event);
1512 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001513 }
1514 } break;
1515
Chris Tate9d1ab882010-11-02 15:55:39 -07001516 /* Entered / exited dispatch
1517 *
1518 * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is
1519 * that we're about to get the corresponding LOCATION event, which we will use to
1520 * determine which of our children is the new target; at that point we will
1521 * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001522 * If no suitable child is detected, dispatch to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001523 *
1524 * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1525 * drag has left this ViewGroup, we know by definition that every contained subview
1526 * is also no longer under the drag point.
1527 */
1528
1529 case DragEvent.ACTION_DRAG_EXITED: {
1530 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001531 final View view = mCurrentDragView;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001532 if (view != this) {
1533 view.dispatchDragEvent(event);
1534 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1535 view.refreshDrawableState();
1536 } else {
1537 super.dispatchDragEvent(event);
1538 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001539
Chris Tate9d1ab882010-11-02 15:55:39 -07001540 mCurrentDragView = null;
1541 }
1542 } break;
1543
Christopher Tatea53146c2010-09-07 11:57:52 -07001544 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -07001545 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Romain Guy6410c0a2013-06-17 11:21:58 -07001546 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
Christopher Tatea53146c2010-09-07 11:57:52 -07001547 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001548 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Romain Guy6410c0a2013-06-17 11:21:58 -07001549 event.mX = localPoint.x;
1550 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001551 retval = target.dispatchDragEvent(event);
1552 event.mX = tx;
1553 event.mY = ty;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001554 } else if (mIsInterestedInDrag) {
1555 retval = super.dispatchDragEvent(event);
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001556 } else {
1557 if (ViewDebug.DEBUG_DRAG) {
1558 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
1559 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001560 }
1561 } break;
1562 }
1563
Christopher Tatea53146c2010-09-07 11:57:52 -07001564 return retval;
1565 }
1566
1567 // Find the frontmost child view that lies under the given point, and calculate
1568 // the position within its own local coordinate system.
1569 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001570 final int count = mChildrenCount;
1571 final View[] children = mChildren;
1572 for (int i = count - 1; i >= 0; i--) {
1573 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001574 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001575 continue;
1576 }
1577
Christopher Tate2c095f32010-10-04 14:13:40 -07001578 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001579 return child;
1580 }
1581 }
1582 return null;
1583 }
1584
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001585 boolean notifyChildOfDragStart(View child) {
1586 // The caller guarantees that the child is not in mChildrenInterestedInDrag yet.
1587
Christopher Tate86cab1b2011-01-13 20:28:55 -08001588 if (ViewDebug.DEBUG_DRAG) {
1589 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1590 }
1591
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001592 final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent);
1593 if (canAccept) {
1594 mChildrenInterestedInDrag.add(child);
1595 if (!child.canAcceptDrag()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001596 child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001597 child.refreshDrawableState();
1598 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001599 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001600 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001601 }
1602
Joe Onorato664644d2011-01-23 17:53:23 -08001603 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001604 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1605 super.dispatchWindowSystemUiVisiblityChanged(visible);
1606
1607 final int count = mChildrenCount;
1608 final View[] children = mChildren;
1609 for (int i=0; i <count; i++) {
1610 final View child = children[i];
1611 child.dispatchWindowSystemUiVisiblityChanged(visible);
1612 }
1613 }
1614
1615 @Override
Joe Onorato664644d2011-01-23 17:53:23 -08001616 public void dispatchSystemUiVisibilityChanged(int visible) {
1617 super.dispatchSystemUiVisibilityChanged(visible);
1618
1619 final int count = mChildrenCount;
1620 final View[] children = mChildren;
1621 for (int i=0; i <count; i++) {
1622 final View child = children[i];
1623 child.dispatchSystemUiVisibilityChanged(visible);
1624 }
1625 }
1626
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001627 @Override
Dianne Hackborncf675782012-05-10 15:07:24 -07001628 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
1629 boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001630
1631 final int count = mChildrenCount;
1632 final View[] children = mChildren;
1633 for (int i=0; i <count; i++) {
1634 final View child = children[i];
Dianne Hackborncf675782012-05-10 15:07:24 -07001635 changed |= child.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001636 }
Dianne Hackborncf675782012-05-10 15:07:24 -07001637 return changed;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001638 }
1639
Christopher Tatea53146c2010-09-07 11:57:52 -07001640 /**
1641 * {@inheritDoc}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 */
1643 @Override
1644 public boolean dispatchKeyEventPreIme(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001645 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1646 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 return super.dispatchKeyEventPreIme(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001648 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1649 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 return mFocused.dispatchKeyEventPreIme(event);
1651 }
1652 return false;
1653 }
1654
1655 /**
1656 * {@inheritDoc}
1657 */
1658 @Override
1659 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001660 if (mInputEventConsistencyVerifier != null) {
1661 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1662 }
1663
Dianne Hackborn4702a852012-08-17 15:18:29 -07001664 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1665 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001666 if (super.dispatchKeyEvent(event)) {
1667 return true;
1668 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001669 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1670 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001671 if (mFocused.dispatchKeyEvent(event)) {
1672 return true;
1673 }
1674 }
1675
1676 if (mInputEventConsistencyVerifier != null) {
1677 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679 return false;
1680 }
1681
1682 /**
1683 * {@inheritDoc}
1684 */
1685 @Override
1686 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001687 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1688 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 return super.dispatchKeyShortcutEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001690 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1691 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 return mFocused.dispatchKeyShortcutEvent(event);
1693 }
1694 return false;
1695 }
1696
1697 /**
1698 * {@inheritDoc}
1699 */
1700 @Override
1701 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001702 if (mInputEventConsistencyVerifier != null) {
1703 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1704 }
1705
Dianne Hackborn4702a852012-08-17 15:18:29 -07001706 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1707 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001708 if (super.dispatchTrackballEvent(event)) {
1709 return true;
1710 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001711 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1712 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001713 if (mFocused.dispatchTrackballEvent(event)) {
1714 return true;
1715 }
1716 }
1717
1718 if (mInputEventConsistencyVerifier != null) {
1719 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
1721 return false;
1722 }
1723
Jun Mukai1db53972015-09-11 18:08:31 -07001724 @Override
Jun Mukaid4eaef72015-10-30 15:54:33 -07001725 public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
Jun Mukai1db53972015-09-11 18:08:31 -07001726 // Check what the child under the pointer says about the pointer.
1727 final int childrenCount = mChildrenCount;
1728 if (childrenCount != 0) {
1729 final ArrayList<View> preorderedList = buildOrderedChildList();
1730 final boolean customOrder = preorderedList == null
1731 && isChildrenDrawingOrderEnabled();
1732 final View[] children = mChildren;
1733 for (int i = childrenCount - 1; i >= 0; i--) {
1734 final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
1735 final View child = (preorderedList == null)
1736 ? children[childIndex] : preorderedList.get(childIndex);
1737 PointF point = getLocalPoint();
1738 if (isTransformedTouchPointInView(x, y, child, point)) {
Jun Mukaid4eaef72015-10-30 15:54:33 -07001739 final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y);
1740 if (pointerIcon != null) {
1741 return pointerIcon;
Jun Mukai1db53972015-09-11 18:08:31 -07001742 }
1743 break;
1744 }
1745 }
1746 }
1747
1748 // The pointer is not a child or the child has no preferences, returning the default
1749 // implementation.
Jun Mukaid4eaef72015-10-30 15:54:33 -07001750 return super.getPointerIcon(event, x, y);
Jun Mukai1db53972015-09-11 18:08:31 -07001751 }
1752
Jeff Brown10b62902011-06-20 16:40:37 -07001753 /**
1754 * {@inheritDoc}
1755 */
Romain Guya9489272011-06-22 20:58:11 -07001756 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08001758 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08001759 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08001760
Jeff Brown10b62902011-06-20 16:40:37 -07001761 // First check whether the view group wants to intercept the hover event.
1762 final boolean interceptHover = onInterceptHoverEvent(event);
1763 event.setAction(action); // restore action in case it was changed
1764
Jeff Brown87b7f802011-06-21 18:35:45 -07001765 MotionEvent eventNoHistory = event;
1766 boolean handled = false;
1767
1768 // Send events to the hovered children and build a new list of hover targets until
1769 // one is found that handles the event.
1770 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1771 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07001772 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08001773 final float x = event.getX();
1774 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001775 final int childrenCount = mChildrenCount;
1776 if (childrenCount != 0) {
Chris Craikab008f02014-05-23 17:55:03 -07001777 final ArrayList<View> preorderedList = buildOrderedChildList();
1778 final boolean customOrder = preorderedList == null
1779 && isChildrenDrawingOrderEnabled();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001780 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07001781 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001782 for (int i = childrenCount - 1; i >= 0; i--) {
Chris Craikab008f02014-05-23 17:55:03 -07001783 int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
1784 final View child = (preorderedList == null)
1785 ? children[childIndex] : preorderedList.get(childIndex);
Jeff Brown87b7f802011-06-21 18:35:45 -07001786 if (!canViewReceivePointerEvents(child)
1787 || !isTransformedTouchPointInView(x, y, child, null)) {
1788 continue;
1789 }
1790
1791 // Obtain a hover target for this child. Dequeue it from the
1792 // old hover target list if the child was previously hovered.
1793 HoverTarget hoverTarget = firstOldHoverTarget;
1794 final boolean wasHovered;
1795 for (HoverTarget predecessor = null; ;) {
1796 if (hoverTarget == null) {
1797 hoverTarget = HoverTarget.obtain(child);
1798 wasHovered = false;
1799 break;
1800 }
1801
1802 if (hoverTarget.child == child) {
1803 if (predecessor != null) {
1804 predecessor.next = hoverTarget.next;
1805 } else {
1806 firstOldHoverTarget = hoverTarget.next;
1807 }
1808 hoverTarget.next = null;
1809 wasHovered = true;
1810 break;
1811 }
1812
1813 predecessor = hoverTarget;
1814 hoverTarget = hoverTarget.next;
1815 }
1816
1817 // Enqueue the hover target onto the new hover target list.
1818 if (lastHoverTarget != null) {
1819 lastHoverTarget.next = hoverTarget;
1820 } else {
Jeff Brown87b7f802011-06-21 18:35:45 -07001821 mFirstHoverTarget = hoverTarget;
1822 }
Sangkyu Lee8725f362013-03-13 09:38:45 +09001823 lastHoverTarget = hoverTarget;
Jeff Brown87b7f802011-06-21 18:35:45 -07001824
1825 // Dispatch the event to the child.
1826 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1827 if (!wasHovered) {
1828 // Send the enter as is.
1829 handled |= dispatchTransformedGenericPointerEvent(
1830 event, child); // enter
1831 }
1832 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1833 if (!wasHovered) {
1834 // Synthesize an enter from a move.
1835 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1836 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1837 handled |= dispatchTransformedGenericPointerEvent(
1838 eventNoHistory, child); // enter
1839 eventNoHistory.setAction(action);
1840
1841 handled |= dispatchTransformedGenericPointerEvent(
1842 eventNoHistory, child); // move
1843 } else {
1844 // Send the move as is.
1845 handled |= dispatchTransformedGenericPointerEvent(event, child);
1846 }
1847 }
1848 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07001849 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001850 }
Jeff Brown10b62902011-06-20 16:40:37 -07001851 }
Chris Craikab008f02014-05-23 17:55:03 -07001852 if (preorderedList != null) preorderedList.clear();
Jeff Brown10b62902011-06-20 16:40:37 -07001853 }
1854 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001855
Jeff Brown87b7f802011-06-21 18:35:45 -07001856 // Send exit events to all previously hovered children that are no longer hovered.
1857 while (firstOldHoverTarget != null) {
1858 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07001859
Jeff Brown87b7f802011-06-21 18:35:45 -07001860 // Exit the old hovered child.
1861 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1862 // Send the exit as is.
1863 handled |= dispatchTransformedGenericPointerEvent(
1864 event, child); // exit
1865 } else {
1866 // Synthesize an exit from a move or enter.
1867 // Ignore the result because hover focus has moved to a different view.
1868 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brown10b62902011-06-20 16:40:37 -07001869 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07001870 event, child); // move
Jeff Brown10b62902011-06-20 16:40:37 -07001871 }
Jeff Brown87b7f802011-06-21 18:35:45 -07001872 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1873 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1874 dispatchTransformedGenericPointerEvent(
1875 eventNoHistory, child); // exit
1876 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07001877 }
1878
Jeff Brown87b7f802011-06-21 18:35:45 -07001879 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1880 firstOldHoverTarget.recycle();
1881 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07001882 }
1883
Jeff Brown87b7f802011-06-21 18:35:45 -07001884 // Send events to the view group itself if no children have handled it.
Jeff Brown10b62902011-06-20 16:40:37 -07001885 boolean newHoveredSelf = !handled;
1886 if (newHoveredSelf == mHoveredSelf) {
1887 if (newHoveredSelf) {
1888 // Send event to the view group as before.
1889 handled |= super.dispatchHoverEvent(event);
1890 }
1891 } else {
1892 if (mHoveredSelf) {
1893 // Exit the view group.
1894 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1895 // Send the exit as is.
1896 handled |= super.dispatchHoverEvent(event); // exit
1897 } else {
1898 // Synthesize an exit from a move or enter.
1899 // Ignore the result because hover focus is moving to a different view.
1900 if (action == MotionEvent.ACTION_HOVER_MOVE) {
1901 super.dispatchHoverEvent(event); // move
1902 }
1903 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1904 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1905 super.dispatchHoverEvent(eventNoHistory); // exit
1906 eventNoHistory.setAction(action);
1907 }
1908 mHoveredSelf = false;
1909 }
1910
1911 if (newHoveredSelf) {
1912 // Enter the view group.
1913 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1914 // Send the enter as is.
1915 handled |= super.dispatchHoverEvent(event); // enter
1916 mHoveredSelf = true;
1917 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1918 // Synthesize an enter from a move.
1919 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1920 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1921 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1922 eventNoHistory.setAction(action);
1923
1924 handled |= super.dispatchHoverEvent(eventNoHistory); // move
1925 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001926 }
1927 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001928 }
1929
Jeff Browna032cc02011-03-07 16:56:21 -08001930 // Recycle the copy of the event that we made.
1931 if (eventNoHistory != event) {
1932 eventNoHistory.recycle();
1933 }
1934
Jeff Browna032cc02011-03-07 16:56:21 -08001935 // Done.
1936 return handled;
1937 }
1938
Jeff Brown59a422e2012-04-19 15:19:19 -07001939 private void exitHoverTargets() {
1940 if (mHoveredSelf || mFirstHoverTarget != null) {
1941 final long now = SystemClock.uptimeMillis();
1942 MotionEvent event = MotionEvent.obtain(now, now,
1943 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1944 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1945 dispatchHoverEvent(event);
1946 event.recycle();
1947 }
1948 }
1949
1950 private void cancelHoverTarget(View view) {
1951 HoverTarget predecessor = null;
1952 HoverTarget target = mFirstHoverTarget;
1953 while (target != null) {
1954 final HoverTarget next = target.next;
1955 if (target.child == view) {
1956 if (predecessor == null) {
1957 mFirstHoverTarget = next;
1958 } else {
1959 predecessor.next = next;
1960 }
1961 target.recycle();
1962
1963 final long now = SystemClock.uptimeMillis();
1964 MotionEvent event = MotionEvent.obtain(now, now,
1965 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1966 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1967 view.dispatchHoverEvent(event);
1968 event.recycle();
1969 return;
1970 }
1971 predecessor = target;
1972 target = next;
1973 }
1974 }
1975
Jeff Brown87b7f802011-06-21 18:35:45 -07001976 /** @hide */
1977 @Override
1978 protected boolean hasHoveredChild() {
1979 return mFirstHoverTarget != null;
1980 }
1981
Svetoslav Ganov42138042012-03-20 11:51:39 -07001982 @Override
Adam Powellcf392d12015-06-25 14:48:45 -07001983 public void addChildrenForAccessibility(ArrayList<View> outChildren) {
Svetoslav762621c2015-06-01 17:21:59 -07001984 if (getAccessibilityNodeProvider() != null) {
1985 return;
1986 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001987 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
1988 try {
1989 final int childrenCount = children.getChildCount();
1990 for (int i = 0; i < childrenCount; i++) {
1991 View child = children.getChildAt(i);
Svetoslav Ganovc406be92012-05-11 16:12:32 -07001992 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001993 if (child.includeForAccessibility()) {
Adam Powellcf392d12015-06-25 14:48:45 -07001994 outChildren.add(child);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001995 } else {
Adam Powellcf392d12015-06-25 14:48:45 -07001996 child.addChildrenForAccessibility(outChildren);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001997 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001998 }
1999 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002000 } finally {
2001 children.recycle();
Svetoslav Ganov42138042012-03-20 11:51:39 -07002002 }
2003 }
2004
2005 /**
Jeff Brown10b62902011-06-20 16:40:37 -07002006 * Implement this method to intercept hover events before they are handled
2007 * by child views.
2008 * <p>
2009 * This method is called before dispatching a hover event to a child of
2010 * the view group or to the view group's own {@link #onHoverEvent} to allow
2011 * the view group a chance to intercept the hover event.
2012 * This method can also be used to watch all pointer motions that occur within
2013 * the bounds of the view group even when the pointer is hovering over
2014 * a child of the view group rather than over the view group itself.
2015 * </p><p>
2016 * The view group can prevent its children from receiving hover events by
2017 * implementing this method and returning <code>true</code> to indicate
2018 * that it would like to intercept hover events. The view group must
2019 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
2020 * for as long as it wishes to continue intercepting hover events from
2021 * its children.
2022 * </p><p>
2023 * Interception preserves the invariant that at most one view can be
2024 * hovered at a time by transferring hover focus from the currently hovered
2025 * child to the view group or vice-versa as needed.
2026 * </p><p>
2027 * If this method returns <code>true</code> and a child is already hovered, then the
2028 * child view will first receive a hover exit event and then the view group
2029 * itself will receive a hover enter event in {@link #onHoverEvent}.
2030 * Likewise, if this method had previously returned <code>true</code> to intercept hover
2031 * events and instead returns <code>false</code> while the pointer is hovering
2032 * within the bounds of one of a child, then the view group will first receive a
2033 * hover exit event in {@link #onHoverEvent} and then the hovered child will
2034 * receive a hover enter event.
2035 * </p><p>
2036 * The default implementation always returns false.
2037 * </p>
2038 *
2039 * @param event The motion event that describes the hover.
2040 * @return True if the view group would like to intercept the hover event
2041 * and prevent its children from receiving it.
2042 */
2043 public boolean onInterceptHoverEvent(MotionEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002044 return false;
2045 }
2046
Jeff Browna032cc02011-03-07 16:56:21 -08002047 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
2048 if (event.getHistorySize() == 0) {
2049 return event;
2050 }
2051 return MotionEvent.obtainNoHistory(event);
2052 }
2053
Jeff Brown10b62902011-06-20 16:40:37 -07002054 /**
2055 * {@inheritDoc}
2056 */
Jeff Browna032cc02011-03-07 16:56:21 -08002057 @Override
2058 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
2059 // Send the event to the child under the pointer.
2060 final int childrenCount = mChildrenCount;
2061 if (childrenCount != 0) {
Jeff Browna032cc02011-03-07 16:56:21 -08002062 final float x = event.getX();
2063 final float y = event.getY();
2064
Chris Craikab008f02014-05-23 17:55:03 -07002065 final ArrayList<View> preorderedList = buildOrderedChildList();
2066 final boolean customOrder = preorderedList == null
2067 && isChildrenDrawingOrderEnabled();
2068 final View[] children = mChildren;
Jeff Browna032cc02011-03-07 16:56:21 -08002069 for (int i = childrenCount - 1; i >= 0; i--) {
Chris Craikab008f02014-05-23 17:55:03 -07002070 int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
2071 final View child = (preorderedList == null)
2072 ? children[childIndex] : preorderedList.get(childIndex);
Jeff Browna032cc02011-03-07 16:56:21 -08002073 if (!canViewReceivePointerEvents(child)
2074 || !isTransformedTouchPointInView(x, y, child, null)) {
2075 continue;
2076 }
2077
2078 if (dispatchTransformedGenericPointerEvent(event, child)) {
Chris Craikab008f02014-05-23 17:55:03 -07002079 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002080 return true;
2081 }
2082 }
Chris Craikab008f02014-05-23 17:55:03 -07002083 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002084 }
2085
2086 // No child handled the event. Send it to this view group.
2087 return super.dispatchGenericPointerEvent(event);
2088 }
2089
Jeff Brown10b62902011-06-20 16:40:37 -07002090 /**
2091 * {@inheritDoc}
2092 */
Jeff Browna032cc02011-03-07 16:56:21 -08002093 @Override
2094 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08002095 // Send the event to the focused child or to this view group if it has focus.
Dianne Hackborn4702a852012-08-17 15:18:29 -07002096 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
2097 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08002098 return super.dispatchGenericFocusedEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07002099 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
2100 == PFLAG_HAS_BOUNDS) {
Jeff Browncb1404e2011-01-15 18:14:15 -08002101 return mFocused.dispatchGenericMotionEvent(event);
2102 }
2103 return false;
2104 }
2105
2106 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002107 * Dispatches a generic pointer event to a child, taking into account
2108 * transformations that apply to the child.
2109 *
2110 * @param event The event to send.
2111 * @param child The view to send the event to.
2112 * @return {@code true} if the child handled the event.
2113 */
2114 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
2115 final float offsetX = mScrollX - child.mLeft;
2116 final float offsetY = mScrollY - child.mTop;
2117
2118 boolean handled;
2119 if (!child.hasIdentityMatrix()) {
2120 MotionEvent transformedEvent = MotionEvent.obtain(event);
2121 transformedEvent.offsetLocation(offsetX, offsetY);
2122 transformedEvent.transform(child.getInverseMatrix());
2123 handled = child.dispatchGenericMotionEvent(transformedEvent);
2124 transformedEvent.recycle();
2125 } else {
2126 event.offsetLocation(offsetX, offsetY);
2127 handled = child.dispatchGenericMotionEvent(event);
2128 event.offsetLocation(-offsetX, -offsetY);
2129 }
2130 return handled;
2131 }
2132
2133 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08002134 * {@inheritDoc}
2135 */
2136 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08002138 if (mInputEventConsistencyVerifier != null) {
2139 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
2140 }
2141
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002142 // If the event targets the accessibility focused view and this is it, start
2143 // normal event dispatch. Maybe a descendant is what will handle the click.
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002144 if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002145 ev.setTargetAccessibilityFocus(false);
2146 }
2147
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002148 boolean handled = false;
2149 if (onFilterTouchEventForSecurity(ev)) {
2150 final int action = ev.getAction();
2151 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07002152
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002153 // Handle an initial down.
2154 if (actionMasked == MotionEvent.ACTION_DOWN) {
2155 // Throw away all previous state when starting a new touch gesture.
2156 // The framework may have dropped the up or cancel event for the previous gesture
2157 // due to an app switch, ANR, or some other state change.
2158 cancelAndClearTouchTargets(ev);
2159 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Adam Powellb08013c2010-09-16 16:28:11 -07002161
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002162 // Check for interception.
2163 final boolean intercepted;
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002164 if (actionMasked == MotionEvent.ACTION_DOWN
2165 || mFirstTouchTarget != null) {
2166 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
2167 if (!disallowIntercept) {
2168 intercepted = onInterceptTouchEvent(ev);
2169 ev.setAction(action); // restore action in case it was changed
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002170 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002171 intercepted = false;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002172 }
2173 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002174 // There are no touch targets and this action is not an initial down
2175 // so this view group continues to intercept touches.
2176 intercepted = true;
2177 }
2178
2179 // If intercepted, start normal event dispatch. Also if there is already
2180 // a view that is handling the gesture, do normal event dispatch.
2181 if (intercepted || mFirstTouchTarget != null) {
2182 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002183 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002184
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002185 // Check for cancelation.
2186 final boolean canceled = resetCancelNextUpFlag(this)
2187 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07002188
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002189 // Update list of touch targets for pointer down, if needed.
2190 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
2191 TouchTarget newTouchTarget = null;
2192 boolean alreadyDispatchedToNewTouchTarget = false;
2193 if (!canceled && !intercepted) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002194
2195 // If the event is targeting accessiiblity focus we give it to the
2196 // view that has accessibility focus and if it does not handle it
2197 // we clear the flag and dispatch the event to all children as usual.
2198 // We are looking up the accessibility focused host to avoid keeping
2199 // state since these events are very rare.
2200 View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
2201 ? findChildWithAccessibilityFocus() : null;
2202
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002203 if (actionMasked == MotionEvent.ACTION_DOWN
2204 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002205 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002206 final int actionIndex = ev.getActionIndex(); // always 0 for down
2207 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
2208 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07002209
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002210 // Clean up earlier touch targets for this pointer id in case they
2211 // have become out of sync.
2212 removePointersFromTouchTargets(idBitsToAssign);
2213
2214 final int childrenCount = mChildrenCount;
Chet Haase9c17fe62013-03-22 17:05:55 -07002215 if (newTouchTarget == null && childrenCount != 0) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002216 final float x = ev.getX(actionIndex);
2217 final float y = ev.getY(actionIndex);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002218 // Find a child that can receive the event.
2219 // Scan children from front to back.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002220 final ArrayList<View> preorderedList = buildTouchDispatchChildList();
Chris Craikab008f02014-05-23 17:55:03 -07002221 final boolean customOrder = preorderedList == null
2222 && isChildrenDrawingOrderEnabled();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002223 final View[] children = mChildren;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002224 for (int i = childrenCount - 1; i >= 0; i--) {
Chris Craikab008f02014-05-23 17:55:03 -07002225 final int childIndex = customOrder
2226 ? getChildDrawingOrder(childrenCount, i) : i;
2227 final View child = (preorderedList == null)
2228 ? children[childIndex] : preorderedList.get(childIndex);
Svetoslavc73cfa02015-02-09 17:14:28 -08002229
2230 // If there is a view that has accessibility focus we want it
2231 // to get the event first and if not handled we will perform a
2232 // normal dispatch. We may do a double iteration but this is
2233 // safer given the timeframe.
2234 if (childWithAccessibilityFocus != null) {
2235 if (childWithAccessibilityFocus != child) {
2236 continue;
2237 }
2238 childWithAccessibilityFocus = null;
2239 i = childrenCount - 1;
2240 }
2241
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002242 if (!canViewReceivePointerEvents(child)
2243 || !isTransformedTouchPointInView(x, y, child, null)) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002244 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002245 continue;
2246 }
2247
2248 newTouchTarget = getTouchTarget(child);
2249 if (newTouchTarget != null) {
2250 // Child is already receiving touch within its bounds.
2251 // Give it the new pointer in addition to the ones it is handling.
2252 newTouchTarget.pointerIdBits |= idBitsToAssign;
2253 break;
2254 }
2255
2256 resetCancelNextUpFlag(child);
2257 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
2258 // Child wants to receive touch within its bounds.
2259 mLastTouchDownTime = ev.getDownTime();
Chris Craikab008f02014-05-23 17:55:03 -07002260 if (preorderedList != null) {
2261 // childIndex points into presorted list, find original index
2262 for (int j = 0; j < childrenCount; j++) {
2263 if (children[childIndex] == mChildren[j]) {
2264 mLastTouchDownIndex = j;
2265 break;
2266 }
2267 }
2268 } else {
2269 mLastTouchDownIndex = childIndex;
2270 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002271 mLastTouchDownX = ev.getX();
2272 mLastTouchDownY = ev.getY();
2273 newTouchTarget = addTouchTarget(child, idBitsToAssign);
2274 alreadyDispatchedToNewTouchTarget = true;
2275 break;
2276 }
Svetoslavc73cfa02015-02-09 17:14:28 -08002277
2278 // The accessibility focus didn't handle the event, so clear
2279 // the flag and do a normal dispatch to all children.
2280 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002281 }
Chris Craikab008f02014-05-23 17:55:03 -07002282 if (preorderedList != null) preorderedList.clear();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002283 }
2284
2285 if (newTouchTarget == null && mFirstTouchTarget != null) {
2286 // Did not find a child to receive the event.
2287 // Assign the pointer to the least recently added target.
2288 newTouchTarget = mFirstTouchTarget;
2289 while (newTouchTarget.next != null) {
2290 newTouchTarget = newTouchTarget.next;
2291 }
2292 newTouchTarget.pointerIdBits |= idBitsToAssign;
2293 }
2294 }
2295 }
2296
2297 // Dispatch to touch targets.
2298 if (mFirstTouchTarget == null) {
2299 // No touch targets so treat this as an ordinary view.
2300 handled = dispatchTransformedTouchEvent(ev, canceled, null,
2301 TouchTarget.ALL_POINTER_IDS);
2302 } else {
2303 // Dispatch to touch targets, excluding the new touch target if we already
2304 // dispatched to it. Cancel touch targets if necessary.
2305 TouchTarget predecessor = null;
2306 TouchTarget target = mFirstTouchTarget;
2307 while (target != null) {
2308 final TouchTarget next = target.next;
2309 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
2310 handled = true;
2311 } else {
2312 final boolean cancelChild = resetCancelNextUpFlag(target.child)
Chet Haase9c17fe62013-03-22 17:05:55 -07002313 || intercepted;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002314 if (dispatchTransformedTouchEvent(ev, cancelChild,
2315 target.child, target.pointerIdBits)) {
2316 handled = true;
2317 }
2318 if (cancelChild) {
2319 if (predecessor == null) {
2320 mFirstTouchTarget = next;
2321 } else {
2322 predecessor.next = next;
2323 }
2324 target.recycle();
2325 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07002326 continue;
2327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002329 predecessor = target;
2330 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002332 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002333
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002334 // Update list of touch targets for pointer up or cancel, if needed.
2335 if (canceled
2336 || actionMasked == MotionEvent.ACTION_UP
2337 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
2338 resetTouchState();
2339 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
2340 final int actionIndex = ev.getActionIndex();
2341 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
2342 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
2344 }
Romain Guy8506ab42009-06-11 17:35:47 -07002345
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002346 if (!handled && mInputEventConsistencyVerifier != null) {
2347 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07002348 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002349 return handled;
2350 }
2351
Romain Guy469b1db2010-10-05 11:49:57 -07002352 /**
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002353 * Provide custom ordering of views in which the touch will be dispatched.
2354 *
2355 * This is called within a tight loop, so you are not allowed to allocate objects, including
2356 * the return array. Instead, you should return a pre-allocated list that will be cleared
2357 * after the dispatch is finished.
2358 * @hide
2359 */
2360 public ArrayList<View> buildTouchDispatchChildList() {
2361 return buildOrderedChildList();
2362 }
2363
2364 /**
Svetoslavc73cfa02015-02-09 17:14:28 -08002365 * Finds the child which has accessibility focus.
2366 *
2367 * @return The child that has focus.
2368 */
2369 private View findChildWithAccessibilityFocus() {
2370 ViewRootImpl viewRoot = getViewRootImpl();
2371 if (viewRoot == null) {
2372 return null;
2373 }
2374
2375 View current = viewRoot.getAccessibilityFocusedHost();
2376 if (current == null) {
2377 return null;
2378 }
2379
2380 ViewParent parent = current.getParent();
2381 while (parent instanceof View) {
2382 if (parent == this) {
2383 return current;
2384 }
2385 current = (View) parent;
2386 parent = current.getParent();
2387 }
2388
2389 return null;
2390 }
2391
2392 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002393 * Resets all touch state in preparation for a new cycle.
2394 */
2395 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002396 clearTouchTargets();
2397 resetCancelNextUpFlag(this);
2398 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
Adam Powell10ba2772014-04-15 09:46:51 -07002399 mNestedScrollAxes = SCROLL_AXIS_NONE;
Jeff Brown20e987b2010-08-23 12:01:02 -07002400 }
2401
Romain Guy469b1db2010-10-05 11:49:57 -07002402 /**
2403 * Resets the cancel next up flag.
2404 * Returns true if the flag was previously set.
2405 */
Romain Guya998dff2012-03-23 18:58:36 -07002406 private static boolean resetCancelNextUpFlag(View view) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002407 if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) {
2408 view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Jeff Brown20e987b2010-08-23 12:01:02 -07002409 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07002410 }
2411 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
2413
Romain Guy469b1db2010-10-05 11:49:57 -07002414 /**
2415 * Clears all touch targets.
2416 */
2417 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002418 TouchTarget target = mFirstTouchTarget;
2419 if (target != null) {
2420 do {
2421 TouchTarget next = target.next;
2422 target.recycle();
2423 target = next;
2424 } while (target != null);
2425 mFirstTouchTarget = null;
2426 }
2427 }
2428
Romain Guy469b1db2010-10-05 11:49:57 -07002429 /**
2430 * Cancels and clears all touch targets.
2431 */
2432 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002433 if (mFirstTouchTarget != null) {
2434 boolean syntheticEvent = false;
2435 if (event == null) {
2436 final long now = SystemClock.uptimeMillis();
2437 event = MotionEvent.obtain(now, now,
2438 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07002439 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07002440 syntheticEvent = true;
2441 }
2442
2443 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2444 resetCancelNextUpFlag(target.child);
2445 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
2446 }
2447 clearTouchTargets();
2448
2449 if (syntheticEvent) {
2450 event.recycle();
2451 }
2452 }
2453 }
2454
Romain Guy469b1db2010-10-05 11:49:57 -07002455 /**
2456 * Gets the touch target for specified child view.
2457 * Returns null if not found.
2458 */
2459 private TouchTarget getTouchTarget(View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002460 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2461 if (target.child == child) {
2462 return target;
2463 }
2464 }
2465 return null;
2466 }
2467
Romain Guy469b1db2010-10-05 11:49:57 -07002468 /**
2469 * Adds a touch target for specified child to the beginning of the list.
2470 * Assumes the target child is not already present.
2471 */
2472 private TouchTarget addTouchTarget(View child, int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002473 TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
2474 target.next = mFirstTouchTarget;
2475 mFirstTouchTarget = target;
2476 return target;
2477 }
2478
Romain Guy469b1db2010-10-05 11:49:57 -07002479 /**
2480 * Removes the pointer ids from consideration.
2481 */
2482 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002483 TouchTarget predecessor = null;
2484 TouchTarget target = mFirstTouchTarget;
2485 while (target != null) {
2486 final TouchTarget next = target.next;
2487 if ((target.pointerIdBits & pointerIdBits) != 0) {
2488 target.pointerIdBits &= ~pointerIdBits;
2489 if (target.pointerIdBits == 0) {
2490 if (predecessor == null) {
2491 mFirstTouchTarget = next;
2492 } else {
2493 predecessor.next = next;
2494 }
2495 target.recycle();
2496 target = next;
2497 continue;
2498 }
2499 }
2500 predecessor = target;
2501 target = next;
2502 }
2503 }
2504
Jeff Brown59a422e2012-04-19 15:19:19 -07002505 private void cancelTouchTarget(View view) {
2506 TouchTarget predecessor = null;
2507 TouchTarget target = mFirstTouchTarget;
2508 while (target != null) {
2509 final TouchTarget next = target.next;
2510 if (target.child == view) {
2511 if (predecessor == null) {
2512 mFirstTouchTarget = next;
2513 } else {
2514 predecessor.next = next;
2515 }
2516 target.recycle();
2517
2518 final long now = SystemClock.uptimeMillis();
2519 MotionEvent event = MotionEvent.obtain(now, now,
2520 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2521 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2522 view.dispatchTouchEvent(event);
2523 event.recycle();
2524 return;
2525 }
2526 predecessor = target;
2527 target = next;
2528 }
2529 }
2530
Romain Guy469b1db2010-10-05 11:49:57 -07002531 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002532 * Returns true if a child view can receive pointer events.
2533 * @hide
2534 */
2535 private static boolean canViewReceivePointerEvents(View child) {
2536 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2537 || child.getAnimation() != null;
2538 }
2539
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002540 private float[] getTempPoint() {
2541 if (mTempPoint == null) {
2542 mTempPoint = new float[2];
2543 }
2544 return mTempPoint;
2545 }
2546
Jeff Browna032cc02011-03-07 16:56:21 -08002547 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002548 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07002549 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07002550 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07002551 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07002552 */
Adam Cohena32edd42010-10-26 10:35:01 -07002553 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07002554 PointF outLocalPoint) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002555 final float[] point = getTempPoint();
2556 point[0] = x;
2557 point[1] = y;
2558 transformPointToViewLocal(point, child);
2559 final boolean isInView = child.pointInView(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002560 if (isInView && outLocalPoint != null) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002561 outLocalPoint.set(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002562 }
2563 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07002564 }
2565
Romain Guy469b1db2010-10-05 11:49:57 -07002566 /**
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002567 * @hide
2568 */
2569 public void transformPointToViewLocal(float[] point, View child) {
2570 point[0] += mScrollX - child.mLeft;
2571 point[1] += mScrollY - child.mTop;
2572
2573 if (!child.hasIdentityMatrix()) {
2574 child.getInverseMatrix().mapPoints(point);
2575 }
2576 }
2577
2578 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002579 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07002580 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07002581 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
2582 */
2583 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07002584 View child, int desiredPointerIdBits) {
2585 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07002586
Jeff Brown20e987b2010-08-23 12:01:02 -07002587 // Canceling motions is a special case. We don't need to perform any transformations
2588 // or filtering. The important part is the action, not the contents.
2589 final int oldAction = event.getAction();
2590 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
2591 event.setAction(MotionEvent.ACTION_CANCEL);
2592 if (child == null) {
2593 handled = super.dispatchTouchEvent(event);
2594 } else {
2595 handled = child.dispatchTouchEvent(event);
2596 }
2597 event.setAction(oldAction);
2598 return handled;
2599 }
Adam Powell2b342f02010-08-18 18:14:13 -07002600
Jeff Brown20e987b2010-08-23 12:01:02 -07002601 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002602 final int oldPointerIdBits = event.getPointerIdBits();
2603 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07002604
Jeff Brown20e987b2010-08-23 12:01:02 -07002605 // If for some reason we ended up in an inconsistent state where it looks like we
2606 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002607 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002608 return false;
2609 }
Adam Powell2b342f02010-08-18 18:14:13 -07002610
Jeff Brown20e987b2010-08-23 12:01:02 -07002611 // If the number of pointers is the same and we don't need to perform any fancy
2612 // irreversible transformations, then we can reuse the motion event for this
2613 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002614 // Otherwise we need to make a copy.
2615 final MotionEvent transformedEvent;
2616 if (newPointerIdBits == oldPointerIdBits) {
2617 if (child == null || child.hasIdentityMatrix()) {
2618 if (child == null) {
2619 handled = super.dispatchTouchEvent(event);
2620 } else {
2621 final float offsetX = mScrollX - child.mLeft;
2622 final float offsetY = mScrollY - child.mTop;
2623 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07002624
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002625 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07002626
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002627 event.offsetLocation(-offsetX, -offsetY);
2628 }
2629 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07002630 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002631 transformedEvent = MotionEvent.obtain(event);
2632 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002633 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07002634 }
2635
Jeff Brown20e987b2010-08-23 12:01:02 -07002636 // Perform any necessary transformations and dispatch.
2637 if (child == null) {
2638 handled = super.dispatchTouchEvent(transformedEvent);
2639 } else {
2640 final float offsetX = mScrollX - child.mLeft;
2641 final float offsetY = mScrollY - child.mTop;
2642 transformedEvent.offsetLocation(offsetX, offsetY);
2643 if (! child.hasIdentityMatrix()) {
2644 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07002645 }
2646
Jeff Brown20e987b2010-08-23 12:01:02 -07002647 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07002648 }
2649
Jeff Brown20e987b2010-08-23 12:01:02 -07002650 // Done.
2651 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07002652 return handled;
2653 }
2654
Romain Guy469b1db2010-10-05 11:49:57 -07002655 /**
Adam Powell2b342f02010-08-18 18:14:13 -07002656 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08002657 * dispatch. This behavior is enabled by default for applications that target an
2658 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07002659 *
2660 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
2661 * views depending on where each pointer initially went down. This allows for user interactions
2662 * such as scrolling two panes of content independently, chording of buttons, and performing
2663 * independent gestures on different pieces of content.
2664 *
2665 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2666 * child views. <code>false</code> to only allow one child view to be the target of
2667 * any MotionEvent received by this ViewGroup.
Scott Main27a85082013-06-10 10:39:48 -07002668 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
Adam Powell2b342f02010-08-18 18:14:13 -07002669 */
2670 public void setMotionEventSplittingEnabled(boolean split) {
2671 // TODO Applications really shouldn't change this setting mid-touch event,
2672 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2673 // with gestures in progress when this is changed.
2674 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07002675 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2676 } else {
2677 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07002678 }
2679 }
2680
2681 /**
Jeff Brown995e7742010-12-22 16:59:36 -08002682 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07002683 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2684 */
2685 public boolean isMotionEventSplittingEnabled() {
2686 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2687 }
2688
2689 /**
George Mount0a778ed2013-12-13 13:35:36 -08002690 * Returns true if this ViewGroup should be considered as a single entity for removal
2691 * when executing an Activity transition. If this is false, child elements will move
2692 * individually during the transition.
George Mount427c6412014-11-05 16:45:36 -08002693 *
George Mount0a778ed2013-12-13 13:35:36 -08002694 * @return True if the ViewGroup should be acted on together during an Activity transition.
George Mount427c6412014-11-05 16:45:36 -08002695 * The default value is true when there is a non-null background or if
2696 * {@link #getTransitionName()} is not null or if a
2697 * non-null {@link android.view.ViewOutlineProvider} other than
2698 * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to
2699 * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise.
George Mount0a778ed2013-12-13 13:35:36 -08002700 */
2701 public boolean isTransitionGroup() {
2702 if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) {
2703 return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0);
2704 } else {
George Mount427c6412014-11-05 16:45:36 -08002705 final ViewOutlineProvider outlineProvider = getOutlineProvider();
2706 return getBackground() != null || getTransitionName() != null ||
2707 (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND);
George Mount0a778ed2013-12-13 13:35:36 -08002708 }
2709 }
2710
2711 /**
2712 * Changes whether or not this ViewGroup should be treated as a single entity during
George Mount31a21722014-03-24 17:44:36 -07002713 * Activity Transitions.
George Mount0a778ed2013-12-13 13:35:36 -08002714 * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
2715 * in Activity transitions. If false, the ViewGroup won't transition,
2716 * only its children. If true, the entire ViewGroup will transition
2717 * together.
George Mount62ab9b72014-05-02 13:51:17 -07002718 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity,
2719 * android.util.Pair[])
George Mount0a778ed2013-12-13 13:35:36 -08002720 */
2721 public void setTransitionGroup(boolean isTransitionGroup) {
2722 mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET;
2723 if (isTransitionGroup) {
2724 mGroupFlags |= FLAG_IS_TRANSITION_GROUP;
2725 } else {
2726 mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP;
2727 }
2728 }
2729
2730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 * {@inheritDoc}
2732 */
2733 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07002734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2736 // We're already in this state, assume our ancestors are too
2737 return;
2738 }
Romain Guy8506ab42009-06-11 17:35:47 -07002739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 if (disallowIntercept) {
2741 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2742 } else {
2743 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2744 }
Romain Guy8506ab42009-06-11 17:35:47 -07002745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 // Pass it up to our parent
2747 if (mParent != null) {
2748 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2749 }
2750 }
2751
2752 /**
2753 * Implement this method to intercept all touch screen motion events. This
2754 * allows you to watch events as they are dispatched to your children, and
2755 * take ownership of the current gesture at any point.
2756 *
2757 * <p>Using this function takes some care, as it has a fairly complicated
2758 * interaction with {@link View#onTouchEvent(MotionEvent)
2759 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2760 * that method as well as this one in the correct way. Events will be
2761 * received in the following order:
2762 *
2763 * <ol>
2764 * <li> You will receive the down event here.
2765 * <li> The down event will be handled either by a child of this view
2766 * group, or given to your own onTouchEvent() method to handle; this means
2767 * you should implement onTouchEvent() to return true, so you will
2768 * continue to see the rest of the gesture (instead of looking for
2769 * a parent view to handle it). Also, by returning true from
2770 * onTouchEvent(), you will not receive any following
2771 * events in onInterceptTouchEvent() and all touch processing must
2772 * happen in onTouchEvent() like normal.
2773 * <li> For as long as you return false from this function, each following
2774 * event (up to and including the final up) will be delivered first here
2775 * and then to the target's onTouchEvent().
2776 * <li> If you return true from here, you will not receive any
2777 * following events: the target view will receive the same event but
2778 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2779 * events will be delivered to your onTouchEvent() method and no longer
2780 * appear here.
2781 * </ol>
2782 *
2783 * @param ev The motion event being dispatched down the hierarchy.
2784 * @return Return true to steal motion events from the children and have
2785 * them dispatched to this ViewGroup through onTouchEvent().
2786 * The current target will receive an ACTION_CANCEL event, and no further
2787 * messages will be delivered here.
2788 */
2789 public boolean onInterceptTouchEvent(MotionEvent ev) {
2790 return false;
2791 }
2792
2793 /**
2794 * {@inheritDoc}
2795 *
2796 * Looks for a view to give focus to respecting the setting specified by
2797 * {@link #getDescendantFocusability()}.
2798 *
2799 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2800 * find focus within the children of this group when appropriate.
2801 *
2802 * @see #FOCUS_BEFORE_DESCENDANTS
2803 * @see #FOCUS_AFTER_DESCENDANTS
2804 * @see #FOCUS_BLOCK_DESCENDANTS
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002805 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 */
2807 @Override
2808 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2809 if (DBG) {
2810 System.out.println(this + " ViewGroup.requestFocus direction="
2811 + direction);
2812 }
2813 int descendantFocusability = getDescendantFocusability();
2814
2815 switch (descendantFocusability) {
2816 case FOCUS_BLOCK_DESCENDANTS:
2817 return super.requestFocus(direction, previouslyFocusedRect);
2818 case FOCUS_BEFORE_DESCENDANTS: {
2819 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2820 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2821 }
2822 case FOCUS_AFTER_DESCENDANTS: {
2823 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2824 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2825 }
2826 default:
2827 throw new IllegalStateException("descendant focusability must be "
2828 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2829 + "but is " + descendantFocusability);
2830 }
2831 }
2832
2833 /**
2834 * Look for a descendant to call {@link View#requestFocus} on.
2835 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2836 * when it wants to request focus within its children. Override this to
2837 * customize how your {@link ViewGroup} requests focus within its children.
2838 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2839 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2840 * to give a finer grained hint about where focus is coming from. May be null
2841 * if there is no hint.
2842 * @return Whether focus was taken.
2843 */
2844 @SuppressWarnings({"ConstantConditions"})
2845 protected boolean onRequestFocusInDescendants(int direction,
2846 Rect previouslyFocusedRect) {
2847 int index;
2848 int increment;
2849 int end;
2850 int count = mChildrenCount;
2851 if ((direction & FOCUS_FORWARD) != 0) {
2852 index = 0;
2853 increment = 1;
2854 end = count;
2855 } else {
2856 index = count - 1;
2857 increment = -1;
2858 end = -1;
2859 }
2860 final View[] children = mChildren;
2861 for (int i = index; i != end; i += increment) {
2862 View child = children[i];
2863 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2864 if (child.requestFocus(direction, previouslyFocusedRect)) {
2865 return true;
2866 }
2867 }
2868 }
2869 return false;
2870 }
Chet Haase5c13d892010-10-08 08:37:55 -07002871
Romain Guya440b002010-02-24 15:57:54 -08002872 /**
2873 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002874 *
Romain Guydcc490f2010-02-24 17:59:35 -08002875 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002876 */
2877 @Override
2878 public void dispatchStartTemporaryDetach() {
2879 super.dispatchStartTemporaryDetach();
2880 final int count = mChildrenCount;
2881 final View[] children = mChildren;
2882 for (int i = 0; i < count; i++) {
2883 children[i].dispatchStartTemporaryDetach();
2884 }
2885 }
Chet Haase5c13d892010-10-08 08:37:55 -07002886
Romain Guya440b002010-02-24 15:57:54 -08002887 /**
2888 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002889 *
Romain Guydcc490f2010-02-24 17:59:35 -08002890 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002891 */
2892 @Override
2893 public void dispatchFinishTemporaryDetach() {
2894 super.dispatchFinishTemporaryDetach();
2895 final int count = mChildrenCount;
2896 final View[] children = mChildren;
2897 for (int i = 0; i < count; i++) {
2898 children[i].dispatchFinishTemporaryDetach();
2899 }
2900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901
2902 /**
2903 * {@inheritDoc}
2904 */
2905 @Override
2906 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07002907 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07002909 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 final int count = mChildrenCount;
2912 final View[] children = mChildren;
2913 for (int i = 0; i < count; i++) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002914 final View child = children[i];
2915 child.dispatchAttachedToWindow(info,
Adam Powelleb2b0af2015-05-20 18:26:35 -07002916 combineVisibility(visibility, child.getVisibility()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 }
Chet Haasec633d2f2015-04-07 10:29:39 -07002918 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
2919 for (int i = 0; i < transientCount; ++i) {
2920 View view = mTransientViews.get(i);
Adam Powelleb2b0af2015-05-20 18:26:35 -07002921 view.dispatchAttachedToWindow(info,
2922 combineVisibility(visibility, view.getVisibility()));
Chet Haasec633d2f2015-04-07 10:29:39 -07002923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 }
2925
svetoslavganov75986cf2009-05-14 22:28:01 -07002926 @Override
Romain Guybb9908b2012-03-08 11:14:07 -08002927 void dispatchScreenStateChanged(int screenState) {
2928 super.dispatchScreenStateChanged(screenState);
2929
2930 final int count = mChildrenCount;
2931 final View[] children = mChildren;
2932 for (int i = 0; i < count; i++) {
2933 children[i].dispatchScreenStateChanged(screenState);
2934 }
2935 }
2936
Alan Viverettea54956a2015-01-07 16:05:02 -08002937 /** @hide */
Romain Guybb9908b2012-03-08 11:14:07 -08002938 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002939 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002940 boolean handled = false;
2941 if (includeForAccessibility()) {
2942 handled = super.dispatchPopulateAccessibilityEventInternal(event);
2943 if (handled) {
2944 return handled;
2945 }
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002946 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002947 // Let our children have a shot in populating the event.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002948 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002949 try {
2950 final int childCount = children.getChildCount();
2951 for (int i = 0; i < childCount; i++) {
2952 View child = children.getChildAt(i);
2953 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2954 handled = child.dispatchPopulateAccessibilityEvent(event);
2955 if (handled) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002956 return handled;
2957 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002958 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002959 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002960 } finally {
2961 children.recycle();
svetoslavganov75986cf2009-05-14 22:28:01 -07002962 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002963 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07002964 }
2965
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002966 /**
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002967 * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002968 * adds in all child views of the view group, in addition to calling the default View
2969 * implementation.
2970 */
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002971 public void dispatchProvideStructure(ViewStructure structure) {
2972 super.dispatchProvideStructure(structure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002973 if (!isAssistBlocked()) {
2974 if (structure.getChildCount() == 0) {
2975 final int childrenCount = getChildCount();
2976 if (childrenCount > 0) {
2977 structure.setChildCount(childrenCount);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002978 ArrayList<View> preorderedList = buildOrderedChildList();
2979 boolean customOrder = preorderedList == null
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002980 && isChildrenDrawingOrderEnabled();
2981 final View[] children = mChildren;
2982 for (int i=0; i<childrenCount; i++) {
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002983 int childIndex;
2984 try {
2985 childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
2986 } catch (IndexOutOfBoundsException e) {
2987 childIndex = i;
2988 if (mContext.getApplicationInfo().targetSdkVersion
2989 < Build.VERSION_CODES.M) {
2990 Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ "
2991 + i + " of " + childrenCount, e);
2992 // At least one app is failing when we call getChildDrawingOrder
2993 // at this point, so deal semi-gracefully with it by falling back
2994 // on the basic order.
2995 customOrder = false;
2996 if (i > 0) {
2997 // If we failed at the first index, there really isn't
2998 // anything to do -- we will just proceed with the simple
2999 // sequence order.
3000 // Otherwise, we failed in the middle, so need to come up
3001 // with an order for the remaining indices and use that.
3002 // Failed at the first one, easy peasy.
3003 int[] permutation = new int[childrenCount];
3004 SparseBooleanArray usedIndices = new SparseBooleanArray();
3005 // Go back and collected the indices we have done so far.
3006 for (int j=0; j<i; j++) {
3007 permutation[j] = getChildDrawingOrder(childrenCount, j);
3008 usedIndices.put(permutation[j], true);
3009 }
3010 // Fill in the remaining indices with indices that have not
3011 // yet been used.
3012 int nextIndex = 0;
3013 for (int j=i; j<childrenCount; j++) {
3014 while (usedIndices.get(nextIndex, false)) {
3015 nextIndex++;
3016 }
3017 permutation[j] = nextIndex;
3018 nextIndex++;
3019 }
3020 // Build the final view list.
3021 preorderedList = new ArrayList<>(childrenCount);
3022 for (int j=0; j<childrenCount; j++) {
3023 preorderedList.add(children[permutation[j]]);
3024 }
3025 }
3026 } else {
3027 throw e;
3028 }
3029 }
Dianne Hackborn799d8d02015-04-22 16:36:39 -07003030 final View child = (preorderedList == null)
3031 ? children[childIndex] : preorderedList.get(childIndex);
Dianne Hackborn49b043f2015-05-07 14:21:38 -07003032 ViewStructure cstructure = structure.newChild(i);
3033 child.dispatchProvideStructure(cstructure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07003034 }
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07003035 }
3036 }
3037 }
3038 }
3039
Alan Viverettea54956a2015-01-07 16:05:02 -08003040 /** @hide */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003041 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08003042 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003043 super.onInitializeAccessibilityNodeInfoInternal(info);
Svet Ganov55bdb102015-02-06 12:41:17 -08003044 if (getAccessibilityNodeProvider() != null) {
3045 return;
3046 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003047 if (mAttachInfo != null) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003048 final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003049 childrenForAccessibility.clear();
3050 addChildrenForAccessibility(childrenForAccessibility);
3051 final int childrenForAccessibilityCount = childrenForAccessibility.size();
3052 for (int i = 0; i < childrenForAccessibilityCount; i++) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003053 final View child = childrenForAccessibility.get(i);
3054 info.addChildUnchecked(child);
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07003055 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003056 childrenForAccessibility.clear();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003057 }
3058 }
3059
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003060 @Override
Maxim Bogatovf399af32015-06-16 15:15:49 -07003061 public CharSequence getAccessibilityClassName() {
3062 return ViewGroup.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003063 }
3064
Svetoslav Ganov42138042012-03-20 11:51:39 -07003065 @Override
Alan Viverette77e9a282013-09-12 17:16:09 -07003066 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
3067 // If this is a live region, we should send a subtree change event
3068 // from this view. Otherwise, we can let it propagate up.
3069 if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) {
3070 notifyViewAccessibilityStateChangedIfNeeded(changeType);
3071 } else if (mParent != null) {
Adam Powell504a10f2013-07-11 15:25:59 -07003072 try {
Alan Viverette77e9a282013-09-12 17:16:09 -07003073 mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType);
Adam Powell504a10f2013-07-11 15:25:59 -07003074 } catch (AbstractMethodError e) {
3075 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
3076 " does not fully implement ViewParent", e);
3077 }
Svetoslav6254f482013-06-04 17:22:14 -07003078 }
3079 }
3080
3081 @Override
3082 void resetSubtreeAccessibilityStateChanged() {
3083 super.resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003084 View[] children = mChildren;
3085 final int childCount = mChildrenCount;
3086 for (int i = 0; i < childCount; i++) {
Svetoslav6254f482013-06-04 17:22:14 -07003087 children[i].resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003088 }
3089 }
3090
3091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 * {@inheritDoc}
Adam Powellb6ab0982015-01-07 17:00:12 -08003093 *
3094 * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p>
3095 *
3096 * @param target The target view dispatching this action
3097 * @param action Action being performed; see
3098 * {@link android.view.accessibility.AccessibilityNodeInfo}
3099 * @param args Optional action arguments
3100 * @return false by default. Subclasses should return true if they handle the event.
3101 */
3102 @Override
3103 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
3104 return false;
3105 }
3106
3107 /**
3108 * {@inheritDoc}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 */
3110 @Override
3111 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07003112 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 // dispatching motion events to a child; we need to get rid of that
3114 // child to avoid dispatching events to it after the window is torn
3115 // down. To make sure we keep the child in a consistent state, we
3116 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07003117 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118
Jeff Brown59a422e2012-04-19 15:19:19 -07003119 // Similarly, set ACTION_EXIT to all hover targets and clear them.
3120 exitHoverTargets();
3121
Chet Haase9c087442011-01-12 16:20:16 -08003122 // In case view is detached while transition is running
Chet Haaseb9895022013-04-02 15:10:58 -07003123 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08003124
Christopher Tate86cab1b2011-01-13 20:28:55 -08003125 // Tear down our drag tracking
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07003126 mChildrenInterestedInDrag = null;
3127 mIsInterestedInDrag = false;
3128 if (mCurrentDragStartEvent != null) {
3129 mCurrentDragStartEvent.recycle();
3130 mCurrentDragStartEvent = null;
Christopher Tate86cab1b2011-01-13 20:28:55 -08003131 }
3132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 final int count = mChildrenCount;
3134 final View[] children = mChildren;
3135 for (int i = 0; i < count; i++) {
3136 children[i].dispatchDetachedFromWindow();
3137 }
John Reckca7a9da2014-03-05 16:29:07 -08003138 clearDisappearingChildren();
Chet Haasec633d2f2015-04-07 10:29:39 -07003139 final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size();
3140 for (int i = 0; i < transientCount; ++i) {
3141 View view = mTransientViews.get(i);
3142 view.dispatchDetachedFromWindow();
3143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 super.dispatchDetachedFromWindow();
3145 }
3146
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003147 /**
3148 * @hide
3149 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 @Override
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003151 protected void internalSetPadding(int left, int top, int right, int bottom) {
Romain Guy2440e672012-08-07 14:43:43 -07003152 super.internalSetPadding(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153
Romain Guy13f35f32011-03-24 12:03:17 -07003154 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 mGroupFlags |= FLAG_PADDING_NOT_NULL;
3156 } else {
3157 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
3158 }
3159 }
3160
3161 /**
3162 * {@inheritDoc}
3163 */
3164 @Override
3165 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
3166 super.dispatchSaveInstanceState(container);
3167 final int count = mChildrenCount;
3168 final View[] children = mChildren;
3169 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003170 View c = children[i];
3171 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3172 c.dispatchSaveInstanceState(container);
3173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 }
3175 }
3176
3177 /**
Romain Guy9fc27812011-04-27 14:21:41 -07003178 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
3179 * to only this view, not to its children. For use when overriding
3180 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
3181 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 *
3183 * @param container the container
3184 */
3185 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
3186 super.dispatchSaveInstanceState(container);
3187 }
3188
3189 /**
3190 * {@inheritDoc}
3191 */
3192 @Override
3193 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
3194 super.dispatchRestoreInstanceState(container);
3195 final int count = mChildrenCount;
3196 final View[] children = mChildren;
3197 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003198 View c = children[i];
3199 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3200 c.dispatchRestoreInstanceState(container);
3201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 }
3203 }
3204
3205 /**
Romain Guy02739a82011-05-16 11:43:18 -07003206 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
3207 * to only this view, not to its children. For use when overriding
3208 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
3209 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 *
3211 * @param container the container
3212 */
3213 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
3214 super.dispatchRestoreInstanceState(container);
3215 }
3216
3217 /**
3218 * Enables or disables the drawing cache for each child of this view group.
3219 *
3220 * @param enabled true to enable the cache, false to dispose of it
3221 */
3222 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
3223 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
3224 final View[] children = mChildren;
3225 final int count = mChildrenCount;
3226 for (int i = 0; i < count; i++) {
3227 children[i].setDrawingCacheEnabled(enabled);
3228 }
3229 }
3230 }
3231
3232 @Override
Romain Guy223ff5c2010-03-02 17:07:47 -08003233 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003234 int count = mChildrenCount;
3235 int[] visibilities = null;
3236
Romain Guy223ff5c2010-03-02 17:07:47 -08003237 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003238 visibilities = new int[count];
3239 for (int i = 0; i < count; i++) {
3240 View child = getChildAt(i);
3241 visibilities[i] = child.getVisibility();
3242 if (visibilities[i] == View.VISIBLE) {
3243 child.setVisibility(INVISIBLE);
3244 }
3245 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003246 }
3247
3248 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07003249
3250 if (skipChildren) {
3251 for (int i = 0; i < count; i++) {
3252 getChildAt(i).setVisibility(visibilities[i]);
Chet Haase5c13d892010-10-08 08:37:55 -07003253 }
Romain Guy65554f22010-03-22 18:58:21 -07003254 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003255
3256 return b;
3257 }
3258
Philip Milne7b757812012-09-19 18:13:44 -07003259 /** Return true if this ViewGroup is laying out using optical bounds. */
3260 boolean isLayoutModeOptical() {
3261 return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS;
3262 }
Romain Guycbc67742012-04-27 16:12:57 -07003263
Philip Milne7b757812012-09-19 18:13:44 -07003264 Insets computeOpticalInsets() {
3265 if (isLayoutModeOptical()) {
3266 int left = 0;
3267 int top = 0;
3268 int right = 0;
3269 int bottom = 0;
3270 for (int i = 0; i < mChildrenCount; i++) {
3271 View child = getChildAt(i);
3272 if (child.getVisibility() == VISIBLE) {
3273 Insets insets = child.getOpticalInsets();
3274 left = Math.max(left, insets.left);
3275 top = Math.max(top, insets.top);
3276 right = Math.max(right, insets.right);
3277 bottom = Math.max(bottom, insets.bottom);
3278 }
3279 }
3280 return Insets.of(left, top, right, bottom);
3281 } else {
3282 return Insets.NONE;
3283 }
3284 }
3285
3286 private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
3287 if (x1 != x2 && y1 != y2) {
3288 if (x1 > x2) {
3289 int tmp = x1; x1 = x2; x2 = tmp;
3290 }
3291 if (y1 > y2) {
3292 int tmp = y1; y1 = y2; y2 = tmp;
3293 }
3294 canvas.drawRect(x1, y1, x2, y2, paint);
3295 }
3296 }
3297
3298 private static int sign(int x) {
3299 return (x >= 0) ? 1 : -1;
3300 }
3301
3302 private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) {
3303 fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy));
3304 fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
3305 }
3306
3307 private int dipsToPixels(int dips) {
3308 float scale = getContext().getResources().getDisplayMetrics().density;
3309 return (int) (dips * scale + 0.5f);
3310 }
3311
Romain Guy6410c0a2013-06-17 11:21:58 -07003312 private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
3313 int lineLength, int lineWidth) {
Philip Milne7b757812012-09-19 18:13:44 -07003314 drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
3315 drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth);
3316 drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth);
3317 drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth);
3318 }
3319
3320 private static void fillDifference(Canvas canvas,
3321 int x2, int y2, int x3, int y3,
3322 int dx1, int dy1, int dx2, int dy2, Paint paint) {
3323 int x1 = x2 - dx1;
3324 int y1 = y2 - dy1;
3325
3326 int x4 = x3 + dx2;
3327 int y4 = y3 + dy2;
3328
3329 fillRect(canvas, paint, x1, y1, x4, y2);
3330 fillRect(canvas, paint, x1, y2, x2, y3);
3331 fillRect(canvas, paint, x3, y2, x4, y3);
3332 fillRect(canvas, paint, x1, y3, x4, y4);
Philip Milne10ca24a2012-04-23 15:38:27 -07003333 }
3334
3335 /**
3336 * @hide
3337 */
Philip Milne7b757812012-09-19 18:13:44 -07003338 protected void onDebugDrawMargins(Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07003339 for (int i = 0; i < getChildCount(); i++) {
3340 View c = getChildAt(i);
Philip Milne7b757812012-09-19 18:13:44 -07003341 c.getLayoutParams().onDebugDraw(c, canvas, paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07003342 }
3343 }
3344
3345 /**
3346 * @hide
3347 */
3348 protected void onDebugDraw(Canvas canvas) {
Philip Milne7b757812012-09-19 18:13:44 -07003349 Paint paint = getDebugPaint();
3350
Philip Milne10ca24a2012-04-23 15:38:27 -07003351 // Draw optical bounds
Philip Milne7b757812012-09-19 18:13:44 -07003352 {
3353 paint.setColor(Color.RED);
3354 paint.setStyle(Paint.Style.STROKE);
Philip Milne7b757812012-09-19 18:13:44 -07003355
Philip Milne10ca24a2012-04-23 15:38:27 -07003356 for (int i = 0; i < getChildCount(); i++) {
3357 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003358 if (c.getVisibility() != View.GONE) {
3359 Insets insets = c.getOpticalInsets();
Philip Milne7b757812012-09-19 18:13:44 -07003360
Chris Craika1dab8b2015-06-30 13:51:25 -07003361 drawRect(canvas, paint,
3362 c.getLeft() + insets.left,
3363 c.getTop() + insets.top,
3364 c.getRight() - insets.right - 1,
3365 c.getBottom() - insets.bottom - 1);
3366 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003367 }
3368 }
3369
Philip Milne10ca24a2012-04-23 15:38:27 -07003370 // Draw margins
Philip Milne7b757812012-09-19 18:13:44 -07003371 {
3372 paint.setColor(Color.argb(63, 255, 0, 255));
3373 paint.setStyle(Paint.Style.FILL);
Philip Milne604f4402012-04-24 19:27:11 -07003374
Philip Milne7b757812012-09-19 18:13:44 -07003375 onDebugDrawMargins(canvas, paint);
3376 }
3377
3378 // Draw clip bounds
3379 {
3380 paint.setColor(Color.rgb(63, 127, 255));
3381 paint.setStyle(Paint.Style.FILL);
3382
3383 int lineLength = dipsToPixels(8);
3384 int lineWidth = dipsToPixels(1);
3385 for (int i = 0; i < getChildCount(); i++) {
3386 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003387 if (c.getVisibility() != View.GONE) {
3388 drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(),
3389 paint, lineLength, lineWidth);
3390 }
Philip Milne7b757812012-09-19 18:13:44 -07003391 }
Philip Milne604f4402012-04-24 19:27:11 -07003392 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003393 }
3394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 /**
3396 * {@inheritDoc}
3397 */
3398 @Override
3399 protected void dispatchDraw(Canvas canvas) {
Chris Craika753f4c2014-07-24 12:39:17 -07003400 boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
Chris Craikab008f02014-05-23 17:55:03 -07003401 final int childrenCount = mChildrenCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 final View[] children = mChildren;
3403 int flags = mGroupFlags;
3404
3405 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
Romain Guy0d9275e2010-10-26 14:22:30 -07003406 final boolean buildCache = !isHardwareAccelerated();
Chris Craikab008f02014-05-23 17:55:03 -07003407 for (int i = 0; i < childrenCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 final View child = children[i];
3409 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
3410 final LayoutParams params = child.getLayoutParams();
Chris Craikab008f02014-05-23 17:55:03 -07003411 attachLayoutAnimationParameters(child, params, i, childrenCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 bindLayoutAnimation(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414 }
3415
3416 final LayoutAnimationController controller = mLayoutAnimationController;
3417 if (controller.willOverlap()) {
3418 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
3419 }
3420
3421 controller.start();
3422
3423 mGroupFlags &= ~FLAG_RUN_ANIMATION;
3424 mGroupFlags &= ~FLAG_ANIMATION_DONE;
3425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 if (mAnimationListener != null) {
3427 mAnimationListener.onAnimationStart(controller.getAnimation());
3428 }
3429 }
3430
Selim Cinek19cadc22014-04-16 17:27:19 +02003431 int clipSaveCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3433 if (clipToPadding) {
Chris Craike4cf1522014-08-04 17:55:22 -07003434 clipSaveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07003435 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
3436 mScrollX + mRight - mLeft - mPaddingRight,
3437 mScrollY + mBottom - mTop - mPaddingBottom);
Selim Cinek19cadc22014-04-16 17:27:19 +02003438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 // We will draw our child's animation, let's reset the flag
Dianne Hackborn4702a852012-08-17 15:18:29 -07003441 mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
3443
3444 boolean more = false;
3445 final long drawingTime = getDrawingTime();
3446
Chris Craik8afd0f22014-08-21 17:41:57 -07003447 if (usingRenderNodeProperties) canvas.insertReorderBarrier();
Chet Haasec633d2f2015-04-07 10:29:39 -07003448 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
3449 int transientIndex = transientCount != 0 ? 0 : -1;
Chris Craikab008f02014-05-23 17:55:03 -07003450 // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
3451 // draw reordering internally
Chris Craika753f4c2014-07-24 12:39:17 -07003452 final ArrayList<View> preorderedList = usingRenderNodeProperties
Chris Craikab008f02014-05-23 17:55:03 -07003453 ? null : buildOrderedChildList();
3454 final boolean customOrder = preorderedList == null
3455 && isChildrenDrawingOrderEnabled();
3456 for (int i = 0; i < childrenCount; i++) {
Chet Haasec633d2f2015-04-07 10:29:39 -07003457 while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
3458 final View transientChild = mTransientViews.get(transientIndex);
3459 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3460 transientChild.getAnimation() != null) {
3461 more |= drawChild(canvas, transientChild, drawingTime);
3462 }
3463 transientIndex++;
3464 if (transientIndex >= transientCount) {
3465 transientIndex = -1;
3466 }
3467 }
Chris Craikab008f02014-05-23 17:55:03 -07003468 int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
3469 final View child = (preorderedList == null)
3470 ? children[childIndex] : preorderedList.get(childIndex);
3471 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
3472 more |= drawChild(canvas, child, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 }
3474 }
Chet Haasec633d2f2015-04-07 10:29:39 -07003475 while (transientIndex >= 0) {
3476 // there may be additional transient views after the normal views
3477 final View transientChild = mTransientViews.get(transientIndex);
3478 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3479 transientChild.getAnimation() != null) {
3480 more |= drawChild(canvas, transientChild, drawingTime);
3481 }
3482 transientIndex++;
3483 if (transientIndex >= transientCount) {
3484 break;
3485 }
3486 }
Chris Craikab008f02014-05-23 17:55:03 -07003487 if (preorderedList != null) preorderedList.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488
3489 // Draw any disappearing views that have animations
3490 if (mDisappearingChildren != null) {
3491 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3492 final int disappearingCount = disappearingChildren.size() - 1;
3493 // Go backwards -- we may delete as animations finish
3494 for (int i = disappearingCount; i >= 0; i--) {
3495 final View child = disappearingChildren.get(i);
3496 more |= drawChild(canvas, child, drawingTime);
3497 }
3498 }
Chris Craik8afd0f22014-08-21 17:41:57 -07003499 if (usingRenderNodeProperties) canvas.insertInorderBarrier();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500
Philip Milne10ca24a2012-04-23 15:38:27 -07003501 if (debugDraw()) {
3502 onDebugDraw(canvas);
3503 }
3504
Chris Craike4cf1522014-08-04 17:55:22 -07003505 if (clipToPadding) {
Selim Cinek19cadc22014-04-16 17:27:19 +02003506 canvas.restoreToCount(clipSaveCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508
3509 // mGroupFlags might have been updated by drawChild()
3510 flags = mGroupFlags;
3511
3512 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08003513 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 }
3515
3516 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
3517 mLayoutAnimationController.isDone() && !more) {
3518 // We want to erase the drawing cache and notify the listener after the
3519 // next frame is drawn because one extra invalidate() is caused by
3520 // drawChild() after the animation is over
3521 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
3522 final Runnable end = new Runnable() {
3523 public void run() {
3524 notifyAnimationListener();
3525 }
3526 };
3527 post(end);
3528 }
3529 }
Romain Guy8506ab42009-06-11 17:35:47 -07003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003532 * Returns the ViewGroupOverlay for this view group, creating it if it does
3533 * not yet exist. In addition to {@link ViewOverlay}'s support for drawables,
3534 * {@link ViewGroupOverlay} allows views to be added to the overlay. These
3535 * views, like overlay drawables, are visual-only; they do not receive input
3536 * events and should not be used as anything other than a temporary
3537 * representation of a view in a parent container, such as might be used
3538 * by an animation effect.
3539 *
Chet Haase95399492013-04-08 14:30:31 -07003540 * <p>Note: Overlays do not currently work correctly with {@link
3541 * SurfaceView} or {@link TextureView}; contents in overlays for these
3542 * types of views may not display correctly.</p>
3543 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003544 * @return The ViewGroupOverlay object for this view.
3545 * @see ViewGroupOverlay
3546 */
3547 @Override
3548 public ViewGroupOverlay getOverlay() {
3549 if (mOverlay == null) {
3550 mOverlay = new ViewGroupOverlay(mContext, this);
3551 }
3552 return (ViewGroupOverlay) mOverlay;
3553 }
3554
3555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 * Returns the index of the child to draw for this iteration. Override this
3557 * if you want to change the drawing order of children. By default, it
3558 * returns i.
3559 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08003560 * NOTE: In order for this method to be called, you must enable child ordering
3561 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07003562 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 * @param i The current iteration.
3564 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07003565 *
Romain Guy293451e2009-11-04 13:59:48 -08003566 * @see #setChildrenDrawingOrderEnabled(boolean)
3567 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 */
3569 protected int getChildDrawingOrder(int childCount, int i) {
3570 return i;
3571 }
Romain Guy8506ab42009-06-11 17:35:47 -07003572
Chris Craikab008f02014-05-23 17:55:03 -07003573 private boolean hasChildWithZ() {
3574 for (int i = 0; i < mChildrenCount; i++) {
3575 if (mChildren[i].getZ() != 0) return true;
3576 }
3577 return false;
3578 }
3579
3580 /**
3581 * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,
Chris Craik57c79c82014-09-30 12:54:31 -07003582 * sorted first by Z, then by child drawing order (if applicable). This list must be cleared
3583 * after use to avoid leaking child Views.
Chris Craikab008f02014-05-23 17:55:03 -07003584 *
3585 * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated
3586 * children.
3587 */
George Mount81206522014-09-26 21:53:39 -07003588 ArrayList<View> buildOrderedChildList() {
Chris Craikab008f02014-05-23 17:55:03 -07003589 final int count = mChildrenCount;
3590 if (count <= 1 || !hasChildWithZ()) return null;
3591
3592 if (mPreSortedChildren == null) {
3593 mPreSortedChildren = new ArrayList<View>(count);
3594 } else {
3595 mPreSortedChildren.ensureCapacity(count);
3596 }
3597
3598 final boolean useCustomOrder = isChildrenDrawingOrderEnabled();
3599 for (int i = 0; i < mChildrenCount; i++) {
3600 // add next child (in child order) to end of list
3601 int childIndex = useCustomOrder ? getChildDrawingOrder(mChildrenCount, i) : i;
3602 View nextChild = mChildren[childIndex];
3603 float currentZ = nextChild.getZ();
3604
3605 // insert ahead of any Views with greater Z
3606 int insertIndex = i;
3607 while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {
3608 insertIndex--;
3609 }
3610 mPreSortedChildren.add(insertIndex, nextChild);
3611 }
3612 return mPreSortedChildren;
3613 }
3614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 private void notifyAnimationListener() {
3616 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
3617 mGroupFlags |= FLAG_ANIMATION_DONE;
3618
3619 if (mAnimationListener != null) {
3620 final Runnable end = new Runnable() {
3621 public void run() {
3622 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
3623 }
3624 };
3625 post(end);
3626 }
3627
Romain Guy849d0a32011-02-01 17:20:48 -08003628 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 }
3630
3631 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08003632 * This method is used to cause children of this ViewGroup to restore or recreate their
3633 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
3634 * to recreate its own display list, which would happen if it went through the normal
3635 * draw/dispatchDraw mechanisms.
3636 *
3637 * @hide
3638 */
3639 @Override
3640 protected void dispatchGetDisplayList() {
3641 final int count = mChildrenCount;
3642 final View[] children = mChildren;
3643 for (int i = 0; i < count; i++) {
3644 final View child = children[i];
John Reckc2330f52015-04-28 13:18:52 -07003645 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) {
Chet Haase6c0665f2014-08-01 13:32:27 -07003646 recreateChildDisplayList(child);
Romain Guy2f57ba52011-02-03 18:03:29 -08003647 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003648 }
Chet Haase91cedf12013-03-11 07:56:30 -07003649 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003650 View overlayView = mOverlay.getOverlayView();
Chet Haase6c0665f2014-08-01 13:32:27 -07003651 recreateChildDisplayList(overlayView);
Chet Haase91cedf12013-03-11 07:56:30 -07003652 }
Chet Haase6c0665f2014-08-01 13:32:27 -07003653 if (mDisappearingChildren != null) {
3654 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3655 final int disappearingCount = disappearingChildren.size();
3656 for (int i = 0; i < disappearingCount; ++i) {
3657 final View child = disappearingChildren.get(i);
3658 recreateChildDisplayList(child);
3659 }
3660 }
3661 }
3662
3663 private void recreateChildDisplayList(View child) {
Chris Craik31a2d062015-05-01 14:22:47 -07003664 child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0;
Chet Haase6c0665f2014-08-01 13:32:27 -07003665 child.mPrivateFlags &= ~PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -07003666 child.updateDisplayListIfDirty();
Chet Haase6c0665f2014-08-01 13:32:27 -07003667 child.mRecreateDisplayList = false;
Chet Haasedaf98e92011-01-10 14:10:36 -08003668 }
3669
3670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 * Draw one child of this View Group. This method is responsible for getting
3672 * the canvas in the right state. This includes clipping, translating so
3673 * that the child's scrolled origin is at 0, 0, and applying any animation
3674 * transformations.
3675 *
3676 * @param canvas The canvas on which to draw the child
3677 * @param child Who to draw
Chet Haasebcca79a2012-02-14 08:45:14 -08003678 * @param drawingTime The time at which draw is occurring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 * @return True if an invalidate() was issued
3680 */
3681 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Chet Haase64a48c12012-02-13 16:33:29 -08003682 return child.draw(canvas, this, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 }
3684
Alan Viverette922e1c62015-05-05 17:18:27 -07003685 @Override
3686 void getScrollIndicatorBounds(@NonNull Rect out) {
3687 super.getScrollIndicatorBounds(out);
3688
3689 // If we have padding and we're supposed to clip children to that
3690 // padding, offset the scroll indicators to match our clip bounds.
3691 final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3692 if (clipToPadding) {
3693 out.left += mPaddingLeft;
3694 out.right -= mPaddingRight;
3695 out.top += mPaddingTop;
3696 out.bottom -= mPaddingBottom;
3697 }
3698 }
3699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 /**
Chris Craikd863a102013-12-19 13:31:15 -08003701 * Returns whether this group's children are clipped to their bounds before drawing.
Chet Haase430742f2013-04-12 11:18:36 -07003702 * The default value is true.
3703 * @see #setClipChildren(boolean)
3704 *
3705 * @return True if the group's children will be clipped to their bounds,
3706 * false otherwise.
3707 */
Chris Craik5c75c522014-09-05 14:08:08 -07003708 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haase430742f2013-04-12 11:18:36 -07003709 public boolean getClipChildren() {
3710 return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0);
3711 }
3712
3713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 * By default, children are clipped to their bounds before drawing. This
3715 * allows view groups to override this behavior for animations, etc.
3716 *
3717 * @param clipChildren true to clip children to their bounds,
3718 * false otherwise
3719 * @attr ref android.R.styleable#ViewGroup_clipChildren
3720 */
3721 public void setClipChildren(boolean clipChildren) {
Chet Haasea1cff502012-02-21 13:43:44 -08003722 boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
3723 if (clipChildren != previousValue) {
3724 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
Chet Haase1271e2c2012-04-20 09:54:27 -07003725 for (int i = 0; i < mChildrenCount; ++i) {
3726 View child = getChildAt(i);
Chris Craik64a12e12014-03-28 18:12:12 -07003727 if (child.mRenderNode != null) {
3728 child.mRenderNode.setClipToBounds(clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -08003729 }
3730 }
John Reckaae9f3b2014-07-28 09:30:36 -07003731 invalidate(true);
Chet Haasea1cff502012-02-21 13:43:44 -08003732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 }
3734
3735 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003736 * Sets whether this ViewGroup will clip its children to its padding and resize (but not
3737 * clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003738 * <p>
3739 * By default, children are clipped to the padding of their parent
Doris Liub134b5e2015-05-28 17:26:46 -07003740 * ViewGroup. This clipping behavior is only enabled if padding is non-zero.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 *
Doris Liub134b5e2015-05-28 17:26:46 -07003742 * @param clipToPadding true to clip children to the padding of the group, and resize (but
3743 * not clip) any EdgeEffect to the padded region. False otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3745 */
3746 public void setClipToPadding(boolean clipToPadding) {
John Reck9fa3a242014-06-27 15:57:19 -07003747 if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) {
3748 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
John Reckaae9f3b2014-07-28 09:30:36 -07003749 invalidate(true);
John Reck9fa3a242014-06-27 15:57:19 -07003750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 }
3752
3753 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003754 * Returns whether this ViewGroup will clip its children to its padding, and resize (but
3755 * not clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003756 * <p>
3757 * By default, children are clipped to the padding of their parent
3758 * Viewgroup. This clipping behavior is only enabled if padding is non-zero.
Adam Powell1c35b082014-07-11 15:37:15 -07003759 *
Doris Liub134b5e2015-05-28 17:26:46 -07003760 * @return true if this ViewGroup clips children to its padding and resizes (but doesn't
3761 * clip) any EdgeEffect to the padded region, false otherwise.
Adam Powell1c35b082014-07-11 15:37:15 -07003762 *
3763 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3764 */
Chris Craik5c75c522014-09-05 14:08:08 -07003765 @ViewDebug.ExportedProperty(category = "drawing")
Adam Powell1c35b082014-07-11 15:37:15 -07003766 public boolean getClipToPadding() {
3767 return hasBooleanFlag(FLAG_CLIP_TO_PADDING);
3768 }
3769
3770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 * {@inheritDoc}
3772 */
3773 @Override
3774 public void dispatchSetSelected(boolean selected) {
3775 final View[] children = mChildren;
3776 final int count = mChildrenCount;
3777 for (int i = 0; i < count; i++) {
3778 children[i].setSelected(selected);
3779 }
3780 }
Romain Guy8506ab42009-06-11 17:35:47 -07003781
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003782 /**
3783 * {@inheritDoc}
3784 */
3785 @Override
3786 public void dispatchSetActivated(boolean activated) {
3787 final View[] children = mChildren;
3788 final int count = mChildrenCount;
3789 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003790 children[i].setActivated(activated);
3791 }
3792 }
3793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 @Override
3795 protected void dispatchSetPressed(boolean pressed) {
3796 final View[] children = mChildren;
3797 final int count = mChildrenCount;
3798 for (int i = 0; i < count; i++) {
Adam Powell035a1fc2012-02-27 15:23:50 -08003799 final View child = children[i];
3800 // Children that are clickable on their own should not
3801 // show a pressed state when their parent view does.
3802 // Clearing a pressed state always propagates.
3803 if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
3804 child.setPressed(pressed);
3805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807 }
3808
Alan Viveretteb942b6f2014-12-08 10:37:39 -08003809 /**
3810 * Dispatches drawable hotspot changes to child views that meet at least
3811 * one of the following criteria:
3812 * <ul>
3813 * <li>Returns {@code false} from both {@link View#isClickable()} and
3814 * {@link View#isLongClickable()}</li>
3815 * <li>Requests duplication of parent state via
3816 * {@link View#setDuplicateParentStateEnabled(boolean)}</li>
3817 * </ul>
3818 *
3819 * @param x hotspot x coordinate
3820 * @param y hotspot y coordinate
3821 * @see #drawableHotspotChanged(float, float)
3822 */
3823 @Override
3824 public void dispatchDrawableHotspotChanged(float x, float y) {
3825 final int count = mChildrenCount;
3826 if (count == 0) {
3827 return;
3828 }
3829
3830 final View[] children = mChildren;
3831 for (int i = 0; i < count; i++) {
3832 final View child = children[i];
3833 // Children that are clickable on their own should not
3834 // receive hotspots when their parent view does.
3835 final boolean nonActionable = !child.isClickable() && !child.isLongClickable();
3836 final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0;
3837 if (nonActionable || duplicatesState) {
3838 final float[] point = getTempPoint();
3839 point[0] = x;
3840 point[1] = y;
3841 transformPointToViewLocal(point, child);
3842 child.drawableHotspotChanged(point[0], point[1]);
3843 }
3844 }
3845 }
3846
Adam Powell14874662013-07-18 19:42:41 -07003847 @Override
3848 void dispatchCancelPendingInputEvents() {
3849 super.dispatchCancelPendingInputEvents();
3850
3851 final View[] children = mChildren;
3852 final int count = mChildrenCount;
3853 for (int i = 0; i < count; i++) {
3854 children[i].dispatchCancelPendingInputEvents();
3855 }
3856 }
3857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 /**
3859 * When this property is set to true, this ViewGroup supports static transformations on
3860 * children; this causes
3861 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3862 * invoked when a child is drawn.
3863 *
3864 * Any subclass overriding
3865 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3866 * set this property to true.
3867 *
3868 * @param enabled True to enable static transformations on children, false otherwise.
3869 *
Chet Haase599913d2012-07-23 16:22:05 -07003870 * @see #getChildStaticTransformation(View, android.view.animation.Transformation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 */
3872 protected void setStaticTransformationsEnabled(boolean enabled) {
3873 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3874 }
3875
3876 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08003877 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
3878 * boolean to indicate whether a static transform was set. The default implementation
3879 * simply returns <code>false</code>; subclasses may override this method for different
Chet Haase599913d2012-07-23 16:22:05 -07003880 * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true
3881 * for this method to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08003883 * @param child The child view whose static transform is being requested
3884 * @param t The Transformation which will hold the result
3885 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07003886 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 */
3888 protected boolean getChildStaticTransformation(View child, Transformation t) {
3889 return false;
3890 }
3891
Romain Guyf6991302013-06-05 17:19:01 -07003892 Transformation getChildTransformation() {
3893 if (mChildTransformation == null) {
3894 mChildTransformation = new Transformation();
3895 }
3896 return mChildTransformation;
3897 }
3898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 /**
3900 * {@hide}
3901 */
3902 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003903 protected View findViewTraversal(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 if (id == mID) {
3905 return this;
3906 }
3907
3908 final View[] where = mChildren;
3909 final int len = mChildrenCount;
3910
3911 for (int i = 0; i < len; i++) {
3912 View v = where[i];
3913
Dianne Hackborn4702a852012-08-17 15:18:29 -07003914 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 v = v.findViewById(id);
3916
3917 if (v != null) {
3918 return v;
3919 }
3920 }
3921 }
3922
3923 return null;
3924 }
3925
3926 /**
3927 * {@hide}
3928 */
3929 @Override
3930 protected View findViewWithTagTraversal(Object tag) {
3931 if (tag != null && tag.equals(mTag)) {
3932 return this;
3933 }
3934
3935 final View[] where = mChildren;
3936 final int len = mChildrenCount;
3937
3938 for (int i = 0; i < len; i++) {
3939 View v = where[i];
3940
Dianne Hackborn4702a852012-08-17 15:18:29 -07003941 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 v = v.findViewWithTag(tag);
3943
3944 if (v != null) {
3945 return v;
3946 }
3947 }
3948 }
3949
3950 return null;
3951 }
3952
3953 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003954 * {@hide}
3955 */
3956 @Override
Jeff Brown4dfbec22011-08-15 14:55:37 -07003957 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003958 if (predicate.apply(this)) {
3959 return this;
3960 }
3961
3962 final View[] where = mChildren;
3963 final int len = mChildrenCount;
3964
3965 for (int i = 0; i < len; i++) {
3966 View v = where[i];
3967
Dianne Hackborn4702a852012-08-17 15:18:29 -07003968 if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003969 v = v.findViewByPredicate(predicate);
3970
3971 if (v != null) {
3972 return v;
3973 }
3974 }
3975 }
3976
3977 return null;
3978 }
3979
3980 /**
Chet Haasec633d2f2015-04-07 10:29:39 -07003981 * This method adds a view to this container at the specified index purely for the
3982 * purposes of allowing that view to draw even though it is not a normal child of
3983 * the container. That is, the view does not participate in layout, focus, accessibility,
3984 * input, or other normal view operations; it is purely an item to be drawn during the normal
3985 * rendering operation of this container. The index that it is added at is the order
3986 * in which it will be drawn, with respect to the other views in the container.
3987 * For example, a transient view added at index 0 will be drawn before all other views
3988 * in the container because it will be drawn first (including before any real view
3989 * at index 0). There can be more than one transient view at any particular index;
3990 * these views will be drawn in the order in which they were added to the list of
3991 * transient views. The index of transient views can also be greater than the number
3992 * of normal views in the container; that just means that they will be drawn after all
3993 * other views are drawn.
3994 *
3995 * <p>Note that since transient views do not participate in layout, they must be sized
3996 * manually or, more typically, they should just use the size that they had before they
3997 * were removed from their container.</p>
3998 *
3999 * <p>Transient views are useful for handling animations of views that have been removed
4000 * from the container, but which should be animated out after the removal. Adding these
4001 * views as transient views allows them to participate in drawing without side-effecting
4002 * the layout of the container.</p>
4003 *
4004 * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed}
4005 * from the container when they are no longer needed. For example, a transient view
4006 * which is added in order to fade it out in its old location should be removed
4007 * once the animation is complete.</p>
4008 *
4009 * @param view The view to be added
4010 * @param index The index at which this view should be drawn, must be >= 0.
4011 * This value is relative to the {@link #getChildAt(int) index} values in the normal
4012 * child list of this container, where any transient view at a particular index will
4013 * be drawn before any normal child at that same index.
Chris Craik66b41392015-04-17 10:08:10 -07004014 *
4015 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004016 */
4017 public void addTransientView(View view, int index) {
4018 if (index < 0) {
4019 return;
4020 }
4021 if (mTransientIndices == null) {
4022 mTransientIndices = new ArrayList<Integer>();
4023 mTransientViews = new ArrayList<View>();
4024 }
4025 final int oldSize = mTransientIndices.size();
4026 if (oldSize > 0) {
4027 int insertionIndex;
4028 for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) {
4029 if (index < mTransientIndices.get(insertionIndex)) {
4030 break;
4031 }
4032 }
4033 mTransientIndices.add(insertionIndex, index);
4034 mTransientViews.add(insertionIndex, view);
4035 } else {
4036 mTransientIndices.add(index);
4037 mTransientViews.add(view);
4038 }
4039 view.mParent = this;
4040 view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4041 invalidate(true);
4042 }
4043
4044 /**
4045 * Removes a view from the list of transient views in this container. If there is no
4046 * such transient view, this method does nothing.
4047 *
4048 * @param view The transient view to be removed
Chris Craik66b41392015-04-17 10:08:10 -07004049 *
4050 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004051 */
4052 public void removeTransientView(View view) {
4053 if (mTransientViews == null) {
4054 return;
4055 }
4056 final int size = mTransientViews.size();
4057 for (int i = 0; i < size; ++i) {
4058 if (view == mTransientViews.get(i)) {
4059 mTransientViews.remove(i);
4060 mTransientIndices.remove(i);
4061 view.mParent = null;
4062 view.dispatchDetachedFromWindow();
4063 invalidate(true);
4064 return;
4065 }
4066 }
4067 }
4068
4069 /**
4070 * Returns the number of transient views in this container. Specific transient
4071 * views and the index at which they were added can be retrieved via
4072 * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}.
4073 *
4074 * @see #addTransientView(View, int)
4075 * @return The number of transient views in this container
Chris Craik66b41392015-04-17 10:08:10 -07004076 *
4077 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004078 */
4079 public int getTransientViewCount() {
4080 return mTransientIndices == null ? 0 : mTransientIndices.size();
4081 }
4082
4083 /**
4084 * Given a valid position within the list of transient views, returns the index of
4085 * the transient view at that position.
4086 *
4087 * @param position The position of the index being queried. Must be at least 0
4088 * and less than the value returned by {@link #getTransientViewCount()}.
4089 * @return The index of the transient view stored in the given position if the
4090 * position is valid, otherwise -1
Chris Craik66b41392015-04-17 10:08:10 -07004091 *
4092 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004093 */
4094 public int getTransientViewIndex(int position) {
4095 if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) {
4096 return -1;
4097 }
4098 return mTransientIndices.get(position);
4099 }
4100
4101 /**
4102 * Given a valid position within the list of transient views, returns the
4103 * transient view at that position.
4104 *
4105 * @param position The position of the view being queried. Must be at least 0
4106 * and less than the value returned by {@link #getTransientViewCount()}.
4107 * @return The transient view stored in the given position if the
4108 * position is valid, otherwise null
Chris Craik66b41392015-04-17 10:08:10 -07004109 *
4110 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004111 */
4112 public View getTransientView(int position) {
4113 if (mTransientViews == null || position >= mTransientViews.size()) {
4114 return null;
4115 }
4116 return mTransientViews.get(position);
4117 }
4118
4119 /**
Romain Guy393a52c2012-05-22 20:21:08 -07004120 * <p>Adds a child view. If no layout parameters are already set on the child, the
4121 * default parameters for this ViewGroup are set on the child.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004122 *
Romain Guy393a52c2012-05-22 20:21:08 -07004123 * <p><strong>Note:</strong> do not invoke this method from
4124 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4125 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 *
4127 * @param child the child view to add
4128 *
4129 * @see #generateDefaultLayoutParams()
4130 */
4131 public void addView(View child) {
4132 addView(child, -1);
4133 }
4134
4135 /**
4136 * Adds a child view. If no layout parameters are already set on the child, the
4137 * default parameters for this ViewGroup are set on the child.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004138 *
Romain Guy393a52c2012-05-22 20:21:08 -07004139 * <p><strong>Note:</strong> do not invoke this method from
4140 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4141 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 *
4143 * @param child the child view to add
4144 * @param index the position at which to add the child
4145 *
4146 * @see #generateDefaultLayoutParams()
4147 */
4148 public void addView(View child, int index) {
Adam Powell45a9da52014-10-09 09:44:18 -07004149 if (child == null) {
4150 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 LayoutParams params = child.getLayoutParams();
4153 if (params == null) {
4154 params = generateDefaultLayoutParams();
4155 if (params == null) {
4156 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
4157 }
4158 }
4159 addView(child, index, params);
4160 }
4161
4162 /**
4163 * Adds a child view with this ViewGroup's default layout parameters and the
4164 * specified width and height.
4165 *
Romain Guy393a52c2012-05-22 20:21:08 -07004166 * <p><strong>Note:</strong> do not invoke this method from
4167 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4168 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 * @param child the child view to add
4171 */
4172 public void addView(View child, int width, int height) {
4173 final LayoutParams params = generateDefaultLayoutParams();
4174 params.width = width;
4175 params.height = height;
4176 addView(child, -1, params);
4177 }
4178
4179 /**
4180 * Adds a child view with the specified layout parameters.
4181 *
Romain Guy393a52c2012-05-22 20:21:08 -07004182 * <p><strong>Note:</strong> do not invoke this method from
4183 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4184 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 * @param child the child view to add
4187 * @param params the layout parameters to set on the child
4188 */
4189 public void addView(View child, LayoutParams params) {
4190 addView(child, -1, params);
4191 }
4192
4193 /**
4194 * Adds a child view with the specified layout parameters.
4195 *
Romain Guy393a52c2012-05-22 20:21:08 -07004196 * <p><strong>Note:</strong> do not invoke this method from
4197 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4198 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 * @param child the child view to add
Alan Viverette77bb6f12015-02-11 17:24:33 -08004201 * @param index the position at which to add the child or -1 to add last
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 * @param params the layout parameters to set on the child
4203 */
4204 public void addView(View child, int index, LayoutParams params) {
4205 if (DBG) {
4206 System.out.println(this + " addView");
4207 }
4208
Adam Powell45a9da52014-10-09 09:44:18 -07004209 if (child == null) {
4210 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4211 }
4212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
4214 // therefore, we call requestLayout() on ourselves before, so that the child's request
4215 // will be blocked at our level
4216 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004217 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 addViewInner(child, index, params, false);
4219 }
4220
4221 /**
4222 * {@inheritDoc}
4223 */
4224 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
4225 if (!checkLayoutParams(params)) {
4226 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
4227 }
4228 if (view.mParent != this) {
4229 throw new IllegalArgumentException("Given view not a child of " + this);
4230 }
4231 view.setLayoutParams(params);
4232 }
4233
4234 /**
4235 * {@inheritDoc}
4236 */
4237 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
4238 return p != null;
4239 }
4240
4241 /**
4242 * Interface definition for a callback to be invoked when the hierarchy
4243 * within this view changed. The hierarchy changes whenever a child is added
4244 * to or removed from this view.
4245 */
4246 public interface OnHierarchyChangeListener {
4247 /**
4248 * Called when a new child is added to a parent view.
4249 *
4250 * @param parent the view in which a child was added
4251 * @param child the new child view added in the hierarchy
4252 */
4253 void onChildViewAdded(View parent, View child);
4254
4255 /**
4256 * Called when a child is removed from a parent view.
4257 *
4258 * @param parent the view from which the child was removed
4259 * @param child the child removed from the hierarchy
4260 */
4261 void onChildViewRemoved(View parent, View child);
4262 }
4263
4264 /**
4265 * Register a callback to be invoked when a child is added to or removed
4266 * from this view.
4267 *
4268 * @param listener the callback to invoke on hierarchy change
4269 */
4270 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
4271 mOnHierarchyChangeListener = listener;
4272 }
4273
Adam Powell6690d012015-06-17 16:41:56 -07004274 void dispatchViewAdded(View child) {
4275 onViewAdded(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004276 if (mOnHierarchyChangeListener != null) {
4277 mOnHierarchyChangeListener.onChildViewAdded(this, child);
4278 }
4279 }
4280
4281 /**
Adam Powell6690d012015-06-17 16:41:56 -07004282 * Called when a new child is added to this ViewGroup. Overrides should always
4283 * call super.onViewAdded.
4284 *
4285 * @param child the added child view
Philip Milnef51d91c2011-07-18 16:12:19 -07004286 */
Adam Powell6690d012015-06-17 16:41:56 -07004287 public void onViewAdded(View child) {
4288 }
4289
4290 void dispatchViewRemoved(View child) {
4291 onViewRemoved(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004292 if (mOnHierarchyChangeListener != null) {
4293 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
4294 }
4295 }
4296
Adam Powell6690d012015-06-17 16:41:56 -07004297 /**
4298 * Called when a child view is removed from this ViewGroup. Overrides should always
4299 * call super.onViewRemoved.
4300 *
4301 * @param child the removed child view
4302 */
4303 public void onViewRemoved(View child) {
4304 }
4305
Philip Milnecfb631b2012-10-26 10:51:46 -07004306 private void clearCachedLayoutMode() {
Svetoslav6254f482013-06-04 17:22:14 -07004307 if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnecfb631b2012-10-26 10:51:46 -07004308 mLayoutMode = LAYOUT_MODE_UNDEFINED;
4309 }
4310 }
4311
4312 @Override
4313 protected void onAttachedToWindow() {
4314 super.onAttachedToWindow();
4315 clearCachedLayoutMode();
4316 }
4317
4318 @Override
4319 protected void onDetachedFromWindow() {
4320 super.onDetachedFromWindow();
4321 clearCachedLayoutMode();
4322 }
4323
Philip Milnef51d91c2011-07-18 16:12:19 -07004324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 * Adds a view during layout. This is useful if in your onLayout() method,
4326 * you need to add more views (as does the list view for example).
4327 *
4328 * If index is negative, it means put it at the end of the list.
4329 *
4330 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004331 * @param index the index at which the child must be added or -1 to add last
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 * @param params the layout parameters to associate with the child
4333 * @return true if the child was added, false otherwise
4334 */
4335 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
4336 return addViewInLayout(child, index, params, false);
4337 }
4338
4339 /**
4340 * Adds a view during layout. This is useful if in your onLayout() method,
4341 * you need to add more views (as does the list view for example).
4342 *
4343 * If index is negative, it means put it at the end of the list.
4344 *
4345 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004346 * @param index the index at which the child must be added or -1 to add last
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 * @param params the layout parameters to associate with the child
4348 * @param preventRequestLayout if true, calling this method will not trigger a
4349 * layout request on child
4350 * @return true if the child was added, false otherwise
4351 */
4352 protected boolean addViewInLayout(View child, int index, LayoutParams params,
4353 boolean preventRequestLayout) {
Adam Powell45a9da52014-10-09 09:44:18 -07004354 if (child == null) {
4355 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 child.mParent = null;
4358 addViewInner(child, index, params, preventRequestLayout);
Dianne Hackborn4702a852012-08-17 15:18:29 -07004359 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 return true;
4361 }
4362
4363 /**
4364 * Prevents the specified child to be laid out during the next layout pass.
4365 *
4366 * @param child the child on which to perform the cleanup
4367 */
4368 protected void cleanupLayoutState(View child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004369 child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 }
4371
4372 private void addViewInner(View child, int index, LayoutParams params,
4373 boolean preventRequestLayout) {
4374
Chet Haasee8e45d32011-03-02 17:07:35 -08004375 if (mTransition != null) {
4376 // Don't prevent other add transitions from completing, but cancel remove
4377 // transitions to let them complete the process before we add to the container
4378 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08004379 }
4380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 if (child.getParent() != null) {
4382 throw new IllegalStateException("The specified child already has a parent. " +
4383 "You must call removeView() on the child's parent first.");
4384 }
4385
Chet Haase21cd1382010-09-01 17:42:29 -07004386 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004387 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004388 }
4389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 if (!checkLayoutParams(params)) {
4391 params = generateLayoutParams(params);
4392 }
4393
4394 if (preventRequestLayout) {
4395 child.mLayoutParams = params;
4396 } else {
4397 child.setLayoutParams(params);
4398 }
4399
4400 if (index < 0) {
4401 index = mChildrenCount;
4402 }
4403
4404 addInArray(child, index);
4405
4406 // tell our children
4407 if (preventRequestLayout) {
4408 child.assignParent(this);
4409 } else {
4410 child.mParent = this;
4411 }
4412
4413 if (child.hasFocus()) {
4414 requestChildFocus(child, child.findFocus());
4415 }
Romain Guy8506ab42009-06-11 17:35:47 -07004416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07004418 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07004419 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 ai.mKeepScreenOn = false;
4421 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4422 if (ai.mKeepScreenOn) {
4423 needGlobalAttributesUpdate(true);
4424 }
4425 ai.mKeepScreenOn = lastKeepOn;
4426 }
4427
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004428 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglioa7e0bcd2012-10-16 19:55:01 -07004429 child.resetRtlProperties();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004430 }
4431
Adam Powell6690d012015-06-17 16:41:56 -07004432 dispatchViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433
4434 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
4435 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
4436 }
Adam Powell539ee872012-02-03 19:00:49 -08004437
4438 if (child.hasTransientState()) {
4439 childHasTransientStateChanged(child, true);
4440 }
Svetoslav6254f482013-06-04 17:22:14 -07004441
Svetoslav8e3feb12014-02-24 13:46:47 -08004442 if (child.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004443 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004444 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004445
4446 if (mTransientIndices != null) {
4447 final int transientCount = mTransientIndices.size();
4448 for (int i = 0; i < transientCount; ++i) {
4449 final int oldIndex = mTransientIndices.get(i);
4450 if (index <= oldIndex) {
4451 mTransientIndices.set(i, oldIndex + 1);
4452 }
4453 }
4454 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004455
4456 if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) {
4457 notifyChildOfDragStart(child);
4458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 }
4460
4461 private void addInArray(View child, int index) {
4462 View[] children = mChildren;
4463 final int count = mChildrenCount;
4464 final int size = children.length;
4465 if (index == count) {
4466 if (size == count) {
4467 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4468 System.arraycopy(children, 0, mChildren, 0, size);
4469 children = mChildren;
4470 }
4471 children[mChildrenCount++] = child;
4472 } else if (index < count) {
4473 if (size == count) {
4474 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4475 System.arraycopy(children, 0, mChildren, 0, index);
4476 System.arraycopy(children, index, mChildren, index + 1, count - index);
4477 children = mChildren;
4478 } else {
4479 System.arraycopy(children, index, children, index + 1, count - index);
4480 }
4481 children[index] = child;
4482 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08004483 if (mLastTouchDownIndex >= index) {
4484 mLastTouchDownIndex++;
4485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 } else {
4487 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
4488 }
4489 }
4490
4491 // This method also sets the child's mParent to null
4492 private void removeFromArray(int index) {
4493 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07004494 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
4495 children[index].mParent = null;
4496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 final int count = mChildrenCount;
4498 if (index == count - 1) {
4499 children[--mChildrenCount] = null;
4500 } else if (index >= 0 && index < count) {
4501 System.arraycopy(children, index + 1, children, index, count - index - 1);
4502 children[--mChildrenCount] = null;
4503 } else {
4504 throw new IndexOutOfBoundsException();
4505 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08004506 if (mLastTouchDownIndex == index) {
4507 mLastTouchDownTime = 0;
4508 mLastTouchDownIndex = -1;
4509 } else if (mLastTouchDownIndex > index) {
4510 mLastTouchDownIndex--;
4511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 }
4513
4514 // This method also sets the children's mParent to null
4515 private void removeFromArray(int start, int count) {
4516 final View[] children = mChildren;
4517 final int childrenCount = mChildrenCount;
4518
4519 start = Math.max(0, start);
4520 final int end = Math.min(childrenCount, start + count);
4521
4522 if (start == end) {
4523 return;
4524 }
4525
4526 if (end == childrenCount) {
4527 for (int i = start; i < end; i++) {
4528 children[i].mParent = null;
4529 children[i] = null;
4530 }
4531 } else {
4532 for (int i = start; i < end; i++) {
4533 children[i].mParent = null;
4534 }
4535
4536 // Since we're looping above, we might as well do the copy, but is arraycopy()
4537 // faster than the extra 2 bounds checks we would do in the loop?
4538 System.arraycopy(children, end, children, start, childrenCount - end);
4539
4540 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
4541 children[i] = null;
4542 }
4543 }
4544
4545 mChildrenCount -= (end - start);
4546 }
4547
4548 private void bindLayoutAnimation(View child) {
4549 Animation a = mLayoutAnimationController.getAnimationForView(child);
4550 child.setAnimation(a);
4551 }
4552
4553 /**
4554 * Subclasses should override this method to set layout animation
4555 * parameters on the supplied child.
4556 *
4557 * @param child the child to associate with animation parameters
4558 * @param params the child's layout parameters which hold the animation
4559 * parameters
4560 * @param index the index of the child in the view group
4561 * @param count the number of children in the view group
4562 */
4563 protected void attachLayoutAnimationParameters(View child,
4564 LayoutParams params, int index, int count) {
4565 LayoutAnimationController.AnimationParameters animationParams =
4566 params.layoutAnimationParameters;
4567 if (animationParams == null) {
4568 animationParams = new LayoutAnimationController.AnimationParameters();
4569 params.layoutAnimationParameters = animationParams;
4570 }
4571
4572 animationParams.count = count;
4573 animationParams.index = index;
4574 }
4575
4576 /**
4577 * {@inheritDoc}
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004578 *
Romain Guy393a52c2012-05-22 20:21:08 -07004579 * <p><strong>Note:</strong> do not invoke this method from
4580 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4581 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 */
4583 public void removeView(View view) {
Alan Viverette177ec4602014-10-17 13:34:50 -07004584 if (removeViewInternal(view)) {
4585 requestLayout();
4586 invalidate(true);
4587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 }
4589
4590 /**
4591 * Removes a view during layout. This is useful if in your onLayout() method,
4592 * you need to remove more views.
4593 *
Romain Guy393a52c2012-05-22 20:21:08 -07004594 * <p><strong>Note:</strong> do not invoke this method from
4595 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4596 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004597 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 * @param view the view to remove from the group
4599 */
4600 public void removeViewInLayout(View view) {
4601 removeViewInternal(view);
4602 }
4603
4604 /**
4605 * Removes a range of views during layout. This is useful if in your onLayout() method,
4606 * you need to remove more views.
4607 *
Romain Guy393a52c2012-05-22 20:21:08 -07004608 * <p><strong>Note:</strong> do not invoke this method from
4609 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4610 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 * @param start the index of the first view to remove from the group
4613 * @param count the number of views to remove from the group
4614 */
4615 public void removeViewsInLayout(int start, int count) {
4616 removeViewsInternal(start, count);
4617 }
4618
4619 /**
4620 * Removes the view at the specified position in the group.
4621 *
Romain Guy393a52c2012-05-22 20:21:08 -07004622 * <p><strong>Note:</strong> do not invoke this method from
4623 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4624 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 * @param index the position in the group of the view to remove
4627 */
4628 public void removeViewAt(int index) {
4629 removeViewInternal(index, getChildAt(index));
4630 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004631 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 }
4633
4634 /**
4635 * Removes the specified range of views from the group.
4636 *
Romain Guy393a52c2012-05-22 20:21:08 -07004637 * <p><strong>Note:</strong> do not invoke this method from
4638 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4639 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 * @param start the first position in the group of the range of views to remove
4642 * @param count the number of views to remove
4643 */
4644 public void removeViews(int start, int count) {
4645 removeViewsInternal(start, count);
4646 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004647 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
4649
Alan Viverette177ec4602014-10-17 13:34:50 -07004650 private boolean removeViewInternal(View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 final int index = indexOfChild(view);
4652 if (index >= 0) {
4653 removeViewInternal(index, view);
Alan Viverette177ec4602014-10-17 13:34:50 -07004654 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 }
Alan Viverette177ec4602014-10-17 13:34:50 -07004656 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 }
4658
4659 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07004660 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004661 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004662 }
4663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 boolean clearChildFocus = false;
4665 if (view == mFocused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004666 view.unFocus(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 clearChildFocus = true;
4668 }
4669
Alan Viverette632af842014-10-28 13:45:11 -07004670 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004671
Jeff Brown59a422e2012-04-19 15:19:19 -07004672 cancelTouchTarget(view);
4673 cancelHoverTarget(view);
4674
Chet Haase21cd1382010-09-01 17:42:29 -07004675 if (view.getAnimation() != null ||
4676 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 addDisappearingView(view);
4678 } else if (view.mAttachInfo != null) {
4679 view.dispatchDetachedFromWindow();
4680 }
4681
Adam Powell539ee872012-02-03 19:00:49 -08004682 if (view.hasTransientState()) {
4683 childHasTransientStateChanged(view, false);
4684 }
4685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 removeFromArray(index);
4689
4690 if (clearChildFocus) {
4691 clearChildFocus(view);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004692 if (!rootViewRequestFocus()) {
4693 notifyGlobalFocusCleared(this);
4694 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004695 }
Romain Guy6fb05632012-11-29 10:50:33 -08004696
Adam Powell6690d012015-06-17 16:41:56 -07004697 dispatchViewRemoved(view);
Svetoslav6254f482013-06-04 17:22:14 -07004698
Svetoslav8e3feb12014-02-24 13:46:47 -08004699 if (view.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004700 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004701 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004702
4703 int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
4704 for (int i = 0; i < transientCount; ++i) {
4705 final int oldIndex = mTransientIndices.get(i);
4706 if (index < oldIndex) {
4707 mTransientIndices.set(i, oldIndex - 1);
4708 }
4709 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004710
4711 if (mCurrentDragStartEvent != null) {
4712 mChildrenInterestedInDrag.remove(view);
4713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 }
4715
Chet Haase21cd1382010-09-01 17:42:29 -07004716 /**
4717 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4718 * not null, changes in layout which occur because of children being added to or removed from
4719 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4720 * object. By default, the transition object is null (so layout changes are not animated).
4721 *
Chet Haaseef3cbfd2013-08-21 14:01:02 -07004722 * <p>Replacing a non-null transition will cause that previous transition to be
4723 * canceled, if it is currently running, to restore this container to
4724 * its correct post-transition state.</p>
4725 *
Chet Haase21cd1382010-09-01 17:42:29 -07004726 * @param transition The LayoutTransition object that will animated changes in layout. A value
4727 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07004728 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07004729 */
4730 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004731 if (mTransition != null) {
Chet Haasefee6f2b2013-08-27 12:22:29 -07004732 LayoutTransition previousTransition = mTransition;
4733 previousTransition.cancel();
4734 previousTransition.removeTransitionListener(mLayoutTransitionListener);
Chet Haaseb20db3e2010-09-10 13:07:30 -07004735 }
Chet Haase21cd1382010-09-01 17:42:29 -07004736 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07004737 if (mTransition != null) {
4738 mTransition.addTransitionListener(mLayoutTransitionListener);
4739 }
4740 }
4741
4742 /**
4743 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4744 * not null, changes in layout which occur because of children being added to or removed from
4745 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4746 * object. By default, the transition object is null (so layout changes are not animated).
4747 *
4748 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
4749 * A value of <code>null</code> means no transition will run on layout changes.
4750 */
4751 public LayoutTransition getLayoutTransition() {
4752 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07004753 }
4754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 private void removeViewsInternal(int start, int count) {
Chris Craik18a759d2015-10-14 14:16:33 -07004756 final int end = start + count;
4757
4758 if (start < 0 || count < 0 || end > mChildrenCount) {
4759 throw new IndexOutOfBoundsException();
4760 }
4761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 final View focused = mFocused;
4763 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004764 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765
4766 final View[] children = mChildren;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767
4768 for (int i = start; i < end; i++) {
4769 final View view = children[i];
4770
Chet Haase21cd1382010-09-01 17:42:29 -07004771 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004772 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004773 }
4774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004776 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004777 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 }
4779
Alan Viverette632af842014-10-28 13:45:11 -07004780 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004781
Jeff Brown59a422e2012-04-19 15:19:19 -07004782 cancelTouchTarget(view);
4783 cancelHoverTarget(view);
4784
Chet Haase21cd1382010-09-01 17:42:29 -07004785 if (view.getAnimation() != null ||
4786 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 addDisappearingView(view);
4788 } else if (detach) {
4789 view.dispatchDetachedFromWindow();
4790 }
4791
Adam Powell539ee872012-02-03 19:00:49 -08004792 if (view.hasTransientState()) {
4793 childHasTransientStateChanged(view, false);
4794 }
4795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004797
Adam Powell6690d012015-06-17 16:41:56 -07004798 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 }
4800
4801 removeFromArray(start, count);
4802
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004803 if (clearChildFocus) {
4804 clearChildFocus(focused);
4805 if (!rootViewRequestFocus()) {
4806 notifyGlobalFocusCleared(focused);
4807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 }
4809 }
4810
4811 /**
4812 * Call this method to remove all child views from the
4813 * ViewGroup.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004814 *
Romain Guy393a52c2012-05-22 20:21:08 -07004815 * <p><strong>Note:</strong> do not invoke this method from
4816 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4817 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 */
4819 public void removeAllViews() {
4820 removeAllViewsInLayout();
4821 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004822 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 }
4824
4825 /**
4826 * Called by a ViewGroup subclass to remove child views from itself,
4827 * when it must first know its size on screen before it can calculate how many
4828 * child views it will render. An example is a Gallery or a ListView, which
4829 * may "have" 50 children, but actually only render the number of children
4830 * that can currently fit inside the object on screen. Do not call
4831 * this method unless you are extending ViewGroup and understand the
4832 * view measuring and layout pipeline.
Romain Guy393a52c2012-05-22 20:21:08 -07004833 *
4834 * <p><strong>Note:</strong> do not invoke this method from
4835 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4836 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 */
4838 public void removeAllViewsInLayout() {
4839 final int count = mChildrenCount;
4840 if (count <= 0) {
4841 return;
4842 }
4843
4844 final View[] children = mChildren;
4845 mChildrenCount = 0;
4846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 final View focused = mFocused;
4848 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004849 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850
4851 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 for (int i = count - 1; i >= 0; i--) {
4854 final View view = children[i];
4855
Chet Haase21cd1382010-09-01 17:42:29 -07004856 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004857 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004858 }
4859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004861 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004862 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 }
4864
Alan Viverette632af842014-10-28 13:45:11 -07004865 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004866
Jeff Brown59a422e2012-04-19 15:19:19 -07004867 cancelTouchTarget(view);
4868 cancelHoverTarget(view);
4869
Chet Haase21cd1382010-09-01 17:42:29 -07004870 if (view.getAnimation() != null ||
4871 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 addDisappearingView(view);
4873 } else if (detach) {
4874 view.dispatchDetachedFromWindow();
4875 }
4876
Adam Powell539ee872012-02-03 19:00:49 -08004877 if (view.hasTransientState()) {
4878 childHasTransientStateChanged(view, false);
4879 }
4880
Adam Powell6690d012015-06-17 16:41:56 -07004881 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882
4883 view.mParent = null;
4884 children[i] = null;
4885 }
4886
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004887 if (clearChildFocus) {
4888 clearChildFocus(focused);
4889 if (!rootViewRequestFocus()) {
4890 notifyGlobalFocusCleared(focused);
4891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 }
4893 }
4894
4895 /**
4896 * Finishes the removal of a detached view. This method will dispatch the detached from
4897 * window event and notify the hierarchy change listener.
Chet Haaseca479d42012-08-30 17:20:08 -07004898 * <p>
4899 * This method is intended to be lightweight and makes no assumptions about whether the
4900 * parent or child should be redrawn. Proper use of this method will include also making
4901 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4902 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4903 * which performs a {@link #requestLayout()} on the next frame, after all detach/remove
4904 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 *
4906 * @param child the child to be definitely removed from the view hierarchy
4907 * @param animate if true and the view has an animation, the view is placed in the
4908 * disappearing views list, otherwise, it is detached from the window
4909 *
4910 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4911 * @see #detachAllViewsFromParent()
4912 * @see #detachViewFromParent(View)
4913 * @see #detachViewFromParent(int)
4914 */
4915 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07004916 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004917 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004918 }
4919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 if (child == mFocused) {
4921 child.clearFocus();
4922 }
Romain Guy8506ab42009-06-11 17:35:47 -07004923
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004924 child.clearAccessibilityFocus();
4925
Jeff Brown59a422e2012-04-19 15:19:19 -07004926 cancelTouchTarget(child);
4927 cancelHoverTarget(child);
4928
Chet Haase21cd1382010-09-01 17:42:29 -07004929 if ((animate && child.getAnimation() != null) ||
4930 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 addDisappearingView(child);
4932 } else if (child.mAttachInfo != null) {
4933 child.dispatchDetachedFromWindow();
4934 }
4935
Adam Powell539ee872012-02-03 19:00:49 -08004936 if (child.hasTransientState()) {
4937 childHasTransientStateChanged(child, false);
4938 }
4939
Adam Powell6690d012015-06-17 16:41:56 -07004940 dispatchViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 }
4942
4943 /**
4944 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
Chet Haaseca479d42012-08-30 17:20:08 -07004945 * sets the layout parameters and puts the view in the list of children so that
4946 * it can be retrieved by calling {@link #getChildAt(int)}.
4947 * <p>
4948 * This method is intended to be lightweight and makes no assumptions about whether the
4949 * parent or child should be redrawn. Proper use of this method will include also making
4950 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4951 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4952 * which performs a {@link #requestLayout()} on the next frame, after all detach/attach
4953 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
4954 * <p>
4955 * This method should be called only for views which were detached from their parent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 *
4957 * @param child the child to attach
4958 * @param index the index at which the child should be attached
4959 * @param params the layout parameters of the child
4960 *
4961 * @see #removeDetachedView(View, boolean)
4962 * @see #detachAllViewsFromParent()
4963 * @see #detachViewFromParent(View)
4964 * @see #detachViewFromParent(int)
4965 */
4966 protected void attachViewToParent(View child, int index, LayoutParams params) {
4967 child.mLayoutParams = params;
4968
4969 if (index < 0) {
4970 index = mChildrenCount;
4971 }
4972
4973 addInArray(child, index);
4974
4975 child.mParent = this;
Dianne Hackborn4702a852012-08-17 15:18:29 -07004976 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK
4977 & ~PFLAG_DRAWING_CACHE_VALID)
4978 | PFLAG_DRAWN | PFLAG_INVALIDATED;
4979 this.mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980
4981 if (child.hasFocus()) {
4982 requestChildFocus(child, child.findFocus());
4983 }
4984 }
4985
4986 /**
Chet Haaseca479d42012-08-30 17:20:08 -07004987 * Detaches a view from its parent. Detaching a view should be followed
4988 * either by a call to
4989 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
4990 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
4991 * temporary; reattachment or removal should happen within the same drawing cycle as
4992 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
4993 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 *
4995 * @param child the child to detach
4996 *
4997 * @see #detachViewFromParent(int)
4998 * @see #detachViewsFromParent(int, int)
4999 * @see #detachAllViewsFromParent()
5000 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5001 * @see #removeDetachedView(View, boolean)
5002 */
5003 protected void detachViewFromParent(View child) {
5004 removeFromArray(indexOfChild(child));
5005 }
5006
5007 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005008 * Detaches a view from its parent. Detaching a view should be followed
5009 * either by a call to
5010 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5011 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5012 * temporary; reattachment or removal should happen within the same drawing cycle as
5013 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5014 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 *
5016 * @param index the index of the child to detach
5017 *
5018 * @see #detachViewFromParent(View)
5019 * @see #detachAllViewsFromParent()
5020 * @see #detachViewsFromParent(int, int)
5021 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5022 * @see #removeDetachedView(View, boolean)
5023 */
5024 protected void detachViewFromParent(int index) {
5025 removeFromArray(index);
5026 }
5027
5028 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005029 * Detaches a range of views from their parents. Detaching a view should be followed
5030 * either by a call to
5031 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5032 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5033 * temporary; reattachment or removal should happen within the same drawing cycle as
5034 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5035 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 *
5037 * @param start the first index of the childrend range to detach
5038 * @param count the number of children to detach
5039 *
5040 * @see #detachViewFromParent(View)
5041 * @see #detachViewFromParent(int)
5042 * @see #detachAllViewsFromParent()
5043 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5044 * @see #removeDetachedView(View, boolean)
5045 */
5046 protected void detachViewsFromParent(int start, int count) {
5047 removeFromArray(start, count);
5048 }
5049
5050 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005051 * Detaches all views from the parent. Detaching a view should be followed
5052 * either by a call to
5053 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5054 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5055 * temporary; reattachment or removal should happen within the same drawing cycle as
5056 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5057 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 *
5059 * @see #detachViewFromParent(View)
5060 * @see #detachViewFromParent(int)
5061 * @see #detachViewsFromParent(int, int)
5062 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5063 * @see #removeDetachedView(View, boolean)
5064 */
5065 protected void detachAllViewsFromParent() {
5066 final int count = mChildrenCount;
5067 if (count <= 0) {
5068 return;
5069 }
5070
5071 final View[] children = mChildren;
5072 mChildrenCount = 0;
5073
5074 for (int i = count - 1; i >= 0; i--) {
5075 children[i].mParent = null;
5076 children[i] = null;
5077 }
5078 }
5079
5080 /**
5081 * Don't call or override this method. It is used for the implementation of
5082 * the view hierarchy.
5083 */
5084 public final void invalidateChild(View child, final Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 ViewParent parent = this;
5086
5087 final AttachInfo attachInfo = mAttachInfo;
5088 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 // If the child is drawing an animation, we want to copy this flag onto
5090 // ourselves and the parent to make sure the invalidate request goes
5091 // through
Dianne Hackborn4702a852012-08-17 15:18:29 -07005092 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION)
5093 == PFLAG_DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07005094
Romain Guyfe455af2012-02-15 16:40:20 -08005095 // Check whether the child that requests the invalidate is fully opaque
5096 // Views being animated or transformed are not considered opaque because we may
5097 // be invalidating their old position and need the parent to paint behind them.
5098 Matrix childMatrix = child.getMatrix();
5099 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
5100 child.getAnimation() == null && childMatrix.isIdentity();
5101 // Mark the child as dirty, using the appropriate flag
5102 // Make sure we do not set both flags at the same time
Dianne Hackborn4702a852012-08-17 15:18:29 -07005103 int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005104
John Reck96bb8ad2014-06-19 10:53:03 -07005105 if (child.mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005106 mPrivateFlags |= PFLAG_INVALIDATED;
5107 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guyfe455af2012-02-15 16:40:20 -08005108 }
5109
5110 final int[] location = attachInfo.mInvalidateChildLocation;
5111 location[CHILD_LEFT_INDEX] = child.mLeft;
5112 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase599913d2012-07-23 16:22:05 -07005113 if (!childMatrix.isIdentity() ||
5114 (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Romain Guyfe455af2012-02-15 16:40:20 -08005115 RectF boundingRect = attachInfo.mTmpTransformRect;
5116 boundingRect.set(dirty);
Chet Haase599913d2012-07-23 16:22:05 -07005117 Matrix transformMatrix;
5118 if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
5119 Transformation t = attachInfo.mTmpTransformation;
5120 boolean transformed = getChildStaticTransformation(child, t);
5121 if (transformed) {
5122 transformMatrix = attachInfo.mTmpMatrix;
5123 transformMatrix.set(t.getMatrix());
5124 if (!childMatrix.isIdentity()) {
5125 transformMatrix.preConcat(childMatrix);
5126 }
5127 } else {
5128 transformMatrix = childMatrix;
5129 }
5130 } else {
5131 transformMatrix = childMatrix;
5132 }
5133 transformMatrix.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005134 dirty.set((int) Math.floor(boundingRect.left),
5135 (int) Math.floor(boundingRect.top),
5136 (int) Math.ceil(boundingRect.right),
5137 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005138 }
5139
5140 do {
5141 View view = null;
5142 if (parent instanceof View) {
5143 view = (View) parent;
Romain Guyfe455af2012-02-15 16:40:20 -08005144 }
5145
5146 if (drawAnimation) {
5147 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005148 view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Romain Guyfe455af2012-02-15 16:40:20 -08005149 } else if (parent instanceof ViewRootImpl) {
5150 ((ViewRootImpl) parent).mIsAnimating = true;
5151 }
5152 }
5153
5154 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
5155 // flag coming from the child that initiated the invalidate
5156 if (view != null) {
5157 if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
5158 view.getSolidColor() == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005159 opaqueFlag = PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005160 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005161 if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) {
5162 view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag;
Romain Guyfe455af2012-02-15 16:40:20 -08005163 }
5164 }
5165
5166 parent = parent.invalidateChildInParent(location, dirty);
5167 if (view != null) {
5168 // Account for transform on current parent
5169 Matrix m = view.getMatrix();
5170 if (!m.isIdentity()) {
5171 RectF boundingRect = attachInfo.mTmpTransformRect;
5172 boundingRect.set(dirty);
5173 m.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005174 dirty.set((int) Math.floor(boundingRect.left),
5175 (int) Math.floor(boundingRect.top),
5176 (int) Math.ceil(boundingRect.right),
5177 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005178 }
5179 }
5180 } while (parent != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 }
5182 }
5183
5184 /**
5185 * Don't call or override this method. It is used for the implementation of
5186 * the view hierarchy.
5187 *
5188 * This implementation returns null if this ViewGroup does not have a parent,
5189 * if this ViewGroup is already fully invalidated or if the dirty rectangle
5190 * does not intersect with this ViewGroup's bounds.
5191 */
5192 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005193 if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
5194 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
5196 FLAG_OPTIMIZE_INVALIDATE) {
5197 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
5198 location[CHILD_TOP_INDEX] - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005199 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5200 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202
5203 final int left = mLeft;
5204 final int top = mTop;
5205
Chet Haase05e91ed2012-07-03 14:17:57 -07005206 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5207 if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) {
5208 dirty.setEmpty();
Romain Guy3a3133d2011-02-01 22:59:58 -08005209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005211 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haase05e91ed2012-07-03 14:17:57 -07005212
5213 location[CHILD_LEFT_INDEX] = left;
5214 location[CHILD_TOP_INDEX] = top;
5215
John Reck96bb8ad2014-06-19 10:53:03 -07005216 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005217 mPrivateFlags |= PFLAG_INVALIDATED;
Chet Haase05e91ed2012-07-03 14:17:57 -07005218 }
5219
5220 return mParent;
5221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005223 mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224
5225 location[CHILD_LEFT_INDEX] = mLeft;
5226 location[CHILD_TOP_INDEX] = mTop;
Chet Haasea3db8662011-07-19 10:36:05 -07005227 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5228 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
5229 } else {
5230 // in case the dirty rect extends outside the bounds of this container
5231 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5232 }
Romain Guy3a3133d2011-02-01 22:59:58 -08005233
John Reck96bb8ad2014-06-19 10:53:03 -07005234 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005235 mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy3a3133d2011-02-01 22:59:58 -08005236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237
5238 return mParent;
5239 }
5240 }
5241
5242 return null;
5243 }
5244
5245 /**
John Recke4267ea2014-06-03 15:53:15 -07005246 * Native-calculated damage path
5247 * Returns false if this path was unable to complete successfully. This means
5248 * it hit a ViewParent it doesn't recognize and needs to fall back to calculating
5249 * damage area
5250 * @hide
5251 */
5252 public boolean damageChildDeferred(View child) {
5253 ViewParent parent = getParent();
5254 while (parent != null) {
5255 if (parent instanceof ViewGroup) {
5256 parent = parent.getParent();
5257 } else if (parent instanceof ViewRootImpl) {
5258 ((ViewRootImpl) parent).invalidate();
5259 return true;
5260 } else {
5261 parent = null;
5262 }
5263 }
5264 return false;
5265 }
5266
5267 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07005268 * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the
5269 * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods
5270 * do; all we want to do here is schedule a traversal with the appropriate dirty rect.
5271 *
5272 * @hide
5273 */
Chris Craik49e6c732014-03-31 12:34:11 -07005274 public void damageChild(View child, final Rect dirty) {
John Recke4267ea2014-06-03 15:53:15 -07005275 if (damageChildDeferred(child)) {
5276 return;
5277 }
5278
Chet Haase9d1992d2012-03-13 11:03:25 -07005279 ViewParent parent = this;
5280
5281 final AttachInfo attachInfo = mAttachInfo;
5282 if (attachInfo != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005283 int left = child.mLeft;
5284 int top = child.mTop;
5285 if (!child.getMatrix().isIdentity()) {
5286 child.transformRect(dirty);
5287 }
5288
5289 do {
5290 if (parent instanceof ViewGroup) {
5291 ViewGroup parentVG = (ViewGroup) parent;
Chet Haaseb85967b2012-03-26 14:37:51 -07005292 if (parentVG.mLayerType != LAYER_TYPE_NONE) {
5293 // Layered parents should be recreated, not just re-issued
5294 parentVG.invalidate();
5295 parent = null;
5296 } else {
Chris Craik49e6c732014-03-31 12:34:11 -07005297 parent = parentVG.damageChildInParent(left, top, dirty);
Chet Haaseb85967b2012-03-26 14:37:51 -07005298 left = parentVG.mLeft;
5299 top = parentVG.mTop;
5300 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005301 } else {
5302 // Reached the top; this calls into the usual invalidate method in
5303 // ViewRootImpl, which schedules a traversal
5304 final int[] location = attachInfo.mInvalidateChildLocation;
5305 location[0] = left;
5306 location[1] = top;
5307 parent = parent.invalidateChildInParent(location, dirty);
5308 }
5309 } while (parent != null);
5310 }
5311 }
5312
5313 /**
5314 * Quick invalidation method that simply transforms the dirty rect into the parent's
5315 * coordinate system, pruning the invalidation if the parent has already been invalidated.
Chet Haasee4a2d7c2013-06-21 17:49:36 -07005316 *
5317 * @hide
Chet Haase9d1992d2012-03-13 11:03:25 -07005318 */
Chris Craik49e6c732014-03-31 12:34:11 -07005319 protected ViewParent damageChildInParent(int left, int top, final Rect dirty) {
5320 if ((mPrivateFlags & PFLAG_DRAWN) != 0
5321 || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005322 dirty.offset(left - mScrollX, top - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005323 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5324 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5325 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005326
5327 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 ||
5328 dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
5329
Chet Haase9d1992d2012-03-13 11:03:25 -07005330 if (!getMatrix().isIdentity()) {
5331 transformRect(dirty);
5332 }
5333
5334 return mParent;
5335 }
5336 }
5337
5338 return null;
5339 }
5340
5341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 * Offset a rectangle that is in a descendant's coordinate
5343 * space into our coordinate space.
5344 * @param descendant A descendant of this view
5345 * @param rect A rectangle defined in descendant's coordinate space.
5346 */
5347 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
5348 offsetRectBetweenParentAndChild(descendant, rect, true, false);
5349 }
5350
5351 /**
5352 * Offset a rectangle that is in our coordinate space into an ancestor's
5353 * coordinate space.
5354 * @param descendant A descendant of this view
5355 * @param rect A rectangle defined in descendant's coordinate space.
5356 */
5357 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
5358 offsetRectBetweenParentAndChild(descendant, rect, false, false);
5359 }
5360
5361 /**
5362 * Helper method that offsets a rect either from parent to descendant or
5363 * descendant to parent.
5364 */
5365 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
5366 boolean offsetFromChildToParent, boolean clipToBounds) {
5367
Adrian Roos83e725d2016-01-08 16:49:17 +01005368 final RectF rectF = mAttachInfo != null ? mAttachInfo.mTmpTransformRect1 : new RectF();
5369 final Matrix inverse = mAttachInfo != null ? mAttachInfo.mTmpMatrix : new Matrix();
5370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 // already in the same coord system :)
5372 if (descendant == this) {
5373 return;
5374 }
5375
5376 ViewParent theParent = descendant.mParent;
5377
5378 // search and offset up to the parent
5379 while ((theParent != null)
5380 && (theParent instanceof View)
5381 && (theParent != this)) {
5382
5383 if (offsetFromChildToParent) {
Adrian Roos83e725d2016-01-08 16:49:17 +01005384 rect.offset(-descendant.mScrollX, -descendant.mScrollY);
5385
5386 if (!descendant.hasIdentityMatrix()) {
5387 rectF.set(rect);
5388 descendant.getMatrix().mapRect(rectF);
5389 rectF.roundOut(rect);
5390 }
5391
5392 rect.offset(descendant.mLeft, descendant.mTop);
5393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 if (clipToBounds) {
5395 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005396 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5397 p.mBottom - p.mTop);
5398 if (!intersected) {
5399 rect.setEmpty();
5400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 }
5402 } else {
5403 if (clipToBounds) {
5404 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005405 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5406 p.mBottom - p.mTop);
5407 if (!intersected) {
5408 rect.setEmpty();
5409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 }
Adrian Roos83e725d2016-01-08 16:49:17 +01005411 rect.offset(-descendant.mLeft, -descendant.mTop);
5412
5413 if (!descendant.hasIdentityMatrix()) {
5414 descendant.getMatrix().invert(inverse);
5415 rectF.set(rect);
5416 inverse.mapRect(rectF);
5417 rectF.roundOut(rect);
5418 }
5419
5420 rect.offset(descendant.mScrollX, descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 }
5422
5423 descendant = (View) theParent;
5424 theParent = descendant.mParent;
5425 }
5426
5427 // now that we are up to this view, need to offset one more time
5428 // to get into our coordinate space
5429 if (theParent == this) {
5430 if (offsetFromChildToParent) {
Adrian Roos83e725d2016-01-08 16:49:17 +01005431 rect.offset(-descendant.mScrollX, -descendant.mScrollY);
5432
5433 if (!descendant.hasIdentityMatrix()) {
5434 rectF.set(rect);
5435 descendant.getMatrix().mapRect(rectF);
5436 rectF.roundOut(rect);
5437 }
5438
5439 rect.offset(descendant.mLeft, descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 } else {
Adrian Roos83e725d2016-01-08 16:49:17 +01005441 rect.offset(-descendant.mLeft, -descendant.mTop);
5442
5443 if (!descendant.hasIdentityMatrix()) {
5444 descendant.getMatrix().invert(inverse);
5445 rectF.set(rect);
5446 inverse.mapRect(rectF);
5447 rectF.roundOut(rect);
5448 }
5449
5450 rect.offset(descendant.mScrollX, descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 }
5452 } else {
5453 throw new IllegalArgumentException("parameter must be a descendant of this view");
5454 }
5455 }
5456
5457 /**
5458 * Offset the vertical location of all children of this view by the specified number of pixels.
5459 *
5460 * @param offset the number of pixels to offset
5461 *
5462 * @hide
5463 */
5464 public void offsetChildrenTopAndBottom(int offset) {
5465 final int count = mChildrenCount;
5466 final View[] children = mChildren;
Romain Guy5549cb52013-05-06 18:42:08 -07005467 boolean invalidate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468
5469 for (int i = 0; i < count; i++) {
5470 final View v = children[i];
5471 v.mTop += offset;
5472 v.mBottom += offset;
Chris Craik64a12e12014-03-28 18:12:12 -07005473 if (v.mRenderNode != null) {
Romain Guy5549cb52013-05-06 18:42:08 -07005474 invalidate = true;
Chris Craik64a12e12014-03-28 18:12:12 -07005475 v.mRenderNode.offsetTopAndBottom(offset);
Chet Haasea1cff502012-02-21 13:43:44 -08005476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 }
Romain Guy5549cb52013-05-06 18:42:08 -07005478
5479 if (invalidate) {
5480 invalidateViewProperty(false, false);
5481 }
Guang Zhu84e25092014-05-01 21:12:55 -07005482 notifySubtreeAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 }
5484
5485 /**
5486 * {@inheritDoc}
5487 */
5488 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005489 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
5490 // but for some simple tests it can be useful. If we don't have attach info this
5491 // will allocate memory.
5492 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01005493 rect.set(r);
5494
5495 if (!child.hasIdentityMatrix()) {
George Mount002d43d2014-11-11 12:54:43 -08005496 child.getMatrix().mapRect(rect);
Gilles Debunnecea45132011-11-24 02:19:27 +01005497 }
5498
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005499 final int dx = child.mLeft - mScrollX;
5500 final int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01005501
5502 rect.offset(dx, dy);
5503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01005505 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005506 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
5507 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01005508 position[0] = offset.x;
5509 position[1] = offset.y;
5510 child.getMatrix().mapPoints(position);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005511 offset.x = Math.round(position[0]);
5512 offset.y = Math.round(position[1]);
Gilles Debunnecea45132011-11-24 02:19:27 +01005513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 offset.x += dx;
5515 offset.y += dy;
5516 }
Gilles Debunnecea45132011-11-24 02:19:27 +01005517
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005518 final int width = mRight - mLeft;
5519 final int height = mBottom - mTop;
5520
George Mount002d43d2014-11-11 12:54:43 -08005521 boolean rectIsVisible = true;
Adam Powell35da41e2014-12-10 18:59:34 -08005522 if (mParent == null ||
5523 (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005524 // Clip to bounds.
5525 rectIsVisible = rect.intersect(0, 0, width, height);
Gilles Debunnecea45132011-11-24 02:19:27 +01005526 }
5527
George Mount002d43d2014-11-11 12:54:43 -08005528 if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005529 // Clip to padding.
George Mount002d43d2014-11-11 12:54:43 -08005530 rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop,
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005531 width - mPaddingRight, height - mPaddingBottom);
George Mount002d43d2014-11-11 12:54:43 -08005532 }
5533
5534 if (rectIsVisible && mClipBounds != null) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005535 // Clip to clipBounds.
George Mount002d43d2014-11-11 12:54:43 -08005536 rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right,
5537 mClipBounds.bottom);
5538 }
Alan Viverettec45b1d42015-11-16 15:38:59 -05005539 r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top),
5540 (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom));
George Mount002d43d2014-11-11 12:54:43 -08005541 if (rectIsVisible && mParent != null) {
5542 rectIsVisible = mParent.getChildVisibleRect(this, r, offset);
5543 }
5544 return rectIsVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 }
5546
5547 /**
5548 * {@inheritDoc}
5549 */
5550 @Override
Chet Haase9c087442011-01-12 16:20:16 -08005551 public final void layout(int l, int t, int r, int b) {
Chet Haase430742f2013-04-12 11:18:36 -07005552 if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
Chet Haase7dd4a532012-04-16 13:35:09 -07005553 if (mTransition != null) {
5554 mTransition.layoutChange(this);
5555 }
Chet Haase9c087442011-01-12 16:20:16 -08005556 super.layout(l, t, r, b);
5557 } else {
5558 // record the fact that we noop'd it; request layout when transition finishes
Chet Haaseb9895022013-04-02 15:10:58 -07005559 mLayoutCalledWhileSuppressed = true;
Chet Haase9c087442011-01-12 16:20:16 -08005560 }
5561 }
5562
5563 /**
5564 * {@inheritDoc}
5565 */
5566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 protected abstract void onLayout(boolean changed,
5568 int l, int t, int r, int b);
5569
5570 /**
5571 * Indicates whether the view group has the ability to animate its children
5572 * after the first layout.
5573 *
5574 * @return true if the children can be animated, false otherwise
5575 */
5576 protected boolean canAnimate() {
5577 return mLayoutAnimationController != null;
5578 }
5579
5580 /**
5581 * Runs the layout animation. Calling this method triggers a relayout of
5582 * this view group.
5583 */
5584 public void startLayoutAnimation() {
5585 if (mLayoutAnimationController != null) {
5586 mGroupFlags |= FLAG_RUN_ANIMATION;
5587 requestLayout();
5588 }
5589 }
5590
5591 /**
5592 * Schedules the layout animation to be played after the next layout pass
5593 * of this view group. This can be used to restart the layout animation
5594 * when the content of the view group changes or when the activity is
5595 * paused and resumed.
5596 */
5597 public void scheduleLayoutAnimation() {
5598 mGroupFlags |= FLAG_RUN_ANIMATION;
5599 }
5600
5601 /**
5602 * Sets the layout animation controller used to animate the group's
5603 * children after the first layout.
5604 *
5605 * @param controller the animation controller
5606 */
5607 public void setLayoutAnimation(LayoutAnimationController controller) {
5608 mLayoutAnimationController = controller;
5609 if (mLayoutAnimationController != null) {
5610 mGroupFlags |= FLAG_RUN_ANIMATION;
5611 }
5612 }
5613
5614 /**
5615 * Returns the layout animation controller used to animate the group's
5616 * children.
5617 *
5618 * @return the current animation controller
5619 */
5620 public LayoutAnimationController getLayoutAnimation() {
5621 return mLayoutAnimationController;
5622 }
5623
5624 /**
5625 * Indicates whether the children's drawing cache is used during a layout
5626 * animation. By default, the drawing cache is enabled but this will prevent
5627 * nested layout animations from working. To nest animations, you must disable
5628 * the cache.
5629 *
5630 * @return true if the animation cache is enabled, false otherwise
5631 *
5632 * @see #setAnimationCacheEnabled(boolean)
5633 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005634 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005635 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005636 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 public boolean isAnimationCacheEnabled() {
5639 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
5640 }
5641
5642 /**
5643 * Enables or disables the children's drawing cache during a layout animation.
5644 * By default, the drawing cache is enabled but this will prevent nested
5645 * layout animations from working. To nest animations, you must disable the
5646 * cache.
5647 *
5648 * @param enabled true to enable the animation cache, false otherwise
5649 *
5650 * @see #isAnimationCacheEnabled()
5651 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005652 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005653 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005654 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 */
5656 public void setAnimationCacheEnabled(boolean enabled) {
5657 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
5658 }
5659
5660 /**
5661 * Indicates whether this ViewGroup will always try to draw its children using their
5662 * drawing cache. By default this property is enabled.
5663 *
5664 * @return true if the animation cache is enabled, false otherwise
5665 *
5666 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5667 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5668 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005669 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005670 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005671 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 public boolean isAlwaysDrawnWithCacheEnabled() {
5674 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
5675 }
5676
5677 /**
5678 * Indicates whether this ViewGroup will always try to draw its children using their
5679 * drawing cache. This property can be set to true when the cache rendering is
5680 * slightly different from the children's normal rendering. Renderings can be different,
5681 * for instance, when the cache's quality is set to low.
5682 *
5683 * When this property is disabled, the ViewGroup will use the drawing cache of its
5684 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
5685 * when to start using the drawing cache and when to stop using it.
5686 *
5687 * @param always true to always draw with the drawing cache, false otherwise
5688 *
5689 * @see #isAlwaysDrawnWithCacheEnabled()
5690 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5691 * @see View#setDrawingCacheEnabled(boolean)
5692 * @see View#setDrawingCacheQuality(int)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005693 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005694 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005695 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 */
5697 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
5698 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
5699 }
5700
5701 /**
5702 * Indicates whether the ViewGroup is currently drawing its children using
5703 * their drawing cache.
5704 *
5705 * @return true if children should be drawn with their cache, false otherwise
5706 *
5707 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5708 * @see #setChildrenDrawnWithCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005709 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005710 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005711 * Child views may no longer be forced to cache their rendering state by their parents.
5712 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 protected boolean isChildrenDrawnWithCacheEnabled() {
5715 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
5716 }
5717
5718 /**
5719 * Tells the ViewGroup to draw its children using their drawing cache. This property
5720 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
5721 * will be used only if it has been enabled.
5722 *
5723 * Subclasses should call this method to start and stop using the drawing cache when
5724 * they perform performance sensitive operations, like scrolling or animating.
5725 *
5726 * @param enabled true if children should be drawn with their cache, false otherwise
5727 *
5728 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5729 * @see #isChildrenDrawnWithCacheEnabled()
Chris Craik5a6bbae2015-04-10 17:41:34 -07005730 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005731 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005732 * Child views may no longer be forced to cache their rendering state by their parents.
5733 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 */
5735 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
5736 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
5737 }
5738
Romain Guy293451e2009-11-04 13:59:48 -08005739 /**
5740 * Indicates whether the ViewGroup is drawing its children in the order defined by
5741 * {@link #getChildDrawingOrder(int, int)}.
5742 *
5743 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
5744 * false otherwise
5745 *
5746 * @see #setChildrenDrawingOrderEnabled(boolean)
5747 * @see #getChildDrawingOrder(int, int)
5748 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005749 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08005750 protected boolean isChildrenDrawingOrderEnabled() {
5751 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
5752 }
5753
5754 /**
5755 * Tells the ViewGroup whether to draw its children in the order defined by the method
5756 * {@link #getChildDrawingOrder(int, int)}.
Chris Craike83cbd42014-09-03 17:52:24 -07005757 * <p>
5758 * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)},
5759 * will override custom child ordering done via this method.
Romain Guy293451e2009-11-04 13:59:48 -08005760 *
5761 * @param enabled true if the order of the children when drawing is determined by
5762 * {@link #getChildDrawingOrder(int, int)}, false otherwise
5763 *
5764 * @see #isChildrenDrawingOrderEnabled()
5765 * @see #getChildDrawingOrder(int, int)
5766 */
5767 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
5768 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
5769 }
5770
Svetoslav6254f482013-06-04 17:22:14 -07005771 private boolean hasBooleanFlag(int flag) {
Philip Milnef091b662013-02-27 11:15:21 -08005772 return (mGroupFlags & flag) == flag;
5773 }
5774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 private void setBooleanFlag(int flag, boolean value) {
5776 if (value) {
5777 mGroupFlags |= flag;
5778 } else {
5779 mGroupFlags &= ~flag;
5780 }
5781 }
5782
5783 /**
5784 * Returns an integer indicating what types of drawing caches are kept in memory.
5785 *
5786 * @see #setPersistentDrawingCache(int)
5787 * @see #setAnimationCacheEnabled(boolean)
5788 *
5789 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
5790 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5791 * and {@link #PERSISTENT_ALL_CACHES}
5792 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005793 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07005795 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
5797 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
5798 })
5799 public int getPersistentDrawingCache() {
5800 return mPersistentDrawingCache;
5801 }
5802
5803 /**
5804 * Indicates what types of drawing caches should be kept in memory after
5805 * they have been created.
5806 *
5807 * @see #getPersistentDrawingCache()
5808 * @see #setAnimationCacheEnabled(boolean)
5809 *
5810 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
5811 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5812 * and {@link #PERSISTENT_ALL_CACHES}
5813 */
5814 public void setPersistentDrawingCache(int drawingCacheToKeep) {
5815 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
5816 }
5817
Philip Milnef091b662013-02-27 11:15:21 -08005818 private void setLayoutMode(int layoutMode, boolean explicitly) {
5819 mLayoutMode = layoutMode;
5820 setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly);
5821 }
5822
5823 /**
5824 * Recursively traverse the view hierarchy, resetting the layoutMode of any
5825 * descendants that had inherited a different layoutMode from a previous parent.
5826 * Recursion terminates when a descendant's mode is:
5827 * <ul>
5828 * <li>Undefined</li>
5829 * <li>The same as the root node's</li>
5830 * <li>A mode that had been explicitly set</li>
5831 * <ul/>
5832 * The first two clauses are optimizations.
5833 * @param layoutModeOfRoot
5834 */
5835 @Override
5836 void invalidateInheritedLayoutMode(int layoutModeOfRoot) {
5837 if (mLayoutMode == LAYOUT_MODE_UNDEFINED ||
5838 mLayoutMode == layoutModeOfRoot ||
Svetoslav6254f482013-06-04 17:22:14 -07005839 hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnef091b662013-02-27 11:15:21 -08005840 return;
5841 }
5842 setLayoutMode(LAYOUT_MODE_UNDEFINED, false);
5843
5844 // apply recursively
5845 for (int i = 0, N = getChildCount(); i < N; i++) {
5846 getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot);
5847 }
5848 }
5849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005851 * Returns the basis of alignment during layout operations on this ViewGroup:
Philip Milne7b757812012-09-19 18:13:44 -07005852 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milnecfb631b2012-10-26 10:51:46 -07005853 * <p>
5854 * If no layoutMode was explicitly set, either programmatically or in an XML resource,
5855 * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
5856 * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005857 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005858 * @return the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005859 *
5860 * @see #setLayoutMode(int)
5861 */
5862 public int getLayoutMode() {
Philip Milnecfb631b2012-10-26 10:51:46 -07005863 if (mLayoutMode == LAYOUT_MODE_UNDEFINED) {
Philip Milnef091b662013-02-27 11:15:21 -08005864 int inheritedLayoutMode = (mParent instanceof ViewGroup) ?
5865 ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT;
5866 setLayoutMode(inheritedLayoutMode, false);
Philip Milnecfb631b2012-10-26 10:51:46 -07005867 }
Philip Milne1557fd72012-04-04 23:41:34 -07005868 return mLayoutMode;
5869 }
5870
5871 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005872 * Sets the basis of alignment during the layout of this ViewGroup.
Philip Milne7b757812012-09-19 18:13:44 -07005873 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
5874 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005875 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005876 * @param layoutMode the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005877 *
5878 * @see #getLayoutMode()
Scott Main27a85082013-06-10 10:39:48 -07005879 * @attr ref android.R.styleable#ViewGroup_layoutMode
Philip Milne1557fd72012-04-04 23:41:34 -07005880 */
5881 public void setLayoutMode(int layoutMode) {
5882 if (mLayoutMode != layoutMode) {
Philip Milnef091b662013-02-27 11:15:21 -08005883 invalidateInheritedLayoutMode(layoutMode);
5884 setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED);
Philip Milne1557fd72012-04-04 23:41:34 -07005885 requestLayout();
5886 }
5887 }
5888
5889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 * Returns a new set of layout parameters based on the supplied attributes set.
5891 *
5892 * @param attrs the attributes to build the layout parameters from
5893 *
5894 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5895 * of its descendants
5896 */
5897 public LayoutParams generateLayoutParams(AttributeSet attrs) {
5898 return new LayoutParams(getContext(), attrs);
5899 }
5900
5901 /**
5902 * Returns a safe set of layout parameters based on the supplied layout params.
5903 * When a ViewGroup is passed a View whose layout params do not pass the test of
5904 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
5905 * is invoked. This method should return a new set of layout params suitable for
5906 * this ViewGroup, possibly by copying the appropriate attributes from the
5907 * specified set of layout params.
5908 *
5909 * @param p The layout parameters to convert into a suitable set of layout parameters
5910 * for this ViewGroup.
5911 *
5912 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5913 * of its descendants
5914 */
5915 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Chet Haase4610eef2015-12-03 07:38:11 -08005916 return p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 }
5918
5919 /**
5920 * Returns a set of default layout parameters. These parameters are requested
5921 * when the View passed to {@link #addView(View)} has no layout parameters
5922 * already set. If null is returned, an exception is thrown from addView.
5923 *
5924 * @return a set of default layout parameters or null
5925 */
5926 protected LayoutParams generateDefaultLayoutParams() {
5927 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
5928 }
5929
5930 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 * {@inheritDoc}
5932 */
5933 @Override
5934 protected void debug(int depth) {
5935 super.debug(depth);
5936 String output;
5937
5938 if (mFocused != null) {
5939 output = debugIndent(depth);
5940 output += "mFocused";
5941 Log.d(VIEW_LOG_TAG, output);
5942 }
5943 if (mChildrenCount != 0) {
5944 output = debugIndent(depth);
5945 output += "{";
5946 Log.d(VIEW_LOG_TAG, output);
5947 }
5948 int count = mChildrenCount;
5949 for (int i = 0; i < count; i++) {
5950 View child = mChildren[i];
5951 child.debug(depth + 1);
5952 }
5953
5954 if (mChildrenCount != 0) {
5955 output = debugIndent(depth);
5956 output += "}";
5957 Log.d(VIEW_LOG_TAG, output);
5958 }
5959 }
5960
5961 /**
5962 * Returns the position in the group of the specified child view.
5963 *
5964 * @param child the view for which to get the position
5965 * @return a positive integer representing the position of the view in the
5966 * group, or -1 if the view does not exist in the group
5967 */
5968 public int indexOfChild(View child) {
5969 final int count = mChildrenCount;
5970 final View[] children = mChildren;
5971 for (int i = 0; i < count; i++) {
5972 if (children[i] == child) {
5973 return i;
5974 }
5975 }
5976 return -1;
5977 }
5978
5979 /**
5980 * Returns the number of children in the group.
5981 *
5982 * @return a positive integer representing the number of children in
5983 * the group
5984 */
5985 public int getChildCount() {
5986 return mChildrenCount;
5987 }
5988
5989 /**
5990 * Returns the view at the specified position in the group.
5991 *
5992 * @param index the position at which to get the view from
5993 * @return the view at the specified position or null if the position
5994 * does not exist within the group
5995 */
5996 public View getChildAt(int index) {
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005997 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 return null;
5999 }
Adam Powell3ba8f5d62011-03-07 15:36:33 -08006000 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 }
6002
6003 /**
6004 * Ask all of the children of this view to measure themselves, taking into
6005 * account both the MeasureSpec requirements for this view and its padding.
6006 * We skip children that are in the GONE state The heavy lifting is done in
6007 * getChildMeasureSpec.
6008 *
6009 * @param widthMeasureSpec The width requirements for this view
6010 * @param heightMeasureSpec The height requirements for this view
6011 */
6012 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
6013 final int size = mChildrenCount;
6014 final View[] children = mChildren;
6015 for (int i = 0; i < size; ++i) {
6016 final View child = children[i];
6017 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
6018 measureChild(child, widthMeasureSpec, heightMeasureSpec);
6019 }
6020 }
6021 }
6022
6023 /**
6024 * Ask one of the children of this view to measure itself, taking into
6025 * account both the MeasureSpec requirements for this view and its padding.
6026 * The heavy lifting is done in getChildMeasureSpec.
6027 *
6028 * @param child The child to measure
6029 * @param parentWidthMeasureSpec The width requirements for this view
6030 * @param parentHeightMeasureSpec The height requirements for this view
6031 */
6032 protected void measureChild(View child, int parentWidthMeasureSpec,
6033 int parentHeightMeasureSpec) {
6034 final LayoutParams lp = child.getLayoutParams();
6035
6036 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6037 mPaddingLeft + mPaddingRight, lp.width);
6038 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6039 mPaddingTop + mPaddingBottom, lp.height);
6040
6041 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6042 }
6043
6044 /**
6045 * Ask one of the children of this view to measure itself, taking into
6046 * account both the MeasureSpec requirements for this view and its padding
6047 * and margins. The child must have MarginLayoutParams The heavy lifting is
6048 * done in getChildMeasureSpec.
6049 *
6050 * @param child The child to measure
6051 * @param parentWidthMeasureSpec The width requirements for this view
6052 * @param widthUsed Extra space that has been used up by the parent
6053 * horizontally (possibly by other children of the parent)
6054 * @param parentHeightMeasureSpec The height requirements for this view
6055 * @param heightUsed Extra space that has been used up by the parent
6056 * vertically (possibly by other children of the parent)
6057 */
6058 protected void measureChildWithMargins(View child,
6059 int parentWidthMeasureSpec, int widthUsed,
6060 int parentHeightMeasureSpec, int heightUsed) {
6061 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
6062
6063 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6064 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
6065 + widthUsed, lp.width);
6066 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6067 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
6068 + heightUsed, lp.height);
6069
6070 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6071 }
6072
6073 /**
6074 * Does the hard part of measureChildren: figuring out the MeasureSpec to
6075 * pass to a particular child. This method figures out the right MeasureSpec
6076 * for one dimension (height or width) of one child view.
6077 *
6078 * The goal is to combine information from our MeasureSpec with the
6079 * LayoutParams of the child to get the best possible results. For example,
6080 * if the this view knows its size (because its MeasureSpec has a mode of
6081 * EXACTLY), and the child has indicated in its LayoutParams that it wants
6082 * to be the same size as the parent, the parent should ask the child to
6083 * layout given an exact size.
6084 *
6085 * @param spec The requirements for this view
6086 * @param padding The padding of this view for the current dimension and
6087 * margins, if applicable
6088 * @param childDimension How big the child wants to be in the current
6089 * dimension
6090 * @return a MeasureSpec integer for the child
6091 */
6092 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
6093 int specMode = MeasureSpec.getMode(spec);
6094 int specSize = MeasureSpec.getSize(spec);
6095
6096 int size = Math.max(0, specSize - padding);
6097
6098 int resultSize = 0;
6099 int resultMode = 0;
6100
6101 switch (specMode) {
6102 // Parent has imposed an exact size on us
6103 case MeasureSpec.EXACTLY:
6104 if (childDimension >= 0) {
6105 resultSize = childDimension;
6106 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006107 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 // Child wants to be our size. So be it.
6109 resultSize = size;
6110 resultMode = MeasureSpec.EXACTLY;
6111 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6112 // Child wants to determine its own size. It can't be
6113 // bigger than us.
6114 resultSize = size;
6115 resultMode = MeasureSpec.AT_MOST;
6116 }
6117 break;
6118
6119 // Parent has imposed a maximum size on us
6120 case MeasureSpec.AT_MOST:
6121 if (childDimension >= 0) {
6122 // Child wants a specific size... so be it
6123 resultSize = childDimension;
6124 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006125 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 // Child wants to be our size, but our size is not fixed.
6127 // Constrain child to not be bigger than us.
6128 resultSize = size;
6129 resultMode = MeasureSpec.AT_MOST;
6130 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6131 // Child wants to determine its own size. It can't be
6132 // bigger than us.
6133 resultSize = size;
6134 resultMode = MeasureSpec.AT_MOST;
6135 }
6136 break;
6137
6138 // Parent asked to see how big we want to be
6139 case MeasureSpec.UNSPECIFIED:
6140 if (childDimension >= 0) {
6141 // Child wants a specific size... let him have it
6142 resultSize = childDimension;
6143 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006144 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 // Child wants to be our size... find out how big it should
6146 // be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006147 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 resultMode = MeasureSpec.UNSPECIFIED;
6149 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6150 // Child wants to determine its own size.... find out how
6151 // big it should be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006152 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 resultMode = MeasureSpec.UNSPECIFIED;
6154 }
6155 break;
6156 }
6157 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
6158 }
6159
6160
6161 /**
6162 * Removes any pending animations for views that have been removed. Call
6163 * this if you don't want animations for exiting views to stack up.
6164 */
6165 public void clearDisappearingChildren() {
John Reckca7a9da2014-03-05 16:29:07 -08006166 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6167 if (disappearingChildren != null) {
6168 final int count = disappearingChildren.size();
6169 for (int i = 0; i < count; i++) {
6170 final View view = disappearingChildren.get(i);
6171 if (view.mAttachInfo != null) {
6172 view.dispatchDetachedFromWindow();
6173 }
6174 view.clearAnimation();
6175 }
6176 disappearingChildren.clear();
Chet Haaseb85967b2012-03-26 14:37:51 -07006177 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 }
6179 }
6180
6181 /**
6182 * Add a view which is removed from mChildren but still needs animation
6183 *
6184 * @param v View to add
6185 */
6186 private void addDisappearingView(View v) {
6187 ArrayList<View> disappearingChildren = mDisappearingChildren;
6188
6189 if (disappearingChildren == null) {
6190 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
6191 }
6192
6193 disappearingChildren.add(v);
6194 }
6195
6196 /**
6197 * Cleanup a view when its animation is done. This may mean removing it from
6198 * the list of disappearing views.
6199 *
6200 * @param view The view whose animation has finished
6201 * @param animation The animation, cannot be null
6202 */
Chet Haase64a48c12012-02-13 16:33:29 -08006203 void finishAnimatingView(final View view, Animation animation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6205 if (disappearingChildren != null) {
6206 if (disappearingChildren.contains(view)) {
6207 disappearingChildren.remove(view);
6208
6209 if (view.mAttachInfo != null) {
6210 view.dispatchDetachedFromWindow();
6211 }
6212
6213 view.clearAnimation();
6214 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6215 }
6216 }
6217
6218 if (animation != null && !animation.getFillAfter()) {
6219 view.clearAnimation();
6220 }
6221
Dianne Hackborn4702a852012-08-17 15:18:29 -07006222 if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 view.onAnimationEnd();
6224 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
6225 // so we'd rather be safe than sorry
Dianne Hackborn4702a852012-08-17 15:18:29 -07006226 view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 // Draw one more frame after the animation is done
6228 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6229 }
6230 }
6231
Chet Haaseb20db3e2010-09-10 13:07:30 -07006232 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07006233 * Utility function called by View during invalidation to determine whether a view that
6234 * is invisible or gone should still be invalidated because it is being transitioned (and
6235 * therefore still needs to be drawn).
6236 */
6237 boolean isViewTransitioning(View view) {
6238 return (mTransitioningViews != null && mTransitioningViews.contains(view));
6239 }
6240
6241 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07006242 * This method tells the ViewGroup that the given View object, which should have this
6243 * ViewGroup as its parent,
6244 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
6245 * is removed from its parent. This allows animations, such as those used by
6246 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
6247 * the removal of views. A call to this method should always be accompanied by a later call
6248 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
6249 * so that the View finally gets removed.
6250 *
6251 * @param view The View object to be kept visible even if it gets removed from its parent.
6252 */
6253 public void startViewTransition(View view) {
6254 if (view.mParent == this) {
6255 if (mTransitioningViews == null) {
6256 mTransitioningViews = new ArrayList<View>();
6257 }
6258 mTransitioningViews.add(view);
6259 }
6260 }
6261
6262 /**
6263 * This method should always be called following an earlier call to
6264 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
6265 * and will no longer be displayed. Note that this method does not perform the functionality
6266 * of removing a view from its parent; it just discontinues the display of a View that
6267 * has previously been removed.
6268 *
6269 * @return view The View object that has been removed but is being kept around in the visible
6270 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
6271 */
6272 public void endViewTransition(View view) {
6273 if (mTransitioningViews != null) {
6274 mTransitioningViews.remove(view);
6275 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6276 if (disappearingChildren != null && disappearingChildren.contains(view)) {
6277 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006278 if (mVisibilityChangingChildren != null &&
6279 mVisibilityChangingChildren.contains(view)) {
6280 mVisibilityChangingChildren.remove(view);
6281 } else {
6282 if (view.mAttachInfo != null) {
6283 view.dispatchDetachedFromWindow();
6284 }
6285 if (view.mParent != null) {
6286 view.mParent = null;
6287 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07006288 }
Chet Haaseb85967b2012-03-26 14:37:51 -07006289 invalidate();
Chet Haaseb20db3e2010-09-10 13:07:30 -07006290 }
6291 }
6292 }
6293
Chet Haase21cd1382010-09-01 17:42:29 -07006294 private LayoutTransition.TransitionListener mLayoutTransitionListener =
6295 new LayoutTransition.TransitionListener() {
6296 @Override
6297 public void startTransition(LayoutTransition transition, ViewGroup container,
6298 View view, int transitionType) {
6299 // We only care about disappearing items, since we need special logic to keep
6300 // those items visible after they've been 'removed'
6301 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006302 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006303 }
6304 }
6305
6306 @Override
6307 public void endTransition(LayoutTransition transition, ViewGroup container,
6308 View view, int transitionType) {
Chet Haaseb9895022013-04-02 15:10:58 -07006309 if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) {
Chet Haase9c087442011-01-12 16:20:16 -08006310 requestLayout();
Chet Haaseb9895022013-04-02 15:10:58 -07006311 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08006312 }
Chet Haase21cd1382010-09-01 17:42:29 -07006313 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006314 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006315 }
6316 }
6317 };
6318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 /**
Chet Haaseb9895022013-04-02 15:10:58 -07006320 * Tells this ViewGroup to suppress all layout() calls until layout
6321 * suppression is disabled with a later call to suppressLayout(false).
6322 * When layout suppression is disabled, a requestLayout() call is sent
6323 * if layout() was attempted while layout was being suppressed.
6324 *
6325 * @hide
6326 */
6327 public void suppressLayout(boolean suppress) {
6328 mSuppressLayout = suppress;
6329 if (!suppress) {
6330 if (mLayoutCalledWhileSuppressed) {
6331 requestLayout();
6332 mLayoutCalledWhileSuppressed = false;
6333 }
6334 }
6335 }
6336
6337 /**
Chet Haase199acdf2013-07-24 18:40:55 -07006338 * Returns whether layout calls on this container are currently being
6339 * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}.
6340 *
6341 * @return true if layout calls are currently suppressed, false otherwise.
6342 *
6343 * @hide
6344 */
6345 public boolean isLayoutSuppressed() {
6346 return mSuppressLayout;
6347 }
6348
6349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 * {@inheritDoc}
6351 */
6352 @Override
6353 public boolean gatherTransparentRegion(Region region) {
6354 // If no transparent regions requested, we are always opaque.
Dianne Hackborn4702a852012-08-17 15:18:29 -07006355 final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 if (meOpaque && region == null) {
6357 // The caller doesn't care about the region, so stop now.
6358 return true;
6359 }
6360 super.gatherTransparentRegion(region);
6361 final View[] children = mChildren;
6362 final int count = mChildrenCount;
6363 boolean noneOfTheChildrenAreTransparent = true;
6364 for (int i = 0; i < count; i++) {
6365 final View child = children[i];
Mathias Agopiane3381152010-12-02 15:19:36 -08006366 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 if (!child.gatherTransparentRegion(region)) {
6368 noneOfTheChildrenAreTransparent = false;
6369 }
6370 }
6371 }
6372 return meOpaque || noneOfTheChildrenAreTransparent;
6373 }
6374
6375 /**
6376 * {@inheritDoc}
6377 */
6378 public void requestTransparentRegion(View child) {
6379 if (child != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006380 child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 if (mParent != null) {
6382 mParent.requestTransparentRegion(this);
6383 }
6384 }
6385 }
Romain Guy8506ab42009-06-11 17:35:47 -07006386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 @Override
Adam Powell46e38fd2014-02-03 10:16:49 -08006388 public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
6389 insets = super.dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006390 if (!insets.isConsumed()) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006391 final int count = getChildCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 for (int i = 0; i < count; i++) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006393 insets = getChildAt(i).dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006394 if (insets.isConsumed()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 break;
6396 }
6397 }
6398 }
Adam Powell46e38fd2014-02-03 10:16:49 -08006399 return insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 }
6401
6402 /**
6403 * Returns the animation listener to which layout animation events are
6404 * sent.
6405 *
6406 * @return an {@link android.view.animation.Animation.AnimationListener}
6407 */
6408 public Animation.AnimationListener getLayoutAnimationListener() {
6409 return mAnimationListener;
6410 }
6411
6412 @Override
6413 protected void drawableStateChanged() {
6414 super.drawableStateChanged();
6415
6416 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
6417 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6418 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
6419 + " child has duplicateParentState set to true");
6420 }
6421
6422 final View[] children = mChildren;
6423 final int count = mChildrenCount;
6424
6425 for (int i = 0; i < count; i++) {
6426 final View child = children[i];
6427 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
6428 child.refreshDrawableState();
6429 }
6430 }
6431 }
6432 }
6433
6434 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07006435 public void jumpDrawablesToCurrentState() {
6436 super.jumpDrawablesToCurrentState();
6437 final View[] children = mChildren;
6438 final int count = mChildrenCount;
6439 for (int i = 0; i < count; i++) {
6440 children[i].jumpDrawablesToCurrentState();
6441 }
6442 }
6443
6444 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 protected int[] onCreateDrawableState(int extraSpace) {
6446 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
6447 return super.onCreateDrawableState(extraSpace);
6448 }
6449
6450 int need = 0;
6451 int n = getChildCount();
6452 for (int i = 0; i < n; i++) {
6453 int[] childState = getChildAt(i).getDrawableState();
6454
6455 if (childState != null) {
6456 need += childState.length;
6457 }
6458 }
6459
6460 int[] state = super.onCreateDrawableState(extraSpace + need);
6461
6462 for (int i = 0; i < n; i++) {
6463 int[] childState = getChildAt(i).getDrawableState();
6464
6465 if (childState != null) {
6466 state = mergeDrawableStates(state, childState);
6467 }
6468 }
6469
6470 return state;
6471 }
6472
6473 /**
6474 * Sets whether this ViewGroup's drawable states also include
6475 * its children's drawable states. This is used, for example, to
6476 * make a group appear to be focused when its child EditText or button
6477 * is focused.
6478 */
6479 public void setAddStatesFromChildren(boolean addsStates) {
6480 if (addsStates) {
6481 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
6482 } else {
6483 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
6484 }
6485
6486 refreshDrawableState();
6487 }
6488
6489 /**
6490 * Returns whether this ViewGroup's drawable states also include
6491 * its children's drawable states. This is used, for example, to
6492 * make a group appear to be focused when its child EditText or button
6493 * is focused.
6494 */
6495 public boolean addStatesFromChildren() {
6496 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
6497 }
6498
6499 /**
Jeff Smitha45746e2012-07-19 14:19:24 -05006500 * If {@link #addStatesFromChildren} is true, refreshes this group's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 * drawable state (to include the states from its children).
6502 */
6503 public void childDrawableStateChanged(View child) {
6504 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6505 refreshDrawableState();
6506 }
6507 }
6508
6509 /**
6510 * Specifies the animation listener to which layout animation events must
6511 * be sent. Only
6512 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
6513 * and
6514 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
6515 * are invoked.
6516 *
6517 * @param animationListener the layout animation listener
6518 */
6519 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
6520 mAnimationListener = animationListener;
6521 }
6522
6523 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006524 * This method is called by LayoutTransition when there are 'changing' animations that need
6525 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
6526 * starts all pending transitions prior to the drawing phase in the current traversal.
6527 *
6528 * @param transition The LayoutTransition to be started on the next traversal.
6529 *
6530 * @hide
6531 */
6532 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006533 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07006534 if (viewAncestor != null) {
6535 viewAncestor.requestTransitionStart(transition);
6536 }
Chet Haasecca2c982011-05-20 14:34:18 -07006537 }
6538
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006539 /**
6540 * @hide
6541 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006542 @Override
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006543 public boolean resolveRtlPropertiesIfNeeded() {
6544 final boolean result = super.resolveRtlPropertiesIfNeeded();
6545 // We dont need to resolve the children RTL properties if nothing has changed for the parent
6546 if (result) {
6547 int count = getChildCount();
6548 for (int i = 0; i < count; i++) {
6549 final View child = getChildAt(i);
6550 if (child.isLayoutDirectionInherited()) {
6551 child.resolveRtlPropertiesIfNeeded();
6552 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006553 }
6554 }
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006555 return result;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006556 }
6557
6558 /**
6559 * @hide
6560 */
6561 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006562 public boolean resolveLayoutDirection() {
6563 final boolean result = super.resolveLayoutDirection();
6564 if (result) {
6565 int count = getChildCount();
6566 for (int i = 0; i < count; i++) {
6567 final View child = getChildAt(i);
6568 if (child.isLayoutDirectionInherited()) {
6569 child.resolveLayoutDirection();
6570 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006571 }
6572 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006573 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006574 }
6575
6576 /**
6577 * @hide
6578 */
6579 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006580 public boolean resolveTextDirection() {
6581 final boolean result = super.resolveTextDirection();
6582 if (result) {
6583 int count = getChildCount();
6584 for (int i = 0; i < count; i++) {
6585 final View child = getChildAt(i);
6586 if (child.isTextDirectionInherited()) {
6587 child.resolveTextDirection();
6588 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006589 }
6590 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006591 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006592 }
6593
6594 /**
6595 * @hide
6596 */
6597 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006598 public boolean resolveTextAlignment() {
6599 final boolean result = super.resolveTextAlignment();
6600 if (result) {
6601 int count = getChildCount();
6602 for (int i = 0; i < count; i++) {
6603 final View child = getChildAt(i);
6604 if (child.isTextAlignmentInherited()) {
6605 child.resolveTextAlignment();
6606 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006607 }
6608 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006609 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006610 }
6611
6612 /**
6613 * @hide
6614 */
6615 @Override
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006616 public void resolvePadding() {
6617 super.resolvePadding();
6618 int count = getChildCount();
6619 for (int i = 0; i < count; i++) {
6620 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006621 if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006622 child.resolvePadding();
6623 }
6624 }
6625 }
6626
6627 /**
6628 * @hide
6629 */
6630 @Override
6631 protected void resolveDrawables() {
6632 super.resolveDrawables();
6633 int count = getChildCount();
6634 for (int i = 0; i < count; i++) {
6635 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006636 if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006637 child.resolveDrawables();
6638 }
6639 }
6640 }
6641
Fabrice Di Meglio1e0ed6b2012-10-18 16:06:52 -07006642 /**
6643 * @hide
6644 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -07006645 @Override
6646 public void resolveLayoutParams() {
6647 super.resolveLayoutParams();
6648 int count = getChildCount();
6649 for (int i = 0; i < count; i++) {
6650 final View child = getChildAt(i);
6651 child.resolveLayoutParams();
6652 }
6653 }
6654
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006655 /**
6656 * @hide
6657 */
6658 @Override
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006659 public void resetResolvedLayoutDirection() {
6660 super.resetResolvedLayoutDirection();
6661
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006662 int count = getChildCount();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006663 for (int i = 0; i < count; i++) {
6664 final View child = getChildAt(i);
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006665 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07006666 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006667 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006668 }
6669 }
6670
6671 /**
6672 * @hide
6673 */
6674 @Override
6675 public void resetResolvedTextDirection() {
6676 super.resetResolvedTextDirection();
6677
6678 int count = getChildCount();
6679 for (int i = 0; i < count; i++) {
6680 final View child = getChildAt(i);
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -07006681 if (child.isTextDirectionInherited()) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006682 child.resetResolvedTextDirection();
6683 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006684 }
6685 }
6686
6687 /**
6688 * @hide
6689 */
6690 @Override
6691 public void resetResolvedTextAlignment() {
6692 super.resetResolvedTextAlignment();
6693
6694 int count = getChildCount();
6695 for (int i = 0; i < count; i++) {
6696 final View child = getChildAt(i);
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -07006697 if (child.isTextAlignmentInherited()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07006698 child.resetResolvedTextAlignment();
6699 }
6700 }
6701 }
6702
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006703 /**
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006704 * @hide
6705 */
6706 @Override
6707 public void resetResolvedPadding() {
6708 super.resetResolvedPadding();
6709
6710 int count = getChildCount();
6711 for (int i = 0; i < count; i++) {
6712 final View child = getChildAt(i);
6713 if (child.isLayoutDirectionInherited()) {
6714 child.resetResolvedPadding();
6715 }
6716 }
6717 }
6718
6719 /**
6720 * @hide
6721 */
6722 @Override
6723 protected void resetResolvedDrawables() {
6724 super.resetResolvedDrawables();
6725
6726 int count = getChildCount();
6727 for (int i = 0; i < count; i++) {
6728 final View child = getChildAt(i);
6729 if (child.isLayoutDirectionInherited()) {
6730 child.resetResolvedDrawables();
6731 }
6732 }
6733 }
6734
6735 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006736 * Return true if the pressed state should be delayed for children or descendants of this
6737 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
6738 * This prevents the pressed state from appearing when the user is actually trying to scroll
6739 * the content.
6740 *
6741 * The default implementation returns true for compatibility reasons. Subclasses that do
6742 * not scroll should generally override this method and return false.
6743 */
6744 public boolean shouldDelayChildPressedState() {
6745 return true;
6746 }
6747
Adam Powell10ba2772014-04-15 09:46:51 -07006748 /**
6749 * @inheritDoc
6750 */
6751 @Override
6752 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6753 return false;
6754 }
6755
6756 /**
6757 * @inheritDoc
6758 */
6759 @Override
6760 public void onNestedScrollAccepted(View child, View target, int axes) {
6761 mNestedScrollAxes = axes;
6762 }
6763
6764 /**
6765 * @inheritDoc
6766 *
6767 * <p>The default implementation of onStopNestedScroll calls
6768 * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p>
6769 */
6770 @Override
6771 public void onStopNestedScroll(View child) {
6772 // Stop any recursive nested scrolling.
6773 stopNestedScroll();
Adam Powelld4a22d42015-04-16 15:44:10 -07006774 mNestedScrollAxes = 0;
Adam Powell10ba2772014-04-15 09:46:51 -07006775 }
6776
6777 /**
6778 * @inheritDoc
6779 */
6780 @Override
6781 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6782 int dxUnconsumed, int dyUnconsumed) {
6783 // Do nothing
6784 }
6785
6786 /**
6787 * @inheritDoc
6788 */
6789 @Override
6790 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
6791 // Do nothing
6792 }
6793
6794 /**
6795 * @inheritDoc
6796 */
6797 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07006798 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Adam Powell10ba2772014-04-15 09:46:51 -07006799 return false;
6800 }
6801
6802 /**
Adam Powellb72be592014-07-16 21:41:31 -07006803 * @inheritDoc
6804 */
6805 @Override
6806 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
6807 return false;
6808 }
6809
6810 /**
Adam Powell10ba2772014-04-15 09:46:51 -07006811 * Return the current axes of nested scrolling for this ViewGroup.
6812 *
6813 * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently
6814 * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p>
6815 *
6816 * @return Flags indicating the current axes of nested scrolling
6817 * @see #SCROLL_AXIS_HORIZONTAL
6818 * @see #SCROLL_AXIS_VERTICAL
6819 * @see #SCROLL_AXIS_NONE
6820 */
6821 public int getNestedScrollAxes() {
6822 return mNestedScrollAxes;
6823 }
6824
Philip Milned7dd8902012-01-26 16:55:30 -08006825 /** @hide */
6826 protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
6827 }
6828
George Mounte1803372014-02-26 19:00:52 +00006829 /** @hide */
6830 @Override
6831 public void captureTransitioningViews(List<View> transitioningViews) {
6832 if (getVisibility() != View.VISIBLE) {
6833 return;
6834 }
6835 if (isTransitionGroup()) {
6836 transitioningViews.add(this);
6837 } else {
6838 int count = getChildCount();
6839 for (int i = 0; i < count; i++) {
6840 View child = getChildAt(i);
6841 child.captureTransitioningViews(transitioningViews);
6842 }
6843 }
6844 }
6845
6846 /** @hide */
6847 @Override
George Mountabb352a2014-05-09 10:27:20 -07006848 public void findNamedViews(Map<String, View> namedElements) {
George Mountfe361d22014-07-08 17:25:25 -07006849 if (getVisibility() != VISIBLE && mGhostView == null) {
George Mounte1803372014-02-26 19:00:52 +00006850 return;
6851 }
George Mountabb352a2014-05-09 10:27:20 -07006852 super.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006853 int count = getChildCount();
6854 for (int i = 0; i < count; i++) {
6855 View child = getChildAt(i);
George Mountabb352a2014-05-09 10:27:20 -07006856 child.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006857 }
6858 }
6859
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006860 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 * LayoutParams are used by views to tell their parents how they want to be
6862 * laid out. See
6863 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
6864 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07006865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 * <p>
6867 * The base LayoutParams class just describes how big the view wants to be
6868 * for both width and height. For each dimension, it can specify one of:
6869 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006870 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
6871 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
6873 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006874 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 * </ul>
6876 * There are subclasses of LayoutParams for different subclasses of
6877 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07006878 * LayoutParams which adds an X and Y value.</p>
6879 *
6880 * <div class="special reference">
6881 * <h3>Developer Guides</h3>
6882 * <p>For more information about creating user interface layouts, read the
6883 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
6884 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885 *
6886 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
6887 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
6888 */
6889 public static class LayoutParams {
6890 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006891 * Special value for the height or width requested by a View.
6892 * FILL_PARENT means that the view wants to be as big as its parent,
6893 * minus the parent's padding, if any. This value is deprecated
6894 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 */
Romain Guy980a9382010-01-08 15:06:28 -08006896 @SuppressWarnings({"UnusedDeclaration"})
6897 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 public static final int FILL_PARENT = -1;
6899
6900 /**
6901 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08006902 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006903 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08006904 */
6905 public static final int MATCH_PARENT = -1;
6906
6907 /**
6908 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 * WRAP_CONTENT means that the view wants to be just large enough to fit
6910 * its own internal content, taking its own padding into account.
6911 */
6912 public static final int WRAP_CONTENT = -2;
6913
6914 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006915 * Information about how wide the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006916 * constants FILL_PARENT (replaced by MATCH_PARENT
6917 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006919 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006920 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6922 })
6923 public int width;
6924
6925 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006926 * Information about how tall the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006927 * constants FILL_PARENT (replaced by MATCH_PARENT
6928 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006930 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006931 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6933 })
6934 public int height;
6935
6936 /**
6937 * Used to animate layouts.
6938 */
6939 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
6940
6941 /**
6942 * Creates a new set of layout parameters. The values are extracted from
6943 * the supplied attributes set and context. The XML attributes mapped
6944 * to this set of layout parameters are:
6945 *
6946 * <ul>
6947 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006948 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6949 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006951 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6952 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 * </ul>
6954 *
6955 * @param c the application environment
6956 * @param attrs the set of attributes from which to extract the layout
6957 * parameters' values
6958 */
6959 public LayoutParams(Context c, AttributeSet attrs) {
6960 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
6961 setBaseAttributes(a,
6962 R.styleable.ViewGroup_Layout_layout_width,
6963 R.styleable.ViewGroup_Layout_layout_height);
6964 a.recycle();
6965 }
6966
6967 /**
6968 * Creates a new set of layout parameters with the specified width
6969 * and height.
6970 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006971 * @param width the width, either {@link #WRAP_CONTENT},
6972 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6973 * API Level 8), or a fixed size in pixels
6974 * @param height the height, either {@link #WRAP_CONTENT},
6975 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6976 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 */
6978 public LayoutParams(int width, int height) {
6979 this.width = width;
6980 this.height = height;
6981 }
6982
6983 /**
6984 * Copy constructor. Clones the width and height values of the source.
6985 *
6986 * @param source The layout params to copy from.
6987 */
6988 public LayoutParams(LayoutParams source) {
6989 this.width = source.width;
6990 this.height = source.height;
6991 }
6992
6993 /**
6994 * Used internally by MarginLayoutParams.
6995 * @hide
6996 */
6997 LayoutParams() {
6998 }
6999
7000 /**
Dave Burke579e1402012-10-18 20:41:55 -07007001 * Extracts the layout parameters from the supplied attributes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 *
7003 * @param a the style attributes to extract the parameters from
7004 * @param widthAttr the identifier of the width attribute
7005 * @param heightAttr the identifier of the height attribute
7006 */
7007 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
Dave Burke579e1402012-10-18 20:41:55 -07007008 width = a.getLayoutDimension(widthAttr, "layout_width");
7009 height = a.getLayoutDimension(heightAttr, "layout_height");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007010 }
7011
7012 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007013 * Resolve layout parameters depending on the layout direction. Subclasses that care about
7014 * layoutDirection changes should override this method. The default implementation does
7015 * nothing.
7016 *
7017 * @param layoutDirection the direction of the layout
7018 *
7019 * {@link View#LAYOUT_DIRECTION_LTR}
7020 * {@link View#LAYOUT_DIRECTION_RTL}
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007021 */
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007022 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007023 }
7024
7025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 * Returns a String representation of this set of layout parameters.
7027 *
7028 * @param output the String to prepend to the internal representation
7029 * @return a String with the following format: output +
7030 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07007031 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007032 * @hide
7033 */
7034 public String debug(String output) {
7035 return output + "ViewGroup.LayoutParams={ width="
7036 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
7037 }
7038
7039 /**
Philip Milne10ca24a2012-04-23 15:38:27 -07007040 * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
7041 *
7042 * @param view the view that contains these layout parameters
7043 * @param canvas the canvas on which to draw
7044 *
7045 * @hide
7046 */
Philip Milne7b757812012-09-19 18:13:44 -07007047 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07007048 }
7049
7050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 * Converts the specified size to a readable String.
7052 *
7053 * @param size the size to convert
7054 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07007055 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 * @hide
7057 */
7058 protected static String sizeToString(int size) {
7059 if (size == WRAP_CONTENT) {
7060 return "wrap-content";
7061 }
Romain Guy980a9382010-01-08 15:06:28 -08007062 if (size == MATCH_PARENT) {
7063 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 }
7065 return String.valueOf(size);
7066 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007067
7068 /** @hide */
7069 void encode(@NonNull ViewHierarchyEncoder encoder) {
7070 encoder.beginObject(this);
7071 encodeProperties(encoder);
7072 encoder.endObject();
7073 }
7074
7075 /** @hide */
7076 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7077 encoder.addProperty("width", width);
7078 encoder.addProperty("height", height);
7079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 }
7081
7082 /**
7083 * Per-child layout information for layouts that support margins.
7084 * See
7085 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
7086 * for a list of all child view attributes that this class supports.
7087 */
7088 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
7089 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007090 * The left margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007091 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7092 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007094 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007095 public int leftMargin;
7096
7097 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007098 * The top margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007099 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7100 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007102 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 public int topMargin;
7104
7105 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007106 * The right margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007107 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7108 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007110 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 public int rightMargin;
7112
7113 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007114 * The bottom margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007115 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7116 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007118 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 public int bottomMargin;
7120
7121 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007122 * The start margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007123 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7124 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007125 */
7126 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007127 private int startMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007128
7129 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007130 * The end margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007131 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7132 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007133 */
7134 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007135 private int endMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007136
7137 /**
7138 * The default start and end margin.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007139 * @hide
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007140 */
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007141 public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007142
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007143 /**
7144 * Bit 0: layout direction
7145 * Bit 1: layout direction
7146 * Bit 2: left margin undefined
7147 * Bit 3: right margin undefined
7148 * Bit 4: is RTL compatibility mode
7149 * Bit 5: need resolution
7150 *
7151 * Bit 6 to 7 not used
7152 *
7153 * @hide
7154 */
7155 @ViewDebug.ExportedProperty(category = "layout", flagMapping = {
7156 @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK,
7157 equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"),
7158 @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK,
7159 equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"),
7160 @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK,
7161 equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"),
7162 @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK,
7163 equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"),
7164 @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK,
7165 equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK")
Jon Miranda4597e982014-07-29 07:25:49 -07007166 }, formatToHexString = true)
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007167 byte mMarginFlags;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007168
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007169 private static final int LAYOUT_DIRECTION_MASK = 0x00000003;
7170 private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004;
7171 private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008;
7172 private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010;
7173 private static final int NEED_RESOLUTION_MASK = 0x00000020;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007174
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007175 private static final int DEFAULT_MARGIN_RESOLVED = 0;
7176 private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007177
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 * Creates a new set of layout parameters. The values are extracted from
7180 * the supplied attributes set and context.
7181 *
7182 * @param c the application environment
7183 * @param attrs the set of attributes from which to extract the layout
7184 * parameters' values
7185 */
7186 public MarginLayoutParams(Context c, AttributeSet attrs) {
7187 super();
7188
7189 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
7190 setBaseAttributes(a,
7191 R.styleable.ViewGroup_MarginLayout_layout_width,
7192 R.styleable.ViewGroup_MarginLayout_layout_height);
7193
7194 int margin = a.getDimensionPixelSize(
7195 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
7196 if (margin >= 0) {
7197 leftMargin = margin;
7198 topMargin = margin;
7199 rightMargin= margin;
7200 bottomMargin = margin;
7201 } else {
7202 leftMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007203 R.styleable.ViewGroup_MarginLayout_layout_marginLeft,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007204 UNDEFINED_MARGIN);
7205 if (leftMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007206 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007207 leftMargin = DEFAULT_MARGIN_RESOLVED;
7208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 rightMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007210 R.styleable.ViewGroup_MarginLayout_layout_marginRight,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007211 UNDEFINED_MARGIN);
7212 if (rightMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007213 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007214 rightMargin = DEFAULT_MARGIN_RESOLVED;
7215 }
7216
7217 topMargin = a.getDimensionPixelSize(
7218 R.styleable.ViewGroup_MarginLayout_layout_marginTop,
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007219 DEFAULT_MARGIN_RESOLVED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 bottomMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007221 R.styleable.ViewGroup_MarginLayout_layout_marginBottom,
7222 DEFAULT_MARGIN_RESOLVED);
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007223
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007224 startMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007225 R.styleable.ViewGroup_MarginLayout_layout_marginStart,
7226 DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007227 endMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007228 R.styleable.ViewGroup_MarginLayout_layout_marginEnd,
7229 DEFAULT_MARGIN_RELATIVE);
7230
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007231 if (isMarginRelative()) {
7232 mMarginFlags |= NEED_RESOLUTION_MASK;
7233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007234 }
7235
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007236 final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
7237 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007238 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
7239 mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
7240 }
7241
7242 // Layout direction is LTR by default
7243 mMarginFlags |= LAYOUT_DIRECTION_LTR;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 a.recycle();
7246 }
7247
7248 /**
7249 * {@inheritDoc}
7250 */
7251 public MarginLayoutParams(int width, int height) {
7252 super(width, height);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007253
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007254 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7255 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007256
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007257 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7258 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 }
7260
7261 /**
7262 * Copy constructor. Clones the width, height and margin values of the source.
7263 *
7264 * @param source The layout params to copy from.
7265 */
7266 public MarginLayoutParams(MarginLayoutParams source) {
7267 this.width = source.width;
7268 this.height = source.height;
7269
7270 this.leftMargin = source.leftMargin;
7271 this.topMargin = source.topMargin;
7272 this.rightMargin = source.rightMargin;
7273 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007274 this.startMargin = source.startMargin;
7275 this.endMargin = source.endMargin;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007276
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007277 this.mMarginFlags = source.mMarginFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 }
7279
7280 /**
7281 * {@inheritDoc}
7282 */
7283 public MarginLayoutParams(LayoutParams source) {
7284 super(source);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007285
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007286 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7287 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007288
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007289 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7290 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 }
7292
7293 /**
Adam Powelld7600832014-07-01 15:22:50 -07007294 * @hide Used internally.
7295 */
7296 public final void copyMarginsFrom(MarginLayoutParams source) {
7297 this.leftMargin = source.leftMargin;
7298 this.topMargin = source.topMargin;
7299 this.rightMargin = source.rightMargin;
7300 this.bottomMargin = source.bottomMargin;
7301 this.startMargin = source.startMargin;
7302 this.endMargin = source.endMargin;
7303
7304 this.mMarginFlags = source.mMarginFlags;
7305 }
7306
7307 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007308 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
7309 * to be done so that the new margins are taken into account. Left and right margins may be
7310 * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
Adam Powella7a735f2014-10-09 12:54:52 -07007311 * Margin values should be positive.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 *
7313 * @param left the left margin size
7314 * @param top the top margin size
7315 * @param right the right margin size
7316 * @param bottom the bottom margin size
7317 *
7318 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
7319 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7320 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
7321 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7322 */
7323 public void setMargins(int left, int top, int right, int bottom) {
7324 leftMargin = left;
7325 topMargin = top;
7326 rightMargin = right;
7327 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007328 mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
7329 mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
7330 if (isMarginRelative()) {
7331 mMarginFlags |= NEED_RESOLUTION_MASK;
7332 } else {
7333 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007336
7337 /**
7338 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
7339 * needs to be done so that the new relative margins are taken into account. Left and right
7340 * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
Adam Powella7a735f2014-10-09 12:54:52 -07007341 * direction. Margin values should be positive.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007342 *
7343 * @param start the start margin size
7344 * @param top the top margin size
7345 * @param end the right margin size
7346 * @param bottom the bottom margin size
7347 *
7348 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7349 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7350 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7351 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7352 *
7353 * @hide
7354 */
7355 public void setMarginsRelative(int start, int top, int end, int bottom) {
7356 startMargin = start;
7357 topMargin = top;
7358 endMargin = end;
7359 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007360 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007361 }
7362
7363 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007364 * Sets the relative start margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007365 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007366 * @param start the start margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007367 *
7368 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7369 */
7370 public void setMarginStart(int start) {
7371 startMargin = start;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007372 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007373 }
7374
7375 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007376 * Returns the start margin in pixels.
7377 *
7378 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7379 *
7380 * @return the start margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007381 */
7382 public int getMarginStart() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007383 if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007384 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007385 doResolveMargins();
7386 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007387 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007388 case View.LAYOUT_DIRECTION_RTL:
7389 return rightMargin;
7390 case View.LAYOUT_DIRECTION_LTR:
7391 default:
7392 return leftMargin;
7393 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007394 }
7395
7396 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007397 * Sets the relative end margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007398 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007399 * @param end the end margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007400 *
7401 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7402 */
7403 public void setMarginEnd(int end) {
7404 endMargin = end;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007405 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007406 }
7407
7408 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007409 * Returns the end margin in pixels.
7410 *
7411 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7412 *
7413 * @return the end margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007414 */
7415 public int getMarginEnd() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007416 if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007417 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007418 doResolveMargins();
7419 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007420 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007421 case View.LAYOUT_DIRECTION_RTL:
7422 return leftMargin;
7423 case View.LAYOUT_DIRECTION_LTR:
7424 default:
7425 return rightMargin;
7426 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007427 }
7428
7429 /**
7430 * Check if margins are relative.
7431 *
7432 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7433 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7434 *
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007435 * @return true if either marginStart or marginEnd has been set.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007436 */
7437 public boolean isMarginRelative() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007438 return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007439 }
7440
7441 /**
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007442 * Set the layout direction
7443 * @param layoutDirection the layout direction.
7444 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
7445 * or {@link View#LAYOUT_DIRECTION_RTL}.
7446 */
7447 public void setLayoutDirection(int layoutDirection) {
7448 if (layoutDirection != View.LAYOUT_DIRECTION_LTR &&
7449 layoutDirection != View.LAYOUT_DIRECTION_RTL) return;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007450 if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) {
7451 mMarginFlags &= ~LAYOUT_DIRECTION_MASK;
7452 mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK);
7453 if (isMarginRelative()) {
7454 mMarginFlags |= NEED_RESOLUTION_MASK;
7455 } else {
7456 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7457 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007458 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007459 }
7460
7461 /**
7462 * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or
7463 * {@link View#LAYOUT_DIRECTION_RTL}.
7464 *
7465 * @return the layout direction.
7466 */
7467 public int getLayoutDirection() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007468 return (mMarginFlags & LAYOUT_DIRECTION_MASK);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007469 }
7470
7471 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007472 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08007473 * may be overridden depending on layout direction.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007474 */
7475 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007476 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007477 setLayoutDirection(layoutDirection);
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007478
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007479 // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything
7480 // Will use the left and right margins if no relative margin is defined.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007481 if (!isMarginRelative() ||
7482 (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007483
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007484 // Proceed with resolution
7485 doResolveMargins();
7486 }
7487
7488 private void doResolveMargins() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007489 if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007490 // if left or right margins are not defined and if we have some start or end margin
7491 // defined then use those start and end margins.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007492 if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK
7493 && startMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007494 leftMargin = startMargin;
7495 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007496 if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK
7497 && endMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007498 rightMargin = endMargin;
7499 }
7500 } else {
7501 // We have some relative margins (either the start one or the end one or both). So use
7502 // them and override what has been defined for left and right margins. If either start
7503 // or end margin is not defined, just set it to default "0".
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007504 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007505 case View.LAYOUT_DIRECTION_RTL:
7506 leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7507 endMargin : DEFAULT_MARGIN_RESOLVED;
7508 rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7509 startMargin : DEFAULT_MARGIN_RESOLVED;
7510 break;
7511 case View.LAYOUT_DIRECTION_LTR:
7512 default:
7513 leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7514 startMargin : DEFAULT_MARGIN_RESOLVED;
7515 rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7516 endMargin : DEFAULT_MARGIN_RESOLVED;
7517 break;
7518 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007519 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007520 mMarginFlags &= ~NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007521 }
Philip Milne10ca24a2012-04-23 15:38:27 -07007522
Fabrice Di Meglio03b8d3a2012-09-27 17:05:27 -07007523 /**
7524 * @hide
7525 */
7526 public boolean isLayoutRtl() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007527 return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007528 }
7529
Philip Milne10ca24a2012-04-23 15:38:27 -07007530 /**
7531 * @hide
7532 */
7533 @Override
Philip Milne7b757812012-09-19 18:13:44 -07007534 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
7535 Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE;
7536
7537 fillDifference(canvas,
7538 view.getLeft() + oi.left,
7539 view.getTop() + oi.top,
7540 view.getRight() - oi.right,
7541 view.getBottom() - oi.bottom,
7542 leftMargin,
7543 topMargin,
7544 rightMargin,
7545 bottomMargin,
7546 paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07007547 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007548
7549 /** @hide */
7550 @Override
7551 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7552 super.encodeProperties(encoder);
7553 encoder.addProperty("leftMargin", leftMargin);
7554 encoder.addProperty("topMargin", topMargin);
7555 encoder.addProperty("rightMargin", rightMargin);
7556 encoder.addProperty("bottomMargin", bottomMargin);
7557 encoder.addProperty("startMargin", startMargin);
7558 encoder.addProperty("endMargin", endMargin);
7559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 }
Adam Powell2b342f02010-08-18 18:14:13 -07007561
Jeff Brown20e987b2010-08-23 12:01:02 -07007562 /* Describes a touched view and the ids of the pointers that it has captured.
7563 *
7564 * This code assumes that pointer ids are always in the range 0..31 such that
7565 * it can use a bitfield to track which pointer ids are present.
7566 * As it happens, the lower layers of the input dispatch pipeline also use the
7567 * same trick so the assumption should be safe here...
7568 */
7569 private static final class TouchTarget {
7570 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007571 private static final Object sRecycleLock = new Object[0];
Jeff Brown20e987b2010-08-23 12:01:02 -07007572 private static TouchTarget sRecycleBin;
7573 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07007574
Jeff Brown20e987b2010-08-23 12:01:02 -07007575 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07007576
Jeff Brown20e987b2010-08-23 12:01:02 -07007577 // The touched child view.
7578 public View child;
7579
7580 // The combined bit mask of pointer ids for all pointers captured by the target.
7581 public int pointerIdBits;
7582
7583 // The next target in the target list.
7584 public TouchTarget next;
7585
7586 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07007587 }
7588
Jeff Brown20e987b2010-08-23 12:01:02 -07007589 public static TouchTarget obtain(View child, int pointerIdBits) {
7590 final TouchTarget target;
7591 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07007592 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007593 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07007594 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07007595 target = sRecycleBin;
7596 sRecycleBin = target.next;
7597 sRecycledCount--;
7598 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007599 }
Adam Powell816c3be2010-08-23 18:00:05 -07007600 }
Jeff Brown20e987b2010-08-23 12:01:02 -07007601 target.child = child;
7602 target.pointerIdBits = pointerIdBits;
7603 return target;
7604 }
Adam Powell816c3be2010-08-23 18:00:05 -07007605
Jeff Brown20e987b2010-08-23 12:01:02 -07007606 public void recycle() {
7607 synchronized (sRecycleLock) {
7608 if (sRecycledCount < MAX_RECYCLED) {
7609 next = sRecycleBin;
7610 sRecycleBin = this;
7611 sRecycledCount += 1;
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007612 } else {
7613 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007614 }
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007615 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007616 }
7617 }
Adam Powell2b342f02010-08-18 18:14:13 -07007618 }
Jeff Brown87b7f802011-06-21 18:35:45 -07007619
7620 /* Describes a hovered view. */
7621 private static final class HoverTarget {
7622 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007623 private static final Object sRecycleLock = new Object[0];
Jeff Brown87b7f802011-06-21 18:35:45 -07007624 private static HoverTarget sRecycleBin;
7625 private static int sRecycledCount;
7626
7627 // The hovered child view.
7628 public View child;
7629
7630 // The next target in the target list.
7631 public HoverTarget next;
7632
7633 private HoverTarget() {
7634 }
7635
7636 public static HoverTarget obtain(View child) {
7637 final HoverTarget target;
7638 synchronized (sRecycleLock) {
7639 if (sRecycleBin == null) {
7640 target = new HoverTarget();
7641 } else {
7642 target = sRecycleBin;
7643 sRecycleBin = target.next;
7644 sRecycledCount--;
7645 target.next = null;
7646 }
7647 }
7648 target.child = child;
7649 return target;
7650 }
7651
7652 public void recycle() {
7653 synchronized (sRecycleLock) {
7654 if (sRecycledCount < MAX_RECYCLED) {
7655 next = sRecycleBin;
7656 sRecycleBin = this;
7657 sRecycledCount += 1;
7658 } else {
7659 next = null;
7660 }
7661 child = null;
7662 }
7663 }
7664 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007665
7666 /**
7667 * Pooled class that orderes the children of a ViewGroup from start
7668 * to end based on how they are laid out and the layout direction.
7669 */
7670 static class ChildListForAccessibility {
7671
7672 private static final int MAX_POOL_SIZE = 32;
7673
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007674 private static final SynchronizedPool<ChildListForAccessibility> sPool =
7675 new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007676
7677 private final ArrayList<View> mChildren = new ArrayList<View>();
7678
7679 private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
7680
7681 public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007682 ChildListForAccessibility list = sPool.acquire();
7683 if (list == null) {
7684 list = new ChildListForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007685 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007686 list.init(parent, sort);
7687 return list;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007688 }
7689
7690 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007691 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007692 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007693 }
7694
7695 public int getChildCount() {
7696 return mChildren.size();
7697 }
7698
7699 public View getChildAt(int index) {
7700 return mChildren.get(index);
7701 }
7702
7703 public int getChildIndex(View child) {
7704 return mChildren.indexOf(child);
7705 }
7706
7707 private void init(ViewGroup parent, boolean sort) {
7708 ArrayList<View> children = mChildren;
7709 final int childCount = parent.getChildCount();
7710 for (int i = 0; i < childCount; i++) {
7711 View child = parent.getChildAt(i);
7712 children.add(child);
7713 }
7714 if (sort) {
7715 ArrayList<ViewLocationHolder> holders = mHolders;
7716 for (int i = 0; i < childCount; i++) {
7717 View child = children.get(i);
7718 ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
7719 holders.add(holder);
7720 }
Svetoslav88e447b2014-10-09 15:49:02 -07007721 sort(holders);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007722 for (int i = 0; i < childCount; i++) {
7723 ViewLocationHolder holder = holders.get(i);
7724 children.set(i, holder.mView);
7725 holder.recycle();
7726 }
7727 holders.clear();
7728 }
7729 }
7730
Svetoslav88e447b2014-10-09 15:49:02 -07007731 private void sort(ArrayList<ViewLocationHolder> holders) {
7732 // This is gross but the least risky solution. The current comparison
7733 // strategy breaks transitivity but produces very good results. Coming
7734 // up with a new strategy requires time which we do not have, so ...
7735 try {
7736 ViewLocationHolder.setComparisonStrategy(
7737 ViewLocationHolder.COMPARISON_STRATEGY_STRIPE);
7738 Collections.sort(holders);
7739 } catch (IllegalArgumentException iae) {
7740 // Note that in practice this occurs extremely rarely in a couple
7741 // of pathological cases.
7742 ViewLocationHolder.setComparisonStrategy(
7743 ViewLocationHolder.COMPARISON_STRATEGY_LOCATION);
7744 Collections.sort(holders);
7745 }
7746 }
7747
Svetoslav Ganov42138042012-03-20 11:51:39 -07007748 private void clear() {
7749 mChildren.clear();
7750 }
7751 }
7752
7753 /**
7754 * Pooled class that holds a View and its location with respect to
7755 * a specified root. This enables sorting of views based on their
7756 * coordinates without recomputing the position relative to the root
7757 * on every comparison.
7758 */
7759 static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
7760
7761 private static final int MAX_POOL_SIZE = 32;
7762
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007763 private static final SynchronizedPool<ViewLocationHolder> sPool =
7764 new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007765
Svetoslav88e447b2014-10-09 15:49:02 -07007766 public static final int COMPARISON_STRATEGY_STRIPE = 1;
7767
7768 public static final int COMPARISON_STRATEGY_LOCATION = 2;
7769
7770 private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE;
7771
Svetoslav Ganov42138042012-03-20 11:51:39 -07007772 private final Rect mLocation = new Rect();
7773
7774 public View mView;
7775
7776 private int mLayoutDirection;
7777
7778 public static ViewLocationHolder obtain(ViewGroup root, View view) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007779 ViewLocationHolder holder = sPool.acquire();
7780 if (holder == null) {
7781 holder = new ViewLocationHolder();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007782 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007783 holder.init(root, view);
7784 return holder;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007785 }
7786
Svetoslav88e447b2014-10-09 15:49:02 -07007787 public static void setComparisonStrategy(int strategy) {
7788 sComparisonStrategy = strategy;
7789 }
7790
Svetoslav Ganov42138042012-03-20 11:51:39 -07007791 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007792 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007793 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007794 }
7795
7796 @Override
7797 public int compareTo(ViewLocationHolder another) {
7798 // This instance is greater than an invalid argument.
7799 if (another == null) {
7800 return 1;
7801 }
Svetoslav88e447b2014-10-09 15:49:02 -07007802
7803 if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) {
7804 // First is above second.
7805 if (mLocation.bottom - another.mLocation.top <= 0) {
7806 return -1;
7807 }
7808 // First is below second.
7809 if (mLocation.top - another.mLocation.bottom >= 0) {
7810 return 1;
7811 }
7812 }
7813
Svetoslav04cab1b2014-08-25 18:35:57 -07007814 // We are ordering left-to-right, top-to-bottom.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007815 if (mLayoutDirection == LAYOUT_DIRECTION_LTR) {
7816 final int leftDifference = mLocation.left - another.mLocation.left;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007817 if (leftDifference != 0) {
7818 return leftDifference;
7819 }
7820 } else { // RTL
7821 final int rightDifference = mLocation.right - another.mLocation.right;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007822 if (rightDifference != 0) {
7823 return -rightDifference;
7824 }
7825 }
Svetoslav04cab1b2014-08-25 18:35:57 -07007826 // We are ordering left-to-right, top-to-bottom.
7827 final int topDifference = mLocation.top - another.mLocation.top;
7828 if (topDifference != 0) {
7829 return topDifference;
7830 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007831 // Break tie by height.
7832 final int heightDiference = mLocation.height() - another.mLocation.height();
7833 if (heightDiference != 0) {
7834 return -heightDiference;
7835 }
7836 // Break tie by width.
7837 final int widthDiference = mLocation.width() - another.mLocation.width();
7838 if (widthDiference != 0) {
7839 return -widthDiference;
7840 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007841 // Just break the tie somehow. The accessibliity ids are unique
7842 // and stable, hence this is deterministic tie breaking.
7843 return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007844 }
7845
7846 private void init(ViewGroup root, View view) {
7847 Rect viewLocation = mLocation;
7848 view.getDrawingRect(viewLocation);
7849 root.offsetDescendantRectToMyCoords(view, viewLocation);
7850 mView = view;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07007851 mLayoutDirection = root.getLayoutDirection();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007852 }
7853
7854 private void clear() {
7855 mView = null;
7856 mLocation.set(0, 0, 0, 0);
7857 }
7858 }
Romain Guycbc67742012-04-27 16:12:57 -07007859
7860 private static Paint getDebugPaint() {
7861 if (sDebugPaint == null) {
7862 sDebugPaint = new Paint();
7863 sDebugPaint.setAntiAlias(false);
7864 }
7865 return sDebugPaint;
7866 }
7867
Romain Guy6410c0a2013-06-17 11:21:58 -07007868 private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
Romain Guycbc67742012-04-27 16:12:57 -07007869 if (sDebugLines== null) {
Romain Guy6410c0a2013-06-17 11:21:58 -07007870 // TODO: This won't work with multiple UI threads in a single process
Romain Guycbc67742012-04-27 16:12:57 -07007871 sDebugLines = new float[16];
7872 }
7873
Romain Guycbc67742012-04-27 16:12:57 -07007874 sDebugLines[0] = x1;
7875 sDebugLines[1] = y1;
7876 sDebugLines[2] = x2;
7877 sDebugLines[3] = y1;
7878
7879 sDebugLines[4] = x2;
7880 sDebugLines[5] = y1;
7881 sDebugLines[6] = x2;
Philip Milne7b757812012-09-19 18:13:44 -07007882 sDebugLines[7] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007883
Philip Milne7b757812012-09-19 18:13:44 -07007884 sDebugLines[8] = x2;
Romain Guycbc67742012-04-27 16:12:57 -07007885 sDebugLines[9] = y2;
7886 sDebugLines[10] = x1;
7887 sDebugLines[11] = y2;
7888
Philip Milne7b757812012-09-19 18:13:44 -07007889 sDebugLines[12] = x1;
7890 sDebugLines[13] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007891 sDebugLines[14] = x1;
7892 sDebugLines[15] = y1;
7893
Philip Milne7b757812012-09-19 18:13:44 -07007894 canvas.drawLines(sDebugLines, paint);
Romain Guycbc67742012-04-27 16:12:57 -07007895 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007896
7897 /** @hide */
7898 @Override
7899 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7900 super.encodeProperties(encoder);
7901
7902 encoder.addProperty("focus:descendantFocusability", getDescendantFocusability());
7903 encoder.addProperty("drawing:clipChildren", getClipChildren());
7904 encoder.addProperty("drawing:clipToPadding", getClipToPadding());
7905 encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled());
7906 encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache());
7907
7908 int n = getChildCount();
7909 encoder.addProperty("meta:__childCount__", (short)n);
7910 for (int i = 0; i < n; i++) {
7911 encoder.addPropertyKey("meta:__child__" + i);
7912 getChildAt(i).encode(encoder);
7913 }
7914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915}