blob: 56ee478956872aca43de2fe6c8c0f89ba93e6ece [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
1294 public void dispatchConfigurationChanged(Configuration newConfig) {
1295 super.dispatchConfigurationChanged(newConfig);
1296 final int count = mChildrenCount;
1297 final View[] children = mChildren;
1298 for (int i = 0; i < count; i++) {
1299 children[i].dispatchConfigurationChanged(newConfig);
1300 }
1301 }
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08001302
Alan Viverettebe463f22016-01-21 10:50:10 -05001303 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -08001305 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1306 ViewParent parent = mParent;
1307 if (parent != null) parent.recomputeViewAttributes(this);
1308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
Romain Guy8506ab42009-06-11 17:35:47 -07001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001312 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1313 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1314 super.dispatchCollectViewAttributes(attachInfo, visibility);
1315 final int count = mChildrenCount;
1316 final View[] children = mChildren;
1317 for (int i = 0; i < count; i++) {
1318 final View child = children[i];
1319 child.dispatchCollectViewAttributes(attachInfo,
1320 visibility | (child.mViewFlags&VISIBILITY_MASK));
1321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323 }
1324
Alan Viverettebe463f22016-01-21 10:50:10 -05001325 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 public void bringChildToFront(View child) {
Alan Viverette77bb6f12015-02-11 17:24:33 -08001327 final int index = indexOfChild(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 if (index >= 0) {
1329 removeFromArray(index);
1330 addInArray(child, mChildrenCount);
1331 child.mParent = this;
Chet Haasecb96db82013-09-04 10:21:46 -07001332 requestLayout();
1333 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
1335 }
1336
Romain Guy6410c0a2013-06-17 11:21:58 -07001337 private PointF getLocalPoint() {
1338 if (mLocalPoint == null) mLocalPoint = new PointF();
1339 return mLocalPoint;
1340 }
1341
Steve Block8a7259b2012-03-01 11:24:41 +00001342 // TODO: Write real docs
Christopher Tatea53146c2010-09-07 11:57:52 -07001343 @Override
1344 public boolean dispatchDragEvent(DragEvent event) {
1345 boolean retval = false;
1346 final float tx = event.mX;
1347 final float ty = event.mY;
1348
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07001349 ViewRootImpl root = getViewRootImpl();
Christopher Tatea53146c2010-09-07 11:57:52 -07001350
1351 // Dispatch down the view hierarchy
Romain Guy6410c0a2013-06-17 11:21:58 -07001352 final PointF localPoint = getLocalPoint();
1353
Christopher Tatea53146c2010-09-07 11:57:52 -07001354 switch (event.mAction) {
1355 case DragEvent.ACTION_DRAG_STARTED: {
1356 // clear state to recalculate which views we drag over
Chris Tate9d1ab882010-11-02 15:55:39 -07001357 mCurrentDragView = null;
Christopher Tatea53146c2010-09-07 11:57:52 -07001358
Christopher Tate86cab1b2011-01-13 20:28:55 -08001359 // Set up our tracking of drag-started notifications
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001360 mCurrentDragStartEvent = DragEvent.obtain(event);
1361 if (mChildrenInterestedInDrag == null) {
1362 mChildrenInterestedInDrag = new HashSet<View>();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001363 } else {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001364 mChildrenInterestedInDrag.clear();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001365 }
1366
Christopher Tatea53146c2010-09-07 11:57:52 -07001367 // Now dispatch down to our children, caching the responses
Christopher Tatea53146c2010-09-07 11:57:52 -07001368 final int count = mChildrenCount;
1369 final View[] children = mChildren;
1370 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001371 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001372 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001373 if (child.getVisibility() == VISIBLE) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001374 if (notifyChildOfDragStart(children[i])) {
1375 retval = true;
Christopher Tate2c095f32010-10-04 14:13:40 -07001376 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001377 }
1378 }
1379
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001380 // Notify itself of the drag start.
1381 mIsInterestedInDrag = super.dispatchDragEvent(event);
1382 if (mIsInterestedInDrag) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001383 retval = true;
1384 }
Yorke Lee0394b212016-03-10 18:20:04 -08001385
1386 if (!retval) {
1387 // Neither us nor any of our children are interested in this drag, so stop tracking
1388 // the current drag event.
1389 mCurrentDragStartEvent.recycle();
1390 mCurrentDragStartEvent = null;
1391 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001392 } break;
1393
1394 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001395 // Release the bookkeeping now that the drag lifecycle has ended
Vadim Tryshev15177892015-12-18 08:01:00 -08001396 final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag;
1397 if (childrenInterestedInDrag != null) {
1398 for (View child : childrenInterestedInDrag) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001399 // If a child was interested in the ongoing drag, it's told that it's over
1400 if (child.dispatchDragEvent(event)) {
1401 retval = true;
1402 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001403 child.mPrivateFlags2 &= ~View.DRAG_MASK;
1404 child.refreshDrawableState();
Christopher Tate1fc014f2011-01-19 12:56:26 -08001405 }
Vadim Tryshev15177892015-12-18 08:01:00 -08001406 childrenInterestedInDrag.clear();
1407 }
1408 if (mCurrentDragStartEvent != null) {
1409 mCurrentDragStartEvent.recycle();
1410 mCurrentDragStartEvent = null;
Christopher Tate1fc014f2011-01-19 12:56:26 -08001411 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001412
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001413 if (mIsInterestedInDrag) {
1414 if (super.dispatchDragEvent(event)) {
1415 retval = true;
1416 }
1417 mIsInterestedInDrag = false;
Christopher Tatea53146c2010-09-07 11:57:52 -07001418 }
1419 } break;
1420
1421 case DragEvent.ACTION_DRAG_LOCATION: {
1422 // Find the [possibly new] drag target
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001423 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
1424 if (target == null && mIsInterestedInDrag) {
1425 target = this;
1426 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001427
1428 // If we've changed apparent drag target, tell the view root which view
Chris Tate9d1ab882010-11-02 15:55:39 -07001429 // we're over now [for purposes of the eventual drag-recipient-changed
1430 // notifications to the framework] and tell the new target that the drag
1431 // has entered its bounds. The root will see setDragFocus() calls all
1432 // the way down to the final leaf view that is handling the LOCATION event
1433 // before reporting the new potential recipient to the framework.
Christopher Tatea53146c2010-09-07 11:57:52 -07001434 if (mCurrentDragView != target) {
Chris Tate9d1ab882010-11-02 15:55:39 -07001435 root.setDragFocus(target);
1436
1437 final int action = event.mAction;
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001438 // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED.
1439 event.mX = 0;
1440 event.mY = 0;
1441
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001442 // If we've dragged off of a child view or this window, send it the EXITED message
Chris Tate9d1ab882010-11-02 15:55:39 -07001443 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001444 final View view = mCurrentDragView;
Chris Tate9d1ab882010-11-02 15:55:39 -07001445 event.mAction = DragEvent.ACTION_DRAG_EXITED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001446 if (view != this) {
1447 view.dispatchDragEvent(event);
1448 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1449 view.refreshDrawableState();
1450 } else {
1451 super.dispatchDragEvent(event);
1452 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001453 }
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001454
Christopher Tatea53146c2010-09-07 11:57:52 -07001455 mCurrentDragView = target;
Chris Tate9d1ab882010-11-02 15:55:39 -07001456
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001457 // If we've dragged over a new child view, send it the ENTERED message, otherwise
1458 // send it to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001459 if (target != null) {
1460 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001461 if (target != this) {
1462 target.dispatchDragEvent(event);
1463 target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
1464 target.refreshDrawableState();
1465 } else {
1466 super.dispatchDragEvent(event);
1467 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001468 }
1469 event.mAction = action; // restore the event's original state
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001470 event.mX = tx;
1471 event.mY = ty;
Christopher Tatea53146c2010-09-07 11:57:52 -07001472 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001473
Christopher Tatea53146c2010-09-07 11:57:52 -07001474 // Dispatch the actual drag location notice, localized into its coordinates
1475 if (target != null) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001476 if (target != this) {
1477 event.mX = localPoint.x;
1478 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001479
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001480 retval = target.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001481
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001482 event.mX = tx;
1483 event.mY = ty;
1484 } else {
1485 retval = super.dispatchDragEvent(event);
1486 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001487 }
1488 } break;
1489
Chris Tate9d1ab882010-11-02 15:55:39 -07001490 /* Entered / exited dispatch
1491 *
1492 * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is
1493 * that we're about to get the corresponding LOCATION event, which we will use to
1494 * determine which of our children is the new target; at that point we will
1495 * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001496 * If no suitable child is detected, dispatch to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001497 *
1498 * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1499 * drag has left this ViewGroup, we know by definition that every contained subview
1500 * is also no longer under the drag point.
1501 */
1502
1503 case DragEvent.ACTION_DRAG_EXITED: {
1504 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001505 final View view = mCurrentDragView;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001506 if (view != this) {
1507 view.dispatchDragEvent(event);
1508 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1509 view.refreshDrawableState();
1510 } else {
1511 super.dispatchDragEvent(event);
1512 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001513
Chris Tate9d1ab882010-11-02 15:55:39 -07001514 mCurrentDragView = null;
1515 }
1516 } break;
1517
Christopher Tatea53146c2010-09-07 11:57:52 -07001518 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -07001519 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Romain Guy6410c0a2013-06-17 11:21:58 -07001520 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
Christopher Tatea53146c2010-09-07 11:57:52 -07001521 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001522 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Romain Guy6410c0a2013-06-17 11:21:58 -07001523 event.mX = localPoint.x;
1524 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001525 retval = target.dispatchDragEvent(event);
1526 event.mX = tx;
1527 event.mY = ty;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001528 } else if (mIsInterestedInDrag) {
1529 retval = super.dispatchDragEvent(event);
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001530 } else {
1531 if (ViewDebug.DEBUG_DRAG) {
1532 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
1533 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001534 }
1535 } break;
1536 }
1537
Christopher Tatea53146c2010-09-07 11:57:52 -07001538 return retval;
1539 }
1540
1541 // Find the frontmost child view that lies under the given point, and calculate
1542 // the position within its own local coordinate system.
1543 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001544 final int count = mChildrenCount;
1545 final View[] children = mChildren;
1546 for (int i = count - 1; i >= 0; i--) {
1547 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001548 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001549 continue;
1550 }
1551
Christopher Tate2c095f32010-10-04 14:13:40 -07001552 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001553 return child;
1554 }
1555 }
1556 return null;
1557 }
1558
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001559 boolean notifyChildOfDragStart(View child) {
1560 // The caller guarantees that the child is not in mChildrenInterestedInDrag yet.
1561
Christopher Tate86cab1b2011-01-13 20:28:55 -08001562 if (ViewDebug.DEBUG_DRAG) {
1563 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1564 }
1565
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001566 final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent);
1567 if (canAccept) {
1568 mChildrenInterestedInDrag.add(child);
1569 if (!child.canAcceptDrag()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001570 child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001571 child.refreshDrawableState();
1572 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001573 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001574 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001575 }
1576
Joe Onorato664644d2011-01-23 17:53:23 -08001577 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001578 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1579 super.dispatchWindowSystemUiVisiblityChanged(visible);
1580
1581 final int count = mChildrenCount;
1582 final View[] children = mChildren;
1583 for (int i=0; i <count; i++) {
1584 final View child = children[i];
1585 child.dispatchWindowSystemUiVisiblityChanged(visible);
1586 }
1587 }
1588
1589 @Override
Joe Onorato664644d2011-01-23 17:53:23 -08001590 public void dispatchSystemUiVisibilityChanged(int visible) {
1591 super.dispatchSystemUiVisibilityChanged(visible);
1592
1593 final int count = mChildrenCount;
1594 final View[] children = mChildren;
1595 for (int i=0; i <count; i++) {
1596 final View child = children[i];
1597 child.dispatchSystemUiVisibilityChanged(visible);
1598 }
1599 }
1600
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001601 @Override
Dianne Hackborncf675782012-05-10 15:07:24 -07001602 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
1603 boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001604
1605 final int count = mChildrenCount;
1606 final View[] children = mChildren;
1607 for (int i=0; i <count; i++) {
1608 final View child = children[i];
Dianne Hackborncf675782012-05-10 15:07:24 -07001609 changed |= child.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001610 }
Dianne Hackborncf675782012-05-10 15:07:24 -07001611 return changed;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001612 }
1613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 @Override
1615 public boolean dispatchKeyEventPreIme(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001616 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1617 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 return super.dispatchKeyEventPreIme(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001619 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1620 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 return mFocused.dispatchKeyEventPreIme(event);
1622 }
1623 return false;
1624 }
1625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 @Override
1627 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001628 if (mInputEventConsistencyVerifier != null) {
1629 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1630 }
1631
Dianne Hackborn4702a852012-08-17 15:18:29 -07001632 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1633 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001634 if (super.dispatchKeyEvent(event)) {
1635 return true;
1636 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001637 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1638 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001639 if (mFocused.dispatchKeyEvent(event)) {
1640 return true;
1641 }
1642 }
1643
1644 if (mInputEventConsistencyVerifier != null) {
1645 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 return false;
1648 }
1649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 @Override
1651 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001652 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1653 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 return super.dispatchKeyShortcutEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001655 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1656 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 return mFocused.dispatchKeyShortcutEvent(event);
1658 }
1659 return false;
1660 }
1661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 @Override
1663 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001664 if (mInputEventConsistencyVerifier != null) {
1665 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1666 }
1667
Dianne Hackborn4702a852012-08-17 15:18:29 -07001668 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1669 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001670 if (super.dispatchTrackballEvent(event)) {
1671 return true;
1672 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001673 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1674 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001675 if (mFocused.dispatchTrackballEvent(event)) {
1676 return true;
1677 }
1678 }
1679
1680 if (mInputEventConsistencyVerifier != null) {
1681 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 return false;
1684 }
1685
Jun Mukai1db53972015-09-11 18:08:31 -07001686 @Override
Jun Mukaid4eaef72015-10-30 15:54:33 -07001687 public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09001688 if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) {
1689 return PointerIcon.getSystemIcon(mContext, PointerIcon.STYLE_ARROW);
1690 }
Jun Mukai1db53972015-09-11 18:08:31 -07001691 // Check what the child under the pointer says about the pointer.
1692 final int childrenCount = mChildrenCount;
1693 if (childrenCount != 0) {
1694 final ArrayList<View> preorderedList = buildOrderedChildList();
1695 final boolean customOrder = preorderedList == null
1696 && isChildrenDrawingOrderEnabled();
1697 final View[] children = mChildren;
1698 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05001699 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
1700 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
1701 final PointF point = getLocalPoint();
Jun Mukai1db53972015-09-11 18:08:31 -07001702 if (isTransformedTouchPointInView(x, y, child, point)) {
Jun Mukaid4eaef72015-10-30 15:54:33 -07001703 final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y);
1704 if (pointerIcon != null) {
1705 return pointerIcon;
Jun Mukai1db53972015-09-11 18:08:31 -07001706 }
1707 break;
1708 }
1709 }
1710 }
1711
1712 // The pointer is not a child or the child has no preferences, returning the default
1713 // implementation.
Jun Mukaid4eaef72015-10-30 15:54:33 -07001714 return super.getPointerIcon(event, x, y);
Jun Mukai1db53972015-09-11 18:08:31 -07001715 }
1716
Alan Viverettea7b85e62016-01-22 10:14:02 -05001717 private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {
1718 final int childIndex;
1719 if (customOrder) {
1720 final int childIndex1 = getChildDrawingOrder(childrenCount, i);
1721 if (childIndex1 >= childrenCount) {
1722 throw new IndexOutOfBoundsException("getChildDrawingOrder() "
1723 + "returned invalid index " + childIndex1
1724 + " (child count is " + childrenCount + ")");
1725 }
1726 childIndex = childIndex1;
1727 } else {
1728 childIndex = i;
1729 }
1730 return childIndex;
1731 }
1732
Romain Guya9489272011-06-22 20:58:11 -07001733 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08001735 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08001736 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08001737
Jeff Brown10b62902011-06-20 16:40:37 -07001738 // First check whether the view group wants to intercept the hover event.
1739 final boolean interceptHover = onInterceptHoverEvent(event);
1740 event.setAction(action); // restore action in case it was changed
1741
Jeff Brown87b7f802011-06-21 18:35:45 -07001742 MotionEvent eventNoHistory = event;
1743 boolean handled = false;
1744
1745 // Send events to the hovered children and build a new list of hover targets until
1746 // one is found that handles the event.
1747 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1748 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07001749 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08001750 final float x = event.getX();
1751 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001752 final int childrenCount = mChildrenCount;
1753 if (childrenCount != 0) {
Chris Craikab008f02014-05-23 17:55:03 -07001754 final ArrayList<View> preorderedList = buildOrderedChildList();
1755 final boolean customOrder = preorderedList == null
1756 && isChildrenDrawingOrderEnabled();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001757 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07001758 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001759 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05001760 final int childIndex = getAndVerifyPreorderedIndex(
1761 childrenCount, i, customOrder);
1762 final View child = getAndVerifyPreorderedView(
1763 preorderedList, children, childIndex);
Jeff Brown87b7f802011-06-21 18:35:45 -07001764 if (!canViewReceivePointerEvents(child)
1765 || !isTransformedTouchPointInView(x, y, child, null)) {
1766 continue;
1767 }
1768
1769 // Obtain a hover target for this child. Dequeue it from the
1770 // old hover target list if the child was previously hovered.
1771 HoverTarget hoverTarget = firstOldHoverTarget;
1772 final boolean wasHovered;
1773 for (HoverTarget predecessor = null; ;) {
1774 if (hoverTarget == null) {
1775 hoverTarget = HoverTarget.obtain(child);
1776 wasHovered = false;
1777 break;
1778 }
1779
1780 if (hoverTarget.child == child) {
1781 if (predecessor != null) {
1782 predecessor.next = hoverTarget.next;
1783 } else {
1784 firstOldHoverTarget = hoverTarget.next;
1785 }
1786 hoverTarget.next = null;
1787 wasHovered = true;
1788 break;
1789 }
1790
1791 predecessor = hoverTarget;
1792 hoverTarget = hoverTarget.next;
1793 }
1794
1795 // Enqueue the hover target onto the new hover target list.
1796 if (lastHoverTarget != null) {
1797 lastHoverTarget.next = hoverTarget;
1798 } else {
Jeff Brown87b7f802011-06-21 18:35:45 -07001799 mFirstHoverTarget = hoverTarget;
1800 }
Sangkyu Lee8725f362013-03-13 09:38:45 +09001801 lastHoverTarget = hoverTarget;
Jeff Brown87b7f802011-06-21 18:35:45 -07001802
1803 // Dispatch the event to the child.
1804 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1805 if (!wasHovered) {
1806 // Send the enter as is.
1807 handled |= dispatchTransformedGenericPointerEvent(
1808 event, child); // enter
1809 }
1810 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1811 if (!wasHovered) {
1812 // Synthesize an enter from a move.
1813 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1814 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1815 handled |= dispatchTransformedGenericPointerEvent(
1816 eventNoHistory, child); // enter
1817 eventNoHistory.setAction(action);
1818
1819 handled |= dispatchTransformedGenericPointerEvent(
1820 eventNoHistory, child); // move
1821 } else {
1822 // Send the move as is.
1823 handled |= dispatchTransformedGenericPointerEvent(event, child);
1824 }
1825 }
1826 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07001827 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001828 }
Jeff Brown10b62902011-06-20 16:40:37 -07001829 }
Chris Craikab008f02014-05-23 17:55:03 -07001830 if (preorderedList != null) preorderedList.clear();
Jeff Brown10b62902011-06-20 16:40:37 -07001831 }
1832 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001833
Jeff Brown87b7f802011-06-21 18:35:45 -07001834 // Send exit events to all previously hovered children that are no longer hovered.
1835 while (firstOldHoverTarget != null) {
1836 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07001837
Jeff Brown87b7f802011-06-21 18:35:45 -07001838 // Exit the old hovered child.
1839 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1840 // Send the exit as is.
1841 handled |= dispatchTransformedGenericPointerEvent(
1842 event, child); // exit
1843 } else {
1844 // Synthesize an exit from a move or enter.
1845 // Ignore the result because hover focus has moved to a different view.
1846 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brown10b62902011-06-20 16:40:37 -07001847 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07001848 event, child); // move
Jeff Brown10b62902011-06-20 16:40:37 -07001849 }
Jeff Brown87b7f802011-06-21 18:35:45 -07001850 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1851 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1852 dispatchTransformedGenericPointerEvent(
1853 eventNoHistory, child); // exit
1854 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07001855 }
1856
Jeff Brown87b7f802011-06-21 18:35:45 -07001857 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1858 firstOldHoverTarget.recycle();
1859 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07001860 }
1861
Jeff Brown87b7f802011-06-21 18:35:45 -07001862 // Send events to the view group itself if no children have handled it.
Jeff Brown10b62902011-06-20 16:40:37 -07001863 boolean newHoveredSelf = !handled;
1864 if (newHoveredSelf == mHoveredSelf) {
1865 if (newHoveredSelf) {
1866 // Send event to the view group as before.
1867 handled |= super.dispatchHoverEvent(event);
1868 }
1869 } else {
1870 if (mHoveredSelf) {
1871 // Exit the view group.
1872 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1873 // Send the exit as is.
1874 handled |= super.dispatchHoverEvent(event); // exit
1875 } else {
1876 // Synthesize an exit from a move or enter.
1877 // Ignore the result because hover focus is moving to a different view.
1878 if (action == MotionEvent.ACTION_HOVER_MOVE) {
1879 super.dispatchHoverEvent(event); // move
1880 }
1881 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1882 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1883 super.dispatchHoverEvent(eventNoHistory); // exit
1884 eventNoHistory.setAction(action);
1885 }
1886 mHoveredSelf = false;
1887 }
1888
1889 if (newHoveredSelf) {
1890 // Enter the view group.
1891 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1892 // Send the enter as is.
1893 handled |= super.dispatchHoverEvent(event); // enter
1894 mHoveredSelf = true;
1895 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1896 // Synthesize an enter from a move.
1897 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1898 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1899 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1900 eventNoHistory.setAction(action);
1901
1902 handled |= super.dispatchHoverEvent(eventNoHistory); // move
1903 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001904 }
1905 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001906 }
1907
Jeff Browna032cc02011-03-07 16:56:21 -08001908 // Recycle the copy of the event that we made.
1909 if (eventNoHistory != event) {
1910 eventNoHistory.recycle();
1911 }
1912
Jeff Browna032cc02011-03-07 16:56:21 -08001913 // Done.
1914 return handled;
1915 }
1916
Jeff Brown59a422e2012-04-19 15:19:19 -07001917 private void exitHoverTargets() {
1918 if (mHoveredSelf || mFirstHoverTarget != null) {
1919 final long now = SystemClock.uptimeMillis();
1920 MotionEvent event = MotionEvent.obtain(now, now,
1921 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1922 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1923 dispatchHoverEvent(event);
1924 event.recycle();
1925 }
1926 }
1927
1928 private void cancelHoverTarget(View view) {
1929 HoverTarget predecessor = null;
1930 HoverTarget target = mFirstHoverTarget;
1931 while (target != null) {
1932 final HoverTarget next = target.next;
1933 if (target.child == view) {
1934 if (predecessor == null) {
1935 mFirstHoverTarget = next;
1936 } else {
1937 predecessor.next = next;
1938 }
1939 target.recycle();
1940
1941 final long now = SystemClock.uptimeMillis();
1942 MotionEvent event = MotionEvent.obtain(now, now,
1943 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1944 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1945 view.dispatchHoverEvent(event);
1946 event.recycle();
1947 return;
1948 }
1949 predecessor = target;
1950 target = next;
1951 }
1952 }
1953
Jeff Brown87b7f802011-06-21 18:35:45 -07001954 /** @hide */
1955 @Override
1956 protected boolean hasHoveredChild() {
1957 return mFirstHoverTarget != null;
1958 }
1959
Svetoslav Ganov42138042012-03-20 11:51:39 -07001960 @Override
Adam Powellcf392d12015-06-25 14:48:45 -07001961 public void addChildrenForAccessibility(ArrayList<View> outChildren) {
Svetoslav762621c2015-06-01 17:21:59 -07001962 if (getAccessibilityNodeProvider() != null) {
1963 return;
1964 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001965 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
1966 try {
1967 final int childrenCount = children.getChildCount();
1968 for (int i = 0; i < childrenCount; i++) {
1969 View child = children.getChildAt(i);
Svetoslav Ganovc406be92012-05-11 16:12:32 -07001970 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001971 if (child.includeForAccessibility()) {
Adam Powellcf392d12015-06-25 14:48:45 -07001972 outChildren.add(child);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001973 } else {
Adam Powellcf392d12015-06-25 14:48:45 -07001974 child.addChildrenForAccessibility(outChildren);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001975 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001976 }
1977 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001978 } finally {
1979 children.recycle();
Svetoslav Ganov42138042012-03-20 11:51:39 -07001980 }
1981 }
1982
1983 /**
Jeff Brown10b62902011-06-20 16:40:37 -07001984 * Implement this method to intercept hover events before they are handled
1985 * by child views.
1986 * <p>
1987 * This method is called before dispatching a hover event to a child of
1988 * the view group or to the view group's own {@link #onHoverEvent} to allow
1989 * the view group a chance to intercept the hover event.
1990 * This method can also be used to watch all pointer motions that occur within
1991 * the bounds of the view group even when the pointer is hovering over
1992 * a child of the view group rather than over the view group itself.
1993 * </p><p>
1994 * The view group can prevent its children from receiving hover events by
1995 * implementing this method and returning <code>true</code> to indicate
1996 * that it would like to intercept hover events. The view group must
1997 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
1998 * for as long as it wishes to continue intercepting hover events from
1999 * its children.
2000 * </p><p>
2001 * Interception preserves the invariant that at most one view can be
2002 * hovered at a time by transferring hover focus from the currently hovered
2003 * child to the view group or vice-versa as needed.
2004 * </p><p>
2005 * If this method returns <code>true</code> and a child is already hovered, then the
2006 * child view will first receive a hover exit event and then the view group
2007 * itself will receive a hover enter event in {@link #onHoverEvent}.
2008 * Likewise, if this method had previously returned <code>true</code> to intercept hover
2009 * events and instead returns <code>false</code> while the pointer is hovering
2010 * within the bounds of one of a child, then the view group will first receive a
2011 * hover exit event in {@link #onHoverEvent} and then the hovered child will
2012 * receive a hover enter event.
2013 * </p><p>
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002014 * The default implementation handles mouse hover on the scroll bars.
Jeff Brown10b62902011-06-20 16:40:37 -07002015 * </p>
2016 *
2017 * @param event The motion event that describes the hover.
2018 * @return True if the view group would like to intercept the hover event
2019 * and prevent its children from receiving it.
2020 */
2021 public boolean onInterceptHoverEvent(MotionEvent event) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002022 if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
2023 final int action = event.getAction();
2024 final float x = event.getX();
2025 final float y = event.getY();
2026 if ((action == MotionEvent.ACTION_HOVER_MOVE
2027 || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) {
2028 return true;
2029 }
2030 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002031 return false;
2032 }
2033
Jeff Browna032cc02011-03-07 16:56:21 -08002034 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
2035 if (event.getHistorySize() == 0) {
2036 return event;
2037 }
2038 return MotionEvent.obtainNoHistory(event);
2039 }
2040
Jeff Browna032cc02011-03-07 16:56:21 -08002041 @Override
2042 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
2043 // Send the event to the child under the pointer.
2044 final int childrenCount = mChildrenCount;
2045 if (childrenCount != 0) {
Jeff Browna032cc02011-03-07 16:56:21 -08002046 final float x = event.getX();
2047 final float y = event.getY();
2048
Chris Craikab008f02014-05-23 17:55:03 -07002049 final ArrayList<View> preorderedList = buildOrderedChildList();
2050 final boolean customOrder = preorderedList == null
2051 && isChildrenDrawingOrderEnabled();
2052 final View[] children = mChildren;
Jeff Browna032cc02011-03-07 16:56:21 -08002053 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002054 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
2055 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Jeff Browna032cc02011-03-07 16:56:21 -08002056 if (!canViewReceivePointerEvents(child)
2057 || !isTransformedTouchPointInView(x, y, child, null)) {
2058 continue;
2059 }
2060
2061 if (dispatchTransformedGenericPointerEvent(event, child)) {
Chris Craikab008f02014-05-23 17:55:03 -07002062 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002063 return true;
2064 }
2065 }
Chris Craikab008f02014-05-23 17:55:03 -07002066 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002067 }
2068
2069 // No child handled the event. Send it to this view group.
2070 return super.dispatchGenericPointerEvent(event);
2071 }
2072
Jeff Browna032cc02011-03-07 16:56:21 -08002073 @Override
2074 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08002075 // Send the event to the focused child or to this view group if it has focus.
Dianne Hackborn4702a852012-08-17 15:18:29 -07002076 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
2077 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08002078 return super.dispatchGenericFocusedEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07002079 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
2080 == PFLAG_HAS_BOUNDS) {
Jeff Browncb1404e2011-01-15 18:14:15 -08002081 return mFocused.dispatchGenericMotionEvent(event);
2082 }
2083 return false;
2084 }
2085
2086 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002087 * Dispatches a generic pointer event to a child, taking into account
2088 * transformations that apply to the child.
2089 *
2090 * @param event The event to send.
2091 * @param child The view to send the event to.
2092 * @return {@code true} if the child handled the event.
2093 */
2094 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
2095 final float offsetX = mScrollX - child.mLeft;
2096 final float offsetY = mScrollY - child.mTop;
2097
2098 boolean handled;
2099 if (!child.hasIdentityMatrix()) {
2100 MotionEvent transformedEvent = MotionEvent.obtain(event);
2101 transformedEvent.offsetLocation(offsetX, offsetY);
2102 transformedEvent.transform(child.getInverseMatrix());
2103 handled = child.dispatchGenericMotionEvent(transformedEvent);
2104 transformedEvent.recycle();
2105 } else {
2106 event.offsetLocation(offsetX, offsetY);
2107 handled = child.dispatchGenericMotionEvent(event);
2108 event.offsetLocation(-offsetX, -offsetY);
2109 }
2110 return handled;
2111 }
2112
Jeff Browncb1404e2011-01-15 18:14:15 -08002113 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08002115 if (mInputEventConsistencyVerifier != null) {
2116 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
2117 }
2118
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002119 // If the event targets the accessibility focused view and this is it, start
2120 // normal event dispatch. Maybe a descendant is what will handle the click.
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002121 if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002122 ev.setTargetAccessibilityFocus(false);
2123 }
2124
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002125 boolean handled = false;
2126 if (onFilterTouchEventForSecurity(ev)) {
2127 final int action = ev.getAction();
2128 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07002129
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002130 // Handle an initial down.
2131 if (actionMasked == MotionEvent.ACTION_DOWN) {
2132 // Throw away all previous state when starting a new touch gesture.
2133 // The framework may have dropped the up or cancel event for the previous gesture
2134 // due to an app switch, ANR, or some other state change.
2135 cancelAndClearTouchTargets(ev);
2136 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
Adam Powellb08013c2010-09-16 16:28:11 -07002138
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002139 // Check for interception.
2140 final boolean intercepted;
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002141 if (actionMasked == MotionEvent.ACTION_DOWN
2142 || mFirstTouchTarget != null) {
2143 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
2144 if (!disallowIntercept) {
2145 intercepted = onInterceptTouchEvent(ev);
2146 ev.setAction(action); // restore action in case it was changed
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002147 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002148 intercepted = false;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002149 }
2150 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002151 // There are no touch targets and this action is not an initial down
2152 // so this view group continues to intercept touches.
2153 intercepted = true;
2154 }
2155
2156 // If intercepted, start normal event dispatch. Also if there is already
2157 // a view that is handling the gesture, do normal event dispatch.
2158 if (intercepted || mFirstTouchTarget != null) {
2159 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002160 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002161
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002162 // Check for cancelation.
2163 final boolean canceled = resetCancelNextUpFlag(this)
2164 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07002165
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002166 // Update list of touch targets for pointer down, if needed.
2167 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
2168 TouchTarget newTouchTarget = null;
2169 boolean alreadyDispatchedToNewTouchTarget = false;
2170 if (!canceled && !intercepted) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002171
2172 // If the event is targeting accessiiblity focus we give it to the
2173 // view that has accessibility focus and if it does not handle it
2174 // we clear the flag and dispatch the event to all children as usual.
2175 // We are looking up the accessibility focused host to avoid keeping
2176 // state since these events are very rare.
2177 View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
2178 ? findChildWithAccessibilityFocus() : null;
2179
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002180 if (actionMasked == MotionEvent.ACTION_DOWN
2181 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002182 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002183 final int actionIndex = ev.getActionIndex(); // always 0 for down
2184 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
2185 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07002186
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002187 // Clean up earlier touch targets for this pointer id in case they
2188 // have become out of sync.
2189 removePointersFromTouchTargets(idBitsToAssign);
2190
2191 final int childrenCount = mChildrenCount;
Chet Haase9c17fe62013-03-22 17:05:55 -07002192 if (newTouchTarget == null && childrenCount != 0) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002193 final float x = ev.getX(actionIndex);
2194 final float y = ev.getY(actionIndex);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002195 // Find a child that can receive the event.
2196 // Scan children from front to back.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002197 final ArrayList<View> preorderedList = buildTouchDispatchChildList();
Chris Craikab008f02014-05-23 17:55:03 -07002198 final boolean customOrder = preorderedList == null
2199 && isChildrenDrawingOrderEnabled();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002200 final View[] children = mChildren;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002201 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002202 final int childIndex = getAndVerifyPreorderedIndex(
2203 childrenCount, i, customOrder);
2204 final View child = getAndVerifyPreorderedView(
2205 preorderedList, children, childIndex);
Svetoslavc73cfa02015-02-09 17:14:28 -08002206
2207 // If there is a view that has accessibility focus we want it
2208 // to get the event first and if not handled we will perform a
2209 // normal dispatch. We may do a double iteration but this is
2210 // safer given the timeframe.
2211 if (childWithAccessibilityFocus != null) {
2212 if (childWithAccessibilityFocus != child) {
2213 continue;
2214 }
2215 childWithAccessibilityFocus = null;
2216 i = childrenCount - 1;
2217 }
2218
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002219 if (!canViewReceivePointerEvents(child)
2220 || !isTransformedTouchPointInView(x, y, child, null)) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002221 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002222 continue;
2223 }
2224
2225 newTouchTarget = getTouchTarget(child);
2226 if (newTouchTarget != null) {
2227 // Child is already receiving touch within its bounds.
2228 // Give it the new pointer in addition to the ones it is handling.
2229 newTouchTarget.pointerIdBits |= idBitsToAssign;
2230 break;
2231 }
2232
2233 resetCancelNextUpFlag(child);
2234 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
2235 // Child wants to receive touch within its bounds.
2236 mLastTouchDownTime = ev.getDownTime();
Chris Craikab008f02014-05-23 17:55:03 -07002237 if (preorderedList != null) {
2238 // childIndex points into presorted list, find original index
2239 for (int j = 0; j < childrenCount; j++) {
2240 if (children[childIndex] == mChildren[j]) {
2241 mLastTouchDownIndex = j;
2242 break;
2243 }
2244 }
2245 } else {
2246 mLastTouchDownIndex = childIndex;
2247 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002248 mLastTouchDownX = ev.getX();
2249 mLastTouchDownY = ev.getY();
2250 newTouchTarget = addTouchTarget(child, idBitsToAssign);
2251 alreadyDispatchedToNewTouchTarget = true;
2252 break;
2253 }
Svetoslavc73cfa02015-02-09 17:14:28 -08002254
2255 // The accessibility focus didn't handle the event, so clear
2256 // the flag and do a normal dispatch to all children.
2257 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002258 }
Chris Craikab008f02014-05-23 17:55:03 -07002259 if (preorderedList != null) preorderedList.clear();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002260 }
2261
2262 if (newTouchTarget == null && mFirstTouchTarget != null) {
2263 // Did not find a child to receive the event.
2264 // Assign the pointer to the least recently added target.
2265 newTouchTarget = mFirstTouchTarget;
2266 while (newTouchTarget.next != null) {
2267 newTouchTarget = newTouchTarget.next;
2268 }
2269 newTouchTarget.pointerIdBits |= idBitsToAssign;
2270 }
2271 }
2272 }
2273
2274 // Dispatch to touch targets.
2275 if (mFirstTouchTarget == null) {
2276 // No touch targets so treat this as an ordinary view.
2277 handled = dispatchTransformedTouchEvent(ev, canceled, null,
2278 TouchTarget.ALL_POINTER_IDS);
2279 } else {
2280 // Dispatch to touch targets, excluding the new touch target if we already
2281 // dispatched to it. Cancel touch targets if necessary.
2282 TouchTarget predecessor = null;
2283 TouchTarget target = mFirstTouchTarget;
2284 while (target != null) {
2285 final TouchTarget next = target.next;
2286 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
2287 handled = true;
2288 } else {
2289 final boolean cancelChild = resetCancelNextUpFlag(target.child)
Chet Haase9c17fe62013-03-22 17:05:55 -07002290 || intercepted;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002291 if (dispatchTransformedTouchEvent(ev, cancelChild,
2292 target.child, target.pointerIdBits)) {
2293 handled = true;
2294 }
2295 if (cancelChild) {
2296 if (predecessor == null) {
2297 mFirstTouchTarget = next;
2298 } else {
2299 predecessor.next = next;
2300 }
2301 target.recycle();
2302 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07002303 continue;
2304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002306 predecessor = target;
2307 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002309 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002310
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002311 // Update list of touch targets for pointer up or cancel, if needed.
2312 if (canceled
2313 || actionMasked == MotionEvent.ACTION_UP
2314 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
2315 resetTouchState();
2316 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
2317 final int actionIndex = ev.getActionIndex();
2318 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
2319 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 }
2321 }
Romain Guy8506ab42009-06-11 17:35:47 -07002322
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002323 if (!handled && mInputEventConsistencyVerifier != null) {
2324 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07002325 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002326 return handled;
2327 }
2328
Romain Guy469b1db2010-10-05 11:49:57 -07002329 /**
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002330 * Provide custom ordering of views in which the touch will be dispatched.
2331 *
2332 * This is called within a tight loop, so you are not allowed to allocate objects, including
2333 * the return array. Instead, you should return a pre-allocated list that will be cleared
2334 * after the dispatch is finished.
2335 * @hide
2336 */
2337 public ArrayList<View> buildTouchDispatchChildList() {
2338 return buildOrderedChildList();
2339 }
2340
2341 /**
Svetoslavc73cfa02015-02-09 17:14:28 -08002342 * Finds the child which has accessibility focus.
2343 *
2344 * @return The child that has focus.
2345 */
2346 private View findChildWithAccessibilityFocus() {
2347 ViewRootImpl viewRoot = getViewRootImpl();
2348 if (viewRoot == null) {
2349 return null;
2350 }
2351
2352 View current = viewRoot.getAccessibilityFocusedHost();
2353 if (current == null) {
2354 return null;
2355 }
2356
2357 ViewParent parent = current.getParent();
2358 while (parent instanceof View) {
2359 if (parent == this) {
2360 return current;
2361 }
2362 current = (View) parent;
2363 parent = current.getParent();
2364 }
2365
2366 return null;
2367 }
2368
2369 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002370 * Resets all touch state in preparation for a new cycle.
2371 */
2372 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002373 clearTouchTargets();
2374 resetCancelNextUpFlag(this);
2375 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
Adam Powell10ba2772014-04-15 09:46:51 -07002376 mNestedScrollAxes = SCROLL_AXIS_NONE;
Jeff Brown20e987b2010-08-23 12:01:02 -07002377 }
2378
Romain Guy469b1db2010-10-05 11:49:57 -07002379 /**
2380 * Resets the cancel next up flag.
2381 * Returns true if the flag was previously set.
2382 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002383 private static boolean resetCancelNextUpFlag(@NonNull View view) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002384 if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) {
2385 view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Jeff Brown20e987b2010-08-23 12:01:02 -07002386 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07002387 }
2388 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390
Romain Guy469b1db2010-10-05 11:49:57 -07002391 /**
2392 * Clears all touch targets.
2393 */
2394 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002395 TouchTarget target = mFirstTouchTarget;
2396 if (target != null) {
2397 do {
2398 TouchTarget next = target.next;
2399 target.recycle();
2400 target = next;
2401 } while (target != null);
2402 mFirstTouchTarget = null;
2403 }
2404 }
2405
Romain Guy469b1db2010-10-05 11:49:57 -07002406 /**
2407 * Cancels and clears all touch targets.
2408 */
2409 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002410 if (mFirstTouchTarget != null) {
2411 boolean syntheticEvent = false;
2412 if (event == null) {
2413 final long now = SystemClock.uptimeMillis();
2414 event = MotionEvent.obtain(now, now,
2415 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07002416 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07002417 syntheticEvent = true;
2418 }
2419
2420 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2421 resetCancelNextUpFlag(target.child);
2422 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
2423 }
2424 clearTouchTargets();
2425
2426 if (syntheticEvent) {
2427 event.recycle();
2428 }
2429 }
2430 }
2431
Romain Guy469b1db2010-10-05 11:49:57 -07002432 /**
2433 * Gets the touch target for specified child view.
2434 * Returns null if not found.
2435 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002436 private TouchTarget getTouchTarget(@NonNull View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002437 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2438 if (target.child == child) {
2439 return target;
2440 }
2441 }
2442 return null;
2443 }
2444
Romain Guy469b1db2010-10-05 11:49:57 -07002445 /**
2446 * Adds a touch target for specified child to the beginning of the list.
2447 * Assumes the target child is not already present.
2448 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002449 private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
2450 final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
Jeff Brown20e987b2010-08-23 12:01:02 -07002451 target.next = mFirstTouchTarget;
2452 mFirstTouchTarget = target;
2453 return target;
2454 }
2455
Romain Guy469b1db2010-10-05 11:49:57 -07002456 /**
2457 * Removes the pointer ids from consideration.
2458 */
2459 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002460 TouchTarget predecessor = null;
2461 TouchTarget target = mFirstTouchTarget;
2462 while (target != null) {
2463 final TouchTarget next = target.next;
2464 if ((target.pointerIdBits & pointerIdBits) != 0) {
2465 target.pointerIdBits &= ~pointerIdBits;
2466 if (target.pointerIdBits == 0) {
2467 if (predecessor == null) {
2468 mFirstTouchTarget = next;
2469 } else {
2470 predecessor.next = next;
2471 }
2472 target.recycle();
2473 target = next;
2474 continue;
2475 }
2476 }
2477 predecessor = target;
2478 target = next;
2479 }
2480 }
2481
Jeff Brown59a422e2012-04-19 15:19:19 -07002482 private void cancelTouchTarget(View view) {
2483 TouchTarget predecessor = null;
2484 TouchTarget target = mFirstTouchTarget;
2485 while (target != null) {
2486 final TouchTarget next = target.next;
2487 if (target.child == view) {
2488 if (predecessor == null) {
2489 mFirstTouchTarget = next;
2490 } else {
2491 predecessor.next = next;
2492 }
2493 target.recycle();
2494
2495 final long now = SystemClock.uptimeMillis();
2496 MotionEvent event = MotionEvent.obtain(now, now,
2497 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2498 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2499 view.dispatchTouchEvent(event);
2500 event.recycle();
2501 return;
2502 }
2503 predecessor = target;
2504 target = next;
2505 }
2506 }
2507
Romain Guy469b1db2010-10-05 11:49:57 -07002508 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002509 * Returns true if a child view can receive pointer events.
2510 * @hide
2511 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002512 private static boolean canViewReceivePointerEvents(@NonNull View child) {
Jeff Browna032cc02011-03-07 16:56:21 -08002513 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2514 || child.getAnimation() != null;
2515 }
2516
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002517 private float[] getTempPoint() {
2518 if (mTempPoint == null) {
2519 mTempPoint = new float[2];
2520 }
2521 return mTempPoint;
2522 }
2523
Jeff Browna032cc02011-03-07 16:56:21 -08002524 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002525 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07002526 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07002527 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07002528 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07002529 */
Adam Cohena32edd42010-10-26 10:35:01 -07002530 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07002531 PointF outLocalPoint) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002532 final float[] point = getTempPoint();
2533 point[0] = x;
2534 point[1] = y;
2535 transformPointToViewLocal(point, child);
2536 final boolean isInView = child.pointInView(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002537 if (isInView && outLocalPoint != null) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002538 outLocalPoint.set(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002539 }
2540 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07002541 }
2542
Romain Guy469b1db2010-10-05 11:49:57 -07002543 /**
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002544 * @hide
2545 */
2546 public void transformPointToViewLocal(float[] point, View child) {
2547 point[0] += mScrollX - child.mLeft;
2548 point[1] += mScrollY - child.mTop;
2549
2550 if (!child.hasIdentityMatrix()) {
2551 child.getInverseMatrix().mapPoints(point);
2552 }
2553 }
2554
2555 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002556 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07002557 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07002558 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
2559 */
2560 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07002561 View child, int desiredPointerIdBits) {
2562 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07002563
Jeff Brown20e987b2010-08-23 12:01:02 -07002564 // Canceling motions is a special case. We don't need to perform any transformations
2565 // or filtering. The important part is the action, not the contents.
2566 final int oldAction = event.getAction();
2567 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
2568 event.setAction(MotionEvent.ACTION_CANCEL);
2569 if (child == null) {
2570 handled = super.dispatchTouchEvent(event);
2571 } else {
2572 handled = child.dispatchTouchEvent(event);
2573 }
2574 event.setAction(oldAction);
2575 return handled;
2576 }
Adam Powell2b342f02010-08-18 18:14:13 -07002577
Jeff Brown20e987b2010-08-23 12:01:02 -07002578 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002579 final int oldPointerIdBits = event.getPointerIdBits();
2580 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07002581
Jeff Brown20e987b2010-08-23 12:01:02 -07002582 // If for some reason we ended up in an inconsistent state where it looks like we
2583 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002584 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002585 return false;
2586 }
Adam Powell2b342f02010-08-18 18:14:13 -07002587
Jeff Brown20e987b2010-08-23 12:01:02 -07002588 // If the number of pointers is the same and we don't need to perform any fancy
2589 // irreversible transformations, then we can reuse the motion event for this
2590 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002591 // Otherwise we need to make a copy.
2592 final MotionEvent transformedEvent;
2593 if (newPointerIdBits == oldPointerIdBits) {
2594 if (child == null || child.hasIdentityMatrix()) {
2595 if (child == null) {
2596 handled = super.dispatchTouchEvent(event);
2597 } else {
2598 final float offsetX = mScrollX - child.mLeft;
2599 final float offsetY = mScrollY - child.mTop;
2600 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07002601
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002602 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07002603
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002604 event.offsetLocation(-offsetX, -offsetY);
2605 }
2606 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07002607 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002608 transformedEvent = MotionEvent.obtain(event);
2609 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002610 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07002611 }
2612
Jeff Brown20e987b2010-08-23 12:01:02 -07002613 // Perform any necessary transformations and dispatch.
2614 if (child == null) {
2615 handled = super.dispatchTouchEvent(transformedEvent);
2616 } else {
2617 final float offsetX = mScrollX - child.mLeft;
2618 final float offsetY = mScrollY - child.mTop;
2619 transformedEvent.offsetLocation(offsetX, offsetY);
2620 if (! child.hasIdentityMatrix()) {
2621 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07002622 }
2623
Jeff Brown20e987b2010-08-23 12:01:02 -07002624 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07002625 }
2626
Jeff Brown20e987b2010-08-23 12:01:02 -07002627 // Done.
2628 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07002629 return handled;
2630 }
2631
Romain Guy469b1db2010-10-05 11:49:57 -07002632 /**
Adam Powell2b342f02010-08-18 18:14:13 -07002633 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08002634 * dispatch. This behavior is enabled by default for applications that target an
2635 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07002636 *
2637 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
2638 * views depending on where each pointer initially went down. This allows for user interactions
2639 * such as scrolling two panes of content independently, chording of buttons, and performing
2640 * independent gestures on different pieces of content.
2641 *
2642 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2643 * child views. <code>false</code> to only allow one child view to be the target of
2644 * any MotionEvent received by this ViewGroup.
Scott Main27a85082013-06-10 10:39:48 -07002645 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
Adam Powell2b342f02010-08-18 18:14:13 -07002646 */
2647 public void setMotionEventSplittingEnabled(boolean split) {
2648 // TODO Applications really shouldn't change this setting mid-touch event,
2649 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2650 // with gestures in progress when this is changed.
2651 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07002652 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2653 } else {
2654 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07002655 }
2656 }
2657
2658 /**
Jeff Brown995e7742010-12-22 16:59:36 -08002659 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07002660 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2661 */
2662 public boolean isMotionEventSplittingEnabled() {
2663 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2664 }
2665
2666 /**
George Mount0a778ed2013-12-13 13:35:36 -08002667 * Returns true if this ViewGroup should be considered as a single entity for removal
2668 * when executing an Activity transition. If this is false, child elements will move
2669 * individually during the transition.
George Mount427c6412014-11-05 16:45:36 -08002670 *
George Mount0a778ed2013-12-13 13:35:36 -08002671 * @return True if the ViewGroup should be acted on together during an Activity transition.
George Mount427c6412014-11-05 16:45:36 -08002672 * The default value is true when there is a non-null background or if
2673 * {@link #getTransitionName()} is not null or if a
2674 * non-null {@link android.view.ViewOutlineProvider} other than
2675 * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to
2676 * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise.
George Mount0a778ed2013-12-13 13:35:36 -08002677 */
2678 public boolean isTransitionGroup() {
2679 if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) {
2680 return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0);
2681 } else {
George Mount427c6412014-11-05 16:45:36 -08002682 final ViewOutlineProvider outlineProvider = getOutlineProvider();
2683 return getBackground() != null || getTransitionName() != null ||
2684 (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND);
George Mount0a778ed2013-12-13 13:35:36 -08002685 }
2686 }
2687
2688 /**
2689 * Changes whether or not this ViewGroup should be treated as a single entity during
George Mount31a21722014-03-24 17:44:36 -07002690 * Activity Transitions.
George Mount0a778ed2013-12-13 13:35:36 -08002691 * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
2692 * in Activity transitions. If false, the ViewGroup won't transition,
2693 * only its children. If true, the entire ViewGroup will transition
2694 * together.
George Mount62ab9b72014-05-02 13:51:17 -07002695 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity,
2696 * android.util.Pair[])
George Mount0a778ed2013-12-13 13:35:36 -08002697 */
2698 public void setTransitionGroup(boolean isTransitionGroup) {
2699 mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET;
2700 if (isTransitionGroup) {
2701 mGroupFlags |= FLAG_IS_TRANSITION_GROUP;
2702 } else {
2703 mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP;
2704 }
2705 }
2706
Alan Viverettebe463f22016-01-21 10:50:10 -05002707 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07002709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2711 // We're already in this state, assume our ancestors are too
2712 return;
2713 }
Romain Guy8506ab42009-06-11 17:35:47 -07002714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 if (disallowIntercept) {
2716 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2717 } else {
2718 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2719 }
Romain Guy8506ab42009-06-11 17:35:47 -07002720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 // Pass it up to our parent
2722 if (mParent != null) {
2723 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2724 }
2725 }
2726
2727 /**
2728 * Implement this method to intercept all touch screen motion events. This
2729 * allows you to watch events as they are dispatched to your children, and
2730 * take ownership of the current gesture at any point.
2731 *
2732 * <p>Using this function takes some care, as it has a fairly complicated
2733 * interaction with {@link View#onTouchEvent(MotionEvent)
2734 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2735 * that method as well as this one in the correct way. Events will be
2736 * received in the following order:
2737 *
2738 * <ol>
2739 * <li> You will receive the down event here.
2740 * <li> The down event will be handled either by a child of this view
2741 * group, or given to your own onTouchEvent() method to handle; this means
2742 * you should implement onTouchEvent() to return true, so you will
2743 * continue to see the rest of the gesture (instead of looking for
2744 * a parent view to handle it). Also, by returning true from
2745 * onTouchEvent(), you will not receive any following
2746 * events in onInterceptTouchEvent() and all touch processing must
2747 * happen in onTouchEvent() like normal.
2748 * <li> For as long as you return false from this function, each following
2749 * event (up to and including the final up) will be delivered first here
2750 * and then to the target's onTouchEvent().
2751 * <li> If you return true from here, you will not receive any
2752 * following events: the target view will receive the same event but
2753 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2754 * events will be delivered to your onTouchEvent() method and no longer
2755 * appear here.
2756 * </ol>
2757 *
2758 * @param ev The motion event being dispatched down the hierarchy.
2759 * @return Return true to steal motion events from the children and have
2760 * them dispatched to this ViewGroup through onTouchEvent().
2761 * The current target will receive an ACTION_CANCEL event, and no further
2762 * messages will be delivered here.
2763 */
2764 public boolean onInterceptTouchEvent(MotionEvent ev) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002765 if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
2766 && ev.getAction() == MotionEvent.ACTION_DOWN
2767 && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
2768 && isOnScrollbarThumb(ev.getX(), ev.getY())) {
2769 return true;
2770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 return false;
2772 }
2773
2774 /**
2775 * {@inheritDoc}
2776 *
2777 * Looks for a view to give focus to respecting the setting specified by
2778 * {@link #getDescendantFocusability()}.
2779 *
2780 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2781 * find focus within the children of this group when appropriate.
2782 *
2783 * @see #FOCUS_BEFORE_DESCENDANTS
2784 * @see #FOCUS_AFTER_DESCENDANTS
2785 * @see #FOCUS_BLOCK_DESCENDANTS
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002786 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 */
2788 @Override
2789 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2790 if (DBG) {
2791 System.out.println(this + " ViewGroup.requestFocus direction="
2792 + direction);
2793 }
2794 int descendantFocusability = getDescendantFocusability();
2795
2796 switch (descendantFocusability) {
2797 case FOCUS_BLOCK_DESCENDANTS:
2798 return super.requestFocus(direction, previouslyFocusedRect);
2799 case FOCUS_BEFORE_DESCENDANTS: {
2800 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2801 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2802 }
2803 case FOCUS_AFTER_DESCENDANTS: {
2804 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2805 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2806 }
2807 default:
2808 throw new IllegalStateException("descendant focusability must be "
2809 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2810 + "but is " + descendantFocusability);
2811 }
2812 }
2813
2814 /**
2815 * Look for a descendant to call {@link View#requestFocus} on.
2816 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2817 * when it wants to request focus within its children. Override this to
2818 * customize how your {@link ViewGroup} requests focus within its children.
2819 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2820 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2821 * to give a finer grained hint about where focus is coming from. May be null
2822 * if there is no hint.
2823 * @return Whether focus was taken.
2824 */
2825 @SuppressWarnings({"ConstantConditions"})
2826 protected boolean onRequestFocusInDescendants(int direction,
2827 Rect previouslyFocusedRect) {
2828 int index;
2829 int increment;
2830 int end;
2831 int count = mChildrenCount;
2832 if ((direction & FOCUS_FORWARD) != 0) {
2833 index = 0;
2834 increment = 1;
2835 end = count;
2836 } else {
2837 index = count - 1;
2838 increment = -1;
2839 end = -1;
2840 }
2841 final View[] children = mChildren;
2842 for (int i = index; i != end; i += increment) {
2843 View child = children[i];
2844 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2845 if (child.requestFocus(direction, previouslyFocusedRect)) {
2846 return true;
2847 }
2848 }
2849 }
2850 return false;
2851 }
Chet Haase5c13d892010-10-08 08:37:55 -07002852
Romain Guya440b002010-02-24 15:57:54 -08002853 /**
2854 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002855 *
Romain Guydcc490f2010-02-24 17:59:35 -08002856 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002857 */
2858 @Override
2859 public void dispatchStartTemporaryDetach() {
2860 super.dispatchStartTemporaryDetach();
2861 final int count = mChildrenCount;
2862 final View[] children = mChildren;
2863 for (int i = 0; i < count; i++) {
2864 children[i].dispatchStartTemporaryDetach();
2865 }
2866 }
Chet Haase5c13d892010-10-08 08:37:55 -07002867
Romain Guya440b002010-02-24 15:57:54 -08002868 /**
2869 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002870 *
Romain Guydcc490f2010-02-24 17:59:35 -08002871 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002872 */
2873 @Override
2874 public void dispatchFinishTemporaryDetach() {
2875 super.dispatchFinishTemporaryDetach();
2876 final int count = mChildrenCount;
2877 final View[] children = mChildren;
2878 for (int i = 0; i < count; i++) {
2879 children[i].dispatchFinishTemporaryDetach();
2880 }
2881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 @Override
2884 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07002885 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07002887 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 final int count = mChildrenCount;
2890 final View[] children = mChildren;
2891 for (int i = 0; i < count; i++) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002892 final View child = children[i];
2893 child.dispatchAttachedToWindow(info,
Adam Powelleb2b0af2015-05-20 18:26:35 -07002894 combineVisibility(visibility, child.getVisibility()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 }
Chet Haasec633d2f2015-04-07 10:29:39 -07002896 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
2897 for (int i = 0; i < transientCount; ++i) {
2898 View view = mTransientViews.get(i);
Adam Powelleb2b0af2015-05-20 18:26:35 -07002899 view.dispatchAttachedToWindow(info,
2900 combineVisibility(visibility, view.getVisibility()));
Chet Haasec633d2f2015-04-07 10:29:39 -07002901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 }
2903
svetoslavganov75986cf2009-05-14 22:28:01 -07002904 @Override
Romain Guybb9908b2012-03-08 11:14:07 -08002905 void dispatchScreenStateChanged(int screenState) {
2906 super.dispatchScreenStateChanged(screenState);
2907
2908 final int count = mChildrenCount;
2909 final View[] children = mChildren;
2910 for (int i = 0; i < count; i++) {
2911 children[i].dispatchScreenStateChanged(screenState);
2912 }
2913 }
2914
Alan Viverettea54956a2015-01-07 16:05:02 -08002915 /** @hide */
Romain Guybb9908b2012-03-08 11:14:07 -08002916 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002917 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002918 boolean handled = false;
2919 if (includeForAccessibility()) {
2920 handled = super.dispatchPopulateAccessibilityEventInternal(event);
2921 if (handled) {
2922 return handled;
2923 }
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002924 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002925 // Let our children have a shot in populating the event.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002926 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002927 try {
2928 final int childCount = children.getChildCount();
2929 for (int i = 0; i < childCount; i++) {
2930 View child = children.getChildAt(i);
2931 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2932 handled = child.dispatchPopulateAccessibilityEvent(event);
2933 if (handled) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002934 return handled;
2935 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002936 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002937 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002938 } finally {
2939 children.recycle();
svetoslavganov75986cf2009-05-14 22:28:01 -07002940 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002941 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07002942 }
2943
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002944 /**
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002945 * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002946 * adds in all child views of the view group, in addition to calling the default View
2947 * implementation.
2948 */
Alan Viverettebe463f22016-01-21 10:50:10 -05002949 @Override
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002950 public void dispatchProvideStructure(ViewStructure structure) {
2951 super.dispatchProvideStructure(structure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002952 if (!isAssistBlocked()) {
2953 if (structure.getChildCount() == 0) {
2954 final int childrenCount = getChildCount();
2955 if (childrenCount > 0) {
2956 structure.setChildCount(childrenCount);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002957 ArrayList<View> preorderedList = buildOrderedChildList();
2958 boolean customOrder = preorderedList == null
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002959 && isChildrenDrawingOrderEnabled();
2960 final View[] children = mChildren;
2961 for (int i=0; i<childrenCount; i++) {
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002962 int childIndex;
2963 try {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002964 childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002965 } catch (IndexOutOfBoundsException e) {
2966 childIndex = i;
2967 if (mContext.getApplicationInfo().targetSdkVersion
2968 < Build.VERSION_CODES.M) {
2969 Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ "
2970 + i + " of " + childrenCount, e);
2971 // At least one app is failing when we call getChildDrawingOrder
2972 // at this point, so deal semi-gracefully with it by falling back
2973 // on the basic order.
2974 customOrder = false;
2975 if (i > 0) {
2976 // If we failed at the first index, there really isn't
2977 // anything to do -- we will just proceed with the simple
2978 // sequence order.
2979 // Otherwise, we failed in the middle, so need to come up
2980 // with an order for the remaining indices and use that.
2981 // Failed at the first one, easy peasy.
2982 int[] permutation = new int[childrenCount];
2983 SparseBooleanArray usedIndices = new SparseBooleanArray();
2984 // Go back and collected the indices we have done so far.
2985 for (int j=0; j<i; j++) {
2986 permutation[j] = getChildDrawingOrder(childrenCount, j);
2987 usedIndices.put(permutation[j], true);
2988 }
2989 // Fill in the remaining indices with indices that have not
2990 // yet been used.
2991 int nextIndex = 0;
2992 for (int j=i; j<childrenCount; j++) {
2993 while (usedIndices.get(nextIndex, false)) {
2994 nextIndex++;
2995 }
2996 permutation[j] = nextIndex;
2997 nextIndex++;
2998 }
2999 // Build the final view list.
3000 preorderedList = new ArrayList<>(childrenCount);
3001 for (int j=0; j<childrenCount; j++) {
3002 preorderedList.add(children[permutation[j]]);
3003 }
3004 }
3005 } else {
3006 throw e;
3007 }
3008 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05003009
3010 final View child = getAndVerifyPreorderedView(
3011 preorderedList, children, childIndex);
3012 final ViewStructure cstructure = structure.newChild(i);
Dianne Hackborn49b043f2015-05-07 14:21:38 -07003013 child.dispatchProvideStructure(cstructure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07003014 }
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07003015 }
3016 }
3017 }
3018 }
3019
Alan Viverettea7b85e62016-01-22 10:14:02 -05003020 private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,
3021 int childIndex) {
3022 final View child;
3023 if (preorderedList != null) {
3024 child = preorderedList.get(childIndex);
3025 if (child == null) {
3026 throw new RuntimeException("Invalid preorderedList contained null child at index "
3027 + childIndex);
3028 }
3029 } else {
3030 child = children[childIndex];
3031 }
3032 return child;
3033 }
3034
Alan Viverettea54956a2015-01-07 16:05:02 -08003035 /** @hide */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003036 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08003037 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003038 super.onInitializeAccessibilityNodeInfoInternal(info);
Svet Ganov55bdb102015-02-06 12:41:17 -08003039 if (getAccessibilityNodeProvider() != null) {
3040 return;
3041 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003042 if (mAttachInfo != null) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003043 final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003044 childrenForAccessibility.clear();
3045 addChildrenForAccessibility(childrenForAccessibility);
3046 final int childrenForAccessibilityCount = childrenForAccessibility.size();
3047 for (int i = 0; i < childrenForAccessibilityCount; i++) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003048 final View child = childrenForAccessibility.get(i);
3049 info.addChildUnchecked(child);
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07003050 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003051 childrenForAccessibility.clear();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003052 }
3053 }
3054
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003055 @Override
Maxim Bogatovf399af32015-06-16 15:15:49 -07003056 public CharSequence getAccessibilityClassName() {
3057 return ViewGroup.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003058 }
3059
Svetoslav Ganov42138042012-03-20 11:51:39 -07003060 @Override
Alan Viverette77e9a282013-09-12 17:16:09 -07003061 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
3062 // If this is a live region, we should send a subtree change event
3063 // from this view. Otherwise, we can let it propagate up.
3064 if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) {
3065 notifyViewAccessibilityStateChangedIfNeeded(changeType);
3066 } else if (mParent != null) {
Adam Powell504a10f2013-07-11 15:25:59 -07003067 try {
Alan Viverette77e9a282013-09-12 17:16:09 -07003068 mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType);
Adam Powell504a10f2013-07-11 15:25:59 -07003069 } catch (AbstractMethodError e) {
3070 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
3071 " does not fully implement ViewParent", e);
3072 }
Svetoslav6254f482013-06-04 17:22:14 -07003073 }
3074 }
3075
3076 @Override
3077 void resetSubtreeAccessibilityStateChanged() {
3078 super.resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003079 View[] children = mChildren;
3080 final int childCount = mChildrenCount;
3081 for (int i = 0; i < childCount; i++) {
Svetoslav6254f482013-06-04 17:22:14 -07003082 children[i].resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003083 }
3084 }
3085
3086 /**
Phil Weaver1f222542016-01-08 11:49:32 -08003087 * Counts the number of children of this View that will be sent to an accessibility service.
3088 *
3089 * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View
3090 * would have.
3091 */
3092 int getNumChildrenForAccessibility() {
3093 int numChildrenForAccessibility = 0;
3094 for (int i = 0; i < getChildCount(); i++) {
3095 View child = getChildAt(i);
3096 if (child.includeForAccessibility()) {
3097 numChildrenForAccessibility++;
3098 } else if (child instanceof ViewGroup) {
3099 numChildrenForAccessibility += ((ViewGroup) child)
3100 .getNumChildrenForAccessibility();
3101 }
3102 }
3103 return numChildrenForAccessibility;
3104 }
3105
3106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 * {@inheritDoc}
Adam Powellb6ab0982015-01-07 17:00:12 -08003108 *
3109 * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p>
3110 *
3111 * @param target The target view dispatching this action
3112 * @param action Action being performed; see
3113 * {@link android.view.accessibility.AccessibilityNodeInfo}
3114 * @param args Optional action arguments
3115 * @return false by default. Subclasses should return true if they handle the event.
3116 */
3117 @Override
3118 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
3119 return false;
3120 }
3121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 @Override
3123 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07003124 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 // dispatching motion events to a child; we need to get rid of that
3126 // child to avoid dispatching events to it after the window is torn
3127 // down. To make sure we keep the child in a consistent state, we
3128 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07003129 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130
Jeff Brown59a422e2012-04-19 15:19:19 -07003131 // Similarly, set ACTION_EXIT to all hover targets and clear them.
3132 exitHoverTargets();
3133
Chet Haase9c087442011-01-12 16:20:16 -08003134 // In case view is detached while transition is running
Chet Haaseb9895022013-04-02 15:10:58 -07003135 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08003136
Christopher Tate86cab1b2011-01-13 20:28:55 -08003137 // Tear down our drag tracking
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07003138 mChildrenInterestedInDrag = null;
3139 mIsInterestedInDrag = false;
3140 if (mCurrentDragStartEvent != null) {
3141 mCurrentDragStartEvent.recycle();
3142 mCurrentDragStartEvent = null;
Christopher Tate86cab1b2011-01-13 20:28:55 -08003143 }
3144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 final int count = mChildrenCount;
3146 final View[] children = mChildren;
3147 for (int i = 0; i < count; i++) {
3148 children[i].dispatchDetachedFromWindow();
3149 }
John Reckca7a9da2014-03-05 16:29:07 -08003150 clearDisappearingChildren();
Chet Haasec633d2f2015-04-07 10:29:39 -07003151 final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size();
3152 for (int i = 0; i < transientCount; ++i) {
3153 View view = mTransientViews.get(i);
3154 view.dispatchDetachedFromWindow();
3155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 super.dispatchDetachedFromWindow();
3157 }
3158
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003159 /**
3160 * @hide
3161 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 @Override
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003163 protected void internalSetPadding(int left, int top, int right, int bottom) {
Romain Guy2440e672012-08-07 14:43:43 -07003164 super.internalSetPadding(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165
Romain Guy13f35f32011-03-24 12:03:17 -07003166 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 mGroupFlags |= FLAG_PADDING_NOT_NULL;
3168 } else {
3169 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
3170 }
3171 }
3172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 @Override
3174 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
3175 super.dispatchSaveInstanceState(container);
3176 final int count = mChildrenCount;
3177 final View[] children = mChildren;
3178 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003179 View c = children[i];
3180 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3181 c.dispatchSaveInstanceState(container);
3182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 }
3184 }
3185
3186 /**
Romain Guy9fc27812011-04-27 14:21:41 -07003187 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
3188 * to only this view, not to its children. For use when overriding
3189 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
3190 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 *
3192 * @param container the container
3193 */
3194 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
3195 super.dispatchSaveInstanceState(container);
3196 }
3197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 @Override
3199 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
3200 super.dispatchRestoreInstanceState(container);
3201 final int count = mChildrenCount;
3202 final View[] children = mChildren;
3203 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003204 View c = children[i];
3205 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3206 c.dispatchRestoreInstanceState(container);
3207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
3209 }
3210
3211 /**
Romain Guy02739a82011-05-16 11:43:18 -07003212 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
3213 * to only this view, not to its children. For use when overriding
3214 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
3215 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 *
3217 * @param container the container
3218 */
3219 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
3220 super.dispatchRestoreInstanceState(container);
3221 }
3222
3223 /**
3224 * Enables or disables the drawing cache for each child of this view group.
3225 *
3226 * @param enabled true to enable the cache, false to dispose of it
3227 */
3228 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
3229 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
3230 final View[] children = mChildren;
3231 final int count = mChildrenCount;
3232 for (int i = 0; i < count; i++) {
3233 children[i].setDrawingCacheEnabled(enabled);
3234 }
3235 }
3236 }
3237
3238 @Override
Romain Guy223ff5c2010-03-02 17:07:47 -08003239 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003240 int count = mChildrenCount;
3241 int[] visibilities = null;
3242
Romain Guy223ff5c2010-03-02 17:07:47 -08003243 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003244 visibilities = new int[count];
3245 for (int i = 0; i < count; i++) {
3246 View child = getChildAt(i);
3247 visibilities[i] = child.getVisibility();
3248 if (visibilities[i] == View.VISIBLE) {
3249 child.setVisibility(INVISIBLE);
3250 }
3251 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003252 }
3253
3254 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07003255
3256 if (skipChildren) {
3257 for (int i = 0; i < count; i++) {
3258 getChildAt(i).setVisibility(visibilities[i]);
Chet Haase5c13d892010-10-08 08:37:55 -07003259 }
Romain Guy65554f22010-03-22 18:58:21 -07003260 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003261
3262 return b;
3263 }
3264
Philip Milne7b757812012-09-19 18:13:44 -07003265 /** Return true if this ViewGroup is laying out using optical bounds. */
3266 boolean isLayoutModeOptical() {
3267 return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS;
3268 }
Romain Guycbc67742012-04-27 16:12:57 -07003269
Alan Viverettebe463f22016-01-21 10:50:10 -05003270 @Override
Philip Milne7b757812012-09-19 18:13:44 -07003271 Insets computeOpticalInsets() {
3272 if (isLayoutModeOptical()) {
3273 int left = 0;
3274 int top = 0;
3275 int right = 0;
3276 int bottom = 0;
3277 for (int i = 0; i < mChildrenCount; i++) {
3278 View child = getChildAt(i);
3279 if (child.getVisibility() == VISIBLE) {
3280 Insets insets = child.getOpticalInsets();
3281 left = Math.max(left, insets.left);
3282 top = Math.max(top, insets.top);
3283 right = Math.max(right, insets.right);
3284 bottom = Math.max(bottom, insets.bottom);
3285 }
3286 }
3287 return Insets.of(left, top, right, bottom);
3288 } else {
3289 return Insets.NONE;
3290 }
3291 }
3292
3293 private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
3294 if (x1 != x2 && y1 != y2) {
3295 if (x1 > x2) {
3296 int tmp = x1; x1 = x2; x2 = tmp;
3297 }
3298 if (y1 > y2) {
3299 int tmp = y1; y1 = y2; y2 = tmp;
3300 }
3301 canvas.drawRect(x1, y1, x2, y2, paint);
3302 }
3303 }
3304
3305 private static int sign(int x) {
3306 return (x >= 0) ? 1 : -1;
3307 }
3308
3309 private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) {
3310 fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy));
3311 fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
3312 }
3313
3314 private int dipsToPixels(int dips) {
3315 float scale = getContext().getResources().getDisplayMetrics().density;
3316 return (int) (dips * scale + 0.5f);
3317 }
3318
Romain Guy6410c0a2013-06-17 11:21:58 -07003319 private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
3320 int lineLength, int lineWidth) {
Philip Milne7b757812012-09-19 18:13:44 -07003321 drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
3322 drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth);
3323 drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth);
3324 drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth);
3325 }
3326
3327 private static void fillDifference(Canvas canvas,
3328 int x2, int y2, int x3, int y3,
3329 int dx1, int dy1, int dx2, int dy2, Paint paint) {
3330 int x1 = x2 - dx1;
3331 int y1 = y2 - dy1;
3332
3333 int x4 = x3 + dx2;
3334 int y4 = y3 + dy2;
3335
3336 fillRect(canvas, paint, x1, y1, x4, y2);
3337 fillRect(canvas, paint, x1, y2, x2, y3);
3338 fillRect(canvas, paint, x3, y2, x4, y3);
3339 fillRect(canvas, paint, x1, y3, x4, y4);
Philip Milne10ca24a2012-04-23 15:38:27 -07003340 }
3341
3342 /**
3343 * @hide
3344 */
Philip Milne7b757812012-09-19 18:13:44 -07003345 protected void onDebugDrawMargins(Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07003346 for (int i = 0; i < getChildCount(); i++) {
3347 View c = getChildAt(i);
Philip Milne7b757812012-09-19 18:13:44 -07003348 c.getLayoutParams().onDebugDraw(c, canvas, paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07003349 }
3350 }
3351
3352 /**
3353 * @hide
3354 */
3355 protected void onDebugDraw(Canvas canvas) {
Philip Milne7b757812012-09-19 18:13:44 -07003356 Paint paint = getDebugPaint();
3357
Philip Milne10ca24a2012-04-23 15:38:27 -07003358 // Draw optical bounds
Philip Milne7b757812012-09-19 18:13:44 -07003359 {
3360 paint.setColor(Color.RED);
3361 paint.setStyle(Paint.Style.STROKE);
Philip Milne7b757812012-09-19 18:13:44 -07003362
Philip Milne10ca24a2012-04-23 15:38:27 -07003363 for (int i = 0; i < getChildCount(); i++) {
3364 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003365 if (c.getVisibility() != View.GONE) {
3366 Insets insets = c.getOpticalInsets();
Philip Milne7b757812012-09-19 18:13:44 -07003367
Chris Craika1dab8b2015-06-30 13:51:25 -07003368 drawRect(canvas, paint,
3369 c.getLeft() + insets.left,
3370 c.getTop() + insets.top,
3371 c.getRight() - insets.right - 1,
3372 c.getBottom() - insets.bottom - 1);
3373 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003374 }
3375 }
3376
Philip Milne10ca24a2012-04-23 15:38:27 -07003377 // Draw margins
Philip Milne7b757812012-09-19 18:13:44 -07003378 {
3379 paint.setColor(Color.argb(63, 255, 0, 255));
3380 paint.setStyle(Paint.Style.FILL);
Philip Milne604f4402012-04-24 19:27:11 -07003381
Philip Milne7b757812012-09-19 18:13:44 -07003382 onDebugDrawMargins(canvas, paint);
3383 }
3384
3385 // Draw clip bounds
3386 {
3387 paint.setColor(Color.rgb(63, 127, 255));
3388 paint.setStyle(Paint.Style.FILL);
3389
3390 int lineLength = dipsToPixels(8);
3391 int lineWidth = dipsToPixels(1);
3392 for (int i = 0; i < getChildCount(); i++) {
3393 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003394 if (c.getVisibility() != View.GONE) {
3395 drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(),
3396 paint, lineLength, lineWidth);
3397 }
Philip Milne7b757812012-09-19 18:13:44 -07003398 }
Philip Milne604f4402012-04-24 19:27:11 -07003399 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003400 }
3401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 @Override
3403 protected void dispatchDraw(Canvas canvas) {
Chris Craika753f4c2014-07-24 12:39:17 -07003404 boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
Chris Craikab008f02014-05-23 17:55:03 -07003405 final int childrenCount = mChildrenCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 final View[] children = mChildren;
3407 int flags = mGroupFlags;
3408
3409 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
Romain Guy0d9275e2010-10-26 14:22:30 -07003410 final boolean buildCache = !isHardwareAccelerated();
Chris Craikab008f02014-05-23 17:55:03 -07003411 for (int i = 0; i < childrenCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 final View child = children[i];
3413 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
3414 final LayoutParams params = child.getLayoutParams();
Chris Craikab008f02014-05-23 17:55:03 -07003415 attachLayoutAnimationParameters(child, params, i, childrenCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 bindLayoutAnimation(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 }
3418 }
3419
3420 final LayoutAnimationController controller = mLayoutAnimationController;
3421 if (controller.willOverlap()) {
3422 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
3423 }
3424
3425 controller.start();
3426
3427 mGroupFlags &= ~FLAG_RUN_ANIMATION;
3428 mGroupFlags &= ~FLAG_ANIMATION_DONE;
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (mAnimationListener != null) {
3431 mAnimationListener.onAnimationStart(controller.getAnimation());
3432 }
3433 }
3434
Selim Cinek19cadc22014-04-16 17:27:19 +02003435 int clipSaveCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3437 if (clipToPadding) {
Chris Craike4cf1522014-08-04 17:55:22 -07003438 clipSaveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07003439 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
3440 mScrollX + mRight - mLeft - mPaddingRight,
3441 mScrollY + mBottom - mTop - mPaddingBottom);
Selim Cinek19cadc22014-04-16 17:27:19 +02003442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 // We will draw our child's animation, let's reset the flag
Dianne Hackborn4702a852012-08-17 15:18:29 -07003445 mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
3447
3448 boolean more = false;
3449 final long drawingTime = getDrawingTime();
3450
Chris Craik8afd0f22014-08-21 17:41:57 -07003451 if (usingRenderNodeProperties) canvas.insertReorderBarrier();
Chet Haasec633d2f2015-04-07 10:29:39 -07003452 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
3453 int transientIndex = transientCount != 0 ? 0 : -1;
Chris Craikab008f02014-05-23 17:55:03 -07003454 // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
3455 // draw reordering internally
Chris Craika753f4c2014-07-24 12:39:17 -07003456 final ArrayList<View> preorderedList = usingRenderNodeProperties
Chris Craikab008f02014-05-23 17:55:03 -07003457 ? null : buildOrderedChildList();
3458 final boolean customOrder = preorderedList == null
3459 && isChildrenDrawingOrderEnabled();
3460 for (int i = 0; i < childrenCount; i++) {
Chet Haasec633d2f2015-04-07 10:29:39 -07003461 while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
3462 final View transientChild = mTransientViews.get(transientIndex);
3463 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3464 transientChild.getAnimation() != null) {
3465 more |= drawChild(canvas, transientChild, drawingTime);
3466 }
3467 transientIndex++;
3468 if (transientIndex >= transientCount) {
3469 transientIndex = -1;
3470 }
3471 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05003472
3473 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3474 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Chris Craikab008f02014-05-23 17:55:03 -07003475 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
3476 more |= drawChild(canvas, child, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 }
3478 }
Chet Haasec633d2f2015-04-07 10:29:39 -07003479 while (transientIndex >= 0) {
3480 // there may be additional transient views after the normal views
3481 final View transientChild = mTransientViews.get(transientIndex);
3482 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3483 transientChild.getAnimation() != null) {
3484 more |= drawChild(canvas, transientChild, drawingTime);
3485 }
3486 transientIndex++;
3487 if (transientIndex >= transientCount) {
3488 break;
3489 }
3490 }
Chris Craikab008f02014-05-23 17:55:03 -07003491 if (preorderedList != null) preorderedList.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492
3493 // Draw any disappearing views that have animations
3494 if (mDisappearingChildren != null) {
3495 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3496 final int disappearingCount = disappearingChildren.size() - 1;
3497 // Go backwards -- we may delete as animations finish
3498 for (int i = disappearingCount; i >= 0; i--) {
3499 final View child = disappearingChildren.get(i);
3500 more |= drawChild(canvas, child, drawingTime);
3501 }
3502 }
Chris Craik8afd0f22014-08-21 17:41:57 -07003503 if (usingRenderNodeProperties) canvas.insertInorderBarrier();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504
Philip Milne10ca24a2012-04-23 15:38:27 -07003505 if (debugDraw()) {
3506 onDebugDraw(canvas);
3507 }
3508
Chris Craike4cf1522014-08-04 17:55:22 -07003509 if (clipToPadding) {
Selim Cinek19cadc22014-04-16 17:27:19 +02003510 canvas.restoreToCount(clipSaveCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
3512
3513 // mGroupFlags might have been updated by drawChild()
3514 flags = mGroupFlags;
3515
3516 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08003517 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
3519
3520 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
3521 mLayoutAnimationController.isDone() && !more) {
3522 // We want to erase the drawing cache and notify the listener after the
3523 // next frame is drawn because one extra invalidate() is caused by
3524 // drawChild() after the animation is over
3525 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
3526 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05003527 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 public void run() {
3529 notifyAnimationListener();
3530 }
3531 };
3532 post(end);
3533 }
3534 }
Romain Guy8506ab42009-06-11 17:35:47 -07003535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003537 * Returns the ViewGroupOverlay for this view group, creating it if it does
3538 * not yet exist. In addition to {@link ViewOverlay}'s support for drawables,
3539 * {@link ViewGroupOverlay} allows views to be added to the overlay. These
3540 * views, like overlay drawables, are visual-only; they do not receive input
3541 * events and should not be used as anything other than a temporary
3542 * representation of a view in a parent container, such as might be used
3543 * by an animation effect.
3544 *
Chet Haase95399492013-04-08 14:30:31 -07003545 * <p>Note: Overlays do not currently work correctly with {@link
3546 * SurfaceView} or {@link TextureView}; contents in overlays for these
3547 * types of views may not display correctly.</p>
3548 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003549 * @return The ViewGroupOverlay object for this view.
3550 * @see ViewGroupOverlay
3551 */
3552 @Override
3553 public ViewGroupOverlay getOverlay() {
3554 if (mOverlay == null) {
3555 mOverlay = new ViewGroupOverlay(mContext, this);
3556 }
3557 return (ViewGroupOverlay) mOverlay;
3558 }
3559
3560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 * Returns the index of the child to draw for this iteration. Override this
3562 * if you want to change the drawing order of children. By default, it
3563 * returns i.
3564 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08003565 * NOTE: In order for this method to be called, you must enable child ordering
3566 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07003567 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 * @param i The current iteration.
3569 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07003570 *
Romain Guy293451e2009-11-04 13:59:48 -08003571 * @see #setChildrenDrawingOrderEnabled(boolean)
3572 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 */
3574 protected int getChildDrawingOrder(int childCount, int i) {
3575 return i;
3576 }
Romain Guy8506ab42009-06-11 17:35:47 -07003577
Chris Craikab008f02014-05-23 17:55:03 -07003578 private boolean hasChildWithZ() {
3579 for (int i = 0; i < mChildrenCount; i++) {
3580 if (mChildren[i].getZ() != 0) return true;
3581 }
3582 return false;
3583 }
3584
3585 /**
3586 * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,
Chris Craik57c79c82014-09-30 12:54:31 -07003587 * sorted first by Z, then by child drawing order (if applicable). This list must be cleared
3588 * after use to avoid leaking child Views.
Chris Craikab008f02014-05-23 17:55:03 -07003589 *
3590 * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated
3591 * children.
3592 */
George Mount81206522014-09-26 21:53:39 -07003593 ArrayList<View> buildOrderedChildList() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003594 final int childrenCount = mChildrenCount;
3595 if (childrenCount <= 1 || !hasChildWithZ()) return null;
Chris Craikab008f02014-05-23 17:55:03 -07003596
3597 if (mPreSortedChildren == null) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003598 mPreSortedChildren = new ArrayList<>(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07003599 } else {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003600 mPreSortedChildren.ensureCapacity(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07003601 }
3602
Alan Viverettea7b85e62016-01-22 10:14:02 -05003603 final boolean customOrder = isChildrenDrawingOrderEnabled();
3604 for (int i = 0; i < childrenCount; i++) {
Chris Craikab008f02014-05-23 17:55:03 -07003605 // add next child (in child order) to end of list
Alan Viverettea7b85e62016-01-22 10:14:02 -05003606 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3607 final View nextChild = mChildren[childIndex];
3608 final float currentZ = nextChild.getZ();
Chris Craikab008f02014-05-23 17:55:03 -07003609
3610 // insert ahead of any Views with greater Z
3611 int insertIndex = i;
3612 while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {
3613 insertIndex--;
3614 }
3615 mPreSortedChildren.add(insertIndex, nextChild);
3616 }
3617 return mPreSortedChildren;
3618 }
3619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 private void notifyAnimationListener() {
3621 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
3622 mGroupFlags |= FLAG_ANIMATION_DONE;
3623
3624 if (mAnimationListener != null) {
3625 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05003626 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 public void run() {
3628 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
3629 }
3630 };
3631 post(end);
3632 }
3633
Romain Guy849d0a32011-02-01 17:20:48 -08003634 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 }
3636
3637 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08003638 * This method is used to cause children of this ViewGroup to restore or recreate their
3639 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
3640 * to recreate its own display list, which would happen if it went through the normal
3641 * draw/dispatchDraw mechanisms.
3642 *
3643 * @hide
3644 */
3645 @Override
3646 protected void dispatchGetDisplayList() {
3647 final int count = mChildrenCount;
3648 final View[] children = mChildren;
3649 for (int i = 0; i < count; i++) {
3650 final View child = children[i];
John Reckc2330f52015-04-28 13:18:52 -07003651 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) {
Chet Haase6c0665f2014-08-01 13:32:27 -07003652 recreateChildDisplayList(child);
Romain Guy2f57ba52011-02-03 18:03:29 -08003653 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003654 }
Chet Haase91cedf12013-03-11 07:56:30 -07003655 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003656 View overlayView = mOverlay.getOverlayView();
Chet Haase6c0665f2014-08-01 13:32:27 -07003657 recreateChildDisplayList(overlayView);
Chet Haase91cedf12013-03-11 07:56:30 -07003658 }
Chet Haase6c0665f2014-08-01 13:32:27 -07003659 if (mDisappearingChildren != null) {
3660 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3661 final int disappearingCount = disappearingChildren.size();
3662 for (int i = 0; i < disappearingCount; ++i) {
3663 final View child = disappearingChildren.get(i);
3664 recreateChildDisplayList(child);
3665 }
3666 }
3667 }
3668
3669 private void recreateChildDisplayList(View child) {
Chris Craik31a2d062015-05-01 14:22:47 -07003670 child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0;
Chet Haase6c0665f2014-08-01 13:32:27 -07003671 child.mPrivateFlags &= ~PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -07003672 child.updateDisplayListIfDirty();
Chet Haase6c0665f2014-08-01 13:32:27 -07003673 child.mRecreateDisplayList = false;
Chet Haasedaf98e92011-01-10 14:10:36 -08003674 }
3675
3676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 * Draw one child of this View Group. This method is responsible for getting
3678 * the canvas in the right state. This includes clipping, translating so
3679 * that the child's scrolled origin is at 0, 0, and applying any animation
3680 * transformations.
3681 *
3682 * @param canvas The canvas on which to draw the child
3683 * @param child Who to draw
Chet Haasebcca79a2012-02-14 08:45:14 -08003684 * @param drawingTime The time at which draw is occurring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 * @return True if an invalidate() was issued
3686 */
3687 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Chet Haase64a48c12012-02-13 16:33:29 -08003688 return child.draw(canvas, this, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 }
3690
Alan Viverette922e1c62015-05-05 17:18:27 -07003691 @Override
3692 void getScrollIndicatorBounds(@NonNull Rect out) {
3693 super.getScrollIndicatorBounds(out);
3694
3695 // If we have padding and we're supposed to clip children to that
3696 // padding, offset the scroll indicators to match our clip bounds.
3697 final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3698 if (clipToPadding) {
3699 out.left += mPaddingLeft;
3700 out.right -= mPaddingRight;
3701 out.top += mPaddingTop;
3702 out.bottom -= mPaddingBottom;
3703 }
3704 }
3705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 /**
Chris Craikd863a102013-12-19 13:31:15 -08003707 * Returns whether this group's children are clipped to their bounds before drawing.
Chet Haase430742f2013-04-12 11:18:36 -07003708 * The default value is true.
3709 * @see #setClipChildren(boolean)
3710 *
3711 * @return True if the group's children will be clipped to their bounds,
3712 * false otherwise.
3713 */
Chris Craik5c75c522014-09-05 14:08:08 -07003714 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haase430742f2013-04-12 11:18:36 -07003715 public boolean getClipChildren() {
3716 return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0);
3717 }
3718
3719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 * By default, children are clipped to their bounds before drawing. This
3721 * allows view groups to override this behavior for animations, etc.
3722 *
3723 * @param clipChildren true to clip children to their bounds,
3724 * false otherwise
3725 * @attr ref android.R.styleable#ViewGroup_clipChildren
3726 */
3727 public void setClipChildren(boolean clipChildren) {
Chet Haasea1cff502012-02-21 13:43:44 -08003728 boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
3729 if (clipChildren != previousValue) {
3730 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
Chet Haase1271e2c2012-04-20 09:54:27 -07003731 for (int i = 0; i < mChildrenCount; ++i) {
3732 View child = getChildAt(i);
Chris Craik64a12e12014-03-28 18:12:12 -07003733 if (child.mRenderNode != null) {
3734 child.mRenderNode.setClipToBounds(clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -08003735 }
3736 }
John Reckaae9f3b2014-07-28 09:30:36 -07003737 invalidate(true);
Chet Haasea1cff502012-02-21 13:43:44 -08003738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
3740
3741 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003742 * Sets whether this ViewGroup will clip its children to its padding and resize (but not
3743 * clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003744 * <p>
3745 * By default, children are clipped to the padding of their parent
Doris Liub134b5e2015-05-28 17:26:46 -07003746 * ViewGroup. This clipping behavior is only enabled if padding is non-zero.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 *
Doris Liub134b5e2015-05-28 17:26:46 -07003748 * @param clipToPadding true to clip children to the padding of the group, and resize (but
3749 * not clip) any EdgeEffect to the padded region. False otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3751 */
3752 public void setClipToPadding(boolean clipToPadding) {
John Reck9fa3a242014-06-27 15:57:19 -07003753 if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) {
3754 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
John Reckaae9f3b2014-07-28 09:30:36 -07003755 invalidate(true);
John Reck9fa3a242014-06-27 15:57:19 -07003756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 }
3758
3759 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003760 * Returns whether this ViewGroup will clip its children to its padding, and resize (but
3761 * not clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003762 * <p>
3763 * By default, children are clipped to the padding of their parent
3764 * Viewgroup. This clipping behavior is only enabled if padding is non-zero.
Adam Powell1c35b082014-07-11 15:37:15 -07003765 *
Doris Liub134b5e2015-05-28 17:26:46 -07003766 * @return true if this ViewGroup clips children to its padding and resizes (but doesn't
3767 * clip) any EdgeEffect to the padded region, false otherwise.
Adam Powell1c35b082014-07-11 15:37:15 -07003768 *
3769 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3770 */
Chris Craik5c75c522014-09-05 14:08:08 -07003771 @ViewDebug.ExportedProperty(category = "drawing")
Adam Powell1c35b082014-07-11 15:37:15 -07003772 public boolean getClipToPadding() {
3773 return hasBooleanFlag(FLAG_CLIP_TO_PADDING);
3774 }
3775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 @Override
3777 public void dispatchSetSelected(boolean selected) {
3778 final View[] children = mChildren;
3779 final int count = mChildrenCount;
3780 for (int i = 0; i < count; i++) {
3781 children[i].setSelected(selected);
3782 }
3783 }
Romain Guy8506ab42009-06-11 17:35:47 -07003784
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003785 @Override
3786 public void dispatchSetActivated(boolean activated) {
3787 final View[] children = mChildren;
3788 final int count = mChildrenCount;
3789 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003790 children[i].setActivated(activated);
3791 }
3792 }
3793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 @Override
3795 protected void dispatchSetPressed(boolean pressed) {
3796 final View[] children = mChildren;
3797 final int count = mChildrenCount;
3798 for (int i = 0; i < count; i++) {
Adam Powell035a1fc2012-02-27 15:23:50 -08003799 final View child = children[i];
3800 // Children that are clickable on their own should not
3801 // show a pressed state when their parent view does.
3802 // Clearing a pressed state always propagates.
3803 if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
3804 child.setPressed(pressed);
3805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807 }
3808
Alan Viveretteb942b6f2014-12-08 10:37:39 -08003809 /**
3810 * Dispatches drawable hotspot changes to child views that meet at least
3811 * one of the following criteria:
3812 * <ul>
3813 * <li>Returns {@code false} from both {@link View#isClickable()} and
3814 * {@link View#isLongClickable()}</li>
3815 * <li>Requests duplication of parent state via
3816 * {@link View#setDuplicateParentStateEnabled(boolean)}</li>
3817 * </ul>
3818 *
3819 * @param x hotspot x coordinate
3820 * @param y hotspot y coordinate
3821 * @see #drawableHotspotChanged(float, float)
3822 */
3823 @Override
3824 public void dispatchDrawableHotspotChanged(float x, float y) {
3825 final int count = mChildrenCount;
3826 if (count == 0) {
3827 return;
3828 }
3829
3830 final View[] children = mChildren;
3831 for (int i = 0; i < count; i++) {
3832 final View child = children[i];
3833 // Children that are clickable on their own should not
3834 // receive hotspots when their parent view does.
3835 final boolean nonActionable = !child.isClickable() && !child.isLongClickable();
3836 final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0;
3837 if (nonActionable || duplicatesState) {
3838 final float[] point = getTempPoint();
3839 point[0] = x;
3840 point[1] = y;
3841 transformPointToViewLocal(point, child);
3842 child.drawableHotspotChanged(point[0], point[1]);
3843 }
3844 }
3845 }
3846
Adam Powell14874662013-07-18 19:42:41 -07003847 @Override
3848 void dispatchCancelPendingInputEvents() {
3849 super.dispatchCancelPendingInputEvents();
3850
3851 final View[] children = mChildren;
3852 final int count = mChildrenCount;
3853 for (int i = 0; i < count; i++) {
3854 children[i].dispatchCancelPendingInputEvents();
3855 }
3856 }
3857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 /**
3859 * When this property is set to true, this ViewGroup supports static transformations on
3860 * children; this causes
3861 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3862 * invoked when a child is drawn.
3863 *
3864 * Any subclass overriding
3865 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3866 * set this property to true.
3867 *
3868 * @param enabled True to enable static transformations on children, false otherwise.
3869 *
Chet Haase599913d2012-07-23 16:22:05 -07003870 * @see #getChildStaticTransformation(View, android.view.animation.Transformation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 */
3872 protected void setStaticTransformationsEnabled(boolean enabled) {
3873 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3874 }
3875
3876 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08003877 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
3878 * boolean to indicate whether a static transform was set. The default implementation
3879 * simply returns <code>false</code>; subclasses may override this method for different
Chet Haase599913d2012-07-23 16:22:05 -07003880 * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true
3881 * for this method to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08003883 * @param child The child view whose static transform is being requested
3884 * @param t The Transformation which will hold the result
3885 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07003886 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 */
3888 protected boolean getChildStaticTransformation(View child, Transformation t) {
3889 return false;
3890 }
3891
Romain Guyf6991302013-06-05 17:19:01 -07003892 Transformation getChildTransformation() {
3893 if (mChildTransformation == null) {
3894 mChildTransformation = new Transformation();
3895 }
3896 return mChildTransformation;
3897 }
3898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 /**
3900 * {@hide}
3901 */
3902 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003903 protected View findViewTraversal(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 if (id == mID) {
3905 return this;
3906 }
3907
3908 final View[] where = mChildren;
3909 final int len = mChildrenCount;
3910
3911 for (int i = 0; i < len; i++) {
3912 View v = where[i];
3913
Dianne Hackborn4702a852012-08-17 15:18:29 -07003914 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 v = v.findViewById(id);
3916
3917 if (v != null) {
3918 return v;
3919 }
3920 }
3921 }
3922
3923 return null;
3924 }
3925
3926 /**
3927 * {@hide}
3928 */
3929 @Override
3930 protected View findViewWithTagTraversal(Object tag) {
3931 if (tag != null && tag.equals(mTag)) {
3932 return this;
3933 }
3934
3935 final View[] where = mChildren;
3936 final int len = mChildrenCount;
3937
3938 for (int i = 0; i < len; i++) {
3939 View v = where[i];
3940
Dianne Hackborn4702a852012-08-17 15:18:29 -07003941 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 v = v.findViewWithTag(tag);
3943
3944 if (v != null) {
3945 return v;
3946 }
3947 }
3948 }
3949
3950 return null;
3951 }
3952
3953 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003954 * {@hide}
3955 */
3956 @Override
Jeff Brown4dfbec22011-08-15 14:55:37 -07003957 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003958 if (predicate.apply(this)) {
3959 return this;
3960 }
3961
3962 final View[] where = mChildren;
3963 final int len = mChildrenCount;
3964
3965 for (int i = 0; i < len; i++) {
3966 View v = where[i];
3967
Dianne Hackborn4702a852012-08-17 15:18:29 -07003968 if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003969 v = v.findViewByPredicate(predicate);
3970
3971 if (v != null) {
3972 return v;
3973 }
3974 }
3975 }
3976
3977 return null;
3978 }
3979
3980 /**
Chet Haasec633d2f2015-04-07 10:29:39 -07003981 * This method adds a view to this container at the specified index purely for the
3982 * purposes of allowing that view to draw even though it is not a normal child of
3983 * the container. That is, the view does not participate in layout, focus, accessibility,
3984 * input, or other normal view operations; it is purely an item to be drawn during the normal
3985 * rendering operation of this container. The index that it is added at is the order
3986 * in which it will be drawn, with respect to the other views in the container.
3987 * For example, a transient view added at index 0 will be drawn before all other views
3988 * in the container because it will be drawn first (including before any real view
3989 * at index 0). There can be more than one transient view at any particular index;
3990 * these views will be drawn in the order in which they were added to the list of
3991 * transient views. The index of transient views can also be greater than the number
3992 * of normal views in the container; that just means that they will be drawn after all
3993 * other views are drawn.
3994 *
3995 * <p>Note that since transient views do not participate in layout, they must be sized
3996 * manually or, more typically, they should just use the size that they had before they
3997 * were removed from their container.</p>
3998 *
3999 * <p>Transient views are useful for handling animations of views that have been removed
4000 * from the container, but which should be animated out after the removal. Adding these
4001 * views as transient views allows them to participate in drawing without side-effecting
4002 * the layout of the container.</p>
4003 *
4004 * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed}
4005 * from the container when they are no longer needed. For example, a transient view
4006 * which is added in order to fade it out in its old location should be removed
4007 * once the animation is complete.</p>
4008 *
4009 * @param view The view to be added
4010 * @param index The index at which this view should be drawn, must be >= 0.
4011 * This value is relative to the {@link #getChildAt(int) index} values in the normal
4012 * child list of this container, where any transient view at a particular index will
4013 * be drawn before any normal child at that same index.
Chris Craik66b41392015-04-17 10:08:10 -07004014 *
4015 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004016 */
4017 public void addTransientView(View view, int index) {
4018 if (index < 0) {
4019 return;
4020 }
4021 if (mTransientIndices == null) {
4022 mTransientIndices = new ArrayList<Integer>();
4023 mTransientViews = new ArrayList<View>();
4024 }
4025 final int oldSize = mTransientIndices.size();
4026 if (oldSize > 0) {
4027 int insertionIndex;
4028 for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) {
4029 if (index < mTransientIndices.get(insertionIndex)) {
4030 break;
4031 }
4032 }
4033 mTransientIndices.add(insertionIndex, index);
4034 mTransientViews.add(insertionIndex, view);
4035 } else {
4036 mTransientIndices.add(index);
4037 mTransientViews.add(view);
4038 }
4039 view.mParent = this;
4040 view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4041 invalidate(true);
4042 }
4043
4044 /**
4045 * Removes a view from the list of transient views in this container. If there is no
4046 * such transient view, this method does nothing.
4047 *
4048 * @param view The transient view to be removed
Chris Craik66b41392015-04-17 10:08:10 -07004049 *
4050 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004051 */
4052 public void removeTransientView(View view) {
4053 if (mTransientViews == null) {
4054 return;
4055 }
4056 final int size = mTransientViews.size();
4057 for (int i = 0; i < size; ++i) {
4058 if (view == mTransientViews.get(i)) {
4059 mTransientViews.remove(i);
4060 mTransientIndices.remove(i);
4061 view.mParent = null;
4062 view.dispatchDetachedFromWindow();
4063 invalidate(true);
4064 return;
4065 }
4066 }
4067 }
4068
4069 /**
4070 * Returns the number of transient views in this container. Specific transient
4071 * views and the index at which they were added can be retrieved via
4072 * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}.
4073 *
4074 * @see #addTransientView(View, int)
4075 * @return The number of transient views in this container
Chris Craik66b41392015-04-17 10:08:10 -07004076 *
4077 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004078 */
4079 public int getTransientViewCount() {
4080 return mTransientIndices == null ? 0 : mTransientIndices.size();
4081 }
4082
4083 /**
4084 * Given a valid position within the list of transient views, returns the index of
4085 * the transient view at that position.
4086 *
4087 * @param position The position of the index being queried. Must be at least 0
4088 * and less than the value returned by {@link #getTransientViewCount()}.
4089 * @return The index of the transient view stored in the given position if the
4090 * position is valid, otherwise -1
Chris Craik66b41392015-04-17 10:08:10 -07004091 *
4092 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004093 */
4094 public int getTransientViewIndex(int position) {
4095 if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) {
4096 return -1;
4097 }
4098 return mTransientIndices.get(position);
4099 }
4100
4101 /**
4102 * Given a valid position within the list of transient views, returns the
4103 * transient view at that position.
4104 *
4105 * @param position The position of the view being queried. Must be at least 0
4106 * and less than the value returned by {@link #getTransientViewCount()}.
4107 * @return The transient view stored in the given position if the
4108 * position is valid, otherwise null
Chris Craik66b41392015-04-17 10:08:10 -07004109 *
4110 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004111 */
4112 public View getTransientView(int position) {
4113 if (mTransientViews == null || position >= mTransientViews.size()) {
4114 return null;
4115 }
4116 return mTransientViews.get(position);
4117 }
4118
4119 /**
Romain Guy393a52c2012-05-22 20:21:08 -07004120 * <p>Adds a child view. If no layout parameters are already set on the child, the
4121 * default parameters for this ViewGroup are set on the child.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004122 *
Romain Guy393a52c2012-05-22 20:21:08 -07004123 * <p><strong>Note:</strong> do not invoke this method from
4124 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4125 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 *
4127 * @param child the child view to add
4128 *
4129 * @see #generateDefaultLayoutParams()
4130 */
4131 public void addView(View child) {
4132 addView(child, -1);
4133 }
4134
4135 /**
4136 * Adds a child view. If no layout parameters are already set on the child, the
4137 * default parameters for this ViewGroup are set on the child.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004138 *
Romain Guy393a52c2012-05-22 20:21:08 -07004139 * <p><strong>Note:</strong> do not invoke this method from
4140 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4141 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 *
4143 * @param child the child view to add
4144 * @param index the position at which to add the child
4145 *
4146 * @see #generateDefaultLayoutParams()
4147 */
4148 public void addView(View child, int index) {
Adam Powell45a9da52014-10-09 09:44:18 -07004149 if (child == null) {
4150 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 LayoutParams params = child.getLayoutParams();
4153 if (params == null) {
4154 params = generateDefaultLayoutParams();
4155 if (params == null) {
4156 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
4157 }
4158 }
4159 addView(child, index, params);
4160 }
4161
4162 /**
4163 * Adds a child view with this ViewGroup's default layout parameters and the
4164 * specified width and height.
4165 *
Romain Guy393a52c2012-05-22 20:21:08 -07004166 * <p><strong>Note:</strong> do not invoke this method from
4167 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4168 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 * @param child the child view to add
4171 */
4172 public void addView(View child, int width, int height) {
4173 final LayoutParams params = generateDefaultLayoutParams();
4174 params.width = width;
4175 params.height = height;
4176 addView(child, -1, params);
4177 }
4178
4179 /**
4180 * Adds a child view with the specified layout parameters.
4181 *
Romain Guy393a52c2012-05-22 20:21:08 -07004182 * <p><strong>Note:</strong> do not invoke this method from
4183 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4184 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 * @param child the child view to add
4187 * @param params the layout parameters to set on the child
4188 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 public void addView(View child, LayoutParams params) {
4191 addView(child, -1, params);
4192 }
4193
4194 /**
4195 * Adds a child view with the specified layout parameters.
4196 *
Romain Guy393a52c2012-05-22 20:21:08 -07004197 * <p><strong>Note:</strong> do not invoke this method from
4198 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4199 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 * @param child the child view to add
Alan Viverette77bb6f12015-02-11 17:24:33 -08004202 * @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 -08004203 * @param params the layout parameters to set on the child
4204 */
4205 public void addView(View child, int index, LayoutParams params) {
4206 if (DBG) {
4207 System.out.println(this + " addView");
4208 }
4209
Adam Powell45a9da52014-10-09 09:44:18 -07004210 if (child == null) {
4211 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4212 }
4213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
4215 // therefore, we call requestLayout() on ourselves before, so that the child's request
4216 // will be blocked at our level
4217 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004218 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 addViewInner(child, index, params, false);
4220 }
4221
Alan Viverettebe463f22016-01-21 10:50:10 -05004222 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
4224 if (!checkLayoutParams(params)) {
4225 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
4226 }
4227 if (view.mParent != this) {
4228 throw new IllegalArgumentException("Given view not a child of " + this);
4229 }
4230 view.setLayoutParams(params);
4231 }
4232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
4234 return p != null;
4235 }
4236
4237 /**
4238 * Interface definition for a callback to be invoked when the hierarchy
4239 * within this view changed. The hierarchy changes whenever a child is added
4240 * to or removed from this view.
4241 */
4242 public interface OnHierarchyChangeListener {
4243 /**
4244 * Called when a new child is added to a parent view.
4245 *
4246 * @param parent the view in which a child was added
4247 * @param child the new child view added in the hierarchy
4248 */
4249 void onChildViewAdded(View parent, View child);
4250
4251 /**
4252 * Called when a child is removed from a parent view.
4253 *
4254 * @param parent the view from which the child was removed
4255 * @param child the child removed from the hierarchy
4256 */
4257 void onChildViewRemoved(View parent, View child);
4258 }
4259
4260 /**
4261 * Register a callback to be invoked when a child is added to or removed
4262 * from this view.
4263 *
4264 * @param listener the callback to invoke on hierarchy change
4265 */
4266 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
4267 mOnHierarchyChangeListener = listener;
4268 }
4269
Adam Powell6690d012015-06-17 16:41:56 -07004270 void dispatchViewAdded(View child) {
4271 onViewAdded(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004272 if (mOnHierarchyChangeListener != null) {
4273 mOnHierarchyChangeListener.onChildViewAdded(this, child);
4274 }
4275 }
4276
4277 /**
Adam Powell6690d012015-06-17 16:41:56 -07004278 * Called when a new child is added to this ViewGroup. Overrides should always
4279 * call super.onViewAdded.
4280 *
4281 * @param child the added child view
Philip Milnef51d91c2011-07-18 16:12:19 -07004282 */
Adam Powell6690d012015-06-17 16:41:56 -07004283 public void onViewAdded(View child) {
4284 }
4285
4286 void dispatchViewRemoved(View child) {
4287 onViewRemoved(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004288 if (mOnHierarchyChangeListener != null) {
4289 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
4290 }
4291 }
4292
Adam Powell6690d012015-06-17 16:41:56 -07004293 /**
4294 * Called when a child view is removed from this ViewGroup. Overrides should always
4295 * call super.onViewRemoved.
4296 *
4297 * @param child the removed child view
4298 */
4299 public void onViewRemoved(View child) {
4300 }
4301
Philip Milnecfb631b2012-10-26 10:51:46 -07004302 private void clearCachedLayoutMode() {
Svetoslav6254f482013-06-04 17:22:14 -07004303 if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnecfb631b2012-10-26 10:51:46 -07004304 mLayoutMode = LAYOUT_MODE_UNDEFINED;
4305 }
4306 }
4307
4308 @Override
4309 protected void onAttachedToWindow() {
4310 super.onAttachedToWindow();
4311 clearCachedLayoutMode();
4312 }
4313
4314 @Override
4315 protected void onDetachedFromWindow() {
4316 super.onDetachedFromWindow();
4317 clearCachedLayoutMode();
4318 }
4319
Philip Milnef51d91c2011-07-18 16:12:19 -07004320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 * Adds a view during layout. This is useful if in your onLayout() method,
4322 * you need to add more views (as does the list view for example).
4323 *
4324 * If index is negative, it means put it at the end of the list.
4325 *
4326 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004327 * @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 -08004328 * @param params the layout parameters to associate with the child
4329 * @return true if the child was added, false otherwise
4330 */
4331 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
4332 return addViewInLayout(child, index, params, false);
4333 }
4334
4335 /**
4336 * Adds a view during layout. This is useful if in your onLayout() method,
4337 * you need to add more views (as does the list view for example).
4338 *
4339 * If index is negative, it means put it at the end of the list.
4340 *
4341 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004342 * @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 -08004343 * @param params the layout parameters to associate with the child
4344 * @param preventRequestLayout if true, calling this method will not trigger a
4345 * layout request on child
4346 * @return true if the child was added, false otherwise
4347 */
4348 protected boolean addViewInLayout(View child, int index, LayoutParams params,
4349 boolean preventRequestLayout) {
Adam Powell45a9da52014-10-09 09:44:18 -07004350 if (child == null) {
4351 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 child.mParent = null;
4354 addViewInner(child, index, params, preventRequestLayout);
Dianne Hackborn4702a852012-08-17 15:18:29 -07004355 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 return true;
4357 }
4358
4359 /**
4360 * Prevents the specified child to be laid out during the next layout pass.
4361 *
4362 * @param child the child on which to perform the cleanup
4363 */
4364 protected void cleanupLayoutState(View child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004365 child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
4367
4368 private void addViewInner(View child, int index, LayoutParams params,
4369 boolean preventRequestLayout) {
4370
Chet Haasee8e45d32011-03-02 17:07:35 -08004371 if (mTransition != null) {
4372 // Don't prevent other add transitions from completing, but cancel remove
4373 // transitions to let them complete the process before we add to the container
4374 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08004375 }
4376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 if (child.getParent() != null) {
4378 throw new IllegalStateException("The specified child already has a parent. " +
4379 "You must call removeView() on the child's parent first.");
4380 }
4381
Chet Haase21cd1382010-09-01 17:42:29 -07004382 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004383 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004384 }
4385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 if (!checkLayoutParams(params)) {
4387 params = generateLayoutParams(params);
4388 }
4389
4390 if (preventRequestLayout) {
4391 child.mLayoutParams = params;
4392 } else {
4393 child.setLayoutParams(params);
4394 }
4395
4396 if (index < 0) {
4397 index = mChildrenCount;
4398 }
4399
4400 addInArray(child, index);
4401
4402 // tell our children
4403 if (preventRequestLayout) {
4404 child.assignParent(this);
4405 } else {
4406 child.mParent = this;
4407 }
4408
4409 if (child.hasFocus()) {
4410 requestChildFocus(child, child.findFocus());
4411 }
Romain Guy8506ab42009-06-11 17:35:47 -07004412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07004414 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07004415 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 ai.mKeepScreenOn = false;
4417 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4418 if (ai.mKeepScreenOn) {
4419 needGlobalAttributesUpdate(true);
4420 }
4421 ai.mKeepScreenOn = lastKeepOn;
4422 }
4423
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004424 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglioa7e0bcd2012-10-16 19:55:01 -07004425 child.resetRtlProperties();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004426 }
4427
Adam Powell6690d012015-06-17 16:41:56 -07004428 dispatchViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429
4430 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
4431 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
4432 }
Adam Powell539ee872012-02-03 19:00:49 -08004433
4434 if (child.hasTransientState()) {
4435 childHasTransientStateChanged(child, true);
4436 }
Svetoslav6254f482013-06-04 17:22:14 -07004437
Svetoslav8e3feb12014-02-24 13:46:47 -08004438 if (child.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004439 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004440 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004441
4442 if (mTransientIndices != null) {
4443 final int transientCount = mTransientIndices.size();
4444 for (int i = 0; i < transientCount; ++i) {
4445 final int oldIndex = mTransientIndices.get(i);
4446 if (index <= oldIndex) {
4447 mTransientIndices.set(i, oldIndex + 1);
4448 }
4449 }
4450 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004451
4452 if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) {
4453 notifyChildOfDragStart(child);
4454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 }
4456
4457 private void addInArray(View child, int index) {
4458 View[] children = mChildren;
4459 final int count = mChildrenCount;
4460 final int size = children.length;
4461 if (index == count) {
4462 if (size == count) {
4463 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4464 System.arraycopy(children, 0, mChildren, 0, size);
4465 children = mChildren;
4466 }
4467 children[mChildrenCount++] = child;
4468 } else if (index < count) {
4469 if (size == count) {
4470 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4471 System.arraycopy(children, 0, mChildren, 0, index);
4472 System.arraycopy(children, index, mChildren, index + 1, count - index);
4473 children = mChildren;
4474 } else {
4475 System.arraycopy(children, index, children, index + 1, count - index);
4476 }
4477 children[index] = child;
4478 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08004479 if (mLastTouchDownIndex >= index) {
4480 mLastTouchDownIndex++;
4481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 } else {
4483 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
4484 }
4485 }
4486
4487 // This method also sets the child's mParent to null
4488 private void removeFromArray(int index) {
4489 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07004490 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
4491 children[index].mParent = null;
4492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 final int count = mChildrenCount;
4494 if (index == count - 1) {
4495 children[--mChildrenCount] = null;
4496 } else if (index >= 0 && index < count) {
4497 System.arraycopy(children, index + 1, children, index, count - index - 1);
4498 children[--mChildrenCount] = null;
4499 } else {
4500 throw new IndexOutOfBoundsException();
4501 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08004502 if (mLastTouchDownIndex == index) {
4503 mLastTouchDownTime = 0;
4504 mLastTouchDownIndex = -1;
4505 } else if (mLastTouchDownIndex > index) {
4506 mLastTouchDownIndex--;
4507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 }
4509
4510 // This method also sets the children's mParent to null
4511 private void removeFromArray(int start, int count) {
4512 final View[] children = mChildren;
4513 final int childrenCount = mChildrenCount;
4514
4515 start = Math.max(0, start);
4516 final int end = Math.min(childrenCount, start + count);
4517
4518 if (start == end) {
4519 return;
4520 }
4521
4522 if (end == childrenCount) {
4523 for (int i = start; i < end; i++) {
4524 children[i].mParent = null;
4525 children[i] = null;
4526 }
4527 } else {
4528 for (int i = start; i < end; i++) {
4529 children[i].mParent = null;
4530 }
4531
4532 // Since we're looping above, we might as well do the copy, but is arraycopy()
4533 // faster than the extra 2 bounds checks we would do in the loop?
4534 System.arraycopy(children, end, children, start, childrenCount - end);
4535
4536 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
4537 children[i] = null;
4538 }
4539 }
4540
4541 mChildrenCount -= (end - start);
4542 }
4543
4544 private void bindLayoutAnimation(View child) {
4545 Animation a = mLayoutAnimationController.getAnimationForView(child);
4546 child.setAnimation(a);
4547 }
4548
4549 /**
4550 * Subclasses should override this method to set layout animation
4551 * parameters on the supplied child.
4552 *
4553 * @param child the child to associate with animation parameters
4554 * @param params the child's layout parameters which hold the animation
4555 * parameters
4556 * @param index the index of the child in the view group
4557 * @param count the number of children in the view group
4558 */
4559 protected void attachLayoutAnimationParameters(View child,
4560 LayoutParams params, int index, int count) {
4561 LayoutAnimationController.AnimationParameters animationParams =
4562 params.layoutAnimationParameters;
4563 if (animationParams == null) {
4564 animationParams = new LayoutAnimationController.AnimationParameters();
4565 params.layoutAnimationParameters = animationParams;
4566 }
4567
4568 animationParams.count = count;
4569 animationParams.index = index;
4570 }
4571
4572 /**
4573 * {@inheritDoc}
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004574 *
Romain Guy393a52c2012-05-22 20:21:08 -07004575 * <p><strong>Note:</strong> do not invoke this method from
4576 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4577 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004579 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 public void removeView(View view) {
Alan Viverette177ec4602014-10-17 13:34:50 -07004581 if (removeViewInternal(view)) {
4582 requestLayout();
4583 invalidate(true);
4584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 }
4586
4587 /**
4588 * Removes a view during layout. This is useful if in your onLayout() method,
4589 * you need to remove more views.
4590 *
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>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004594 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 * @param view the view to remove from the group
4596 */
4597 public void removeViewInLayout(View view) {
4598 removeViewInternal(view);
4599 }
4600
4601 /**
4602 * Removes a range of views during layout. This is useful if in your onLayout() method,
4603 * you need to remove more views.
4604 *
Romain Guy393a52c2012-05-22 20:21:08 -07004605 * <p><strong>Note:</strong> do not invoke this method from
4606 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4607 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 * @param start the index of the first view to remove from the group
4610 * @param count the number of views to remove from the group
4611 */
4612 public void removeViewsInLayout(int start, int count) {
4613 removeViewsInternal(start, count);
4614 }
4615
4616 /**
4617 * Removes the view at the specified position in the group.
4618 *
Romain Guy393a52c2012-05-22 20:21:08 -07004619 * <p><strong>Note:</strong> do not invoke this method from
4620 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4621 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004622 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 * @param index the position in the group of the view to remove
4624 */
4625 public void removeViewAt(int index) {
4626 removeViewInternal(index, getChildAt(index));
4627 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004628 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 }
4630
4631 /**
4632 * Removes the specified range of views from the group.
4633 *
Romain Guy393a52c2012-05-22 20:21:08 -07004634 * <p><strong>Note:</strong> do not invoke this method from
4635 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4636 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 * @param start the first position in the group of the range of views to remove
4639 * @param count the number of views to remove
4640 */
4641 public void removeViews(int start, int count) {
4642 removeViewsInternal(start, count);
4643 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004644 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 }
4646
Alan Viverette177ec4602014-10-17 13:34:50 -07004647 private boolean removeViewInternal(View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 final int index = indexOfChild(view);
4649 if (index >= 0) {
4650 removeViewInternal(index, view);
Alan Viverette177ec4602014-10-17 13:34:50 -07004651 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 }
Alan Viverette177ec4602014-10-17 13:34:50 -07004653 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 }
4655
4656 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07004657 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004658 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004659 }
4660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 boolean clearChildFocus = false;
4662 if (view == mFocused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004663 view.unFocus(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 clearChildFocus = true;
4665 }
4666
Alan Viverette632af842014-10-28 13:45:11 -07004667 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004668
Jeff Brown59a422e2012-04-19 15:19:19 -07004669 cancelTouchTarget(view);
4670 cancelHoverTarget(view);
4671
Chet Haase21cd1382010-09-01 17:42:29 -07004672 if (view.getAnimation() != null ||
4673 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 addDisappearingView(view);
4675 } else if (view.mAttachInfo != null) {
4676 view.dispatchDetachedFromWindow();
4677 }
4678
Adam Powell539ee872012-02-03 19:00:49 -08004679 if (view.hasTransientState()) {
4680 childHasTransientStateChanged(view, false);
4681 }
4682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 removeFromArray(index);
4686
4687 if (clearChildFocus) {
4688 clearChildFocus(view);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004689 if (!rootViewRequestFocus()) {
4690 notifyGlobalFocusCleared(this);
4691 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004692 }
Romain Guy6fb05632012-11-29 10:50:33 -08004693
Adam Powell6690d012015-06-17 16:41:56 -07004694 dispatchViewRemoved(view);
Svetoslav6254f482013-06-04 17:22:14 -07004695
Svetoslav8e3feb12014-02-24 13:46:47 -08004696 if (view.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004697 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004698 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004699
4700 int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
4701 for (int i = 0; i < transientCount; ++i) {
4702 final int oldIndex = mTransientIndices.get(i);
4703 if (index < oldIndex) {
4704 mTransientIndices.set(i, oldIndex - 1);
4705 }
4706 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004707
4708 if (mCurrentDragStartEvent != null) {
4709 mChildrenInterestedInDrag.remove(view);
4710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 }
4712
Chet Haase21cd1382010-09-01 17:42:29 -07004713 /**
4714 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4715 * not null, changes in layout which occur because of children being added to or removed from
4716 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4717 * object. By default, the transition object is null (so layout changes are not animated).
4718 *
Chet Haaseef3cbfd2013-08-21 14:01:02 -07004719 * <p>Replacing a non-null transition will cause that previous transition to be
4720 * canceled, if it is currently running, to restore this container to
4721 * its correct post-transition state.</p>
4722 *
Chet Haase21cd1382010-09-01 17:42:29 -07004723 * @param transition The LayoutTransition object that will animated changes in layout. A value
4724 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07004725 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07004726 */
4727 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004728 if (mTransition != null) {
Chet Haasefee6f2b2013-08-27 12:22:29 -07004729 LayoutTransition previousTransition = mTransition;
4730 previousTransition.cancel();
4731 previousTransition.removeTransitionListener(mLayoutTransitionListener);
Chet Haaseb20db3e2010-09-10 13:07:30 -07004732 }
Chet Haase21cd1382010-09-01 17:42:29 -07004733 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07004734 if (mTransition != null) {
4735 mTransition.addTransitionListener(mLayoutTransitionListener);
4736 }
4737 }
4738
4739 /**
4740 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4741 * not null, changes in layout which occur because of children being added to or removed from
4742 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4743 * object. By default, the transition object is null (so layout changes are not animated).
4744 *
4745 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
4746 * A value of <code>null</code> means no transition will run on layout changes.
4747 */
4748 public LayoutTransition getLayoutTransition() {
4749 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07004750 }
4751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 private void removeViewsInternal(int start, int count) {
Chris Craik18a759d2015-10-14 14:16:33 -07004753 final int end = start + count;
4754
4755 if (start < 0 || count < 0 || end > mChildrenCount) {
4756 throw new IndexOutOfBoundsException();
4757 }
4758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 final View focused = mFocused;
4760 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004761 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762
4763 final View[] children = mChildren;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764
4765 for (int i = start; i < end; i++) {
4766 final View view = children[i];
4767
Chet Haase21cd1382010-09-01 17:42:29 -07004768 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004769 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004770 }
4771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004773 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004774 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 }
4776
Alan Viverette632af842014-10-28 13:45:11 -07004777 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004778
Jeff Brown59a422e2012-04-19 15:19:19 -07004779 cancelTouchTarget(view);
4780 cancelHoverTarget(view);
4781
Chet Haase21cd1382010-09-01 17:42:29 -07004782 if (view.getAnimation() != null ||
4783 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 addDisappearingView(view);
4785 } else if (detach) {
4786 view.dispatchDetachedFromWindow();
4787 }
4788
Adam Powell539ee872012-02-03 19:00:49 -08004789 if (view.hasTransientState()) {
4790 childHasTransientStateChanged(view, false);
4791 }
4792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004794
Adam Powell6690d012015-06-17 16:41:56 -07004795 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 }
4797
4798 removeFromArray(start, count);
4799
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004800 if (clearChildFocus) {
4801 clearChildFocus(focused);
4802 if (!rootViewRequestFocus()) {
4803 notifyGlobalFocusCleared(focused);
4804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 }
4806 }
4807
4808 /**
4809 * Call this method to remove all child views from the
4810 * ViewGroup.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004811 *
Romain Guy393a52c2012-05-22 20:21:08 -07004812 * <p><strong>Note:</strong> do not invoke this method from
4813 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4814 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 */
4816 public void removeAllViews() {
4817 removeAllViewsInLayout();
4818 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004819 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 }
4821
4822 /**
4823 * Called by a ViewGroup subclass to remove child views from itself,
4824 * when it must first know its size on screen before it can calculate how many
4825 * child views it will render. An example is a Gallery or a ListView, which
4826 * may "have" 50 children, but actually only render the number of children
4827 * that can currently fit inside the object on screen. Do not call
4828 * this method unless you are extending ViewGroup and understand the
4829 * view measuring and layout pipeline.
Romain Guy393a52c2012-05-22 20:21:08 -07004830 *
4831 * <p><strong>Note:</strong> do not invoke this method from
4832 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4833 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 */
4835 public void removeAllViewsInLayout() {
4836 final int count = mChildrenCount;
4837 if (count <= 0) {
4838 return;
4839 }
4840
4841 final View[] children = mChildren;
4842 mChildrenCount = 0;
4843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 final View focused = mFocused;
4845 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004846 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847
4848 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 for (int i = count - 1; i >= 0; i--) {
4851 final View view = children[i];
4852
Chet Haase21cd1382010-09-01 17:42:29 -07004853 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004854 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004855 }
4856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004858 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004859 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 }
4861
Alan Viverette632af842014-10-28 13:45:11 -07004862 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004863
Jeff Brown59a422e2012-04-19 15:19:19 -07004864 cancelTouchTarget(view);
4865 cancelHoverTarget(view);
4866
Chet Haase21cd1382010-09-01 17:42:29 -07004867 if (view.getAnimation() != null ||
4868 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 addDisappearingView(view);
4870 } else if (detach) {
4871 view.dispatchDetachedFromWindow();
4872 }
4873
Adam Powell539ee872012-02-03 19:00:49 -08004874 if (view.hasTransientState()) {
4875 childHasTransientStateChanged(view, false);
4876 }
4877
Adam Powell6690d012015-06-17 16:41:56 -07004878 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879
4880 view.mParent = null;
4881 children[i] = null;
4882 }
4883
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004884 if (clearChildFocus) {
4885 clearChildFocus(focused);
4886 if (!rootViewRequestFocus()) {
4887 notifyGlobalFocusCleared(focused);
4888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 }
4890 }
4891
4892 /**
4893 * Finishes the removal of a detached view. This method will dispatch the detached from
4894 * window event and notify the hierarchy change listener.
Chet Haaseca479d42012-08-30 17:20:08 -07004895 * <p>
4896 * This method is intended to be lightweight and makes no assumptions about whether the
4897 * parent or child should be redrawn. Proper use of this method will include also making
4898 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4899 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4900 * which performs a {@link #requestLayout()} on the next frame, after all detach/remove
4901 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 *
4903 * @param child the child to be definitely removed from the view hierarchy
4904 * @param animate if true and the view has an animation, the view is placed in the
4905 * disappearing views list, otherwise, it is detached from the window
4906 *
4907 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4908 * @see #detachAllViewsFromParent()
4909 * @see #detachViewFromParent(View)
4910 * @see #detachViewFromParent(int)
4911 */
4912 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07004913 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004914 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004915 }
4916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 if (child == mFocused) {
4918 child.clearFocus();
4919 }
Romain Guy8506ab42009-06-11 17:35:47 -07004920
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004921 child.clearAccessibilityFocus();
4922
Jeff Brown59a422e2012-04-19 15:19:19 -07004923 cancelTouchTarget(child);
4924 cancelHoverTarget(child);
4925
Chet Haase21cd1382010-09-01 17:42:29 -07004926 if ((animate && child.getAnimation() != null) ||
4927 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 addDisappearingView(child);
4929 } else if (child.mAttachInfo != null) {
4930 child.dispatchDetachedFromWindow();
4931 }
4932
Adam Powell539ee872012-02-03 19:00:49 -08004933 if (child.hasTransientState()) {
4934 childHasTransientStateChanged(child, false);
4935 }
4936
Adam Powell6690d012015-06-17 16:41:56 -07004937 dispatchViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938 }
4939
4940 /**
4941 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
Chet Haaseca479d42012-08-30 17:20:08 -07004942 * sets the layout parameters and puts the view in the list of children so that
4943 * it can be retrieved by calling {@link #getChildAt(int)}.
4944 * <p>
4945 * This method is intended to be lightweight and makes no assumptions about whether the
4946 * parent or child should be redrawn. Proper use of this method will include also making
4947 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4948 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4949 * which performs a {@link #requestLayout()} on the next frame, after all detach/attach
4950 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
4951 * <p>
4952 * This method should be called only for views which were detached from their parent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 *
4954 * @param child the child to attach
4955 * @param index the index at which the child should be attached
4956 * @param params the layout parameters of the child
4957 *
4958 * @see #removeDetachedView(View, boolean)
4959 * @see #detachAllViewsFromParent()
4960 * @see #detachViewFromParent(View)
4961 * @see #detachViewFromParent(int)
4962 */
4963 protected void attachViewToParent(View child, int index, LayoutParams params) {
4964 child.mLayoutParams = params;
4965
4966 if (index < 0) {
4967 index = mChildrenCount;
4968 }
4969
4970 addInArray(child, index);
4971
4972 child.mParent = this;
Dianne Hackborn4702a852012-08-17 15:18:29 -07004973 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK
4974 & ~PFLAG_DRAWING_CACHE_VALID)
4975 | PFLAG_DRAWN | PFLAG_INVALIDATED;
4976 this.mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977
4978 if (child.hasFocus()) {
4979 requestChildFocus(child, child.findFocus());
4980 }
4981 }
4982
4983 /**
Chet Haaseca479d42012-08-30 17:20:08 -07004984 * Detaches a view from its parent. Detaching a view should be followed
4985 * either by a call to
4986 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
4987 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
4988 * temporary; reattachment or removal should happen within the same drawing cycle as
4989 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
4990 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 *
4992 * @param child the child to detach
4993 *
4994 * @see #detachViewFromParent(int)
4995 * @see #detachViewsFromParent(int, int)
4996 * @see #detachAllViewsFromParent()
4997 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4998 * @see #removeDetachedView(View, boolean)
4999 */
5000 protected void detachViewFromParent(View child) {
5001 removeFromArray(indexOfChild(child));
5002 }
5003
5004 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005005 * Detaches a view from its parent. Detaching a view should be followed
5006 * either by a call to
5007 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5008 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5009 * temporary; reattachment or removal should happen within the same drawing cycle as
5010 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5011 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 *
5013 * @param index the index of the child to detach
5014 *
5015 * @see #detachViewFromParent(View)
5016 * @see #detachAllViewsFromParent()
5017 * @see #detachViewsFromParent(int, int)
5018 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5019 * @see #removeDetachedView(View, boolean)
5020 */
5021 protected void detachViewFromParent(int index) {
5022 removeFromArray(index);
5023 }
5024
5025 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005026 * Detaches a range of views from their parents. Detaching a view should be followed
5027 * either by a call to
5028 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5029 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5030 * temporary; reattachment or removal should happen within the same drawing cycle as
5031 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5032 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 *
5034 * @param start the first index of the childrend range to detach
5035 * @param count the number of children to detach
5036 *
5037 * @see #detachViewFromParent(View)
5038 * @see #detachViewFromParent(int)
5039 * @see #detachAllViewsFromParent()
5040 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5041 * @see #removeDetachedView(View, boolean)
5042 */
5043 protected void detachViewsFromParent(int start, int count) {
5044 removeFromArray(start, count);
5045 }
5046
5047 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005048 * Detaches all views from the parent. Detaching a view should be followed
5049 * either by a call to
5050 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5051 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5052 * temporary; reattachment or removal should happen within the same drawing cycle as
5053 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5054 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 *
5056 * @see #detachViewFromParent(View)
5057 * @see #detachViewFromParent(int)
5058 * @see #detachViewsFromParent(int, int)
5059 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5060 * @see #removeDetachedView(View, boolean)
5061 */
5062 protected void detachAllViewsFromParent() {
5063 final int count = mChildrenCount;
5064 if (count <= 0) {
5065 return;
5066 }
5067
5068 final View[] children = mChildren;
5069 mChildrenCount = 0;
5070
5071 for (int i = count - 1; i >= 0; i--) {
5072 children[i].mParent = null;
5073 children[i] = null;
5074 }
5075 }
5076
5077 /**
5078 * Don't call or override this method. It is used for the implementation of
5079 * the view hierarchy.
5080 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005081 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 public final void invalidateChild(View child, final Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 ViewParent parent = this;
5084
5085 final AttachInfo attachInfo = mAttachInfo;
5086 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 // If the child is drawing an animation, we want to copy this flag onto
5088 // ourselves and the parent to make sure the invalidate request goes
5089 // through
Dianne Hackborn4702a852012-08-17 15:18:29 -07005090 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION)
5091 == PFLAG_DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07005092
Romain Guyfe455af2012-02-15 16:40:20 -08005093 // Check whether the child that requests the invalidate is fully opaque
5094 // Views being animated or transformed are not considered opaque because we may
5095 // be invalidating their old position and need the parent to paint behind them.
5096 Matrix childMatrix = child.getMatrix();
5097 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
5098 child.getAnimation() == null && childMatrix.isIdentity();
5099 // Mark the child as dirty, using the appropriate flag
5100 // Make sure we do not set both flags at the same time
Dianne Hackborn4702a852012-08-17 15:18:29 -07005101 int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005102
John Reck96bb8ad2014-06-19 10:53:03 -07005103 if (child.mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005104 mPrivateFlags |= PFLAG_INVALIDATED;
5105 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guyfe455af2012-02-15 16:40:20 -08005106 }
5107
5108 final int[] location = attachInfo.mInvalidateChildLocation;
5109 location[CHILD_LEFT_INDEX] = child.mLeft;
5110 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase599913d2012-07-23 16:22:05 -07005111 if (!childMatrix.isIdentity() ||
5112 (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Romain Guyfe455af2012-02-15 16:40:20 -08005113 RectF boundingRect = attachInfo.mTmpTransformRect;
5114 boundingRect.set(dirty);
Chet Haase599913d2012-07-23 16:22:05 -07005115 Matrix transformMatrix;
5116 if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
5117 Transformation t = attachInfo.mTmpTransformation;
5118 boolean transformed = getChildStaticTransformation(child, t);
5119 if (transformed) {
5120 transformMatrix = attachInfo.mTmpMatrix;
5121 transformMatrix.set(t.getMatrix());
5122 if (!childMatrix.isIdentity()) {
5123 transformMatrix.preConcat(childMatrix);
5124 }
5125 } else {
5126 transformMatrix = childMatrix;
5127 }
5128 } else {
5129 transformMatrix = childMatrix;
5130 }
5131 transformMatrix.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005132 dirty.set((int) Math.floor(boundingRect.left),
5133 (int) Math.floor(boundingRect.top),
5134 (int) Math.ceil(boundingRect.right),
5135 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005136 }
5137
5138 do {
5139 View view = null;
5140 if (parent instanceof View) {
5141 view = (View) parent;
Romain Guyfe455af2012-02-15 16:40:20 -08005142 }
5143
5144 if (drawAnimation) {
5145 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005146 view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Romain Guyfe455af2012-02-15 16:40:20 -08005147 } else if (parent instanceof ViewRootImpl) {
5148 ((ViewRootImpl) parent).mIsAnimating = true;
5149 }
5150 }
5151
5152 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
5153 // flag coming from the child that initiated the invalidate
5154 if (view != null) {
5155 if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
5156 view.getSolidColor() == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005157 opaqueFlag = PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005158 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005159 if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) {
5160 view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag;
Romain Guyfe455af2012-02-15 16:40:20 -08005161 }
5162 }
5163
5164 parent = parent.invalidateChildInParent(location, dirty);
5165 if (view != null) {
5166 // Account for transform on current parent
5167 Matrix m = view.getMatrix();
5168 if (!m.isIdentity()) {
5169 RectF boundingRect = attachInfo.mTmpTransformRect;
5170 boundingRect.set(dirty);
5171 m.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005172 dirty.set((int) Math.floor(boundingRect.left),
5173 (int) Math.floor(boundingRect.top),
5174 (int) Math.ceil(boundingRect.right),
5175 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005176 }
5177 }
5178 } while (parent != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 }
5180 }
5181
5182 /**
5183 * Don't call or override this method. It is used for the implementation of
5184 * the view hierarchy.
5185 *
5186 * This implementation returns null if this ViewGroup does not have a parent,
5187 * if this ViewGroup is already fully invalidated or if the dirty rectangle
5188 * does not intersect with this ViewGroup's bounds.
5189 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005192 if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
5193 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
5195 FLAG_OPTIMIZE_INVALIDATE) {
5196 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
5197 location[CHILD_TOP_INDEX] - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005198 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5199 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201
5202 final int left = mLeft;
5203 final int top = mTop;
5204
Chet Haase05e91ed2012-07-03 14:17:57 -07005205 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5206 if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) {
5207 dirty.setEmpty();
Romain Guy3a3133d2011-02-01 22:59:58 -08005208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005210 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haase05e91ed2012-07-03 14:17:57 -07005211
5212 location[CHILD_LEFT_INDEX] = left;
5213 location[CHILD_TOP_INDEX] = top;
5214
John Reck96bb8ad2014-06-19 10:53:03 -07005215 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005216 mPrivateFlags |= PFLAG_INVALIDATED;
Chet Haase05e91ed2012-07-03 14:17:57 -07005217 }
5218
5219 return mParent;
5220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005222 mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223
5224 location[CHILD_LEFT_INDEX] = mLeft;
5225 location[CHILD_TOP_INDEX] = mTop;
Chet Haasea3db8662011-07-19 10:36:05 -07005226 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5227 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
5228 } else {
5229 // in case the dirty rect extends outside the bounds of this container
5230 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5231 }
Romain Guy3a3133d2011-02-01 22:59:58 -08005232
John Reck96bb8ad2014-06-19 10:53:03 -07005233 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005234 mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy3a3133d2011-02-01 22:59:58 -08005235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236
5237 return mParent;
5238 }
5239 }
5240
5241 return null;
5242 }
5243
5244 /**
John Recke4267ea2014-06-03 15:53:15 -07005245 * Native-calculated damage path
5246 * Returns false if this path was unable to complete successfully. This means
5247 * it hit a ViewParent it doesn't recognize and needs to fall back to calculating
5248 * damage area
5249 * @hide
5250 */
5251 public boolean damageChildDeferred(View child) {
5252 ViewParent parent = getParent();
5253 while (parent != null) {
5254 if (parent instanceof ViewGroup) {
5255 parent = parent.getParent();
5256 } else if (parent instanceof ViewRootImpl) {
5257 ((ViewRootImpl) parent).invalidate();
5258 return true;
5259 } else {
5260 parent = null;
5261 }
5262 }
5263 return false;
5264 }
5265
5266 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07005267 * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the
5268 * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods
5269 * do; all we want to do here is schedule a traversal with the appropriate dirty rect.
5270 *
5271 * @hide
5272 */
Chris Craik49e6c732014-03-31 12:34:11 -07005273 public void damageChild(View child, final Rect dirty) {
John Recke4267ea2014-06-03 15:53:15 -07005274 if (damageChildDeferred(child)) {
5275 return;
5276 }
5277
Chet Haase9d1992d2012-03-13 11:03:25 -07005278 ViewParent parent = this;
5279
5280 final AttachInfo attachInfo = mAttachInfo;
5281 if (attachInfo != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005282 int left = child.mLeft;
5283 int top = child.mTop;
5284 if (!child.getMatrix().isIdentity()) {
5285 child.transformRect(dirty);
5286 }
5287
5288 do {
5289 if (parent instanceof ViewGroup) {
5290 ViewGroup parentVG = (ViewGroup) parent;
Chet Haaseb85967b2012-03-26 14:37:51 -07005291 if (parentVG.mLayerType != LAYER_TYPE_NONE) {
5292 // Layered parents should be recreated, not just re-issued
5293 parentVG.invalidate();
5294 parent = null;
5295 } else {
Chris Craik49e6c732014-03-31 12:34:11 -07005296 parent = parentVG.damageChildInParent(left, top, dirty);
Chet Haaseb85967b2012-03-26 14:37:51 -07005297 left = parentVG.mLeft;
5298 top = parentVG.mTop;
5299 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005300 } else {
5301 // Reached the top; this calls into the usual invalidate method in
5302 // ViewRootImpl, which schedules a traversal
5303 final int[] location = attachInfo.mInvalidateChildLocation;
5304 location[0] = left;
5305 location[1] = top;
5306 parent = parent.invalidateChildInParent(location, dirty);
5307 }
5308 } while (parent != null);
5309 }
5310 }
5311
5312 /**
5313 * Quick invalidation method that simply transforms the dirty rect into the parent's
5314 * coordinate system, pruning the invalidation if the parent has already been invalidated.
Chet Haasee4a2d7c2013-06-21 17:49:36 -07005315 *
5316 * @hide
Chet Haase9d1992d2012-03-13 11:03:25 -07005317 */
Chris Craik49e6c732014-03-31 12:34:11 -07005318 protected ViewParent damageChildInParent(int left, int top, final Rect dirty) {
5319 if ((mPrivateFlags & PFLAG_DRAWN) != 0
5320 || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005321 dirty.offset(left - mScrollX, top - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005322 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5323 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5324 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005325
5326 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 ||
5327 dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
5328
Chet Haase9d1992d2012-03-13 11:03:25 -07005329 if (!getMatrix().isIdentity()) {
5330 transformRect(dirty);
5331 }
5332
5333 return mParent;
5334 }
5335 }
5336
5337 return null;
5338 }
5339
5340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 * Offset a rectangle that is in a descendant's coordinate
5342 * space into our coordinate space.
5343 * @param descendant A descendant of this view
5344 * @param rect A rectangle defined in descendant's coordinate space.
5345 */
5346 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
5347 offsetRectBetweenParentAndChild(descendant, rect, true, false);
5348 }
5349
5350 /**
5351 * Offset a rectangle that is in our coordinate space into an ancestor's
5352 * coordinate space.
5353 * @param descendant A descendant of this view
5354 * @param rect A rectangle defined in descendant's coordinate space.
5355 */
5356 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
5357 offsetRectBetweenParentAndChild(descendant, rect, false, false);
5358 }
5359
5360 /**
5361 * Helper method that offsets a rect either from parent to descendant or
5362 * descendant to parent.
5363 */
5364 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
5365 boolean offsetFromChildToParent, boolean clipToBounds) {
5366
5367 // already in the same coord system :)
5368 if (descendant == this) {
5369 return;
5370 }
5371
5372 ViewParent theParent = descendant.mParent;
5373
5374 // search and offset up to the parent
5375 while ((theParent != null)
5376 && (theParent instanceof View)
5377 && (theParent != this)) {
5378
5379 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005380 rect.offset(descendant.mLeft - descendant.mScrollX,
5381 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 if (clipToBounds) {
5383 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005384 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5385 p.mBottom - p.mTop);
5386 if (!intersected) {
5387 rect.setEmpty();
5388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 }
5390 } else {
5391 if (clipToBounds) {
5392 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005393 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5394 p.mBottom - p.mTop);
5395 if (!intersected) {
5396 rect.setEmpty();
5397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 }
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005399 rect.offset(descendant.mScrollX - descendant.mLeft,
5400 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 }
5402
5403 descendant = (View) theParent;
5404 theParent = descendant.mParent;
5405 }
5406
5407 // now that we are up to this view, need to offset one more time
5408 // to get into our coordinate space
5409 if (theParent == this) {
5410 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005411 rect.offset(descendant.mLeft - descendant.mScrollX,
5412 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 } else {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005414 rect.offset(descendant.mScrollX - descendant.mLeft,
5415 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 }
5417 } else {
5418 throw new IllegalArgumentException("parameter must be a descendant of this view");
5419 }
5420 }
5421
5422 /**
5423 * Offset the vertical location of all children of this view by the specified number of pixels.
5424 *
5425 * @param offset the number of pixels to offset
5426 *
5427 * @hide
5428 */
5429 public void offsetChildrenTopAndBottom(int offset) {
5430 final int count = mChildrenCount;
5431 final View[] children = mChildren;
Romain Guy5549cb52013-05-06 18:42:08 -07005432 boolean invalidate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433
5434 for (int i = 0; i < count; i++) {
5435 final View v = children[i];
5436 v.mTop += offset;
5437 v.mBottom += offset;
Chris Craik64a12e12014-03-28 18:12:12 -07005438 if (v.mRenderNode != null) {
Romain Guy5549cb52013-05-06 18:42:08 -07005439 invalidate = true;
Chris Craik64a12e12014-03-28 18:12:12 -07005440 v.mRenderNode.offsetTopAndBottom(offset);
Chet Haasea1cff502012-02-21 13:43:44 -08005441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 }
Romain Guy5549cb52013-05-06 18:42:08 -07005443
5444 if (invalidate) {
5445 invalidateViewProperty(false, false);
5446 }
Guang Zhu84e25092014-05-01 21:12:55 -07005447 notifySubtreeAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 }
5449
Alan Viverettebe463f22016-01-21 10:50:10 -05005450 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005452 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
5453 // but for some simple tests it can be useful. If we don't have attach info this
5454 // will allocate memory.
5455 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01005456 rect.set(r);
5457
5458 if (!child.hasIdentityMatrix()) {
George Mount002d43d2014-11-11 12:54:43 -08005459 child.getMatrix().mapRect(rect);
Gilles Debunnecea45132011-11-24 02:19:27 +01005460 }
5461
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005462 final int dx = child.mLeft - mScrollX;
5463 final int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01005464
5465 rect.offset(dx, dy);
5466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01005468 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005469 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
5470 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01005471 position[0] = offset.x;
5472 position[1] = offset.y;
5473 child.getMatrix().mapPoints(position);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005474 offset.x = Math.round(position[0]);
5475 offset.y = Math.round(position[1]);
Gilles Debunnecea45132011-11-24 02:19:27 +01005476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 offset.x += dx;
5478 offset.y += dy;
5479 }
Gilles Debunnecea45132011-11-24 02:19:27 +01005480
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005481 final int width = mRight - mLeft;
5482 final int height = mBottom - mTop;
5483
George Mount002d43d2014-11-11 12:54:43 -08005484 boolean rectIsVisible = true;
Adam Powell35da41e2014-12-10 18:59:34 -08005485 if (mParent == null ||
5486 (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005487 // Clip to bounds.
5488 rectIsVisible = rect.intersect(0, 0, width, height);
Gilles Debunnecea45132011-11-24 02:19:27 +01005489 }
5490
George Mount002d43d2014-11-11 12:54:43 -08005491 if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005492 // Clip to padding.
George Mount002d43d2014-11-11 12:54:43 -08005493 rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop,
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005494 width - mPaddingRight, height - mPaddingBottom);
George Mount002d43d2014-11-11 12:54:43 -08005495 }
5496
5497 if (rectIsVisible && mClipBounds != null) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005498 // Clip to clipBounds.
George Mount002d43d2014-11-11 12:54:43 -08005499 rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right,
5500 mClipBounds.bottom);
5501 }
Alan Viverettec45b1d42015-11-16 15:38:59 -05005502 r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top),
5503 (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom));
George Mount002d43d2014-11-11 12:54:43 -08005504 if (rectIsVisible && mParent != null) {
5505 rectIsVisible = mParent.getChildVisibleRect(this, r, offset);
5506 }
5507 return rectIsVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 }
5509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 @Override
Chet Haase9c087442011-01-12 16:20:16 -08005511 public final void layout(int l, int t, int r, int b) {
Chet Haase430742f2013-04-12 11:18:36 -07005512 if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
Chet Haase7dd4a532012-04-16 13:35:09 -07005513 if (mTransition != null) {
5514 mTransition.layoutChange(this);
5515 }
Chet Haase9c087442011-01-12 16:20:16 -08005516 super.layout(l, t, r, b);
5517 } else {
5518 // record the fact that we noop'd it; request layout when transition finishes
Chet Haaseb9895022013-04-02 15:10:58 -07005519 mLayoutCalledWhileSuppressed = true;
Chet Haase9c087442011-01-12 16:20:16 -08005520 }
5521 }
5522
Chet Haase9c087442011-01-12 16:20:16 -08005523 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 protected abstract void onLayout(boolean changed,
5525 int l, int t, int r, int b);
5526
5527 /**
5528 * Indicates whether the view group has the ability to animate its children
5529 * after the first layout.
5530 *
5531 * @return true if the children can be animated, false otherwise
5532 */
5533 protected boolean canAnimate() {
5534 return mLayoutAnimationController != null;
5535 }
5536
5537 /**
5538 * Runs the layout animation. Calling this method triggers a relayout of
5539 * this view group.
5540 */
5541 public void startLayoutAnimation() {
5542 if (mLayoutAnimationController != null) {
5543 mGroupFlags |= FLAG_RUN_ANIMATION;
5544 requestLayout();
5545 }
5546 }
5547
5548 /**
5549 * Schedules the layout animation to be played after the next layout pass
5550 * of this view group. This can be used to restart the layout animation
5551 * when the content of the view group changes or when the activity is
5552 * paused and resumed.
5553 */
5554 public void scheduleLayoutAnimation() {
5555 mGroupFlags |= FLAG_RUN_ANIMATION;
5556 }
5557
5558 /**
5559 * Sets the layout animation controller used to animate the group's
5560 * children after the first layout.
5561 *
5562 * @param controller the animation controller
5563 */
5564 public void setLayoutAnimation(LayoutAnimationController controller) {
5565 mLayoutAnimationController = controller;
5566 if (mLayoutAnimationController != null) {
5567 mGroupFlags |= FLAG_RUN_ANIMATION;
5568 }
5569 }
5570
5571 /**
5572 * Returns the layout animation controller used to animate the group's
5573 * children.
5574 *
5575 * @return the current animation controller
5576 */
5577 public LayoutAnimationController getLayoutAnimation() {
5578 return mLayoutAnimationController;
5579 }
5580
5581 /**
5582 * Indicates whether the children's drawing cache is used during a layout
5583 * animation. By default, the drawing cache is enabled but this will prevent
5584 * nested layout animations from working. To nest animations, you must disable
5585 * the cache.
5586 *
5587 * @return true if the animation cache is enabled, false otherwise
5588 *
5589 * @see #setAnimationCacheEnabled(boolean)
5590 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005591 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005592 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005593 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 public boolean isAnimationCacheEnabled() {
5596 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
5597 }
5598
5599 /**
5600 * Enables or disables the children's drawing cache during a layout animation.
5601 * By default, the drawing cache is enabled but this will prevent nested
5602 * layout animations from working. To nest animations, you must disable the
5603 * cache.
5604 *
5605 * @param enabled true to enable the animation cache, false otherwise
5606 *
5607 * @see #isAnimationCacheEnabled()
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 */
5613 public void setAnimationCacheEnabled(boolean enabled) {
5614 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
5615 }
5616
5617 /**
5618 * Indicates whether this ViewGroup will always try to draw its children using their
5619 * drawing cache. By default this property is enabled.
5620 *
5621 * @return true if the animation cache is enabled, false otherwise
5622 *
5623 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5624 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5625 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005626 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005627 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005628 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 public boolean isAlwaysDrawnWithCacheEnabled() {
5631 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
5632 }
5633
5634 /**
5635 * Indicates whether this ViewGroup will always try to draw its children using their
5636 * drawing cache. This property can be set to true when the cache rendering is
5637 * slightly different from the children's normal rendering. Renderings can be different,
5638 * for instance, when the cache's quality is set to low.
5639 *
5640 * When this property is disabled, the ViewGroup will use the drawing cache of its
5641 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
5642 * when to start using the drawing cache and when to stop using it.
5643 *
5644 * @param always true to always draw with the drawing cache, false otherwise
5645 *
5646 * @see #isAlwaysDrawnWithCacheEnabled()
5647 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5648 * @see View#setDrawingCacheEnabled(boolean)
5649 * @see View#setDrawingCacheQuality(int)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005650 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005651 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005652 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 */
5654 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
5655 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
5656 }
5657
5658 /**
5659 * Indicates whether the ViewGroup is currently drawing its children using
5660 * their drawing cache.
5661 *
5662 * @return true if children should be drawn with their cache, false otherwise
5663 *
5664 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5665 * @see #setChildrenDrawnWithCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005666 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005667 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005668 * Child views may no longer be forced to cache their rendering state by their parents.
5669 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 protected boolean isChildrenDrawnWithCacheEnabled() {
5672 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
5673 }
5674
5675 /**
5676 * Tells the ViewGroup to draw its children using their drawing cache. This property
5677 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
5678 * will be used only if it has been enabled.
5679 *
5680 * Subclasses should call this method to start and stop using the drawing cache when
5681 * they perform performance sensitive operations, like scrolling or animating.
5682 *
5683 * @param enabled true if children should be drawn with their cache, false otherwise
5684 *
5685 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5686 * @see #isChildrenDrawnWithCacheEnabled()
Chris Craik5a6bbae2015-04-10 17:41:34 -07005687 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005688 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005689 * Child views may no longer be forced to cache their rendering state by their parents.
5690 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 */
5692 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
5693 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
5694 }
5695
Romain Guy293451e2009-11-04 13:59:48 -08005696 /**
5697 * Indicates whether the ViewGroup is drawing its children in the order defined by
5698 * {@link #getChildDrawingOrder(int, int)}.
5699 *
5700 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
5701 * false otherwise
5702 *
5703 * @see #setChildrenDrawingOrderEnabled(boolean)
5704 * @see #getChildDrawingOrder(int, int)
5705 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005706 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08005707 protected boolean isChildrenDrawingOrderEnabled() {
5708 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
5709 }
5710
5711 /**
5712 * Tells the ViewGroup whether to draw its children in the order defined by the method
5713 * {@link #getChildDrawingOrder(int, int)}.
Chris Craike83cbd42014-09-03 17:52:24 -07005714 * <p>
5715 * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)},
5716 * will override custom child ordering done via this method.
Romain Guy293451e2009-11-04 13:59:48 -08005717 *
5718 * @param enabled true if the order of the children when drawing is determined by
5719 * {@link #getChildDrawingOrder(int, int)}, false otherwise
5720 *
5721 * @see #isChildrenDrawingOrderEnabled()
5722 * @see #getChildDrawingOrder(int, int)
5723 */
5724 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
5725 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
5726 }
5727
Svetoslav6254f482013-06-04 17:22:14 -07005728 private boolean hasBooleanFlag(int flag) {
Philip Milnef091b662013-02-27 11:15:21 -08005729 return (mGroupFlags & flag) == flag;
5730 }
5731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 private void setBooleanFlag(int flag, boolean value) {
5733 if (value) {
5734 mGroupFlags |= flag;
5735 } else {
5736 mGroupFlags &= ~flag;
5737 }
5738 }
5739
5740 /**
5741 * Returns an integer indicating what types of drawing caches are kept in memory.
5742 *
5743 * @see #setPersistentDrawingCache(int)
5744 * @see #setAnimationCacheEnabled(boolean)
5745 *
5746 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
5747 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5748 * and {@link #PERSISTENT_ALL_CACHES}
5749 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005750 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07005752 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
5754 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
5755 })
5756 public int getPersistentDrawingCache() {
5757 return mPersistentDrawingCache;
5758 }
5759
5760 /**
5761 * Indicates what types of drawing caches should be kept in memory after
5762 * they have been created.
5763 *
5764 * @see #getPersistentDrawingCache()
5765 * @see #setAnimationCacheEnabled(boolean)
5766 *
5767 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
5768 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5769 * and {@link #PERSISTENT_ALL_CACHES}
5770 */
5771 public void setPersistentDrawingCache(int drawingCacheToKeep) {
5772 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
5773 }
5774
Philip Milnef091b662013-02-27 11:15:21 -08005775 private void setLayoutMode(int layoutMode, boolean explicitly) {
5776 mLayoutMode = layoutMode;
5777 setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly);
5778 }
5779
5780 /**
5781 * Recursively traverse the view hierarchy, resetting the layoutMode of any
5782 * descendants that had inherited a different layoutMode from a previous parent.
5783 * Recursion terminates when a descendant's mode is:
5784 * <ul>
5785 * <li>Undefined</li>
5786 * <li>The same as the root node's</li>
5787 * <li>A mode that had been explicitly set</li>
5788 * <ul/>
5789 * The first two clauses are optimizations.
5790 * @param layoutModeOfRoot
5791 */
5792 @Override
5793 void invalidateInheritedLayoutMode(int layoutModeOfRoot) {
5794 if (mLayoutMode == LAYOUT_MODE_UNDEFINED ||
5795 mLayoutMode == layoutModeOfRoot ||
Svetoslav6254f482013-06-04 17:22:14 -07005796 hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnef091b662013-02-27 11:15:21 -08005797 return;
5798 }
5799 setLayoutMode(LAYOUT_MODE_UNDEFINED, false);
5800
5801 // apply recursively
5802 for (int i = 0, N = getChildCount(); i < N; i++) {
5803 getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot);
5804 }
5805 }
5806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005808 * Returns the basis of alignment during layout operations on this ViewGroup:
Philip Milne7b757812012-09-19 18:13:44 -07005809 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milnecfb631b2012-10-26 10:51:46 -07005810 * <p>
5811 * If no layoutMode was explicitly set, either programmatically or in an XML resource,
5812 * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
5813 * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005814 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005815 * @return the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005816 *
5817 * @see #setLayoutMode(int)
5818 */
5819 public int getLayoutMode() {
Philip Milnecfb631b2012-10-26 10:51:46 -07005820 if (mLayoutMode == LAYOUT_MODE_UNDEFINED) {
Philip Milnef091b662013-02-27 11:15:21 -08005821 int inheritedLayoutMode = (mParent instanceof ViewGroup) ?
5822 ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT;
5823 setLayoutMode(inheritedLayoutMode, false);
Philip Milnecfb631b2012-10-26 10:51:46 -07005824 }
Philip Milne1557fd72012-04-04 23:41:34 -07005825 return mLayoutMode;
5826 }
5827
5828 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005829 * Sets the basis of alignment during the layout of this ViewGroup.
Philip Milne7b757812012-09-19 18:13:44 -07005830 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
5831 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005832 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005833 * @param layoutMode the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005834 *
5835 * @see #getLayoutMode()
Scott Main27a85082013-06-10 10:39:48 -07005836 * @attr ref android.R.styleable#ViewGroup_layoutMode
Philip Milne1557fd72012-04-04 23:41:34 -07005837 */
5838 public void setLayoutMode(int layoutMode) {
5839 if (mLayoutMode != layoutMode) {
Philip Milnef091b662013-02-27 11:15:21 -08005840 invalidateInheritedLayoutMode(layoutMode);
5841 setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED);
Philip Milne1557fd72012-04-04 23:41:34 -07005842 requestLayout();
5843 }
5844 }
5845
5846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 * Returns a new set of layout parameters based on the supplied attributes set.
5848 *
5849 * @param attrs the attributes to build the layout parameters from
5850 *
5851 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5852 * of its descendants
5853 */
5854 public LayoutParams generateLayoutParams(AttributeSet attrs) {
5855 return new LayoutParams(getContext(), attrs);
5856 }
5857
5858 /**
5859 * Returns a safe set of layout parameters based on the supplied layout params.
5860 * When a ViewGroup is passed a View whose layout params do not pass the test of
5861 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
5862 * is invoked. This method should return a new set of layout params suitable for
5863 * this ViewGroup, possibly by copying the appropriate attributes from the
5864 * specified set of layout params.
5865 *
5866 * @param p The layout parameters to convert into a suitable set of layout parameters
5867 * for this ViewGroup.
5868 *
5869 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5870 * of its descendants
5871 */
5872 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Chet Haase4610eef2015-12-03 07:38:11 -08005873 return p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 }
5875
5876 /**
5877 * Returns a set of default layout parameters. These parameters are requested
5878 * when the View passed to {@link #addView(View)} has no layout parameters
5879 * already set. If null is returned, an exception is thrown from addView.
5880 *
5881 * @return a set of default layout parameters or null
5882 */
5883 protected LayoutParams generateDefaultLayoutParams() {
5884 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
5885 }
5886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 @Override
5888 protected void debug(int depth) {
5889 super.debug(depth);
5890 String output;
5891
5892 if (mFocused != null) {
5893 output = debugIndent(depth);
5894 output += "mFocused";
5895 Log.d(VIEW_LOG_TAG, output);
5896 }
5897 if (mChildrenCount != 0) {
5898 output = debugIndent(depth);
5899 output += "{";
5900 Log.d(VIEW_LOG_TAG, output);
5901 }
5902 int count = mChildrenCount;
5903 for (int i = 0; i < count; i++) {
5904 View child = mChildren[i];
5905 child.debug(depth + 1);
5906 }
5907
5908 if (mChildrenCount != 0) {
5909 output = debugIndent(depth);
5910 output += "}";
5911 Log.d(VIEW_LOG_TAG, output);
5912 }
5913 }
5914
5915 /**
5916 * Returns the position in the group of the specified child view.
5917 *
5918 * @param child the view for which to get the position
5919 * @return a positive integer representing the position of the view in the
5920 * group, or -1 if the view does not exist in the group
5921 */
5922 public int indexOfChild(View child) {
5923 final int count = mChildrenCount;
5924 final View[] children = mChildren;
5925 for (int i = 0; i < count; i++) {
5926 if (children[i] == child) {
5927 return i;
5928 }
5929 }
5930 return -1;
5931 }
5932
5933 /**
5934 * Returns the number of children in the group.
5935 *
5936 * @return a positive integer representing the number of children in
5937 * the group
5938 */
5939 public int getChildCount() {
5940 return mChildrenCount;
5941 }
5942
5943 /**
5944 * Returns the view at the specified position in the group.
5945 *
5946 * @param index the position at which to get the view from
5947 * @return the view at the specified position or null if the position
5948 * does not exist within the group
5949 */
5950 public View getChildAt(int index) {
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005951 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 return null;
5953 }
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005954 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 }
5956
5957 /**
5958 * Ask all of the children of this view to measure themselves, taking into
5959 * account both the MeasureSpec requirements for this view and its padding.
5960 * We skip children that are in the GONE state The heavy lifting is done in
5961 * getChildMeasureSpec.
5962 *
5963 * @param widthMeasureSpec The width requirements for this view
5964 * @param heightMeasureSpec The height requirements for this view
5965 */
5966 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
5967 final int size = mChildrenCount;
5968 final View[] children = mChildren;
5969 for (int i = 0; i < size; ++i) {
5970 final View child = children[i];
5971 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
5972 measureChild(child, widthMeasureSpec, heightMeasureSpec);
5973 }
5974 }
5975 }
5976
5977 /**
5978 * Ask one of the children of this view to measure itself, taking into
5979 * account both the MeasureSpec requirements for this view and its padding.
5980 * The heavy lifting is done in getChildMeasureSpec.
5981 *
5982 * @param child The child to measure
5983 * @param parentWidthMeasureSpec The width requirements for this view
5984 * @param parentHeightMeasureSpec The height requirements for this view
5985 */
5986 protected void measureChild(View child, int parentWidthMeasureSpec,
5987 int parentHeightMeasureSpec) {
5988 final LayoutParams lp = child.getLayoutParams();
5989
5990 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
5991 mPaddingLeft + mPaddingRight, lp.width);
5992 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
5993 mPaddingTop + mPaddingBottom, lp.height);
5994
5995 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
5996 }
5997
5998 /**
5999 * Ask one of the children of this view to measure itself, taking into
6000 * account both the MeasureSpec requirements for this view and its padding
6001 * and margins. The child must have MarginLayoutParams The heavy lifting is
6002 * done in getChildMeasureSpec.
6003 *
6004 * @param child The child to measure
6005 * @param parentWidthMeasureSpec The width requirements for this view
6006 * @param widthUsed Extra space that has been used up by the parent
6007 * horizontally (possibly by other children of the parent)
6008 * @param parentHeightMeasureSpec The height requirements for this view
6009 * @param heightUsed Extra space that has been used up by the parent
6010 * vertically (possibly by other children of the parent)
6011 */
6012 protected void measureChildWithMargins(View child,
6013 int parentWidthMeasureSpec, int widthUsed,
6014 int parentHeightMeasureSpec, int heightUsed) {
6015 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
6016
6017 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6018 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
6019 + widthUsed, lp.width);
6020 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6021 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
6022 + heightUsed, lp.height);
6023
6024 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6025 }
6026
6027 /**
6028 * Does the hard part of measureChildren: figuring out the MeasureSpec to
6029 * pass to a particular child. This method figures out the right MeasureSpec
6030 * for one dimension (height or width) of one child view.
6031 *
6032 * The goal is to combine information from our MeasureSpec with the
6033 * LayoutParams of the child to get the best possible results. For example,
6034 * if the this view knows its size (because its MeasureSpec has a mode of
6035 * EXACTLY), and the child has indicated in its LayoutParams that it wants
6036 * to be the same size as the parent, the parent should ask the child to
6037 * layout given an exact size.
6038 *
6039 * @param spec The requirements for this view
6040 * @param padding The padding of this view for the current dimension and
6041 * margins, if applicable
6042 * @param childDimension How big the child wants to be in the current
6043 * dimension
6044 * @return a MeasureSpec integer for the child
6045 */
6046 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
6047 int specMode = MeasureSpec.getMode(spec);
6048 int specSize = MeasureSpec.getSize(spec);
6049
6050 int size = Math.max(0, specSize - padding);
6051
6052 int resultSize = 0;
6053 int resultMode = 0;
6054
6055 switch (specMode) {
6056 // Parent has imposed an exact size on us
6057 case MeasureSpec.EXACTLY:
6058 if (childDimension >= 0) {
6059 resultSize = childDimension;
6060 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006061 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 // Child wants to be our size. So be it.
6063 resultSize = size;
6064 resultMode = MeasureSpec.EXACTLY;
6065 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6066 // Child wants to determine its own size. It can't be
6067 // bigger than us.
6068 resultSize = size;
6069 resultMode = MeasureSpec.AT_MOST;
6070 }
6071 break;
6072
6073 // Parent has imposed a maximum size on us
6074 case MeasureSpec.AT_MOST:
6075 if (childDimension >= 0) {
6076 // Child wants a specific size... so be it
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, but our size is not fixed.
6081 // Constrain child to not be bigger than us.
6082 resultSize = size;
6083 resultMode = MeasureSpec.AT_MOST;
6084 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6085 // Child wants to determine its own size. It can't be
6086 // bigger than us.
6087 resultSize = size;
6088 resultMode = MeasureSpec.AT_MOST;
6089 }
6090 break;
6091
6092 // Parent asked to see how big we want to be
6093 case MeasureSpec.UNSPECIFIED:
6094 if (childDimension >= 0) {
6095 // Child wants a specific size... let him have it
6096 resultSize = childDimension;
6097 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006098 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 // Child wants to be our size... find out how big it should
6100 // be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006101 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 resultMode = MeasureSpec.UNSPECIFIED;
6103 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6104 // Child wants to determine its own size.... find out how
6105 // big it should be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006106 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 resultMode = MeasureSpec.UNSPECIFIED;
6108 }
6109 break;
6110 }
Tor Norbye67568642015-03-31 07:47:23 -07006111 //noinspection ResourceType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
6113 }
6114
6115
6116 /**
6117 * Removes any pending animations for views that have been removed. Call
6118 * this if you don't want animations for exiting views to stack up.
6119 */
6120 public void clearDisappearingChildren() {
John Reckca7a9da2014-03-05 16:29:07 -08006121 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6122 if (disappearingChildren != null) {
6123 final int count = disappearingChildren.size();
6124 for (int i = 0; i < count; i++) {
6125 final View view = disappearingChildren.get(i);
6126 if (view.mAttachInfo != null) {
6127 view.dispatchDetachedFromWindow();
6128 }
6129 view.clearAnimation();
6130 }
6131 disappearingChildren.clear();
Chet Haaseb85967b2012-03-26 14:37:51 -07006132 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 }
6134 }
6135
6136 /**
6137 * Add a view which is removed from mChildren but still needs animation
6138 *
6139 * @param v View to add
6140 */
6141 private void addDisappearingView(View v) {
6142 ArrayList<View> disappearingChildren = mDisappearingChildren;
6143
6144 if (disappearingChildren == null) {
6145 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
6146 }
6147
6148 disappearingChildren.add(v);
6149 }
6150
6151 /**
6152 * Cleanup a view when its animation is done. This may mean removing it from
6153 * the list of disappearing views.
6154 *
6155 * @param view The view whose animation has finished
6156 * @param animation The animation, cannot be null
6157 */
Chet Haase64a48c12012-02-13 16:33:29 -08006158 void finishAnimatingView(final View view, Animation animation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6160 if (disappearingChildren != null) {
6161 if (disappearingChildren.contains(view)) {
6162 disappearingChildren.remove(view);
6163
6164 if (view.mAttachInfo != null) {
6165 view.dispatchDetachedFromWindow();
6166 }
6167
6168 view.clearAnimation();
6169 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6170 }
6171 }
6172
6173 if (animation != null && !animation.getFillAfter()) {
6174 view.clearAnimation();
6175 }
6176
Dianne Hackborn4702a852012-08-17 15:18:29 -07006177 if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 view.onAnimationEnd();
6179 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
6180 // so we'd rather be safe than sorry
Dianne Hackborn4702a852012-08-17 15:18:29 -07006181 view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 // Draw one more frame after the animation is done
6183 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6184 }
6185 }
6186
Chet Haaseb20db3e2010-09-10 13:07:30 -07006187 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07006188 * Utility function called by View during invalidation to determine whether a view that
6189 * is invisible or gone should still be invalidated because it is being transitioned (and
6190 * therefore still needs to be drawn).
6191 */
6192 boolean isViewTransitioning(View view) {
6193 return (mTransitioningViews != null && mTransitioningViews.contains(view));
6194 }
6195
6196 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07006197 * This method tells the ViewGroup that the given View object, which should have this
6198 * ViewGroup as its parent,
6199 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
6200 * is removed from its parent. This allows animations, such as those used by
6201 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
6202 * the removal of views. A call to this method should always be accompanied by a later call
6203 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
6204 * so that the View finally gets removed.
6205 *
6206 * @param view The View object to be kept visible even if it gets removed from its parent.
6207 */
6208 public void startViewTransition(View view) {
6209 if (view.mParent == this) {
6210 if (mTransitioningViews == null) {
6211 mTransitioningViews = new ArrayList<View>();
6212 }
6213 mTransitioningViews.add(view);
6214 }
6215 }
6216
6217 /**
6218 * This method should always be called following an earlier call to
6219 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
6220 * and will no longer be displayed. Note that this method does not perform the functionality
6221 * of removing a view from its parent; it just discontinues the display of a View that
6222 * has previously been removed.
6223 *
6224 * @return view The View object that has been removed but is being kept around in the visible
6225 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
6226 */
6227 public void endViewTransition(View view) {
6228 if (mTransitioningViews != null) {
6229 mTransitioningViews.remove(view);
6230 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6231 if (disappearingChildren != null && disappearingChildren.contains(view)) {
6232 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006233 if (mVisibilityChangingChildren != null &&
6234 mVisibilityChangingChildren.contains(view)) {
6235 mVisibilityChangingChildren.remove(view);
6236 } else {
6237 if (view.mAttachInfo != null) {
6238 view.dispatchDetachedFromWindow();
6239 }
6240 if (view.mParent != null) {
6241 view.mParent = null;
6242 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07006243 }
Chet Haaseb85967b2012-03-26 14:37:51 -07006244 invalidate();
Chet Haaseb20db3e2010-09-10 13:07:30 -07006245 }
6246 }
6247 }
6248
Chet Haase21cd1382010-09-01 17:42:29 -07006249 private LayoutTransition.TransitionListener mLayoutTransitionListener =
6250 new LayoutTransition.TransitionListener() {
6251 @Override
6252 public void startTransition(LayoutTransition transition, ViewGroup container,
6253 View view, int transitionType) {
6254 // We only care about disappearing items, since we need special logic to keep
6255 // those items visible after they've been 'removed'
6256 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006257 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006258 }
6259 }
6260
6261 @Override
6262 public void endTransition(LayoutTransition transition, ViewGroup container,
6263 View view, int transitionType) {
Chet Haaseb9895022013-04-02 15:10:58 -07006264 if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) {
Chet Haase9c087442011-01-12 16:20:16 -08006265 requestLayout();
Chet Haaseb9895022013-04-02 15:10:58 -07006266 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08006267 }
Chet Haase21cd1382010-09-01 17:42:29 -07006268 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006269 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006270 }
6271 }
6272 };
6273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 /**
Chet Haaseb9895022013-04-02 15:10:58 -07006275 * Tells this ViewGroup to suppress all layout() calls until layout
6276 * suppression is disabled with a later call to suppressLayout(false).
6277 * When layout suppression is disabled, a requestLayout() call is sent
6278 * if layout() was attempted while layout was being suppressed.
6279 *
6280 * @hide
6281 */
6282 public void suppressLayout(boolean suppress) {
6283 mSuppressLayout = suppress;
6284 if (!suppress) {
6285 if (mLayoutCalledWhileSuppressed) {
6286 requestLayout();
6287 mLayoutCalledWhileSuppressed = false;
6288 }
6289 }
6290 }
6291
6292 /**
Chet Haase199acdf2013-07-24 18:40:55 -07006293 * Returns whether layout calls on this container are currently being
6294 * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}.
6295 *
6296 * @return true if layout calls are currently suppressed, false otherwise.
6297 *
6298 * @hide
6299 */
6300 public boolean isLayoutSuppressed() {
6301 return mSuppressLayout;
6302 }
6303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 @Override
6305 public boolean gatherTransparentRegion(Region region) {
6306 // If no transparent regions requested, we are always opaque.
Dianne Hackborn4702a852012-08-17 15:18:29 -07006307 final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 if (meOpaque && region == null) {
6309 // The caller doesn't care about the region, so stop now.
6310 return true;
6311 }
6312 super.gatherTransparentRegion(region);
6313 final View[] children = mChildren;
6314 final int count = mChildrenCount;
6315 boolean noneOfTheChildrenAreTransparent = true;
6316 for (int i = 0; i < count; i++) {
6317 final View child = children[i];
Mathias Agopiane3381152010-12-02 15:19:36 -08006318 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 if (!child.gatherTransparentRegion(region)) {
6320 noneOfTheChildrenAreTransparent = false;
6321 }
6322 }
6323 }
6324 return meOpaque || noneOfTheChildrenAreTransparent;
6325 }
6326
Alan Viverettebe463f22016-01-21 10:50:10 -05006327 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 public void requestTransparentRegion(View child) {
6329 if (child != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006330 child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 if (mParent != null) {
6332 mParent.requestTransparentRegion(this);
6333 }
6334 }
6335 }
Romain Guy8506ab42009-06-11 17:35:47 -07006336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 @Override
Adam Powell46e38fd2014-02-03 10:16:49 -08006338 public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
6339 insets = super.dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006340 if (!insets.isConsumed()) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006341 final int count = getChildCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 for (int i = 0; i < count; i++) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006343 insets = getChildAt(i).dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006344 if (insets.isConsumed()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 break;
6346 }
6347 }
6348 }
Adam Powell46e38fd2014-02-03 10:16:49 -08006349 return insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 }
6351
6352 /**
6353 * Returns the animation listener to which layout animation events are
6354 * sent.
6355 *
6356 * @return an {@link android.view.animation.Animation.AnimationListener}
6357 */
6358 public Animation.AnimationListener getLayoutAnimationListener() {
6359 return mAnimationListener;
6360 }
6361
6362 @Override
6363 protected void drawableStateChanged() {
6364 super.drawableStateChanged();
6365
6366 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
6367 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6368 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
6369 + " child has duplicateParentState set to true");
6370 }
6371
6372 final View[] children = mChildren;
6373 final int count = mChildrenCount;
6374
6375 for (int i = 0; i < count; i++) {
6376 final View child = children[i];
6377 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
6378 child.refreshDrawableState();
6379 }
6380 }
6381 }
6382 }
6383
6384 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07006385 public void jumpDrawablesToCurrentState() {
6386 super.jumpDrawablesToCurrentState();
6387 final View[] children = mChildren;
6388 final int count = mChildrenCount;
6389 for (int i = 0; i < count; i++) {
6390 children[i].jumpDrawablesToCurrentState();
6391 }
6392 }
6393
6394 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 protected int[] onCreateDrawableState(int extraSpace) {
6396 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
6397 return super.onCreateDrawableState(extraSpace);
6398 }
6399
6400 int need = 0;
6401 int n = getChildCount();
6402 for (int i = 0; i < n; i++) {
6403 int[] childState = getChildAt(i).getDrawableState();
6404
6405 if (childState != null) {
6406 need += childState.length;
6407 }
6408 }
6409
6410 int[] state = super.onCreateDrawableState(extraSpace + need);
6411
6412 for (int i = 0; i < n; i++) {
6413 int[] childState = getChildAt(i).getDrawableState();
6414
6415 if (childState != null) {
6416 state = mergeDrawableStates(state, childState);
6417 }
6418 }
6419
6420 return state;
6421 }
6422
6423 /**
6424 * Sets whether this ViewGroup's drawable states also include
6425 * its children's drawable states. This is used, for example, to
6426 * make a group appear to be focused when its child EditText or button
6427 * is focused.
6428 */
6429 public void setAddStatesFromChildren(boolean addsStates) {
6430 if (addsStates) {
6431 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
6432 } else {
6433 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
6434 }
6435
6436 refreshDrawableState();
6437 }
6438
6439 /**
6440 * Returns whether this ViewGroup's drawable states also include
6441 * its children's drawable states. This is used, for example, to
6442 * make a group appear to be focused when its child EditText or button
6443 * is focused.
6444 */
6445 public boolean addStatesFromChildren() {
6446 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
6447 }
6448
6449 /**
Jeff Smitha45746e2012-07-19 14:19:24 -05006450 * If {@link #addStatesFromChildren} is true, refreshes this group's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 * drawable state (to include the states from its children).
6452 */
Alan Viverettebe463f22016-01-21 10:50:10 -05006453 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 public void childDrawableStateChanged(View child) {
6455 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6456 refreshDrawableState();
6457 }
6458 }
6459
6460 /**
6461 * Specifies the animation listener to which layout animation events must
6462 * be sent. Only
6463 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
6464 * and
6465 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
6466 * are invoked.
6467 *
6468 * @param animationListener the layout animation listener
6469 */
6470 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
6471 mAnimationListener = animationListener;
6472 }
6473
6474 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006475 * This method is called by LayoutTransition when there are 'changing' animations that need
6476 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
6477 * starts all pending transitions prior to the drawing phase in the current traversal.
6478 *
6479 * @param transition The LayoutTransition to be started on the next traversal.
6480 *
6481 * @hide
6482 */
6483 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006484 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07006485 if (viewAncestor != null) {
6486 viewAncestor.requestTransitionStart(transition);
6487 }
Chet Haasecca2c982011-05-20 14:34:18 -07006488 }
6489
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006490 /**
6491 * @hide
6492 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006493 @Override
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006494 public boolean resolveRtlPropertiesIfNeeded() {
6495 final boolean result = super.resolveRtlPropertiesIfNeeded();
6496 // We dont need to resolve the children RTL properties if nothing has changed for the parent
6497 if (result) {
6498 int count = getChildCount();
6499 for (int i = 0; i < count; i++) {
6500 final View child = getChildAt(i);
6501 if (child.isLayoutDirectionInherited()) {
6502 child.resolveRtlPropertiesIfNeeded();
6503 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006504 }
6505 }
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006506 return result;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006507 }
6508
6509 /**
6510 * @hide
6511 */
6512 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006513 public boolean resolveLayoutDirection() {
6514 final boolean result = super.resolveLayoutDirection();
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.resolveLayoutDirection();
6521 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006522 }
6523 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006524 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006525 }
6526
6527 /**
6528 * @hide
6529 */
6530 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006531 public boolean resolveTextDirection() {
6532 final boolean result = super.resolveTextDirection();
6533 if (result) {
6534 int count = getChildCount();
6535 for (int i = 0; i < count; i++) {
6536 final View child = getChildAt(i);
6537 if (child.isTextDirectionInherited()) {
6538 child.resolveTextDirection();
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 resolveTextAlignment() {
6550 final boolean result = super.resolveTextAlignment();
6551 if (result) {
6552 int count = getChildCount();
6553 for (int i = 0; i < count; i++) {
6554 final View child = getChildAt(i);
6555 if (child.isTextAlignmentInherited()) {
6556 child.resolveTextAlignment();
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 Meglio84ebb352012-10-11 16:27:37 -07006567 public void resolvePadding() {
6568 super.resolvePadding();
6569 int count = getChildCount();
6570 for (int i = 0; i < count; i++) {
6571 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006572 if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006573 child.resolvePadding();
6574 }
6575 }
6576 }
6577
6578 /**
6579 * @hide
6580 */
6581 @Override
6582 protected void resolveDrawables() {
6583 super.resolveDrawables();
6584 int count = getChildCount();
6585 for (int i = 0; i < count; i++) {
6586 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006587 if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006588 child.resolveDrawables();
6589 }
6590 }
6591 }
6592
Fabrice Di Meglio1e0ed6b2012-10-18 16:06:52 -07006593 /**
6594 * @hide
6595 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -07006596 @Override
6597 public void resolveLayoutParams() {
6598 super.resolveLayoutParams();
6599 int count = getChildCount();
6600 for (int i = 0; i < count; i++) {
6601 final View child = getChildAt(i);
6602 child.resolveLayoutParams();
6603 }
6604 }
6605
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006606 /**
6607 * @hide
6608 */
6609 @Override
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006610 public void resetResolvedLayoutDirection() {
6611 super.resetResolvedLayoutDirection();
6612
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006613 int count = getChildCount();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006614 for (int i = 0; i < count; i++) {
6615 final View child = getChildAt(i);
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006616 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07006617 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006618 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006619 }
6620 }
6621
6622 /**
6623 * @hide
6624 */
6625 @Override
6626 public void resetResolvedTextDirection() {
6627 super.resetResolvedTextDirection();
6628
6629 int count = getChildCount();
6630 for (int i = 0; i < count; i++) {
6631 final View child = getChildAt(i);
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -07006632 if (child.isTextDirectionInherited()) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006633 child.resetResolvedTextDirection();
6634 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006635 }
6636 }
6637
6638 /**
6639 * @hide
6640 */
6641 @Override
6642 public void resetResolvedTextAlignment() {
6643 super.resetResolvedTextAlignment();
6644
6645 int count = getChildCount();
6646 for (int i = 0; i < count; i++) {
6647 final View child = getChildAt(i);
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -07006648 if (child.isTextAlignmentInherited()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07006649 child.resetResolvedTextAlignment();
6650 }
6651 }
6652 }
6653
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006654 /**
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006655 * @hide
6656 */
6657 @Override
6658 public void resetResolvedPadding() {
6659 super.resetResolvedPadding();
6660
6661 int count = getChildCount();
6662 for (int i = 0; i < count; i++) {
6663 final View child = getChildAt(i);
6664 if (child.isLayoutDirectionInherited()) {
6665 child.resetResolvedPadding();
6666 }
6667 }
6668 }
6669
6670 /**
6671 * @hide
6672 */
6673 @Override
6674 protected void resetResolvedDrawables() {
6675 super.resetResolvedDrawables();
6676
6677 int count = getChildCount();
6678 for (int i = 0; i < count; i++) {
6679 final View child = getChildAt(i);
6680 if (child.isLayoutDirectionInherited()) {
6681 child.resetResolvedDrawables();
6682 }
6683 }
6684 }
6685
6686 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006687 * Return true if the pressed state should be delayed for children or descendants of this
6688 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
6689 * This prevents the pressed state from appearing when the user is actually trying to scroll
6690 * the content.
6691 *
6692 * The default implementation returns true for compatibility reasons. Subclasses that do
6693 * not scroll should generally override this method and return false.
6694 */
6695 public boolean shouldDelayChildPressedState() {
6696 return true;
6697 }
6698
Adam Powell10ba2772014-04-15 09:46:51 -07006699 /**
6700 * @inheritDoc
6701 */
6702 @Override
6703 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6704 return false;
6705 }
6706
6707 /**
6708 * @inheritDoc
6709 */
6710 @Override
6711 public void onNestedScrollAccepted(View child, View target, int axes) {
6712 mNestedScrollAxes = axes;
6713 }
6714
6715 /**
6716 * @inheritDoc
6717 *
6718 * <p>The default implementation of onStopNestedScroll calls
6719 * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p>
6720 */
6721 @Override
6722 public void onStopNestedScroll(View child) {
6723 // Stop any recursive nested scrolling.
6724 stopNestedScroll();
Adam Powelld4a22d42015-04-16 15:44:10 -07006725 mNestedScrollAxes = 0;
Adam Powell10ba2772014-04-15 09:46:51 -07006726 }
6727
6728 /**
6729 * @inheritDoc
6730 */
6731 @Override
6732 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6733 int dxUnconsumed, int dyUnconsumed) {
6734 // Do nothing
6735 }
6736
6737 /**
6738 * @inheritDoc
6739 */
6740 @Override
6741 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
6742 // Do nothing
6743 }
6744
6745 /**
6746 * @inheritDoc
6747 */
6748 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07006749 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Adam Powell10ba2772014-04-15 09:46:51 -07006750 return false;
6751 }
6752
6753 /**
Adam Powellb72be592014-07-16 21:41:31 -07006754 * @inheritDoc
6755 */
6756 @Override
6757 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
6758 return false;
6759 }
6760
6761 /**
Adam Powell10ba2772014-04-15 09:46:51 -07006762 * Return the current axes of nested scrolling for this ViewGroup.
6763 *
6764 * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently
6765 * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p>
6766 *
6767 * @return Flags indicating the current axes of nested scrolling
6768 * @see #SCROLL_AXIS_HORIZONTAL
6769 * @see #SCROLL_AXIS_VERTICAL
6770 * @see #SCROLL_AXIS_NONE
6771 */
6772 public int getNestedScrollAxes() {
6773 return mNestedScrollAxes;
6774 }
6775
Philip Milned7dd8902012-01-26 16:55:30 -08006776 /** @hide */
6777 protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
6778 }
6779
George Mounte1803372014-02-26 19:00:52 +00006780 /** @hide */
6781 @Override
6782 public void captureTransitioningViews(List<View> transitioningViews) {
6783 if (getVisibility() != View.VISIBLE) {
6784 return;
6785 }
6786 if (isTransitionGroup()) {
6787 transitioningViews.add(this);
6788 } else {
6789 int count = getChildCount();
6790 for (int i = 0; i < count; i++) {
6791 View child = getChildAt(i);
6792 child.captureTransitioningViews(transitioningViews);
6793 }
6794 }
6795 }
6796
6797 /** @hide */
6798 @Override
George Mountabb352a2014-05-09 10:27:20 -07006799 public void findNamedViews(Map<String, View> namedElements) {
George Mountfe361d22014-07-08 17:25:25 -07006800 if (getVisibility() != VISIBLE && mGhostView == null) {
George Mounte1803372014-02-26 19:00:52 +00006801 return;
6802 }
George Mountabb352a2014-05-09 10:27:20 -07006803 super.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006804 int count = getChildCount();
6805 for (int i = 0; i < count; i++) {
6806 View child = getChildAt(i);
George Mountabb352a2014-05-09 10:27:20 -07006807 child.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006808 }
6809 }
6810
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 * LayoutParams are used by views to tell their parents how they want to be
6813 * laid out. See
6814 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
6815 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07006816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 * <p>
6818 * The base LayoutParams class just describes how big the view wants to be
6819 * for both width and height. For each dimension, it can specify one of:
6820 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006821 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
6822 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
6824 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006825 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 * </ul>
6827 * There are subclasses of LayoutParams for different subclasses of
6828 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07006829 * LayoutParams which adds an X and Y value.</p>
6830 *
6831 * <div class="special reference">
6832 * <h3>Developer Guides</h3>
6833 * <p>For more information about creating user interface layouts, read the
6834 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
6835 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 *
6837 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
6838 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
6839 */
6840 public static class LayoutParams {
6841 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006842 * Special value for the height or width requested by a View.
6843 * FILL_PARENT means that the view wants to be as big as its parent,
6844 * minus the parent's padding, if any. This value is deprecated
6845 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 */
Romain Guy980a9382010-01-08 15:06:28 -08006847 @SuppressWarnings({"UnusedDeclaration"})
6848 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 public static final int FILL_PARENT = -1;
6850
6851 /**
6852 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08006853 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006854 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08006855 */
6856 public static final int MATCH_PARENT = -1;
6857
6858 /**
6859 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 * WRAP_CONTENT means that the view wants to be just large enough to fit
6861 * its own internal content, taking its own padding into account.
6862 */
6863 public static final int WRAP_CONTENT = -2;
6864
6865 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006866 * Information about how wide the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006867 * constants FILL_PARENT (replaced by MATCH_PARENT
6868 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006870 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006871 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6873 })
6874 public int width;
6875
6876 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006877 * Information about how tall the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006878 * constants FILL_PARENT (replaced by MATCH_PARENT
6879 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006881 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006882 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6884 })
6885 public int height;
6886
6887 /**
6888 * Used to animate layouts.
6889 */
6890 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
6891
6892 /**
6893 * Creates a new set of layout parameters. The values are extracted from
6894 * the supplied attributes set and context. The XML attributes mapped
6895 * to this set of layout parameters are:
6896 *
6897 * <ul>
6898 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006899 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6900 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006902 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6903 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 * </ul>
6905 *
6906 * @param c the application environment
6907 * @param attrs the set of attributes from which to extract the layout
6908 * parameters' values
6909 */
6910 public LayoutParams(Context c, AttributeSet attrs) {
6911 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
6912 setBaseAttributes(a,
6913 R.styleable.ViewGroup_Layout_layout_width,
6914 R.styleable.ViewGroup_Layout_layout_height);
6915 a.recycle();
6916 }
6917
6918 /**
6919 * Creates a new set of layout parameters with the specified width
6920 * and height.
6921 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006922 * @param width the width, either {@link #WRAP_CONTENT},
6923 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6924 * API Level 8), or a fixed size in pixels
6925 * @param height the height, either {@link #WRAP_CONTENT},
6926 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6927 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 */
6929 public LayoutParams(int width, int height) {
6930 this.width = width;
6931 this.height = height;
6932 }
6933
6934 /**
6935 * Copy constructor. Clones the width and height values of the source.
6936 *
6937 * @param source The layout params to copy from.
6938 */
6939 public LayoutParams(LayoutParams source) {
6940 this.width = source.width;
6941 this.height = source.height;
6942 }
6943
6944 /**
6945 * Used internally by MarginLayoutParams.
6946 * @hide
6947 */
6948 LayoutParams() {
6949 }
6950
6951 /**
Dave Burke579e1402012-10-18 20:41:55 -07006952 * Extracts the layout parameters from the supplied attributes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 *
6954 * @param a the style attributes to extract the parameters from
6955 * @param widthAttr the identifier of the width attribute
6956 * @param heightAttr the identifier of the height attribute
6957 */
6958 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
Dave Burke579e1402012-10-18 20:41:55 -07006959 width = a.getLayoutDimension(widthAttr, "layout_width");
6960 height = a.getLayoutDimension(heightAttr, "layout_height");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 }
6962
6963 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006964 * Resolve layout parameters depending on the layout direction. Subclasses that care about
6965 * layoutDirection changes should override this method. The default implementation does
6966 * nothing.
6967 *
6968 * @param layoutDirection the direction of the layout
6969 *
6970 * {@link View#LAYOUT_DIRECTION_LTR}
6971 * {@link View#LAYOUT_DIRECTION_RTL}
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006972 */
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07006973 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006974 }
6975
6976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 * Returns a String representation of this set of layout parameters.
6978 *
6979 * @param output the String to prepend to the internal representation
6980 * @return a String with the following format: output +
6981 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07006982 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 * @hide
6984 */
6985 public String debug(String output) {
6986 return output + "ViewGroup.LayoutParams={ width="
6987 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
6988 }
6989
6990 /**
Philip Milne10ca24a2012-04-23 15:38:27 -07006991 * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
6992 *
6993 * @param view the view that contains these layout parameters
6994 * @param canvas the canvas on which to draw
6995 *
6996 * @hide
6997 */
Philip Milne7b757812012-09-19 18:13:44 -07006998 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07006999 }
7000
7001 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 * Converts the specified size to a readable String.
7003 *
7004 * @param size the size to convert
7005 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07007006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 * @hide
7008 */
7009 protected static String sizeToString(int size) {
7010 if (size == WRAP_CONTENT) {
7011 return "wrap-content";
7012 }
Romain Guy980a9382010-01-08 15:06:28 -08007013 if (size == MATCH_PARENT) {
7014 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 }
7016 return String.valueOf(size);
7017 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007018
7019 /** @hide */
7020 void encode(@NonNull ViewHierarchyEncoder encoder) {
7021 encoder.beginObject(this);
7022 encodeProperties(encoder);
7023 encoder.endObject();
7024 }
7025
7026 /** @hide */
7027 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7028 encoder.addProperty("width", width);
7029 encoder.addProperty("height", height);
7030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032
7033 /**
7034 * Per-child layout information for layouts that support margins.
7035 * See
7036 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
7037 * for a list of all child view attributes that this class supports.
7038 */
7039 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
7040 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007041 * The left margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007042 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7043 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007045 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 public int leftMargin;
7047
7048 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007049 * The top margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007050 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7051 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007053 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 public int topMargin;
7055
7056 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007057 * The right margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007058 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7059 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007061 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062 public int rightMargin;
7063
7064 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007065 * The bottom margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007066 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7067 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007069 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 public int bottomMargin;
7071
7072 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007073 * The start margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007074 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7075 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007076 */
7077 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007078 private int startMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007079
7080 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007081 * The end margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007082 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7083 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007084 */
7085 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007086 private int endMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007087
7088 /**
7089 * The default start and end margin.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007090 * @hide
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007091 */
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007092 public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007093
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007094 /**
7095 * Bit 0: layout direction
7096 * Bit 1: layout direction
7097 * Bit 2: left margin undefined
7098 * Bit 3: right margin undefined
7099 * Bit 4: is RTL compatibility mode
7100 * Bit 5: need resolution
7101 *
7102 * Bit 6 to 7 not used
7103 *
7104 * @hide
7105 */
7106 @ViewDebug.ExportedProperty(category = "layout", flagMapping = {
7107 @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK,
7108 equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"),
7109 @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK,
7110 equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"),
7111 @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK,
7112 equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"),
7113 @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK,
7114 equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"),
7115 @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK,
7116 equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK")
Jon Miranda4597e982014-07-29 07:25:49 -07007117 }, formatToHexString = true)
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007118 byte mMarginFlags;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007119
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007120 private static final int LAYOUT_DIRECTION_MASK = 0x00000003;
7121 private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004;
7122 private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008;
7123 private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010;
7124 private static final int NEED_RESOLUTION_MASK = 0x00000020;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007125
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007126 private static final int DEFAULT_MARGIN_RESOLVED = 0;
7127 private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007128
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 * Creates a new set of layout parameters. The values are extracted from
7131 * the supplied attributes set and context.
7132 *
7133 * @param c the application environment
7134 * @param attrs the set of attributes from which to extract the layout
7135 * parameters' values
7136 */
7137 public MarginLayoutParams(Context c, AttributeSet attrs) {
7138 super();
7139
7140 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
7141 setBaseAttributes(a,
7142 R.styleable.ViewGroup_MarginLayout_layout_width,
7143 R.styleable.ViewGroup_MarginLayout_layout_height);
7144
7145 int margin = a.getDimensionPixelSize(
7146 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
7147 if (margin >= 0) {
7148 leftMargin = margin;
7149 topMargin = margin;
7150 rightMargin= margin;
7151 bottomMargin = margin;
7152 } else {
7153 leftMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007154 R.styleable.ViewGroup_MarginLayout_layout_marginLeft,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007155 UNDEFINED_MARGIN);
7156 if (leftMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007157 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007158 leftMargin = DEFAULT_MARGIN_RESOLVED;
7159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 rightMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007161 R.styleable.ViewGroup_MarginLayout_layout_marginRight,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007162 UNDEFINED_MARGIN);
7163 if (rightMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007164 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007165 rightMargin = DEFAULT_MARGIN_RESOLVED;
7166 }
7167
7168 topMargin = a.getDimensionPixelSize(
7169 R.styleable.ViewGroup_MarginLayout_layout_marginTop,
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007170 DEFAULT_MARGIN_RESOLVED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 bottomMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007172 R.styleable.ViewGroup_MarginLayout_layout_marginBottom,
7173 DEFAULT_MARGIN_RESOLVED);
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007174
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007175 startMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007176 R.styleable.ViewGroup_MarginLayout_layout_marginStart,
7177 DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007178 endMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007179 R.styleable.ViewGroup_MarginLayout_layout_marginEnd,
7180 DEFAULT_MARGIN_RELATIVE);
7181
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007182 if (isMarginRelative()) {
7183 mMarginFlags |= NEED_RESOLUTION_MASK;
7184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007185 }
7186
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007187 final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
7188 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007189 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
7190 mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
7191 }
7192
7193 // Layout direction is LTR by default
7194 mMarginFlags |= LAYOUT_DIRECTION_LTR;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 a.recycle();
7197 }
7198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 public MarginLayoutParams(int width, int height) {
7200 super(width, height);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007201
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007202 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7203 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007204
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007205 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7206 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
7208
7209 /**
7210 * Copy constructor. Clones the width, height and margin values of the source.
7211 *
7212 * @param source The layout params to copy from.
7213 */
7214 public MarginLayoutParams(MarginLayoutParams source) {
7215 this.width = source.width;
7216 this.height = source.height;
7217
7218 this.leftMargin = source.leftMargin;
7219 this.topMargin = source.topMargin;
7220 this.rightMargin = source.rightMargin;
7221 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007222 this.startMargin = source.startMargin;
7223 this.endMargin = source.endMargin;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007224
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007225 this.mMarginFlags = source.mMarginFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 }
7227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 public MarginLayoutParams(LayoutParams source) {
7229 super(source);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007230
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007231 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7232 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007233
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007234 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7235 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 }
7237
7238 /**
Adam Powelld7600832014-07-01 15:22:50 -07007239 * @hide Used internally.
7240 */
7241 public final void copyMarginsFrom(MarginLayoutParams source) {
7242 this.leftMargin = source.leftMargin;
7243 this.topMargin = source.topMargin;
7244 this.rightMargin = source.rightMargin;
7245 this.bottomMargin = source.bottomMargin;
7246 this.startMargin = source.startMargin;
7247 this.endMargin = source.endMargin;
7248
7249 this.mMarginFlags = source.mMarginFlags;
7250 }
7251
7252 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007253 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
7254 * to be done so that the new margins are taken into account. Left and right margins may be
7255 * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
Adam Powella7a735f2014-10-09 12:54:52 -07007256 * Margin values should be positive.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 *
7258 * @param left the left margin size
7259 * @param top the top margin size
7260 * @param right the right margin size
7261 * @param bottom the bottom margin size
7262 *
7263 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
7264 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7265 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
7266 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7267 */
7268 public void setMargins(int left, int top, int right, int bottom) {
7269 leftMargin = left;
7270 topMargin = top;
7271 rightMargin = right;
7272 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007273 mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
7274 mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
7275 if (isMarginRelative()) {
7276 mMarginFlags |= NEED_RESOLUTION_MASK;
7277 } else {
7278 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007281
7282 /**
7283 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
7284 * needs to be done so that the new relative margins are taken into account. Left and right
7285 * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
Adam Powella7a735f2014-10-09 12:54:52 -07007286 * direction. Margin values should be positive.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007287 *
7288 * @param start the start margin size
7289 * @param top the top margin size
7290 * @param end the right margin size
7291 * @param bottom the bottom margin size
7292 *
7293 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7294 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7295 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7296 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7297 *
7298 * @hide
7299 */
7300 public void setMarginsRelative(int start, int top, int end, int bottom) {
7301 startMargin = start;
7302 topMargin = top;
7303 endMargin = end;
7304 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007305 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007306 }
7307
7308 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007309 * Sets the relative start margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007310 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007311 * @param start the start margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007312 *
7313 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7314 */
7315 public void setMarginStart(int start) {
7316 startMargin = start;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007317 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007318 }
7319
7320 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007321 * Returns the start margin in pixels.
7322 *
7323 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7324 *
7325 * @return the start margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007326 */
7327 public int getMarginStart() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007328 if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007329 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007330 doResolveMargins();
7331 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007332 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007333 case View.LAYOUT_DIRECTION_RTL:
7334 return rightMargin;
7335 case View.LAYOUT_DIRECTION_LTR:
7336 default:
7337 return leftMargin;
7338 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007339 }
7340
7341 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007342 * Sets the relative end margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007343 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007344 * @param end the end margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007345 *
7346 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7347 */
7348 public void setMarginEnd(int end) {
7349 endMargin = end;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007350 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007351 }
7352
7353 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007354 * Returns the end margin in pixels.
7355 *
7356 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7357 *
7358 * @return the end margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007359 */
7360 public int getMarginEnd() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007361 if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007362 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007363 doResolveMargins();
7364 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007365 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007366 case View.LAYOUT_DIRECTION_RTL:
7367 return leftMargin;
7368 case View.LAYOUT_DIRECTION_LTR:
7369 default:
7370 return rightMargin;
7371 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007372 }
7373
7374 /**
7375 * Check if margins are relative.
7376 *
7377 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7378 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7379 *
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007380 * @return true if either marginStart or marginEnd has been set.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007381 */
7382 public boolean isMarginRelative() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007383 return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007384 }
7385
7386 /**
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007387 * Set the layout direction
7388 * @param layoutDirection the layout direction.
7389 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
7390 * or {@link View#LAYOUT_DIRECTION_RTL}.
7391 */
7392 public void setLayoutDirection(int layoutDirection) {
7393 if (layoutDirection != View.LAYOUT_DIRECTION_LTR &&
7394 layoutDirection != View.LAYOUT_DIRECTION_RTL) return;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007395 if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) {
7396 mMarginFlags &= ~LAYOUT_DIRECTION_MASK;
7397 mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK);
7398 if (isMarginRelative()) {
7399 mMarginFlags |= NEED_RESOLUTION_MASK;
7400 } else {
7401 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7402 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007403 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007404 }
7405
7406 /**
7407 * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or
7408 * {@link View#LAYOUT_DIRECTION_RTL}.
7409 *
7410 * @return the layout direction.
7411 */
7412 public int getLayoutDirection() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007413 return (mMarginFlags & LAYOUT_DIRECTION_MASK);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007414 }
7415
7416 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007417 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08007418 * may be overridden depending on layout direction.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007419 */
7420 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007421 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007422 setLayoutDirection(layoutDirection);
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007423
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007424 // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything
7425 // Will use the left and right margins if no relative margin is defined.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007426 if (!isMarginRelative() ||
7427 (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007428
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007429 // Proceed with resolution
7430 doResolveMargins();
7431 }
7432
7433 private void doResolveMargins() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007434 if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007435 // if left or right margins are not defined and if we have some start or end margin
7436 // defined then use those start and end margins.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007437 if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK
7438 && startMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007439 leftMargin = startMargin;
7440 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007441 if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK
7442 && endMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007443 rightMargin = endMargin;
7444 }
7445 } else {
7446 // We have some relative margins (either the start one or the end one or both). So use
7447 // them and override what has been defined for left and right margins. If either start
7448 // or end margin is not defined, just set it to default "0".
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007449 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007450 case View.LAYOUT_DIRECTION_RTL:
7451 leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7452 endMargin : DEFAULT_MARGIN_RESOLVED;
7453 rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7454 startMargin : DEFAULT_MARGIN_RESOLVED;
7455 break;
7456 case View.LAYOUT_DIRECTION_LTR:
7457 default:
7458 leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7459 startMargin : DEFAULT_MARGIN_RESOLVED;
7460 rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7461 endMargin : DEFAULT_MARGIN_RESOLVED;
7462 break;
7463 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007464 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007465 mMarginFlags &= ~NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007466 }
Philip Milne10ca24a2012-04-23 15:38:27 -07007467
Fabrice Di Meglio03b8d3a2012-09-27 17:05:27 -07007468 /**
7469 * @hide
7470 */
7471 public boolean isLayoutRtl() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007472 return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007473 }
7474
Philip Milne10ca24a2012-04-23 15:38:27 -07007475 /**
7476 * @hide
7477 */
7478 @Override
Philip Milne7b757812012-09-19 18:13:44 -07007479 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
7480 Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE;
7481
7482 fillDifference(canvas,
7483 view.getLeft() + oi.left,
7484 view.getTop() + oi.top,
7485 view.getRight() - oi.right,
7486 view.getBottom() - oi.bottom,
7487 leftMargin,
7488 topMargin,
7489 rightMargin,
7490 bottomMargin,
7491 paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07007492 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007493
7494 /** @hide */
7495 @Override
7496 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7497 super.encodeProperties(encoder);
7498 encoder.addProperty("leftMargin", leftMargin);
7499 encoder.addProperty("topMargin", topMargin);
7500 encoder.addProperty("rightMargin", rightMargin);
7501 encoder.addProperty("bottomMargin", bottomMargin);
7502 encoder.addProperty("startMargin", startMargin);
7503 encoder.addProperty("endMargin", endMargin);
7504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 }
Adam Powell2b342f02010-08-18 18:14:13 -07007506
Jeff Brown20e987b2010-08-23 12:01:02 -07007507 /* Describes a touched view and the ids of the pointers that it has captured.
7508 *
7509 * This code assumes that pointer ids are always in the range 0..31 such that
7510 * it can use a bitfield to track which pointer ids are present.
7511 * As it happens, the lower layers of the input dispatch pipeline also use the
7512 * same trick so the assumption should be safe here...
7513 */
7514 private static final class TouchTarget {
7515 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007516 private static final Object sRecycleLock = new Object[0];
Jeff Brown20e987b2010-08-23 12:01:02 -07007517 private static TouchTarget sRecycleBin;
7518 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07007519
Jeff Brown20e987b2010-08-23 12:01:02 -07007520 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07007521
Jeff Brown20e987b2010-08-23 12:01:02 -07007522 // The touched child view.
7523 public View child;
7524
7525 // The combined bit mask of pointer ids for all pointers captured by the target.
7526 public int pointerIdBits;
7527
7528 // The next target in the target list.
7529 public TouchTarget next;
7530
7531 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07007532 }
7533
Alan Viverettea7b85e62016-01-22 10:14:02 -05007534 public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {
7535 if (child == null) {
7536 throw new IllegalArgumentException("child must be non-null");
7537 }
7538
Jeff Brown20e987b2010-08-23 12:01:02 -07007539 final TouchTarget target;
7540 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07007541 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007542 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07007543 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07007544 target = sRecycleBin;
7545 sRecycleBin = target.next;
7546 sRecycledCount--;
7547 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007548 }
Adam Powell816c3be2010-08-23 18:00:05 -07007549 }
Jeff Brown20e987b2010-08-23 12:01:02 -07007550 target.child = child;
7551 target.pointerIdBits = pointerIdBits;
7552 return target;
7553 }
Adam Powell816c3be2010-08-23 18:00:05 -07007554
Jeff Brown20e987b2010-08-23 12:01:02 -07007555 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05007556 if (child == null) {
7557 throw new IllegalStateException("already recycled once");
7558 }
7559
Jeff Brown20e987b2010-08-23 12:01:02 -07007560 synchronized (sRecycleLock) {
7561 if (sRecycledCount < MAX_RECYCLED) {
7562 next = sRecycleBin;
7563 sRecycleBin = this;
7564 sRecycledCount += 1;
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007565 } else {
7566 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007567 }
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007568 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007569 }
7570 }
Adam Powell2b342f02010-08-18 18:14:13 -07007571 }
Jeff Brown87b7f802011-06-21 18:35:45 -07007572
7573 /* Describes a hovered view. */
7574 private static final class HoverTarget {
7575 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007576 private static final Object sRecycleLock = new Object[0];
Jeff Brown87b7f802011-06-21 18:35:45 -07007577 private static HoverTarget sRecycleBin;
7578 private static int sRecycledCount;
7579
7580 // The hovered child view.
7581 public View child;
7582
7583 // The next target in the target list.
7584 public HoverTarget next;
7585
7586 private HoverTarget() {
7587 }
7588
Alan Viverettea7b85e62016-01-22 10:14:02 -05007589 public static HoverTarget obtain(@NonNull View child) {
7590 if (child == null) {
7591 throw new IllegalArgumentException("child must be non-null");
7592 }
7593
Jeff Brown87b7f802011-06-21 18:35:45 -07007594 final HoverTarget target;
7595 synchronized (sRecycleLock) {
7596 if (sRecycleBin == null) {
7597 target = new HoverTarget();
7598 } else {
7599 target = sRecycleBin;
7600 sRecycleBin = target.next;
Alan Viverettea7b85e62016-01-22 10:14:02 -05007601 sRecycledCount--;
Jeff Brown87b7f802011-06-21 18:35:45 -07007602 target.next = null;
7603 }
7604 }
7605 target.child = child;
7606 return target;
7607 }
7608
7609 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05007610 if (child == null) {
7611 throw new IllegalStateException("already recycled once");
7612 }
7613
Jeff Brown87b7f802011-06-21 18:35:45 -07007614 synchronized (sRecycleLock) {
7615 if (sRecycledCount < MAX_RECYCLED) {
7616 next = sRecycleBin;
7617 sRecycleBin = this;
7618 sRecycledCount += 1;
7619 } else {
7620 next = null;
7621 }
7622 child = null;
7623 }
7624 }
7625 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007626
7627 /**
7628 * Pooled class that orderes the children of a ViewGroup from start
7629 * to end based on how they are laid out and the layout direction.
7630 */
7631 static class ChildListForAccessibility {
7632
7633 private static final int MAX_POOL_SIZE = 32;
7634
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007635 private static final SynchronizedPool<ChildListForAccessibility> sPool =
7636 new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007637
7638 private final ArrayList<View> mChildren = new ArrayList<View>();
7639
7640 private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
7641
7642 public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007643 ChildListForAccessibility list = sPool.acquire();
7644 if (list == null) {
7645 list = new ChildListForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007646 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007647 list.init(parent, sort);
7648 return list;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007649 }
7650
7651 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007652 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007653 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007654 }
7655
7656 public int getChildCount() {
7657 return mChildren.size();
7658 }
7659
7660 public View getChildAt(int index) {
7661 return mChildren.get(index);
7662 }
7663
7664 public int getChildIndex(View child) {
7665 return mChildren.indexOf(child);
7666 }
7667
7668 private void init(ViewGroup parent, boolean sort) {
7669 ArrayList<View> children = mChildren;
7670 final int childCount = parent.getChildCount();
7671 for (int i = 0; i < childCount; i++) {
7672 View child = parent.getChildAt(i);
7673 children.add(child);
7674 }
7675 if (sort) {
7676 ArrayList<ViewLocationHolder> holders = mHolders;
7677 for (int i = 0; i < childCount; i++) {
7678 View child = children.get(i);
7679 ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
7680 holders.add(holder);
7681 }
Svetoslav88e447b2014-10-09 15:49:02 -07007682 sort(holders);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007683 for (int i = 0; i < childCount; i++) {
7684 ViewLocationHolder holder = holders.get(i);
7685 children.set(i, holder.mView);
7686 holder.recycle();
7687 }
7688 holders.clear();
7689 }
7690 }
7691
Svetoslav88e447b2014-10-09 15:49:02 -07007692 private void sort(ArrayList<ViewLocationHolder> holders) {
7693 // This is gross but the least risky solution. The current comparison
7694 // strategy breaks transitivity but produces very good results. Coming
7695 // up with a new strategy requires time which we do not have, so ...
7696 try {
7697 ViewLocationHolder.setComparisonStrategy(
7698 ViewLocationHolder.COMPARISON_STRATEGY_STRIPE);
7699 Collections.sort(holders);
7700 } catch (IllegalArgumentException iae) {
7701 // Note that in practice this occurs extremely rarely in a couple
7702 // of pathological cases.
7703 ViewLocationHolder.setComparisonStrategy(
7704 ViewLocationHolder.COMPARISON_STRATEGY_LOCATION);
7705 Collections.sort(holders);
7706 }
7707 }
7708
Svetoslav Ganov42138042012-03-20 11:51:39 -07007709 private void clear() {
7710 mChildren.clear();
7711 }
7712 }
7713
7714 /**
7715 * Pooled class that holds a View and its location with respect to
7716 * a specified root. This enables sorting of views based on their
7717 * coordinates without recomputing the position relative to the root
7718 * on every comparison.
7719 */
7720 static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
7721
7722 private static final int MAX_POOL_SIZE = 32;
7723
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007724 private static final SynchronizedPool<ViewLocationHolder> sPool =
7725 new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007726
Svetoslav88e447b2014-10-09 15:49:02 -07007727 public static final int COMPARISON_STRATEGY_STRIPE = 1;
7728
7729 public static final int COMPARISON_STRATEGY_LOCATION = 2;
7730
7731 private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE;
7732
Svetoslav Ganov42138042012-03-20 11:51:39 -07007733 private final Rect mLocation = new Rect();
7734
7735 public View mView;
7736
7737 private int mLayoutDirection;
7738
7739 public static ViewLocationHolder obtain(ViewGroup root, View view) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007740 ViewLocationHolder holder = sPool.acquire();
7741 if (holder == null) {
7742 holder = new ViewLocationHolder();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007743 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007744 holder.init(root, view);
7745 return holder;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007746 }
7747
Svetoslav88e447b2014-10-09 15:49:02 -07007748 public static void setComparisonStrategy(int strategy) {
7749 sComparisonStrategy = strategy;
7750 }
7751
Svetoslav Ganov42138042012-03-20 11:51:39 -07007752 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007753 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007754 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007755 }
7756
7757 @Override
7758 public int compareTo(ViewLocationHolder another) {
7759 // This instance is greater than an invalid argument.
7760 if (another == null) {
7761 return 1;
7762 }
Svetoslav88e447b2014-10-09 15:49:02 -07007763
7764 if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) {
7765 // First is above second.
7766 if (mLocation.bottom - another.mLocation.top <= 0) {
7767 return -1;
7768 }
7769 // First is below second.
7770 if (mLocation.top - another.mLocation.bottom >= 0) {
7771 return 1;
7772 }
7773 }
7774
Svetoslav04cab1b2014-08-25 18:35:57 -07007775 // We are ordering left-to-right, top-to-bottom.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007776 if (mLayoutDirection == LAYOUT_DIRECTION_LTR) {
7777 final int leftDifference = mLocation.left - another.mLocation.left;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007778 if (leftDifference != 0) {
7779 return leftDifference;
7780 }
7781 } else { // RTL
7782 final int rightDifference = mLocation.right - another.mLocation.right;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007783 if (rightDifference != 0) {
7784 return -rightDifference;
7785 }
7786 }
Svetoslav04cab1b2014-08-25 18:35:57 -07007787 // We are ordering left-to-right, top-to-bottom.
7788 final int topDifference = mLocation.top - another.mLocation.top;
7789 if (topDifference != 0) {
7790 return topDifference;
7791 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007792 // Break tie by height.
7793 final int heightDiference = mLocation.height() - another.mLocation.height();
7794 if (heightDiference != 0) {
7795 return -heightDiference;
7796 }
7797 // Break tie by width.
7798 final int widthDiference = mLocation.width() - another.mLocation.width();
7799 if (widthDiference != 0) {
7800 return -widthDiference;
7801 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007802 // Just break the tie somehow. The accessibliity ids are unique
7803 // and stable, hence this is deterministic tie breaking.
7804 return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007805 }
7806
7807 private void init(ViewGroup root, View view) {
7808 Rect viewLocation = mLocation;
7809 view.getDrawingRect(viewLocation);
7810 root.offsetDescendantRectToMyCoords(view, viewLocation);
7811 mView = view;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07007812 mLayoutDirection = root.getLayoutDirection();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007813 }
7814
7815 private void clear() {
7816 mView = null;
7817 mLocation.set(0, 0, 0, 0);
7818 }
7819 }
Romain Guycbc67742012-04-27 16:12:57 -07007820
7821 private static Paint getDebugPaint() {
7822 if (sDebugPaint == null) {
7823 sDebugPaint = new Paint();
7824 sDebugPaint.setAntiAlias(false);
7825 }
7826 return sDebugPaint;
7827 }
7828
Romain Guy6410c0a2013-06-17 11:21:58 -07007829 private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
Romain Guycbc67742012-04-27 16:12:57 -07007830 if (sDebugLines== null) {
Romain Guy6410c0a2013-06-17 11:21:58 -07007831 // TODO: This won't work with multiple UI threads in a single process
Romain Guycbc67742012-04-27 16:12:57 -07007832 sDebugLines = new float[16];
7833 }
7834
Romain Guycbc67742012-04-27 16:12:57 -07007835 sDebugLines[0] = x1;
7836 sDebugLines[1] = y1;
7837 sDebugLines[2] = x2;
7838 sDebugLines[3] = y1;
7839
7840 sDebugLines[4] = x2;
7841 sDebugLines[5] = y1;
7842 sDebugLines[6] = x2;
Philip Milne7b757812012-09-19 18:13:44 -07007843 sDebugLines[7] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007844
Philip Milne7b757812012-09-19 18:13:44 -07007845 sDebugLines[8] = x2;
Romain Guycbc67742012-04-27 16:12:57 -07007846 sDebugLines[9] = y2;
7847 sDebugLines[10] = x1;
7848 sDebugLines[11] = y2;
7849
Philip Milne7b757812012-09-19 18:13:44 -07007850 sDebugLines[12] = x1;
7851 sDebugLines[13] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007852 sDebugLines[14] = x1;
7853 sDebugLines[15] = y1;
7854
Philip Milne7b757812012-09-19 18:13:44 -07007855 canvas.drawLines(sDebugLines, paint);
Romain Guycbc67742012-04-27 16:12:57 -07007856 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007857
7858 /** @hide */
7859 @Override
7860 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7861 super.encodeProperties(encoder);
7862
7863 encoder.addProperty("focus:descendantFocusability", getDescendantFocusability());
7864 encoder.addProperty("drawing:clipChildren", getClipChildren());
7865 encoder.addProperty("drawing:clipToPadding", getClipToPadding());
7866 encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled());
7867 encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache());
7868
7869 int n = getChildCount();
7870 encoder.addProperty("meta:__childCount__", (short)n);
7871 for (int i = 0; i < n; i++) {
7872 encoder.addPropertyKey("meta:__child__" + i);
7873 getChildAt(i).encode(encoder);
7874 }
7875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876}