blob: f6958738353b0e6ac58f4f49fb32d27a6bfc8044 [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;
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +090062
Fabrice Di Meglio0072f642013-03-26 15:50:24 -070063import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065/**
66 * <p>
67 * A <code>ViewGroup</code> is a special view that can contain other views
68 * (called children.) The view group is the base class for layouts and views
69 * containers. This class also defines the
70 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
71 * class for layouts parameters.
72 * </p>
73 *
74 * <p>
75 * Also see {@link LayoutParams} for layout attributes.
76 * </p>
Romain Guyd6a463a2009-05-21 23:10:10 -070077 *
Joe Fernandez558459f2011-10-13 16:47:36 -070078 * <div class="special reference">
79 * <h3>Developer Guides</h3>
80 * <p>For more information about creating user interface layouts, read the
81 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
82 * guide.</p></div>
83 *
Dianne Hackborn7caab0f2013-03-06 13:47:04 -080084 * <p>Here is a complete implementation of a custom ViewGroup that implements
85 * a simple {@link android.widget.FrameLayout} along with the ability to stack
86 * children in left and right gutters.</p>
87 *
88 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java
89 * Complete}
90 *
91 * <p>If you are implementing XML layout attributes as shown in the example, this is the
92 * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p>
93 *
94 * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout}
95 *
96 * <p>Finally the layout manager can be used in an XML layout like so:</p>
97 *
98 * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete}
99 *
Romain Guyd6a463a2009-05-21 23:10:10 -0700100 * @attr ref android.R.styleable#ViewGroup_clipChildren
101 * @attr ref android.R.styleable#ViewGroup_clipToPadding
102 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
103 * @attr ref android.R.styleable#ViewGroup_animationCache
104 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
105 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
106 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
107 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
Chet Haase13cc1202010-09-03 15:39:20 -0700108 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Scott Main27a85082013-06-10 10:39:48 -0700109 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
110 * @attr ref android.R.styleable#ViewGroup_layoutMode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 */
Tor Norbye83c68962015-03-10 20:55:31 -0700112@UiThread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Adam Powell539ee872012-02-03 19:00:49 -0800114 private static final String TAG = "ViewGroup";
Chet Haase21cd1382010-09-01 17:42:29 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 private static final boolean DBG = false;
Philip Milne7b757812012-09-19 18:13:44 -0700117 /** @hide */
118 public static boolean DEBUG_DRAW = false;
Gilles Debunnecea45132011-11-24 02:19:27 +0100119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 /**
121 * Views which have been hidden or removed which need to be animated on
122 * their way out.
123 * This field should be made private, so it is hidden from the SDK.
124 * {@hide}
125 */
126 protected ArrayList<View> mDisappearingChildren;
127
128 /**
129 * Listener used to propagate events indicating when children are added
130 * and/or removed from a view group.
131 * This field should be made private, so it is hidden from the SDK.
132 * {@hide}
133 */
134 protected OnHierarchyChangeListener mOnHierarchyChangeListener;
135
136 // The view contained within this ViewGroup that has or contains focus.
137 private View mFocused;
138
Chet Haase48460322010-06-11 14:22:25 -0700139 /**
140 * A Transformation used when drawing children, to
141 * apply on the child being drawn.
142 */
Romain Guyf6991302013-06-05 17:19:01 -0700143 private Transformation mChildTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700144
145 /**
146 * Used to track the current invalidation region.
147 */
Chet Haase64a48c12012-02-13 16:33:29 -0800148 RectF mInvalidateRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Chet Haase48460322010-06-11 14:22:25 -0700150 /**
151 * A Transformation used to calculate a correct
152 * invalidation area when the application is autoscaled.
153 */
Chet Haase64a48c12012-02-13 16:33:29 -0800154 Transformation mInvalidationTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700155
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700156 // View currently under an ongoing drag. Can be null, a child or this window.
Christopher Tatea53146c2010-09-07 11:57:52 -0700157 private View mCurrentDragView;
158
Christopher Tate86cab1b2011-01-13 20:28:55 -0800159 // Metadata about the ongoing drag
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700160 private DragEvent mCurrentDragStartEvent;
161 private boolean mIsInterestedInDrag;
162 private HashSet<View> mChildrenInterestedInDrag;
Christopher Tatea53146c2010-09-07 11:57:52 -0700163
164 // Used during drag dispatch
Romain Guy6410c0a2013-06-17 11:21:58 -0700165 private PointF mLocalPoint;
Christopher Tatea53146c2010-09-07 11:57:52 -0700166
Alan Viveretteb942b6f2014-12-08 10:37:39 -0800167 // Lazily-created holder for point computations.
168 private float[] mTempPoint;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 // Layout animation
171 private LayoutAnimationController mLayoutAnimationController;
172 private Animation.AnimationListener mAnimationListener;
173
Jeff Brown20e987b2010-08-23 12:01:02 -0700174 // First touch target in the linked list of touch targets.
175 private TouchTarget mFirstTouchTarget;
176
Joe Onorato03ab0c72011-01-06 15:46:27 -0800177 // For debugging only. You can see these in hierarchyviewer.
Romain Guye95003e2011-01-09 13:53:06 -0800178 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800179 @ViewDebug.ExportedProperty(category = "events")
180 private long mLastTouchDownTime;
181 @ViewDebug.ExportedProperty(category = "events")
182 private int mLastTouchDownIndex = -1;
Romain Guye95003e2011-01-09 13:53:06 -0800183 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800184 @ViewDebug.ExportedProperty(category = "events")
185 private float mLastTouchDownX;
Romain Guye95003e2011-01-09 13:53:06 -0800186 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800187 @ViewDebug.ExportedProperty(category = "events")
188 private float mLastTouchDownY;
189
Jeff Brown87b7f802011-06-21 18:35:45 -0700190 // First hover target in the linked list of hover targets.
191 // The hover targets are children which have received ACTION_HOVER_ENTER.
192 // They might not have actually handled the hover event, but we will
193 // continue sending hover events to them as long as the pointer remains over
194 // their bounds and the view group does not intercept hover.
195 private HoverTarget mFirstHoverTarget;
Jeff Browna032cc02011-03-07 16:56:21 -0800196
Jeff Brown10b62902011-06-20 16:40:37 -0700197 // True if the view group itself received a hover event.
198 // It might not have actually handled the hover event.
199 private boolean mHoveredSelf;
200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * This field should be made private, so it is hidden from the SDK.
205 * {@hide}
206 */
Romain Guy2440e672012-08-07 14:43:43 -0700207 @ViewDebug.ExportedProperty(flagMapping = {
208 @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN,
209 name = "CLIP_CHILDREN"),
210 @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING,
211 name = "CLIP_TO_PADDING"),
212 @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL,
213 name = "PADDING_NOT_NULL")
Jon Miranda4597e982014-07-29 07:25:49 -0700214 }, formatToHexString = true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 protected int mGroupFlags;
216
Philip Milne7b757812012-09-19 18:13:44 -0700217 /**
218 * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -0700219 */
Philip Milnecfb631b2012-10-26 10:51:46 -0700220 private int mLayoutMode = LAYOUT_MODE_UNDEFINED;
Philip Milne1557fd72012-04-04 23:41:34 -0700221
Romain Guy33f6beb2012-02-16 19:24:51 -0800222 /**
223 * NOTE: If you change the flags below make sure to reflect the changes
224 * the DisplayList class
225 */
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 // When set, ViewGroup invalidates only the child's rectangle
228 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800229 static final int FLAG_CLIP_CHILDREN = 0x1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 // When set, ViewGroup excludes the padding area from the invalidate rectangle
232 // Set by default
233 private static final int FLAG_CLIP_TO_PADDING = 0x2;
234
235 // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
236 // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
Chet Haase64a48c12012-02-13 16:33:29 -0800237 static final int FLAG_INVALIDATE_REQUIRED = 0x4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // When set, dispatchDraw() will run the layout animation and unset the flag
240 private static final int FLAG_RUN_ANIMATION = 0x8;
241
242 // When set, there is either no layout animation on the ViewGroup or the layout
243 // animation is over
244 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800245 static final int FLAG_ANIMATION_DONE = 0x10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
247 // If set, this ViewGroup has padding; if unset there is no padding and we don't need
248 // to clip it, even if FLAG_CLIP_TO_PADDING is set
249 private static final int FLAG_PADDING_NOT_NULL = 0x20;
250
Chris Craik5a6bbae2015-04-10 17:41:34 -0700251 /** @deprecated - functionality removed */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 private static final int FLAG_ANIMATION_CACHE = 0x40;
253
254 // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
255 // layout animation; this avoid clobbering the hierarchy
256 // Automatically set when the layout animation starts, depending on the animation's
257 // characteristics
Chet Haase64a48c12012-02-13 16:33:29 -0800258 static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
260 // When set, the next call to drawChild() will clear mChildTransformation's matrix
Chet Haase64a48c12012-02-13 16:33:29 -0800261 static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
264 // the children's Bitmap caches if necessary
265 // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
266 private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
267
268 /**
269 * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
270 * to get the index of the child to draw for that iteration.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800271 *
Romain Guy293451e2009-11-04 13:59:48 -0800272 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
274 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * When set, this ViewGroup supports static transformations on children; this causes
278 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
279 * invoked when a child is drawn.
280 *
281 * Any subclass overriding
282 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
283 * set this flags in {@link #mGroupFlags}.
Romain Guy8506ab42009-06-11 17:35:47 -0700284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * {@hide}
286 */
287 protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
288
John Reckfb5899d2014-08-15 18:51:27 -0700289 // UNUSED FLAG VALUE: 0x1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
291 /**
292 * When set, this ViewGroup's drawable states also include those
293 * of its children.
294 */
295 private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
296
Chris Craik5a6bbae2015-04-10 17:41:34 -0700297 /** @deprecated functionality removed */
298 private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Chris Craik5a6bbae2015-04-10 17:41:34 -0700300 /** @deprecated functionality removed */
301 private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
303 /**
304 * When set, this group will go through its list of children to notify them of
305 * any drawable state change.
306 */
307 private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
308
309 private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
310
311 /**
312 * This view will get focus before any of its descendants.
313 */
314 public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
315
316 /**
317 * This view will get focus only if none of its descendants want it.
318 */
319 public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
320
321 /**
322 * This view will block any of its descendants from getting focus, even
323 * if they are focusable.
324 */
325 public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
326
327 /**
328 * Used to map between enum in attrubutes and flag values.
329 */
330 private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
331 {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
332 FOCUS_BLOCK_DESCENDANTS};
333
334 /**
335 * When set, this ViewGroup should not intercept touch events.
Adam Powell110486f2010-06-22 17:14:44 -0700336 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 */
Adam Powell110486f2010-06-22 17:14:44 -0700338 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700341 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
342 */
Adam Powellf37df072010-09-17 16:22:49 -0700343 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700344
345 /**
Adam Powell4b867882011-09-16 12:59:46 -0700346 * When set, this ViewGroup will not dispatch onAttachedToWindow calls
347 * to children when adding new views. This is used to prevent multiple
348 * onAttached calls when a ViewGroup adds children in its own onAttached method.
349 */
350 private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000;
351
352 /**
Philip Milnecfb631b2012-10-26 10:51:46 -0700353 * When true, indicates that a layoutMode has been explicitly set, either with
354 * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource.
355 * This distinguishes the situation in which a layout mode was inherited from
356 * one of the ViewGroup's ancestors and cached locally.
357 */
358 private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000;
359
Chris Craikb49f4462014-03-20 12:44:20 -0700360 static final int FLAG_IS_TRANSITION_GROUP = 0x1000000;
Chris Craikd863a102013-12-19 13:31:15 -0800361
Chris Craikb49f4462014-03-20 12:44:20 -0700362 static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000;
George Mount0a778ed2013-12-13 13:35:36 -0800363
Chris Craikd863a102013-12-19 13:31:15 -0800364 /**
Adam Powellff0d2982014-07-10 20:34:14 -0700365 * When set, focus will not be permitted to enter this group if a touchscreen is present.
366 */
367 static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000;
368
369 /**
Clara Bayarri4423d912015-03-02 19:42:48 +0000370 * When true, indicates that a call to startActionModeForChild was made with the type parameter
371 * and should not be ignored. This helps in backwards compatibility with the existing method
372 * without a type.
373 *
374 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
375 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
376 */
377 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000;
378
379 /**
380 * When true, indicates that a call to startActionModeForChild was made without the type
381 * parameter. This helps in backwards compatibility with the existing method
382 * without a type.
383 *
384 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
385 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
386 */
387 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000;
388
389 /**
Adam Powell2af189a2016-02-05 15:52:02 -0800390 * When set, indicates that a call to showContextMenuForChild was made with explicit
391 * coordinates within the initiating child view.
392 */
393 private static final int FLAG_SHOW_CONTEXT_MENU_WITH_COORDS = 0x20000000;
394
395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 * Indicates which types of drawing caches are to be kept in memory.
397 * This field should be made private, so it is hidden from the SDK.
398 * {@hide}
399 */
400 protected int mPersistentDrawingCache;
401
402 /**
403 * Used to indicate that no drawing cache should be kept in memory.
404 */
405 public static final int PERSISTENT_NO_CACHE = 0x0;
406
407 /**
408 * Used to indicate that the animation drawing cache should be kept in memory.
409 */
410 public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
411
412 /**
413 * Used to indicate that the scrolling drawing cache should be kept in memory.
414 */
415 public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
416
417 /**
418 * Used to indicate that all drawing caches should be kept in memory.
419 */
420 public static final int PERSISTENT_ALL_CACHES = 0x3;
421
Philip Milne1557fd72012-04-04 23:41:34 -0700422 // Layout Modes
423
Philip Milnecfb631b2012-10-26 10:51:46 -0700424 private static final int LAYOUT_MODE_UNDEFINED = -1;
425
Philip Milne1557fd72012-04-04 23:41:34 -0700426 /**
427 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700428 * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
Philip Milne1557fd72012-04-04 23:41:34 -0700429 * {@link #getRight() right} and {@link #getBottom() bottom}.
430 */
Philip Milne7b757812012-09-19 18:13:44 -0700431 public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
Philip Milne1557fd72012-04-04 23:41:34 -0700432
433 /**
434 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700435 * Optical bounds describe where a widget appears to be. They sit inside the clip
436 * bounds which need to cover a larger area to allow other effects,
437 * such as shadows and glows, to be drawn.
Philip Milne1557fd72012-04-04 23:41:34 -0700438 */
Philip Milne7b757812012-09-19 18:13:44 -0700439 public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
440
441 /** @hide */
Philip Milnecfb631b2012-10-26 10:51:46 -0700442 public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS;
Philip Milne1557fd72012-04-04 23:41:34 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
446 * are set at the same time.
447 */
448 protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
449
450 // Index of the child's left position in the mLocation array
451 private static final int CHILD_LEFT_INDEX = 0;
452 // Index of the child's top position in the mLocation array
453 private static final int CHILD_TOP_INDEX = 1;
454
455 // Child views of this ViewGroup
456 private View[] mChildren;
457 // Number of valid children in the mChildren array, the rest should be null or not
458 // considered as children
459 private int mChildrenCount;
460
Chet Haaseb9895022013-04-02 15:10:58 -0700461 // Whether layout calls are currently being suppressed, controlled by calls to
462 // suppressLayout()
463 boolean mSuppressLayout = false;
464
465 // Whether any layout calls have actually been suppressed while mSuppressLayout
466 // has been true. This tracks whether we need to issue a requestLayout() when
467 // layout is later re-enabled.
468 private boolean mLayoutCalledWhileSuppressed = false;
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 private static final int ARRAY_INITIAL_CAPACITY = 12;
471 private static final int ARRAY_CAPACITY_INCREMENT = 12;
472
Romain Guycbc67742012-04-27 16:12:57 -0700473 private static Paint sDebugPaint;
474 private static float[] sDebugLines;
Philip Milne604f4402012-04-24 19:27:11 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 // Used to draw cached views
Chet Haase64a48c12012-02-13 16:33:29 -0800477 Paint mCachePaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
Chet Haase21cd1382010-09-01 17:42:29 -0700479 // Used to animate add/remove changes in layout
480 private LayoutTransition mTransition;
481
482 // The set of views that are currently being transitioned. This list is used to track views
483 // being removed that should not actually be removed from the parent yet because they are
484 // being animated.
485 private ArrayList<View> mTransitioningViews;
486
Chet Haase5e25c2c2010-09-16 11:15:56 -0700487 // List of children changing visibility. This is used to potentially keep rendering
488 // views during a transition when they otherwise would have become gone/invisible
489 private ArrayList<View> mVisibilityChangingChildren;
490
Chris Craikab008f02014-05-23 17:55:03 -0700491 // Temporary holder of presorted children, only used for
492 // input/software draw dispatch for correctly Z ordering.
493 private ArrayList<View> mPreSortedChildren;
494
Adam Powell539ee872012-02-03 19:00:49 -0800495 // Indicates how many of this container's child subtrees contain transient state
496 @ViewDebug.ExportedProperty(category = "layout")
497 private int mChildCountWithTransientState = 0;
498
Adam Powell10ba2772014-04-15 09:46:51 -0700499 /**
500 * Currently registered axes for nested scrolling. Flag set consisting of
501 * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE}
502 * for null.
503 */
504 private int mNestedScrollAxes;
505
Chet Haasec633d2f2015-04-07 10:29:39 -0700506 // Used to manage the list of transient views, added by addTransientView()
507 private List<Integer> mTransientIndices = null;
508 private List<View> mTransientViews = null;
509
510
Clara Bayarri4423d912015-03-02 19:42:48 +0000511 /**
512 * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild.
513 *
514 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
515 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
516 */
517 private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() {
518 @Override
519 public void setTitle(CharSequence title) {}
520
521 @Override
522 public void setTitle(int resId) {}
523
524 @Override
525 public void setSubtitle(CharSequence subtitle) {}
526
527 @Override
528 public void setSubtitle(int resId) {}
529
530 @Override
531 public void setCustomView(View view) {}
532
533 @Override
534 public void invalidate() {}
535
536 @Override
537 public void finish() {}
538
539 @Override
540 public Menu getMenu() {
541 return null;
542 }
543
544 @Override
545 public CharSequence getTitle() {
546 return null;
547 }
548
549 @Override
550 public CharSequence getSubtitle() {
551 return null;
552 }
553
554 @Override
555 public View getCustomView() {
556 return null;
557 }
558
559 @Override
560 public MenuInflater getMenuInflater() {
561 return null;
562 }
563 };
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public ViewGroup(Context context) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700566 this(context, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568
569 public ViewGroup(Context context, AttributeSet attrs) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700570 this(context, attrs, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
Alan Viverette617feb92013-09-09 18:09:13 -0700573 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700574 this(context, attrs, defStyleAttr, 0);
Alan Viverette617feb92013-09-09 18:09:13 -0700575 }
576
577 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
578 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 initViewGroup();
Alan Viveretted6479ec2013-09-10 17:03:02 -0700580 initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
Philip Milne10ca24a2012-04-23 15:38:27 -0700583 private boolean debugDraw() {
Philip Milne7b757812012-09-19 18:13:44 -0700584 return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout;
Philip Milne10ca24a2012-04-23 15:38:27 -0700585 }
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 private void initViewGroup() {
588 // ViewGroup doesn't draw by default
Philip Milne10ca24a2012-04-23 15:38:27 -0700589 if (!debugDraw()) {
590 setFlags(WILL_NOT_DRAW, DRAW_MASK);
591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mGroupFlags |= FLAG_CLIP_CHILDREN;
593 mGroupFlags |= FLAG_CLIP_TO_PADDING;
594 mGroupFlags |= FLAG_ANIMATION_DONE;
Chris Craikf6ce8fd2015-05-11 15:33:11 -0700595 mGroupFlags |= FLAG_ANIMATION_CACHE;
596 mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597
Jeff Brown995e7742010-12-22 16:59:36 -0800598 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
599 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
600 }
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
603
604 mChildren = new View[ARRAY_INITIAL_CAPACITY];
605 mChildrenCount = 0;
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
608 }
609
Alan Viveretted6479ec2013-09-10 17:03:02 -0700610 private void initFromAttributes(
611 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Filip Gruszczyńskib50cea02014-03-05 17:54:58 -0800612 final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr,
613 defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 final int N = a.getIndexCount();
616 for (int i = 0; i < N; i++) {
617 int attr = a.getIndex(i);
618 switch (attr) {
619 case R.styleable.ViewGroup_clipChildren:
620 setClipChildren(a.getBoolean(attr, true));
621 break;
622 case R.styleable.ViewGroup_clipToPadding:
623 setClipToPadding(a.getBoolean(attr, true));
624 break;
625 case R.styleable.ViewGroup_animationCache:
626 setAnimationCacheEnabled(a.getBoolean(attr, true));
627 break;
628 case R.styleable.ViewGroup_persistentDrawingCache:
629 setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
630 break;
631 case R.styleable.ViewGroup_addStatesFromChildren:
632 setAddStatesFromChildren(a.getBoolean(attr, false));
633 break;
634 case R.styleable.ViewGroup_alwaysDrawnWithCache:
635 setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
636 break;
637 case R.styleable.ViewGroup_layoutAnimation:
638 int id = a.getResourceId(attr, -1);
639 if (id > 0) {
640 setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
641 }
642 break;
643 case R.styleable.ViewGroup_descendantFocusability:
644 setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
645 break;
Adam Powell2b342f02010-08-18 18:14:13 -0700646 case R.styleable.ViewGroup_splitMotionEvents:
647 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
648 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700649 case R.styleable.ViewGroup_animateLayoutChanges:
650 boolean animateLayoutChanges = a.getBoolean(attr, false);
651 if (animateLayoutChanges) {
652 setLayoutTransition(new LayoutTransition());
653 }
654 break;
Philip Milne7b757812012-09-19 18:13:44 -0700655 case R.styleable.ViewGroup_layoutMode:
Philip Milnecfb631b2012-10-26 10:51:46 -0700656 setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED));
Philip Milne7b757812012-09-19 18:13:44 -0700657 break;
George Mount0a778ed2013-12-13 13:35:36 -0800658 case R.styleable.ViewGroup_transitionGroup:
659 setTransitionGroup(a.getBoolean(attr, false));
660 break;
Adam Powellff0d2982014-07-10 20:34:14 -0700661 case R.styleable.ViewGroup_touchscreenBlocksFocus:
662 setTouchscreenBlocksFocus(a.getBoolean(attr, false));
663 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
666
667 a.recycle();
668 }
669
670 /**
671 * Gets the descendant focusability of this view group. The descendant
672 * focusability defines the relationship between this view group and its
673 * descendants when looking for a view to take focus in
674 * {@link #requestFocus(int, android.graphics.Rect)}.
675 *
676 * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
677 * {@link #FOCUS_BLOCK_DESCENDANTS}.
678 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700679 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
681 @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
682 @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
683 })
684 public int getDescendantFocusability() {
685 return mGroupFlags & FLAG_MASK_FOCUSABILITY;
686 }
687
688 /**
689 * Set the descendant focusability of this view group. This defines the relationship
690 * between this view group and its descendants when looking for a view to
691 * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
692 *
693 * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
694 * {@link #FOCUS_BLOCK_DESCENDANTS}.
695 */
696 public void setDescendantFocusability(int focusability) {
697 switch (focusability) {
698 case FOCUS_BEFORE_DESCENDANTS:
699 case FOCUS_AFTER_DESCENDANTS:
700 case FOCUS_BLOCK_DESCENDANTS:
701 break;
702 default:
703 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
704 + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
705 }
706 mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
707 mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
708 }
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 @Override
711 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
712 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800713 mFocused.unFocus(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 mFocused = null;
715 }
716 super.handleFocusGainInternal(direction, previouslyFocusedRect);
717 }
718
Alan Viverettebe463f22016-01-21 10:50:10 -0500719 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 public void requestChildFocus(View child, View focused) {
721 if (DBG) {
722 System.out.println(this + " requestChildFocus()");
723 }
724 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
725 return;
726 }
727
728 // Unfocus us, if necessary
Alan Viverette223622a2013-12-17 13:29:02 -0800729 super.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730
731 // We had a previous notion of who had focus. Clear it.
732 if (mFocused != child) {
733 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800734 mFocused.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736
737 mFocused = child;
738 }
739 if (mParent != null) {
740 mParent.requestChildFocus(this, focused);
741 }
742 }
743
Alan Viverettebe463f22016-01-21 10:50:10 -0500744 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 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
Alan Viverettebe463f22016-01-21 10:50:10 -0500763 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 public boolean showContextMenuForChild(View originalView) {
Adam Powell2af189a2016-02-05 15:52:02 -0800765 if (isShowingContextMenuWithCoords()) {
766 // We're being called for compatibility. Return false and let the version
767 // with coordinates recurse up.
768 return false;
769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 return mParent != null && mParent.showContextMenuForChild(originalView);
771 }
772
Adam Powell2af189a2016-02-05 15:52:02 -0800773 /**
774 * @hide used internally for compatibility with existing app code only
775 */
776 public final boolean isShowingContextMenuWithCoords() {
777 return (mGroupFlags & FLAG_SHOW_CONTEXT_MENU_WITH_COORDS) != 0;
778 }
779
Oren Blasberged391262015-09-01 12:12:51 -0700780 @Override
781 public boolean showContextMenuForChild(View originalView, float x, float y) {
Adam Powell2af189a2016-02-05 15:52:02 -0800782 try {
783 mGroupFlags |= FLAG_SHOW_CONTEXT_MENU_WITH_COORDS;
784 if (showContextMenuForChild(originalView)) {
785 return true;
786 }
787 } finally {
788 mGroupFlags &= ~FLAG_SHOW_CONTEXT_MENU_WITH_COORDS;
789 }
Oren Blasberged391262015-09-01 12:12:51 -0700790 return mParent != null && mParent.showContextMenuForChild(originalView, x, y);
791 }
792
Clara Bayarri4423d912015-03-02 19:42:48 +0000793 @Override
Adam Powell6e346362010-07-23 10:18:23 -0700794 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000795 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) {
796 // This is the original call.
797 try {
798 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
799 return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY);
800 } finally {
801 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
802 }
803 } else {
804 // We are being called from the new method with type.
805 return SENTINEL_ACTION_MODE;
806 }
807 }
808
Clara Bayarri4423d912015-03-02 19:42:48 +0000809 @Override
810 public ActionMode startActionModeForChild(
811 View originalView, ActionMode.Callback callback, int type) {
Adam Powelle9fd6d22015-06-01 11:26:32 -0700812 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0
813 && type == ActionMode.TYPE_PRIMARY) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000814 ActionMode mode;
815 try {
816 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
817 mode = startActionModeForChild(originalView, callback);
818 } finally {
819 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
820 }
821 if (mode != SENTINEL_ACTION_MODE) {
822 return mode;
823 }
824 }
825 if (mParent != null) {
826 try {
827 return mParent.startActionModeForChild(originalView, callback, type);
828 } catch (AbstractMethodError ame) {
829 // Custom view parents might not implement this method.
830 return mParent.startActionModeForChild(originalView, callback);
831 }
832 }
833 return null;
Adam Powell6e346362010-07-23 10:18:23 -0700834 }
835
836 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000837 * @hide
838 */
839 @Override
840 public boolean dispatchActivityResult(
841 String who, int requestCode, int resultCode, Intent data) {
842 if (super.dispatchActivityResult(who, requestCode, resultCode, data)) {
843 return true;
844 }
845 int childCount = getChildCount();
846 for (int i = 0; i < childCount; i++) {
847 View child = getChildAt(i);
848 if (child.dispatchActivityResult(who, requestCode, resultCode, data)) {
849 return true;
850 }
851 }
852 return false;
853 }
854
855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 * Find the nearest view in the specified direction that wants to take
857 * focus.
858 *
859 * @param focused The view that currently has focus
860 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
861 * FOCUS_RIGHT, or 0 for not applicable.
862 */
Alan Viverettebe463f22016-01-21 10:50:10 -0500863 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 public View focusSearch(View focused, int direction) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -0700865 if (isRootNamespace()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 // root namespace means we should consider ourselves the top of the
867 // tree for focus searching; otherwise we could be focus searching
868 // into other tabs. see LocalActivityManager and TabHost for more info
869 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
870 } else if (mParent != null) {
871 return mParent.focusSearch(focused, direction);
872 }
873 return null;
874 }
875
Alan Viverettebe463f22016-01-21 10:50:10 -0500876 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
878 return false;
879 }
880
Svetoslav Ganov42138042012-03-20 11:51:39 -0700881 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700882 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -0700883 ViewParent parent = mParent;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700884 if (parent == null) {
885 return false;
886 }
887 final boolean propagate = onRequestSendAccessibilityEvent(child, event);
888 if (!propagate) {
889 return false;
890 }
891 return parent.requestSendAccessibilityEvent(this, event);
892 }
893
894 /**
895 * Called when a child has requested sending an {@link AccessibilityEvent} and
896 * gives an opportunity to its parent to augment the event.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700897 * <p>
Adam Powell2fcbbd02011-09-28 18:56:43 -0700898 * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling
899 * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its
900 * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)}
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700901 * is responsible for handling this call.
902 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700903 *
904 * @param child The child which requests sending the event.
905 * @param event The event to be sent.
906 * @return True if the event should be sent.
907 *
908 * @see #requestSendAccessibilityEvent(View, AccessibilityEvent)
909 */
910 public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700911 if (mAccessibilityDelegate != null) {
912 return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event);
913 } else {
914 return onRequestSendAccessibilityEventInternal(child, event);
915 }
916 }
917
918 /**
919 * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent)
920 *
921 * Note: Called from the default {@link View.AccessibilityDelegate}.
Alan Viverettea54956a2015-01-07 16:05:02 -0800922 *
923 * @hide
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700924 */
Alan Viverettea54956a2015-01-07 16:05:02 -0800925 public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700926 return true;
927 }
928
929 /**
Adam Powell539ee872012-02-03 19:00:49 -0800930 * Called when a child view has changed whether or not it is tracking transient state.
Adam Powell539ee872012-02-03 19:00:49 -0800931 */
Alan Viverettebe463f22016-01-21 10:50:10 -0500932 @Override
Adam Powell539ee872012-02-03 19:00:49 -0800933 public void childHasTransientStateChanged(View child, boolean childHasTransientState) {
934 final boolean oldHasTransientState = hasTransientState();
935 if (childHasTransientState) {
936 mChildCountWithTransientState++;
937 } else {
938 mChildCountWithTransientState--;
939 }
940
941 final boolean newHasTransientState = hasTransientState();
942 if (mParent != null && oldHasTransientState != newHasTransientState) {
943 try {
944 mParent.childHasTransientStateChanged(this, newHasTransientState);
945 } catch (AbstractMethodError e) {
946 Log.e(TAG, mParent.getClass().getSimpleName() +
947 " does not fully implement ViewParent", e);
948 }
949 }
950 }
951
Adam Powell539ee872012-02-03 19:00:49 -0800952 @Override
953 public boolean hasTransientState() {
954 return mChildCountWithTransientState > 0 || super.hasTransientState();
955 }
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 @Override
958 public boolean dispatchUnhandledMove(View focused, int direction) {
959 return mFocused != null &&
960 mFocused.dispatchUnhandledMove(focused, direction);
961 }
962
Alan Viverettebe463f22016-01-21 10:50:10 -0500963 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 public void clearChildFocus(View child) {
965 if (DBG) {
966 System.out.println(this + " clearChildFocus()");
967 }
968
969 mFocused = null;
970 if (mParent != null) {
971 mParent.clearChildFocus(this);
972 }
973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 @Override
976 public void clearFocus() {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800977 if (DBG) {
978 System.out.println(this + " clearFocus()");
979 }
980 if (mFocused == null) {
981 super.clearFocus();
982 } else {
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700983 View focused = mFocused;
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800984 mFocused = null;
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700985 focused.clearFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
987 }
988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 @Override
Alan Viverette223622a2013-12-17 13:29:02 -0800990 void unFocus(View focused) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 if (DBG) {
992 System.out.println(this + " unFocus()");
993 }
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800994 if (mFocused == null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800995 super.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800996 } else {
Alan Viverette223622a2013-12-17 13:29:02 -0800997 mFocused.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800998 mFocused = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 /**
1003 * Returns the focused child of this view, if any. The child may have focus
1004 * or contain focus.
1005 *
1006 * @return the focused child or null.
1007 */
1008 public View getFocusedChild() {
1009 return mFocused;
1010 }
1011
Adam Powell88c11752014-07-21 17:19:16 -07001012 View getDeepestFocusedChild() {
1013 View v = this;
1014 while (v != null) {
1015 if (v.isFocused()) {
1016 return v;
1017 }
1018 v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null;
1019 }
1020 return null;
1021 }
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 /**
1024 * Returns true if this view has or contains focus
1025 *
1026 * @return true if this view has or contains focus
1027 */
1028 @Override
1029 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001030 return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032
1033 /*
1034 * (non-Javadoc)
1035 *
1036 * @see android.view.View#findFocus()
1037 */
1038 @Override
1039 public View findFocus() {
1040 if (DBG) {
1041 System.out.println("Find focus in " + this + ": flags="
1042 + isFocused() + ", child=" + mFocused);
1043 }
1044
1045 if (isFocused()) {
1046 return this;
1047 }
1048
1049 if (mFocused != null) {
1050 return mFocused.findFocus();
1051 }
1052 return null;
1053 }
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 @Override
1056 public boolean hasFocusable() {
1057 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
1058 return false;
1059 }
1060
1061 if (isFocusable()) {
1062 return true;
1063 }
1064
1065 final int descendantFocusability = getDescendantFocusability();
Adam Powell88c11752014-07-21 17:19:16 -07001066 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 final int count = mChildrenCount;
1068 final View[] children = mChildren;
1069
1070 for (int i = 0; i < count; i++) {
1071 final View child = children[i];
1072 if (child.hasFocusable()) {
1073 return true;
1074 }
1075 }
1076 }
1077
1078 return false;
1079 }
1080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001082 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 final int focusableCount = views.size();
1084
1085 final int descendantFocusability = getDescendantFocusability();
1086
Adam Powell88c11752014-07-21 17:19:16 -07001087 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
1088 if (shouldBlockFocusForTouchscreen()) {
1089 focusableMode |= FOCUSABLES_TOUCH_MODE;
1090 }
1091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 final int count = mChildrenCount;
1093 final View[] children = mChildren;
1094
1095 for (int i = 0; i < count; i++) {
1096 final View child = children[i];
1097 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001098 child.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 }
1101 }
1102
1103 // we add ourselves (if focusable) in all cases except for when we are
1104 // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is
1105 // to avoid the focus search finding layouts when a more precise search
1106 // among the focusable children would be more interesting.
Adam Powellff0d2982014-07-10 20:34:14 -07001107 if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 // No focusable descendants
Adam Powell88c11752014-07-21 17:19:16 -07001109 || (focusableCount == views.size())) &&
1110 (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001111 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113 }
1114
Adam Powellff0d2982014-07-10 20:34:14 -07001115 /**
1116 * Set whether this ViewGroup should ignore focus requests for itself and its children.
1117 * If this option is enabled and the ViewGroup or a descendant currently has focus, focus
1118 * will proceed forward.
1119 *
1120 * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen
1121 */
1122 public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {
1123 if (touchscreenBlocksFocus) {
1124 mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1125 if (hasFocus()) {
Adam Powell88c11752014-07-21 17:19:16 -07001126 final View focusedChild = getDeepestFocusedChild();
1127 if (!focusedChild.isFocusableInTouchMode()) {
1128 final View newFocus = focusSearch(FOCUS_FORWARD);
1129 if (newFocus != null) {
1130 newFocus.requestFocus();
1131 }
Adam Powellff0d2982014-07-10 20:34:14 -07001132 }
1133 }
1134 } else {
1135 mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1136 }
1137 }
1138
1139 /**
1140 * Check whether this ViewGroup should ignore focus requests for itself and its children.
1141 */
1142 public boolean getTouchscreenBlocksFocus() {
1143 return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0;
1144 }
1145
1146 boolean shouldBlockFocusForTouchscreen() {
1147 return getTouchscreenBlocksFocus() &&
1148 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
1149 }
1150
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001151 @Override
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001152 public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
1153 super.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001154 final int childrenCount = mChildrenCount;
1155 final View[] children = mChildren;
1156 for (int i = 0; i < childrenCount; i++) {
1157 View child = children[i];
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001158 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
Dianne Hackborn4702a852012-08-17 15:18:29 -07001159 && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001160 child.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001161 }
1162 }
1163 }
1164
Svetoslav5b578da2013-05-08 14:23:32 -07001165 /** @hide */
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001166 @Override
Svetoslav5b578da2013-05-08 14:23:32 -07001167 public View findViewByAccessibilityIdTraversal(int accessibilityId) {
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001168 View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId);
1169 if (foundView != null) {
1170 return foundView;
1171 }
Svetoslav6cc46272015-06-03 11:38:30 -07001172
1173 if (getAccessibilityNodeProvider() != null) {
1174 return null;
1175 }
1176
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001177 final int childrenCount = mChildrenCount;
1178 final View[] children = mChildren;
1179 for (int i = 0; i < childrenCount; i++) {
1180 View child = children[i];
1181 foundView = child.findViewByAccessibilityIdTraversal(accessibilityId);
1182 if (foundView != null) {
1183 return foundView;
1184 }
1185 }
Svetoslav6cc46272015-06-03 11:38:30 -07001186
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001187 return null;
1188 }
1189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 @Override
1191 public void dispatchWindowFocusChanged(boolean hasFocus) {
1192 super.dispatchWindowFocusChanged(hasFocus);
1193 final int count = mChildrenCount;
1194 final View[] children = mChildren;
1195 for (int i = 0; i < count; i++) {
1196 children[i].dispatchWindowFocusChanged(hasFocus);
1197 }
1198 }
1199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 @Override
1201 public void addTouchables(ArrayList<View> views) {
1202 super.addTouchables(views);
1203
1204 final int count = mChildrenCount;
1205 final View[] children = mChildren;
1206
1207 for (int i = 0; i < count; i++) {
1208 final View child = children[i];
1209 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1210 child.addTouchables(views);
1211 }
1212 }
1213 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001214
1215 /**
1216 * @hide
1217 */
1218 @Override
1219 public void makeOptionalFitsSystemWindows() {
1220 super.makeOptionalFitsSystemWindows();
1221 final int count = mChildrenCount;
1222 final View[] children = mChildren;
1223 for (int i = 0; i < count; i++) {
1224 children[i].makeOptionalFitsSystemWindows();
1225 }
1226 }
1227
Romain Guy43c9cdf2010-01-27 13:53:55 -08001228 @Override
1229 public void dispatchDisplayHint(int hint) {
1230 super.dispatchDisplayHint(hint);
1231 final int count = mChildrenCount;
1232 final View[] children = mChildren;
1233 for (int i = 0; i < count; i++) {
1234 children[i].dispatchDisplayHint(hint);
1235 }
1236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237
1238 /**
Chet Haase0d299362012-01-26 10:51:48 -08001239 * Called when a view's visibility has changed. Notify the parent to take any appropriate
1240 * action.
1241 *
1242 * @param child The view whose visibility has changed
1243 * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
1244 * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
Chet Haase5e25c2c2010-09-16 11:15:56 -07001245 * @hide
Chet Haase5e25c2c2010-09-16 11:15:56 -07001246 */
Chet Haase0d299362012-01-26 10:51:48 -08001247 protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07001248 if (mTransition != null) {
Chet Haase0d299362012-01-26 10:51:48 -08001249 if (newVisibility == VISIBLE) {
1250 mTransition.showChild(this, child, oldVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001251 } else {
Chet Haase0d299362012-01-26 10:51:48 -08001252 mTransition.hideChild(this, child, newVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001253 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
Chet Haaseddbb3462012-06-19 13:54:29 -07001254 // Only track this on disappearing views - appearing views are already visible
1255 // and don't need special handling during drawChild()
1256 if (mVisibilityChangingChildren == null) {
1257 mVisibilityChangingChildren = new ArrayList<View>();
1258 }
1259 mVisibilityChangingChildren.add(child);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001260 addDisappearingView(child);
1261 }
1262 }
1263 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001264
1265 // in all cases, for drags
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001266 if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) {
1267 if (!mChildrenInterestedInDrag.contains(child)) {
1268 notifyChildOfDragStart(child);
Christopher Tate86cab1b2011-01-13 20:28:55 -08001269 }
1270 }
Chet Haase5e25c2c2010-09-16 11:15:56 -07001271 }
1272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 @Override
Adam Powell326d8082009-12-09 15:10:07 -08001274 protected void dispatchVisibilityChanged(View changedView, int visibility) {
1275 super.dispatchVisibilityChanged(changedView, visibility);
1276 final int count = mChildrenCount;
1277 final View[] children = mChildren;
1278 for (int i = 0; i < count; i++) {
1279 children[i].dispatchVisibilityChanged(changedView, visibility);
1280 }
1281 }
1282
Adam Powell326d8082009-12-09 15:10:07 -08001283 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public void dispatchWindowVisibilityChanged(int visibility) {
1285 super.dispatchWindowVisibilityChanged(visibility);
1286 final int count = mChildrenCount;
1287 final View[] children = mChildren;
1288 for (int i = 0; i < count; i++) {
1289 children[i].dispatchWindowVisibilityChanged(visibility);
1290 }
1291 }
1292
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001293 @Override
Adam Powell9c146bf2016-03-15 17:35:00 -07001294 boolean dispatchVisibilityAggregated(boolean isVisible) {
1295 isVisible = super.dispatchVisibilityAggregated(isVisible);
Adam Powell41d96902016-03-15 14:43:19 -07001296 final int count = mChildrenCount;
1297 final View[] children = mChildren;
1298 for (int i = 0; i < count; i++) {
Adam Powell9c146bf2016-03-15 17:35:00 -07001299 // Only dispatch to visible children. Not visible children and their subtrees already
1300 // know that they aren't visible and that's not going to change as a result of
1301 // whatever triggered this dispatch.
1302 if (children[i].getVisibility() == VISIBLE) {
1303 children[i].dispatchVisibilityAggregated(isVisible);
Adam Powell41d96902016-03-15 14:43:19 -07001304 }
1305 }
Adam Powell9c146bf2016-03-15 17:35:00 -07001306 return isVisible;
Adam Powell41d96902016-03-15 14:43:19 -07001307 }
1308
1309 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001310 public void dispatchConfigurationChanged(Configuration newConfig) {
1311 super.dispatchConfigurationChanged(newConfig);
1312 final int count = mChildrenCount;
1313 final View[] children = mChildren;
1314 for (int i = 0; i < count; i++) {
1315 children[i].dispatchConfigurationChanged(newConfig);
1316 }
1317 }
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08001318
Alan Viverettebe463f22016-01-21 10:50:10 -05001319 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -08001321 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1322 ViewParent parent = mParent;
1323 if (parent != null) parent.recomputeViewAttributes(this);
1324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
Romain Guy8506ab42009-06-11 17:35:47 -07001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001328 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1329 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1330 super.dispatchCollectViewAttributes(attachInfo, visibility);
1331 final int count = mChildrenCount;
1332 final View[] children = mChildren;
1333 for (int i = 0; i < count; i++) {
1334 final View child = children[i];
1335 child.dispatchCollectViewAttributes(attachInfo,
1336 visibility | (child.mViewFlags&VISIBILITY_MASK));
1337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 }
1340
Alan Viverettebe463f22016-01-21 10:50:10 -05001341 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 public void bringChildToFront(View child) {
Alan Viverette77bb6f12015-02-11 17:24:33 -08001343 final int index = indexOfChild(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (index >= 0) {
1345 removeFromArray(index);
1346 addInArray(child, mChildrenCount);
1347 child.mParent = this;
Chet Haasecb96db82013-09-04 10:21:46 -07001348 requestLayout();
1349 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
1352
Romain Guy6410c0a2013-06-17 11:21:58 -07001353 private PointF getLocalPoint() {
1354 if (mLocalPoint == null) mLocalPoint = new PointF();
1355 return mLocalPoint;
1356 }
1357
Steve Block8a7259b2012-03-01 11:24:41 +00001358 // TODO: Write real docs
Christopher Tatea53146c2010-09-07 11:57:52 -07001359 @Override
1360 public boolean dispatchDragEvent(DragEvent event) {
1361 boolean retval = false;
1362 final float tx = event.mX;
1363 final float ty = event.mY;
1364
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07001365 ViewRootImpl root = getViewRootImpl();
Christopher Tatea53146c2010-09-07 11:57:52 -07001366
1367 // Dispatch down the view hierarchy
Romain Guy6410c0a2013-06-17 11:21:58 -07001368 final PointF localPoint = getLocalPoint();
1369
Christopher Tatea53146c2010-09-07 11:57:52 -07001370 switch (event.mAction) {
1371 case DragEvent.ACTION_DRAG_STARTED: {
1372 // clear state to recalculate which views we drag over
Chris Tate9d1ab882010-11-02 15:55:39 -07001373 mCurrentDragView = null;
Christopher Tatea53146c2010-09-07 11:57:52 -07001374
Christopher Tate86cab1b2011-01-13 20:28:55 -08001375 // Set up our tracking of drag-started notifications
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001376 mCurrentDragStartEvent = DragEvent.obtain(event);
1377 if (mChildrenInterestedInDrag == null) {
1378 mChildrenInterestedInDrag = new HashSet<View>();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001379 } else {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001380 mChildrenInterestedInDrag.clear();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001381 }
1382
Christopher Tatea53146c2010-09-07 11:57:52 -07001383 // Now dispatch down to our children, caching the responses
Christopher Tatea53146c2010-09-07 11:57:52 -07001384 final int count = mChildrenCount;
1385 final View[] children = mChildren;
1386 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001387 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001388 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001389 if (child.getVisibility() == VISIBLE) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001390 if (notifyChildOfDragStart(children[i])) {
1391 retval = true;
Christopher Tate2c095f32010-10-04 14:13:40 -07001392 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001393 }
1394 }
1395
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001396 // Notify itself of the drag start.
1397 mIsInterestedInDrag = super.dispatchDragEvent(event);
1398 if (mIsInterestedInDrag) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001399 retval = true;
1400 }
Yorke Lee0394b212016-03-10 18:20:04 -08001401
1402 if (!retval) {
1403 // Neither us nor any of our children are interested in this drag, so stop tracking
1404 // the current drag event.
1405 mCurrentDragStartEvent.recycle();
1406 mCurrentDragStartEvent = null;
1407 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001408 } break;
1409
1410 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001411 // Release the bookkeeping now that the drag lifecycle has ended
Vadim Tryshev15177892015-12-18 08:01:00 -08001412 final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag;
1413 if (childrenInterestedInDrag != null) {
1414 for (View child : childrenInterestedInDrag) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001415 // If a child was interested in the ongoing drag, it's told that it's over
1416 if (child.dispatchDragEvent(event)) {
1417 retval = true;
1418 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001419 child.mPrivateFlags2 &= ~View.DRAG_MASK;
1420 child.refreshDrawableState();
Christopher Tate1fc014f2011-01-19 12:56:26 -08001421 }
Vadim Tryshev15177892015-12-18 08:01:00 -08001422 childrenInterestedInDrag.clear();
1423 }
1424 if (mCurrentDragStartEvent != null) {
1425 mCurrentDragStartEvent.recycle();
1426 mCurrentDragStartEvent = null;
Christopher Tate1fc014f2011-01-19 12:56:26 -08001427 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001428
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001429 if (mIsInterestedInDrag) {
1430 if (super.dispatchDragEvent(event)) {
1431 retval = true;
1432 }
1433 mIsInterestedInDrag = false;
Christopher Tatea53146c2010-09-07 11:57:52 -07001434 }
1435 } break;
1436
1437 case DragEvent.ACTION_DRAG_LOCATION: {
1438 // Find the [possibly new] drag target
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001439 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
1440 if (target == null && mIsInterestedInDrag) {
1441 target = this;
1442 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001443
1444 // If we've changed apparent drag target, tell the view root which view
Chris Tate9d1ab882010-11-02 15:55:39 -07001445 // we're over now [for purposes of the eventual drag-recipient-changed
1446 // notifications to the framework] and tell the new target that the drag
1447 // has entered its bounds. The root will see setDragFocus() calls all
1448 // the way down to the final leaf view that is handling the LOCATION event
1449 // before reporting the new potential recipient to the framework.
Christopher Tatea53146c2010-09-07 11:57:52 -07001450 if (mCurrentDragView != target) {
Chris Tate9d1ab882010-11-02 15:55:39 -07001451 root.setDragFocus(target);
1452
1453 final int action = event.mAction;
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001454 // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED.
1455 event.mX = 0;
1456 event.mY = 0;
1457
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001458 // If we've dragged off of a child view or this window, send it the EXITED message
Chris Tate9d1ab882010-11-02 15:55:39 -07001459 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001460 final View view = mCurrentDragView;
Chris Tate9d1ab882010-11-02 15:55:39 -07001461 event.mAction = DragEvent.ACTION_DRAG_EXITED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001462 if (view != this) {
1463 view.dispatchDragEvent(event);
1464 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1465 view.refreshDrawableState();
1466 } else {
1467 super.dispatchDragEvent(event);
1468 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001469 }
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001470
Christopher Tatea53146c2010-09-07 11:57:52 -07001471 mCurrentDragView = target;
Chris Tate9d1ab882010-11-02 15:55:39 -07001472
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001473 // If we've dragged over a new child view, send it the ENTERED message, otherwise
1474 // send it to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001475 if (target != null) {
1476 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001477 if (target != this) {
1478 target.dispatchDragEvent(event);
1479 target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
1480 target.refreshDrawableState();
1481 } else {
1482 super.dispatchDragEvent(event);
1483 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001484 }
1485 event.mAction = action; // restore the event's original state
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001486 event.mX = tx;
1487 event.mY = ty;
Christopher Tatea53146c2010-09-07 11:57:52 -07001488 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001489
Christopher Tatea53146c2010-09-07 11:57:52 -07001490 // Dispatch the actual drag location notice, localized into its coordinates
1491 if (target != null) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001492 if (target != this) {
1493 event.mX = localPoint.x;
1494 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001495
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001496 retval = target.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001497
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001498 event.mX = tx;
1499 event.mY = ty;
1500 } else {
1501 retval = super.dispatchDragEvent(event);
1502 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001503 }
1504 } break;
1505
Chris Tate9d1ab882010-11-02 15:55:39 -07001506 /* Entered / exited dispatch
1507 *
1508 * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is
1509 * that we're about to get the corresponding LOCATION event, which we will use to
1510 * determine which of our children is the new target; at that point we will
1511 * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001512 * If no suitable child is detected, dispatch to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001513 *
1514 * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1515 * drag has left this ViewGroup, we know by definition that every contained subview
1516 * is also no longer under the drag point.
1517 */
1518
1519 case DragEvent.ACTION_DRAG_EXITED: {
1520 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001521 final View view = mCurrentDragView;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001522 if (view != this) {
1523 view.dispatchDragEvent(event);
1524 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1525 view.refreshDrawableState();
1526 } else {
1527 super.dispatchDragEvent(event);
1528 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001529
Chris Tate9d1ab882010-11-02 15:55:39 -07001530 mCurrentDragView = null;
1531 }
1532 } break;
1533
Christopher Tatea53146c2010-09-07 11:57:52 -07001534 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -07001535 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Romain Guy6410c0a2013-06-17 11:21:58 -07001536 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
Christopher Tatea53146c2010-09-07 11:57:52 -07001537 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001538 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Romain Guy6410c0a2013-06-17 11:21:58 -07001539 event.mX = localPoint.x;
1540 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001541 retval = target.dispatchDragEvent(event);
1542 event.mX = tx;
1543 event.mY = ty;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001544 } else if (mIsInterestedInDrag) {
1545 retval = super.dispatchDragEvent(event);
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001546 } else {
1547 if (ViewDebug.DEBUG_DRAG) {
1548 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
1549 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001550 }
1551 } break;
1552 }
1553
Christopher Tatea53146c2010-09-07 11:57:52 -07001554 return retval;
1555 }
1556
1557 // Find the frontmost child view that lies under the given point, and calculate
1558 // the position within its own local coordinate system.
1559 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001560 final int count = mChildrenCount;
1561 final View[] children = mChildren;
1562 for (int i = count - 1; i >= 0; i--) {
1563 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001564 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001565 continue;
1566 }
1567
Christopher Tate2c095f32010-10-04 14:13:40 -07001568 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001569 return child;
1570 }
1571 }
1572 return null;
1573 }
1574
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001575 boolean notifyChildOfDragStart(View child) {
1576 // The caller guarantees that the child is not in mChildrenInterestedInDrag yet.
1577
Christopher Tate86cab1b2011-01-13 20:28:55 -08001578 if (ViewDebug.DEBUG_DRAG) {
1579 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1580 }
1581
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001582 final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent);
1583 if (canAccept) {
1584 mChildrenInterestedInDrag.add(child);
1585 if (!child.canAcceptDrag()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001586 child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001587 child.refreshDrawableState();
1588 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001589 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001590 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001591 }
1592
Joe Onorato664644d2011-01-23 17:53:23 -08001593 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001594 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1595 super.dispatchWindowSystemUiVisiblityChanged(visible);
1596
1597 final int count = mChildrenCount;
1598 final View[] children = mChildren;
1599 for (int i=0; i <count; i++) {
1600 final View child = children[i];
1601 child.dispatchWindowSystemUiVisiblityChanged(visible);
1602 }
1603 }
1604
1605 @Override
Joe Onorato664644d2011-01-23 17:53:23 -08001606 public void dispatchSystemUiVisibilityChanged(int visible) {
1607 super.dispatchSystemUiVisibilityChanged(visible);
1608
1609 final int count = mChildrenCount;
1610 final View[] children = mChildren;
1611 for (int i=0; i <count; i++) {
1612 final View child = children[i];
1613 child.dispatchSystemUiVisibilityChanged(visible);
1614 }
1615 }
1616
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001617 @Override
Dianne Hackborncf675782012-05-10 15:07:24 -07001618 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
1619 boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001620
1621 final int count = mChildrenCount;
1622 final View[] children = mChildren;
1623 for (int i=0; i <count; i++) {
1624 final View child = children[i];
Dianne Hackborncf675782012-05-10 15:07:24 -07001625 changed |= child.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001626 }
Dianne Hackborncf675782012-05-10 15:07:24 -07001627 return changed;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001628 }
1629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 @Override
1631 public boolean dispatchKeyEventPreIme(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001632 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1633 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 return super.dispatchKeyEventPreIme(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001635 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1636 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 return mFocused.dispatchKeyEventPreIme(event);
1638 }
1639 return false;
1640 }
1641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 @Override
1643 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001644 if (mInputEventConsistencyVerifier != null) {
1645 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1646 }
1647
Dianne Hackborn4702a852012-08-17 15:18:29 -07001648 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1649 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001650 if (super.dispatchKeyEvent(event)) {
1651 return true;
1652 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001653 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1654 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001655 if (mFocused.dispatchKeyEvent(event)) {
1656 return true;
1657 }
1658 }
1659
1660 if (mInputEventConsistencyVerifier != null) {
1661 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 }
1663 return false;
1664 }
1665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 @Override
1667 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001668 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1669 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 return super.dispatchKeyShortcutEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001671 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1672 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 return mFocused.dispatchKeyShortcutEvent(event);
1674 }
1675 return false;
1676 }
1677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 @Override
1679 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001680 if (mInputEventConsistencyVerifier != null) {
1681 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1682 }
1683
Dianne Hackborn4702a852012-08-17 15:18:29 -07001684 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1685 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001686 if (super.dispatchTrackballEvent(event)) {
1687 return true;
1688 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001689 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1690 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001691 if (mFocused.dispatchTrackballEvent(event)) {
1692 return true;
1693 }
1694 }
1695
1696 if (mInputEventConsistencyVerifier != null) {
1697 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699 return false;
1700 }
1701
Jun Mukai1db53972015-09-11 18:08:31 -07001702 @Override
Jun Mukaid4eaef72015-10-30 15:54:33 -07001703 public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09001704 if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) {
1705 return PointerIcon.getSystemIcon(mContext, PointerIcon.STYLE_ARROW);
1706 }
Jun Mukai1db53972015-09-11 18:08:31 -07001707 // Check what the child under the pointer says about the pointer.
1708 final int childrenCount = mChildrenCount;
1709 if (childrenCount != 0) {
1710 final ArrayList<View> preorderedList = buildOrderedChildList();
1711 final boolean customOrder = preorderedList == null
1712 && isChildrenDrawingOrderEnabled();
1713 final View[] children = mChildren;
1714 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05001715 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
1716 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
1717 final PointF point = getLocalPoint();
Jun Mukai1db53972015-09-11 18:08:31 -07001718 if (isTransformedTouchPointInView(x, y, child, point)) {
Jun Mukaid4eaef72015-10-30 15:54:33 -07001719 final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y);
1720 if (pointerIcon != null) {
1721 return pointerIcon;
Jun Mukai1db53972015-09-11 18:08:31 -07001722 }
1723 break;
1724 }
1725 }
1726 }
1727
1728 // The pointer is not a child or the child has no preferences, returning the default
1729 // implementation.
Jun Mukaid4eaef72015-10-30 15:54:33 -07001730 return super.getPointerIcon(event, x, y);
Jun Mukai1db53972015-09-11 18:08:31 -07001731 }
1732
Alan Viverettea7b85e62016-01-22 10:14:02 -05001733 private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {
1734 final int childIndex;
1735 if (customOrder) {
1736 final int childIndex1 = getChildDrawingOrder(childrenCount, i);
1737 if (childIndex1 >= childrenCount) {
1738 throw new IndexOutOfBoundsException("getChildDrawingOrder() "
1739 + "returned invalid index " + childIndex1
1740 + " (child count is " + childrenCount + ")");
1741 }
1742 childIndex = childIndex1;
1743 } else {
1744 childIndex = i;
1745 }
1746 return childIndex;
1747 }
1748
Romain Guya9489272011-06-22 20:58:11 -07001749 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08001751 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08001752 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08001753
Jeff Brown10b62902011-06-20 16:40:37 -07001754 // First check whether the view group wants to intercept the hover event.
1755 final boolean interceptHover = onInterceptHoverEvent(event);
1756 event.setAction(action); // restore action in case it was changed
1757
Jeff Brown87b7f802011-06-21 18:35:45 -07001758 MotionEvent eventNoHistory = event;
1759 boolean handled = false;
1760
1761 // Send events to the hovered children and build a new list of hover targets until
1762 // one is found that handles the event.
1763 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1764 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07001765 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08001766 final float x = event.getX();
1767 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001768 final int childrenCount = mChildrenCount;
1769 if (childrenCount != 0) {
Chris Craikab008f02014-05-23 17:55:03 -07001770 final ArrayList<View> preorderedList = buildOrderedChildList();
1771 final boolean customOrder = preorderedList == null
1772 && isChildrenDrawingOrderEnabled();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001773 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07001774 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001775 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05001776 final int childIndex = getAndVerifyPreorderedIndex(
1777 childrenCount, i, customOrder);
1778 final View child = getAndVerifyPreorderedView(
1779 preorderedList, children, childIndex);
Jeff Brown87b7f802011-06-21 18:35:45 -07001780 if (!canViewReceivePointerEvents(child)
1781 || !isTransformedTouchPointInView(x, y, child, null)) {
1782 continue;
1783 }
1784
1785 // Obtain a hover target for this child. Dequeue it from the
1786 // old hover target list if the child was previously hovered.
1787 HoverTarget hoverTarget = firstOldHoverTarget;
1788 final boolean wasHovered;
1789 for (HoverTarget predecessor = null; ;) {
1790 if (hoverTarget == null) {
1791 hoverTarget = HoverTarget.obtain(child);
1792 wasHovered = false;
1793 break;
1794 }
1795
1796 if (hoverTarget.child == child) {
1797 if (predecessor != null) {
1798 predecessor.next = hoverTarget.next;
1799 } else {
1800 firstOldHoverTarget = hoverTarget.next;
1801 }
1802 hoverTarget.next = null;
1803 wasHovered = true;
1804 break;
1805 }
1806
1807 predecessor = hoverTarget;
1808 hoverTarget = hoverTarget.next;
1809 }
1810
1811 // Enqueue the hover target onto the new hover target list.
1812 if (lastHoverTarget != null) {
1813 lastHoverTarget.next = hoverTarget;
1814 } else {
Jeff Brown87b7f802011-06-21 18:35:45 -07001815 mFirstHoverTarget = hoverTarget;
1816 }
Sangkyu Lee8725f362013-03-13 09:38:45 +09001817 lastHoverTarget = hoverTarget;
Jeff Brown87b7f802011-06-21 18:35:45 -07001818
1819 // Dispatch the event to the child.
1820 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1821 if (!wasHovered) {
1822 // Send the enter as is.
1823 handled |= dispatchTransformedGenericPointerEvent(
1824 event, child); // enter
1825 }
1826 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1827 if (!wasHovered) {
1828 // Synthesize an enter from a move.
1829 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1830 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1831 handled |= dispatchTransformedGenericPointerEvent(
1832 eventNoHistory, child); // enter
1833 eventNoHistory.setAction(action);
1834
1835 handled |= dispatchTransformedGenericPointerEvent(
1836 eventNoHistory, child); // move
1837 } else {
1838 // Send the move as is.
1839 handled |= dispatchTransformedGenericPointerEvent(event, child);
1840 }
1841 }
1842 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07001843 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001844 }
Jeff Brown10b62902011-06-20 16:40:37 -07001845 }
Chris Craikab008f02014-05-23 17:55:03 -07001846 if (preorderedList != null) preorderedList.clear();
Jeff Brown10b62902011-06-20 16:40:37 -07001847 }
1848 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001849
Jeff Brown87b7f802011-06-21 18:35:45 -07001850 // Send exit events to all previously hovered children that are no longer hovered.
1851 while (firstOldHoverTarget != null) {
1852 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07001853
Jeff Brown87b7f802011-06-21 18:35:45 -07001854 // Exit the old hovered child.
1855 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1856 // Send the exit as is.
1857 handled |= dispatchTransformedGenericPointerEvent(
1858 event, child); // exit
1859 } else {
1860 // Synthesize an exit from a move or enter.
1861 // Ignore the result because hover focus has moved to a different view.
1862 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brown10b62902011-06-20 16:40:37 -07001863 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07001864 event, child); // move
Jeff Brown10b62902011-06-20 16:40:37 -07001865 }
Jeff Brown87b7f802011-06-21 18:35:45 -07001866 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1867 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1868 dispatchTransformedGenericPointerEvent(
1869 eventNoHistory, child); // exit
1870 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07001871 }
1872
Jeff Brown87b7f802011-06-21 18:35:45 -07001873 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1874 firstOldHoverTarget.recycle();
1875 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07001876 }
1877
Jeff Brown87b7f802011-06-21 18:35:45 -07001878 // Send events to the view group itself if no children have handled it.
Jeff Brown10b62902011-06-20 16:40:37 -07001879 boolean newHoveredSelf = !handled;
1880 if (newHoveredSelf == mHoveredSelf) {
1881 if (newHoveredSelf) {
1882 // Send event to the view group as before.
1883 handled |= super.dispatchHoverEvent(event);
1884 }
1885 } else {
1886 if (mHoveredSelf) {
1887 // Exit the view group.
1888 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1889 // Send the exit as is.
1890 handled |= super.dispatchHoverEvent(event); // exit
1891 } else {
1892 // Synthesize an exit from a move or enter.
1893 // Ignore the result because hover focus is moving to a different view.
1894 if (action == MotionEvent.ACTION_HOVER_MOVE) {
1895 super.dispatchHoverEvent(event); // move
1896 }
1897 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1898 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1899 super.dispatchHoverEvent(eventNoHistory); // exit
1900 eventNoHistory.setAction(action);
1901 }
1902 mHoveredSelf = false;
1903 }
1904
1905 if (newHoveredSelf) {
1906 // Enter the view group.
1907 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1908 // Send the enter as is.
1909 handled |= super.dispatchHoverEvent(event); // enter
1910 mHoveredSelf = true;
1911 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1912 // Synthesize an enter from a move.
1913 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1914 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1915 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1916 eventNoHistory.setAction(action);
1917
1918 handled |= super.dispatchHoverEvent(eventNoHistory); // move
1919 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001920 }
1921 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001922 }
1923
Jeff Browna032cc02011-03-07 16:56:21 -08001924 // Recycle the copy of the event that we made.
1925 if (eventNoHistory != event) {
1926 eventNoHistory.recycle();
1927 }
1928
Jeff Browna032cc02011-03-07 16:56:21 -08001929 // Done.
1930 return handled;
1931 }
1932
Jeff Brown59a422e2012-04-19 15:19:19 -07001933 private void exitHoverTargets() {
1934 if (mHoveredSelf || mFirstHoverTarget != null) {
1935 final long now = SystemClock.uptimeMillis();
1936 MotionEvent event = MotionEvent.obtain(now, now,
1937 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1938 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1939 dispatchHoverEvent(event);
1940 event.recycle();
1941 }
1942 }
1943
1944 private void cancelHoverTarget(View view) {
1945 HoverTarget predecessor = null;
1946 HoverTarget target = mFirstHoverTarget;
1947 while (target != null) {
1948 final HoverTarget next = target.next;
1949 if (target.child == view) {
1950 if (predecessor == null) {
1951 mFirstHoverTarget = next;
1952 } else {
1953 predecessor.next = next;
1954 }
1955 target.recycle();
1956
1957 final long now = SystemClock.uptimeMillis();
1958 MotionEvent event = MotionEvent.obtain(now, now,
1959 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1960 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1961 view.dispatchHoverEvent(event);
1962 event.recycle();
1963 return;
1964 }
1965 predecessor = target;
1966 target = next;
1967 }
1968 }
1969
Jeff Brown87b7f802011-06-21 18:35:45 -07001970 /** @hide */
1971 @Override
1972 protected boolean hasHoveredChild() {
1973 return mFirstHoverTarget != null;
1974 }
1975
Svetoslav Ganov42138042012-03-20 11:51:39 -07001976 @Override
Adam Powellcf392d12015-06-25 14:48:45 -07001977 public void addChildrenForAccessibility(ArrayList<View> outChildren) {
Svetoslav762621c2015-06-01 17:21:59 -07001978 if (getAccessibilityNodeProvider() != null) {
1979 return;
1980 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001981 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
1982 try {
1983 final int childrenCount = children.getChildCount();
1984 for (int i = 0; i < childrenCount; i++) {
1985 View child = children.getChildAt(i);
Svetoslav Ganovc406be92012-05-11 16:12:32 -07001986 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001987 if (child.includeForAccessibility()) {
Adam Powellcf392d12015-06-25 14:48:45 -07001988 outChildren.add(child);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001989 } else {
Adam Powellcf392d12015-06-25 14:48:45 -07001990 child.addChildrenForAccessibility(outChildren);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001991 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001992 }
1993 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001994 } finally {
1995 children.recycle();
Svetoslav Ganov42138042012-03-20 11:51:39 -07001996 }
1997 }
1998
1999 /**
Jeff Brown10b62902011-06-20 16:40:37 -07002000 * Implement this method to intercept hover events before they are handled
2001 * by child views.
2002 * <p>
2003 * This method is called before dispatching a hover event to a child of
2004 * the view group or to the view group's own {@link #onHoverEvent} to allow
2005 * the view group a chance to intercept the hover event.
2006 * This method can also be used to watch all pointer motions that occur within
2007 * the bounds of the view group even when the pointer is hovering over
2008 * a child of the view group rather than over the view group itself.
2009 * </p><p>
2010 * The view group can prevent its children from receiving hover events by
2011 * implementing this method and returning <code>true</code> to indicate
2012 * that it would like to intercept hover events. The view group must
2013 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
2014 * for as long as it wishes to continue intercepting hover events from
2015 * its children.
2016 * </p><p>
2017 * Interception preserves the invariant that at most one view can be
2018 * hovered at a time by transferring hover focus from the currently hovered
2019 * child to the view group or vice-versa as needed.
2020 * </p><p>
2021 * If this method returns <code>true</code> and a child is already hovered, then the
2022 * child view will first receive a hover exit event and then the view group
2023 * itself will receive a hover enter event in {@link #onHoverEvent}.
2024 * Likewise, if this method had previously returned <code>true</code> to intercept hover
2025 * events and instead returns <code>false</code> while the pointer is hovering
2026 * within the bounds of one of a child, then the view group will first receive a
2027 * hover exit event in {@link #onHoverEvent} and then the hovered child will
2028 * receive a hover enter event.
2029 * </p><p>
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002030 * The default implementation handles mouse hover on the scroll bars.
Jeff Brown10b62902011-06-20 16:40:37 -07002031 * </p>
2032 *
2033 * @param event The motion event that describes the hover.
2034 * @return True if the view group would like to intercept the hover event
2035 * and prevent its children from receiving it.
2036 */
2037 public boolean onInterceptHoverEvent(MotionEvent event) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002038 if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
2039 final int action = event.getAction();
2040 final float x = event.getX();
2041 final float y = event.getY();
2042 if ((action == MotionEvent.ACTION_HOVER_MOVE
2043 || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) {
2044 return true;
2045 }
2046 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002047 return false;
2048 }
2049
Jeff Browna032cc02011-03-07 16:56:21 -08002050 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
2051 if (event.getHistorySize() == 0) {
2052 return event;
2053 }
2054 return MotionEvent.obtainNoHistory(event);
2055 }
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--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002070 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
2071 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Jeff Browna032cc02011-03-07 16:56:21 -08002072 if (!canViewReceivePointerEvents(child)
2073 || !isTransformedTouchPointInView(x, y, child, null)) {
2074 continue;
2075 }
2076
2077 if (dispatchTransformedGenericPointerEvent(event, child)) {
Chris Craikab008f02014-05-23 17:55:03 -07002078 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002079 return true;
2080 }
2081 }
Chris Craikab008f02014-05-23 17:55:03 -07002082 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002083 }
2084
2085 // No child handled the event. Send it to this view group.
2086 return super.dispatchGenericPointerEvent(event);
2087 }
2088
Jeff Browna032cc02011-03-07 16:56:21 -08002089 @Override
2090 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08002091 // Send the event to the focused child or to this view group if it has focus.
Dianne Hackborn4702a852012-08-17 15:18:29 -07002092 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
2093 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08002094 return super.dispatchGenericFocusedEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07002095 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
2096 == PFLAG_HAS_BOUNDS) {
Jeff Browncb1404e2011-01-15 18:14:15 -08002097 return mFocused.dispatchGenericMotionEvent(event);
2098 }
2099 return false;
2100 }
2101
2102 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002103 * Dispatches a generic pointer event to a child, taking into account
2104 * transformations that apply to the child.
2105 *
2106 * @param event The event to send.
2107 * @param child The view to send the event to.
2108 * @return {@code true} if the child handled the event.
2109 */
2110 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
2111 final float offsetX = mScrollX - child.mLeft;
2112 final float offsetY = mScrollY - child.mTop;
2113
2114 boolean handled;
2115 if (!child.hasIdentityMatrix()) {
2116 MotionEvent transformedEvent = MotionEvent.obtain(event);
2117 transformedEvent.offsetLocation(offsetX, offsetY);
2118 transformedEvent.transform(child.getInverseMatrix());
2119 handled = child.dispatchGenericMotionEvent(transformedEvent);
2120 transformedEvent.recycle();
2121 } else {
2122 event.offsetLocation(offsetX, offsetY);
2123 handled = child.dispatchGenericMotionEvent(event);
2124 event.offsetLocation(-offsetX, -offsetY);
2125 }
2126 return handled;
2127 }
2128
Jeff Browncb1404e2011-01-15 18:14:15 -08002129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08002131 if (mInputEventConsistencyVerifier != null) {
2132 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
2133 }
2134
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002135 // If the event targets the accessibility focused view and this is it, start
2136 // normal event dispatch. Maybe a descendant is what will handle the click.
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002137 if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002138 ev.setTargetAccessibilityFocus(false);
2139 }
2140
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002141 boolean handled = false;
2142 if (onFilterTouchEventForSecurity(ev)) {
2143 final int action = ev.getAction();
2144 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07002145
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002146 // Handle an initial down.
2147 if (actionMasked == MotionEvent.ACTION_DOWN) {
2148 // Throw away all previous state when starting a new touch gesture.
2149 // The framework may have dropped the up or cancel event for the previous gesture
2150 // due to an app switch, ANR, or some other state change.
2151 cancelAndClearTouchTargets(ev);
2152 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Adam Powellb08013c2010-09-16 16:28:11 -07002154
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002155 // Check for interception.
2156 final boolean intercepted;
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002157 if (actionMasked == MotionEvent.ACTION_DOWN
2158 || mFirstTouchTarget != null) {
2159 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
2160 if (!disallowIntercept) {
2161 intercepted = onInterceptTouchEvent(ev);
2162 ev.setAction(action); // restore action in case it was changed
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002163 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002164 intercepted = false;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002165 }
2166 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002167 // There are no touch targets and this action is not an initial down
2168 // so this view group continues to intercept touches.
2169 intercepted = true;
2170 }
2171
2172 // If intercepted, start normal event dispatch. Also if there is already
2173 // a view that is handling the gesture, do normal event dispatch.
2174 if (intercepted || mFirstTouchTarget != null) {
2175 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002176 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002177
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002178 // Check for cancelation.
2179 final boolean canceled = resetCancelNextUpFlag(this)
2180 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07002181
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002182 // Update list of touch targets for pointer down, if needed.
2183 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
2184 TouchTarget newTouchTarget = null;
2185 boolean alreadyDispatchedToNewTouchTarget = false;
2186 if (!canceled && !intercepted) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002187
2188 // If the event is targeting accessiiblity focus we give it to the
2189 // view that has accessibility focus and if it does not handle it
2190 // we clear the flag and dispatch the event to all children as usual.
2191 // We are looking up the accessibility focused host to avoid keeping
2192 // state since these events are very rare.
2193 View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
2194 ? findChildWithAccessibilityFocus() : null;
2195
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002196 if (actionMasked == MotionEvent.ACTION_DOWN
2197 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002198 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002199 final int actionIndex = ev.getActionIndex(); // always 0 for down
2200 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
2201 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07002202
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002203 // Clean up earlier touch targets for this pointer id in case they
2204 // have become out of sync.
2205 removePointersFromTouchTargets(idBitsToAssign);
2206
2207 final int childrenCount = mChildrenCount;
Chet Haase9c17fe62013-03-22 17:05:55 -07002208 if (newTouchTarget == null && childrenCount != 0) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002209 final float x = ev.getX(actionIndex);
2210 final float y = ev.getY(actionIndex);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002211 // Find a child that can receive the event.
2212 // Scan children from front to back.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002213 final ArrayList<View> preorderedList = buildTouchDispatchChildList();
Chris Craikab008f02014-05-23 17:55:03 -07002214 final boolean customOrder = preorderedList == null
2215 && isChildrenDrawingOrderEnabled();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002216 final View[] children = mChildren;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002217 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002218 final int childIndex = getAndVerifyPreorderedIndex(
2219 childrenCount, i, customOrder);
2220 final View child = getAndVerifyPreorderedView(
2221 preorderedList, children, childIndex);
Svetoslavc73cfa02015-02-09 17:14:28 -08002222
2223 // If there is a view that has accessibility focus we want it
2224 // to get the event first and if not handled we will perform a
2225 // normal dispatch. We may do a double iteration but this is
2226 // safer given the timeframe.
2227 if (childWithAccessibilityFocus != null) {
2228 if (childWithAccessibilityFocus != child) {
2229 continue;
2230 }
2231 childWithAccessibilityFocus = null;
2232 i = childrenCount - 1;
2233 }
2234
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002235 if (!canViewReceivePointerEvents(child)
2236 || !isTransformedTouchPointInView(x, y, child, null)) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002237 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002238 continue;
2239 }
2240
2241 newTouchTarget = getTouchTarget(child);
2242 if (newTouchTarget != null) {
2243 // Child is already receiving touch within its bounds.
2244 // Give it the new pointer in addition to the ones it is handling.
2245 newTouchTarget.pointerIdBits |= idBitsToAssign;
2246 break;
2247 }
2248
2249 resetCancelNextUpFlag(child);
2250 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
2251 // Child wants to receive touch within its bounds.
2252 mLastTouchDownTime = ev.getDownTime();
Chris Craikab008f02014-05-23 17:55:03 -07002253 if (preorderedList != null) {
2254 // childIndex points into presorted list, find original index
2255 for (int j = 0; j < childrenCount; j++) {
2256 if (children[childIndex] == mChildren[j]) {
2257 mLastTouchDownIndex = j;
2258 break;
2259 }
2260 }
2261 } else {
2262 mLastTouchDownIndex = childIndex;
2263 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002264 mLastTouchDownX = ev.getX();
2265 mLastTouchDownY = ev.getY();
2266 newTouchTarget = addTouchTarget(child, idBitsToAssign);
2267 alreadyDispatchedToNewTouchTarget = true;
2268 break;
2269 }
Svetoslavc73cfa02015-02-09 17:14:28 -08002270
2271 // The accessibility focus didn't handle the event, so clear
2272 // the flag and do a normal dispatch to all children.
2273 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002274 }
Chris Craikab008f02014-05-23 17:55:03 -07002275 if (preorderedList != null) preorderedList.clear();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002276 }
2277
2278 if (newTouchTarget == null && mFirstTouchTarget != null) {
2279 // Did not find a child to receive the event.
2280 // Assign the pointer to the least recently added target.
2281 newTouchTarget = mFirstTouchTarget;
2282 while (newTouchTarget.next != null) {
2283 newTouchTarget = newTouchTarget.next;
2284 }
2285 newTouchTarget.pointerIdBits |= idBitsToAssign;
2286 }
2287 }
2288 }
2289
2290 // Dispatch to touch targets.
2291 if (mFirstTouchTarget == null) {
2292 // No touch targets so treat this as an ordinary view.
2293 handled = dispatchTransformedTouchEvent(ev, canceled, null,
2294 TouchTarget.ALL_POINTER_IDS);
2295 } else {
2296 // Dispatch to touch targets, excluding the new touch target if we already
2297 // dispatched to it. Cancel touch targets if necessary.
2298 TouchTarget predecessor = null;
2299 TouchTarget target = mFirstTouchTarget;
2300 while (target != null) {
2301 final TouchTarget next = target.next;
2302 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
2303 handled = true;
2304 } else {
2305 final boolean cancelChild = resetCancelNextUpFlag(target.child)
Chet Haase9c17fe62013-03-22 17:05:55 -07002306 || intercepted;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002307 if (dispatchTransformedTouchEvent(ev, cancelChild,
2308 target.child, target.pointerIdBits)) {
2309 handled = true;
2310 }
2311 if (cancelChild) {
2312 if (predecessor == null) {
2313 mFirstTouchTarget = next;
2314 } else {
2315 predecessor.next = next;
2316 }
2317 target.recycle();
2318 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07002319 continue;
2320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002322 predecessor = target;
2323 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002325 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002326
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002327 // Update list of touch targets for pointer up or cancel, if needed.
2328 if (canceled
2329 || actionMasked == MotionEvent.ACTION_UP
2330 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
2331 resetTouchState();
2332 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
2333 final int actionIndex = ev.getActionIndex();
2334 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
2335 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337 }
Romain Guy8506ab42009-06-11 17:35:47 -07002338
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002339 if (!handled && mInputEventConsistencyVerifier != null) {
2340 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07002341 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002342 return handled;
2343 }
2344
Romain Guy469b1db2010-10-05 11:49:57 -07002345 /**
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002346 * Provide custom ordering of views in which the touch will be dispatched.
2347 *
2348 * This is called within a tight loop, so you are not allowed to allocate objects, including
2349 * the return array. Instead, you should return a pre-allocated list that will be cleared
2350 * after the dispatch is finished.
2351 * @hide
2352 */
2353 public ArrayList<View> buildTouchDispatchChildList() {
2354 return buildOrderedChildList();
2355 }
2356
2357 /**
Svetoslavc73cfa02015-02-09 17:14:28 -08002358 * Finds the child which has accessibility focus.
2359 *
2360 * @return The child that has focus.
2361 */
2362 private View findChildWithAccessibilityFocus() {
2363 ViewRootImpl viewRoot = getViewRootImpl();
2364 if (viewRoot == null) {
2365 return null;
2366 }
2367
2368 View current = viewRoot.getAccessibilityFocusedHost();
2369 if (current == null) {
2370 return null;
2371 }
2372
2373 ViewParent parent = current.getParent();
2374 while (parent instanceof View) {
2375 if (parent == this) {
2376 return current;
2377 }
2378 current = (View) parent;
2379 parent = current.getParent();
2380 }
2381
2382 return null;
2383 }
2384
2385 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002386 * Resets all touch state in preparation for a new cycle.
2387 */
2388 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002389 clearTouchTargets();
2390 resetCancelNextUpFlag(this);
2391 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
Adam Powell10ba2772014-04-15 09:46:51 -07002392 mNestedScrollAxes = SCROLL_AXIS_NONE;
Jeff Brown20e987b2010-08-23 12:01:02 -07002393 }
2394
Romain Guy469b1db2010-10-05 11:49:57 -07002395 /**
2396 * Resets the cancel next up flag.
2397 * Returns true if the flag was previously set.
2398 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002399 private static boolean resetCancelNextUpFlag(@NonNull View view) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002400 if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) {
2401 view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Jeff Brown20e987b2010-08-23 12:01:02 -07002402 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07002403 }
2404 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406
Romain Guy469b1db2010-10-05 11:49:57 -07002407 /**
2408 * Clears all touch targets.
2409 */
2410 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002411 TouchTarget target = mFirstTouchTarget;
2412 if (target != null) {
2413 do {
2414 TouchTarget next = target.next;
2415 target.recycle();
2416 target = next;
2417 } while (target != null);
2418 mFirstTouchTarget = null;
2419 }
2420 }
2421
Romain Guy469b1db2010-10-05 11:49:57 -07002422 /**
2423 * Cancels and clears all touch targets.
2424 */
2425 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002426 if (mFirstTouchTarget != null) {
2427 boolean syntheticEvent = false;
2428 if (event == null) {
2429 final long now = SystemClock.uptimeMillis();
2430 event = MotionEvent.obtain(now, now,
2431 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07002432 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07002433 syntheticEvent = true;
2434 }
2435
2436 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2437 resetCancelNextUpFlag(target.child);
2438 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
2439 }
2440 clearTouchTargets();
2441
2442 if (syntheticEvent) {
2443 event.recycle();
2444 }
2445 }
2446 }
2447
Romain Guy469b1db2010-10-05 11:49:57 -07002448 /**
2449 * Gets the touch target for specified child view.
2450 * Returns null if not found.
2451 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002452 private TouchTarget getTouchTarget(@NonNull View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002453 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2454 if (target.child == child) {
2455 return target;
2456 }
2457 }
2458 return null;
2459 }
2460
Romain Guy469b1db2010-10-05 11:49:57 -07002461 /**
2462 * Adds a touch target for specified child to the beginning of the list.
2463 * Assumes the target child is not already present.
2464 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002465 private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
2466 final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
Jeff Brown20e987b2010-08-23 12:01:02 -07002467 target.next = mFirstTouchTarget;
2468 mFirstTouchTarget = target;
2469 return target;
2470 }
2471
Romain Guy469b1db2010-10-05 11:49:57 -07002472 /**
2473 * Removes the pointer ids from consideration.
2474 */
2475 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002476 TouchTarget predecessor = null;
2477 TouchTarget target = mFirstTouchTarget;
2478 while (target != null) {
2479 final TouchTarget next = target.next;
2480 if ((target.pointerIdBits & pointerIdBits) != 0) {
2481 target.pointerIdBits &= ~pointerIdBits;
2482 if (target.pointerIdBits == 0) {
2483 if (predecessor == null) {
2484 mFirstTouchTarget = next;
2485 } else {
2486 predecessor.next = next;
2487 }
2488 target.recycle();
2489 target = next;
2490 continue;
2491 }
2492 }
2493 predecessor = target;
2494 target = next;
2495 }
2496 }
2497
Jeff Brown59a422e2012-04-19 15:19:19 -07002498 private void cancelTouchTarget(View view) {
2499 TouchTarget predecessor = null;
2500 TouchTarget target = mFirstTouchTarget;
2501 while (target != null) {
2502 final TouchTarget next = target.next;
2503 if (target.child == view) {
2504 if (predecessor == null) {
2505 mFirstTouchTarget = next;
2506 } else {
2507 predecessor.next = next;
2508 }
2509 target.recycle();
2510
2511 final long now = SystemClock.uptimeMillis();
2512 MotionEvent event = MotionEvent.obtain(now, now,
2513 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2514 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2515 view.dispatchTouchEvent(event);
2516 event.recycle();
2517 return;
2518 }
2519 predecessor = target;
2520 target = next;
2521 }
2522 }
2523
Romain Guy469b1db2010-10-05 11:49:57 -07002524 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002525 * Returns true if a child view can receive pointer events.
2526 * @hide
2527 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002528 private static boolean canViewReceivePointerEvents(@NonNull View child) {
Jeff Browna032cc02011-03-07 16:56:21 -08002529 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2530 || child.getAnimation() != null;
2531 }
2532
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002533 private float[] getTempPoint() {
2534 if (mTempPoint == null) {
2535 mTempPoint = new float[2];
2536 }
2537 return mTempPoint;
2538 }
2539
Jeff Browna032cc02011-03-07 16:56:21 -08002540 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002541 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07002542 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07002543 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07002544 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07002545 */
Adam Cohena32edd42010-10-26 10:35:01 -07002546 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07002547 PointF outLocalPoint) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002548 final float[] point = getTempPoint();
2549 point[0] = x;
2550 point[1] = y;
2551 transformPointToViewLocal(point, child);
2552 final boolean isInView = child.pointInView(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002553 if (isInView && outLocalPoint != null) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002554 outLocalPoint.set(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002555 }
2556 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07002557 }
2558
Romain Guy469b1db2010-10-05 11:49:57 -07002559 /**
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002560 * @hide
2561 */
2562 public void transformPointToViewLocal(float[] point, View child) {
2563 point[0] += mScrollX - child.mLeft;
2564 point[1] += mScrollY - child.mTop;
2565
2566 if (!child.hasIdentityMatrix()) {
2567 child.getInverseMatrix().mapPoints(point);
2568 }
2569 }
2570
2571 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002572 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07002573 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07002574 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
2575 */
2576 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07002577 View child, int desiredPointerIdBits) {
2578 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07002579
Jeff Brown20e987b2010-08-23 12:01:02 -07002580 // Canceling motions is a special case. We don't need to perform any transformations
2581 // or filtering. The important part is the action, not the contents.
2582 final int oldAction = event.getAction();
2583 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
2584 event.setAction(MotionEvent.ACTION_CANCEL);
2585 if (child == null) {
2586 handled = super.dispatchTouchEvent(event);
2587 } else {
2588 handled = child.dispatchTouchEvent(event);
2589 }
2590 event.setAction(oldAction);
2591 return handled;
2592 }
Adam Powell2b342f02010-08-18 18:14:13 -07002593
Jeff Brown20e987b2010-08-23 12:01:02 -07002594 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002595 final int oldPointerIdBits = event.getPointerIdBits();
2596 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07002597
Jeff Brown20e987b2010-08-23 12:01:02 -07002598 // If for some reason we ended up in an inconsistent state where it looks like we
2599 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002600 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002601 return false;
2602 }
Adam Powell2b342f02010-08-18 18:14:13 -07002603
Jeff Brown20e987b2010-08-23 12:01:02 -07002604 // If the number of pointers is the same and we don't need to perform any fancy
2605 // irreversible transformations, then we can reuse the motion event for this
2606 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002607 // Otherwise we need to make a copy.
2608 final MotionEvent transformedEvent;
2609 if (newPointerIdBits == oldPointerIdBits) {
2610 if (child == null || child.hasIdentityMatrix()) {
2611 if (child == null) {
2612 handled = super.dispatchTouchEvent(event);
2613 } else {
2614 final float offsetX = mScrollX - child.mLeft;
2615 final float offsetY = mScrollY - child.mTop;
2616 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07002617
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002618 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07002619
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002620 event.offsetLocation(-offsetX, -offsetY);
2621 }
2622 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07002623 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002624 transformedEvent = MotionEvent.obtain(event);
2625 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002626 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07002627 }
2628
Jeff Brown20e987b2010-08-23 12:01:02 -07002629 // Perform any necessary transformations and dispatch.
2630 if (child == null) {
2631 handled = super.dispatchTouchEvent(transformedEvent);
2632 } else {
2633 final float offsetX = mScrollX - child.mLeft;
2634 final float offsetY = mScrollY - child.mTop;
2635 transformedEvent.offsetLocation(offsetX, offsetY);
2636 if (! child.hasIdentityMatrix()) {
2637 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07002638 }
2639
Jeff Brown20e987b2010-08-23 12:01:02 -07002640 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07002641 }
2642
Jeff Brown20e987b2010-08-23 12:01:02 -07002643 // Done.
2644 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07002645 return handled;
2646 }
2647
Romain Guy469b1db2010-10-05 11:49:57 -07002648 /**
Adam Powell2b342f02010-08-18 18:14:13 -07002649 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08002650 * dispatch. This behavior is enabled by default for applications that target an
2651 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07002652 *
2653 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
2654 * views depending on where each pointer initially went down. This allows for user interactions
2655 * such as scrolling two panes of content independently, chording of buttons, and performing
2656 * independent gestures on different pieces of content.
2657 *
2658 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2659 * child views. <code>false</code> to only allow one child view to be the target of
2660 * any MotionEvent received by this ViewGroup.
Scott Main27a85082013-06-10 10:39:48 -07002661 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
Adam Powell2b342f02010-08-18 18:14:13 -07002662 */
2663 public void setMotionEventSplittingEnabled(boolean split) {
2664 // TODO Applications really shouldn't change this setting mid-touch event,
2665 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2666 // with gestures in progress when this is changed.
2667 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07002668 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2669 } else {
2670 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07002671 }
2672 }
2673
2674 /**
Jeff Brown995e7742010-12-22 16:59:36 -08002675 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07002676 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2677 */
2678 public boolean isMotionEventSplittingEnabled() {
2679 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2680 }
2681
2682 /**
George Mount0a778ed2013-12-13 13:35:36 -08002683 * Returns true if this ViewGroup should be considered as a single entity for removal
2684 * when executing an Activity transition. If this is false, child elements will move
2685 * individually during the transition.
George Mount427c6412014-11-05 16:45:36 -08002686 *
George Mount0a778ed2013-12-13 13:35:36 -08002687 * @return True if the ViewGroup should be acted on together during an Activity transition.
George Mount427c6412014-11-05 16:45:36 -08002688 * The default value is true when there is a non-null background or if
2689 * {@link #getTransitionName()} is not null or if a
2690 * non-null {@link android.view.ViewOutlineProvider} other than
2691 * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to
2692 * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise.
George Mount0a778ed2013-12-13 13:35:36 -08002693 */
2694 public boolean isTransitionGroup() {
2695 if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) {
2696 return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0);
2697 } else {
George Mount427c6412014-11-05 16:45:36 -08002698 final ViewOutlineProvider outlineProvider = getOutlineProvider();
2699 return getBackground() != null || getTransitionName() != null ||
2700 (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND);
George Mount0a778ed2013-12-13 13:35:36 -08002701 }
2702 }
2703
2704 /**
2705 * Changes whether or not this ViewGroup should be treated as a single entity during
George Mount31a21722014-03-24 17:44:36 -07002706 * Activity Transitions.
George Mount0a778ed2013-12-13 13:35:36 -08002707 * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
2708 * in Activity transitions. If false, the ViewGroup won't transition,
2709 * only its children. If true, the entire ViewGroup will transition
2710 * together.
George Mount62ab9b72014-05-02 13:51:17 -07002711 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity,
2712 * android.util.Pair[])
George Mount0a778ed2013-12-13 13:35:36 -08002713 */
2714 public void setTransitionGroup(boolean isTransitionGroup) {
2715 mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET;
2716 if (isTransitionGroup) {
2717 mGroupFlags |= FLAG_IS_TRANSITION_GROUP;
2718 } else {
2719 mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP;
2720 }
2721 }
2722
Alan Viverettebe463f22016-01-21 10:50:10 -05002723 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07002725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2727 // We're already in this state, assume our ancestors are too
2728 return;
2729 }
Romain Guy8506ab42009-06-11 17:35:47 -07002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 if (disallowIntercept) {
2732 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2733 } else {
2734 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2735 }
Romain Guy8506ab42009-06-11 17:35:47 -07002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 // Pass it up to our parent
2738 if (mParent != null) {
2739 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2740 }
2741 }
2742
2743 /**
2744 * Implement this method to intercept all touch screen motion events. This
2745 * allows you to watch events as they are dispatched to your children, and
2746 * take ownership of the current gesture at any point.
2747 *
2748 * <p>Using this function takes some care, as it has a fairly complicated
2749 * interaction with {@link View#onTouchEvent(MotionEvent)
2750 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2751 * that method as well as this one in the correct way. Events will be
2752 * received in the following order:
2753 *
2754 * <ol>
2755 * <li> You will receive the down event here.
2756 * <li> The down event will be handled either by a child of this view
2757 * group, or given to your own onTouchEvent() method to handle; this means
2758 * you should implement onTouchEvent() to return true, so you will
2759 * continue to see the rest of the gesture (instead of looking for
2760 * a parent view to handle it). Also, by returning true from
2761 * onTouchEvent(), you will not receive any following
2762 * events in onInterceptTouchEvent() and all touch processing must
2763 * happen in onTouchEvent() like normal.
2764 * <li> For as long as you return false from this function, each following
2765 * event (up to and including the final up) will be delivered first here
2766 * and then to the target's onTouchEvent().
2767 * <li> If you return true from here, you will not receive any
2768 * following events: the target view will receive the same event but
2769 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2770 * events will be delivered to your onTouchEvent() method and no longer
2771 * appear here.
2772 * </ol>
2773 *
2774 * @param ev The motion event being dispatched down the hierarchy.
2775 * @return Return true to steal motion events from the children and have
2776 * them dispatched to this ViewGroup through onTouchEvent().
2777 * The current target will receive an ACTION_CANCEL event, and no further
2778 * messages will be delivered here.
2779 */
2780 public boolean onInterceptTouchEvent(MotionEvent ev) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002781 if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
2782 && ev.getAction() == MotionEvent.ACTION_DOWN
2783 && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
2784 && isOnScrollbarThumb(ev.getX(), ev.getY())) {
2785 return true;
2786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 return false;
2788 }
2789
2790 /**
2791 * {@inheritDoc}
2792 *
2793 * Looks for a view to give focus to respecting the setting specified by
2794 * {@link #getDescendantFocusability()}.
2795 *
2796 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2797 * find focus within the children of this group when appropriate.
2798 *
2799 * @see #FOCUS_BEFORE_DESCENDANTS
2800 * @see #FOCUS_AFTER_DESCENDANTS
2801 * @see #FOCUS_BLOCK_DESCENDANTS
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002802 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 */
2804 @Override
2805 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2806 if (DBG) {
2807 System.out.println(this + " ViewGroup.requestFocus direction="
2808 + direction);
2809 }
2810 int descendantFocusability = getDescendantFocusability();
2811
2812 switch (descendantFocusability) {
2813 case FOCUS_BLOCK_DESCENDANTS:
2814 return super.requestFocus(direction, previouslyFocusedRect);
2815 case FOCUS_BEFORE_DESCENDANTS: {
2816 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2817 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2818 }
2819 case FOCUS_AFTER_DESCENDANTS: {
2820 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2821 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2822 }
2823 default:
2824 throw new IllegalStateException("descendant focusability must be "
2825 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2826 + "but is " + descendantFocusability);
2827 }
2828 }
2829
2830 /**
2831 * Look for a descendant to call {@link View#requestFocus} on.
2832 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2833 * when it wants to request focus within its children. Override this to
2834 * customize how your {@link ViewGroup} requests focus within its children.
2835 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2836 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2837 * to give a finer grained hint about where focus is coming from. May be null
2838 * if there is no hint.
2839 * @return Whether focus was taken.
2840 */
2841 @SuppressWarnings({"ConstantConditions"})
2842 protected boolean onRequestFocusInDescendants(int direction,
2843 Rect previouslyFocusedRect) {
2844 int index;
2845 int increment;
2846 int end;
2847 int count = mChildrenCount;
2848 if ((direction & FOCUS_FORWARD) != 0) {
2849 index = 0;
2850 increment = 1;
2851 end = count;
2852 } else {
2853 index = count - 1;
2854 increment = -1;
2855 end = -1;
2856 }
2857 final View[] children = mChildren;
2858 for (int i = index; i != end; i += increment) {
2859 View child = children[i];
2860 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2861 if (child.requestFocus(direction, previouslyFocusedRect)) {
2862 return true;
2863 }
2864 }
2865 }
2866 return false;
2867 }
Chet Haase5c13d892010-10-08 08:37:55 -07002868
Romain Guya440b002010-02-24 15:57:54 -08002869 /**
2870 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002871 *
Romain Guydcc490f2010-02-24 17:59:35 -08002872 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002873 */
2874 @Override
2875 public void dispatchStartTemporaryDetach() {
2876 super.dispatchStartTemporaryDetach();
2877 final int count = mChildrenCount;
2878 final View[] children = mChildren;
2879 for (int i = 0; i < count; i++) {
2880 children[i].dispatchStartTemporaryDetach();
2881 }
2882 }
Chet Haase5c13d892010-10-08 08:37:55 -07002883
Romain Guya440b002010-02-24 15:57:54 -08002884 /**
2885 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002886 *
Romain Guydcc490f2010-02-24 17:59:35 -08002887 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002888 */
2889 @Override
2890 public void dispatchFinishTemporaryDetach() {
2891 super.dispatchFinishTemporaryDetach();
2892 final int count = mChildrenCount;
2893 final View[] children = mChildren;
2894 for (int i = 0; i < count; i++) {
2895 children[i].dispatchFinishTemporaryDetach();
2896 }
2897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 @Override
2900 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07002901 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07002903 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 final int count = mChildrenCount;
2906 final View[] children = mChildren;
2907 for (int i = 0; i < count; i++) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002908 final View child = children[i];
2909 child.dispatchAttachedToWindow(info,
Adam Powelleb2b0af2015-05-20 18:26:35 -07002910 combineVisibility(visibility, child.getVisibility()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
Chet Haasec633d2f2015-04-07 10:29:39 -07002912 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
2913 for (int i = 0; i < transientCount; ++i) {
2914 View view = mTransientViews.get(i);
Adam Powelleb2b0af2015-05-20 18:26:35 -07002915 view.dispatchAttachedToWindow(info,
2916 combineVisibility(visibility, view.getVisibility()));
Chet Haasec633d2f2015-04-07 10:29:39 -07002917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
2919
svetoslavganov75986cf2009-05-14 22:28:01 -07002920 @Override
Romain Guybb9908b2012-03-08 11:14:07 -08002921 void dispatchScreenStateChanged(int screenState) {
2922 super.dispatchScreenStateChanged(screenState);
2923
2924 final int count = mChildrenCount;
2925 final View[] children = mChildren;
2926 for (int i = 0; i < count; i++) {
2927 children[i].dispatchScreenStateChanged(screenState);
2928 }
2929 }
2930
Alan Viverettea54956a2015-01-07 16:05:02 -08002931 /** @hide */
Romain Guybb9908b2012-03-08 11:14:07 -08002932 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002933 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002934 boolean handled = false;
2935 if (includeForAccessibility()) {
2936 handled = super.dispatchPopulateAccessibilityEventInternal(event);
2937 if (handled) {
2938 return handled;
2939 }
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002940 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002941 // Let our children have a shot in populating the event.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002942 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002943 try {
2944 final int childCount = children.getChildCount();
2945 for (int i = 0; i < childCount; i++) {
2946 View child = children.getChildAt(i);
2947 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2948 handled = child.dispatchPopulateAccessibilityEvent(event);
2949 if (handled) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002950 return handled;
2951 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002952 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002953 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002954 } finally {
2955 children.recycle();
svetoslavganov75986cf2009-05-14 22:28:01 -07002956 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002957 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07002958 }
2959
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002960 /**
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002961 * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002962 * adds in all child views of the view group, in addition to calling the default View
2963 * implementation.
2964 */
Alan Viverettebe463f22016-01-21 10:50:10 -05002965 @Override
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002966 public void dispatchProvideStructure(ViewStructure structure) {
2967 super.dispatchProvideStructure(structure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002968 if (!isAssistBlocked()) {
2969 if (structure.getChildCount() == 0) {
2970 final int childrenCount = getChildCount();
2971 if (childrenCount > 0) {
2972 structure.setChildCount(childrenCount);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002973 ArrayList<View> preorderedList = buildOrderedChildList();
2974 boolean customOrder = preorderedList == null
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002975 && isChildrenDrawingOrderEnabled();
2976 final View[] children = mChildren;
2977 for (int i=0; i<childrenCount; i++) {
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002978 int childIndex;
2979 try {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002980 childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002981 } catch (IndexOutOfBoundsException e) {
2982 childIndex = i;
2983 if (mContext.getApplicationInfo().targetSdkVersion
2984 < Build.VERSION_CODES.M) {
2985 Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ "
2986 + i + " of " + childrenCount, e);
2987 // At least one app is failing when we call getChildDrawingOrder
2988 // at this point, so deal semi-gracefully with it by falling back
2989 // on the basic order.
2990 customOrder = false;
2991 if (i > 0) {
2992 // If we failed at the first index, there really isn't
2993 // anything to do -- we will just proceed with the simple
2994 // sequence order.
2995 // Otherwise, we failed in the middle, so need to come up
2996 // with an order for the remaining indices and use that.
2997 // Failed at the first one, easy peasy.
2998 int[] permutation = new int[childrenCount];
2999 SparseBooleanArray usedIndices = new SparseBooleanArray();
3000 // Go back and collected the indices we have done so far.
3001 for (int j=0; j<i; j++) {
3002 permutation[j] = getChildDrawingOrder(childrenCount, j);
3003 usedIndices.put(permutation[j], true);
3004 }
3005 // Fill in the remaining indices with indices that have not
3006 // yet been used.
3007 int nextIndex = 0;
3008 for (int j=i; j<childrenCount; j++) {
3009 while (usedIndices.get(nextIndex, false)) {
3010 nextIndex++;
3011 }
3012 permutation[j] = nextIndex;
3013 nextIndex++;
3014 }
3015 // Build the final view list.
3016 preorderedList = new ArrayList<>(childrenCount);
3017 for (int j=0; j<childrenCount; j++) {
3018 preorderedList.add(children[permutation[j]]);
3019 }
3020 }
3021 } else {
3022 throw e;
3023 }
3024 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05003025
3026 final View child = getAndVerifyPreorderedView(
3027 preorderedList, children, childIndex);
3028 final ViewStructure cstructure = structure.newChild(i);
Dianne Hackborn49b043f2015-05-07 14:21:38 -07003029 child.dispatchProvideStructure(cstructure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07003030 }
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07003031 }
3032 }
3033 }
3034 }
3035
Alan Viverettea7b85e62016-01-22 10:14:02 -05003036 private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,
3037 int childIndex) {
3038 final View child;
3039 if (preorderedList != null) {
3040 child = preorderedList.get(childIndex);
3041 if (child == null) {
3042 throw new RuntimeException("Invalid preorderedList contained null child at index "
3043 + childIndex);
3044 }
3045 } else {
3046 child = children[childIndex];
3047 }
3048 return child;
3049 }
3050
Alan Viverettea54956a2015-01-07 16:05:02 -08003051 /** @hide */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003052 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08003053 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003054 super.onInitializeAccessibilityNodeInfoInternal(info);
Svet Ganov55bdb102015-02-06 12:41:17 -08003055 if (getAccessibilityNodeProvider() != null) {
3056 return;
3057 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003058 if (mAttachInfo != null) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003059 final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003060 childrenForAccessibility.clear();
3061 addChildrenForAccessibility(childrenForAccessibility);
3062 final int childrenForAccessibilityCount = childrenForAccessibility.size();
3063 for (int i = 0; i < childrenForAccessibilityCount; i++) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003064 final View child = childrenForAccessibility.get(i);
3065 info.addChildUnchecked(child);
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07003066 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003067 childrenForAccessibility.clear();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003068 }
3069 }
3070
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003071 @Override
Maxim Bogatovf399af32015-06-16 15:15:49 -07003072 public CharSequence getAccessibilityClassName() {
3073 return ViewGroup.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003074 }
3075
Svetoslav Ganov42138042012-03-20 11:51:39 -07003076 @Override
Alan Viverette77e9a282013-09-12 17:16:09 -07003077 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
3078 // If this is a live region, we should send a subtree change event
3079 // from this view. Otherwise, we can let it propagate up.
3080 if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) {
3081 notifyViewAccessibilityStateChangedIfNeeded(changeType);
3082 } else if (mParent != null) {
Adam Powell504a10f2013-07-11 15:25:59 -07003083 try {
Alan Viverette77e9a282013-09-12 17:16:09 -07003084 mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType);
Adam Powell504a10f2013-07-11 15:25:59 -07003085 } catch (AbstractMethodError e) {
3086 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
3087 " does not fully implement ViewParent", e);
3088 }
Svetoslav6254f482013-06-04 17:22:14 -07003089 }
3090 }
3091
3092 @Override
3093 void resetSubtreeAccessibilityStateChanged() {
3094 super.resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003095 View[] children = mChildren;
3096 final int childCount = mChildrenCount;
3097 for (int i = 0; i < childCount; i++) {
Svetoslav6254f482013-06-04 17:22:14 -07003098 children[i].resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003099 }
3100 }
3101
3102 /**
Phil Weaver1f222542016-01-08 11:49:32 -08003103 * Counts the number of children of this View that will be sent to an accessibility service.
3104 *
3105 * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View
3106 * would have.
3107 */
3108 int getNumChildrenForAccessibility() {
3109 int numChildrenForAccessibility = 0;
3110 for (int i = 0; i < getChildCount(); i++) {
3111 View child = getChildAt(i);
3112 if (child.includeForAccessibility()) {
3113 numChildrenForAccessibility++;
3114 } else if (child instanceof ViewGroup) {
3115 numChildrenForAccessibility += ((ViewGroup) child)
3116 .getNumChildrenForAccessibility();
3117 }
3118 }
3119 return numChildrenForAccessibility;
3120 }
3121
3122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 * {@inheritDoc}
Adam Powellb6ab0982015-01-07 17:00:12 -08003124 *
3125 * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p>
3126 *
3127 * @param target The target view dispatching this action
3128 * @param action Action being performed; see
3129 * {@link android.view.accessibility.AccessibilityNodeInfo}
3130 * @param args Optional action arguments
3131 * @return false by default. Subclasses should return true if they handle the event.
3132 */
3133 @Override
3134 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
3135 return false;
3136 }
3137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 @Override
3139 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07003140 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 // dispatching motion events to a child; we need to get rid of that
3142 // child to avoid dispatching events to it after the window is torn
3143 // down. To make sure we keep the child in a consistent state, we
3144 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07003145 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146
Jeff Brown59a422e2012-04-19 15:19:19 -07003147 // Similarly, set ACTION_EXIT to all hover targets and clear them.
3148 exitHoverTargets();
3149
Chet Haase9c087442011-01-12 16:20:16 -08003150 // In case view is detached while transition is running
Chet Haaseb9895022013-04-02 15:10:58 -07003151 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08003152
Christopher Tate86cab1b2011-01-13 20:28:55 -08003153 // Tear down our drag tracking
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07003154 mChildrenInterestedInDrag = null;
3155 mIsInterestedInDrag = false;
3156 if (mCurrentDragStartEvent != null) {
3157 mCurrentDragStartEvent.recycle();
3158 mCurrentDragStartEvent = null;
Christopher Tate86cab1b2011-01-13 20:28:55 -08003159 }
3160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 final int count = mChildrenCount;
3162 final View[] children = mChildren;
3163 for (int i = 0; i < count; i++) {
3164 children[i].dispatchDetachedFromWindow();
3165 }
John Reckca7a9da2014-03-05 16:29:07 -08003166 clearDisappearingChildren();
Chet Haasec633d2f2015-04-07 10:29:39 -07003167 final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size();
3168 for (int i = 0; i < transientCount; ++i) {
3169 View view = mTransientViews.get(i);
3170 view.dispatchDetachedFromWindow();
3171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 super.dispatchDetachedFromWindow();
3173 }
3174
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003175 /**
3176 * @hide
3177 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 @Override
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003179 protected void internalSetPadding(int left, int top, int right, int bottom) {
Romain Guy2440e672012-08-07 14:43:43 -07003180 super.internalSetPadding(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181
Romain Guy13f35f32011-03-24 12:03:17 -07003182 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 mGroupFlags |= FLAG_PADDING_NOT_NULL;
3184 } else {
3185 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
3186 }
3187 }
3188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 @Override
3190 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
3191 super.dispatchSaveInstanceState(container);
3192 final int count = mChildrenCount;
3193 final View[] children = mChildren;
3194 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003195 View c = children[i];
3196 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3197 c.dispatchSaveInstanceState(container);
3198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200 }
3201
3202 /**
Romain Guy9fc27812011-04-27 14:21:41 -07003203 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
3204 * to only this view, not to its children. For use when overriding
3205 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
3206 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 *
3208 * @param container the container
3209 */
3210 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
3211 super.dispatchSaveInstanceState(container);
3212 }
3213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 @Override
3215 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
3216 super.dispatchRestoreInstanceState(container);
3217 final int count = mChildrenCount;
3218 final View[] children = mChildren;
3219 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003220 View c = children[i];
3221 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3222 c.dispatchRestoreInstanceState(container);
3223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 }
3226
3227 /**
Romain Guy02739a82011-05-16 11:43:18 -07003228 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
3229 * to only this view, not to its children. For use when overriding
3230 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
3231 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 *
3233 * @param container the container
3234 */
3235 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
3236 super.dispatchRestoreInstanceState(container);
3237 }
3238
3239 /**
3240 * Enables or disables the drawing cache for each child of this view group.
3241 *
3242 * @param enabled true to enable the cache, false to dispose of it
3243 */
3244 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
3245 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
3246 final View[] children = mChildren;
3247 final int count = mChildrenCount;
3248 for (int i = 0; i < count; i++) {
3249 children[i].setDrawingCacheEnabled(enabled);
3250 }
3251 }
3252 }
3253
3254 @Override
Romain Guy223ff5c2010-03-02 17:07:47 -08003255 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003256 int count = mChildrenCount;
3257 int[] visibilities = null;
3258
Romain Guy223ff5c2010-03-02 17:07:47 -08003259 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003260 visibilities = new int[count];
3261 for (int i = 0; i < count; i++) {
3262 View child = getChildAt(i);
3263 visibilities[i] = child.getVisibility();
3264 if (visibilities[i] == View.VISIBLE) {
3265 child.setVisibility(INVISIBLE);
3266 }
3267 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003268 }
3269
3270 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07003271
3272 if (skipChildren) {
3273 for (int i = 0; i < count; i++) {
3274 getChildAt(i).setVisibility(visibilities[i]);
Chet Haase5c13d892010-10-08 08:37:55 -07003275 }
Romain Guy65554f22010-03-22 18:58:21 -07003276 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003277
3278 return b;
3279 }
3280
Philip Milne7b757812012-09-19 18:13:44 -07003281 /** Return true if this ViewGroup is laying out using optical bounds. */
3282 boolean isLayoutModeOptical() {
3283 return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS;
3284 }
Romain Guycbc67742012-04-27 16:12:57 -07003285
Alan Viverettebe463f22016-01-21 10:50:10 -05003286 @Override
Philip Milne7b757812012-09-19 18:13:44 -07003287 Insets computeOpticalInsets() {
3288 if (isLayoutModeOptical()) {
3289 int left = 0;
3290 int top = 0;
3291 int right = 0;
3292 int bottom = 0;
3293 for (int i = 0; i < mChildrenCount; i++) {
3294 View child = getChildAt(i);
3295 if (child.getVisibility() == VISIBLE) {
3296 Insets insets = child.getOpticalInsets();
3297 left = Math.max(left, insets.left);
3298 top = Math.max(top, insets.top);
3299 right = Math.max(right, insets.right);
3300 bottom = Math.max(bottom, insets.bottom);
3301 }
3302 }
3303 return Insets.of(left, top, right, bottom);
3304 } else {
3305 return Insets.NONE;
3306 }
3307 }
3308
3309 private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
3310 if (x1 != x2 && y1 != y2) {
3311 if (x1 > x2) {
3312 int tmp = x1; x1 = x2; x2 = tmp;
3313 }
3314 if (y1 > y2) {
3315 int tmp = y1; y1 = y2; y2 = tmp;
3316 }
3317 canvas.drawRect(x1, y1, x2, y2, paint);
3318 }
3319 }
3320
3321 private static int sign(int x) {
3322 return (x >= 0) ? 1 : -1;
3323 }
3324
3325 private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) {
3326 fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy));
3327 fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
3328 }
3329
3330 private int dipsToPixels(int dips) {
3331 float scale = getContext().getResources().getDisplayMetrics().density;
3332 return (int) (dips * scale + 0.5f);
3333 }
3334
Romain Guy6410c0a2013-06-17 11:21:58 -07003335 private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
3336 int lineLength, int lineWidth) {
Philip Milne7b757812012-09-19 18:13:44 -07003337 drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
3338 drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth);
3339 drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth);
3340 drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth);
3341 }
3342
3343 private static void fillDifference(Canvas canvas,
3344 int x2, int y2, int x3, int y3,
3345 int dx1, int dy1, int dx2, int dy2, Paint paint) {
3346 int x1 = x2 - dx1;
3347 int y1 = y2 - dy1;
3348
3349 int x4 = x3 + dx2;
3350 int y4 = y3 + dy2;
3351
3352 fillRect(canvas, paint, x1, y1, x4, y2);
3353 fillRect(canvas, paint, x1, y2, x2, y3);
3354 fillRect(canvas, paint, x3, y2, x4, y3);
3355 fillRect(canvas, paint, x1, y3, x4, y4);
Philip Milne10ca24a2012-04-23 15:38:27 -07003356 }
3357
3358 /**
3359 * @hide
3360 */
Philip Milne7b757812012-09-19 18:13:44 -07003361 protected void onDebugDrawMargins(Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07003362 for (int i = 0; i < getChildCount(); i++) {
3363 View c = getChildAt(i);
Philip Milne7b757812012-09-19 18:13:44 -07003364 c.getLayoutParams().onDebugDraw(c, canvas, paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07003365 }
3366 }
3367
3368 /**
3369 * @hide
3370 */
3371 protected void onDebugDraw(Canvas canvas) {
Philip Milne7b757812012-09-19 18:13:44 -07003372 Paint paint = getDebugPaint();
3373
Philip Milne10ca24a2012-04-23 15:38:27 -07003374 // Draw optical bounds
Philip Milne7b757812012-09-19 18:13:44 -07003375 {
3376 paint.setColor(Color.RED);
3377 paint.setStyle(Paint.Style.STROKE);
Philip Milne7b757812012-09-19 18:13:44 -07003378
Philip Milne10ca24a2012-04-23 15:38:27 -07003379 for (int i = 0; i < getChildCount(); i++) {
3380 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003381 if (c.getVisibility() != View.GONE) {
3382 Insets insets = c.getOpticalInsets();
Philip Milne7b757812012-09-19 18:13:44 -07003383
Chris Craika1dab8b2015-06-30 13:51:25 -07003384 drawRect(canvas, paint,
3385 c.getLeft() + insets.left,
3386 c.getTop() + insets.top,
3387 c.getRight() - insets.right - 1,
3388 c.getBottom() - insets.bottom - 1);
3389 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003390 }
3391 }
3392
Philip Milne10ca24a2012-04-23 15:38:27 -07003393 // Draw margins
Philip Milne7b757812012-09-19 18:13:44 -07003394 {
3395 paint.setColor(Color.argb(63, 255, 0, 255));
3396 paint.setStyle(Paint.Style.FILL);
Philip Milne604f4402012-04-24 19:27:11 -07003397
Philip Milne7b757812012-09-19 18:13:44 -07003398 onDebugDrawMargins(canvas, paint);
3399 }
3400
3401 // Draw clip bounds
3402 {
3403 paint.setColor(Color.rgb(63, 127, 255));
3404 paint.setStyle(Paint.Style.FILL);
3405
3406 int lineLength = dipsToPixels(8);
3407 int lineWidth = dipsToPixels(1);
3408 for (int i = 0; i < getChildCount(); i++) {
3409 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003410 if (c.getVisibility() != View.GONE) {
3411 drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(),
3412 paint, lineLength, lineWidth);
3413 }
Philip Milne7b757812012-09-19 18:13:44 -07003414 }
Philip Milne604f4402012-04-24 19:27:11 -07003415 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 @Override
3419 protected void dispatchDraw(Canvas canvas) {
Chris Craika753f4c2014-07-24 12:39:17 -07003420 boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
Chris Craikab008f02014-05-23 17:55:03 -07003421 final int childrenCount = mChildrenCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 final View[] children = mChildren;
3423 int flags = mGroupFlags;
3424
3425 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
Romain Guy0d9275e2010-10-26 14:22:30 -07003426 final boolean buildCache = !isHardwareAccelerated();
Chris Craikab008f02014-05-23 17:55:03 -07003427 for (int i = 0; i < childrenCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 final View child = children[i];
3429 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
3430 final LayoutParams params = child.getLayoutParams();
Chris Craikab008f02014-05-23 17:55:03 -07003431 attachLayoutAnimationParameters(child, params, i, childrenCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 bindLayoutAnimation(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 }
3434 }
3435
3436 final LayoutAnimationController controller = mLayoutAnimationController;
3437 if (controller.willOverlap()) {
3438 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
3439 }
3440
3441 controller.start();
3442
3443 mGroupFlags &= ~FLAG_RUN_ANIMATION;
3444 mGroupFlags &= ~FLAG_ANIMATION_DONE;
3445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 if (mAnimationListener != null) {
3447 mAnimationListener.onAnimationStart(controller.getAnimation());
3448 }
3449 }
3450
Selim Cinek19cadc22014-04-16 17:27:19 +02003451 int clipSaveCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3453 if (clipToPadding) {
Chris Craike4cf1522014-08-04 17:55:22 -07003454 clipSaveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07003455 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
3456 mScrollX + mRight - mLeft - mPaddingRight,
3457 mScrollY + mBottom - mTop - mPaddingBottom);
Selim Cinek19cadc22014-04-16 17:27:19 +02003458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 // We will draw our child's animation, let's reset the flag
Dianne Hackborn4702a852012-08-17 15:18:29 -07003461 mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
3463
3464 boolean more = false;
3465 final long drawingTime = getDrawingTime();
3466
Chris Craik8afd0f22014-08-21 17:41:57 -07003467 if (usingRenderNodeProperties) canvas.insertReorderBarrier();
Chet Haasec633d2f2015-04-07 10:29:39 -07003468 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
3469 int transientIndex = transientCount != 0 ? 0 : -1;
Chris Craikab008f02014-05-23 17:55:03 -07003470 // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
3471 // draw reordering internally
Chris Craika753f4c2014-07-24 12:39:17 -07003472 final ArrayList<View> preorderedList = usingRenderNodeProperties
Chris Craikab008f02014-05-23 17:55:03 -07003473 ? null : buildOrderedChildList();
3474 final boolean customOrder = preorderedList == null
3475 && isChildrenDrawingOrderEnabled();
3476 for (int i = 0; i < childrenCount; i++) {
Chet Haasec633d2f2015-04-07 10:29:39 -07003477 while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
3478 final View transientChild = mTransientViews.get(transientIndex);
3479 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3480 transientChild.getAnimation() != null) {
3481 more |= drawChild(canvas, transientChild, drawingTime);
3482 }
3483 transientIndex++;
3484 if (transientIndex >= transientCount) {
3485 transientIndex = -1;
3486 }
3487 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05003488
3489 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3490 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Chris Craikab008f02014-05-23 17:55:03 -07003491 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
3492 more |= drawChild(canvas, child, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494 }
Chet Haasec633d2f2015-04-07 10:29:39 -07003495 while (transientIndex >= 0) {
3496 // there may be additional transient views after the normal views
3497 final View transientChild = mTransientViews.get(transientIndex);
3498 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3499 transientChild.getAnimation() != null) {
3500 more |= drawChild(canvas, transientChild, drawingTime);
3501 }
3502 transientIndex++;
3503 if (transientIndex >= transientCount) {
3504 break;
3505 }
3506 }
Chris Craikab008f02014-05-23 17:55:03 -07003507 if (preorderedList != null) preorderedList.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508
3509 // Draw any disappearing views that have animations
3510 if (mDisappearingChildren != null) {
3511 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3512 final int disappearingCount = disappearingChildren.size() - 1;
3513 // Go backwards -- we may delete as animations finish
3514 for (int i = disappearingCount; i >= 0; i--) {
3515 final View child = disappearingChildren.get(i);
3516 more |= drawChild(canvas, child, drawingTime);
3517 }
3518 }
Chris Craik8afd0f22014-08-21 17:41:57 -07003519 if (usingRenderNodeProperties) canvas.insertInorderBarrier();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520
Philip Milne10ca24a2012-04-23 15:38:27 -07003521 if (debugDraw()) {
3522 onDebugDraw(canvas);
3523 }
3524
Chris Craike4cf1522014-08-04 17:55:22 -07003525 if (clipToPadding) {
Selim Cinek19cadc22014-04-16 17:27:19 +02003526 canvas.restoreToCount(clipSaveCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 }
3528
3529 // mGroupFlags might have been updated by drawChild()
3530 flags = mGroupFlags;
3531
3532 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08003533 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 }
3535
3536 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
3537 mLayoutAnimationController.isDone() && !more) {
3538 // We want to erase the drawing cache and notify the listener after the
3539 // next frame is drawn because one extra invalidate() is caused by
3540 // drawChild() after the animation is over
3541 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
3542 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05003543 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 public void run() {
3545 notifyAnimationListener();
3546 }
3547 };
3548 post(end);
3549 }
3550 }
Romain Guy8506ab42009-06-11 17:35:47 -07003551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003553 * Returns the ViewGroupOverlay for this view group, creating it if it does
3554 * not yet exist. In addition to {@link ViewOverlay}'s support for drawables,
3555 * {@link ViewGroupOverlay} allows views to be added to the overlay. These
3556 * views, like overlay drawables, are visual-only; they do not receive input
3557 * events and should not be used as anything other than a temporary
3558 * representation of a view in a parent container, such as might be used
3559 * by an animation effect.
3560 *
Chet Haase95399492013-04-08 14:30:31 -07003561 * <p>Note: Overlays do not currently work correctly with {@link
3562 * SurfaceView} or {@link TextureView}; contents in overlays for these
3563 * types of views may not display correctly.</p>
3564 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003565 * @return The ViewGroupOverlay object for this view.
3566 * @see ViewGroupOverlay
3567 */
3568 @Override
3569 public ViewGroupOverlay getOverlay() {
3570 if (mOverlay == null) {
3571 mOverlay = new ViewGroupOverlay(mContext, this);
3572 }
3573 return (ViewGroupOverlay) mOverlay;
3574 }
3575
3576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 * Returns the index of the child to draw for this iteration. Override this
3578 * if you want to change the drawing order of children. By default, it
3579 * returns i.
3580 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08003581 * NOTE: In order for this method to be called, you must enable child ordering
3582 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07003583 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 * @param i The current iteration.
3585 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07003586 *
Romain Guy293451e2009-11-04 13:59:48 -08003587 * @see #setChildrenDrawingOrderEnabled(boolean)
3588 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 */
3590 protected int getChildDrawingOrder(int childCount, int i) {
3591 return i;
3592 }
Romain Guy8506ab42009-06-11 17:35:47 -07003593
Chris Craikab008f02014-05-23 17:55:03 -07003594 private boolean hasChildWithZ() {
3595 for (int i = 0; i < mChildrenCount; i++) {
3596 if (mChildren[i].getZ() != 0) return true;
3597 }
3598 return false;
3599 }
3600
3601 /**
3602 * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,
Chris Craik57c79c82014-09-30 12:54:31 -07003603 * sorted first by Z, then by child drawing order (if applicable). This list must be cleared
3604 * after use to avoid leaking child Views.
Chris Craikab008f02014-05-23 17:55:03 -07003605 *
3606 * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated
3607 * children.
3608 */
George Mount81206522014-09-26 21:53:39 -07003609 ArrayList<View> buildOrderedChildList() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003610 final int childrenCount = mChildrenCount;
3611 if (childrenCount <= 1 || !hasChildWithZ()) return null;
Chris Craikab008f02014-05-23 17:55:03 -07003612
3613 if (mPreSortedChildren == null) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003614 mPreSortedChildren = new ArrayList<>(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07003615 } else {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003616 mPreSortedChildren.ensureCapacity(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07003617 }
3618
Alan Viverettea7b85e62016-01-22 10:14:02 -05003619 final boolean customOrder = isChildrenDrawingOrderEnabled();
3620 for (int i = 0; i < childrenCount; i++) {
Chris Craikab008f02014-05-23 17:55:03 -07003621 // add next child (in child order) to end of list
Alan Viverettea7b85e62016-01-22 10:14:02 -05003622 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3623 final View nextChild = mChildren[childIndex];
3624 final float currentZ = nextChild.getZ();
Chris Craikab008f02014-05-23 17:55:03 -07003625
3626 // insert ahead of any Views with greater Z
3627 int insertIndex = i;
3628 while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {
3629 insertIndex--;
3630 }
3631 mPreSortedChildren.add(insertIndex, nextChild);
3632 }
3633 return mPreSortedChildren;
3634 }
3635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 private void notifyAnimationListener() {
3637 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
3638 mGroupFlags |= FLAG_ANIMATION_DONE;
3639
3640 if (mAnimationListener != null) {
3641 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05003642 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 public void run() {
3644 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
3645 }
3646 };
3647 post(end);
3648 }
3649
Romain Guy849d0a32011-02-01 17:20:48 -08003650 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 }
3652
3653 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08003654 * This method is used to cause children of this ViewGroup to restore or recreate their
3655 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
3656 * to recreate its own display list, which would happen if it went through the normal
3657 * draw/dispatchDraw mechanisms.
3658 *
3659 * @hide
3660 */
3661 @Override
3662 protected void dispatchGetDisplayList() {
3663 final int count = mChildrenCount;
3664 final View[] children = mChildren;
3665 for (int i = 0; i < count; i++) {
3666 final View child = children[i];
John Reckc2330f52015-04-28 13:18:52 -07003667 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) {
Chet Haase6c0665f2014-08-01 13:32:27 -07003668 recreateChildDisplayList(child);
Romain Guy2f57ba52011-02-03 18:03:29 -08003669 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003670 }
Chet Haase91cedf12013-03-11 07:56:30 -07003671 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003672 View overlayView = mOverlay.getOverlayView();
Chet Haase6c0665f2014-08-01 13:32:27 -07003673 recreateChildDisplayList(overlayView);
Chet Haase91cedf12013-03-11 07:56:30 -07003674 }
Chet Haase6c0665f2014-08-01 13:32:27 -07003675 if (mDisappearingChildren != null) {
3676 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3677 final int disappearingCount = disappearingChildren.size();
3678 for (int i = 0; i < disappearingCount; ++i) {
3679 final View child = disappearingChildren.get(i);
3680 recreateChildDisplayList(child);
3681 }
3682 }
3683 }
3684
3685 private void recreateChildDisplayList(View child) {
Chris Craik31a2d062015-05-01 14:22:47 -07003686 child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0;
Chet Haase6c0665f2014-08-01 13:32:27 -07003687 child.mPrivateFlags &= ~PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -07003688 child.updateDisplayListIfDirty();
Chet Haase6c0665f2014-08-01 13:32:27 -07003689 child.mRecreateDisplayList = false;
Chet Haasedaf98e92011-01-10 14:10:36 -08003690 }
3691
3692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 * Draw one child of this View Group. This method is responsible for getting
3694 * the canvas in the right state. This includes clipping, translating so
3695 * that the child's scrolled origin is at 0, 0, and applying any animation
3696 * transformations.
3697 *
3698 * @param canvas The canvas on which to draw the child
3699 * @param child Who to draw
Chet Haasebcca79a2012-02-14 08:45:14 -08003700 * @param drawingTime The time at which draw is occurring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 * @return True if an invalidate() was issued
3702 */
3703 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Chet Haase64a48c12012-02-13 16:33:29 -08003704 return child.draw(canvas, this, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 }
3706
Alan Viverette922e1c62015-05-05 17:18:27 -07003707 @Override
3708 void getScrollIndicatorBounds(@NonNull Rect out) {
3709 super.getScrollIndicatorBounds(out);
3710
3711 // If we have padding and we're supposed to clip children to that
3712 // padding, offset the scroll indicators to match our clip bounds.
3713 final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3714 if (clipToPadding) {
3715 out.left += mPaddingLeft;
3716 out.right -= mPaddingRight;
3717 out.top += mPaddingTop;
3718 out.bottom -= mPaddingBottom;
3719 }
3720 }
3721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 /**
Chris Craikd863a102013-12-19 13:31:15 -08003723 * Returns whether this group's children are clipped to their bounds before drawing.
Chet Haase430742f2013-04-12 11:18:36 -07003724 * The default value is true.
3725 * @see #setClipChildren(boolean)
3726 *
3727 * @return True if the group's children will be clipped to their bounds,
3728 * false otherwise.
3729 */
Chris Craik5c75c522014-09-05 14:08:08 -07003730 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haase430742f2013-04-12 11:18:36 -07003731 public boolean getClipChildren() {
3732 return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0);
3733 }
3734
3735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 * By default, children are clipped to their bounds before drawing. This
3737 * allows view groups to override this behavior for animations, etc.
3738 *
3739 * @param clipChildren true to clip children to their bounds,
3740 * false otherwise
3741 * @attr ref android.R.styleable#ViewGroup_clipChildren
3742 */
3743 public void setClipChildren(boolean clipChildren) {
Chet Haasea1cff502012-02-21 13:43:44 -08003744 boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
3745 if (clipChildren != previousValue) {
3746 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
Chet Haase1271e2c2012-04-20 09:54:27 -07003747 for (int i = 0; i < mChildrenCount; ++i) {
3748 View child = getChildAt(i);
Chris Craik64a12e12014-03-28 18:12:12 -07003749 if (child.mRenderNode != null) {
3750 child.mRenderNode.setClipToBounds(clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -08003751 }
3752 }
John Reckaae9f3b2014-07-28 09:30:36 -07003753 invalidate(true);
Chet Haasea1cff502012-02-21 13:43:44 -08003754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
3756
3757 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003758 * Sets whether this ViewGroup will clip its children to its padding and resize (but not
3759 * clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003760 * <p>
3761 * By default, children are clipped to the padding of their parent
Doris Liub134b5e2015-05-28 17:26:46 -07003762 * ViewGroup. This clipping behavior is only enabled if padding is non-zero.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 *
Doris Liub134b5e2015-05-28 17:26:46 -07003764 * @param clipToPadding true to clip children to the padding of the group, and resize (but
3765 * not clip) any EdgeEffect to the padded region. False otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3767 */
3768 public void setClipToPadding(boolean clipToPadding) {
John Reck9fa3a242014-06-27 15:57:19 -07003769 if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) {
3770 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
John Reckaae9f3b2014-07-28 09:30:36 -07003771 invalidate(true);
John Reck9fa3a242014-06-27 15:57:19 -07003772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 }
3774
3775 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003776 * Returns whether this ViewGroup will clip its children to its padding, and resize (but
3777 * not clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003778 * <p>
3779 * By default, children are clipped to the padding of their parent
3780 * Viewgroup. This clipping behavior is only enabled if padding is non-zero.
Adam Powell1c35b082014-07-11 15:37:15 -07003781 *
Doris Liub134b5e2015-05-28 17:26:46 -07003782 * @return true if this ViewGroup clips children to its padding and resizes (but doesn't
3783 * clip) any EdgeEffect to the padded region, false otherwise.
Adam Powell1c35b082014-07-11 15:37:15 -07003784 *
3785 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3786 */
Chris Craik5c75c522014-09-05 14:08:08 -07003787 @ViewDebug.ExportedProperty(category = "drawing")
Adam Powell1c35b082014-07-11 15:37:15 -07003788 public boolean getClipToPadding() {
3789 return hasBooleanFlag(FLAG_CLIP_TO_PADDING);
3790 }
3791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 @Override
3793 public void dispatchSetSelected(boolean selected) {
3794 final View[] children = mChildren;
3795 final int count = mChildrenCount;
3796 for (int i = 0; i < count; i++) {
3797 children[i].setSelected(selected);
3798 }
3799 }
Romain Guy8506ab42009-06-11 17:35:47 -07003800
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003801 @Override
3802 public void dispatchSetActivated(boolean activated) {
3803 final View[] children = mChildren;
3804 final int count = mChildrenCount;
3805 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003806 children[i].setActivated(activated);
3807 }
3808 }
3809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 @Override
3811 protected void dispatchSetPressed(boolean pressed) {
3812 final View[] children = mChildren;
3813 final int count = mChildrenCount;
3814 for (int i = 0; i < count; i++) {
Adam Powell035a1fc2012-02-27 15:23:50 -08003815 final View child = children[i];
3816 // Children that are clickable on their own should not
3817 // show a pressed state when their parent view does.
3818 // Clearing a pressed state always propagates.
3819 if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
3820 child.setPressed(pressed);
3821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 }
3823 }
3824
Alan Viveretteb942b6f2014-12-08 10:37:39 -08003825 /**
3826 * Dispatches drawable hotspot changes to child views that meet at least
3827 * one of the following criteria:
3828 * <ul>
3829 * <li>Returns {@code false} from both {@link View#isClickable()} and
3830 * {@link View#isLongClickable()}</li>
3831 * <li>Requests duplication of parent state via
3832 * {@link View#setDuplicateParentStateEnabled(boolean)}</li>
3833 * </ul>
3834 *
3835 * @param x hotspot x coordinate
3836 * @param y hotspot y coordinate
3837 * @see #drawableHotspotChanged(float, float)
3838 */
3839 @Override
3840 public void dispatchDrawableHotspotChanged(float x, float y) {
3841 final int count = mChildrenCount;
3842 if (count == 0) {
3843 return;
3844 }
3845
3846 final View[] children = mChildren;
3847 for (int i = 0; i < count; i++) {
3848 final View child = children[i];
3849 // Children that are clickable on their own should not
3850 // receive hotspots when their parent view does.
3851 final boolean nonActionable = !child.isClickable() && !child.isLongClickable();
3852 final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0;
3853 if (nonActionable || duplicatesState) {
3854 final float[] point = getTempPoint();
3855 point[0] = x;
3856 point[1] = y;
3857 transformPointToViewLocal(point, child);
3858 child.drawableHotspotChanged(point[0], point[1]);
3859 }
3860 }
3861 }
3862
Adam Powell14874662013-07-18 19:42:41 -07003863 @Override
3864 void dispatchCancelPendingInputEvents() {
3865 super.dispatchCancelPendingInputEvents();
3866
3867 final View[] children = mChildren;
3868 final int count = mChildrenCount;
3869 for (int i = 0; i < count; i++) {
3870 children[i].dispatchCancelPendingInputEvents();
3871 }
3872 }
3873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 /**
3875 * When this property is set to true, this ViewGroup supports static transformations on
3876 * children; this causes
3877 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3878 * invoked when a child is drawn.
3879 *
3880 * Any subclass overriding
3881 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3882 * set this property to true.
3883 *
3884 * @param enabled True to enable static transformations on children, false otherwise.
3885 *
Chet Haase599913d2012-07-23 16:22:05 -07003886 * @see #getChildStaticTransformation(View, android.view.animation.Transformation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 */
3888 protected void setStaticTransformationsEnabled(boolean enabled) {
3889 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3890 }
3891
3892 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08003893 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
3894 * boolean to indicate whether a static transform was set. The default implementation
3895 * simply returns <code>false</code>; subclasses may override this method for different
Chet Haase599913d2012-07-23 16:22:05 -07003896 * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true
3897 * for this method to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08003899 * @param child The child view whose static transform is being requested
3900 * @param t The Transformation which will hold the result
3901 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07003902 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 */
3904 protected boolean getChildStaticTransformation(View child, Transformation t) {
3905 return false;
3906 }
3907
Romain Guyf6991302013-06-05 17:19:01 -07003908 Transformation getChildTransformation() {
3909 if (mChildTransformation == null) {
3910 mChildTransformation = new Transformation();
3911 }
3912 return mChildTransformation;
3913 }
3914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 /**
3916 * {@hide}
3917 */
3918 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003919 protected View findViewTraversal(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 if (id == mID) {
3921 return this;
3922 }
3923
3924 final View[] where = mChildren;
3925 final int len = mChildrenCount;
3926
3927 for (int i = 0; i < len; i++) {
3928 View v = where[i];
3929
Dianne Hackborn4702a852012-08-17 15:18:29 -07003930 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 v = v.findViewById(id);
3932
3933 if (v != null) {
3934 return v;
3935 }
3936 }
3937 }
3938
3939 return null;
3940 }
3941
3942 /**
3943 * {@hide}
3944 */
3945 @Override
3946 protected View findViewWithTagTraversal(Object tag) {
3947 if (tag != null && tag.equals(mTag)) {
3948 return this;
3949 }
3950
3951 final View[] where = mChildren;
3952 final int len = mChildrenCount;
3953
3954 for (int i = 0; i < len; i++) {
3955 View v = where[i];
3956
Dianne Hackborn4702a852012-08-17 15:18:29 -07003957 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 v = v.findViewWithTag(tag);
3959
3960 if (v != null) {
3961 return v;
3962 }
3963 }
3964 }
3965
3966 return null;
3967 }
3968
3969 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003970 * {@hide}
3971 */
3972 @Override
Jeff Brown4dfbec22011-08-15 14:55:37 -07003973 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003974 if (predicate.apply(this)) {
3975 return this;
3976 }
3977
3978 final View[] where = mChildren;
3979 final int len = mChildrenCount;
3980
3981 for (int i = 0; i < len; i++) {
3982 View v = where[i];
3983
Dianne Hackborn4702a852012-08-17 15:18:29 -07003984 if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003985 v = v.findViewByPredicate(predicate);
3986
3987 if (v != null) {
3988 return v;
3989 }
3990 }
3991 }
3992
3993 return null;
3994 }
3995
3996 /**
Chet Haasec633d2f2015-04-07 10:29:39 -07003997 * This method adds a view to this container at the specified index purely for the
3998 * purposes of allowing that view to draw even though it is not a normal child of
3999 * the container. That is, the view does not participate in layout, focus, accessibility,
4000 * input, or other normal view operations; it is purely an item to be drawn during the normal
4001 * rendering operation of this container. The index that it is added at is the order
4002 * in which it will be drawn, with respect to the other views in the container.
4003 * For example, a transient view added at index 0 will be drawn before all other views
4004 * in the container because it will be drawn first (including before any real view
4005 * at index 0). There can be more than one transient view at any particular index;
4006 * these views will be drawn in the order in which they were added to the list of
4007 * transient views. The index of transient views can also be greater than the number
4008 * of normal views in the container; that just means that they will be drawn after all
4009 * other views are drawn.
4010 *
4011 * <p>Note that since transient views do not participate in layout, they must be sized
4012 * manually or, more typically, they should just use the size that they had before they
4013 * were removed from their container.</p>
4014 *
4015 * <p>Transient views are useful for handling animations of views that have been removed
4016 * from the container, but which should be animated out after the removal. Adding these
4017 * views as transient views allows them to participate in drawing without side-effecting
4018 * the layout of the container.</p>
4019 *
4020 * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed}
4021 * from the container when they are no longer needed. For example, a transient view
4022 * which is added in order to fade it out in its old location should be removed
4023 * once the animation is complete.</p>
4024 *
4025 * @param view The view to be added
4026 * @param index The index at which this view should be drawn, must be >= 0.
4027 * This value is relative to the {@link #getChildAt(int) index} values in the normal
4028 * child list of this container, where any transient view at a particular index will
4029 * be drawn before any normal child at that same index.
Chris Craik66b41392015-04-17 10:08:10 -07004030 *
4031 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004032 */
4033 public void addTransientView(View view, int index) {
4034 if (index < 0) {
4035 return;
4036 }
4037 if (mTransientIndices == null) {
4038 mTransientIndices = new ArrayList<Integer>();
4039 mTransientViews = new ArrayList<View>();
4040 }
4041 final int oldSize = mTransientIndices.size();
4042 if (oldSize > 0) {
4043 int insertionIndex;
4044 for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) {
4045 if (index < mTransientIndices.get(insertionIndex)) {
4046 break;
4047 }
4048 }
4049 mTransientIndices.add(insertionIndex, index);
4050 mTransientViews.add(insertionIndex, view);
4051 } else {
4052 mTransientIndices.add(index);
4053 mTransientViews.add(view);
4054 }
4055 view.mParent = this;
4056 view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4057 invalidate(true);
4058 }
4059
4060 /**
4061 * Removes a view from the list of transient views in this container. If there is no
4062 * such transient view, this method does nothing.
4063 *
4064 * @param view The transient view to be removed
Chris Craik66b41392015-04-17 10:08:10 -07004065 *
4066 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004067 */
4068 public void removeTransientView(View view) {
4069 if (mTransientViews == null) {
4070 return;
4071 }
4072 final int size = mTransientViews.size();
4073 for (int i = 0; i < size; ++i) {
4074 if (view == mTransientViews.get(i)) {
4075 mTransientViews.remove(i);
4076 mTransientIndices.remove(i);
4077 view.mParent = null;
4078 view.dispatchDetachedFromWindow();
4079 invalidate(true);
4080 return;
4081 }
4082 }
4083 }
4084
4085 /**
4086 * Returns the number of transient views in this container. Specific transient
4087 * views and the index at which they were added can be retrieved via
4088 * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}.
4089 *
4090 * @see #addTransientView(View, int)
4091 * @return The number of transient views in this container
Chris Craik66b41392015-04-17 10:08:10 -07004092 *
4093 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004094 */
4095 public int getTransientViewCount() {
4096 return mTransientIndices == null ? 0 : mTransientIndices.size();
4097 }
4098
4099 /**
4100 * Given a valid position within the list of transient views, returns the index of
4101 * the transient view at that position.
4102 *
4103 * @param position The position of the index being queried. Must be at least 0
4104 * and less than the value returned by {@link #getTransientViewCount()}.
4105 * @return The index of the transient view stored in the given position if the
4106 * position is valid, otherwise -1
Chris Craik66b41392015-04-17 10:08:10 -07004107 *
4108 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004109 */
4110 public int getTransientViewIndex(int position) {
4111 if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) {
4112 return -1;
4113 }
4114 return mTransientIndices.get(position);
4115 }
4116
4117 /**
4118 * Given a valid position within the list of transient views, returns the
4119 * transient view at that position.
4120 *
4121 * @param position The position of the view being queried. Must be at least 0
4122 * and less than the value returned by {@link #getTransientViewCount()}.
4123 * @return The transient view stored in the given position if the
4124 * position is valid, otherwise null
Chris Craik66b41392015-04-17 10:08:10 -07004125 *
4126 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004127 */
4128 public View getTransientView(int position) {
4129 if (mTransientViews == null || position >= mTransientViews.size()) {
4130 return null;
4131 }
4132 return mTransientViews.get(position);
4133 }
4134
4135 /**
Romain Guy393a52c2012-05-22 20:21:08 -07004136 * <p>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.</p>
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 *
4145 * @see #generateDefaultLayoutParams()
4146 */
4147 public void addView(View child) {
4148 addView(child, -1);
4149 }
4150
4151 /**
4152 * Adds a child view. If no layout parameters are already set on the child, the
4153 * default parameters for this ViewGroup are set on the child.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004154 *
Romain Guy393a52c2012-05-22 20:21:08 -07004155 * <p><strong>Note:</strong> do not invoke this method from
4156 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4157 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 *
4159 * @param child the child view to add
4160 * @param index the position at which to add the child
4161 *
4162 * @see #generateDefaultLayoutParams()
4163 */
4164 public void addView(View child, int index) {
Adam Powell45a9da52014-10-09 09:44:18 -07004165 if (child == null) {
4166 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 LayoutParams params = child.getLayoutParams();
4169 if (params == null) {
4170 params = generateDefaultLayoutParams();
4171 if (params == null) {
4172 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
4173 }
4174 }
4175 addView(child, index, params);
4176 }
4177
4178 /**
4179 * Adds a child view with this ViewGroup's default layout parameters and the
4180 * specified width and height.
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 */
4188 public void addView(View child, int width, int height) {
4189 final LayoutParams params = generateDefaultLayoutParams();
4190 params.width = width;
4191 params.height = height;
4192 addView(child, -1, params);
4193 }
4194
4195 /**
4196 * Adds a child view with the specified layout parameters.
4197 *
Romain Guy393a52c2012-05-22 20:21:08 -07004198 * <p><strong>Note:</strong> do not invoke this method from
4199 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4200 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 * @param child the child view to add
4203 * @param params the layout parameters to set on the child
4204 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004205 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 public void addView(View child, LayoutParams params) {
4207 addView(child, -1, params);
4208 }
4209
4210 /**
4211 * Adds a child view with the specified layout parameters.
4212 *
Romain Guy393a52c2012-05-22 20:21:08 -07004213 * <p><strong>Note:</strong> do not invoke this method from
4214 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4215 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4216 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 * @param child the child view to add
Alan Viverette77bb6f12015-02-11 17:24:33 -08004218 * @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 -08004219 * @param params the layout parameters to set on the child
4220 */
4221 public void addView(View child, int index, LayoutParams params) {
4222 if (DBG) {
4223 System.out.println(this + " addView");
4224 }
4225
Adam Powell45a9da52014-10-09 09:44:18 -07004226 if (child == null) {
4227 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4228 }
4229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
4231 // therefore, we call requestLayout() on ourselves before, so that the child's request
4232 // will be blocked at our level
4233 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004234 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 addViewInner(child, index, params, false);
4236 }
4237
Alan Viverettebe463f22016-01-21 10:50:10 -05004238 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
4240 if (!checkLayoutParams(params)) {
4241 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
4242 }
4243 if (view.mParent != this) {
4244 throw new IllegalArgumentException("Given view not a child of " + this);
4245 }
4246 view.setLayoutParams(params);
4247 }
4248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
4250 return p != null;
4251 }
4252
4253 /**
4254 * Interface definition for a callback to be invoked when the hierarchy
4255 * within this view changed. The hierarchy changes whenever a child is added
4256 * to or removed from this view.
4257 */
4258 public interface OnHierarchyChangeListener {
4259 /**
4260 * Called when a new child is added to a parent view.
4261 *
4262 * @param parent the view in which a child was added
4263 * @param child the new child view added in the hierarchy
4264 */
4265 void onChildViewAdded(View parent, View child);
4266
4267 /**
4268 * Called when a child is removed from a parent view.
4269 *
4270 * @param parent the view from which the child was removed
4271 * @param child the child removed from the hierarchy
4272 */
4273 void onChildViewRemoved(View parent, View child);
4274 }
4275
4276 /**
4277 * Register a callback to be invoked when a child is added to or removed
4278 * from this view.
4279 *
4280 * @param listener the callback to invoke on hierarchy change
4281 */
4282 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
4283 mOnHierarchyChangeListener = listener;
4284 }
4285
Adam Powell6690d012015-06-17 16:41:56 -07004286 void dispatchViewAdded(View child) {
4287 onViewAdded(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004288 if (mOnHierarchyChangeListener != null) {
4289 mOnHierarchyChangeListener.onChildViewAdded(this, child);
4290 }
4291 }
4292
4293 /**
Adam Powell6690d012015-06-17 16:41:56 -07004294 * Called when a new child is added to this ViewGroup. Overrides should always
4295 * call super.onViewAdded.
4296 *
4297 * @param child the added child view
Philip Milnef51d91c2011-07-18 16:12:19 -07004298 */
Adam Powell6690d012015-06-17 16:41:56 -07004299 public void onViewAdded(View child) {
4300 }
4301
4302 void dispatchViewRemoved(View child) {
4303 onViewRemoved(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004304 if (mOnHierarchyChangeListener != null) {
4305 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
4306 }
4307 }
4308
Adam Powell6690d012015-06-17 16:41:56 -07004309 /**
4310 * Called when a child view is removed from this ViewGroup. Overrides should always
4311 * call super.onViewRemoved.
4312 *
4313 * @param child the removed child view
4314 */
4315 public void onViewRemoved(View child) {
4316 }
4317
Philip Milnecfb631b2012-10-26 10:51:46 -07004318 private void clearCachedLayoutMode() {
Svetoslav6254f482013-06-04 17:22:14 -07004319 if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnecfb631b2012-10-26 10:51:46 -07004320 mLayoutMode = LAYOUT_MODE_UNDEFINED;
4321 }
4322 }
4323
4324 @Override
4325 protected void onAttachedToWindow() {
4326 super.onAttachedToWindow();
4327 clearCachedLayoutMode();
4328 }
4329
4330 @Override
4331 protected void onDetachedFromWindow() {
4332 super.onDetachedFromWindow();
4333 clearCachedLayoutMode();
4334 }
4335
Philip Milnef51d91c2011-07-18 16:12:19 -07004336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 * Adds a view during layout. This is useful if in your onLayout() method,
4338 * you need to add more views (as does the list view for example).
4339 *
4340 * If index is negative, it means put it at the end of the list.
4341 *
4342 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004343 * @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 -08004344 * @param params the layout parameters to associate with the child
4345 * @return true if the child was added, false otherwise
4346 */
4347 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
4348 return addViewInLayout(child, index, params, false);
4349 }
4350
4351 /**
4352 * Adds a view during layout. This is useful if in your onLayout() method,
4353 * you need to add more views (as does the list view for example).
4354 *
4355 * If index is negative, it means put it at the end of the list.
4356 *
4357 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004358 * @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 -08004359 * @param params the layout parameters to associate with the child
4360 * @param preventRequestLayout if true, calling this method will not trigger a
4361 * layout request on child
4362 * @return true if the child was added, false otherwise
4363 */
4364 protected boolean addViewInLayout(View child, int index, LayoutParams params,
4365 boolean preventRequestLayout) {
Adam Powell45a9da52014-10-09 09:44:18 -07004366 if (child == null) {
4367 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 child.mParent = null;
4370 addViewInner(child, index, params, preventRequestLayout);
Dianne Hackborn4702a852012-08-17 15:18:29 -07004371 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 return true;
4373 }
4374
4375 /**
4376 * Prevents the specified child to be laid out during the next layout pass.
4377 *
4378 * @param child the child on which to perform the cleanup
4379 */
4380 protected void cleanupLayoutState(View child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004381 child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 }
4383
4384 private void addViewInner(View child, int index, LayoutParams params,
4385 boolean preventRequestLayout) {
4386
Chet Haasee8e45d32011-03-02 17:07:35 -08004387 if (mTransition != null) {
4388 // Don't prevent other add transitions from completing, but cancel remove
4389 // transitions to let them complete the process before we add to the container
4390 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08004391 }
4392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 if (child.getParent() != null) {
4394 throw new IllegalStateException("The specified child already has a parent. " +
4395 "You must call removeView() on the child's parent first.");
4396 }
4397
Chet Haase21cd1382010-09-01 17:42:29 -07004398 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004399 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004400 }
4401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 if (!checkLayoutParams(params)) {
4403 params = generateLayoutParams(params);
4404 }
4405
4406 if (preventRequestLayout) {
4407 child.mLayoutParams = params;
4408 } else {
4409 child.setLayoutParams(params);
4410 }
4411
4412 if (index < 0) {
4413 index = mChildrenCount;
4414 }
4415
4416 addInArray(child, index);
4417
4418 // tell our children
4419 if (preventRequestLayout) {
4420 child.assignParent(this);
4421 } else {
4422 child.mParent = this;
4423 }
4424
4425 if (child.hasFocus()) {
4426 requestChildFocus(child, child.findFocus());
4427 }
Romain Guy8506ab42009-06-11 17:35:47 -07004428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07004430 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07004431 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 ai.mKeepScreenOn = false;
4433 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4434 if (ai.mKeepScreenOn) {
4435 needGlobalAttributesUpdate(true);
4436 }
4437 ai.mKeepScreenOn = lastKeepOn;
4438 }
4439
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004440 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglioa7e0bcd2012-10-16 19:55:01 -07004441 child.resetRtlProperties();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004442 }
4443
Adam Powell6690d012015-06-17 16:41:56 -07004444 dispatchViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445
4446 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
4447 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
4448 }
Adam Powell539ee872012-02-03 19:00:49 -08004449
4450 if (child.hasTransientState()) {
4451 childHasTransientStateChanged(child, true);
4452 }
Svetoslav6254f482013-06-04 17:22:14 -07004453
Svetoslav8e3feb12014-02-24 13:46:47 -08004454 if (child.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004455 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004456 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004457
4458 if (mTransientIndices != null) {
4459 final int transientCount = mTransientIndices.size();
4460 for (int i = 0; i < transientCount; ++i) {
4461 final int oldIndex = mTransientIndices.get(i);
4462 if (index <= oldIndex) {
4463 mTransientIndices.set(i, oldIndex + 1);
4464 }
4465 }
4466 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004467
4468 if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) {
4469 notifyChildOfDragStart(child);
4470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 }
4472
4473 private void addInArray(View child, int index) {
4474 View[] children = mChildren;
4475 final int count = mChildrenCount;
4476 final int size = children.length;
4477 if (index == count) {
4478 if (size == count) {
4479 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4480 System.arraycopy(children, 0, mChildren, 0, size);
4481 children = mChildren;
4482 }
4483 children[mChildrenCount++] = child;
4484 } else if (index < count) {
4485 if (size == count) {
4486 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4487 System.arraycopy(children, 0, mChildren, 0, index);
4488 System.arraycopy(children, index, mChildren, index + 1, count - index);
4489 children = mChildren;
4490 } else {
4491 System.arraycopy(children, index, children, index + 1, count - index);
4492 }
4493 children[index] = child;
4494 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08004495 if (mLastTouchDownIndex >= index) {
4496 mLastTouchDownIndex++;
4497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 } else {
4499 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
4500 }
4501 }
4502
4503 // This method also sets the child's mParent to null
4504 private void removeFromArray(int index) {
4505 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07004506 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
4507 children[index].mParent = null;
4508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 final int count = mChildrenCount;
4510 if (index == count - 1) {
4511 children[--mChildrenCount] = null;
4512 } else if (index >= 0 && index < count) {
4513 System.arraycopy(children, index + 1, children, index, count - index - 1);
4514 children[--mChildrenCount] = null;
4515 } else {
4516 throw new IndexOutOfBoundsException();
4517 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08004518 if (mLastTouchDownIndex == index) {
4519 mLastTouchDownTime = 0;
4520 mLastTouchDownIndex = -1;
4521 } else if (mLastTouchDownIndex > index) {
4522 mLastTouchDownIndex--;
4523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 }
4525
4526 // This method also sets the children's mParent to null
4527 private void removeFromArray(int start, int count) {
4528 final View[] children = mChildren;
4529 final int childrenCount = mChildrenCount;
4530
4531 start = Math.max(0, start);
4532 final int end = Math.min(childrenCount, start + count);
4533
4534 if (start == end) {
4535 return;
4536 }
4537
4538 if (end == childrenCount) {
4539 for (int i = start; i < end; i++) {
4540 children[i].mParent = null;
4541 children[i] = null;
4542 }
4543 } else {
4544 for (int i = start; i < end; i++) {
4545 children[i].mParent = null;
4546 }
4547
4548 // Since we're looping above, we might as well do the copy, but is arraycopy()
4549 // faster than the extra 2 bounds checks we would do in the loop?
4550 System.arraycopy(children, end, children, start, childrenCount - end);
4551
4552 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
4553 children[i] = null;
4554 }
4555 }
4556
4557 mChildrenCount -= (end - start);
4558 }
4559
4560 private void bindLayoutAnimation(View child) {
4561 Animation a = mLayoutAnimationController.getAnimationForView(child);
4562 child.setAnimation(a);
4563 }
4564
4565 /**
4566 * Subclasses should override this method to set layout animation
4567 * parameters on the supplied child.
4568 *
4569 * @param child the child to associate with animation parameters
4570 * @param params the child's layout parameters which hold the animation
4571 * parameters
4572 * @param index the index of the child in the view group
4573 * @param count the number of children in the view group
4574 */
4575 protected void attachLayoutAnimationParameters(View child,
4576 LayoutParams params, int index, int count) {
4577 LayoutAnimationController.AnimationParameters animationParams =
4578 params.layoutAnimationParameters;
4579 if (animationParams == null) {
4580 animationParams = new LayoutAnimationController.AnimationParameters();
4581 params.layoutAnimationParameters = animationParams;
4582 }
4583
4584 animationParams.count = count;
4585 animationParams.index = index;
4586 }
4587
4588 /**
4589 * {@inheritDoc}
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004590 *
Romain Guy393a52c2012-05-22 20:21:08 -07004591 * <p><strong>Note:</strong> do not invoke this method from
4592 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4593 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004595 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 public void removeView(View view) {
Alan Viverette177ec4602014-10-17 13:34:50 -07004597 if (removeViewInternal(view)) {
4598 requestLayout();
4599 invalidate(true);
4600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 }
4602
4603 /**
4604 * Removes a view during layout. This is useful if in your onLayout() method,
4605 * you need to remove more views.
4606 *
Romain Guy393a52c2012-05-22 20:21:08 -07004607 * <p><strong>Note:</strong> do not invoke this method from
4608 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4609 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 * @param view the view to remove from the group
4612 */
4613 public void removeViewInLayout(View view) {
4614 removeViewInternal(view);
4615 }
4616
4617 /**
4618 * Removes a range of views during layout. This is useful if in your onLayout() method,
4619 * you need to remove more views.
4620 *
Romain Guy393a52c2012-05-22 20:21:08 -07004621 * <p><strong>Note:</strong> do not invoke this method from
4622 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4623 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 * @param start the index of the first view to remove from the group
4626 * @param count the number of views to remove from the group
4627 */
4628 public void removeViewsInLayout(int start, int count) {
4629 removeViewsInternal(start, count);
4630 }
4631
4632 /**
4633 * Removes the view at the specified position in the group.
4634 *
Romain Guy393a52c2012-05-22 20:21:08 -07004635 * <p><strong>Note:</strong> do not invoke this method from
4636 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4637 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 * @param index the position in the group of the view to remove
4640 */
4641 public void removeViewAt(int index) {
4642 removeViewInternal(index, getChildAt(index));
4643 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004644 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 }
4646
4647 /**
4648 * Removes the specified range of views from the group.
4649 *
Romain Guy393a52c2012-05-22 20:21:08 -07004650 * <p><strong>Note:</strong> do not invoke this method from
4651 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4652 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 * @param start the first position in the group of the range of views to remove
4655 * @param count the number of views to remove
4656 */
4657 public void removeViews(int start, int count) {
4658 removeViewsInternal(start, count);
4659 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004660 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 }
4662
Alan Viverette177ec4602014-10-17 13:34:50 -07004663 private boolean removeViewInternal(View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 final int index = indexOfChild(view);
4665 if (index >= 0) {
4666 removeViewInternal(index, view);
Alan Viverette177ec4602014-10-17 13:34:50 -07004667 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 }
Alan Viverette177ec4602014-10-17 13:34:50 -07004669 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 }
4671
4672 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07004673 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004674 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004675 }
4676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 boolean clearChildFocus = false;
4678 if (view == mFocused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004679 view.unFocus(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 clearChildFocus = true;
4681 }
4682
Alan Viverette632af842014-10-28 13:45:11 -07004683 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004684
Jeff Brown59a422e2012-04-19 15:19:19 -07004685 cancelTouchTarget(view);
4686 cancelHoverTarget(view);
4687
Chet Haase21cd1382010-09-01 17:42:29 -07004688 if (view.getAnimation() != null ||
4689 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 addDisappearingView(view);
4691 } else if (view.mAttachInfo != null) {
4692 view.dispatchDetachedFromWindow();
4693 }
4694
Adam Powell539ee872012-02-03 19:00:49 -08004695 if (view.hasTransientState()) {
4696 childHasTransientStateChanged(view, false);
4697 }
4698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 removeFromArray(index);
4702
4703 if (clearChildFocus) {
4704 clearChildFocus(view);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004705 if (!rootViewRequestFocus()) {
4706 notifyGlobalFocusCleared(this);
4707 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004708 }
Romain Guy6fb05632012-11-29 10:50:33 -08004709
Adam Powell6690d012015-06-17 16:41:56 -07004710 dispatchViewRemoved(view);
Svetoslav6254f482013-06-04 17:22:14 -07004711
Svetoslav8e3feb12014-02-24 13:46:47 -08004712 if (view.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004713 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004714 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004715
4716 int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
4717 for (int i = 0; i < transientCount; ++i) {
4718 final int oldIndex = mTransientIndices.get(i);
4719 if (index < oldIndex) {
4720 mTransientIndices.set(i, oldIndex - 1);
4721 }
4722 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004723
4724 if (mCurrentDragStartEvent != null) {
4725 mChildrenInterestedInDrag.remove(view);
4726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 }
4728
Chet Haase21cd1382010-09-01 17:42:29 -07004729 /**
4730 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4731 * not null, changes in layout which occur because of children being added to or removed from
4732 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4733 * object. By default, the transition object is null (so layout changes are not animated).
4734 *
Chet Haaseef3cbfd2013-08-21 14:01:02 -07004735 * <p>Replacing a non-null transition will cause that previous transition to be
4736 * canceled, if it is currently running, to restore this container to
4737 * its correct post-transition state.</p>
4738 *
Chet Haase21cd1382010-09-01 17:42:29 -07004739 * @param transition The LayoutTransition object that will animated changes in layout. A value
4740 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07004741 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07004742 */
4743 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004744 if (mTransition != null) {
Chet Haasefee6f2b2013-08-27 12:22:29 -07004745 LayoutTransition previousTransition = mTransition;
4746 previousTransition.cancel();
4747 previousTransition.removeTransitionListener(mLayoutTransitionListener);
Chet Haaseb20db3e2010-09-10 13:07:30 -07004748 }
Chet Haase21cd1382010-09-01 17:42:29 -07004749 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07004750 if (mTransition != null) {
4751 mTransition.addTransitionListener(mLayoutTransitionListener);
4752 }
4753 }
4754
4755 /**
4756 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4757 * not null, changes in layout which occur because of children being added to or removed from
4758 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4759 * object. By default, the transition object is null (so layout changes are not animated).
4760 *
4761 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
4762 * A value of <code>null</code> means no transition will run on layout changes.
4763 */
4764 public LayoutTransition getLayoutTransition() {
4765 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07004766 }
4767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004768 private void removeViewsInternal(int start, int count) {
Chris Craik18a759d2015-10-14 14:16:33 -07004769 final int end = start + count;
4770
4771 if (start < 0 || count < 0 || end > mChildrenCount) {
4772 throw new IndexOutOfBoundsException();
4773 }
4774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 final View focused = mFocused;
4776 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004777 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778
4779 final View[] children = mChildren;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780
4781 for (int i = start; i < end; i++) {
4782 final View view = children[i];
4783
Chet Haase21cd1382010-09-01 17:42:29 -07004784 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004785 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004786 }
4787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004789 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004790 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004791 }
4792
Alan Viverette632af842014-10-28 13:45:11 -07004793 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004794
Jeff Brown59a422e2012-04-19 15:19:19 -07004795 cancelTouchTarget(view);
4796 cancelHoverTarget(view);
4797
Chet Haase21cd1382010-09-01 17:42:29 -07004798 if (view.getAnimation() != null ||
4799 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 addDisappearingView(view);
4801 } else if (detach) {
4802 view.dispatchDetachedFromWindow();
4803 }
4804
Adam Powell539ee872012-02-03 19:00:49 -08004805 if (view.hasTransientState()) {
4806 childHasTransientStateChanged(view, false);
4807 }
4808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004810
Adam Powell6690d012015-06-17 16:41:56 -07004811 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813
4814 removeFromArray(start, count);
4815
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004816 if (clearChildFocus) {
4817 clearChildFocus(focused);
4818 if (!rootViewRequestFocus()) {
4819 notifyGlobalFocusCleared(focused);
4820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 }
4822 }
4823
4824 /**
4825 * Call this method to remove all child views from the
4826 * ViewGroup.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004827 *
Romain Guy393a52c2012-05-22 20:21:08 -07004828 * <p><strong>Note:</strong> do not invoke this method from
4829 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4830 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 */
4832 public void removeAllViews() {
4833 removeAllViewsInLayout();
4834 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004835 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 }
4837
4838 /**
4839 * Called by a ViewGroup subclass to remove child views from itself,
4840 * when it must first know its size on screen before it can calculate how many
4841 * child views it will render. An example is a Gallery or a ListView, which
4842 * may "have" 50 children, but actually only render the number of children
4843 * that can currently fit inside the object on screen. Do not call
4844 * this method unless you are extending ViewGroup and understand the
4845 * view measuring and layout pipeline.
Romain Guy393a52c2012-05-22 20:21:08 -07004846 *
4847 * <p><strong>Note:</strong> do not invoke this method from
4848 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4849 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 */
4851 public void removeAllViewsInLayout() {
4852 final int count = mChildrenCount;
4853 if (count <= 0) {
4854 return;
4855 }
4856
4857 final View[] children = mChildren;
4858 mChildrenCount = 0;
4859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 final View focused = mFocused;
4861 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004862 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863
4864 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 for (int i = count - 1; i >= 0; i--) {
4867 final View view = children[i];
4868
Chet Haase21cd1382010-09-01 17:42:29 -07004869 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004870 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004871 }
4872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004874 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004875 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
4877
Alan Viverette632af842014-10-28 13:45:11 -07004878 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004879
Jeff Brown59a422e2012-04-19 15:19:19 -07004880 cancelTouchTarget(view);
4881 cancelHoverTarget(view);
4882
Chet Haase21cd1382010-09-01 17:42:29 -07004883 if (view.getAnimation() != null ||
4884 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 addDisappearingView(view);
4886 } else if (detach) {
4887 view.dispatchDetachedFromWindow();
4888 }
4889
Adam Powell539ee872012-02-03 19:00:49 -08004890 if (view.hasTransientState()) {
4891 childHasTransientStateChanged(view, false);
4892 }
4893
Adam Powell6690d012015-06-17 16:41:56 -07004894 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895
4896 view.mParent = null;
4897 children[i] = null;
4898 }
4899
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004900 if (clearChildFocus) {
4901 clearChildFocus(focused);
4902 if (!rootViewRequestFocus()) {
4903 notifyGlobalFocusCleared(focused);
4904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 }
4906 }
4907
4908 /**
4909 * Finishes the removal of a detached view. This method will dispatch the detached from
4910 * window event and notify the hierarchy change listener.
Chet Haaseca479d42012-08-30 17:20:08 -07004911 * <p>
4912 * This method is intended to be lightweight and makes no assumptions about whether the
4913 * parent or child should be redrawn. Proper use of this method will include also making
4914 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4915 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4916 * which performs a {@link #requestLayout()} on the next frame, after all detach/remove
4917 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 *
4919 * @param child the child to be definitely removed from the view hierarchy
4920 * @param animate if true and the view has an animation, the view is placed in the
4921 * disappearing views list, otherwise, it is detached from the window
4922 *
4923 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4924 * @see #detachAllViewsFromParent()
4925 * @see #detachViewFromParent(View)
4926 * @see #detachViewFromParent(int)
4927 */
4928 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07004929 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004930 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004931 }
4932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 if (child == mFocused) {
4934 child.clearFocus();
4935 }
Romain Guy8506ab42009-06-11 17:35:47 -07004936
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004937 child.clearAccessibilityFocus();
4938
Jeff Brown59a422e2012-04-19 15:19:19 -07004939 cancelTouchTarget(child);
4940 cancelHoverTarget(child);
4941
Chet Haase21cd1382010-09-01 17:42:29 -07004942 if ((animate && child.getAnimation() != null) ||
4943 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 addDisappearingView(child);
4945 } else if (child.mAttachInfo != null) {
4946 child.dispatchDetachedFromWindow();
4947 }
4948
Adam Powell539ee872012-02-03 19:00:49 -08004949 if (child.hasTransientState()) {
4950 childHasTransientStateChanged(child, false);
4951 }
4952
Adam Powell6690d012015-06-17 16:41:56 -07004953 dispatchViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 }
4955
4956 /**
4957 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
Chet Haaseca479d42012-08-30 17:20:08 -07004958 * sets the layout parameters and puts the view in the list of children so that
4959 * it can be retrieved by calling {@link #getChildAt(int)}.
4960 * <p>
4961 * This method is intended to be lightweight and makes no assumptions about whether the
4962 * parent or child should be redrawn. Proper use of this method will include also making
4963 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4964 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4965 * which performs a {@link #requestLayout()} on the next frame, after all detach/attach
4966 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
4967 * <p>
4968 * This method should be called only for views which were detached from their parent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 *
4970 * @param child the child to attach
4971 * @param index the index at which the child should be attached
4972 * @param params the layout parameters of the child
4973 *
4974 * @see #removeDetachedView(View, boolean)
4975 * @see #detachAllViewsFromParent()
4976 * @see #detachViewFromParent(View)
4977 * @see #detachViewFromParent(int)
4978 */
4979 protected void attachViewToParent(View child, int index, LayoutParams params) {
4980 child.mLayoutParams = params;
4981
4982 if (index < 0) {
4983 index = mChildrenCount;
4984 }
4985
4986 addInArray(child, index);
4987
4988 child.mParent = this;
Dianne Hackborn4702a852012-08-17 15:18:29 -07004989 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK
4990 & ~PFLAG_DRAWING_CACHE_VALID)
4991 | PFLAG_DRAWN | PFLAG_INVALIDATED;
4992 this.mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993
4994 if (child.hasFocus()) {
4995 requestChildFocus(child, child.findFocus());
4996 }
Adam Powellc5874092016-03-17 16:27:24 -07004997 dispatchVisibilityAggregated(isAttachedToWindow() && getWindowVisibility() == VISIBLE
4998 && isShown());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 }
5000
5001 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005002 * Detaches a view from its parent. Detaching a view should be followed
5003 * either by a call to
5004 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5005 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5006 * temporary; reattachment or removal should happen within the same drawing cycle as
5007 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5008 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 *
5010 * @param child the child to detach
5011 *
5012 * @see #detachViewFromParent(int)
5013 * @see #detachViewsFromParent(int, int)
5014 * @see #detachAllViewsFromParent()
5015 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5016 * @see #removeDetachedView(View, boolean)
5017 */
5018 protected void detachViewFromParent(View child) {
5019 removeFromArray(indexOfChild(child));
5020 }
5021
5022 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005023 * Detaches a view from its parent. Detaching a view should be followed
5024 * either by a call to
5025 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5026 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5027 * temporary; reattachment or removal should happen within the same drawing cycle as
5028 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5029 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 *
5031 * @param index the index of the child to detach
5032 *
5033 * @see #detachViewFromParent(View)
5034 * @see #detachAllViewsFromParent()
5035 * @see #detachViewsFromParent(int, int)
5036 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5037 * @see #removeDetachedView(View, boolean)
5038 */
5039 protected void detachViewFromParent(int index) {
5040 removeFromArray(index);
5041 }
5042
5043 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005044 * Detaches a range of views from their parents. Detaching a view should be followed
5045 * either by a call to
5046 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5047 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5048 * temporary; reattachment or removal should happen within the same drawing cycle as
5049 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5050 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 *
5052 * @param start the first index of the childrend range to detach
5053 * @param count the number of children to detach
5054 *
5055 * @see #detachViewFromParent(View)
5056 * @see #detachViewFromParent(int)
5057 * @see #detachAllViewsFromParent()
5058 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5059 * @see #removeDetachedView(View, boolean)
5060 */
5061 protected void detachViewsFromParent(int start, int count) {
5062 removeFromArray(start, count);
5063 }
5064
5065 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005066 * Detaches all views from the parent. Detaching a view should be followed
5067 * either by a call to
5068 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5069 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5070 * temporary; reattachment or removal should happen within the same drawing cycle as
5071 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5072 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 *
5074 * @see #detachViewFromParent(View)
5075 * @see #detachViewFromParent(int)
5076 * @see #detachViewsFromParent(int, int)
5077 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5078 * @see #removeDetachedView(View, boolean)
5079 */
5080 protected void detachAllViewsFromParent() {
5081 final int count = mChildrenCount;
5082 if (count <= 0) {
5083 return;
5084 }
5085
5086 final View[] children = mChildren;
5087 mChildrenCount = 0;
5088
5089 for (int i = count - 1; i >= 0; i--) {
5090 children[i].mParent = null;
5091 children[i] = null;
5092 }
5093 }
5094
5095 /**
5096 * Don't call or override this method. It is used for the implementation of
5097 * the view hierarchy.
5098 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005099 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 public final void invalidateChild(View child, final Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 ViewParent parent = this;
5102
5103 final AttachInfo attachInfo = mAttachInfo;
5104 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 // If the child is drawing an animation, we want to copy this flag onto
5106 // ourselves and the parent to make sure the invalidate request goes
5107 // through
Dianne Hackborn4702a852012-08-17 15:18:29 -07005108 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION)
5109 == PFLAG_DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07005110
Romain Guyfe455af2012-02-15 16:40:20 -08005111 // Check whether the child that requests the invalidate is fully opaque
5112 // Views being animated or transformed are not considered opaque because we may
5113 // be invalidating their old position and need the parent to paint behind them.
5114 Matrix childMatrix = child.getMatrix();
5115 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
5116 child.getAnimation() == null && childMatrix.isIdentity();
5117 // Mark the child as dirty, using the appropriate flag
5118 // Make sure we do not set both flags at the same time
Dianne Hackborn4702a852012-08-17 15:18:29 -07005119 int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005120
John Reck96bb8ad2014-06-19 10:53:03 -07005121 if (child.mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005122 mPrivateFlags |= PFLAG_INVALIDATED;
5123 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guyfe455af2012-02-15 16:40:20 -08005124 }
5125
5126 final int[] location = attachInfo.mInvalidateChildLocation;
5127 location[CHILD_LEFT_INDEX] = child.mLeft;
5128 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase599913d2012-07-23 16:22:05 -07005129 if (!childMatrix.isIdentity() ||
5130 (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Romain Guyfe455af2012-02-15 16:40:20 -08005131 RectF boundingRect = attachInfo.mTmpTransformRect;
5132 boundingRect.set(dirty);
Chet Haase599913d2012-07-23 16:22:05 -07005133 Matrix transformMatrix;
5134 if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
5135 Transformation t = attachInfo.mTmpTransformation;
5136 boolean transformed = getChildStaticTransformation(child, t);
5137 if (transformed) {
5138 transformMatrix = attachInfo.mTmpMatrix;
5139 transformMatrix.set(t.getMatrix());
5140 if (!childMatrix.isIdentity()) {
5141 transformMatrix.preConcat(childMatrix);
5142 }
5143 } else {
5144 transformMatrix = childMatrix;
5145 }
5146 } else {
5147 transformMatrix = childMatrix;
5148 }
5149 transformMatrix.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005150 dirty.set((int) Math.floor(boundingRect.left),
5151 (int) Math.floor(boundingRect.top),
5152 (int) Math.ceil(boundingRect.right),
5153 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005154 }
5155
5156 do {
5157 View view = null;
5158 if (parent instanceof View) {
5159 view = (View) parent;
Romain Guyfe455af2012-02-15 16:40:20 -08005160 }
5161
5162 if (drawAnimation) {
5163 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005164 view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Romain Guyfe455af2012-02-15 16:40:20 -08005165 } else if (parent instanceof ViewRootImpl) {
5166 ((ViewRootImpl) parent).mIsAnimating = true;
5167 }
5168 }
5169
5170 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
5171 // flag coming from the child that initiated the invalidate
5172 if (view != null) {
5173 if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
5174 view.getSolidColor() == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005175 opaqueFlag = PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005176 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005177 if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) {
5178 view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag;
Romain Guyfe455af2012-02-15 16:40:20 -08005179 }
5180 }
5181
5182 parent = parent.invalidateChildInParent(location, dirty);
5183 if (view != null) {
5184 // Account for transform on current parent
5185 Matrix m = view.getMatrix();
5186 if (!m.isIdentity()) {
5187 RectF boundingRect = attachInfo.mTmpTransformRect;
5188 boundingRect.set(dirty);
5189 m.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005190 dirty.set((int) Math.floor(boundingRect.left),
5191 (int) Math.floor(boundingRect.top),
5192 (int) Math.ceil(boundingRect.right),
5193 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005194 }
5195 }
5196 } while (parent != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 }
5198 }
5199
5200 /**
5201 * Don't call or override this method. It is used for the implementation of
5202 * the view hierarchy.
5203 *
5204 * This implementation returns null if this ViewGroup does not have a parent,
5205 * if this ViewGroup is already fully invalidated or if the dirty rectangle
5206 * does not intersect with this ViewGroup's bounds.
5207 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005208 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005210 if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
5211 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
5213 FLAG_OPTIMIZE_INVALIDATE) {
5214 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
5215 location[CHILD_TOP_INDEX] - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005216 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5217 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219
5220 final int left = mLeft;
5221 final int top = mTop;
5222
Chet Haase05e91ed2012-07-03 14:17:57 -07005223 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5224 if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) {
5225 dirty.setEmpty();
Romain Guy3a3133d2011-02-01 22:59:58 -08005226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005228 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haase05e91ed2012-07-03 14:17:57 -07005229
5230 location[CHILD_LEFT_INDEX] = left;
5231 location[CHILD_TOP_INDEX] = top;
5232
John Reck96bb8ad2014-06-19 10:53:03 -07005233 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005234 mPrivateFlags |= PFLAG_INVALIDATED;
Chet Haase05e91ed2012-07-03 14:17:57 -07005235 }
5236
5237 return mParent;
5238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005240 mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241
5242 location[CHILD_LEFT_INDEX] = mLeft;
5243 location[CHILD_TOP_INDEX] = mTop;
Chet Haasea3db8662011-07-19 10:36:05 -07005244 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5245 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
5246 } else {
5247 // in case the dirty rect extends outside the bounds of this container
5248 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5249 }
Romain Guy3a3133d2011-02-01 22:59:58 -08005250
John Reck96bb8ad2014-06-19 10:53:03 -07005251 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005252 mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy3a3133d2011-02-01 22:59:58 -08005253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254
5255 return mParent;
5256 }
5257 }
5258
5259 return null;
5260 }
5261
5262 /**
John Recke4267ea2014-06-03 15:53:15 -07005263 * Native-calculated damage path
5264 * Returns false if this path was unable to complete successfully. This means
5265 * it hit a ViewParent it doesn't recognize and needs to fall back to calculating
5266 * damage area
5267 * @hide
5268 */
5269 public boolean damageChildDeferred(View child) {
5270 ViewParent parent = getParent();
5271 while (parent != null) {
5272 if (parent instanceof ViewGroup) {
5273 parent = parent.getParent();
5274 } else if (parent instanceof ViewRootImpl) {
5275 ((ViewRootImpl) parent).invalidate();
5276 return true;
5277 } else {
5278 parent = null;
5279 }
5280 }
5281 return false;
5282 }
5283
5284 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07005285 * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the
5286 * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods
5287 * do; all we want to do here is schedule a traversal with the appropriate dirty rect.
5288 *
5289 * @hide
5290 */
Chris Craik49e6c732014-03-31 12:34:11 -07005291 public void damageChild(View child, final Rect dirty) {
John Recke4267ea2014-06-03 15:53:15 -07005292 if (damageChildDeferred(child)) {
5293 return;
5294 }
5295
Chet Haase9d1992d2012-03-13 11:03:25 -07005296 ViewParent parent = this;
5297
5298 final AttachInfo attachInfo = mAttachInfo;
5299 if (attachInfo != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005300 int left = child.mLeft;
5301 int top = child.mTop;
5302 if (!child.getMatrix().isIdentity()) {
5303 child.transformRect(dirty);
5304 }
5305
5306 do {
5307 if (parent instanceof ViewGroup) {
5308 ViewGroup parentVG = (ViewGroup) parent;
Chet Haaseb85967b2012-03-26 14:37:51 -07005309 if (parentVG.mLayerType != LAYER_TYPE_NONE) {
5310 // Layered parents should be recreated, not just re-issued
5311 parentVG.invalidate();
5312 parent = null;
5313 } else {
Chris Craik49e6c732014-03-31 12:34:11 -07005314 parent = parentVG.damageChildInParent(left, top, dirty);
Chet Haaseb85967b2012-03-26 14:37:51 -07005315 left = parentVG.mLeft;
5316 top = parentVG.mTop;
5317 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005318 } else {
5319 // Reached the top; this calls into the usual invalidate method in
5320 // ViewRootImpl, which schedules a traversal
5321 final int[] location = attachInfo.mInvalidateChildLocation;
5322 location[0] = left;
5323 location[1] = top;
5324 parent = parent.invalidateChildInParent(location, dirty);
5325 }
5326 } while (parent != null);
5327 }
5328 }
5329
5330 /**
5331 * Quick invalidation method that simply transforms the dirty rect into the parent's
5332 * coordinate system, pruning the invalidation if the parent has already been invalidated.
Chet Haasee4a2d7c2013-06-21 17:49:36 -07005333 *
5334 * @hide
Chet Haase9d1992d2012-03-13 11:03:25 -07005335 */
Chris Craik49e6c732014-03-31 12:34:11 -07005336 protected ViewParent damageChildInParent(int left, int top, final Rect dirty) {
5337 if ((mPrivateFlags & PFLAG_DRAWN) != 0
5338 || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005339 dirty.offset(left - mScrollX, top - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005340 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5341 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5342 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005343
5344 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 ||
5345 dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
5346
Chet Haase9d1992d2012-03-13 11:03:25 -07005347 if (!getMatrix().isIdentity()) {
5348 transformRect(dirty);
5349 }
5350
5351 return mParent;
5352 }
5353 }
5354
5355 return null;
5356 }
5357
5358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 * Offset a rectangle that is in a descendant's coordinate
5360 * space into our coordinate space.
5361 * @param descendant A descendant of this view
5362 * @param rect A rectangle defined in descendant's coordinate space.
5363 */
5364 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
5365 offsetRectBetweenParentAndChild(descendant, rect, true, false);
5366 }
5367
5368 /**
5369 * Offset a rectangle that is in our coordinate space into an ancestor's
5370 * coordinate space.
5371 * @param descendant A descendant of this view
5372 * @param rect A rectangle defined in descendant's coordinate space.
5373 */
5374 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
5375 offsetRectBetweenParentAndChild(descendant, rect, false, false);
5376 }
5377
5378 /**
5379 * Helper method that offsets a rect either from parent to descendant or
5380 * descendant to parent.
5381 */
5382 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
5383 boolean offsetFromChildToParent, boolean clipToBounds) {
5384
5385 // already in the same coord system :)
5386 if (descendant == this) {
5387 return;
5388 }
5389
5390 ViewParent theParent = descendant.mParent;
5391
5392 // search and offset up to the parent
5393 while ((theParent != null)
5394 && (theParent instanceof View)
5395 && (theParent != this)) {
5396
5397 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005398 rect.offset(descendant.mLeft - descendant.mScrollX,
5399 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 if (clipToBounds) {
5401 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005402 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5403 p.mBottom - p.mTop);
5404 if (!intersected) {
5405 rect.setEmpty();
5406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 }
5408 } else {
5409 if (clipToBounds) {
5410 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005411 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5412 p.mBottom - p.mTop);
5413 if (!intersected) {
5414 rect.setEmpty();
5415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 }
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005417 rect.offset(descendant.mScrollX - descendant.mLeft,
5418 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 }
5420
5421 descendant = (View) theParent;
5422 theParent = descendant.mParent;
5423 }
5424
5425 // now that we are up to this view, need to offset one more time
5426 // to get into our coordinate space
5427 if (theParent == this) {
5428 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005429 rect.offset(descendant.mLeft - descendant.mScrollX,
5430 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 } else {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005432 rect.offset(descendant.mScrollX - descendant.mLeft,
5433 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 }
5435 } else {
5436 throw new IllegalArgumentException("parameter must be a descendant of this view");
5437 }
5438 }
5439
5440 /**
5441 * Offset the vertical location of all children of this view by the specified number of pixels.
5442 *
5443 * @param offset the number of pixels to offset
5444 *
5445 * @hide
5446 */
5447 public void offsetChildrenTopAndBottom(int offset) {
5448 final int count = mChildrenCount;
5449 final View[] children = mChildren;
Romain Guy5549cb52013-05-06 18:42:08 -07005450 boolean invalidate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451
5452 for (int i = 0; i < count; i++) {
5453 final View v = children[i];
5454 v.mTop += offset;
5455 v.mBottom += offset;
Chris Craik64a12e12014-03-28 18:12:12 -07005456 if (v.mRenderNode != null) {
Romain Guy5549cb52013-05-06 18:42:08 -07005457 invalidate = true;
Chris Craik64a12e12014-03-28 18:12:12 -07005458 v.mRenderNode.offsetTopAndBottom(offset);
Chet Haasea1cff502012-02-21 13:43:44 -08005459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 }
Romain Guy5549cb52013-05-06 18:42:08 -07005461
5462 if (invalidate) {
5463 invalidateViewProperty(false, false);
5464 }
Guang Zhu84e25092014-05-01 21:12:55 -07005465 notifySubtreeAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
5467
Alan Viverettebe463f22016-01-21 10:50:10 -05005468 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005470 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
5471 // but for some simple tests it can be useful. If we don't have attach info this
5472 // will allocate memory.
5473 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01005474 rect.set(r);
5475
5476 if (!child.hasIdentityMatrix()) {
George Mount002d43d2014-11-11 12:54:43 -08005477 child.getMatrix().mapRect(rect);
Gilles Debunnecea45132011-11-24 02:19:27 +01005478 }
5479
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005480 final int dx = child.mLeft - mScrollX;
5481 final int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01005482
5483 rect.offset(dx, dy);
5484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01005486 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005487 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
5488 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01005489 position[0] = offset.x;
5490 position[1] = offset.y;
5491 child.getMatrix().mapPoints(position);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005492 offset.x = Math.round(position[0]);
5493 offset.y = Math.round(position[1]);
Gilles Debunnecea45132011-11-24 02:19:27 +01005494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 offset.x += dx;
5496 offset.y += dy;
5497 }
Gilles Debunnecea45132011-11-24 02:19:27 +01005498
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005499 final int width = mRight - mLeft;
5500 final int height = mBottom - mTop;
5501
George Mount002d43d2014-11-11 12:54:43 -08005502 boolean rectIsVisible = true;
Adam Powell35da41e2014-12-10 18:59:34 -08005503 if (mParent == null ||
5504 (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005505 // Clip to bounds.
5506 rectIsVisible = rect.intersect(0, 0, width, height);
Gilles Debunnecea45132011-11-24 02:19:27 +01005507 }
5508
George Mount002d43d2014-11-11 12:54:43 -08005509 if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005510 // Clip to padding.
George Mount002d43d2014-11-11 12:54:43 -08005511 rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop,
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005512 width - mPaddingRight, height - mPaddingBottom);
George Mount002d43d2014-11-11 12:54:43 -08005513 }
5514
5515 if (rectIsVisible && mClipBounds != null) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005516 // Clip to clipBounds.
George Mount002d43d2014-11-11 12:54:43 -08005517 rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right,
5518 mClipBounds.bottom);
5519 }
Alan Viverettec45b1d42015-11-16 15:38:59 -05005520 r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top),
5521 (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom));
George Mount002d43d2014-11-11 12:54:43 -08005522 if (rectIsVisible && mParent != null) {
5523 rectIsVisible = mParent.getChildVisibleRect(this, r, offset);
5524 }
5525 return rectIsVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 }
5527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 @Override
Chet Haase9c087442011-01-12 16:20:16 -08005529 public final void layout(int l, int t, int r, int b) {
Chet Haase430742f2013-04-12 11:18:36 -07005530 if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
Chet Haase7dd4a532012-04-16 13:35:09 -07005531 if (mTransition != null) {
5532 mTransition.layoutChange(this);
5533 }
Chet Haase9c087442011-01-12 16:20:16 -08005534 super.layout(l, t, r, b);
5535 } else {
5536 // record the fact that we noop'd it; request layout when transition finishes
Chet Haaseb9895022013-04-02 15:10:58 -07005537 mLayoutCalledWhileSuppressed = true;
Chet Haase9c087442011-01-12 16:20:16 -08005538 }
5539 }
5540
Chet Haase9c087442011-01-12 16:20:16 -08005541 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 protected abstract void onLayout(boolean changed,
5543 int l, int t, int r, int b);
5544
5545 /**
5546 * Indicates whether the view group has the ability to animate its children
5547 * after the first layout.
5548 *
5549 * @return true if the children can be animated, false otherwise
5550 */
5551 protected boolean canAnimate() {
5552 return mLayoutAnimationController != null;
5553 }
5554
5555 /**
5556 * Runs the layout animation. Calling this method triggers a relayout of
5557 * this view group.
5558 */
5559 public void startLayoutAnimation() {
5560 if (mLayoutAnimationController != null) {
5561 mGroupFlags |= FLAG_RUN_ANIMATION;
5562 requestLayout();
5563 }
5564 }
5565
5566 /**
5567 * Schedules the layout animation to be played after the next layout pass
5568 * of this view group. This can be used to restart the layout animation
5569 * when the content of the view group changes or when the activity is
5570 * paused and resumed.
5571 */
5572 public void scheduleLayoutAnimation() {
5573 mGroupFlags |= FLAG_RUN_ANIMATION;
5574 }
5575
5576 /**
5577 * Sets the layout animation controller used to animate the group's
5578 * children after the first layout.
5579 *
5580 * @param controller the animation controller
5581 */
5582 public void setLayoutAnimation(LayoutAnimationController controller) {
5583 mLayoutAnimationController = controller;
5584 if (mLayoutAnimationController != null) {
5585 mGroupFlags |= FLAG_RUN_ANIMATION;
5586 }
5587 }
5588
5589 /**
5590 * Returns the layout animation controller used to animate the group's
5591 * children.
5592 *
5593 * @return the current animation controller
5594 */
5595 public LayoutAnimationController getLayoutAnimation() {
5596 return mLayoutAnimationController;
5597 }
5598
5599 /**
5600 * Indicates whether the children's drawing cache is used during a layout
5601 * animation. By default, the drawing cache is enabled but this will prevent
5602 * nested layout animations from working. To nest animations, you must disable
5603 * the cache.
5604 *
5605 * @return true if the animation cache is enabled, false otherwise
5606 *
5607 * @see #setAnimationCacheEnabled(boolean)
5608 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005609 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005610 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005611 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 public boolean isAnimationCacheEnabled() {
5614 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
5615 }
5616
5617 /**
5618 * Enables or disables the children's drawing cache during a layout animation.
5619 * By default, the drawing cache is enabled but this will prevent nested
5620 * layout animations from working. To nest animations, you must disable the
5621 * cache.
5622 *
5623 * @param enabled true to enable the animation cache, false otherwise
5624 *
5625 * @see #isAnimationCacheEnabled()
5626 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005627 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005628 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005629 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 */
5631 public void setAnimationCacheEnabled(boolean enabled) {
5632 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
5633 }
5634
5635 /**
5636 * Indicates whether this ViewGroup will always try to draw its children using their
5637 * drawing cache. By default this property is enabled.
5638 *
5639 * @return true if the animation cache is enabled, false otherwise
5640 *
5641 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5642 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5643 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005644 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005645 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005646 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 public boolean isAlwaysDrawnWithCacheEnabled() {
5649 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
5650 }
5651
5652 /**
5653 * Indicates whether this ViewGroup will always try to draw its children using their
5654 * drawing cache. This property can be set to true when the cache rendering is
5655 * slightly different from the children's normal rendering. Renderings can be different,
5656 * for instance, when the cache's quality is set to low.
5657 *
5658 * When this property is disabled, the ViewGroup will use the drawing cache of its
5659 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
5660 * when to start using the drawing cache and when to stop using it.
5661 *
5662 * @param always true to always draw with the drawing cache, false otherwise
5663 *
5664 * @see #isAlwaysDrawnWithCacheEnabled()
5665 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5666 * @see View#setDrawingCacheEnabled(boolean)
5667 * @see View#setDrawingCacheQuality(int)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005668 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005669 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005670 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 */
5672 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
5673 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
5674 }
5675
5676 /**
5677 * Indicates whether the ViewGroup is currently drawing its children using
5678 * their drawing cache.
5679 *
5680 * @return true if children should be drawn with their cache, false otherwise
5681 *
5682 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5683 * @see #setChildrenDrawnWithCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005684 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005685 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005686 * Child views may no longer be forced to cache their rendering state by their parents.
5687 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 protected boolean isChildrenDrawnWithCacheEnabled() {
5690 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
5691 }
5692
5693 /**
5694 * Tells the ViewGroup to draw its children using their drawing cache. This property
5695 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
5696 * will be used only if it has been enabled.
5697 *
5698 * Subclasses should call this method to start and stop using the drawing cache when
5699 * they perform performance sensitive operations, like scrolling or animating.
5700 *
5701 * @param enabled true if children should be drawn with their cache, false otherwise
5702 *
5703 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5704 * @see #isChildrenDrawnWithCacheEnabled()
Chris Craik5a6bbae2015-04-10 17:41:34 -07005705 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005706 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005707 * Child views may no longer be forced to cache their rendering state by their parents.
5708 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 */
5710 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
5711 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
5712 }
5713
Romain Guy293451e2009-11-04 13:59:48 -08005714 /**
5715 * Indicates whether the ViewGroup is drawing its children in the order defined by
5716 * {@link #getChildDrawingOrder(int, int)}.
5717 *
5718 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
5719 * false otherwise
5720 *
5721 * @see #setChildrenDrawingOrderEnabled(boolean)
5722 * @see #getChildDrawingOrder(int, int)
5723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005724 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08005725 protected boolean isChildrenDrawingOrderEnabled() {
5726 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
5727 }
5728
5729 /**
5730 * Tells the ViewGroup whether to draw its children in the order defined by the method
5731 * {@link #getChildDrawingOrder(int, int)}.
Chris Craike83cbd42014-09-03 17:52:24 -07005732 * <p>
5733 * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)},
5734 * will override custom child ordering done via this method.
Romain Guy293451e2009-11-04 13:59:48 -08005735 *
5736 * @param enabled true if the order of the children when drawing is determined by
5737 * {@link #getChildDrawingOrder(int, int)}, false otherwise
5738 *
5739 * @see #isChildrenDrawingOrderEnabled()
5740 * @see #getChildDrawingOrder(int, int)
5741 */
5742 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
5743 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
5744 }
5745
Svetoslav6254f482013-06-04 17:22:14 -07005746 private boolean hasBooleanFlag(int flag) {
Philip Milnef091b662013-02-27 11:15:21 -08005747 return (mGroupFlags & flag) == flag;
5748 }
5749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 private void setBooleanFlag(int flag, boolean value) {
5751 if (value) {
5752 mGroupFlags |= flag;
5753 } else {
5754 mGroupFlags &= ~flag;
5755 }
5756 }
5757
5758 /**
5759 * Returns an integer indicating what types of drawing caches are kept in memory.
5760 *
5761 * @see #setPersistentDrawingCache(int)
5762 * @see #setAnimationCacheEnabled(boolean)
5763 *
5764 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
5765 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5766 * and {@link #PERSISTENT_ALL_CACHES}
5767 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005768 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07005770 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
5772 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
5773 })
5774 public int getPersistentDrawingCache() {
5775 return mPersistentDrawingCache;
5776 }
5777
5778 /**
5779 * Indicates what types of drawing caches should be kept in memory after
5780 * they have been created.
5781 *
5782 * @see #getPersistentDrawingCache()
5783 * @see #setAnimationCacheEnabled(boolean)
5784 *
5785 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
5786 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5787 * and {@link #PERSISTENT_ALL_CACHES}
5788 */
5789 public void setPersistentDrawingCache(int drawingCacheToKeep) {
5790 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
5791 }
5792
Philip Milnef091b662013-02-27 11:15:21 -08005793 private void setLayoutMode(int layoutMode, boolean explicitly) {
5794 mLayoutMode = layoutMode;
5795 setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly);
5796 }
5797
5798 /**
5799 * Recursively traverse the view hierarchy, resetting the layoutMode of any
5800 * descendants that had inherited a different layoutMode from a previous parent.
5801 * Recursion terminates when a descendant's mode is:
5802 * <ul>
5803 * <li>Undefined</li>
5804 * <li>The same as the root node's</li>
5805 * <li>A mode that had been explicitly set</li>
5806 * <ul/>
5807 * The first two clauses are optimizations.
5808 * @param layoutModeOfRoot
5809 */
5810 @Override
5811 void invalidateInheritedLayoutMode(int layoutModeOfRoot) {
5812 if (mLayoutMode == LAYOUT_MODE_UNDEFINED ||
5813 mLayoutMode == layoutModeOfRoot ||
Svetoslav6254f482013-06-04 17:22:14 -07005814 hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnef091b662013-02-27 11:15:21 -08005815 return;
5816 }
5817 setLayoutMode(LAYOUT_MODE_UNDEFINED, false);
5818
5819 // apply recursively
5820 for (int i = 0, N = getChildCount(); i < N; i++) {
5821 getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot);
5822 }
5823 }
5824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005826 * Returns the basis of alignment during layout operations on this ViewGroup:
Philip Milne7b757812012-09-19 18:13:44 -07005827 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milnecfb631b2012-10-26 10:51:46 -07005828 * <p>
5829 * If no layoutMode was explicitly set, either programmatically or in an XML resource,
5830 * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
5831 * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005832 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005833 * @return the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005834 *
5835 * @see #setLayoutMode(int)
5836 */
5837 public int getLayoutMode() {
Philip Milnecfb631b2012-10-26 10:51:46 -07005838 if (mLayoutMode == LAYOUT_MODE_UNDEFINED) {
Philip Milnef091b662013-02-27 11:15:21 -08005839 int inheritedLayoutMode = (mParent instanceof ViewGroup) ?
5840 ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT;
5841 setLayoutMode(inheritedLayoutMode, false);
Philip Milnecfb631b2012-10-26 10:51:46 -07005842 }
Philip Milne1557fd72012-04-04 23:41:34 -07005843 return mLayoutMode;
5844 }
5845
5846 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005847 * Sets the basis of alignment during the layout of this ViewGroup.
Philip Milne7b757812012-09-19 18:13:44 -07005848 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
5849 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005850 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005851 * @param layoutMode the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005852 *
5853 * @see #getLayoutMode()
Scott Main27a85082013-06-10 10:39:48 -07005854 * @attr ref android.R.styleable#ViewGroup_layoutMode
Philip Milne1557fd72012-04-04 23:41:34 -07005855 */
5856 public void setLayoutMode(int layoutMode) {
5857 if (mLayoutMode != layoutMode) {
Philip Milnef091b662013-02-27 11:15:21 -08005858 invalidateInheritedLayoutMode(layoutMode);
5859 setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED);
Philip Milne1557fd72012-04-04 23:41:34 -07005860 requestLayout();
5861 }
5862 }
5863
5864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 * Returns a new set of layout parameters based on the supplied attributes set.
5866 *
5867 * @param attrs the attributes to build the layout parameters from
5868 *
5869 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5870 * of its descendants
5871 */
5872 public LayoutParams generateLayoutParams(AttributeSet attrs) {
5873 return new LayoutParams(getContext(), attrs);
5874 }
5875
5876 /**
5877 * Returns a safe set of layout parameters based on the supplied layout params.
5878 * When a ViewGroup is passed a View whose layout params do not pass the test of
5879 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
5880 * is invoked. This method should return a new set of layout params suitable for
5881 * this ViewGroup, possibly by copying the appropriate attributes from the
5882 * specified set of layout params.
5883 *
5884 * @param p The layout parameters to convert into a suitable set of layout parameters
5885 * for this ViewGroup.
5886 *
5887 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5888 * of its descendants
5889 */
5890 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Chet Haase4610eef2015-12-03 07:38:11 -08005891 return p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 }
5893
5894 /**
5895 * Returns a set of default layout parameters. These parameters are requested
5896 * when the View passed to {@link #addView(View)} has no layout parameters
5897 * already set. If null is returned, an exception is thrown from addView.
5898 *
5899 * @return a set of default layout parameters or null
5900 */
5901 protected LayoutParams generateDefaultLayoutParams() {
5902 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
5903 }
5904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 @Override
5906 protected void debug(int depth) {
5907 super.debug(depth);
5908 String output;
5909
5910 if (mFocused != null) {
5911 output = debugIndent(depth);
5912 output += "mFocused";
5913 Log.d(VIEW_LOG_TAG, output);
5914 }
5915 if (mChildrenCount != 0) {
5916 output = debugIndent(depth);
5917 output += "{";
5918 Log.d(VIEW_LOG_TAG, output);
5919 }
5920 int count = mChildrenCount;
5921 for (int i = 0; i < count; i++) {
5922 View child = mChildren[i];
5923 child.debug(depth + 1);
5924 }
5925
5926 if (mChildrenCount != 0) {
5927 output = debugIndent(depth);
5928 output += "}";
5929 Log.d(VIEW_LOG_TAG, output);
5930 }
5931 }
5932
5933 /**
5934 * Returns the position in the group of the specified child view.
5935 *
5936 * @param child the view for which to get the position
5937 * @return a positive integer representing the position of the view in the
5938 * group, or -1 if the view does not exist in the group
5939 */
5940 public int indexOfChild(View child) {
5941 final int count = mChildrenCount;
5942 final View[] children = mChildren;
5943 for (int i = 0; i < count; i++) {
5944 if (children[i] == child) {
5945 return i;
5946 }
5947 }
5948 return -1;
5949 }
5950
5951 /**
5952 * Returns the number of children in the group.
5953 *
5954 * @return a positive integer representing the number of children in
5955 * the group
5956 */
5957 public int getChildCount() {
5958 return mChildrenCount;
5959 }
5960
5961 /**
5962 * Returns the view at the specified position in the group.
5963 *
5964 * @param index the position at which to get the view from
5965 * @return the view at the specified position or null if the position
5966 * does not exist within the group
5967 */
5968 public View getChildAt(int index) {
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005969 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 return null;
5971 }
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005972 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 }
5974
5975 /**
5976 * Ask all of the children of this view to measure themselves, taking into
5977 * account both the MeasureSpec requirements for this view and its padding.
5978 * We skip children that are in the GONE state The heavy lifting is done in
5979 * getChildMeasureSpec.
5980 *
5981 * @param widthMeasureSpec The width requirements for this view
5982 * @param heightMeasureSpec The height requirements for this view
5983 */
5984 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
5985 final int size = mChildrenCount;
5986 final View[] children = mChildren;
5987 for (int i = 0; i < size; ++i) {
5988 final View child = children[i];
5989 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
5990 measureChild(child, widthMeasureSpec, heightMeasureSpec);
5991 }
5992 }
5993 }
5994
5995 /**
5996 * Ask one of the children of this view to measure itself, taking into
5997 * account both the MeasureSpec requirements for this view and its padding.
5998 * The heavy lifting is done in getChildMeasureSpec.
5999 *
6000 * @param child The child to measure
6001 * @param parentWidthMeasureSpec The width requirements for this view
6002 * @param parentHeightMeasureSpec The height requirements for this view
6003 */
6004 protected void measureChild(View child, int parentWidthMeasureSpec,
6005 int parentHeightMeasureSpec) {
6006 final LayoutParams lp = child.getLayoutParams();
6007
6008 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6009 mPaddingLeft + mPaddingRight, lp.width);
6010 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6011 mPaddingTop + mPaddingBottom, lp.height);
6012
6013 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6014 }
6015
6016 /**
6017 * Ask one of the children of this view to measure itself, taking into
6018 * account both the MeasureSpec requirements for this view and its padding
6019 * and margins. The child must have MarginLayoutParams The heavy lifting is
6020 * done in getChildMeasureSpec.
6021 *
6022 * @param child The child to measure
6023 * @param parentWidthMeasureSpec The width requirements for this view
6024 * @param widthUsed Extra space that has been used up by the parent
6025 * horizontally (possibly by other children of the parent)
6026 * @param parentHeightMeasureSpec The height requirements for this view
6027 * @param heightUsed Extra space that has been used up by the parent
6028 * vertically (possibly by other children of the parent)
6029 */
6030 protected void measureChildWithMargins(View child,
6031 int parentWidthMeasureSpec, int widthUsed,
6032 int parentHeightMeasureSpec, int heightUsed) {
6033 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
6034
6035 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6036 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
6037 + widthUsed, lp.width);
6038 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6039 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
6040 + heightUsed, lp.height);
6041
6042 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6043 }
6044
6045 /**
6046 * Does the hard part of measureChildren: figuring out the MeasureSpec to
6047 * pass to a particular child. This method figures out the right MeasureSpec
6048 * for one dimension (height or width) of one child view.
6049 *
6050 * The goal is to combine information from our MeasureSpec with the
6051 * LayoutParams of the child to get the best possible results. For example,
6052 * if the this view knows its size (because its MeasureSpec has a mode of
6053 * EXACTLY), and the child has indicated in its LayoutParams that it wants
6054 * to be the same size as the parent, the parent should ask the child to
6055 * layout given an exact size.
6056 *
6057 * @param spec The requirements for this view
6058 * @param padding The padding of this view for the current dimension and
6059 * margins, if applicable
6060 * @param childDimension How big the child wants to be in the current
6061 * dimension
6062 * @return a MeasureSpec integer for the child
6063 */
6064 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
6065 int specMode = MeasureSpec.getMode(spec);
6066 int specSize = MeasureSpec.getSize(spec);
6067
6068 int size = Math.max(0, specSize - padding);
6069
6070 int resultSize = 0;
6071 int resultMode = 0;
6072
6073 switch (specMode) {
6074 // Parent has imposed an exact size on us
6075 case MeasureSpec.EXACTLY:
6076 if (childDimension >= 0) {
6077 resultSize = childDimension;
6078 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006079 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 // Child wants to be our size. So be it.
6081 resultSize = size;
6082 resultMode = MeasureSpec.EXACTLY;
6083 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6084 // Child wants to determine its own size. It can't be
6085 // bigger than us.
6086 resultSize = size;
6087 resultMode = MeasureSpec.AT_MOST;
6088 }
6089 break;
6090
6091 // Parent has imposed a maximum size on us
6092 case MeasureSpec.AT_MOST:
6093 if (childDimension >= 0) {
6094 // Child wants a specific size... so be it
6095 resultSize = childDimension;
6096 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006097 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 // Child wants to be our size, but our size is not fixed.
6099 // Constrain child to not be bigger than us.
6100 resultSize = size;
6101 resultMode = MeasureSpec.AT_MOST;
6102 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6103 // Child wants to determine its own size. It can't be
6104 // bigger than us.
6105 resultSize = size;
6106 resultMode = MeasureSpec.AT_MOST;
6107 }
6108 break;
6109
6110 // Parent asked to see how big we want to be
6111 case MeasureSpec.UNSPECIFIED:
6112 if (childDimension >= 0) {
6113 // Child wants a specific size... let him have it
6114 resultSize = childDimension;
6115 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006116 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 // Child wants to be our size... find out how big it should
6118 // be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006119 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 resultMode = MeasureSpec.UNSPECIFIED;
6121 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6122 // Child wants to determine its own size.... find out how
6123 // big it should be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006124 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 resultMode = MeasureSpec.UNSPECIFIED;
6126 }
6127 break;
6128 }
Tor Norbye67568642015-03-31 07:47:23 -07006129 //noinspection ResourceType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
6131 }
6132
6133
6134 /**
6135 * Removes any pending animations for views that have been removed. Call
6136 * this if you don't want animations for exiting views to stack up.
6137 */
6138 public void clearDisappearingChildren() {
John Reckca7a9da2014-03-05 16:29:07 -08006139 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6140 if (disappearingChildren != null) {
6141 final int count = disappearingChildren.size();
6142 for (int i = 0; i < count; i++) {
6143 final View view = disappearingChildren.get(i);
6144 if (view.mAttachInfo != null) {
6145 view.dispatchDetachedFromWindow();
6146 }
6147 view.clearAnimation();
6148 }
6149 disappearingChildren.clear();
Chet Haaseb85967b2012-03-26 14:37:51 -07006150 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 }
6152 }
6153
6154 /**
6155 * Add a view which is removed from mChildren but still needs animation
6156 *
6157 * @param v View to add
6158 */
6159 private void addDisappearingView(View v) {
6160 ArrayList<View> disappearingChildren = mDisappearingChildren;
6161
6162 if (disappearingChildren == null) {
6163 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
6164 }
6165
6166 disappearingChildren.add(v);
6167 }
6168
6169 /**
6170 * Cleanup a view when its animation is done. This may mean removing it from
6171 * the list of disappearing views.
6172 *
6173 * @param view The view whose animation has finished
6174 * @param animation The animation, cannot be null
6175 */
Chet Haase64a48c12012-02-13 16:33:29 -08006176 void finishAnimatingView(final View view, Animation animation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6178 if (disappearingChildren != null) {
6179 if (disappearingChildren.contains(view)) {
6180 disappearingChildren.remove(view);
6181
6182 if (view.mAttachInfo != null) {
6183 view.dispatchDetachedFromWindow();
6184 }
6185
6186 view.clearAnimation();
6187 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6188 }
6189 }
6190
6191 if (animation != null && !animation.getFillAfter()) {
6192 view.clearAnimation();
6193 }
6194
Dianne Hackborn4702a852012-08-17 15:18:29 -07006195 if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 view.onAnimationEnd();
6197 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
6198 // so we'd rather be safe than sorry
Dianne Hackborn4702a852012-08-17 15:18:29 -07006199 view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 // Draw one more frame after the animation is done
6201 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6202 }
6203 }
6204
Chet Haaseb20db3e2010-09-10 13:07:30 -07006205 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07006206 * Utility function called by View during invalidation to determine whether a view that
6207 * is invisible or gone should still be invalidated because it is being transitioned (and
6208 * therefore still needs to be drawn).
6209 */
6210 boolean isViewTransitioning(View view) {
6211 return (mTransitioningViews != null && mTransitioningViews.contains(view));
6212 }
6213
6214 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07006215 * This method tells the ViewGroup that the given View object, which should have this
6216 * ViewGroup as its parent,
6217 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
6218 * is removed from its parent. This allows animations, such as those used by
6219 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
6220 * the removal of views. A call to this method should always be accompanied by a later call
6221 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
6222 * so that the View finally gets removed.
6223 *
6224 * @param view The View object to be kept visible even if it gets removed from its parent.
6225 */
6226 public void startViewTransition(View view) {
6227 if (view.mParent == this) {
6228 if (mTransitioningViews == null) {
6229 mTransitioningViews = new ArrayList<View>();
6230 }
6231 mTransitioningViews.add(view);
6232 }
6233 }
6234
6235 /**
6236 * This method should always be called following an earlier call to
6237 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
6238 * and will no longer be displayed. Note that this method does not perform the functionality
6239 * of removing a view from its parent; it just discontinues the display of a View that
6240 * has previously been removed.
6241 *
6242 * @return view The View object that has been removed but is being kept around in the visible
6243 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
6244 */
6245 public void endViewTransition(View view) {
6246 if (mTransitioningViews != null) {
6247 mTransitioningViews.remove(view);
6248 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6249 if (disappearingChildren != null && disappearingChildren.contains(view)) {
6250 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006251 if (mVisibilityChangingChildren != null &&
6252 mVisibilityChangingChildren.contains(view)) {
6253 mVisibilityChangingChildren.remove(view);
6254 } else {
6255 if (view.mAttachInfo != null) {
6256 view.dispatchDetachedFromWindow();
6257 }
6258 if (view.mParent != null) {
6259 view.mParent = null;
6260 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07006261 }
Chet Haaseb85967b2012-03-26 14:37:51 -07006262 invalidate();
Chet Haaseb20db3e2010-09-10 13:07:30 -07006263 }
6264 }
6265 }
6266
Chet Haase21cd1382010-09-01 17:42:29 -07006267 private LayoutTransition.TransitionListener mLayoutTransitionListener =
6268 new LayoutTransition.TransitionListener() {
6269 @Override
6270 public void startTransition(LayoutTransition transition, ViewGroup container,
6271 View view, int transitionType) {
6272 // We only care about disappearing items, since we need special logic to keep
6273 // those items visible after they've been 'removed'
6274 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006275 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006276 }
6277 }
6278
6279 @Override
6280 public void endTransition(LayoutTransition transition, ViewGroup container,
6281 View view, int transitionType) {
Chet Haaseb9895022013-04-02 15:10:58 -07006282 if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) {
Chet Haase9c087442011-01-12 16:20:16 -08006283 requestLayout();
Chet Haaseb9895022013-04-02 15:10:58 -07006284 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08006285 }
Chet Haase21cd1382010-09-01 17:42:29 -07006286 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006287 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006288 }
6289 }
6290 };
6291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 /**
Chet Haaseb9895022013-04-02 15:10:58 -07006293 * Tells this ViewGroup to suppress all layout() calls until layout
6294 * suppression is disabled with a later call to suppressLayout(false).
6295 * When layout suppression is disabled, a requestLayout() call is sent
6296 * if layout() was attempted while layout was being suppressed.
6297 *
6298 * @hide
6299 */
6300 public void suppressLayout(boolean suppress) {
6301 mSuppressLayout = suppress;
6302 if (!suppress) {
6303 if (mLayoutCalledWhileSuppressed) {
6304 requestLayout();
6305 mLayoutCalledWhileSuppressed = false;
6306 }
6307 }
6308 }
6309
6310 /**
Chet Haase199acdf2013-07-24 18:40:55 -07006311 * Returns whether layout calls on this container are currently being
6312 * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}.
6313 *
6314 * @return true if layout calls are currently suppressed, false otherwise.
6315 *
6316 * @hide
6317 */
6318 public boolean isLayoutSuppressed() {
6319 return mSuppressLayout;
6320 }
6321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 @Override
6323 public boolean gatherTransparentRegion(Region region) {
6324 // If no transparent regions requested, we are always opaque.
Dianne Hackborn4702a852012-08-17 15:18:29 -07006325 final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 if (meOpaque && region == null) {
6327 // The caller doesn't care about the region, so stop now.
6328 return true;
6329 }
6330 super.gatherTransparentRegion(region);
6331 final View[] children = mChildren;
6332 final int count = mChildrenCount;
6333 boolean noneOfTheChildrenAreTransparent = true;
6334 for (int i = 0; i < count; i++) {
6335 final View child = children[i];
Mathias Agopiane3381152010-12-02 15:19:36 -08006336 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 if (!child.gatherTransparentRegion(region)) {
6338 noneOfTheChildrenAreTransparent = false;
6339 }
6340 }
6341 }
6342 return meOpaque || noneOfTheChildrenAreTransparent;
6343 }
6344
Alan Viverettebe463f22016-01-21 10:50:10 -05006345 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 public void requestTransparentRegion(View child) {
6347 if (child != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006348 child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 if (mParent != null) {
6350 mParent.requestTransparentRegion(this);
6351 }
6352 }
6353 }
Romain Guy8506ab42009-06-11 17:35:47 -07006354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006355 @Override
Adam Powell46e38fd2014-02-03 10:16:49 -08006356 public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
6357 insets = super.dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006358 if (!insets.isConsumed()) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006359 final int count = getChildCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 for (int i = 0; i < count; i++) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006361 insets = getChildAt(i).dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006362 if (insets.isConsumed()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 break;
6364 }
6365 }
6366 }
Adam Powell46e38fd2014-02-03 10:16:49 -08006367 return insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 }
6369
6370 /**
6371 * Returns the animation listener to which layout animation events are
6372 * sent.
6373 *
6374 * @return an {@link android.view.animation.Animation.AnimationListener}
6375 */
6376 public Animation.AnimationListener getLayoutAnimationListener() {
6377 return mAnimationListener;
6378 }
6379
6380 @Override
6381 protected void drawableStateChanged() {
6382 super.drawableStateChanged();
6383
6384 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
6385 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6386 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
6387 + " child has duplicateParentState set to true");
6388 }
6389
6390 final View[] children = mChildren;
6391 final int count = mChildrenCount;
6392
6393 for (int i = 0; i < count; i++) {
6394 final View child = children[i];
6395 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
6396 child.refreshDrawableState();
6397 }
6398 }
6399 }
6400 }
6401
6402 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07006403 public void jumpDrawablesToCurrentState() {
6404 super.jumpDrawablesToCurrentState();
6405 final View[] children = mChildren;
6406 final int count = mChildrenCount;
6407 for (int i = 0; i < count; i++) {
6408 children[i].jumpDrawablesToCurrentState();
6409 }
6410 }
6411
6412 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006413 protected int[] onCreateDrawableState(int extraSpace) {
6414 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
6415 return super.onCreateDrawableState(extraSpace);
6416 }
6417
6418 int need = 0;
6419 int n = getChildCount();
6420 for (int i = 0; i < n; i++) {
6421 int[] childState = getChildAt(i).getDrawableState();
6422
6423 if (childState != null) {
6424 need += childState.length;
6425 }
6426 }
6427
6428 int[] state = super.onCreateDrawableState(extraSpace + need);
6429
6430 for (int i = 0; i < n; i++) {
6431 int[] childState = getChildAt(i).getDrawableState();
6432
6433 if (childState != null) {
6434 state = mergeDrawableStates(state, childState);
6435 }
6436 }
6437
6438 return state;
6439 }
6440
6441 /**
6442 * Sets whether this ViewGroup's drawable states also include
6443 * its children's drawable states. This is used, for example, to
6444 * make a group appear to be focused when its child EditText or button
6445 * is focused.
6446 */
6447 public void setAddStatesFromChildren(boolean addsStates) {
6448 if (addsStates) {
6449 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
6450 } else {
6451 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
6452 }
6453
6454 refreshDrawableState();
6455 }
6456
6457 /**
6458 * Returns whether this ViewGroup's drawable states also include
6459 * its children's drawable states. This is used, for example, to
6460 * make a group appear to be focused when its child EditText or button
6461 * is focused.
6462 */
6463 public boolean addStatesFromChildren() {
6464 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
6465 }
6466
6467 /**
Jeff Smitha45746e2012-07-19 14:19:24 -05006468 * If {@link #addStatesFromChildren} is true, refreshes this group's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 * drawable state (to include the states from its children).
6470 */
Alan Viverettebe463f22016-01-21 10:50:10 -05006471 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 public void childDrawableStateChanged(View child) {
6473 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6474 refreshDrawableState();
6475 }
6476 }
6477
6478 /**
6479 * Specifies the animation listener to which layout animation events must
6480 * be sent. Only
6481 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
6482 * and
6483 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
6484 * are invoked.
6485 *
6486 * @param animationListener the layout animation listener
6487 */
6488 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
6489 mAnimationListener = animationListener;
6490 }
6491
6492 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006493 * This method is called by LayoutTransition when there are 'changing' animations that need
6494 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
6495 * starts all pending transitions prior to the drawing phase in the current traversal.
6496 *
6497 * @param transition The LayoutTransition to be started on the next traversal.
6498 *
6499 * @hide
6500 */
6501 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006502 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07006503 if (viewAncestor != null) {
6504 viewAncestor.requestTransitionStart(transition);
6505 }
Chet Haasecca2c982011-05-20 14:34:18 -07006506 }
6507
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006508 /**
6509 * @hide
6510 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006511 @Override
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006512 public boolean resolveRtlPropertiesIfNeeded() {
6513 final boolean result = super.resolveRtlPropertiesIfNeeded();
6514 // We dont need to resolve the children RTL properties if nothing has changed for the parent
6515 if (result) {
6516 int count = getChildCount();
6517 for (int i = 0; i < count; i++) {
6518 final View child = getChildAt(i);
6519 if (child.isLayoutDirectionInherited()) {
6520 child.resolveRtlPropertiesIfNeeded();
6521 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006522 }
6523 }
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006524 return result;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006525 }
6526
6527 /**
6528 * @hide
6529 */
6530 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006531 public boolean resolveLayoutDirection() {
6532 final boolean result = super.resolveLayoutDirection();
6533 if (result) {
6534 int count = getChildCount();
6535 for (int i = 0; i < count; i++) {
6536 final View child = getChildAt(i);
6537 if (child.isLayoutDirectionInherited()) {
6538 child.resolveLayoutDirection();
6539 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006540 }
6541 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006542 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006543 }
6544
6545 /**
6546 * @hide
6547 */
6548 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006549 public boolean resolveTextDirection() {
6550 final boolean result = super.resolveTextDirection();
6551 if (result) {
6552 int count = getChildCount();
6553 for (int i = 0; i < count; i++) {
6554 final View child = getChildAt(i);
6555 if (child.isTextDirectionInherited()) {
6556 child.resolveTextDirection();
6557 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006558 }
6559 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006560 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006561 }
6562
6563 /**
6564 * @hide
6565 */
6566 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006567 public boolean resolveTextAlignment() {
6568 final boolean result = super.resolveTextAlignment();
6569 if (result) {
6570 int count = getChildCount();
6571 for (int i = 0; i < count; i++) {
6572 final View child = getChildAt(i);
6573 if (child.isTextAlignmentInherited()) {
6574 child.resolveTextAlignment();
6575 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006576 }
6577 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006578 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006579 }
6580
6581 /**
6582 * @hide
6583 */
6584 @Override
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006585 public void resolvePadding() {
6586 super.resolvePadding();
6587 int count = getChildCount();
6588 for (int i = 0; i < count; i++) {
6589 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006590 if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006591 child.resolvePadding();
6592 }
6593 }
6594 }
6595
6596 /**
6597 * @hide
6598 */
6599 @Override
6600 protected void resolveDrawables() {
6601 super.resolveDrawables();
6602 int count = getChildCount();
6603 for (int i = 0; i < count; i++) {
6604 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006605 if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006606 child.resolveDrawables();
6607 }
6608 }
6609 }
6610
Fabrice Di Meglio1e0ed6b2012-10-18 16:06:52 -07006611 /**
6612 * @hide
6613 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -07006614 @Override
6615 public void resolveLayoutParams() {
6616 super.resolveLayoutParams();
6617 int count = getChildCount();
6618 for (int i = 0; i < count; i++) {
6619 final View child = getChildAt(i);
6620 child.resolveLayoutParams();
6621 }
6622 }
6623
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006624 /**
6625 * @hide
6626 */
6627 @Override
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006628 public void resetResolvedLayoutDirection() {
6629 super.resetResolvedLayoutDirection();
6630
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006631 int count = getChildCount();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006632 for (int i = 0; i < count; i++) {
6633 final View child = getChildAt(i);
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006634 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07006635 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006636 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006637 }
6638 }
6639
6640 /**
6641 * @hide
6642 */
6643 @Override
6644 public void resetResolvedTextDirection() {
6645 super.resetResolvedTextDirection();
6646
6647 int count = getChildCount();
6648 for (int i = 0; i < count; i++) {
6649 final View child = getChildAt(i);
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -07006650 if (child.isTextDirectionInherited()) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006651 child.resetResolvedTextDirection();
6652 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006653 }
6654 }
6655
6656 /**
6657 * @hide
6658 */
6659 @Override
6660 public void resetResolvedTextAlignment() {
6661 super.resetResolvedTextAlignment();
6662
6663 int count = getChildCount();
6664 for (int i = 0; i < count; i++) {
6665 final View child = getChildAt(i);
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -07006666 if (child.isTextAlignmentInherited()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07006667 child.resetResolvedTextAlignment();
6668 }
6669 }
6670 }
6671
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006672 /**
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006673 * @hide
6674 */
6675 @Override
6676 public void resetResolvedPadding() {
6677 super.resetResolvedPadding();
6678
6679 int count = getChildCount();
6680 for (int i = 0; i < count; i++) {
6681 final View child = getChildAt(i);
6682 if (child.isLayoutDirectionInherited()) {
6683 child.resetResolvedPadding();
6684 }
6685 }
6686 }
6687
6688 /**
6689 * @hide
6690 */
6691 @Override
6692 protected void resetResolvedDrawables() {
6693 super.resetResolvedDrawables();
6694
6695 int count = getChildCount();
6696 for (int i = 0; i < count; i++) {
6697 final View child = getChildAt(i);
6698 if (child.isLayoutDirectionInherited()) {
6699 child.resetResolvedDrawables();
6700 }
6701 }
6702 }
6703
6704 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006705 * Return true if the pressed state should be delayed for children or descendants of this
6706 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
6707 * This prevents the pressed state from appearing when the user is actually trying to scroll
6708 * the content.
6709 *
6710 * The default implementation returns true for compatibility reasons. Subclasses that do
6711 * not scroll should generally override this method and return false.
6712 */
6713 public boolean shouldDelayChildPressedState() {
6714 return true;
6715 }
6716
Adam Powell10ba2772014-04-15 09:46:51 -07006717 /**
6718 * @inheritDoc
6719 */
6720 @Override
6721 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6722 return false;
6723 }
6724
6725 /**
6726 * @inheritDoc
6727 */
6728 @Override
6729 public void onNestedScrollAccepted(View child, View target, int axes) {
6730 mNestedScrollAxes = axes;
6731 }
6732
6733 /**
6734 * @inheritDoc
6735 *
6736 * <p>The default implementation of onStopNestedScroll calls
6737 * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p>
6738 */
6739 @Override
6740 public void onStopNestedScroll(View child) {
6741 // Stop any recursive nested scrolling.
6742 stopNestedScroll();
Adam Powelld4a22d42015-04-16 15:44:10 -07006743 mNestedScrollAxes = 0;
Adam Powell10ba2772014-04-15 09:46:51 -07006744 }
6745
6746 /**
6747 * @inheritDoc
6748 */
6749 @Override
6750 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6751 int dxUnconsumed, int dyUnconsumed) {
Chris Banes61d50702016-03-23 13:11:45 +00006752 // Re-dispatch up the tree by default
6753 dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
Adam Powell10ba2772014-04-15 09:46:51 -07006754 }
6755
6756 /**
6757 * @inheritDoc
6758 */
6759 @Override
6760 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
Chris Banes61d50702016-03-23 13:11:45 +00006761 // Re-dispatch up the tree by default
6762 dispatchNestedPreScroll(dx, dy, consumed, null);
Adam Powell10ba2772014-04-15 09:46:51 -07006763 }
6764
6765 /**
6766 * @inheritDoc
6767 */
6768 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07006769 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Chris Banes61d50702016-03-23 13:11:45 +00006770 // Re-dispatch up the tree by default
6771 return dispatchNestedFling(velocityX, velocityY, consumed);
Adam Powell10ba2772014-04-15 09:46:51 -07006772 }
6773
6774 /**
Adam Powellb72be592014-07-16 21:41:31 -07006775 * @inheritDoc
6776 */
6777 @Override
6778 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
Chris Banes61d50702016-03-23 13:11:45 +00006779 // Re-dispatch up the tree by default
6780 return dispatchNestedPreFling(velocityX, velocityY);
Adam Powellb72be592014-07-16 21:41:31 -07006781 }
6782
6783 /**
Adam Powell10ba2772014-04-15 09:46:51 -07006784 * Return the current axes of nested scrolling for this ViewGroup.
6785 *
6786 * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently
6787 * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p>
6788 *
6789 * @return Flags indicating the current axes of nested scrolling
6790 * @see #SCROLL_AXIS_HORIZONTAL
6791 * @see #SCROLL_AXIS_VERTICAL
6792 * @see #SCROLL_AXIS_NONE
6793 */
6794 public int getNestedScrollAxes() {
6795 return mNestedScrollAxes;
6796 }
6797
Philip Milned7dd8902012-01-26 16:55:30 -08006798 /** @hide */
6799 protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
6800 }
6801
George Mounte1803372014-02-26 19:00:52 +00006802 /** @hide */
6803 @Override
6804 public void captureTransitioningViews(List<View> transitioningViews) {
6805 if (getVisibility() != View.VISIBLE) {
6806 return;
6807 }
6808 if (isTransitionGroup()) {
6809 transitioningViews.add(this);
6810 } else {
6811 int count = getChildCount();
6812 for (int i = 0; i < count; i++) {
6813 View child = getChildAt(i);
6814 child.captureTransitioningViews(transitioningViews);
6815 }
6816 }
6817 }
6818
6819 /** @hide */
6820 @Override
George Mountabb352a2014-05-09 10:27:20 -07006821 public void findNamedViews(Map<String, View> namedElements) {
George Mountfe361d22014-07-08 17:25:25 -07006822 if (getVisibility() != VISIBLE && mGhostView == null) {
George Mounte1803372014-02-26 19:00:52 +00006823 return;
6824 }
George Mountabb352a2014-05-09 10:27:20 -07006825 super.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006826 int count = getChildCount();
6827 for (int i = 0; i < count; i++) {
6828 View child = getChildAt(i);
George Mountabb352a2014-05-09 10:27:20 -07006829 child.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006830 }
6831 }
6832
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 * LayoutParams are used by views to tell their parents how they want to be
6835 * laid out. See
6836 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
6837 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07006838 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 * <p>
6840 * The base LayoutParams class just describes how big the view wants to be
6841 * for both width and height. For each dimension, it can specify one of:
6842 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006843 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
6844 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
6846 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006847 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 * </ul>
6849 * There are subclasses of LayoutParams for different subclasses of
6850 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07006851 * LayoutParams which adds an X and Y value.</p>
6852 *
6853 * <div class="special reference">
6854 * <h3>Developer Guides</h3>
6855 * <p>For more information about creating user interface layouts, read the
6856 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
6857 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 *
6859 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
6860 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
6861 */
6862 public static class LayoutParams {
6863 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006864 * Special value for the height or width requested by a View.
6865 * FILL_PARENT means that the view wants to be as big as its parent,
6866 * minus the parent's padding, if any. This value is deprecated
6867 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 */
Romain Guy980a9382010-01-08 15:06:28 -08006869 @SuppressWarnings({"UnusedDeclaration"})
6870 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 public static final int FILL_PARENT = -1;
6872
6873 /**
6874 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08006875 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006876 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08006877 */
6878 public static final int MATCH_PARENT = -1;
6879
6880 /**
6881 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 * WRAP_CONTENT means that the view wants to be just large enough to fit
6883 * its own internal content, taking its own padding into account.
6884 */
6885 public static final int WRAP_CONTENT = -2;
6886
6887 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006888 * Information about how wide the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006889 * constants FILL_PARENT (replaced by MATCH_PARENT
6890 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006892 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006893 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6895 })
6896 public int width;
6897
6898 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006899 * Information about how tall the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006900 * constants FILL_PARENT (replaced by MATCH_PARENT
6901 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006902 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006903 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006904 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6906 })
6907 public int height;
6908
6909 /**
6910 * Used to animate layouts.
6911 */
6912 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
6913
6914 /**
6915 * Creates a new set of layout parameters. The values are extracted from
6916 * the supplied attributes set and context. The XML attributes mapped
6917 * to this set of layout parameters are:
6918 *
6919 * <ul>
6920 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006921 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6922 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006924 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6925 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 * </ul>
6927 *
6928 * @param c the application environment
6929 * @param attrs the set of attributes from which to extract the layout
6930 * parameters' values
6931 */
6932 public LayoutParams(Context c, AttributeSet attrs) {
6933 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
6934 setBaseAttributes(a,
6935 R.styleable.ViewGroup_Layout_layout_width,
6936 R.styleable.ViewGroup_Layout_layout_height);
6937 a.recycle();
6938 }
6939
6940 /**
6941 * Creates a new set of layout parameters with the specified width
6942 * and height.
6943 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006944 * @param width the width, either {@link #WRAP_CONTENT},
6945 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6946 * API Level 8), or a fixed size in pixels
6947 * @param height the height, either {@link #WRAP_CONTENT},
6948 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6949 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 */
6951 public LayoutParams(int width, int height) {
6952 this.width = width;
6953 this.height = height;
6954 }
6955
6956 /**
6957 * Copy constructor. Clones the width and height values of the source.
6958 *
6959 * @param source The layout params to copy from.
6960 */
6961 public LayoutParams(LayoutParams source) {
6962 this.width = source.width;
6963 this.height = source.height;
6964 }
6965
6966 /**
6967 * Used internally by MarginLayoutParams.
6968 * @hide
6969 */
6970 LayoutParams() {
6971 }
6972
6973 /**
Dave Burke579e1402012-10-18 20:41:55 -07006974 * Extracts the layout parameters from the supplied attributes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 *
6976 * @param a the style attributes to extract the parameters from
6977 * @param widthAttr the identifier of the width attribute
6978 * @param heightAttr the identifier of the height attribute
6979 */
6980 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
Dave Burke579e1402012-10-18 20:41:55 -07006981 width = a.getLayoutDimension(widthAttr, "layout_width");
6982 height = a.getLayoutDimension(heightAttr, "layout_height");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 }
6984
6985 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006986 * Resolve layout parameters depending on the layout direction. Subclasses that care about
6987 * layoutDirection changes should override this method. The default implementation does
6988 * nothing.
6989 *
6990 * @param layoutDirection the direction of the layout
6991 *
6992 * {@link View#LAYOUT_DIRECTION_LTR}
6993 * {@link View#LAYOUT_DIRECTION_RTL}
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006994 */
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07006995 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006996 }
6997
6998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 * Returns a String representation of this set of layout parameters.
7000 *
7001 * @param output the String to prepend to the internal representation
7002 * @return a String with the following format: output +
7003 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07007004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 * @hide
7006 */
7007 public String debug(String output) {
7008 return output + "ViewGroup.LayoutParams={ width="
7009 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
7010 }
7011
7012 /**
Philip Milne10ca24a2012-04-23 15:38:27 -07007013 * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
7014 *
7015 * @param view the view that contains these layout parameters
7016 * @param canvas the canvas on which to draw
7017 *
7018 * @hide
7019 */
Philip Milne7b757812012-09-19 18:13:44 -07007020 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07007021 }
7022
7023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 * Converts the specified size to a readable String.
7025 *
7026 * @param size the size to convert
7027 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07007028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 * @hide
7030 */
7031 protected static String sizeToString(int size) {
7032 if (size == WRAP_CONTENT) {
7033 return "wrap-content";
7034 }
Romain Guy980a9382010-01-08 15:06:28 -08007035 if (size == MATCH_PARENT) {
7036 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 }
7038 return String.valueOf(size);
7039 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007040
7041 /** @hide */
7042 void encode(@NonNull ViewHierarchyEncoder encoder) {
7043 encoder.beginObject(this);
7044 encodeProperties(encoder);
7045 encoder.endObject();
7046 }
7047
7048 /** @hide */
7049 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7050 encoder.addProperty("width", width);
7051 encoder.addProperty("height", height);
7052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 }
7054
7055 /**
7056 * Per-child layout information for layouts that support margins.
7057 * See
7058 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
7059 * for a list of all child view attributes that this class supports.
7060 */
7061 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
7062 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007063 * The left margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007064 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7065 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007067 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 public int leftMargin;
7069
7070 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007071 * The top margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007072 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7073 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007075 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 public int topMargin;
7077
7078 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007079 * The right margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007080 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7081 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007083 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 public int rightMargin;
7085
7086 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007087 * The bottom margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007088 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7089 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007091 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007092 public int bottomMargin;
7093
7094 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007095 * The start margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007096 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7097 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007098 */
7099 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007100 private int startMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007101
7102 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007103 * The end margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007104 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7105 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007106 */
7107 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007108 private int endMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007109
7110 /**
7111 * The default start and end margin.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007112 * @hide
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007113 */
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007114 public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007115
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007116 /**
7117 * Bit 0: layout direction
7118 * Bit 1: layout direction
7119 * Bit 2: left margin undefined
7120 * Bit 3: right margin undefined
7121 * Bit 4: is RTL compatibility mode
7122 * Bit 5: need resolution
7123 *
7124 * Bit 6 to 7 not used
7125 *
7126 * @hide
7127 */
7128 @ViewDebug.ExportedProperty(category = "layout", flagMapping = {
7129 @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK,
7130 equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"),
7131 @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK,
7132 equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"),
7133 @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK,
7134 equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"),
7135 @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK,
7136 equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"),
7137 @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK,
7138 equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK")
Jon Miranda4597e982014-07-29 07:25:49 -07007139 }, formatToHexString = true)
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007140 byte mMarginFlags;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007141
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007142 private static final int LAYOUT_DIRECTION_MASK = 0x00000003;
7143 private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004;
7144 private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008;
7145 private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010;
7146 private static final int NEED_RESOLUTION_MASK = 0x00000020;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007147
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007148 private static final int DEFAULT_MARGIN_RESOLVED = 0;
7149 private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007150
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 * Creates a new set of layout parameters. The values are extracted from
7153 * the supplied attributes set and context.
7154 *
7155 * @param c the application environment
7156 * @param attrs the set of attributes from which to extract the layout
7157 * parameters' values
7158 */
7159 public MarginLayoutParams(Context c, AttributeSet attrs) {
7160 super();
7161
7162 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
7163 setBaseAttributes(a,
7164 R.styleable.ViewGroup_MarginLayout_layout_width,
7165 R.styleable.ViewGroup_MarginLayout_layout_height);
7166
7167 int margin = a.getDimensionPixelSize(
7168 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
7169 if (margin >= 0) {
7170 leftMargin = margin;
7171 topMargin = margin;
7172 rightMargin= margin;
7173 bottomMargin = margin;
7174 } else {
7175 leftMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007176 R.styleable.ViewGroup_MarginLayout_layout_marginLeft,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007177 UNDEFINED_MARGIN);
7178 if (leftMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007179 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007180 leftMargin = DEFAULT_MARGIN_RESOLVED;
7181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 rightMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007183 R.styleable.ViewGroup_MarginLayout_layout_marginRight,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007184 UNDEFINED_MARGIN);
7185 if (rightMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007186 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007187 rightMargin = DEFAULT_MARGIN_RESOLVED;
7188 }
7189
7190 topMargin = a.getDimensionPixelSize(
7191 R.styleable.ViewGroup_MarginLayout_layout_marginTop,
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007192 DEFAULT_MARGIN_RESOLVED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 bottomMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007194 R.styleable.ViewGroup_MarginLayout_layout_marginBottom,
7195 DEFAULT_MARGIN_RESOLVED);
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007196
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007197 startMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007198 R.styleable.ViewGroup_MarginLayout_layout_marginStart,
7199 DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007200 endMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007201 R.styleable.ViewGroup_MarginLayout_layout_marginEnd,
7202 DEFAULT_MARGIN_RELATIVE);
7203
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007204 if (isMarginRelative()) {
7205 mMarginFlags |= NEED_RESOLUTION_MASK;
7206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
7208
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007209 final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
7210 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007211 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
7212 mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
7213 }
7214
7215 // Layout direction is LTR by default
7216 mMarginFlags |= LAYOUT_DIRECTION_LTR;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 a.recycle();
7219 }
7220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007221 public MarginLayoutParams(int width, int height) {
7222 super(width, height);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007223
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007224 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7225 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007226
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007227 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7228 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 }
7230
7231 /**
7232 * Copy constructor. Clones the width, height and margin values of the source.
7233 *
7234 * @param source The layout params to copy from.
7235 */
7236 public MarginLayoutParams(MarginLayoutParams source) {
7237 this.width = source.width;
7238 this.height = source.height;
7239
7240 this.leftMargin = source.leftMargin;
7241 this.topMargin = source.topMargin;
7242 this.rightMargin = source.rightMargin;
7243 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007244 this.startMargin = source.startMargin;
7245 this.endMargin = source.endMargin;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007246
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007247 this.mMarginFlags = source.mMarginFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 }
7249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 public MarginLayoutParams(LayoutParams source) {
7251 super(source);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007252
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007253 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7254 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007255
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007256 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7257 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 }
7259
7260 /**
Adam Powelld7600832014-07-01 15:22:50 -07007261 * @hide Used internally.
7262 */
7263 public final void copyMarginsFrom(MarginLayoutParams source) {
7264 this.leftMargin = source.leftMargin;
7265 this.topMargin = source.topMargin;
7266 this.rightMargin = source.rightMargin;
7267 this.bottomMargin = source.bottomMargin;
7268 this.startMargin = source.startMargin;
7269 this.endMargin = source.endMargin;
7270
7271 this.mMarginFlags = source.mMarginFlags;
7272 }
7273
7274 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007275 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
7276 * to be done so that the new margins are taken into account. Left and right margins may be
7277 * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
Adam Powella7a735f2014-10-09 12:54:52 -07007278 * Margin values should be positive.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 *
7280 * @param left the left margin size
7281 * @param top the top margin size
7282 * @param right the right margin size
7283 * @param bottom the bottom margin size
7284 *
7285 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
7286 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7287 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
7288 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7289 */
7290 public void setMargins(int left, int top, int right, int bottom) {
7291 leftMargin = left;
7292 topMargin = top;
7293 rightMargin = right;
7294 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007295 mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
7296 mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
7297 if (isMarginRelative()) {
7298 mMarginFlags |= NEED_RESOLUTION_MASK;
7299 } else {
7300 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007302 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007303
7304 /**
7305 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
7306 * needs to be done so that the new relative margins are taken into account. Left and right
7307 * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
Adam Powella7a735f2014-10-09 12:54:52 -07007308 * direction. Margin values should be positive.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007309 *
7310 * @param start the start margin size
7311 * @param top the top margin size
7312 * @param end the right margin size
7313 * @param bottom the bottom margin size
7314 *
7315 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7316 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7317 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7318 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7319 *
7320 * @hide
7321 */
7322 public void setMarginsRelative(int start, int top, int end, int bottom) {
7323 startMargin = start;
7324 topMargin = top;
7325 endMargin = end;
7326 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007327 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007328 }
7329
7330 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007331 * Sets the relative start margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007332 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007333 * @param start the start margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007334 *
7335 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7336 */
7337 public void setMarginStart(int start) {
7338 startMargin = start;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007339 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007340 }
7341
7342 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007343 * Returns the start margin in pixels.
7344 *
7345 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7346 *
7347 * @return the start margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007348 */
7349 public int getMarginStart() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007350 if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007351 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007352 doResolveMargins();
7353 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007354 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007355 case View.LAYOUT_DIRECTION_RTL:
7356 return rightMargin;
7357 case View.LAYOUT_DIRECTION_LTR:
7358 default:
7359 return leftMargin;
7360 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007361 }
7362
7363 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007364 * Sets the relative end 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 end the end margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007367 *
7368 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7369 */
7370 public void setMarginEnd(int end) {
7371 endMargin = end;
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 end margin in pixels.
7377 *
7378 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7379 *
7380 * @return the end margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007381 */
7382 public int getMarginEnd() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007383 if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin;
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 leftMargin;
7390 case View.LAYOUT_DIRECTION_LTR:
7391 default:
7392 return rightMargin;
7393 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007394 }
7395
7396 /**
7397 * Check if margins are relative.
7398 *
7399 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7400 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7401 *
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007402 * @return true if either marginStart or marginEnd has been set.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007403 */
7404 public boolean isMarginRelative() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007405 return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007406 }
7407
7408 /**
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007409 * Set the layout direction
7410 * @param layoutDirection the layout direction.
7411 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
7412 * or {@link View#LAYOUT_DIRECTION_RTL}.
7413 */
7414 public void setLayoutDirection(int layoutDirection) {
7415 if (layoutDirection != View.LAYOUT_DIRECTION_LTR &&
7416 layoutDirection != View.LAYOUT_DIRECTION_RTL) return;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007417 if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) {
7418 mMarginFlags &= ~LAYOUT_DIRECTION_MASK;
7419 mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK);
7420 if (isMarginRelative()) {
7421 mMarginFlags |= NEED_RESOLUTION_MASK;
7422 } else {
7423 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7424 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007425 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007426 }
7427
7428 /**
7429 * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or
7430 * {@link View#LAYOUT_DIRECTION_RTL}.
7431 *
7432 * @return the layout direction.
7433 */
7434 public int getLayoutDirection() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007435 return (mMarginFlags & LAYOUT_DIRECTION_MASK);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007436 }
7437
7438 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007439 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08007440 * may be overridden depending on layout direction.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007441 */
7442 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007443 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007444 setLayoutDirection(layoutDirection);
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007445
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007446 // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything
7447 // Will use the left and right margins if no relative margin is defined.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007448 if (!isMarginRelative() ||
7449 (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007450
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007451 // Proceed with resolution
7452 doResolveMargins();
7453 }
7454
7455 private void doResolveMargins() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007456 if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007457 // if left or right margins are not defined and if we have some start or end margin
7458 // defined then use those start and end margins.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007459 if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK
7460 && startMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007461 leftMargin = startMargin;
7462 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007463 if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK
7464 && endMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007465 rightMargin = endMargin;
7466 }
7467 } else {
7468 // We have some relative margins (either the start one or the end one or both). So use
7469 // them and override what has been defined for left and right margins. If either start
7470 // or end margin is not defined, just set it to default "0".
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007471 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007472 case View.LAYOUT_DIRECTION_RTL:
7473 leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7474 endMargin : DEFAULT_MARGIN_RESOLVED;
7475 rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7476 startMargin : DEFAULT_MARGIN_RESOLVED;
7477 break;
7478 case View.LAYOUT_DIRECTION_LTR:
7479 default:
7480 leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7481 startMargin : DEFAULT_MARGIN_RESOLVED;
7482 rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7483 endMargin : DEFAULT_MARGIN_RESOLVED;
7484 break;
7485 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007486 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007487 mMarginFlags &= ~NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007488 }
Philip Milne10ca24a2012-04-23 15:38:27 -07007489
Fabrice Di Meglio03b8d3a2012-09-27 17:05:27 -07007490 /**
7491 * @hide
7492 */
7493 public boolean isLayoutRtl() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007494 return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007495 }
7496
Philip Milne10ca24a2012-04-23 15:38:27 -07007497 /**
7498 * @hide
7499 */
7500 @Override
Philip Milne7b757812012-09-19 18:13:44 -07007501 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
7502 Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE;
7503
7504 fillDifference(canvas,
7505 view.getLeft() + oi.left,
7506 view.getTop() + oi.top,
7507 view.getRight() - oi.right,
7508 view.getBottom() - oi.bottom,
7509 leftMargin,
7510 topMargin,
7511 rightMargin,
7512 bottomMargin,
7513 paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07007514 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007515
7516 /** @hide */
7517 @Override
7518 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7519 super.encodeProperties(encoder);
7520 encoder.addProperty("leftMargin", leftMargin);
7521 encoder.addProperty("topMargin", topMargin);
7522 encoder.addProperty("rightMargin", rightMargin);
7523 encoder.addProperty("bottomMargin", bottomMargin);
7524 encoder.addProperty("startMargin", startMargin);
7525 encoder.addProperty("endMargin", endMargin);
7526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 }
Adam Powell2b342f02010-08-18 18:14:13 -07007528
Jeff Brown20e987b2010-08-23 12:01:02 -07007529 /* Describes a touched view and the ids of the pointers that it has captured.
7530 *
7531 * This code assumes that pointer ids are always in the range 0..31 such that
7532 * it can use a bitfield to track which pointer ids are present.
7533 * As it happens, the lower layers of the input dispatch pipeline also use the
7534 * same trick so the assumption should be safe here...
7535 */
7536 private static final class TouchTarget {
7537 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007538 private static final Object sRecycleLock = new Object[0];
Jeff Brown20e987b2010-08-23 12:01:02 -07007539 private static TouchTarget sRecycleBin;
7540 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07007541
Jeff Brown20e987b2010-08-23 12:01:02 -07007542 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07007543
Jeff Brown20e987b2010-08-23 12:01:02 -07007544 // The touched child view.
7545 public View child;
7546
7547 // The combined bit mask of pointer ids for all pointers captured by the target.
7548 public int pointerIdBits;
7549
7550 // The next target in the target list.
7551 public TouchTarget next;
7552
7553 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07007554 }
7555
Alan Viverettea7b85e62016-01-22 10:14:02 -05007556 public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {
7557 if (child == null) {
7558 throw new IllegalArgumentException("child must be non-null");
7559 }
7560
Jeff Brown20e987b2010-08-23 12:01:02 -07007561 final TouchTarget target;
7562 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07007563 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007564 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07007565 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07007566 target = sRecycleBin;
7567 sRecycleBin = target.next;
7568 sRecycledCount--;
7569 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007570 }
Adam Powell816c3be2010-08-23 18:00:05 -07007571 }
Jeff Brown20e987b2010-08-23 12:01:02 -07007572 target.child = child;
7573 target.pointerIdBits = pointerIdBits;
7574 return target;
7575 }
Adam Powell816c3be2010-08-23 18:00:05 -07007576
Jeff Brown20e987b2010-08-23 12:01:02 -07007577 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05007578 if (child == null) {
7579 throw new IllegalStateException("already recycled once");
7580 }
7581
Jeff Brown20e987b2010-08-23 12:01:02 -07007582 synchronized (sRecycleLock) {
7583 if (sRecycledCount < MAX_RECYCLED) {
7584 next = sRecycleBin;
7585 sRecycleBin = this;
7586 sRecycledCount += 1;
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007587 } else {
7588 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007589 }
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007590 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007591 }
7592 }
Adam Powell2b342f02010-08-18 18:14:13 -07007593 }
Jeff Brown87b7f802011-06-21 18:35:45 -07007594
7595 /* Describes a hovered view. */
7596 private static final class HoverTarget {
7597 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007598 private static final Object sRecycleLock = new Object[0];
Jeff Brown87b7f802011-06-21 18:35:45 -07007599 private static HoverTarget sRecycleBin;
7600 private static int sRecycledCount;
7601
7602 // The hovered child view.
7603 public View child;
7604
7605 // The next target in the target list.
7606 public HoverTarget next;
7607
7608 private HoverTarget() {
7609 }
7610
Alan Viverettea7b85e62016-01-22 10:14:02 -05007611 public static HoverTarget obtain(@NonNull View child) {
7612 if (child == null) {
7613 throw new IllegalArgumentException("child must be non-null");
7614 }
7615
Jeff Brown87b7f802011-06-21 18:35:45 -07007616 final HoverTarget target;
7617 synchronized (sRecycleLock) {
7618 if (sRecycleBin == null) {
7619 target = new HoverTarget();
7620 } else {
7621 target = sRecycleBin;
7622 sRecycleBin = target.next;
Alan Viverettea7b85e62016-01-22 10:14:02 -05007623 sRecycledCount--;
Jeff Brown87b7f802011-06-21 18:35:45 -07007624 target.next = null;
7625 }
7626 }
7627 target.child = child;
7628 return target;
7629 }
7630
7631 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05007632 if (child == null) {
7633 throw new IllegalStateException("already recycled once");
7634 }
7635
Jeff Brown87b7f802011-06-21 18:35:45 -07007636 synchronized (sRecycleLock) {
7637 if (sRecycledCount < MAX_RECYCLED) {
7638 next = sRecycleBin;
7639 sRecycleBin = this;
7640 sRecycledCount += 1;
7641 } else {
7642 next = null;
7643 }
7644 child = null;
7645 }
7646 }
7647 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007648
7649 /**
7650 * Pooled class that orderes the children of a ViewGroup from start
7651 * to end based on how they are laid out and the layout direction.
7652 */
7653 static class ChildListForAccessibility {
7654
7655 private static final int MAX_POOL_SIZE = 32;
7656
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007657 private static final SynchronizedPool<ChildListForAccessibility> sPool =
7658 new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007659
7660 private final ArrayList<View> mChildren = new ArrayList<View>();
7661
7662 private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
7663
7664 public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007665 ChildListForAccessibility list = sPool.acquire();
7666 if (list == null) {
7667 list = new ChildListForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007668 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007669 list.init(parent, sort);
7670 return list;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007671 }
7672
7673 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007674 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007675 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007676 }
7677
7678 public int getChildCount() {
7679 return mChildren.size();
7680 }
7681
7682 public View getChildAt(int index) {
7683 return mChildren.get(index);
7684 }
7685
7686 public int getChildIndex(View child) {
7687 return mChildren.indexOf(child);
7688 }
7689
7690 private void init(ViewGroup parent, boolean sort) {
7691 ArrayList<View> children = mChildren;
7692 final int childCount = parent.getChildCount();
7693 for (int i = 0; i < childCount; i++) {
7694 View child = parent.getChildAt(i);
7695 children.add(child);
7696 }
7697 if (sort) {
7698 ArrayList<ViewLocationHolder> holders = mHolders;
7699 for (int i = 0; i < childCount; i++) {
7700 View child = children.get(i);
7701 ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
7702 holders.add(holder);
7703 }
Svetoslav88e447b2014-10-09 15:49:02 -07007704 sort(holders);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007705 for (int i = 0; i < childCount; i++) {
7706 ViewLocationHolder holder = holders.get(i);
7707 children.set(i, holder.mView);
7708 holder.recycle();
7709 }
7710 holders.clear();
7711 }
7712 }
7713
Svetoslav88e447b2014-10-09 15:49:02 -07007714 private void sort(ArrayList<ViewLocationHolder> holders) {
7715 // This is gross but the least risky solution. The current comparison
7716 // strategy breaks transitivity but produces very good results. Coming
7717 // up with a new strategy requires time which we do not have, so ...
7718 try {
7719 ViewLocationHolder.setComparisonStrategy(
7720 ViewLocationHolder.COMPARISON_STRATEGY_STRIPE);
7721 Collections.sort(holders);
7722 } catch (IllegalArgumentException iae) {
7723 // Note that in practice this occurs extremely rarely in a couple
7724 // of pathological cases.
7725 ViewLocationHolder.setComparisonStrategy(
7726 ViewLocationHolder.COMPARISON_STRATEGY_LOCATION);
7727 Collections.sort(holders);
7728 }
7729 }
7730
Svetoslav Ganov42138042012-03-20 11:51:39 -07007731 private void clear() {
7732 mChildren.clear();
7733 }
7734 }
7735
7736 /**
7737 * Pooled class that holds a View and its location with respect to
7738 * a specified root. This enables sorting of views based on their
7739 * coordinates without recomputing the position relative to the root
7740 * on every comparison.
7741 */
7742 static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
7743
7744 private static final int MAX_POOL_SIZE = 32;
7745
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007746 private static final SynchronizedPool<ViewLocationHolder> sPool =
7747 new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007748
Svetoslav88e447b2014-10-09 15:49:02 -07007749 public static final int COMPARISON_STRATEGY_STRIPE = 1;
7750
7751 public static final int COMPARISON_STRATEGY_LOCATION = 2;
7752
7753 private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE;
7754
Svetoslav Ganov42138042012-03-20 11:51:39 -07007755 private final Rect mLocation = new Rect();
7756
7757 public View mView;
7758
7759 private int mLayoutDirection;
7760
7761 public static ViewLocationHolder obtain(ViewGroup root, View view) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007762 ViewLocationHolder holder = sPool.acquire();
7763 if (holder == null) {
7764 holder = new ViewLocationHolder();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007765 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007766 holder.init(root, view);
7767 return holder;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007768 }
7769
Svetoslav88e447b2014-10-09 15:49:02 -07007770 public static void setComparisonStrategy(int strategy) {
7771 sComparisonStrategy = strategy;
7772 }
7773
Svetoslav Ganov42138042012-03-20 11:51:39 -07007774 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007775 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007776 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007777 }
7778
7779 @Override
7780 public int compareTo(ViewLocationHolder another) {
7781 // This instance is greater than an invalid argument.
7782 if (another == null) {
7783 return 1;
7784 }
Svetoslav88e447b2014-10-09 15:49:02 -07007785
7786 if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) {
7787 // First is above second.
7788 if (mLocation.bottom - another.mLocation.top <= 0) {
7789 return -1;
7790 }
7791 // First is below second.
7792 if (mLocation.top - another.mLocation.bottom >= 0) {
7793 return 1;
7794 }
7795 }
7796
Svetoslav04cab1b2014-08-25 18:35:57 -07007797 // We are ordering left-to-right, top-to-bottom.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007798 if (mLayoutDirection == LAYOUT_DIRECTION_LTR) {
7799 final int leftDifference = mLocation.left - another.mLocation.left;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007800 if (leftDifference != 0) {
7801 return leftDifference;
7802 }
7803 } else { // RTL
7804 final int rightDifference = mLocation.right - another.mLocation.right;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007805 if (rightDifference != 0) {
7806 return -rightDifference;
7807 }
7808 }
Svetoslav04cab1b2014-08-25 18:35:57 -07007809 // We are ordering left-to-right, top-to-bottom.
7810 final int topDifference = mLocation.top - another.mLocation.top;
7811 if (topDifference != 0) {
7812 return topDifference;
7813 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007814 // Break tie by height.
7815 final int heightDiference = mLocation.height() - another.mLocation.height();
7816 if (heightDiference != 0) {
7817 return -heightDiference;
7818 }
7819 // Break tie by width.
7820 final int widthDiference = mLocation.width() - another.mLocation.width();
7821 if (widthDiference != 0) {
7822 return -widthDiference;
7823 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007824 // Just break the tie somehow. The accessibliity ids are unique
7825 // and stable, hence this is deterministic tie breaking.
7826 return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007827 }
7828
7829 private void init(ViewGroup root, View view) {
7830 Rect viewLocation = mLocation;
7831 view.getDrawingRect(viewLocation);
7832 root.offsetDescendantRectToMyCoords(view, viewLocation);
7833 mView = view;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07007834 mLayoutDirection = root.getLayoutDirection();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007835 }
7836
7837 private void clear() {
7838 mView = null;
7839 mLocation.set(0, 0, 0, 0);
7840 }
7841 }
Romain Guycbc67742012-04-27 16:12:57 -07007842
7843 private static Paint getDebugPaint() {
7844 if (sDebugPaint == null) {
7845 sDebugPaint = new Paint();
7846 sDebugPaint.setAntiAlias(false);
7847 }
7848 return sDebugPaint;
7849 }
7850
Romain Guy6410c0a2013-06-17 11:21:58 -07007851 private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
Romain Guycbc67742012-04-27 16:12:57 -07007852 if (sDebugLines== null) {
Romain Guy6410c0a2013-06-17 11:21:58 -07007853 // TODO: This won't work with multiple UI threads in a single process
Romain Guycbc67742012-04-27 16:12:57 -07007854 sDebugLines = new float[16];
7855 }
7856
Romain Guycbc67742012-04-27 16:12:57 -07007857 sDebugLines[0] = x1;
7858 sDebugLines[1] = y1;
7859 sDebugLines[2] = x2;
7860 sDebugLines[3] = y1;
7861
7862 sDebugLines[4] = x2;
7863 sDebugLines[5] = y1;
7864 sDebugLines[6] = x2;
Philip Milne7b757812012-09-19 18:13:44 -07007865 sDebugLines[7] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007866
Philip Milne7b757812012-09-19 18:13:44 -07007867 sDebugLines[8] = x2;
Romain Guycbc67742012-04-27 16:12:57 -07007868 sDebugLines[9] = y2;
7869 sDebugLines[10] = x1;
7870 sDebugLines[11] = y2;
7871
Philip Milne7b757812012-09-19 18:13:44 -07007872 sDebugLines[12] = x1;
7873 sDebugLines[13] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007874 sDebugLines[14] = x1;
7875 sDebugLines[15] = y1;
7876
Philip Milne7b757812012-09-19 18:13:44 -07007877 canvas.drawLines(sDebugLines, paint);
Romain Guycbc67742012-04-27 16:12:57 -07007878 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007879
7880 /** @hide */
7881 @Override
7882 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7883 super.encodeProperties(encoder);
7884
7885 encoder.addProperty("focus:descendantFocusability", getDescendantFocusability());
7886 encoder.addProperty("drawing:clipChildren", getClipChildren());
7887 encoder.addProperty("drawing:clipToPadding", getClipToPadding());
7888 encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled());
7889 encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache());
7890
7891 int n = getChildCount();
7892 encoder.addProperty("meta:__childCount__", (short)n);
7893 for (int i = 0; i < n; i++) {
7894 encoder.addPropertyKey("meta:__child__" + i);
7895 getChildAt(i).encode(encoder);
7896 }
7897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898}