blob: 3f7bbdf24e97e405bdb1491c9a7975e034e449b6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Chet Haase21cd1382010-09-01 17:42:29 -070019import android.animation.LayoutTransition;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.IdRes;
Alan Viverette922e1c62015-05-05 17:18:27 -070021import android.annotation.NonNull;
Tor Norbye83c68962015-03-10 20:55:31 -070022import android.annotation.UiThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +000024import android.content.Intent;
Adam Powellff0d2982014-07-10 20:34:14 -070025import android.content.pm.PackageManager;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080026import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.res.TypedArray;
28import android.graphics.Bitmap;
29import android.graphics.Canvas;
Philip Milne10ca24a2012-04-23 15:38:27 -070030import android.graphics.Color;
31import android.graphics.Insets;
Adam Powell6e346362010-07-23 10:18:23 -070032import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Paint;
Christopher Tatea53146c2010-09-07 11:57:52 -070034import android.graphics.PointF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.graphics.RectF;
svetoslavganov75986cf2009-05-14 22:28:01 -070037import android.graphics.Region;
Jeff Brown995e7742010-12-22 16:59:36 -080038import android.os.Build;
Adam Powellb6ab0982015-01-07 17:00:12 -080039import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.Parcelable;
41import android.os.SystemClock;
42import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.util.Log;
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -080044import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.util.SparseArray;
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -070046import android.util.SparseBooleanArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070047import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070048import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.view.animation.Animation;
50import android.view.animation.AnimationUtils;
51import android.view.animation.LayoutAnimationController;
52import android.view.animation.Transformation;
Doug Feltcb3791202011-07-07 11:57:48 -070053
Romain Guy0211a0a2011-02-14 16:34:59 -080054import com.android.internal.R;
55import com.android.internal.util.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57import java.util.ArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -070058import java.util.Collections;
Christopher Tate86cab1b2011-01-13 20:28:55 -080059import java.util.HashSet;
George Mounte1803372014-02-26 19:00:52 +000060import java.util.List;
61import java.util.Map;
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +090062
Fabrice Di Meglio0072f642013-03-26 15:50:24 -070063import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065/**
66 * <p>
67 * A <code>ViewGroup</code> is a special view that can contain other views
68 * (called children.) The view group is the base class for layouts and views
69 * containers. This class also defines the
70 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
71 * class for layouts parameters.
72 * </p>
73 *
74 * <p>
75 * Also see {@link LayoutParams} for layout attributes.
76 * </p>
Romain Guyd6a463a2009-05-21 23:10:10 -070077 *
Joe Fernandez558459f2011-10-13 16:47:36 -070078 * <div class="special reference">
79 * <h3>Developer Guides</h3>
80 * <p>For more information about creating user interface layouts, read the
81 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
82 * guide.</p></div>
83 *
Dianne Hackborn7caab0f2013-03-06 13:47:04 -080084 * <p>Here is a complete implementation of a custom ViewGroup that implements
85 * a simple {@link android.widget.FrameLayout} along with the ability to stack
86 * children in left and right gutters.</p>
87 *
88 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java
89 * Complete}
90 *
91 * <p>If you are implementing XML layout attributes as shown in the example, this is the
92 * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p>
93 *
94 * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout}
95 *
96 * <p>Finally the layout manager can be used in an XML layout like so:</p>
97 *
98 * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete}
99 *
Romain Guyd6a463a2009-05-21 23:10:10 -0700100 * @attr ref android.R.styleable#ViewGroup_clipChildren
101 * @attr ref android.R.styleable#ViewGroup_clipToPadding
102 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
103 * @attr ref android.R.styleable#ViewGroup_animationCache
104 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
105 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
106 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
107 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
Chet Haase13cc1202010-09-03 15:39:20 -0700108 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Scott Main27a85082013-06-10 10:39:48 -0700109 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
110 * @attr ref android.R.styleable#ViewGroup_layoutMode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 */
Tor Norbye83c68962015-03-10 20:55:31 -0700112@UiThread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Adam Powell539ee872012-02-03 19:00:49 -0800114 private static final String TAG = "ViewGroup";
Chet Haase21cd1382010-09-01 17:42:29 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 private static final boolean DBG = false;
Philip Milne7b757812012-09-19 18:13:44 -0700117 /** @hide */
118 public static boolean DEBUG_DRAW = false;
Gilles Debunnecea45132011-11-24 02:19:27 +0100119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 /**
121 * Views which have been hidden or removed which need to be animated on
122 * their way out.
123 * This field should be made private, so it is hidden from the SDK.
124 * {@hide}
125 */
126 protected ArrayList<View> mDisappearingChildren;
127
128 /**
129 * Listener used to propagate events indicating when children are added
130 * and/or removed from a view group.
131 * This field should be made private, so it is hidden from the SDK.
132 * {@hide}
133 */
134 protected OnHierarchyChangeListener mOnHierarchyChangeListener;
135
136 // The view contained within this ViewGroup that has or contains focus.
137 private View mFocused;
138
Chet Haase48460322010-06-11 14:22:25 -0700139 /**
140 * A Transformation used when drawing children, to
141 * apply on the child being drawn.
142 */
Romain Guyf6991302013-06-05 17:19:01 -0700143 private Transformation mChildTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700144
145 /**
146 * Used to track the current invalidation region.
147 */
Chet Haase64a48c12012-02-13 16:33:29 -0800148 RectF mInvalidateRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Chet Haase48460322010-06-11 14:22:25 -0700150 /**
151 * A Transformation used to calculate a correct
152 * invalidation area when the application is autoscaled.
153 */
Chet Haase64a48c12012-02-13 16:33:29 -0800154 Transformation mInvalidationTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700155
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700156 // View currently under an ongoing drag. Can be null, a child or this window.
Christopher Tatea53146c2010-09-07 11:57:52 -0700157 private View mCurrentDragView;
158
Christopher Tate86cab1b2011-01-13 20:28:55 -0800159 // Metadata about the ongoing drag
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700160 private DragEvent mCurrentDragStartEvent;
161 private boolean mIsInterestedInDrag;
162 private HashSet<View> mChildrenInterestedInDrag;
Christopher Tatea53146c2010-09-07 11:57:52 -0700163
164 // Used during drag dispatch
Romain Guy6410c0a2013-06-17 11:21:58 -0700165 private PointF mLocalPoint;
Christopher Tatea53146c2010-09-07 11:57:52 -0700166
Alan Viveretteb942b6f2014-12-08 10:37:39 -0800167 // Lazily-created holder for point computations.
168 private float[] mTempPoint;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 // Layout animation
171 private LayoutAnimationController mLayoutAnimationController;
172 private Animation.AnimationListener mAnimationListener;
173
Jeff Brown20e987b2010-08-23 12:01:02 -0700174 // First touch target in the linked list of touch targets.
175 private TouchTarget mFirstTouchTarget;
176
Joe Onorato03ab0c72011-01-06 15:46:27 -0800177 // For debugging only. You can see these in hierarchyviewer.
Romain Guye95003e2011-01-09 13:53:06 -0800178 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800179 @ViewDebug.ExportedProperty(category = "events")
180 private long mLastTouchDownTime;
181 @ViewDebug.ExportedProperty(category = "events")
182 private int mLastTouchDownIndex = -1;
Romain Guye95003e2011-01-09 13:53:06 -0800183 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800184 @ViewDebug.ExportedProperty(category = "events")
185 private float mLastTouchDownX;
Romain Guye95003e2011-01-09 13:53:06 -0800186 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800187 @ViewDebug.ExportedProperty(category = "events")
188 private float mLastTouchDownY;
189
Jeff Brown87b7f802011-06-21 18:35:45 -0700190 // First hover target in the linked list of hover targets.
191 // The hover targets are children which have received ACTION_HOVER_ENTER.
192 // They might not have actually handled the hover event, but we will
193 // continue sending hover events to them as long as the pointer remains over
194 // their bounds and the view group does not intercept hover.
195 private HoverTarget mFirstHoverTarget;
Jeff Browna032cc02011-03-07 16:56:21 -0800196
Jeff Brown10b62902011-06-20 16:40:37 -0700197 // True if the view group itself received a hover event.
198 // It might not have actually handled the hover event.
199 private boolean mHoveredSelf;
200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * This field should be made private, so it is hidden from the SDK.
205 * {@hide}
206 */
Romain Guy2440e672012-08-07 14:43:43 -0700207 @ViewDebug.ExportedProperty(flagMapping = {
208 @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN,
209 name = "CLIP_CHILDREN"),
210 @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING,
211 name = "CLIP_TO_PADDING"),
212 @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL,
213 name = "PADDING_NOT_NULL")
Jon Miranda4597e982014-07-29 07:25:49 -0700214 }, formatToHexString = true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 protected int mGroupFlags;
216
Philip Milne7b757812012-09-19 18:13:44 -0700217 /**
218 * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -0700219 */
Philip Milnecfb631b2012-10-26 10:51:46 -0700220 private int mLayoutMode = LAYOUT_MODE_UNDEFINED;
Philip Milne1557fd72012-04-04 23:41:34 -0700221
Romain Guy33f6beb2012-02-16 19:24:51 -0800222 /**
223 * NOTE: If you change the flags below make sure to reflect the changes
224 * the DisplayList class
225 */
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 // When set, ViewGroup invalidates only the child's rectangle
228 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800229 static final int FLAG_CLIP_CHILDREN = 0x1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 // When set, ViewGroup excludes the padding area from the invalidate rectangle
232 // Set by default
233 private static final int FLAG_CLIP_TO_PADDING = 0x2;
234
235 // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
236 // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
Chet Haase64a48c12012-02-13 16:33:29 -0800237 static final int FLAG_INVALIDATE_REQUIRED = 0x4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // When set, dispatchDraw() will run the layout animation and unset the flag
240 private static final int FLAG_RUN_ANIMATION = 0x8;
241
242 // When set, there is either no layout animation on the ViewGroup or the layout
243 // animation is over
244 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800245 static final int FLAG_ANIMATION_DONE = 0x10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
247 // If set, this ViewGroup has padding; if unset there is no padding and we don't need
248 // to clip it, even if FLAG_CLIP_TO_PADDING is set
249 private static final int FLAG_PADDING_NOT_NULL = 0x20;
250
Chris Craik5a6bbae2015-04-10 17:41:34 -0700251 /** @deprecated - functionality removed */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 private static final int FLAG_ANIMATION_CACHE = 0x40;
253
254 // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
255 // layout animation; this avoid clobbering the hierarchy
256 // Automatically set when the layout animation starts, depending on the animation's
257 // characteristics
Chet Haase64a48c12012-02-13 16:33:29 -0800258 static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
260 // When set, the next call to drawChild() will clear mChildTransformation's matrix
Chet Haase64a48c12012-02-13 16:33:29 -0800261 static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
264 // the children's Bitmap caches if necessary
265 // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
266 private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
267
268 /**
269 * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
270 * to get the index of the child to draw for that iteration.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800271 *
Romain Guy293451e2009-11-04 13:59:48 -0800272 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
274 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * When set, this ViewGroup supports static transformations on children; this causes
278 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
279 * invoked when a child is drawn.
280 *
281 * Any subclass overriding
282 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
283 * set this flags in {@link #mGroupFlags}.
Romain Guy8506ab42009-06-11 17:35:47 -0700284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * {@hide}
286 */
287 protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
288
John Reckfb5899d2014-08-15 18:51:27 -0700289 // UNUSED FLAG VALUE: 0x1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
291 /**
292 * When set, this ViewGroup's drawable states also include those
293 * of its children.
294 */
295 private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
296
Chris Craik5a6bbae2015-04-10 17:41:34 -0700297 /** @deprecated functionality removed */
298 private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Chris Craik5a6bbae2015-04-10 17:41:34 -0700300 /** @deprecated functionality removed */
301 private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
303 /**
304 * When set, this group will go through its list of children to notify them of
305 * any drawable state change.
306 */
307 private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
308
309 private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
310
311 /**
312 * This view will get focus before any of its descendants.
313 */
314 public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
315
316 /**
317 * This view will get focus only if none of its descendants want it.
318 */
319 public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
320
321 /**
322 * This view will block any of its descendants from getting focus, even
323 * if they are focusable.
324 */
325 public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
326
327 /**
328 * Used to map between enum in attrubutes and flag values.
329 */
330 private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
331 {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
332 FOCUS_BLOCK_DESCENDANTS};
333
334 /**
335 * When set, this ViewGroup should not intercept touch events.
Adam Powell110486f2010-06-22 17:14:44 -0700336 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 */
Adam Powell110486f2010-06-22 17:14:44 -0700338 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700341 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
342 */
Adam Powellf37df072010-09-17 16:22:49 -0700343 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700344
345 /**
Adam Powell4b867882011-09-16 12:59:46 -0700346 * When set, this ViewGroup will not dispatch onAttachedToWindow calls
347 * to children when adding new views. This is used to prevent multiple
348 * onAttached calls when a ViewGroup adds children in its own onAttached method.
349 */
350 private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000;
351
352 /**
Philip Milnecfb631b2012-10-26 10:51:46 -0700353 * When true, indicates that a layoutMode has been explicitly set, either with
354 * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource.
355 * This distinguishes the situation in which a layout mode was inherited from
356 * one of the ViewGroup's ancestors and cached locally.
357 */
358 private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000;
359
Chris Craikb49f4462014-03-20 12:44:20 -0700360 static final int FLAG_IS_TRANSITION_GROUP = 0x1000000;
Chris Craikd863a102013-12-19 13:31:15 -0800361
Chris Craikb49f4462014-03-20 12:44:20 -0700362 static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000;
George Mount0a778ed2013-12-13 13:35:36 -0800363
Chris Craikd863a102013-12-19 13:31:15 -0800364 /**
Adam Powellff0d2982014-07-10 20:34:14 -0700365 * When set, focus will not be permitted to enter this group if a touchscreen is present.
366 */
367 static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000;
368
369 /**
Clara Bayarri4423d912015-03-02 19:42:48 +0000370 * When true, indicates that a call to startActionModeForChild was made with the type parameter
371 * and should not be ignored. This helps in backwards compatibility with the existing method
372 * without a type.
373 *
374 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
375 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
376 */
377 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000;
378
379 /**
380 * When true, indicates that a call to startActionModeForChild was made without the type
381 * parameter. This helps in backwards compatibility with the existing method
382 * without a type.
383 *
384 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
385 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
386 */
387 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000;
388
389 /**
Adam Powell2af189a2016-02-05 15:52:02 -0800390 * When set, indicates that a call to showContextMenuForChild was made with explicit
391 * coordinates within the initiating child view.
392 */
393 private static final int FLAG_SHOW_CONTEXT_MENU_WITH_COORDS = 0x20000000;
394
395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 * Indicates which types of drawing caches are to be kept in memory.
397 * This field should be made private, so it is hidden from the SDK.
398 * {@hide}
399 */
400 protected int mPersistentDrawingCache;
401
402 /**
403 * Used to indicate that no drawing cache should be kept in memory.
404 */
405 public static final int PERSISTENT_NO_CACHE = 0x0;
406
407 /**
408 * Used to indicate that the animation drawing cache should be kept in memory.
409 */
410 public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
411
412 /**
413 * Used to indicate that the scrolling drawing cache should be kept in memory.
414 */
415 public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
416
417 /**
418 * Used to indicate that all drawing caches should be kept in memory.
419 */
420 public static final int PERSISTENT_ALL_CACHES = 0x3;
421
Philip Milne1557fd72012-04-04 23:41:34 -0700422 // Layout Modes
423
Philip Milnecfb631b2012-10-26 10:51:46 -0700424 private static final int LAYOUT_MODE_UNDEFINED = -1;
425
Philip Milne1557fd72012-04-04 23:41:34 -0700426 /**
427 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700428 * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
Philip Milne1557fd72012-04-04 23:41:34 -0700429 * {@link #getRight() right} and {@link #getBottom() bottom}.
430 */
Philip Milne7b757812012-09-19 18:13:44 -0700431 public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
Philip Milne1557fd72012-04-04 23:41:34 -0700432
433 /**
434 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700435 * Optical bounds describe where a widget appears to be. They sit inside the clip
436 * bounds which need to cover a larger area to allow other effects,
437 * such as shadows and glows, to be drawn.
Philip Milne1557fd72012-04-04 23:41:34 -0700438 */
Philip Milne7b757812012-09-19 18:13:44 -0700439 public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
440
441 /** @hide */
Philip Milnecfb631b2012-10-26 10:51:46 -0700442 public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS;
Philip Milne1557fd72012-04-04 23:41:34 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
446 * are set at the same time.
447 */
448 protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
449
450 // Index of the child's left position in the mLocation array
451 private static final int CHILD_LEFT_INDEX = 0;
452 // Index of the child's top position in the mLocation array
453 private static final int CHILD_TOP_INDEX = 1;
454
455 // Child views of this ViewGroup
456 private View[] mChildren;
457 // Number of valid children in the mChildren array, the rest should be null or not
458 // considered as children
459 private int mChildrenCount;
460
Chet Haaseb9895022013-04-02 15:10:58 -0700461 // Whether layout calls are currently being suppressed, controlled by calls to
462 // suppressLayout()
463 boolean mSuppressLayout = false;
464
465 // Whether any layout calls have actually been suppressed while mSuppressLayout
466 // has been true. This tracks whether we need to issue a requestLayout() when
467 // layout is later re-enabled.
468 private boolean mLayoutCalledWhileSuppressed = false;
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 private static final int ARRAY_INITIAL_CAPACITY = 12;
471 private static final int ARRAY_CAPACITY_INCREMENT = 12;
472
Romain Guycbc67742012-04-27 16:12:57 -0700473 private static Paint sDebugPaint;
474 private static float[] sDebugLines;
Philip Milne604f4402012-04-24 19:27:11 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 // Used to draw cached views
Chet Haase64a48c12012-02-13 16:33:29 -0800477 Paint mCachePaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
Chet Haase21cd1382010-09-01 17:42:29 -0700479 // Used to animate add/remove changes in layout
480 private LayoutTransition mTransition;
481
482 // The set of views that are currently being transitioned. This list is used to track views
483 // being removed that should not actually be removed from the parent yet because they are
484 // being animated.
485 private ArrayList<View> mTransitioningViews;
486
Chet Haase5e25c2c2010-09-16 11:15:56 -0700487 // List of children changing visibility. This is used to potentially keep rendering
488 // views during a transition when they otherwise would have become gone/invisible
489 private ArrayList<View> mVisibilityChangingChildren;
490
Chris Craikab008f02014-05-23 17:55:03 -0700491 // Temporary holder of presorted children, only used for
492 // input/software draw dispatch for correctly Z ordering.
493 private ArrayList<View> mPreSortedChildren;
494
Adam Powell539ee872012-02-03 19:00:49 -0800495 // Indicates how many of this container's child subtrees contain transient state
496 @ViewDebug.ExportedProperty(category = "layout")
497 private int mChildCountWithTransientState = 0;
498
Adam Powell10ba2772014-04-15 09:46:51 -0700499 /**
500 * Currently registered axes for nested scrolling. Flag set consisting of
501 * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE}
502 * for null.
503 */
504 private int mNestedScrollAxes;
505
Chet Haasec633d2f2015-04-07 10:29:39 -0700506 // Used to manage the list of transient views, added by addTransientView()
507 private List<Integer> mTransientIndices = null;
508 private List<View> mTransientViews = null;
509
510
Clara Bayarri4423d912015-03-02 19:42:48 +0000511 /**
512 * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild.
513 *
514 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
515 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
516 */
517 private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() {
518 @Override
519 public void setTitle(CharSequence title) {}
520
521 @Override
522 public void setTitle(int resId) {}
523
524 @Override
525 public void setSubtitle(CharSequence subtitle) {}
526
527 @Override
528 public void setSubtitle(int resId) {}
529
530 @Override
531 public void setCustomView(View view) {}
532
533 @Override
534 public void invalidate() {}
535
536 @Override
537 public void finish() {}
538
539 @Override
540 public Menu getMenu() {
541 return null;
542 }
543
544 @Override
545 public CharSequence getTitle() {
546 return null;
547 }
548
549 @Override
550 public CharSequence getSubtitle() {
551 return null;
552 }
553
554 @Override
555 public View getCustomView() {
556 return null;
557 }
558
559 @Override
560 public MenuInflater getMenuInflater() {
561 return null;
562 }
563 };
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public ViewGroup(Context context) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700566 this(context, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568
569 public ViewGroup(Context context, AttributeSet attrs) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700570 this(context, attrs, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
Alan Viverette617feb92013-09-09 18:09:13 -0700573 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700574 this(context, attrs, defStyleAttr, 0);
Alan Viverette617feb92013-09-09 18:09:13 -0700575 }
576
577 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
578 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 initViewGroup();
Alan Viveretted6479ec2013-09-10 17:03:02 -0700580 initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
Philip Milne10ca24a2012-04-23 15:38:27 -0700583 private boolean debugDraw() {
Philip Milne7b757812012-09-19 18:13:44 -0700584 return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout;
Philip Milne10ca24a2012-04-23 15:38:27 -0700585 }
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 private void initViewGroup() {
588 // ViewGroup doesn't draw by default
Philip Milne10ca24a2012-04-23 15:38:27 -0700589 if (!debugDraw()) {
590 setFlags(WILL_NOT_DRAW, DRAW_MASK);
591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mGroupFlags |= FLAG_CLIP_CHILDREN;
593 mGroupFlags |= FLAG_CLIP_TO_PADDING;
594 mGroupFlags |= FLAG_ANIMATION_DONE;
Chris Craikf6ce8fd2015-05-11 15:33:11 -0700595 mGroupFlags |= FLAG_ANIMATION_CACHE;
596 mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597
Jeff Brown995e7742010-12-22 16:59:36 -0800598 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
599 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
600 }
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
603
604 mChildren = new View[ARRAY_INITIAL_CAPACITY];
605 mChildrenCount = 0;
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
608 }
609
Alan Viveretted6479ec2013-09-10 17:03:02 -0700610 private void initFromAttributes(
611 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Filip Gruszczyńskib50cea02014-03-05 17:54:58 -0800612 final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr,
613 defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 final int N = a.getIndexCount();
616 for (int i = 0; i < N; i++) {
617 int attr = a.getIndex(i);
618 switch (attr) {
619 case R.styleable.ViewGroup_clipChildren:
620 setClipChildren(a.getBoolean(attr, true));
621 break;
622 case R.styleable.ViewGroup_clipToPadding:
623 setClipToPadding(a.getBoolean(attr, true));
624 break;
625 case R.styleable.ViewGroup_animationCache:
626 setAnimationCacheEnabled(a.getBoolean(attr, true));
627 break;
628 case R.styleable.ViewGroup_persistentDrawingCache:
629 setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
630 break;
631 case R.styleable.ViewGroup_addStatesFromChildren:
632 setAddStatesFromChildren(a.getBoolean(attr, false));
633 break;
634 case R.styleable.ViewGroup_alwaysDrawnWithCache:
635 setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
636 break;
637 case R.styleable.ViewGroup_layoutAnimation:
638 int id = a.getResourceId(attr, -1);
639 if (id > 0) {
640 setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
641 }
642 break;
643 case R.styleable.ViewGroup_descendantFocusability:
644 setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
645 break;
Adam Powell2b342f02010-08-18 18:14:13 -0700646 case R.styleable.ViewGroup_splitMotionEvents:
647 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
648 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700649 case R.styleable.ViewGroup_animateLayoutChanges:
650 boolean animateLayoutChanges = a.getBoolean(attr, false);
651 if (animateLayoutChanges) {
652 setLayoutTransition(new LayoutTransition());
653 }
654 break;
Philip Milne7b757812012-09-19 18:13:44 -0700655 case R.styleable.ViewGroup_layoutMode:
Philip Milnecfb631b2012-10-26 10:51:46 -0700656 setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED));
Philip Milne7b757812012-09-19 18:13:44 -0700657 break;
George Mount0a778ed2013-12-13 13:35:36 -0800658 case R.styleable.ViewGroup_transitionGroup:
659 setTransitionGroup(a.getBoolean(attr, false));
660 break;
Adam Powellff0d2982014-07-10 20:34:14 -0700661 case R.styleable.ViewGroup_touchscreenBlocksFocus:
662 setTouchscreenBlocksFocus(a.getBoolean(attr, false));
663 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
666
667 a.recycle();
668 }
669
670 /**
671 * Gets the descendant focusability of this view group. The descendant
672 * focusability defines the relationship between this view group and its
673 * descendants when looking for a view to take focus in
674 * {@link #requestFocus(int, android.graphics.Rect)}.
675 *
676 * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
677 * {@link #FOCUS_BLOCK_DESCENDANTS}.
678 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700679 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
681 @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
682 @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
683 })
684 public int getDescendantFocusability() {
685 return mGroupFlags & FLAG_MASK_FOCUSABILITY;
686 }
687
688 /**
689 * Set the descendant focusability of this view group. This defines the relationship
690 * between this view group and its descendants when looking for a view to
691 * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
692 *
693 * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
694 * {@link #FOCUS_BLOCK_DESCENDANTS}.
695 */
696 public void setDescendantFocusability(int focusability) {
697 switch (focusability) {
698 case FOCUS_BEFORE_DESCENDANTS:
699 case FOCUS_AFTER_DESCENDANTS:
700 case FOCUS_BLOCK_DESCENDANTS:
701 break;
702 default:
703 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
704 + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
705 }
706 mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
707 mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
708 }
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 @Override
711 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
712 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800713 mFocused.unFocus(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 mFocused = null;
715 }
716 super.handleFocusGainInternal(direction, previouslyFocusedRect);
717 }
718
Alan Viverettebe463f22016-01-21 10:50:10 -0500719 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 public void requestChildFocus(View child, View focused) {
721 if (DBG) {
722 System.out.println(this + " requestChildFocus()");
723 }
724 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
725 return;
726 }
727
728 // Unfocus us, if necessary
Alan Viverette223622a2013-12-17 13:29:02 -0800729 super.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730
731 // We had a previous notion of who had focus. Clear it.
732 if (mFocused != child) {
733 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800734 mFocused.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736
737 mFocused = child;
738 }
739 if (mParent != null) {
740 mParent.requestChildFocus(this, focused);
741 }
742 }
743
Alan Viverettebe463f22016-01-21 10:50:10 -0500744 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 public void focusableViewAvailable(View v) {
746 if (mParent != null
747 // shortcut: don't report a new focusable view if we block our descendants from
748 // getting focus
749 && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS)
Adam Powell88c11752014-07-21 17:19:16 -0700750 && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 // shortcut: don't report a new focusable view if we already are focused
752 // (and we don't prefer our descendants)
753 //
754 // note: knowing that mFocused is non-null is not a good enough reason
755 // to break the traversal since in that case we'd actually have to find
756 // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700757 // an ancestor of v; this will get checked for at ViewAncestor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) {
759 mParent.focusableViewAvailable(v);
760 }
761 }
762
Alan Viverettebe463f22016-01-21 10:50:10 -0500763 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 public boolean showContextMenuForChild(View originalView) {
Adam Powell2af189a2016-02-05 15:52:02 -0800765 if (isShowingContextMenuWithCoords()) {
766 // We're being called for compatibility. Return false and let the version
767 // with coordinates recurse up.
768 return false;
769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 return mParent != null && mParent.showContextMenuForChild(originalView);
771 }
772
Adam Powell2af189a2016-02-05 15:52:02 -0800773 /**
774 * @hide used internally for compatibility with existing app code only
775 */
776 public final boolean isShowingContextMenuWithCoords() {
777 return (mGroupFlags & FLAG_SHOW_CONTEXT_MENU_WITH_COORDS) != 0;
778 }
779
Oren Blasberged391262015-09-01 12:12:51 -0700780 @Override
781 public boolean showContextMenuForChild(View originalView, float x, float y) {
Adam Powell2af189a2016-02-05 15:52:02 -0800782 try {
783 mGroupFlags |= FLAG_SHOW_CONTEXT_MENU_WITH_COORDS;
784 if (showContextMenuForChild(originalView)) {
785 return true;
786 }
787 } finally {
788 mGroupFlags &= ~FLAG_SHOW_CONTEXT_MENU_WITH_COORDS;
789 }
Oren Blasberged391262015-09-01 12:12:51 -0700790 return mParent != null && mParent.showContextMenuForChild(originalView, x, y);
791 }
792
Clara Bayarri4423d912015-03-02 19:42:48 +0000793 @Override
Adam Powell6e346362010-07-23 10:18:23 -0700794 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000795 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) {
796 // This is the original call.
797 try {
798 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
799 return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY);
800 } finally {
801 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
802 }
803 } else {
804 // We are being called from the new method with type.
805 return SENTINEL_ACTION_MODE;
806 }
807 }
808
Clara Bayarri4423d912015-03-02 19:42:48 +0000809 @Override
810 public ActionMode startActionModeForChild(
811 View originalView, ActionMode.Callback callback, int type) {
Adam Powelle9fd6d22015-06-01 11:26:32 -0700812 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0
813 && type == ActionMode.TYPE_PRIMARY) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000814 ActionMode mode;
815 try {
816 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
817 mode = startActionModeForChild(originalView, callback);
818 } finally {
819 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
820 }
821 if (mode != SENTINEL_ACTION_MODE) {
822 return mode;
823 }
824 }
825 if (mParent != null) {
826 try {
827 return mParent.startActionModeForChild(originalView, callback, type);
828 } catch (AbstractMethodError ame) {
829 // Custom view parents might not implement this method.
830 return mParent.startActionModeForChild(originalView, callback);
831 }
832 }
833 return null;
Adam Powell6e346362010-07-23 10:18:23 -0700834 }
835
836 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000837 * @hide
838 */
839 @Override
840 public boolean dispatchActivityResult(
841 String who, int requestCode, int resultCode, Intent data) {
842 if (super.dispatchActivityResult(who, requestCode, resultCode, data)) {
843 return true;
844 }
845 int childCount = getChildCount();
846 for (int i = 0; i < childCount; i++) {
847 View child = getChildAt(i);
848 if (child.dispatchActivityResult(who, requestCode, resultCode, data)) {
849 return true;
850 }
851 }
852 return false;
853 }
854
855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 * Find the nearest view in the specified direction that wants to take
857 * focus.
858 *
859 * @param focused The view that currently has focus
860 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
861 * FOCUS_RIGHT, or 0 for not applicable.
862 */
Alan Viverettebe463f22016-01-21 10:50:10 -0500863 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 public View focusSearch(View focused, int direction) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -0700865 if (isRootNamespace()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 // root namespace means we should consider ourselves the top of the
867 // tree for focus searching; otherwise we could be focus searching
868 // into other tabs. see LocalActivityManager and TabHost for more info
869 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
870 } else if (mParent != null) {
871 return mParent.focusSearch(focused, direction);
872 }
873 return null;
874 }
875
Alan Viverettebe463f22016-01-21 10:50:10 -0500876 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
878 return false;
879 }
880
Svetoslav Ganov42138042012-03-20 11:51:39 -0700881 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700882 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -0700883 ViewParent parent = mParent;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700884 if (parent == null) {
885 return false;
886 }
887 final boolean propagate = onRequestSendAccessibilityEvent(child, event);
888 if (!propagate) {
889 return false;
890 }
891 return parent.requestSendAccessibilityEvent(this, event);
892 }
893
894 /**
895 * Called when a child has requested sending an {@link AccessibilityEvent} and
896 * gives an opportunity to its parent to augment the event.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700897 * <p>
Adam Powell2fcbbd02011-09-28 18:56:43 -0700898 * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling
899 * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its
900 * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)}
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700901 * is responsible for handling this call.
902 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700903 *
904 * @param child The child which requests sending the event.
905 * @param event The event to be sent.
906 * @return True if the event should be sent.
907 *
908 * @see #requestSendAccessibilityEvent(View, AccessibilityEvent)
909 */
910 public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700911 if (mAccessibilityDelegate != null) {
912 return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event);
913 } else {
914 return onRequestSendAccessibilityEventInternal(child, event);
915 }
916 }
917
918 /**
919 * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent)
920 *
921 * Note: Called from the default {@link View.AccessibilityDelegate}.
Alan Viverettea54956a2015-01-07 16:05:02 -0800922 *
923 * @hide
Svetoslav Ganov031d9c12011-09-09 16:41:13 -0700924 */
Alan Viverettea54956a2015-01-07 16:05:02 -0800925 public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700926 return true;
927 }
928
929 /**
Adam Powell539ee872012-02-03 19:00:49 -0800930 * Called when a child view has changed whether or not it is tracking transient state.
Adam Powell539ee872012-02-03 19:00:49 -0800931 */
Alan Viverettebe463f22016-01-21 10:50:10 -0500932 @Override
Adam Powell539ee872012-02-03 19:00:49 -0800933 public void childHasTransientStateChanged(View child, boolean childHasTransientState) {
934 final boolean oldHasTransientState = hasTransientState();
935 if (childHasTransientState) {
936 mChildCountWithTransientState++;
937 } else {
938 mChildCountWithTransientState--;
939 }
940
941 final boolean newHasTransientState = hasTransientState();
942 if (mParent != null && oldHasTransientState != newHasTransientState) {
943 try {
944 mParent.childHasTransientStateChanged(this, newHasTransientState);
945 } catch (AbstractMethodError e) {
946 Log.e(TAG, mParent.getClass().getSimpleName() +
947 " does not fully implement ViewParent", e);
948 }
949 }
950 }
951
Adam Powell539ee872012-02-03 19:00:49 -0800952 @Override
953 public boolean hasTransientState() {
954 return mChildCountWithTransientState > 0 || super.hasTransientState();
955 }
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 @Override
958 public boolean dispatchUnhandledMove(View focused, int direction) {
959 return mFocused != null &&
960 mFocused.dispatchUnhandledMove(focused, direction);
961 }
962
Alan Viverettebe463f22016-01-21 10:50:10 -0500963 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 public void clearChildFocus(View child) {
965 if (DBG) {
966 System.out.println(this + " clearChildFocus()");
967 }
968
969 mFocused = null;
970 if (mParent != null) {
971 mParent.clearChildFocus(this);
972 }
973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 @Override
976 public void clearFocus() {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800977 if (DBG) {
978 System.out.println(this + " clearFocus()");
979 }
980 if (mFocused == null) {
981 super.clearFocus();
982 } else {
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700983 View focused = mFocused;
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800984 mFocused = null;
Svetoslav Ganovb552d892012-06-02 14:35:02 -0700985 focused.clearFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
987 }
988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 @Override
Alan Viverette223622a2013-12-17 13:29:02 -0800990 void unFocus(View focused) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 if (DBG) {
992 System.out.println(this + " unFocus()");
993 }
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800994 if (mFocused == null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800995 super.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800996 } else {
Alan Viverette223622a2013-12-17 13:29:02 -0800997 mFocused.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -0800998 mFocused = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 /**
1003 * Returns the focused child of this view, if any. The child may have focus
1004 * or contain focus.
1005 *
1006 * @return the focused child or null.
1007 */
1008 public View getFocusedChild() {
1009 return mFocused;
1010 }
1011
Adam Powell88c11752014-07-21 17:19:16 -07001012 View getDeepestFocusedChild() {
1013 View v = this;
1014 while (v != null) {
1015 if (v.isFocused()) {
1016 return v;
1017 }
1018 v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null;
1019 }
1020 return null;
1021 }
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 /**
1024 * Returns true if this view has or contains focus
1025 *
1026 * @return true if this view has or contains focus
1027 */
1028 @Override
1029 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001030 return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032
1033 /*
1034 * (non-Javadoc)
1035 *
1036 * @see android.view.View#findFocus()
1037 */
1038 @Override
1039 public View findFocus() {
1040 if (DBG) {
1041 System.out.println("Find focus in " + this + ": flags="
1042 + isFocused() + ", child=" + mFocused);
1043 }
1044
1045 if (isFocused()) {
1046 return this;
1047 }
1048
1049 if (mFocused != null) {
1050 return mFocused.findFocus();
1051 }
1052 return null;
1053 }
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 @Override
1056 public boolean hasFocusable() {
1057 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
1058 return false;
1059 }
1060
1061 if (isFocusable()) {
1062 return true;
1063 }
1064
1065 final int descendantFocusability = getDescendantFocusability();
Adam Powell88c11752014-07-21 17:19:16 -07001066 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 final int count = mChildrenCount;
1068 final View[] children = mChildren;
1069
1070 for (int i = 0; i < count; i++) {
1071 final View child = children[i];
1072 if (child.hasFocusable()) {
1073 return true;
1074 }
1075 }
1076 }
1077
1078 return false;
1079 }
1080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001082 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 final int focusableCount = views.size();
1084
1085 final int descendantFocusability = getDescendantFocusability();
1086
Adam Powell88c11752014-07-21 17:19:16 -07001087 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
1088 if (shouldBlockFocusForTouchscreen()) {
1089 focusableMode |= FOCUSABLES_TOUCH_MODE;
1090 }
1091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 final int count = mChildrenCount;
1093 final View[] children = mChildren;
1094
1095 for (int i = 0; i < count; i++) {
1096 final View child = children[i];
1097 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001098 child.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 }
1101 }
1102
1103 // we add ourselves (if focusable) in all cases except for when we are
1104 // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is
1105 // to avoid the focus search finding layouts when a more precise search
1106 // among the focusable children would be more interesting.
Adam Powellff0d2982014-07-10 20:34:14 -07001107 if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 // No focusable descendants
Adam Powell88c11752014-07-21 17:19:16 -07001109 || (focusableCount == views.size())) &&
1110 (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001111 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113 }
1114
Adam Powellff0d2982014-07-10 20:34:14 -07001115 /**
1116 * Set whether this ViewGroup should ignore focus requests for itself and its children.
1117 * If this option is enabled and the ViewGroup or a descendant currently has focus, focus
1118 * will proceed forward.
1119 *
1120 * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen
1121 */
1122 public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {
1123 if (touchscreenBlocksFocus) {
1124 mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1125 if (hasFocus()) {
Adam Powell88c11752014-07-21 17:19:16 -07001126 final View focusedChild = getDeepestFocusedChild();
1127 if (!focusedChild.isFocusableInTouchMode()) {
1128 final View newFocus = focusSearch(FOCUS_FORWARD);
1129 if (newFocus != null) {
1130 newFocus.requestFocus();
1131 }
Adam Powellff0d2982014-07-10 20:34:14 -07001132 }
1133 }
1134 } else {
1135 mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1136 }
1137 }
1138
1139 /**
1140 * Check whether this ViewGroup should ignore focus requests for itself and its children.
1141 */
1142 public boolean getTouchscreenBlocksFocus() {
1143 return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0;
1144 }
1145
1146 boolean shouldBlockFocusForTouchscreen() {
1147 return getTouchscreenBlocksFocus() &&
1148 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
1149 }
1150
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001151 @Override
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001152 public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
1153 super.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001154 final int childrenCount = mChildrenCount;
1155 final View[] children = mChildren;
1156 for (int i = 0; i < childrenCount; i++) {
1157 View child = children[i];
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001158 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
Dianne Hackborn4702a852012-08-17 15:18:29 -07001159 && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001160 child.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001161 }
1162 }
1163 }
1164
Svetoslav5b578da2013-05-08 14:23:32 -07001165 /** @hide */
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001166 @Override
Svetoslav5b578da2013-05-08 14:23:32 -07001167 public View findViewByAccessibilityIdTraversal(int accessibilityId) {
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001168 View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId);
1169 if (foundView != null) {
1170 return foundView;
1171 }
Svetoslav6cc46272015-06-03 11:38:30 -07001172
1173 if (getAccessibilityNodeProvider() != null) {
1174 return null;
1175 }
1176
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001177 final int childrenCount = mChildrenCount;
1178 final View[] children = mChildren;
1179 for (int i = 0; i < childrenCount; i++) {
1180 View child = children[i];
1181 foundView = child.findViewByAccessibilityIdTraversal(accessibilityId);
1182 if (foundView != null) {
1183 return foundView;
1184 }
1185 }
Svetoslav6cc46272015-06-03 11:38:30 -07001186
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001187 return null;
1188 }
1189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 @Override
1191 public void dispatchWindowFocusChanged(boolean hasFocus) {
1192 super.dispatchWindowFocusChanged(hasFocus);
1193 final int count = mChildrenCount;
1194 final View[] children = mChildren;
1195 for (int i = 0; i < count; i++) {
1196 children[i].dispatchWindowFocusChanged(hasFocus);
1197 }
1198 }
1199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 @Override
1201 public void addTouchables(ArrayList<View> views) {
1202 super.addTouchables(views);
1203
1204 final int count = mChildrenCount;
1205 final View[] children = mChildren;
1206
1207 for (int i = 0; i < count; i++) {
1208 final View child = children[i];
1209 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1210 child.addTouchables(views);
1211 }
1212 }
1213 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001214
1215 /**
1216 * @hide
1217 */
1218 @Override
1219 public void makeOptionalFitsSystemWindows() {
1220 super.makeOptionalFitsSystemWindows();
1221 final int count = mChildrenCount;
1222 final View[] children = mChildren;
1223 for (int i = 0; i < count; i++) {
1224 children[i].makeOptionalFitsSystemWindows();
1225 }
1226 }
1227
Romain Guy43c9cdf2010-01-27 13:53:55 -08001228 @Override
1229 public void dispatchDisplayHint(int hint) {
1230 super.dispatchDisplayHint(hint);
1231 final int count = mChildrenCount;
1232 final View[] children = mChildren;
1233 for (int i = 0; i < count; i++) {
1234 children[i].dispatchDisplayHint(hint);
1235 }
1236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237
1238 /**
Chet Haase0d299362012-01-26 10:51:48 -08001239 * Called when a view's visibility has changed. Notify the parent to take any appropriate
1240 * action.
1241 *
1242 * @param child The view whose visibility has changed
1243 * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
1244 * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
Chet Haase5e25c2c2010-09-16 11:15:56 -07001245 * @hide
Chet Haase5e25c2c2010-09-16 11:15:56 -07001246 */
Chet Haase0d299362012-01-26 10:51:48 -08001247 protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07001248 if (mTransition != null) {
Chet Haase0d299362012-01-26 10:51:48 -08001249 if (newVisibility == VISIBLE) {
1250 mTransition.showChild(this, child, oldVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001251 } else {
Chet Haase0d299362012-01-26 10:51:48 -08001252 mTransition.hideChild(this, child, newVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001253 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
Chet Haaseddbb3462012-06-19 13:54:29 -07001254 // Only track this on disappearing views - appearing views are already visible
1255 // and don't need special handling during drawChild()
1256 if (mVisibilityChangingChildren == null) {
1257 mVisibilityChangingChildren = new ArrayList<View>();
1258 }
1259 mVisibilityChangingChildren.add(child);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001260 addDisappearingView(child);
1261 }
1262 }
1263 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001264
1265 // in all cases, for drags
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001266 if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) {
1267 if (!mChildrenInterestedInDrag.contains(child)) {
1268 notifyChildOfDragStart(child);
Christopher Tate86cab1b2011-01-13 20:28:55 -08001269 }
1270 }
Chet Haase5e25c2c2010-09-16 11:15:56 -07001271 }
1272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 @Override
Adam Powell326d8082009-12-09 15:10:07 -08001274 protected void dispatchVisibilityChanged(View changedView, int visibility) {
1275 super.dispatchVisibilityChanged(changedView, visibility);
1276 final int count = mChildrenCount;
1277 final View[] children = mChildren;
1278 for (int i = 0; i < count; i++) {
1279 children[i].dispatchVisibilityChanged(changedView, visibility);
1280 }
1281 }
1282
Adam Powell326d8082009-12-09 15:10:07 -08001283 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public void dispatchWindowVisibilityChanged(int visibility) {
1285 super.dispatchWindowVisibilityChanged(visibility);
1286 final int count = mChildrenCount;
1287 final View[] children = mChildren;
1288 for (int i = 0; i < count; i++) {
1289 children[i].dispatchWindowVisibilityChanged(visibility);
1290 }
1291 }
1292
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001293 @Override
Adam Powell9c146bf2016-03-15 17:35:00 -07001294 boolean dispatchVisibilityAggregated(boolean isVisible) {
1295 isVisible = super.dispatchVisibilityAggregated(isVisible);
Adam Powell41d96902016-03-15 14:43:19 -07001296 final int count = mChildrenCount;
1297 final View[] children = mChildren;
1298 for (int i = 0; i < count; i++) {
Adam Powell9c146bf2016-03-15 17:35:00 -07001299 // Only dispatch to visible children. Not visible children and their subtrees already
1300 // know that they aren't visible and that's not going to change as a result of
1301 // whatever triggered this dispatch.
1302 if (children[i].getVisibility() == VISIBLE) {
1303 children[i].dispatchVisibilityAggregated(isVisible);
Adam Powell41d96902016-03-15 14:43:19 -07001304 }
1305 }
Adam Powell9c146bf2016-03-15 17:35:00 -07001306 return isVisible;
Adam Powell41d96902016-03-15 14:43:19 -07001307 }
1308
1309 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001310 public void dispatchConfigurationChanged(Configuration newConfig) {
1311 super.dispatchConfigurationChanged(newConfig);
1312 final int count = mChildrenCount;
1313 final View[] children = mChildren;
1314 for (int i = 0; i < count; i++) {
1315 children[i].dispatchConfigurationChanged(newConfig);
1316 }
1317 }
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08001318
Alan Viverettebe463f22016-01-21 10:50:10 -05001319 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -08001321 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1322 ViewParent parent = mParent;
1323 if (parent != null) parent.recomputeViewAttributes(this);
1324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
Romain Guy8506ab42009-06-11 17:35:47 -07001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001328 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1329 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1330 super.dispatchCollectViewAttributes(attachInfo, visibility);
1331 final int count = mChildrenCount;
1332 final View[] children = mChildren;
1333 for (int i = 0; i < count; i++) {
1334 final View child = children[i];
1335 child.dispatchCollectViewAttributes(attachInfo,
1336 visibility | (child.mViewFlags&VISIBILITY_MASK));
1337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 }
1340
Alan Viverettebe463f22016-01-21 10:50:10 -05001341 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 public void bringChildToFront(View child) {
Alan Viverette77bb6f12015-02-11 17:24:33 -08001343 final int index = indexOfChild(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (index >= 0) {
1345 removeFromArray(index);
1346 addInArray(child, mChildrenCount);
1347 child.mParent = this;
Chet Haasecb96db82013-09-04 10:21:46 -07001348 requestLayout();
1349 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
1352
Romain Guy6410c0a2013-06-17 11:21:58 -07001353 private PointF getLocalPoint() {
1354 if (mLocalPoint == null) mLocalPoint = new PointF();
1355 return mLocalPoint;
1356 }
1357
Steve Block8a7259b2012-03-01 11:24:41 +00001358 // TODO: Write real docs
Christopher Tatea53146c2010-09-07 11:57:52 -07001359 @Override
1360 public boolean dispatchDragEvent(DragEvent event) {
1361 boolean retval = false;
1362 final float tx = event.mX;
1363 final float ty = event.mY;
1364
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07001365 ViewRootImpl root = getViewRootImpl();
Christopher Tatea53146c2010-09-07 11:57:52 -07001366
1367 // Dispatch down the view hierarchy
Romain Guy6410c0a2013-06-17 11:21:58 -07001368 final PointF localPoint = getLocalPoint();
1369
Christopher Tatea53146c2010-09-07 11:57:52 -07001370 switch (event.mAction) {
1371 case DragEvent.ACTION_DRAG_STARTED: {
1372 // clear state to recalculate which views we drag over
Chris Tate9d1ab882010-11-02 15:55:39 -07001373 mCurrentDragView = null;
Christopher Tatea53146c2010-09-07 11:57:52 -07001374
Christopher Tate86cab1b2011-01-13 20:28:55 -08001375 // Set up our tracking of drag-started notifications
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001376 mCurrentDragStartEvent = DragEvent.obtain(event);
1377 if (mChildrenInterestedInDrag == null) {
1378 mChildrenInterestedInDrag = new HashSet<View>();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001379 } else {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001380 mChildrenInterestedInDrag.clear();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001381 }
1382
Christopher Tatea53146c2010-09-07 11:57:52 -07001383 // Now dispatch down to our children, caching the responses
Christopher Tatea53146c2010-09-07 11:57:52 -07001384 final int count = mChildrenCount;
1385 final View[] children = mChildren;
1386 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001387 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001388 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001389 if (child.getVisibility() == VISIBLE) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001390 if (notifyChildOfDragStart(children[i])) {
1391 retval = true;
Christopher Tate2c095f32010-10-04 14:13:40 -07001392 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001393 }
1394 }
1395
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001396 // Notify itself of the drag start.
1397 mIsInterestedInDrag = super.dispatchDragEvent(event);
1398 if (mIsInterestedInDrag) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001399 retval = true;
1400 }
Yorke Lee0394b212016-03-10 18:20:04 -08001401
1402 if (!retval) {
1403 // Neither us nor any of our children are interested in this drag, so stop tracking
1404 // the current drag event.
1405 mCurrentDragStartEvent.recycle();
1406 mCurrentDragStartEvent = null;
1407 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001408 } break;
1409
1410 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001411 // Release the bookkeeping now that the drag lifecycle has ended
Vadim Tryshev15177892015-12-18 08:01:00 -08001412 final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag;
1413 if (childrenInterestedInDrag != null) {
1414 for (View child : childrenInterestedInDrag) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001415 // If a child was interested in the ongoing drag, it's told that it's over
1416 if (child.dispatchDragEvent(event)) {
1417 retval = true;
1418 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001419 child.mPrivateFlags2 &= ~View.DRAG_MASK;
1420 child.refreshDrawableState();
Christopher Tate1fc014f2011-01-19 12:56:26 -08001421 }
Vadim Tryshev15177892015-12-18 08:01:00 -08001422 childrenInterestedInDrag.clear();
1423 }
1424 if (mCurrentDragStartEvent != null) {
1425 mCurrentDragStartEvent.recycle();
1426 mCurrentDragStartEvent = null;
Christopher Tate1fc014f2011-01-19 12:56:26 -08001427 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001428
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001429 if (mIsInterestedInDrag) {
1430 if (super.dispatchDragEvent(event)) {
1431 retval = true;
1432 }
1433 mIsInterestedInDrag = false;
Christopher Tatea53146c2010-09-07 11:57:52 -07001434 }
1435 } break;
1436
1437 case DragEvent.ACTION_DRAG_LOCATION: {
1438 // Find the [possibly new] drag target
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001439 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
1440 if (target == null && mIsInterestedInDrag) {
1441 target = this;
1442 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001443
1444 // If we've changed apparent drag target, tell the view root which view
Chris Tate9d1ab882010-11-02 15:55:39 -07001445 // we're over now [for purposes of the eventual drag-recipient-changed
1446 // notifications to the framework] and tell the new target that the drag
1447 // has entered its bounds. The root will see setDragFocus() calls all
1448 // the way down to the final leaf view that is handling the LOCATION event
1449 // before reporting the new potential recipient to the framework.
Christopher Tatea53146c2010-09-07 11:57:52 -07001450 if (mCurrentDragView != target) {
Chris Tate9d1ab882010-11-02 15:55:39 -07001451 root.setDragFocus(target);
1452
1453 final int action = event.mAction;
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001454 // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED.
1455 event.mX = 0;
1456 event.mY = 0;
1457
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001458 // If we've dragged off of a child view or this window, send it the EXITED message
Chris Tate9d1ab882010-11-02 15:55:39 -07001459 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001460 final View view = mCurrentDragView;
Chris Tate9d1ab882010-11-02 15:55:39 -07001461 event.mAction = DragEvent.ACTION_DRAG_EXITED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001462 if (view != this) {
1463 view.dispatchDragEvent(event);
1464 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1465 view.refreshDrawableState();
1466 } else {
1467 super.dispatchDragEvent(event);
1468 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001469 }
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001470
Christopher Tatea53146c2010-09-07 11:57:52 -07001471 mCurrentDragView = target;
Chris Tate9d1ab882010-11-02 15:55:39 -07001472
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001473 // If we've dragged over a new child view, send it the ENTERED message, otherwise
1474 // send it to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001475 if (target != null) {
1476 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001477 if (target != this) {
1478 target.dispatchDragEvent(event);
1479 target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
1480 target.refreshDrawableState();
1481 } else {
1482 super.dispatchDragEvent(event);
1483 }
Chris Tate9d1ab882010-11-02 15:55:39 -07001484 }
1485 event.mAction = action; // restore the event's original state
Vladislav Kaznacheevcc010b22016-01-21 16:24:40 -08001486 event.mX = tx;
1487 event.mY = ty;
Christopher Tatea53146c2010-09-07 11:57:52 -07001488 }
Christopher Tate2c095f32010-10-04 14:13:40 -07001489
Christopher Tatea53146c2010-09-07 11:57:52 -07001490 // Dispatch the actual drag location notice, localized into its coordinates
1491 if (target != null) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001492 if (target != this) {
1493 event.mX = localPoint.x;
1494 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001495
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001496 retval = target.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001497
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001498 event.mX = tx;
1499 event.mY = ty;
1500 } else {
1501 retval = super.dispatchDragEvent(event);
1502 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001503 }
1504 } break;
1505
Chris Tate9d1ab882010-11-02 15:55:39 -07001506 /* Entered / exited dispatch
1507 *
1508 * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is
1509 * that we're about to get the corresponding LOCATION event, which we will use to
1510 * determine which of our children is the new target; at that point we will
1511 * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001512 * If no suitable child is detected, dispatch to this window.
Chris Tate9d1ab882010-11-02 15:55:39 -07001513 *
1514 * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
1515 * drag has left this ViewGroup, we know by definition that every contained subview
1516 * is also no longer under the drag point.
1517 */
1518
1519 case DragEvent.ACTION_DRAG_EXITED: {
1520 if (mCurrentDragView != null) {
Christopher Tate3d4bf172011-03-28 16:16:46 -07001521 final View view = mCurrentDragView;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001522 if (view != this) {
1523 view.dispatchDragEvent(event);
1524 view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
1525 view.refreshDrawableState();
1526 } else {
1527 super.dispatchDragEvent(event);
1528 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001529
Chris Tate9d1ab882010-11-02 15:55:39 -07001530 mCurrentDragView = null;
1531 }
1532 } break;
1533
Christopher Tatea53146c2010-09-07 11:57:52 -07001534 case DragEvent.ACTION_DROP: {
Christopher Tate2c095f32010-10-04 14:13:40 -07001535 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
Romain Guy6410c0a2013-06-17 11:21:58 -07001536 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
Christopher Tatea53146c2010-09-07 11:57:52 -07001537 if (target != null) {
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001538 if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target);
Romain Guy6410c0a2013-06-17 11:21:58 -07001539 event.mX = localPoint.x;
1540 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001541 retval = target.dispatchDragEvent(event);
1542 event.mX = tx;
1543 event.mY = ty;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001544 } else if (mIsInterestedInDrag) {
1545 retval = super.dispatchDragEvent(event);
Christopher Tate5ada6cb2010-10-05 14:15:29 -07001546 } else {
1547 if (ViewDebug.DEBUG_DRAG) {
1548 Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view");
1549 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001550 }
1551 } break;
1552 }
1553
Christopher Tatea53146c2010-09-07 11:57:52 -07001554 return retval;
1555 }
1556
1557 // Find the frontmost child view that lies under the given point, and calculate
1558 // the position within its own local coordinate system.
1559 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001560 final int count = mChildrenCount;
1561 final View[] children = mChildren;
1562 for (int i = count - 1; i >= 0; i--) {
1563 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001564 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001565 continue;
1566 }
1567
Christopher Tate2c095f32010-10-04 14:13:40 -07001568 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001569 return child;
1570 }
1571 }
1572 return null;
1573 }
1574
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001575 boolean notifyChildOfDragStart(View child) {
1576 // The caller guarantees that the child is not in mChildrenInterestedInDrag yet.
1577
Christopher Tate86cab1b2011-01-13 20:28:55 -08001578 if (ViewDebug.DEBUG_DRAG) {
1579 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1580 }
1581
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001582 final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent);
1583 if (canAccept) {
1584 mChildrenInterestedInDrag.add(child);
1585 if (!child.canAcceptDrag()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001586 child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001587 child.refreshDrawableState();
1588 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001589 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001590 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001591 }
1592
Joe Onorato664644d2011-01-23 17:53:23 -08001593 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001594 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1595 super.dispatchWindowSystemUiVisiblityChanged(visible);
1596
1597 final int count = mChildrenCount;
1598 final View[] children = mChildren;
1599 for (int i=0; i <count; i++) {
1600 final View child = children[i];
1601 child.dispatchWindowSystemUiVisiblityChanged(visible);
1602 }
1603 }
1604
1605 @Override
Joe Onorato664644d2011-01-23 17:53:23 -08001606 public void dispatchSystemUiVisibilityChanged(int visible) {
1607 super.dispatchSystemUiVisibilityChanged(visible);
1608
1609 final int count = mChildrenCount;
1610 final View[] children = mChildren;
1611 for (int i=0; i <count; i++) {
1612 final View child = children[i];
1613 child.dispatchSystemUiVisibilityChanged(visible);
1614 }
1615 }
1616
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001617 @Override
Dianne Hackborncf675782012-05-10 15:07:24 -07001618 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
1619 boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001620
1621 final int count = mChildrenCount;
1622 final View[] children = mChildren;
1623 for (int i=0; i <count; i++) {
1624 final View child = children[i];
Dianne Hackborncf675782012-05-10 15:07:24 -07001625 changed |= child.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001626 }
Dianne Hackborncf675782012-05-10 15:07:24 -07001627 return changed;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001628 }
1629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 @Override
1631 public boolean dispatchKeyEventPreIme(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001632 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1633 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 return super.dispatchKeyEventPreIme(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001635 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1636 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 return mFocused.dispatchKeyEventPreIme(event);
1638 }
1639 return false;
1640 }
1641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 @Override
1643 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001644 if (mInputEventConsistencyVerifier != null) {
1645 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1646 }
1647
Dianne Hackborn4702a852012-08-17 15:18:29 -07001648 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1649 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001650 if (super.dispatchKeyEvent(event)) {
1651 return true;
1652 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001653 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1654 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001655 if (mFocused.dispatchKeyEvent(event)) {
1656 return true;
1657 }
1658 }
1659
1660 if (mInputEventConsistencyVerifier != null) {
1661 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 }
1663 return false;
1664 }
1665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 @Override
1667 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001668 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1669 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 return super.dispatchKeyShortcutEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001671 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1672 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 return mFocused.dispatchKeyShortcutEvent(event);
1674 }
1675 return false;
1676 }
1677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 @Override
1679 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001680 if (mInputEventConsistencyVerifier != null) {
1681 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1682 }
1683
Dianne Hackborn4702a852012-08-17 15:18:29 -07001684 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1685 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001686 if (super.dispatchTrackballEvent(event)) {
1687 return true;
1688 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001689 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1690 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001691 if (mFocused.dispatchTrackballEvent(event)) {
1692 return true;
1693 }
1694 }
1695
1696 if (mInputEventConsistencyVerifier != null) {
1697 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699 return false;
1700 }
1701
Jun Mukai1db53972015-09-11 18:08:31 -07001702 @Override
Jun Mukaid4eaef72015-10-30 15:54:33 -07001703 public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09001704 if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) {
1705 return PointerIcon.getSystemIcon(mContext, PointerIcon.STYLE_ARROW);
1706 }
Jun Mukai1db53972015-09-11 18:08:31 -07001707 // Check what the child under the pointer says about the pointer.
1708 final int childrenCount = mChildrenCount;
1709 if (childrenCount != 0) {
1710 final ArrayList<View> preorderedList = buildOrderedChildList();
1711 final boolean customOrder = preorderedList == null
1712 && isChildrenDrawingOrderEnabled();
1713 final View[] children = mChildren;
1714 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05001715 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
1716 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
1717 final PointF point = getLocalPoint();
Jun Mukai1db53972015-09-11 18:08:31 -07001718 if (isTransformedTouchPointInView(x, y, child, point)) {
Jun Mukaid4eaef72015-10-30 15:54:33 -07001719 final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y);
1720 if (pointerIcon != null) {
1721 return pointerIcon;
Jun Mukai1db53972015-09-11 18:08:31 -07001722 }
1723 break;
1724 }
1725 }
1726 }
1727
1728 // The pointer is not a child or the child has no preferences, returning the default
1729 // implementation.
Jun Mukaid4eaef72015-10-30 15:54:33 -07001730 return super.getPointerIcon(event, x, y);
Jun Mukai1db53972015-09-11 18:08:31 -07001731 }
1732
Alan Viverettea7b85e62016-01-22 10:14:02 -05001733 private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {
1734 final int childIndex;
1735 if (customOrder) {
1736 final int childIndex1 = getChildDrawingOrder(childrenCount, i);
1737 if (childIndex1 >= childrenCount) {
1738 throw new IndexOutOfBoundsException("getChildDrawingOrder() "
1739 + "returned invalid index " + childIndex1
1740 + " (child count is " + childrenCount + ")");
1741 }
1742 childIndex = childIndex1;
1743 } else {
1744 childIndex = i;
1745 }
1746 return childIndex;
1747 }
1748
Romain Guya9489272011-06-22 20:58:11 -07001749 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08001751 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08001752 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08001753
Jeff Brown10b62902011-06-20 16:40:37 -07001754 // First check whether the view group wants to intercept the hover event.
1755 final boolean interceptHover = onInterceptHoverEvent(event);
1756 event.setAction(action); // restore action in case it was changed
1757
Jeff Brown87b7f802011-06-21 18:35:45 -07001758 MotionEvent eventNoHistory = event;
1759 boolean handled = false;
1760
1761 // Send events to the hovered children and build a new list of hover targets until
1762 // one is found that handles the event.
1763 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
1764 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07001765 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08001766 final float x = event.getX();
1767 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001768 final int childrenCount = mChildrenCount;
1769 if (childrenCount != 0) {
Chris Craikab008f02014-05-23 17:55:03 -07001770 final ArrayList<View> preorderedList = buildOrderedChildList();
1771 final boolean customOrder = preorderedList == null
1772 && isChildrenDrawingOrderEnabled();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001773 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07001774 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001775 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05001776 final int childIndex = getAndVerifyPreorderedIndex(
1777 childrenCount, i, customOrder);
1778 final View child = getAndVerifyPreorderedView(
1779 preorderedList, children, childIndex);
Jeff Brown87b7f802011-06-21 18:35:45 -07001780 if (!canViewReceivePointerEvents(child)
1781 || !isTransformedTouchPointInView(x, y, child, null)) {
1782 continue;
1783 }
1784
1785 // Obtain a hover target for this child. Dequeue it from the
1786 // old hover target list if the child was previously hovered.
1787 HoverTarget hoverTarget = firstOldHoverTarget;
1788 final boolean wasHovered;
1789 for (HoverTarget predecessor = null; ;) {
1790 if (hoverTarget == null) {
1791 hoverTarget = HoverTarget.obtain(child);
1792 wasHovered = false;
1793 break;
1794 }
1795
1796 if (hoverTarget.child == child) {
1797 if (predecessor != null) {
1798 predecessor.next = hoverTarget.next;
1799 } else {
1800 firstOldHoverTarget = hoverTarget.next;
1801 }
1802 hoverTarget.next = null;
1803 wasHovered = true;
1804 break;
1805 }
1806
1807 predecessor = hoverTarget;
1808 hoverTarget = hoverTarget.next;
1809 }
1810
1811 // Enqueue the hover target onto the new hover target list.
1812 if (lastHoverTarget != null) {
1813 lastHoverTarget.next = hoverTarget;
1814 } else {
Jeff Brown87b7f802011-06-21 18:35:45 -07001815 mFirstHoverTarget = hoverTarget;
1816 }
Sangkyu Lee8725f362013-03-13 09:38:45 +09001817 lastHoverTarget = hoverTarget;
Jeff Brown87b7f802011-06-21 18:35:45 -07001818
1819 // Dispatch the event to the child.
1820 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1821 if (!wasHovered) {
1822 // Send the enter as is.
1823 handled |= dispatchTransformedGenericPointerEvent(
1824 event, child); // enter
1825 }
1826 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1827 if (!wasHovered) {
1828 // Synthesize an enter from a move.
1829 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1830 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1831 handled |= dispatchTransformedGenericPointerEvent(
1832 eventNoHistory, child); // enter
1833 eventNoHistory.setAction(action);
1834
1835 handled |= dispatchTransformedGenericPointerEvent(
1836 eventNoHistory, child); // move
1837 } else {
1838 // Send the move as is.
1839 handled |= dispatchTransformedGenericPointerEvent(event, child);
1840 }
1841 }
1842 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07001843 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001844 }
Jeff Brown10b62902011-06-20 16:40:37 -07001845 }
Chris Craikab008f02014-05-23 17:55:03 -07001846 if (preorderedList != null) preorderedList.clear();
Jeff Brown10b62902011-06-20 16:40:37 -07001847 }
1848 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001849
Jeff Brown87b7f802011-06-21 18:35:45 -07001850 // Send exit events to all previously hovered children that are no longer hovered.
1851 while (firstOldHoverTarget != null) {
1852 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07001853
Jeff Brown87b7f802011-06-21 18:35:45 -07001854 // Exit the old hovered child.
1855 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1856 // Send the exit as is.
1857 handled |= dispatchTransformedGenericPointerEvent(
1858 event, child); // exit
1859 } else {
1860 // Synthesize an exit from a move or enter.
1861 // Ignore the result because hover focus has moved to a different view.
1862 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brown10b62902011-06-20 16:40:37 -07001863 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07001864 event, child); // move
Jeff Brown10b62902011-06-20 16:40:37 -07001865 }
Jeff Brown87b7f802011-06-21 18:35:45 -07001866 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1867 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1868 dispatchTransformedGenericPointerEvent(
1869 eventNoHistory, child); // exit
1870 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07001871 }
1872
Jeff Brown87b7f802011-06-21 18:35:45 -07001873 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
1874 firstOldHoverTarget.recycle();
1875 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07001876 }
1877
Jeff Brown87b7f802011-06-21 18:35:45 -07001878 // Send events to the view group itself if no children have handled it.
Jeff Brown10b62902011-06-20 16:40:37 -07001879 boolean newHoveredSelf = !handled;
1880 if (newHoveredSelf == mHoveredSelf) {
1881 if (newHoveredSelf) {
1882 // Send event to the view group as before.
1883 handled |= super.dispatchHoverEvent(event);
1884 }
1885 } else {
1886 if (mHoveredSelf) {
1887 // Exit the view group.
1888 if (action == MotionEvent.ACTION_HOVER_EXIT) {
1889 // Send the exit as is.
1890 handled |= super.dispatchHoverEvent(event); // exit
1891 } else {
1892 // Synthesize an exit from a move or enter.
1893 // Ignore the result because hover focus is moving to a different view.
1894 if (action == MotionEvent.ACTION_HOVER_MOVE) {
1895 super.dispatchHoverEvent(event); // move
1896 }
1897 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1898 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
1899 super.dispatchHoverEvent(eventNoHistory); // exit
1900 eventNoHistory.setAction(action);
1901 }
1902 mHoveredSelf = false;
1903 }
1904
1905 if (newHoveredSelf) {
1906 // Enter the view group.
1907 if (action == MotionEvent.ACTION_HOVER_ENTER) {
1908 // Send the enter as is.
1909 handled |= super.dispatchHoverEvent(event); // enter
1910 mHoveredSelf = true;
1911 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
1912 // Synthesize an enter from a move.
1913 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
1914 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
1915 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
1916 eventNoHistory.setAction(action);
1917
1918 handled |= super.dispatchHoverEvent(eventNoHistory); // move
1919 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001920 }
1921 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001922 }
1923
Jeff Browna032cc02011-03-07 16:56:21 -08001924 // Recycle the copy of the event that we made.
1925 if (eventNoHistory != event) {
1926 eventNoHistory.recycle();
1927 }
1928
Jeff Browna032cc02011-03-07 16:56:21 -08001929 // Done.
1930 return handled;
1931 }
1932
Jeff Brown59a422e2012-04-19 15:19:19 -07001933 private void exitHoverTargets() {
1934 if (mHoveredSelf || mFirstHoverTarget != null) {
1935 final long now = SystemClock.uptimeMillis();
1936 MotionEvent event = MotionEvent.obtain(now, now,
1937 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1938 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1939 dispatchHoverEvent(event);
1940 event.recycle();
1941 }
1942 }
1943
1944 private void cancelHoverTarget(View view) {
1945 HoverTarget predecessor = null;
1946 HoverTarget target = mFirstHoverTarget;
1947 while (target != null) {
1948 final HoverTarget next = target.next;
1949 if (target.child == view) {
1950 if (predecessor == null) {
1951 mFirstHoverTarget = next;
1952 } else {
1953 predecessor.next = next;
1954 }
1955 target.recycle();
1956
1957 final long now = SystemClock.uptimeMillis();
1958 MotionEvent event = MotionEvent.obtain(now, now,
1959 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
1960 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
1961 view.dispatchHoverEvent(event);
1962 event.recycle();
1963 return;
1964 }
1965 predecessor = target;
1966 target = next;
1967 }
1968 }
1969
Jeff Brown87b7f802011-06-21 18:35:45 -07001970 /** @hide */
1971 @Override
1972 protected boolean hasHoveredChild() {
1973 return mFirstHoverTarget != null;
1974 }
1975
Svetoslav Ganov42138042012-03-20 11:51:39 -07001976 @Override
Adam Powellcf392d12015-06-25 14:48:45 -07001977 public void addChildrenForAccessibility(ArrayList<View> outChildren) {
Svetoslav762621c2015-06-01 17:21:59 -07001978 if (getAccessibilityNodeProvider() != null) {
1979 return;
1980 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001981 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
1982 try {
1983 final int childrenCount = children.getChildCount();
1984 for (int i = 0; i < childrenCount; i++) {
1985 View child = children.getChildAt(i);
Svetoslav Ganovc406be92012-05-11 16:12:32 -07001986 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001987 if (child.includeForAccessibility()) {
Adam Powellcf392d12015-06-25 14:48:45 -07001988 outChildren.add(child);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001989 } else {
Adam Powellcf392d12015-06-25 14:48:45 -07001990 child.addChildrenForAccessibility(outChildren);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001991 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001992 }
1993 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07001994 } finally {
1995 children.recycle();
Svetoslav Ganov42138042012-03-20 11:51:39 -07001996 }
1997 }
1998
1999 /**
Jeff Brown10b62902011-06-20 16:40:37 -07002000 * Implement this method to intercept hover events before they are handled
2001 * by child views.
2002 * <p>
2003 * This method is called before dispatching a hover event to a child of
2004 * the view group or to the view group's own {@link #onHoverEvent} to allow
2005 * the view group a chance to intercept the hover event.
2006 * This method can also be used to watch all pointer motions that occur within
2007 * the bounds of the view group even when the pointer is hovering over
2008 * a child of the view group rather than over the view group itself.
2009 * </p><p>
2010 * The view group can prevent its children from receiving hover events by
2011 * implementing this method and returning <code>true</code> to indicate
2012 * that it would like to intercept hover events. The view group must
2013 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
2014 * for as long as it wishes to continue intercepting hover events from
2015 * its children.
2016 * </p><p>
2017 * Interception preserves the invariant that at most one view can be
2018 * hovered at a time by transferring hover focus from the currently hovered
2019 * child to the view group or vice-versa as needed.
2020 * </p><p>
2021 * If this method returns <code>true</code> and a child is already hovered, then the
2022 * child view will first receive a hover exit event and then the view group
2023 * itself will receive a hover enter event in {@link #onHoverEvent}.
2024 * Likewise, if this method had previously returned <code>true</code> to intercept hover
2025 * events and instead returns <code>false</code> while the pointer is hovering
2026 * within the bounds of one of a child, then the view group will first receive a
2027 * hover exit event in {@link #onHoverEvent} and then the hovered child will
2028 * receive a hover enter event.
2029 * </p><p>
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002030 * The default implementation handles mouse hover on the scroll bars.
Jeff Brown10b62902011-06-20 16:40:37 -07002031 * </p>
2032 *
2033 * @param event The motion event that describes the hover.
2034 * @return True if the view group would like to intercept the hover event
2035 * and prevent its children from receiving it.
2036 */
2037 public boolean onInterceptHoverEvent(MotionEvent event) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002038 if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
2039 final int action = event.getAction();
2040 final float x = event.getX();
2041 final float y = event.getY();
2042 if ((action == MotionEvent.ACTION_HOVER_MOVE
2043 || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) {
2044 return true;
2045 }
2046 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002047 return false;
2048 }
2049
Jeff Browna032cc02011-03-07 16:56:21 -08002050 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
2051 if (event.getHistorySize() == 0) {
2052 return event;
2053 }
2054 return MotionEvent.obtainNoHistory(event);
2055 }
2056
Jeff Browna032cc02011-03-07 16:56:21 -08002057 @Override
2058 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
2059 // Send the event to the child under the pointer.
2060 final int childrenCount = mChildrenCount;
2061 if (childrenCount != 0) {
Jeff Browna032cc02011-03-07 16:56:21 -08002062 final float x = event.getX();
2063 final float y = event.getY();
2064
Chris Craikab008f02014-05-23 17:55:03 -07002065 final ArrayList<View> preorderedList = buildOrderedChildList();
2066 final boolean customOrder = preorderedList == null
2067 && isChildrenDrawingOrderEnabled();
2068 final View[] children = mChildren;
Jeff Browna032cc02011-03-07 16:56:21 -08002069 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002070 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
2071 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Jeff Browna032cc02011-03-07 16:56:21 -08002072 if (!canViewReceivePointerEvents(child)
2073 || !isTransformedTouchPointInView(x, y, child, null)) {
2074 continue;
2075 }
2076
2077 if (dispatchTransformedGenericPointerEvent(event, child)) {
Chris Craikab008f02014-05-23 17:55:03 -07002078 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002079 return true;
2080 }
2081 }
Chris Craikab008f02014-05-23 17:55:03 -07002082 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002083 }
2084
2085 // No child handled the event. Send it to this view group.
2086 return super.dispatchGenericPointerEvent(event);
2087 }
2088
Jeff Browna032cc02011-03-07 16:56:21 -08002089 @Override
2090 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08002091 // Send the event to the focused child or to this view group if it has focus.
Dianne Hackborn4702a852012-08-17 15:18:29 -07002092 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
2093 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08002094 return super.dispatchGenericFocusedEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07002095 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
2096 == PFLAG_HAS_BOUNDS) {
Jeff Browncb1404e2011-01-15 18:14:15 -08002097 return mFocused.dispatchGenericMotionEvent(event);
2098 }
2099 return false;
2100 }
2101
2102 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002103 * Dispatches a generic pointer event to a child, taking into account
2104 * transformations that apply to the child.
2105 *
2106 * @param event The event to send.
2107 * @param child The view to send the event to.
2108 * @return {@code true} if the child handled the event.
2109 */
2110 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
2111 final float offsetX = mScrollX - child.mLeft;
2112 final float offsetY = mScrollY - child.mTop;
2113
2114 boolean handled;
2115 if (!child.hasIdentityMatrix()) {
2116 MotionEvent transformedEvent = MotionEvent.obtain(event);
2117 transformedEvent.offsetLocation(offsetX, offsetY);
2118 transformedEvent.transform(child.getInverseMatrix());
2119 handled = child.dispatchGenericMotionEvent(transformedEvent);
2120 transformedEvent.recycle();
2121 } else {
2122 event.offsetLocation(offsetX, offsetY);
2123 handled = child.dispatchGenericMotionEvent(event);
2124 event.offsetLocation(-offsetX, -offsetY);
2125 }
2126 return handled;
2127 }
2128
Jeff Browncb1404e2011-01-15 18:14:15 -08002129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08002131 if (mInputEventConsistencyVerifier != null) {
2132 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
2133 }
2134
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002135 // If the event targets the accessibility focused view and this is it, start
2136 // normal event dispatch. Maybe a descendant is what will handle the click.
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002137 if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002138 ev.setTargetAccessibilityFocus(false);
2139 }
2140
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002141 boolean handled = false;
2142 if (onFilterTouchEventForSecurity(ev)) {
2143 final int action = ev.getAction();
2144 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07002145
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002146 // Handle an initial down.
2147 if (actionMasked == MotionEvent.ACTION_DOWN) {
2148 // Throw away all previous state when starting a new touch gesture.
2149 // The framework may have dropped the up or cancel event for the previous gesture
2150 // due to an app switch, ANR, or some other state change.
2151 cancelAndClearTouchTargets(ev);
2152 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Adam Powellb08013c2010-09-16 16:28:11 -07002154
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002155 // Check for interception.
2156 final boolean intercepted;
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002157 if (actionMasked == MotionEvent.ACTION_DOWN
2158 || mFirstTouchTarget != null) {
2159 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
2160 if (!disallowIntercept) {
2161 intercepted = onInterceptTouchEvent(ev);
2162 ev.setAction(action); // restore action in case it was changed
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002163 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002164 intercepted = false;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002165 }
2166 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002167 // There are no touch targets and this action is not an initial down
2168 // so this view group continues to intercept touches.
2169 intercepted = true;
2170 }
2171
2172 // If intercepted, start normal event dispatch. Also if there is already
2173 // a view that is handling the gesture, do normal event dispatch.
2174 if (intercepted || mFirstTouchTarget != null) {
2175 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002176 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002177
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002178 // Check for cancelation.
2179 final boolean canceled = resetCancelNextUpFlag(this)
2180 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07002181
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002182 // Update list of touch targets for pointer down, if needed.
2183 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
2184 TouchTarget newTouchTarget = null;
2185 boolean alreadyDispatchedToNewTouchTarget = false;
2186 if (!canceled && !intercepted) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002187
2188 // If the event is targeting accessiiblity focus we give it to the
2189 // view that has accessibility focus and if it does not handle it
2190 // we clear the flag and dispatch the event to all children as usual.
2191 // We are looking up the accessibility focused host to avoid keeping
2192 // state since these events are very rare.
2193 View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
2194 ? findChildWithAccessibilityFocus() : null;
2195
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002196 if (actionMasked == MotionEvent.ACTION_DOWN
2197 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002198 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002199 final int actionIndex = ev.getActionIndex(); // always 0 for down
2200 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
2201 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07002202
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002203 // Clean up earlier touch targets for this pointer id in case they
2204 // have become out of sync.
2205 removePointersFromTouchTargets(idBitsToAssign);
2206
2207 final int childrenCount = mChildrenCount;
Chet Haase9c17fe62013-03-22 17:05:55 -07002208 if (newTouchTarget == null && childrenCount != 0) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002209 final float x = ev.getX(actionIndex);
2210 final float y = ev.getY(actionIndex);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002211 // Find a child that can receive the event.
2212 // Scan children from front to back.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002213 final ArrayList<View> preorderedList = buildTouchDispatchChildList();
Chris Craikab008f02014-05-23 17:55:03 -07002214 final boolean customOrder = preorderedList == null
2215 && isChildrenDrawingOrderEnabled();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002216 final View[] children = mChildren;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002217 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002218 final int childIndex = getAndVerifyPreorderedIndex(
2219 childrenCount, i, customOrder);
2220 final View child = getAndVerifyPreorderedView(
2221 preorderedList, children, childIndex);
Svetoslavc73cfa02015-02-09 17:14:28 -08002222
2223 // If there is a view that has accessibility focus we want it
2224 // to get the event first and if not handled we will perform a
2225 // normal dispatch. We may do a double iteration but this is
2226 // safer given the timeframe.
2227 if (childWithAccessibilityFocus != null) {
2228 if (childWithAccessibilityFocus != child) {
2229 continue;
2230 }
2231 childWithAccessibilityFocus = null;
2232 i = childrenCount - 1;
2233 }
2234
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002235 if (!canViewReceivePointerEvents(child)
2236 || !isTransformedTouchPointInView(x, y, child, null)) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002237 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002238 continue;
2239 }
2240
2241 newTouchTarget = getTouchTarget(child);
2242 if (newTouchTarget != null) {
2243 // Child is already receiving touch within its bounds.
2244 // Give it the new pointer in addition to the ones it is handling.
2245 newTouchTarget.pointerIdBits |= idBitsToAssign;
2246 break;
2247 }
2248
2249 resetCancelNextUpFlag(child);
2250 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
2251 // Child wants to receive touch within its bounds.
2252 mLastTouchDownTime = ev.getDownTime();
Chris Craikab008f02014-05-23 17:55:03 -07002253 if (preorderedList != null) {
2254 // childIndex points into presorted list, find original index
2255 for (int j = 0; j < childrenCount; j++) {
2256 if (children[childIndex] == mChildren[j]) {
2257 mLastTouchDownIndex = j;
2258 break;
2259 }
2260 }
2261 } else {
2262 mLastTouchDownIndex = childIndex;
2263 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002264 mLastTouchDownX = ev.getX();
2265 mLastTouchDownY = ev.getY();
2266 newTouchTarget = addTouchTarget(child, idBitsToAssign);
2267 alreadyDispatchedToNewTouchTarget = true;
2268 break;
2269 }
Svetoslavc73cfa02015-02-09 17:14:28 -08002270
2271 // The accessibility focus didn't handle the event, so clear
2272 // the flag and do a normal dispatch to all children.
2273 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002274 }
Chris Craikab008f02014-05-23 17:55:03 -07002275 if (preorderedList != null) preorderedList.clear();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002276 }
2277
2278 if (newTouchTarget == null && mFirstTouchTarget != null) {
2279 // Did not find a child to receive the event.
2280 // Assign the pointer to the least recently added target.
2281 newTouchTarget = mFirstTouchTarget;
2282 while (newTouchTarget.next != null) {
2283 newTouchTarget = newTouchTarget.next;
2284 }
2285 newTouchTarget.pointerIdBits |= idBitsToAssign;
2286 }
2287 }
2288 }
2289
2290 // Dispatch to touch targets.
2291 if (mFirstTouchTarget == null) {
2292 // No touch targets so treat this as an ordinary view.
2293 handled = dispatchTransformedTouchEvent(ev, canceled, null,
2294 TouchTarget.ALL_POINTER_IDS);
2295 } else {
2296 // Dispatch to touch targets, excluding the new touch target if we already
2297 // dispatched to it. Cancel touch targets if necessary.
2298 TouchTarget predecessor = null;
2299 TouchTarget target = mFirstTouchTarget;
2300 while (target != null) {
2301 final TouchTarget next = target.next;
2302 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
2303 handled = true;
2304 } else {
2305 final boolean cancelChild = resetCancelNextUpFlag(target.child)
Chet Haase9c17fe62013-03-22 17:05:55 -07002306 || intercepted;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002307 if (dispatchTransformedTouchEvent(ev, cancelChild,
2308 target.child, target.pointerIdBits)) {
2309 handled = true;
2310 }
2311 if (cancelChild) {
2312 if (predecessor == null) {
2313 mFirstTouchTarget = next;
2314 } else {
2315 predecessor.next = next;
2316 }
2317 target.recycle();
2318 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07002319 continue;
2320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002322 predecessor = target;
2323 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002325 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002326
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002327 // Update list of touch targets for pointer up or cancel, if needed.
2328 if (canceled
2329 || actionMasked == MotionEvent.ACTION_UP
2330 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
2331 resetTouchState();
2332 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
2333 final int actionIndex = ev.getActionIndex();
2334 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
2335 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337 }
Romain Guy8506ab42009-06-11 17:35:47 -07002338
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002339 if (!handled && mInputEventConsistencyVerifier != null) {
2340 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07002341 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002342 return handled;
2343 }
2344
Romain Guy469b1db2010-10-05 11:49:57 -07002345 /**
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002346 * Provide custom ordering of views in which the touch will be dispatched.
2347 *
2348 * This is called within a tight loop, so you are not allowed to allocate objects, including
2349 * the return array. Instead, you should return a pre-allocated list that will be cleared
2350 * after the dispatch is finished.
2351 * @hide
2352 */
2353 public ArrayList<View> buildTouchDispatchChildList() {
2354 return buildOrderedChildList();
2355 }
2356
2357 /**
Svetoslavc73cfa02015-02-09 17:14:28 -08002358 * Finds the child which has accessibility focus.
2359 *
2360 * @return The child that has focus.
2361 */
2362 private View findChildWithAccessibilityFocus() {
2363 ViewRootImpl viewRoot = getViewRootImpl();
2364 if (viewRoot == null) {
2365 return null;
2366 }
2367
2368 View current = viewRoot.getAccessibilityFocusedHost();
2369 if (current == null) {
2370 return null;
2371 }
2372
2373 ViewParent parent = current.getParent();
2374 while (parent instanceof View) {
2375 if (parent == this) {
2376 return current;
2377 }
2378 current = (View) parent;
2379 parent = current.getParent();
2380 }
2381
2382 return null;
2383 }
2384
2385 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002386 * Resets all touch state in preparation for a new cycle.
2387 */
2388 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002389 clearTouchTargets();
2390 resetCancelNextUpFlag(this);
2391 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
Adam Powell10ba2772014-04-15 09:46:51 -07002392 mNestedScrollAxes = SCROLL_AXIS_NONE;
Jeff Brown20e987b2010-08-23 12:01:02 -07002393 }
2394
Romain Guy469b1db2010-10-05 11:49:57 -07002395 /**
2396 * Resets the cancel next up flag.
2397 * Returns true if the flag was previously set.
2398 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002399 private static boolean resetCancelNextUpFlag(@NonNull View view) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002400 if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) {
2401 view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Jeff Brown20e987b2010-08-23 12:01:02 -07002402 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07002403 }
2404 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406
Romain Guy469b1db2010-10-05 11:49:57 -07002407 /**
2408 * Clears all touch targets.
2409 */
2410 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002411 TouchTarget target = mFirstTouchTarget;
2412 if (target != null) {
2413 do {
2414 TouchTarget next = target.next;
2415 target.recycle();
2416 target = next;
2417 } while (target != null);
2418 mFirstTouchTarget = null;
2419 }
2420 }
2421
Romain Guy469b1db2010-10-05 11:49:57 -07002422 /**
2423 * Cancels and clears all touch targets.
2424 */
2425 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002426 if (mFirstTouchTarget != null) {
2427 boolean syntheticEvent = false;
2428 if (event == null) {
2429 final long now = SystemClock.uptimeMillis();
2430 event = MotionEvent.obtain(now, now,
2431 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07002432 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07002433 syntheticEvent = true;
2434 }
2435
2436 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2437 resetCancelNextUpFlag(target.child);
2438 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
2439 }
2440 clearTouchTargets();
2441
2442 if (syntheticEvent) {
2443 event.recycle();
2444 }
2445 }
2446 }
2447
Romain Guy469b1db2010-10-05 11:49:57 -07002448 /**
2449 * Gets the touch target for specified child view.
2450 * Returns null if not found.
2451 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002452 private TouchTarget getTouchTarget(@NonNull View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002453 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2454 if (target.child == child) {
2455 return target;
2456 }
2457 }
2458 return null;
2459 }
2460
Romain Guy469b1db2010-10-05 11:49:57 -07002461 /**
2462 * Adds a touch target for specified child to the beginning of the list.
2463 * Assumes the target child is not already present.
2464 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002465 private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
2466 final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
Jeff Brown20e987b2010-08-23 12:01:02 -07002467 target.next = mFirstTouchTarget;
2468 mFirstTouchTarget = target;
2469 return target;
2470 }
2471
Romain Guy469b1db2010-10-05 11:49:57 -07002472 /**
2473 * Removes the pointer ids from consideration.
2474 */
2475 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002476 TouchTarget predecessor = null;
2477 TouchTarget target = mFirstTouchTarget;
2478 while (target != null) {
2479 final TouchTarget next = target.next;
2480 if ((target.pointerIdBits & pointerIdBits) != 0) {
2481 target.pointerIdBits &= ~pointerIdBits;
2482 if (target.pointerIdBits == 0) {
2483 if (predecessor == null) {
2484 mFirstTouchTarget = next;
2485 } else {
2486 predecessor.next = next;
2487 }
2488 target.recycle();
2489 target = next;
2490 continue;
2491 }
2492 }
2493 predecessor = target;
2494 target = next;
2495 }
2496 }
2497
Jeff Brown59a422e2012-04-19 15:19:19 -07002498 private void cancelTouchTarget(View view) {
2499 TouchTarget predecessor = null;
2500 TouchTarget target = mFirstTouchTarget;
2501 while (target != null) {
2502 final TouchTarget next = target.next;
2503 if (target.child == view) {
2504 if (predecessor == null) {
2505 mFirstTouchTarget = next;
2506 } else {
2507 predecessor.next = next;
2508 }
2509 target.recycle();
2510
2511 final long now = SystemClock.uptimeMillis();
2512 MotionEvent event = MotionEvent.obtain(now, now,
2513 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2514 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2515 view.dispatchTouchEvent(event);
2516 event.recycle();
2517 return;
2518 }
2519 predecessor = target;
2520 target = next;
2521 }
2522 }
2523
Romain Guy469b1db2010-10-05 11:49:57 -07002524 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002525 * Returns true if a child view can receive pointer events.
2526 * @hide
2527 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002528 private static boolean canViewReceivePointerEvents(@NonNull View child) {
Jeff Browna032cc02011-03-07 16:56:21 -08002529 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2530 || child.getAnimation() != null;
2531 }
2532
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002533 private float[] getTempPoint() {
2534 if (mTempPoint == null) {
2535 mTempPoint = new float[2];
2536 }
2537 return mTempPoint;
2538 }
2539
Jeff Browna032cc02011-03-07 16:56:21 -08002540 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002541 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07002542 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07002543 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07002544 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07002545 */
Adam Cohena32edd42010-10-26 10:35:01 -07002546 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07002547 PointF outLocalPoint) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002548 final float[] point = getTempPoint();
2549 point[0] = x;
2550 point[1] = y;
2551 transformPointToViewLocal(point, child);
2552 final boolean isInView = child.pointInView(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002553 if (isInView && outLocalPoint != null) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002554 outLocalPoint.set(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002555 }
2556 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07002557 }
2558
Romain Guy469b1db2010-10-05 11:49:57 -07002559 /**
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002560 * @hide
2561 */
2562 public void transformPointToViewLocal(float[] point, View child) {
2563 point[0] += mScrollX - child.mLeft;
2564 point[1] += mScrollY - child.mTop;
2565
2566 if (!child.hasIdentityMatrix()) {
2567 child.getInverseMatrix().mapPoints(point);
2568 }
2569 }
2570
2571 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002572 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07002573 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07002574 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
2575 */
2576 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07002577 View child, int desiredPointerIdBits) {
2578 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07002579
Jeff Brown20e987b2010-08-23 12:01:02 -07002580 // Canceling motions is a special case. We don't need to perform any transformations
2581 // or filtering. The important part is the action, not the contents.
2582 final int oldAction = event.getAction();
2583 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
2584 event.setAction(MotionEvent.ACTION_CANCEL);
2585 if (child == null) {
2586 handled = super.dispatchTouchEvent(event);
2587 } else {
2588 handled = child.dispatchTouchEvent(event);
2589 }
2590 event.setAction(oldAction);
2591 return handled;
2592 }
Adam Powell2b342f02010-08-18 18:14:13 -07002593
Jeff Brown20e987b2010-08-23 12:01:02 -07002594 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002595 final int oldPointerIdBits = event.getPointerIdBits();
2596 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07002597
Jeff Brown20e987b2010-08-23 12:01:02 -07002598 // If for some reason we ended up in an inconsistent state where it looks like we
2599 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002600 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002601 return false;
2602 }
Adam Powell2b342f02010-08-18 18:14:13 -07002603
Jeff Brown20e987b2010-08-23 12:01:02 -07002604 // If the number of pointers is the same and we don't need to perform any fancy
2605 // irreversible transformations, then we can reuse the motion event for this
2606 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002607 // Otherwise we need to make a copy.
2608 final MotionEvent transformedEvent;
2609 if (newPointerIdBits == oldPointerIdBits) {
2610 if (child == null || child.hasIdentityMatrix()) {
2611 if (child == null) {
2612 handled = super.dispatchTouchEvent(event);
2613 } else {
2614 final float offsetX = mScrollX - child.mLeft;
2615 final float offsetY = mScrollY - child.mTop;
2616 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07002617
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002618 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07002619
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002620 event.offsetLocation(-offsetX, -offsetY);
2621 }
2622 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07002623 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002624 transformedEvent = MotionEvent.obtain(event);
2625 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002626 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07002627 }
2628
Jeff Brown20e987b2010-08-23 12:01:02 -07002629 // Perform any necessary transformations and dispatch.
2630 if (child == null) {
2631 handled = super.dispatchTouchEvent(transformedEvent);
2632 } else {
2633 final float offsetX = mScrollX - child.mLeft;
2634 final float offsetY = mScrollY - child.mTop;
2635 transformedEvent.offsetLocation(offsetX, offsetY);
2636 if (! child.hasIdentityMatrix()) {
2637 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07002638 }
2639
Jeff Brown20e987b2010-08-23 12:01:02 -07002640 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07002641 }
2642
Jeff Brown20e987b2010-08-23 12:01:02 -07002643 // Done.
2644 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07002645 return handled;
2646 }
2647
Romain Guy469b1db2010-10-05 11:49:57 -07002648 /**
Adam Powell2b342f02010-08-18 18:14:13 -07002649 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08002650 * dispatch. This behavior is enabled by default for applications that target an
2651 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07002652 *
2653 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
2654 * views depending on where each pointer initially went down. This allows for user interactions
2655 * such as scrolling two panes of content independently, chording of buttons, and performing
2656 * independent gestures on different pieces of content.
2657 *
2658 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
2659 * child views. <code>false</code> to only allow one child view to be the target of
2660 * any MotionEvent received by this ViewGroup.
Scott Main27a85082013-06-10 10:39:48 -07002661 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
Adam Powell2b342f02010-08-18 18:14:13 -07002662 */
2663 public void setMotionEventSplittingEnabled(boolean split) {
2664 // TODO Applications really shouldn't change this setting mid-touch event,
2665 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
2666 // with gestures in progress when this is changed.
2667 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07002668 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
2669 } else {
2670 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07002671 }
2672 }
2673
2674 /**
Jeff Brown995e7742010-12-22 16:59:36 -08002675 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07002676 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
2677 */
2678 public boolean isMotionEventSplittingEnabled() {
2679 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
2680 }
2681
2682 /**
George Mount0a778ed2013-12-13 13:35:36 -08002683 * Returns true if this ViewGroup should be considered as a single entity for removal
2684 * when executing an Activity transition. If this is false, child elements will move
2685 * individually during the transition.
George Mount427c6412014-11-05 16:45:36 -08002686 *
George Mount0a778ed2013-12-13 13:35:36 -08002687 * @return True if the ViewGroup should be acted on together during an Activity transition.
George Mount427c6412014-11-05 16:45:36 -08002688 * The default value is true when there is a non-null background or if
2689 * {@link #getTransitionName()} is not null or if a
2690 * non-null {@link android.view.ViewOutlineProvider} other than
2691 * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to
2692 * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise.
George Mount0a778ed2013-12-13 13:35:36 -08002693 */
2694 public boolean isTransitionGroup() {
2695 if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) {
2696 return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0);
2697 } else {
George Mount427c6412014-11-05 16:45:36 -08002698 final ViewOutlineProvider outlineProvider = getOutlineProvider();
2699 return getBackground() != null || getTransitionName() != null ||
2700 (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND);
George Mount0a778ed2013-12-13 13:35:36 -08002701 }
2702 }
2703
2704 /**
2705 * Changes whether or not this ViewGroup should be treated as a single entity during
George Mount31a21722014-03-24 17:44:36 -07002706 * Activity Transitions.
George Mount0a778ed2013-12-13 13:35:36 -08002707 * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
2708 * in Activity transitions. If false, the ViewGroup won't transition,
2709 * only its children. If true, the entire ViewGroup will transition
2710 * together.
George Mount62ab9b72014-05-02 13:51:17 -07002711 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity,
2712 * android.util.Pair[])
George Mount0a778ed2013-12-13 13:35:36 -08002713 */
2714 public void setTransitionGroup(boolean isTransitionGroup) {
2715 mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET;
2716 if (isTransitionGroup) {
2717 mGroupFlags |= FLAG_IS_TRANSITION_GROUP;
2718 } else {
2719 mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP;
2720 }
2721 }
2722
Alan Viverettebe463f22016-01-21 10:50:10 -05002723 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07002725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
2727 // We're already in this state, assume our ancestors are too
2728 return;
2729 }
Romain Guy8506ab42009-06-11 17:35:47 -07002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 if (disallowIntercept) {
2732 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
2733 } else {
2734 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
2735 }
Romain Guy8506ab42009-06-11 17:35:47 -07002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 // Pass it up to our parent
2738 if (mParent != null) {
2739 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
2740 }
2741 }
2742
2743 /**
2744 * Implement this method to intercept all touch screen motion events. This
2745 * allows you to watch events as they are dispatched to your children, and
2746 * take ownership of the current gesture at any point.
2747 *
2748 * <p>Using this function takes some care, as it has a fairly complicated
2749 * interaction with {@link View#onTouchEvent(MotionEvent)
2750 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
2751 * that method as well as this one in the correct way. Events will be
2752 * received in the following order:
2753 *
2754 * <ol>
2755 * <li> You will receive the down event here.
2756 * <li> The down event will be handled either by a child of this view
2757 * group, or given to your own onTouchEvent() method to handle; this means
2758 * you should implement onTouchEvent() to return true, so you will
2759 * continue to see the rest of the gesture (instead of looking for
2760 * a parent view to handle it). Also, by returning true from
2761 * onTouchEvent(), you will not receive any following
2762 * events in onInterceptTouchEvent() and all touch processing must
2763 * happen in onTouchEvent() like normal.
2764 * <li> For as long as you return false from this function, each following
2765 * event (up to and including the final up) will be delivered first here
2766 * and then to the target's onTouchEvent().
2767 * <li> If you return true from here, you will not receive any
2768 * following events: the target view will receive the same event but
2769 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
2770 * events will be delivered to your onTouchEvent() method and no longer
2771 * appear here.
2772 * </ol>
2773 *
2774 * @param ev The motion event being dispatched down the hierarchy.
2775 * @return Return true to steal motion events from the children and have
2776 * them dispatched to this ViewGroup through onTouchEvent().
2777 * The current target will receive an ACTION_CANCEL event, and no further
2778 * messages will be delivered here.
2779 */
2780 public boolean onInterceptTouchEvent(MotionEvent ev) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002781 if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
2782 && ev.getAction() == MotionEvent.ACTION_DOWN
2783 && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
2784 && isOnScrollbarThumb(ev.getX(), ev.getY())) {
2785 return true;
2786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 return false;
2788 }
2789
2790 /**
2791 * {@inheritDoc}
2792 *
2793 * Looks for a view to give focus to respecting the setting specified by
2794 * {@link #getDescendantFocusability()}.
2795 *
2796 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
2797 * find focus within the children of this group when appropriate.
2798 *
2799 * @see #FOCUS_BEFORE_DESCENDANTS
2800 * @see #FOCUS_AFTER_DESCENDANTS
2801 * @see #FOCUS_BLOCK_DESCENDANTS
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002802 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 */
2804 @Override
2805 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
2806 if (DBG) {
2807 System.out.println(this + " ViewGroup.requestFocus direction="
2808 + direction);
2809 }
2810 int descendantFocusability = getDescendantFocusability();
2811
2812 switch (descendantFocusability) {
2813 case FOCUS_BLOCK_DESCENDANTS:
2814 return super.requestFocus(direction, previouslyFocusedRect);
2815 case FOCUS_BEFORE_DESCENDANTS: {
2816 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
2817 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
2818 }
2819 case FOCUS_AFTER_DESCENDANTS: {
2820 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
2821 return took ? took : super.requestFocus(direction, previouslyFocusedRect);
2822 }
2823 default:
2824 throw new IllegalStateException("descendant focusability must be "
2825 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
2826 + "but is " + descendantFocusability);
2827 }
2828 }
2829
2830 /**
2831 * Look for a descendant to call {@link View#requestFocus} on.
2832 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
2833 * when it wants to request focus within its children. Override this to
2834 * customize how your {@link ViewGroup} requests focus within its children.
2835 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
2836 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
2837 * to give a finer grained hint about where focus is coming from. May be null
2838 * if there is no hint.
2839 * @return Whether focus was taken.
2840 */
2841 @SuppressWarnings({"ConstantConditions"})
2842 protected boolean onRequestFocusInDescendants(int direction,
2843 Rect previouslyFocusedRect) {
2844 int index;
2845 int increment;
2846 int end;
2847 int count = mChildrenCount;
2848 if ((direction & FOCUS_FORWARD) != 0) {
2849 index = 0;
2850 increment = 1;
2851 end = count;
2852 } else {
2853 index = count - 1;
2854 increment = -1;
2855 end = -1;
2856 }
2857 final View[] children = mChildren;
2858 for (int i = index; i != end; i += increment) {
2859 View child = children[i];
2860 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2861 if (child.requestFocus(direction, previouslyFocusedRect)) {
2862 return true;
2863 }
2864 }
2865 }
2866 return false;
2867 }
Chet Haase5c13d892010-10-08 08:37:55 -07002868
Romain Guya440b002010-02-24 15:57:54 -08002869 /**
2870 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002871 *
Romain Guydcc490f2010-02-24 17:59:35 -08002872 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002873 */
2874 @Override
2875 public void dispatchStartTemporaryDetach() {
2876 super.dispatchStartTemporaryDetach();
2877 final int count = mChildrenCount;
2878 final View[] children = mChildren;
2879 for (int i = 0; i < count; i++) {
2880 children[i].dispatchStartTemporaryDetach();
2881 }
2882 }
Chet Haase5c13d892010-10-08 08:37:55 -07002883
Romain Guya440b002010-02-24 15:57:54 -08002884 /**
2885 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07002886 *
Romain Guydcc490f2010-02-24 17:59:35 -08002887 * @hide
Romain Guya440b002010-02-24 15:57:54 -08002888 */
2889 @Override
2890 public void dispatchFinishTemporaryDetach() {
2891 super.dispatchFinishTemporaryDetach();
2892 final int count = mChildrenCount;
2893 final View[] children = mChildren;
2894 for (int i = 0; i < count; i++) {
2895 children[i].dispatchFinishTemporaryDetach();
2896 }
2897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 @Override
2900 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07002901 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07002903 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
2904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 final int count = mChildrenCount;
2906 final View[] children = mChildren;
2907 for (int i = 0; i < count; i++) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002908 final View child = children[i];
2909 child.dispatchAttachedToWindow(info,
Adam Powelleb2b0af2015-05-20 18:26:35 -07002910 combineVisibility(visibility, child.getVisibility()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
Chet Haasec633d2f2015-04-07 10:29:39 -07002912 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
2913 for (int i = 0; i < transientCount; ++i) {
2914 View view = mTransientViews.get(i);
Adam Powelleb2b0af2015-05-20 18:26:35 -07002915 view.dispatchAttachedToWindow(info,
2916 combineVisibility(visibility, view.getVisibility()));
Chet Haasec633d2f2015-04-07 10:29:39 -07002917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
2919
svetoslavganov75986cf2009-05-14 22:28:01 -07002920 @Override
Romain Guybb9908b2012-03-08 11:14:07 -08002921 void dispatchScreenStateChanged(int screenState) {
2922 super.dispatchScreenStateChanged(screenState);
2923
2924 final int count = mChildrenCount;
2925 final View[] children = mChildren;
2926 for (int i = 0; i < count; i++) {
2927 children[i].dispatchScreenStateChanged(screenState);
2928 }
2929 }
2930
Alan Viverettea54956a2015-01-07 16:05:02 -08002931 /** @hide */
Romain Guybb9908b2012-03-08 11:14:07 -08002932 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002933 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002934 boolean handled = false;
2935 if (includeForAccessibility()) {
2936 handled = super.dispatchPopulateAccessibilityEventInternal(event);
2937 if (handled) {
2938 return handled;
2939 }
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07002940 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002941 // Let our children have a shot in populating the event.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002942 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002943 try {
2944 final int childCount = children.getChildCount();
2945 for (int i = 0; i < childCount; i++) {
2946 View child = children.getChildAt(i);
2947 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
2948 handled = child.dispatchPopulateAccessibilityEvent(event);
2949 if (handled) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002950 return handled;
2951 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002952 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002953 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002954 } finally {
2955 children.recycle();
svetoslavganov75986cf2009-05-14 22:28:01 -07002956 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002957 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07002958 }
2959
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002960 /**
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002961 * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07002962 * adds in all child views of the view group, in addition to calling the default View
2963 * implementation.
2964 */
Alan Viverettebe463f22016-01-21 10:50:10 -05002965 @Override
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002966 public void dispatchProvideStructure(ViewStructure structure) {
2967 super.dispatchProvideStructure(structure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002968 if (!isAssistBlocked()) {
2969 if (structure.getChildCount() == 0) {
2970 final int childrenCount = getChildCount();
2971 if (childrenCount > 0) {
2972 structure.setChildCount(childrenCount);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002973 ArrayList<View> preorderedList = buildOrderedChildList();
2974 boolean customOrder = preorderedList == null
Dianne Hackborn799d8d02015-04-22 16:36:39 -07002975 && isChildrenDrawingOrderEnabled();
2976 final View[] children = mChildren;
2977 for (int i=0; i<childrenCount; i++) {
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002978 int childIndex;
2979 try {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002980 childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -07002981 } catch (IndexOutOfBoundsException e) {
2982 childIndex = i;
2983 if (mContext.getApplicationInfo().targetSdkVersion
2984 < Build.VERSION_CODES.M) {
2985 Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ "
2986 + i + " of " + childrenCount, e);
2987 // At least one app is failing when we call getChildDrawingOrder
2988 // at this point, so deal semi-gracefully with it by falling back
2989 // on the basic order.
2990 customOrder = false;
2991 if (i > 0) {
2992 // If we failed at the first index, there really isn't
2993 // anything to do -- we will just proceed with the simple
2994 // sequence order.
2995 // Otherwise, we failed in the middle, so need to come up
2996 // with an order for the remaining indices and use that.
2997 // Failed at the first one, easy peasy.
2998 int[] permutation = new int[childrenCount];
2999 SparseBooleanArray usedIndices = new SparseBooleanArray();
3000 // Go back and collected the indices we have done so far.
3001 for (int j=0; j<i; j++) {
3002 permutation[j] = getChildDrawingOrder(childrenCount, j);
3003 usedIndices.put(permutation[j], true);
3004 }
3005 // Fill in the remaining indices with indices that have not
3006 // yet been used.
3007 int nextIndex = 0;
3008 for (int j=i; j<childrenCount; j++) {
3009 while (usedIndices.get(nextIndex, false)) {
3010 nextIndex++;
3011 }
3012 permutation[j] = nextIndex;
3013 nextIndex++;
3014 }
3015 // Build the final view list.
3016 preorderedList = new ArrayList<>(childrenCount);
3017 for (int j=0; j<childrenCount; j++) {
3018 preorderedList.add(children[permutation[j]]);
3019 }
3020 }
3021 } else {
3022 throw e;
3023 }
3024 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05003025
3026 final View child = getAndVerifyPreorderedView(
3027 preorderedList, children, childIndex);
3028 final ViewStructure cstructure = structure.newChild(i);
Dianne Hackborn49b043f2015-05-07 14:21:38 -07003029 child.dispatchProvideStructure(cstructure);
Dianne Hackborn799d8d02015-04-22 16:36:39 -07003030 }
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07003031 }
3032 }
3033 }
3034 }
3035
Alan Viverettea7b85e62016-01-22 10:14:02 -05003036 private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,
3037 int childIndex) {
3038 final View child;
3039 if (preorderedList != null) {
3040 child = preorderedList.get(childIndex);
3041 if (child == null) {
3042 throw new RuntimeException("Invalid preorderedList contained null child at index "
3043 + childIndex);
3044 }
3045 } else {
3046 child = children[childIndex];
3047 }
3048 return child;
3049 }
3050
Alan Viverettea54956a2015-01-07 16:05:02 -08003051 /** @hide */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003052 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08003053 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003054 super.onInitializeAccessibilityNodeInfoInternal(info);
Svet Ganov55bdb102015-02-06 12:41:17 -08003055 if (getAccessibilityNodeProvider() != null) {
3056 return;
3057 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003058 if (mAttachInfo != null) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003059 final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003060 childrenForAccessibility.clear();
3061 addChildrenForAccessibility(childrenForAccessibility);
3062 final int childrenForAccessibilityCount = childrenForAccessibility.size();
3063 for (int i = 0; i < childrenForAccessibilityCount; i++) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003064 final View child = childrenForAccessibility.get(i);
3065 info.addChildUnchecked(child);
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07003066 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003067 childrenForAccessibility.clear();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003068 }
3069 }
3070
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003071 @Override
Maxim Bogatovf399af32015-06-16 15:15:49 -07003072 public CharSequence getAccessibilityClassName() {
3073 return ViewGroup.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003074 }
3075
Svetoslav Ganov42138042012-03-20 11:51:39 -07003076 @Override
Alan Viverette77e9a282013-09-12 17:16:09 -07003077 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
3078 // If this is a live region, we should send a subtree change event
3079 // from this view. Otherwise, we can let it propagate up.
3080 if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) {
3081 notifyViewAccessibilityStateChangedIfNeeded(changeType);
3082 } else if (mParent != null) {
Adam Powell504a10f2013-07-11 15:25:59 -07003083 try {
Alan Viverette77e9a282013-09-12 17:16:09 -07003084 mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType);
Adam Powell504a10f2013-07-11 15:25:59 -07003085 } catch (AbstractMethodError e) {
3086 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
3087 " does not fully implement ViewParent", e);
3088 }
Svetoslav6254f482013-06-04 17:22:14 -07003089 }
3090 }
3091
3092 @Override
3093 void resetSubtreeAccessibilityStateChanged() {
3094 super.resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003095 View[] children = mChildren;
3096 final int childCount = mChildrenCount;
3097 for (int i = 0; i < childCount; i++) {
Svetoslav6254f482013-06-04 17:22:14 -07003098 children[i].resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003099 }
3100 }
3101
3102 /**
Phil Weaver1f222542016-01-08 11:49:32 -08003103 * Counts the number of children of this View that will be sent to an accessibility service.
3104 *
3105 * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View
3106 * would have.
3107 */
3108 int getNumChildrenForAccessibility() {
3109 int numChildrenForAccessibility = 0;
3110 for (int i = 0; i < getChildCount(); i++) {
3111 View child = getChildAt(i);
3112 if (child.includeForAccessibility()) {
3113 numChildrenForAccessibility++;
3114 } else if (child instanceof ViewGroup) {
3115 numChildrenForAccessibility += ((ViewGroup) child)
3116 .getNumChildrenForAccessibility();
3117 }
3118 }
3119 return numChildrenForAccessibility;
3120 }
3121
3122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 * {@inheritDoc}
Adam Powellb6ab0982015-01-07 17:00:12 -08003124 *
3125 * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p>
3126 *
3127 * @param target The target view dispatching this action
3128 * @param action Action being performed; see
3129 * {@link android.view.accessibility.AccessibilityNodeInfo}
3130 * @param args Optional action arguments
3131 * @return false by default. Subclasses should return true if they handle the event.
3132 */
3133 @Override
3134 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
3135 return false;
3136 }
3137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 @Override
3139 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07003140 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 // dispatching motion events to a child; we need to get rid of that
3142 // child to avoid dispatching events to it after the window is torn
3143 // down. To make sure we keep the child in a consistent state, we
3144 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07003145 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146
Jeff Brown59a422e2012-04-19 15:19:19 -07003147 // Similarly, set ACTION_EXIT to all hover targets and clear them.
3148 exitHoverTargets();
3149
Chet Haase9c087442011-01-12 16:20:16 -08003150 // In case view is detached while transition is running
Chet Haaseb9895022013-04-02 15:10:58 -07003151 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08003152
Christopher Tate86cab1b2011-01-13 20:28:55 -08003153 // Tear down our drag tracking
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07003154 mChildrenInterestedInDrag = null;
3155 mIsInterestedInDrag = false;
3156 if (mCurrentDragStartEvent != null) {
3157 mCurrentDragStartEvent.recycle();
3158 mCurrentDragStartEvent = null;
Christopher Tate86cab1b2011-01-13 20:28:55 -08003159 }
3160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 final int count = mChildrenCount;
3162 final View[] children = mChildren;
3163 for (int i = 0; i < count; i++) {
3164 children[i].dispatchDetachedFromWindow();
3165 }
John Reckca7a9da2014-03-05 16:29:07 -08003166 clearDisappearingChildren();
Chet Haasec633d2f2015-04-07 10:29:39 -07003167 final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size();
3168 for (int i = 0; i < transientCount; ++i) {
3169 View view = mTransientViews.get(i);
3170 view.dispatchDetachedFromWindow();
3171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 super.dispatchDetachedFromWindow();
3173 }
3174
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003175 /**
3176 * @hide
3177 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 @Override
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003179 protected void internalSetPadding(int left, int top, int right, int bottom) {
Romain Guy2440e672012-08-07 14:43:43 -07003180 super.internalSetPadding(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181
Romain Guy13f35f32011-03-24 12:03:17 -07003182 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 mGroupFlags |= FLAG_PADDING_NOT_NULL;
3184 } else {
3185 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
3186 }
3187 }
3188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 @Override
3190 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
3191 super.dispatchSaveInstanceState(container);
3192 final int count = mChildrenCount;
3193 final View[] children = mChildren;
3194 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003195 View c = children[i];
3196 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3197 c.dispatchSaveInstanceState(container);
3198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200 }
3201
3202 /**
Romain Guy9fc27812011-04-27 14:21:41 -07003203 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
3204 * to only this view, not to its children. For use when overriding
3205 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
3206 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 *
3208 * @param container the container
3209 */
3210 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
3211 super.dispatchSaveInstanceState(container);
3212 }
3213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 @Override
3215 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
3216 super.dispatchRestoreInstanceState(container);
3217 final int count = mChildrenCount;
3218 final View[] children = mChildren;
3219 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003220 View c = children[i];
3221 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3222 c.dispatchRestoreInstanceState(container);
3223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 }
3226
3227 /**
Romain Guy02739a82011-05-16 11:43:18 -07003228 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
3229 * to only this view, not to its children. For use when overriding
3230 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
3231 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 *
3233 * @param container the container
3234 */
3235 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
3236 super.dispatchRestoreInstanceState(container);
3237 }
3238
3239 /**
3240 * Enables or disables the drawing cache for each child of this view group.
3241 *
3242 * @param enabled true to enable the cache, false to dispose of it
3243 */
3244 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
3245 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
3246 final View[] children = mChildren;
3247 final int count = mChildrenCount;
3248 for (int i = 0; i < count; i++) {
3249 children[i].setDrawingCacheEnabled(enabled);
3250 }
3251 }
3252 }
3253
sergeyvb37d44e2016-03-29 20:27:44 -07003254 /**
3255 * @hide
3256 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 @Override
sergeyvb37d44e2016-03-29 20:27:44 -07003258 public Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003259 int count = mChildrenCount;
3260 int[] visibilities = null;
3261
Romain Guy223ff5c2010-03-02 17:07:47 -08003262 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003263 visibilities = new int[count];
3264 for (int i = 0; i < count; i++) {
3265 View child = getChildAt(i);
3266 visibilities[i] = child.getVisibility();
3267 if (visibilities[i] == View.VISIBLE) {
sergeyvb37d44e2016-03-29 20:27:44 -07003268 child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK)
3269 | (View.INVISIBLE & View.VISIBILITY_MASK);
Romain Guy65554f22010-03-22 18:58:21 -07003270 }
3271 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003272 }
3273
3274 Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren);
Romain Guy65554f22010-03-22 18:58:21 -07003275
3276 if (skipChildren) {
3277 for (int i = 0; i < count; i++) {
sergeyvb37d44e2016-03-29 20:27:44 -07003278 View child = getChildAt(i);
3279 child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK)
3280 | (visibilities[i] & View.VISIBILITY_MASK);
Chet Haase5c13d892010-10-08 08:37:55 -07003281 }
Romain Guy65554f22010-03-22 18:58:21 -07003282 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003283
3284 return b;
3285 }
3286
Philip Milne7b757812012-09-19 18:13:44 -07003287 /** Return true if this ViewGroup is laying out using optical bounds. */
3288 boolean isLayoutModeOptical() {
3289 return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS;
3290 }
Romain Guycbc67742012-04-27 16:12:57 -07003291
Alan Viverettebe463f22016-01-21 10:50:10 -05003292 @Override
Philip Milne7b757812012-09-19 18:13:44 -07003293 Insets computeOpticalInsets() {
3294 if (isLayoutModeOptical()) {
3295 int left = 0;
3296 int top = 0;
3297 int right = 0;
3298 int bottom = 0;
3299 for (int i = 0; i < mChildrenCount; i++) {
3300 View child = getChildAt(i);
3301 if (child.getVisibility() == VISIBLE) {
3302 Insets insets = child.getOpticalInsets();
3303 left = Math.max(left, insets.left);
3304 top = Math.max(top, insets.top);
3305 right = Math.max(right, insets.right);
3306 bottom = Math.max(bottom, insets.bottom);
3307 }
3308 }
3309 return Insets.of(left, top, right, bottom);
3310 } else {
3311 return Insets.NONE;
3312 }
3313 }
3314
3315 private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
3316 if (x1 != x2 && y1 != y2) {
3317 if (x1 > x2) {
3318 int tmp = x1; x1 = x2; x2 = tmp;
3319 }
3320 if (y1 > y2) {
3321 int tmp = y1; y1 = y2; y2 = tmp;
3322 }
3323 canvas.drawRect(x1, y1, x2, y2, paint);
3324 }
3325 }
3326
3327 private static int sign(int x) {
3328 return (x >= 0) ? 1 : -1;
3329 }
3330
3331 private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) {
3332 fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy));
3333 fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
3334 }
3335
3336 private int dipsToPixels(int dips) {
3337 float scale = getContext().getResources().getDisplayMetrics().density;
3338 return (int) (dips * scale + 0.5f);
3339 }
3340
Romain Guy6410c0a2013-06-17 11:21:58 -07003341 private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
3342 int lineLength, int lineWidth) {
Philip Milne7b757812012-09-19 18:13:44 -07003343 drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
3344 drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth);
3345 drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth);
3346 drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth);
3347 }
3348
3349 private static void fillDifference(Canvas canvas,
3350 int x2, int y2, int x3, int y3,
3351 int dx1, int dy1, int dx2, int dy2, Paint paint) {
3352 int x1 = x2 - dx1;
3353 int y1 = y2 - dy1;
3354
3355 int x4 = x3 + dx2;
3356 int y4 = y3 + dy2;
3357
3358 fillRect(canvas, paint, x1, y1, x4, y2);
3359 fillRect(canvas, paint, x1, y2, x2, y3);
3360 fillRect(canvas, paint, x3, y2, x4, y3);
3361 fillRect(canvas, paint, x1, y3, x4, y4);
Philip Milne10ca24a2012-04-23 15:38:27 -07003362 }
3363
3364 /**
3365 * @hide
3366 */
Philip Milne7b757812012-09-19 18:13:44 -07003367 protected void onDebugDrawMargins(Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07003368 for (int i = 0; i < getChildCount(); i++) {
3369 View c = getChildAt(i);
Philip Milne7b757812012-09-19 18:13:44 -07003370 c.getLayoutParams().onDebugDraw(c, canvas, paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07003371 }
3372 }
3373
3374 /**
3375 * @hide
3376 */
3377 protected void onDebugDraw(Canvas canvas) {
Philip Milne7b757812012-09-19 18:13:44 -07003378 Paint paint = getDebugPaint();
3379
Philip Milne10ca24a2012-04-23 15:38:27 -07003380 // Draw optical bounds
Philip Milne7b757812012-09-19 18:13:44 -07003381 {
3382 paint.setColor(Color.RED);
3383 paint.setStyle(Paint.Style.STROKE);
Philip Milne7b757812012-09-19 18:13:44 -07003384
Philip Milne10ca24a2012-04-23 15:38:27 -07003385 for (int i = 0; i < getChildCount(); i++) {
3386 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003387 if (c.getVisibility() != View.GONE) {
3388 Insets insets = c.getOpticalInsets();
Philip Milne7b757812012-09-19 18:13:44 -07003389
Chris Craika1dab8b2015-06-30 13:51:25 -07003390 drawRect(canvas, paint,
3391 c.getLeft() + insets.left,
3392 c.getTop() + insets.top,
3393 c.getRight() - insets.right - 1,
3394 c.getBottom() - insets.bottom - 1);
3395 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003396 }
3397 }
3398
Philip Milne10ca24a2012-04-23 15:38:27 -07003399 // Draw margins
Philip Milne7b757812012-09-19 18:13:44 -07003400 {
3401 paint.setColor(Color.argb(63, 255, 0, 255));
3402 paint.setStyle(Paint.Style.FILL);
Philip Milne604f4402012-04-24 19:27:11 -07003403
Philip Milne7b757812012-09-19 18:13:44 -07003404 onDebugDrawMargins(canvas, paint);
3405 }
3406
3407 // Draw clip bounds
3408 {
3409 paint.setColor(Color.rgb(63, 127, 255));
3410 paint.setStyle(Paint.Style.FILL);
3411
3412 int lineLength = dipsToPixels(8);
3413 int lineWidth = dipsToPixels(1);
3414 for (int i = 0; i < getChildCount(); i++) {
3415 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07003416 if (c.getVisibility() != View.GONE) {
3417 drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(),
3418 paint, lineLength, lineWidth);
3419 }
Philip Milne7b757812012-09-19 18:13:44 -07003420 }
Philip Milne604f4402012-04-24 19:27:11 -07003421 }
Philip Milne10ca24a2012-04-23 15:38:27 -07003422 }
3423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 @Override
3425 protected void dispatchDraw(Canvas canvas) {
Chris Craika753f4c2014-07-24 12:39:17 -07003426 boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
Chris Craikab008f02014-05-23 17:55:03 -07003427 final int childrenCount = mChildrenCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 final View[] children = mChildren;
3429 int flags = mGroupFlags;
3430
3431 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
Romain Guy0d9275e2010-10-26 14:22:30 -07003432 final boolean buildCache = !isHardwareAccelerated();
Chris Craikab008f02014-05-23 17:55:03 -07003433 for (int i = 0; i < childrenCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 final View child = children[i];
3435 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
3436 final LayoutParams params = child.getLayoutParams();
Chris Craikab008f02014-05-23 17:55:03 -07003437 attachLayoutAnimationParameters(child, params, i, childrenCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 bindLayoutAnimation(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 }
3440 }
3441
3442 final LayoutAnimationController controller = mLayoutAnimationController;
3443 if (controller.willOverlap()) {
3444 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
3445 }
3446
3447 controller.start();
3448
3449 mGroupFlags &= ~FLAG_RUN_ANIMATION;
3450 mGroupFlags &= ~FLAG_ANIMATION_DONE;
3451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 if (mAnimationListener != null) {
3453 mAnimationListener.onAnimationStart(controller.getAnimation());
3454 }
3455 }
3456
Selim Cinek19cadc22014-04-16 17:27:19 +02003457 int clipSaveCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3459 if (clipToPadding) {
Chris Craike4cf1522014-08-04 17:55:22 -07003460 clipSaveCount = canvas.save();
Romain Guy8f2d94f2009-03-25 18:04:42 -07003461 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
3462 mScrollX + mRight - mLeft - mPaddingRight,
3463 mScrollY + mBottom - mTop - mPaddingBottom);
Selim Cinek19cadc22014-04-16 17:27:19 +02003464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 // We will draw our child's animation, let's reset the flag
Dianne Hackborn4702a852012-08-17 15:18:29 -07003467 mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
3469
3470 boolean more = false;
3471 final long drawingTime = getDrawingTime();
3472
Chris Craik8afd0f22014-08-21 17:41:57 -07003473 if (usingRenderNodeProperties) canvas.insertReorderBarrier();
Chet Haasec633d2f2015-04-07 10:29:39 -07003474 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
3475 int transientIndex = transientCount != 0 ? 0 : -1;
Chris Craikab008f02014-05-23 17:55:03 -07003476 // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
3477 // draw reordering internally
Chris Craika753f4c2014-07-24 12:39:17 -07003478 final ArrayList<View> preorderedList = usingRenderNodeProperties
Chris Craikab008f02014-05-23 17:55:03 -07003479 ? null : buildOrderedChildList();
3480 final boolean customOrder = preorderedList == null
3481 && isChildrenDrawingOrderEnabled();
3482 for (int i = 0; i < childrenCount; i++) {
Chet Haasec633d2f2015-04-07 10:29:39 -07003483 while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
3484 final View transientChild = mTransientViews.get(transientIndex);
3485 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3486 transientChild.getAnimation() != null) {
3487 more |= drawChild(canvas, transientChild, drawingTime);
3488 }
3489 transientIndex++;
3490 if (transientIndex >= transientCount) {
3491 transientIndex = -1;
3492 }
3493 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05003494
3495 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3496 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Chris Craikab008f02014-05-23 17:55:03 -07003497 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
3498 more |= drawChild(canvas, child, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500 }
Chet Haasec633d2f2015-04-07 10:29:39 -07003501 while (transientIndex >= 0) {
3502 // there may be additional transient views after the normal views
3503 final View transientChild = mTransientViews.get(transientIndex);
3504 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
3505 transientChild.getAnimation() != null) {
3506 more |= drawChild(canvas, transientChild, drawingTime);
3507 }
3508 transientIndex++;
3509 if (transientIndex >= transientCount) {
3510 break;
3511 }
3512 }
Chris Craikab008f02014-05-23 17:55:03 -07003513 if (preorderedList != null) preorderedList.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514
3515 // Draw any disappearing views that have animations
3516 if (mDisappearingChildren != null) {
3517 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3518 final int disappearingCount = disappearingChildren.size() - 1;
3519 // Go backwards -- we may delete as animations finish
3520 for (int i = disappearingCount; i >= 0; i--) {
3521 final View child = disappearingChildren.get(i);
3522 more |= drawChild(canvas, child, drawingTime);
3523 }
3524 }
Chris Craik8afd0f22014-08-21 17:41:57 -07003525 if (usingRenderNodeProperties) canvas.insertInorderBarrier();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526
Philip Milne10ca24a2012-04-23 15:38:27 -07003527 if (debugDraw()) {
3528 onDebugDraw(canvas);
3529 }
3530
Chris Craike4cf1522014-08-04 17:55:22 -07003531 if (clipToPadding) {
Selim Cinek19cadc22014-04-16 17:27:19 +02003532 canvas.restoreToCount(clipSaveCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 }
3534
3535 // mGroupFlags might have been updated by drawChild()
3536 flags = mGroupFlags;
3537
3538 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08003539 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 }
3541
3542 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
3543 mLayoutAnimationController.isDone() && !more) {
3544 // We want to erase the drawing cache and notify the listener after the
3545 // next frame is drawn because one extra invalidate() is caused by
3546 // drawChild() after the animation is over
3547 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
3548 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05003549 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 public void run() {
3551 notifyAnimationListener();
3552 }
3553 };
3554 post(end);
3555 }
3556 }
Romain Guy8506ab42009-06-11 17:35:47 -07003557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003559 * Returns the ViewGroupOverlay for this view group, creating it if it does
3560 * not yet exist. In addition to {@link ViewOverlay}'s support for drawables,
3561 * {@link ViewGroupOverlay} allows views to be added to the overlay. These
3562 * views, like overlay drawables, are visual-only; they do not receive input
3563 * events and should not be used as anything other than a temporary
3564 * representation of a view in a parent container, such as might be used
3565 * by an animation effect.
3566 *
Chet Haase95399492013-04-08 14:30:31 -07003567 * <p>Note: Overlays do not currently work correctly with {@link
3568 * SurfaceView} or {@link TextureView}; contents in overlays for these
3569 * types of views may not display correctly.</p>
3570 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003571 * @return The ViewGroupOverlay object for this view.
3572 * @see ViewGroupOverlay
3573 */
3574 @Override
3575 public ViewGroupOverlay getOverlay() {
3576 if (mOverlay == null) {
3577 mOverlay = new ViewGroupOverlay(mContext, this);
3578 }
3579 return (ViewGroupOverlay) mOverlay;
3580 }
3581
3582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 * Returns the index of the child to draw for this iteration. Override this
3584 * if you want to change the drawing order of children. By default, it
3585 * returns i.
3586 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08003587 * NOTE: In order for this method to be called, you must enable child ordering
3588 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07003589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 * @param i The current iteration.
3591 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07003592 *
Romain Guy293451e2009-11-04 13:59:48 -08003593 * @see #setChildrenDrawingOrderEnabled(boolean)
3594 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 */
3596 protected int getChildDrawingOrder(int childCount, int i) {
3597 return i;
3598 }
Romain Guy8506ab42009-06-11 17:35:47 -07003599
Chris Craikab008f02014-05-23 17:55:03 -07003600 private boolean hasChildWithZ() {
3601 for (int i = 0; i < mChildrenCount; i++) {
3602 if (mChildren[i].getZ() != 0) return true;
3603 }
3604 return false;
3605 }
3606
3607 /**
3608 * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,
Chris Craik57c79c82014-09-30 12:54:31 -07003609 * sorted first by Z, then by child drawing order (if applicable). This list must be cleared
3610 * after use to avoid leaking child Views.
Chris Craikab008f02014-05-23 17:55:03 -07003611 *
3612 * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated
3613 * children.
3614 */
George Mount81206522014-09-26 21:53:39 -07003615 ArrayList<View> buildOrderedChildList() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003616 final int childrenCount = mChildrenCount;
3617 if (childrenCount <= 1 || !hasChildWithZ()) return null;
Chris Craikab008f02014-05-23 17:55:03 -07003618
3619 if (mPreSortedChildren == null) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003620 mPreSortedChildren = new ArrayList<>(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07003621 } else {
Alan Viverettea7b85e62016-01-22 10:14:02 -05003622 mPreSortedChildren.ensureCapacity(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07003623 }
3624
Alan Viverettea7b85e62016-01-22 10:14:02 -05003625 final boolean customOrder = isChildrenDrawingOrderEnabled();
3626 for (int i = 0; i < childrenCount; i++) {
Chris Craikab008f02014-05-23 17:55:03 -07003627 // add next child (in child order) to end of list
Alan Viverettea7b85e62016-01-22 10:14:02 -05003628 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3629 final View nextChild = mChildren[childIndex];
3630 final float currentZ = nextChild.getZ();
Chris Craikab008f02014-05-23 17:55:03 -07003631
3632 // insert ahead of any Views with greater Z
3633 int insertIndex = i;
3634 while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {
3635 insertIndex--;
3636 }
3637 mPreSortedChildren.add(insertIndex, nextChild);
3638 }
3639 return mPreSortedChildren;
3640 }
3641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 private void notifyAnimationListener() {
3643 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
3644 mGroupFlags |= FLAG_ANIMATION_DONE;
3645
3646 if (mAnimationListener != null) {
3647 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05003648 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 public void run() {
3650 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
3651 }
3652 };
3653 post(end);
3654 }
3655
Romain Guy849d0a32011-02-01 17:20:48 -08003656 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 }
3658
3659 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08003660 * This method is used to cause children of this ViewGroup to restore or recreate their
3661 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
3662 * to recreate its own display list, which would happen if it went through the normal
3663 * draw/dispatchDraw mechanisms.
3664 *
3665 * @hide
3666 */
3667 @Override
3668 protected void dispatchGetDisplayList() {
3669 final int count = mChildrenCount;
3670 final View[] children = mChildren;
3671 for (int i = 0; i < count; i++) {
3672 final View child = children[i];
John Reckc2330f52015-04-28 13:18:52 -07003673 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) {
Chet Haase6c0665f2014-08-01 13:32:27 -07003674 recreateChildDisplayList(child);
Romain Guy2f57ba52011-02-03 18:03:29 -08003675 }
Chet Haasedaf98e92011-01-10 14:10:36 -08003676 }
Chet Haase91cedf12013-03-11 07:56:30 -07003677 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07003678 View overlayView = mOverlay.getOverlayView();
Chet Haase6c0665f2014-08-01 13:32:27 -07003679 recreateChildDisplayList(overlayView);
Chet Haase91cedf12013-03-11 07:56:30 -07003680 }
Chet Haase6c0665f2014-08-01 13:32:27 -07003681 if (mDisappearingChildren != null) {
3682 final ArrayList<View> disappearingChildren = mDisappearingChildren;
3683 final int disappearingCount = disappearingChildren.size();
3684 for (int i = 0; i < disappearingCount; ++i) {
3685 final View child = disappearingChildren.get(i);
3686 recreateChildDisplayList(child);
3687 }
3688 }
3689 }
3690
3691 private void recreateChildDisplayList(View child) {
Chris Craik31a2d062015-05-01 14:22:47 -07003692 child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0;
Chet Haase6c0665f2014-08-01 13:32:27 -07003693 child.mPrivateFlags &= ~PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -07003694 child.updateDisplayListIfDirty();
Chet Haase6c0665f2014-08-01 13:32:27 -07003695 child.mRecreateDisplayList = false;
Chet Haasedaf98e92011-01-10 14:10:36 -08003696 }
3697
3698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 * Draw one child of this View Group. This method is responsible for getting
3700 * the canvas in the right state. This includes clipping, translating so
3701 * that the child's scrolled origin is at 0, 0, and applying any animation
3702 * transformations.
3703 *
3704 * @param canvas The canvas on which to draw the child
3705 * @param child Who to draw
Chet Haasebcca79a2012-02-14 08:45:14 -08003706 * @param drawingTime The time at which draw is occurring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 * @return True if an invalidate() was issued
3708 */
3709 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Chet Haase64a48c12012-02-13 16:33:29 -08003710 return child.draw(canvas, this, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 }
3712
Alan Viverette922e1c62015-05-05 17:18:27 -07003713 @Override
3714 void getScrollIndicatorBounds(@NonNull Rect out) {
3715 super.getScrollIndicatorBounds(out);
3716
3717 // If we have padding and we're supposed to clip children to that
3718 // padding, offset the scroll indicators to match our clip bounds.
3719 final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
3720 if (clipToPadding) {
3721 out.left += mPaddingLeft;
3722 out.right -= mPaddingRight;
3723 out.top += mPaddingTop;
3724 out.bottom -= mPaddingBottom;
3725 }
3726 }
3727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 /**
Chris Craikd863a102013-12-19 13:31:15 -08003729 * Returns whether this group's children are clipped to their bounds before drawing.
Chet Haase430742f2013-04-12 11:18:36 -07003730 * The default value is true.
3731 * @see #setClipChildren(boolean)
3732 *
3733 * @return True if the group's children will be clipped to their bounds,
3734 * false otherwise.
3735 */
Chris Craik5c75c522014-09-05 14:08:08 -07003736 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haase430742f2013-04-12 11:18:36 -07003737 public boolean getClipChildren() {
3738 return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0);
3739 }
3740
3741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 * By default, children are clipped to their bounds before drawing. This
3743 * allows view groups to override this behavior for animations, etc.
3744 *
3745 * @param clipChildren true to clip children to their bounds,
3746 * false otherwise
3747 * @attr ref android.R.styleable#ViewGroup_clipChildren
3748 */
3749 public void setClipChildren(boolean clipChildren) {
Chet Haasea1cff502012-02-21 13:43:44 -08003750 boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
3751 if (clipChildren != previousValue) {
3752 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
Chet Haase1271e2c2012-04-20 09:54:27 -07003753 for (int i = 0; i < mChildrenCount; ++i) {
3754 View child = getChildAt(i);
Chris Craik64a12e12014-03-28 18:12:12 -07003755 if (child.mRenderNode != null) {
3756 child.mRenderNode.setClipToBounds(clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -08003757 }
3758 }
John Reckaae9f3b2014-07-28 09:30:36 -07003759 invalidate(true);
Chet Haasea1cff502012-02-21 13:43:44 -08003760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 }
3762
3763 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003764 * Sets whether this ViewGroup will clip its children to its padding and resize (but not
3765 * clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003766 * <p>
3767 * By default, children are clipped to the padding of their parent
Doris Liub134b5e2015-05-28 17:26:46 -07003768 * ViewGroup. This clipping behavior is only enabled if padding is non-zero.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 *
Doris Liub134b5e2015-05-28 17:26:46 -07003770 * @param clipToPadding true to clip children to the padding of the group, and resize (but
3771 * not clip) any EdgeEffect to the padded region. False otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3773 */
3774 public void setClipToPadding(boolean clipToPadding) {
John Reck9fa3a242014-06-27 15:57:19 -07003775 if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) {
3776 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
John Reckaae9f3b2014-07-28 09:30:36 -07003777 invalidate(true);
John Reck9fa3a242014-06-27 15:57:19 -07003778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 }
3780
3781 /**
Doris Liub134b5e2015-05-28 17:26:46 -07003782 * Returns whether this ViewGroup will clip its children to its padding, and resize (but
3783 * not clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08003784 * <p>
3785 * By default, children are clipped to the padding of their parent
3786 * Viewgroup. This clipping behavior is only enabled if padding is non-zero.
Adam Powell1c35b082014-07-11 15:37:15 -07003787 *
Doris Liub134b5e2015-05-28 17:26:46 -07003788 * @return true if this ViewGroup clips children to its padding and resizes (but doesn't
3789 * clip) any EdgeEffect to the padded region, false otherwise.
Adam Powell1c35b082014-07-11 15:37:15 -07003790 *
3791 * @attr ref android.R.styleable#ViewGroup_clipToPadding
3792 */
Chris Craik5c75c522014-09-05 14:08:08 -07003793 @ViewDebug.ExportedProperty(category = "drawing")
Adam Powell1c35b082014-07-11 15:37:15 -07003794 public boolean getClipToPadding() {
3795 return hasBooleanFlag(FLAG_CLIP_TO_PADDING);
3796 }
3797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 @Override
3799 public void dispatchSetSelected(boolean selected) {
3800 final View[] children = mChildren;
3801 final int count = mChildrenCount;
3802 for (int i = 0; i < count; i++) {
3803 children[i].setSelected(selected);
3804 }
3805 }
Romain Guy8506ab42009-06-11 17:35:47 -07003806
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003807 @Override
3808 public void dispatchSetActivated(boolean activated) {
3809 final View[] children = mChildren;
3810 final int count = mChildrenCount;
3811 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07003812 children[i].setActivated(activated);
3813 }
3814 }
3815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 @Override
3817 protected void dispatchSetPressed(boolean pressed) {
3818 final View[] children = mChildren;
3819 final int count = mChildrenCount;
3820 for (int i = 0; i < count; i++) {
Adam Powell035a1fc2012-02-27 15:23:50 -08003821 final View child = children[i];
3822 // Children that are clickable on their own should not
3823 // show a pressed state when their parent view does.
3824 // Clearing a pressed state always propagates.
3825 if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
3826 child.setPressed(pressed);
3827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 }
3829 }
3830
Alan Viveretteb942b6f2014-12-08 10:37:39 -08003831 /**
3832 * Dispatches drawable hotspot changes to child views that meet at least
3833 * one of the following criteria:
3834 * <ul>
3835 * <li>Returns {@code false} from both {@link View#isClickable()} and
3836 * {@link View#isLongClickable()}</li>
3837 * <li>Requests duplication of parent state via
3838 * {@link View#setDuplicateParentStateEnabled(boolean)}</li>
3839 * </ul>
3840 *
3841 * @param x hotspot x coordinate
3842 * @param y hotspot y coordinate
3843 * @see #drawableHotspotChanged(float, float)
3844 */
3845 @Override
3846 public void dispatchDrawableHotspotChanged(float x, float y) {
3847 final int count = mChildrenCount;
3848 if (count == 0) {
3849 return;
3850 }
3851
3852 final View[] children = mChildren;
3853 for (int i = 0; i < count; i++) {
3854 final View child = children[i];
3855 // Children that are clickable on their own should not
3856 // receive hotspots when their parent view does.
3857 final boolean nonActionable = !child.isClickable() && !child.isLongClickable();
3858 final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0;
3859 if (nonActionable || duplicatesState) {
3860 final float[] point = getTempPoint();
3861 point[0] = x;
3862 point[1] = y;
3863 transformPointToViewLocal(point, child);
3864 child.drawableHotspotChanged(point[0], point[1]);
3865 }
3866 }
3867 }
3868
Adam Powell14874662013-07-18 19:42:41 -07003869 @Override
3870 void dispatchCancelPendingInputEvents() {
3871 super.dispatchCancelPendingInputEvents();
3872
3873 final View[] children = mChildren;
3874 final int count = mChildrenCount;
3875 for (int i = 0; i < count; i++) {
3876 children[i].dispatchCancelPendingInputEvents();
3877 }
3878 }
3879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 /**
3881 * When this property is set to true, this ViewGroup supports static transformations on
3882 * children; this causes
3883 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
3884 * invoked when a child is drawn.
3885 *
3886 * Any subclass overriding
3887 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
3888 * set this property to true.
3889 *
3890 * @param enabled True to enable static transformations on children, false otherwise.
3891 *
Chet Haase599913d2012-07-23 16:22:05 -07003892 * @see #getChildStaticTransformation(View, android.view.animation.Transformation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 */
3894 protected void setStaticTransformationsEnabled(boolean enabled) {
3895 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
3896 }
3897
3898 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08003899 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
3900 * boolean to indicate whether a static transform was set. The default implementation
3901 * simply returns <code>false</code>; subclasses may override this method for different
Chet Haase599913d2012-07-23 16:22:05 -07003902 * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true
3903 * for this method to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08003905 * @param child The child view whose static transform is being requested
3906 * @param t The Transformation which will hold the result
3907 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07003908 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 */
3910 protected boolean getChildStaticTransformation(View child, Transformation t) {
3911 return false;
3912 }
3913
Romain Guyf6991302013-06-05 17:19:01 -07003914 Transformation getChildTransformation() {
3915 if (mChildTransformation == null) {
3916 mChildTransformation = new Transformation();
3917 }
3918 return mChildTransformation;
3919 }
3920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 /**
3922 * {@hide}
3923 */
3924 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003925 protected View findViewTraversal(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 if (id == mID) {
3927 return this;
3928 }
3929
3930 final View[] where = mChildren;
3931 final int len = mChildrenCount;
3932
3933 for (int i = 0; i < len; i++) {
3934 View v = where[i];
3935
Dianne Hackborn4702a852012-08-17 15:18:29 -07003936 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 v = v.findViewById(id);
3938
3939 if (v != null) {
3940 return v;
3941 }
3942 }
3943 }
3944
3945 return null;
3946 }
3947
3948 /**
3949 * {@hide}
3950 */
3951 @Override
3952 protected View findViewWithTagTraversal(Object tag) {
3953 if (tag != null && tag.equals(mTag)) {
3954 return this;
3955 }
3956
3957 final View[] where = mChildren;
3958 final int len = mChildrenCount;
3959
3960 for (int i = 0; i < len; i++) {
3961 View v = where[i];
3962
Dianne Hackborn4702a852012-08-17 15:18:29 -07003963 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 v = v.findViewWithTag(tag);
3965
3966 if (v != null) {
3967 return v;
3968 }
3969 }
3970 }
3971
3972 return null;
3973 }
3974
3975 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003976 * {@hide}
3977 */
3978 @Override
Jeff Brown4dfbec22011-08-15 14:55:37 -07003979 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003980 if (predicate.apply(this)) {
3981 return this;
3982 }
3983
3984 final View[] where = mChildren;
3985 final int len = mChildrenCount;
3986
3987 for (int i = 0; i < len; i++) {
3988 View v = where[i];
3989
Dianne Hackborn4702a852012-08-17 15:18:29 -07003990 if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08003991 v = v.findViewByPredicate(predicate);
3992
3993 if (v != null) {
3994 return v;
3995 }
3996 }
3997 }
3998
3999 return null;
4000 }
4001
4002 /**
Chet Haasec633d2f2015-04-07 10:29:39 -07004003 * This method adds a view to this container at the specified index purely for the
4004 * purposes of allowing that view to draw even though it is not a normal child of
4005 * the container. That is, the view does not participate in layout, focus, accessibility,
4006 * input, or other normal view operations; it is purely an item to be drawn during the normal
4007 * rendering operation of this container. The index that it is added at is the order
4008 * in which it will be drawn, with respect to the other views in the container.
4009 * For example, a transient view added at index 0 will be drawn before all other views
4010 * in the container because it will be drawn first (including before any real view
4011 * at index 0). There can be more than one transient view at any particular index;
4012 * these views will be drawn in the order in which they were added to the list of
4013 * transient views. The index of transient views can also be greater than the number
4014 * of normal views in the container; that just means that they will be drawn after all
4015 * other views are drawn.
4016 *
4017 * <p>Note that since transient views do not participate in layout, they must be sized
4018 * manually or, more typically, they should just use the size that they had before they
4019 * were removed from their container.</p>
4020 *
4021 * <p>Transient views are useful for handling animations of views that have been removed
4022 * from the container, but which should be animated out after the removal. Adding these
4023 * views as transient views allows them to participate in drawing without side-effecting
4024 * the layout of the container.</p>
4025 *
4026 * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed}
4027 * from the container when they are no longer needed. For example, a transient view
4028 * which is added in order to fade it out in its old location should be removed
4029 * once the animation is complete.</p>
4030 *
4031 * @param view The view to be added
4032 * @param index The index at which this view should be drawn, must be >= 0.
4033 * This value is relative to the {@link #getChildAt(int) index} values in the normal
4034 * child list of this container, where any transient view at a particular index will
4035 * be drawn before any normal child at that same index.
Chris Craik66b41392015-04-17 10:08:10 -07004036 *
4037 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004038 */
4039 public void addTransientView(View view, int index) {
4040 if (index < 0) {
4041 return;
4042 }
4043 if (mTransientIndices == null) {
4044 mTransientIndices = new ArrayList<Integer>();
4045 mTransientViews = new ArrayList<View>();
4046 }
4047 final int oldSize = mTransientIndices.size();
4048 if (oldSize > 0) {
4049 int insertionIndex;
4050 for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) {
4051 if (index < mTransientIndices.get(insertionIndex)) {
4052 break;
4053 }
4054 }
4055 mTransientIndices.add(insertionIndex, index);
4056 mTransientViews.add(insertionIndex, view);
4057 } else {
4058 mTransientIndices.add(index);
4059 mTransientViews.add(view);
4060 }
4061 view.mParent = this;
4062 view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4063 invalidate(true);
4064 }
4065
4066 /**
4067 * Removes a view from the list of transient views in this container. If there is no
4068 * such transient view, this method does nothing.
4069 *
4070 * @param view The transient view to be removed
Chris Craik66b41392015-04-17 10:08:10 -07004071 *
4072 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004073 */
4074 public void removeTransientView(View view) {
4075 if (mTransientViews == null) {
4076 return;
4077 }
4078 final int size = mTransientViews.size();
4079 for (int i = 0; i < size; ++i) {
4080 if (view == mTransientViews.get(i)) {
4081 mTransientViews.remove(i);
4082 mTransientIndices.remove(i);
4083 view.mParent = null;
4084 view.dispatchDetachedFromWindow();
4085 invalidate(true);
4086 return;
4087 }
4088 }
4089 }
4090
4091 /**
4092 * Returns the number of transient views in this container. Specific transient
4093 * views and the index at which they were added can be retrieved via
4094 * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}.
4095 *
4096 * @see #addTransientView(View, int)
4097 * @return The number of transient views in this container
Chris Craik66b41392015-04-17 10:08:10 -07004098 *
4099 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004100 */
4101 public int getTransientViewCount() {
4102 return mTransientIndices == null ? 0 : mTransientIndices.size();
4103 }
4104
4105 /**
4106 * Given a valid position within the list of transient views, returns the index of
4107 * the transient view at that position.
4108 *
4109 * @param position The position of the index being queried. Must be at least 0
4110 * and less than the value returned by {@link #getTransientViewCount()}.
4111 * @return The index of the transient view stored in the given position if the
4112 * position is valid, otherwise -1
Chris Craik66b41392015-04-17 10:08:10 -07004113 *
4114 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004115 */
4116 public int getTransientViewIndex(int position) {
4117 if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) {
4118 return -1;
4119 }
4120 return mTransientIndices.get(position);
4121 }
4122
4123 /**
4124 * Given a valid position within the list of transient views, returns the
4125 * transient view at that position.
4126 *
4127 * @param position The position of the view being queried. Must be at least 0
4128 * and less than the value returned by {@link #getTransientViewCount()}.
4129 * @return The transient view stored in the given position if the
4130 * position is valid, otherwise null
Chris Craik66b41392015-04-17 10:08:10 -07004131 *
4132 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004133 */
4134 public View getTransientView(int position) {
4135 if (mTransientViews == null || position >= mTransientViews.size()) {
4136 return null;
4137 }
4138 return mTransientViews.get(position);
4139 }
4140
4141 /**
Romain Guy393a52c2012-05-22 20:21:08 -07004142 * <p>Adds a child view. If no layout parameters are already set on the child, the
4143 * default parameters for this ViewGroup are set on the child.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004144 *
Romain Guy393a52c2012-05-22 20:21:08 -07004145 * <p><strong>Note:</strong> do not invoke this method from
4146 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4147 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 *
4149 * @param child the child view to add
4150 *
4151 * @see #generateDefaultLayoutParams()
4152 */
4153 public void addView(View child) {
4154 addView(child, -1);
4155 }
4156
4157 /**
4158 * Adds a child view. If no layout parameters are already set on the child, the
4159 * default parameters for this ViewGroup are set on the child.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004160 *
Romain Guy393a52c2012-05-22 20:21:08 -07004161 * <p><strong>Note:</strong> do not invoke this method from
4162 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4163 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 *
4165 * @param child the child view to add
4166 * @param index the position at which to add the child
4167 *
4168 * @see #generateDefaultLayoutParams()
4169 */
4170 public void addView(View child, int index) {
Adam Powell45a9da52014-10-09 09:44:18 -07004171 if (child == null) {
4172 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 LayoutParams params = child.getLayoutParams();
4175 if (params == null) {
4176 params = generateDefaultLayoutParams();
4177 if (params == null) {
4178 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
4179 }
4180 }
4181 addView(child, index, params);
4182 }
4183
4184 /**
4185 * Adds a child view with this ViewGroup's default layout parameters and the
4186 * specified width and height.
4187 *
Romain Guy393a52c2012-05-22 20:21:08 -07004188 * <p><strong>Note:</strong> do not invoke this method from
4189 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4190 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 * @param child the child view to add
4193 */
4194 public void addView(View child, int width, int height) {
4195 final LayoutParams params = generateDefaultLayoutParams();
4196 params.width = width;
4197 params.height = height;
4198 addView(child, -1, params);
4199 }
4200
4201 /**
4202 * Adds a child view with the specified layout parameters.
4203 *
Romain Guy393a52c2012-05-22 20:21:08 -07004204 * <p><strong>Note:</strong> do not invoke this method from
4205 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4206 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 * @param child the child view to add
4209 * @param params the layout parameters to set on the child
4210 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004211 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 public void addView(View child, LayoutParams params) {
4213 addView(child, -1, params);
4214 }
4215
4216 /**
4217 * Adds a child view with the specified layout parameters.
4218 *
Romain Guy393a52c2012-05-22 20:21:08 -07004219 * <p><strong>Note:</strong> do not invoke this method from
4220 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4221 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4222 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 * @param child the child view to add
Alan Viverette77bb6f12015-02-11 17:24:33 -08004224 * @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 -08004225 * @param params the layout parameters to set on the child
4226 */
4227 public void addView(View child, int index, LayoutParams params) {
4228 if (DBG) {
4229 System.out.println(this + " addView");
4230 }
4231
Adam Powell45a9da52014-10-09 09:44:18 -07004232 if (child == null) {
4233 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4234 }
4235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
4237 // therefore, we call requestLayout() on ourselves before, so that the child's request
4238 // will be blocked at our level
4239 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004240 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 addViewInner(child, index, params, false);
4242 }
4243
Alan Viverettebe463f22016-01-21 10:50:10 -05004244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
4246 if (!checkLayoutParams(params)) {
4247 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
4248 }
4249 if (view.mParent != this) {
4250 throw new IllegalArgumentException("Given view not a child of " + this);
4251 }
4252 view.setLayoutParams(params);
4253 }
4254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
4256 return p != null;
4257 }
4258
4259 /**
4260 * Interface definition for a callback to be invoked when the hierarchy
4261 * within this view changed. The hierarchy changes whenever a child is added
4262 * to or removed from this view.
4263 */
4264 public interface OnHierarchyChangeListener {
4265 /**
4266 * Called when a new child is added to a parent view.
4267 *
4268 * @param parent the view in which a child was added
4269 * @param child the new child view added in the hierarchy
4270 */
4271 void onChildViewAdded(View parent, View child);
4272
4273 /**
4274 * Called when a child is removed from a parent view.
4275 *
4276 * @param parent the view from which the child was removed
4277 * @param child the child removed from the hierarchy
4278 */
4279 void onChildViewRemoved(View parent, View child);
4280 }
4281
4282 /**
4283 * Register a callback to be invoked when a child is added to or removed
4284 * from this view.
4285 *
4286 * @param listener the callback to invoke on hierarchy change
4287 */
4288 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
4289 mOnHierarchyChangeListener = listener;
4290 }
4291
Adam Powell6690d012015-06-17 16:41:56 -07004292 void dispatchViewAdded(View child) {
4293 onViewAdded(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004294 if (mOnHierarchyChangeListener != null) {
4295 mOnHierarchyChangeListener.onChildViewAdded(this, child);
4296 }
4297 }
4298
4299 /**
Adam Powell6690d012015-06-17 16:41:56 -07004300 * Called when a new child is added to this ViewGroup. Overrides should always
4301 * call super.onViewAdded.
4302 *
4303 * @param child the added child view
Philip Milnef51d91c2011-07-18 16:12:19 -07004304 */
Adam Powell6690d012015-06-17 16:41:56 -07004305 public void onViewAdded(View child) {
4306 }
4307
4308 void dispatchViewRemoved(View child) {
4309 onViewRemoved(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004310 if (mOnHierarchyChangeListener != null) {
4311 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
4312 }
4313 }
4314
Adam Powell6690d012015-06-17 16:41:56 -07004315 /**
4316 * Called when a child view is removed from this ViewGroup. Overrides should always
4317 * call super.onViewRemoved.
4318 *
4319 * @param child the removed child view
4320 */
4321 public void onViewRemoved(View child) {
4322 }
4323
Philip Milnecfb631b2012-10-26 10:51:46 -07004324 private void clearCachedLayoutMode() {
Svetoslav6254f482013-06-04 17:22:14 -07004325 if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnecfb631b2012-10-26 10:51:46 -07004326 mLayoutMode = LAYOUT_MODE_UNDEFINED;
4327 }
4328 }
4329
4330 @Override
4331 protected void onAttachedToWindow() {
4332 super.onAttachedToWindow();
4333 clearCachedLayoutMode();
4334 }
4335
4336 @Override
4337 protected void onDetachedFromWindow() {
4338 super.onDetachedFromWindow();
4339 clearCachedLayoutMode();
4340 }
4341
Philip Milnef51d91c2011-07-18 16:12:19 -07004342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 * Adds a view during layout. This is useful if in your onLayout() method,
4344 * you need to add more views (as does the list view for example).
4345 *
4346 * If index is negative, it means put it at the end of the list.
4347 *
4348 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004349 * @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 -08004350 * @param params the layout parameters to associate with the child
4351 * @return true if the child was added, false otherwise
4352 */
4353 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
4354 return addViewInLayout(child, index, params, false);
4355 }
4356
4357 /**
4358 * Adds a view during layout. This is useful if in your onLayout() method,
4359 * you need to add more views (as does the list view for example).
4360 *
4361 * If index is negative, it means put it at the end of the list.
4362 *
4363 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08004364 * @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 -08004365 * @param params the layout parameters to associate with the child
4366 * @param preventRequestLayout if true, calling this method will not trigger a
4367 * layout request on child
4368 * @return true if the child was added, false otherwise
4369 */
4370 protected boolean addViewInLayout(View child, int index, LayoutParams params,
4371 boolean preventRequestLayout) {
Adam Powell45a9da52014-10-09 09:44:18 -07004372 if (child == null) {
4373 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 child.mParent = null;
4376 addViewInner(child, index, params, preventRequestLayout);
Dianne Hackborn4702a852012-08-17 15:18:29 -07004377 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 return true;
4379 }
4380
4381 /**
4382 * Prevents the specified child to be laid out during the next layout pass.
4383 *
4384 * @param child the child on which to perform the cleanup
4385 */
4386 protected void cleanupLayoutState(View child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004387 child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 }
4389
4390 private void addViewInner(View child, int index, LayoutParams params,
4391 boolean preventRequestLayout) {
4392
Chet Haasee8e45d32011-03-02 17:07:35 -08004393 if (mTransition != null) {
4394 // Don't prevent other add transitions from completing, but cancel remove
4395 // transitions to let them complete the process before we add to the container
4396 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08004397 }
4398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 if (child.getParent() != null) {
4400 throw new IllegalStateException("The specified child already has a parent. " +
4401 "You must call removeView() on the child's parent first.");
4402 }
4403
Chet Haase21cd1382010-09-01 17:42:29 -07004404 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004405 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004406 }
4407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 if (!checkLayoutParams(params)) {
4409 params = generateLayoutParams(params);
4410 }
4411
4412 if (preventRequestLayout) {
4413 child.mLayoutParams = params;
4414 } else {
4415 child.setLayoutParams(params);
4416 }
4417
4418 if (index < 0) {
4419 index = mChildrenCount;
4420 }
4421
4422 addInArray(child, index);
4423
4424 // tell our children
4425 if (preventRequestLayout) {
4426 child.assignParent(this);
4427 } else {
4428 child.mParent = this;
4429 }
4430
4431 if (child.hasFocus()) {
4432 requestChildFocus(child, child.findFocus());
4433 }
Romain Guy8506ab42009-06-11 17:35:47 -07004434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07004436 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07004437 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 ai.mKeepScreenOn = false;
4439 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
4440 if (ai.mKeepScreenOn) {
4441 needGlobalAttributesUpdate(true);
4442 }
4443 ai.mKeepScreenOn = lastKeepOn;
4444 }
4445
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004446 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglioa7e0bcd2012-10-16 19:55:01 -07004447 child.resetRtlProperties();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07004448 }
4449
Adam Powell6690d012015-06-17 16:41:56 -07004450 dispatchViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451
4452 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
4453 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
4454 }
Adam Powell539ee872012-02-03 19:00:49 -08004455
4456 if (child.hasTransientState()) {
4457 childHasTransientStateChanged(child, true);
4458 }
Svetoslav6254f482013-06-04 17:22:14 -07004459
Svetoslav8e3feb12014-02-24 13:46:47 -08004460 if (child.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004461 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004462 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004463
4464 if (mTransientIndices != null) {
4465 final int transientCount = mTransientIndices.size();
4466 for (int i = 0; i < transientCount; ++i) {
4467 final int oldIndex = mTransientIndices.get(i);
4468 if (index <= oldIndex) {
4469 mTransientIndices.set(i, oldIndex + 1);
4470 }
4471 }
4472 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004473
4474 if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) {
4475 notifyChildOfDragStart(child);
4476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 }
4478
4479 private void addInArray(View child, int index) {
4480 View[] children = mChildren;
4481 final int count = mChildrenCount;
4482 final int size = children.length;
4483 if (index == count) {
4484 if (size == count) {
4485 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4486 System.arraycopy(children, 0, mChildren, 0, size);
4487 children = mChildren;
4488 }
4489 children[mChildrenCount++] = child;
4490 } else if (index < count) {
4491 if (size == count) {
4492 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
4493 System.arraycopy(children, 0, mChildren, 0, index);
4494 System.arraycopy(children, index, mChildren, index + 1, count - index);
4495 children = mChildren;
4496 } else {
4497 System.arraycopy(children, index, children, index + 1, count - index);
4498 }
4499 children[index] = child;
4500 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08004501 if (mLastTouchDownIndex >= index) {
4502 mLastTouchDownIndex++;
4503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 } else {
4505 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
4506 }
4507 }
4508
4509 // This method also sets the child's mParent to null
4510 private void removeFromArray(int index) {
4511 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07004512 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
4513 children[index].mParent = null;
4514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 final int count = mChildrenCount;
4516 if (index == count - 1) {
4517 children[--mChildrenCount] = null;
4518 } else if (index >= 0 && index < count) {
4519 System.arraycopy(children, index + 1, children, index, count - index - 1);
4520 children[--mChildrenCount] = null;
4521 } else {
4522 throw new IndexOutOfBoundsException();
4523 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08004524 if (mLastTouchDownIndex == index) {
4525 mLastTouchDownTime = 0;
4526 mLastTouchDownIndex = -1;
4527 } else if (mLastTouchDownIndex > index) {
4528 mLastTouchDownIndex--;
4529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 }
4531
4532 // This method also sets the children's mParent to null
4533 private void removeFromArray(int start, int count) {
4534 final View[] children = mChildren;
4535 final int childrenCount = mChildrenCount;
4536
4537 start = Math.max(0, start);
4538 final int end = Math.min(childrenCount, start + count);
4539
4540 if (start == end) {
4541 return;
4542 }
4543
4544 if (end == childrenCount) {
4545 for (int i = start; i < end; i++) {
4546 children[i].mParent = null;
4547 children[i] = null;
4548 }
4549 } else {
4550 for (int i = start; i < end; i++) {
4551 children[i].mParent = null;
4552 }
4553
4554 // Since we're looping above, we might as well do the copy, but is arraycopy()
4555 // faster than the extra 2 bounds checks we would do in the loop?
4556 System.arraycopy(children, end, children, start, childrenCount - end);
4557
4558 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
4559 children[i] = null;
4560 }
4561 }
4562
4563 mChildrenCount -= (end - start);
4564 }
4565
4566 private void bindLayoutAnimation(View child) {
4567 Animation a = mLayoutAnimationController.getAnimationForView(child);
4568 child.setAnimation(a);
4569 }
4570
4571 /**
4572 * Subclasses should override this method to set layout animation
4573 * parameters on the supplied child.
4574 *
4575 * @param child the child to associate with animation parameters
4576 * @param params the child's layout parameters which hold the animation
4577 * parameters
4578 * @param index the index of the child in the view group
4579 * @param count the number of children in the view group
4580 */
4581 protected void attachLayoutAnimationParameters(View child,
4582 LayoutParams params, int index, int count) {
4583 LayoutAnimationController.AnimationParameters animationParams =
4584 params.layoutAnimationParameters;
4585 if (animationParams == null) {
4586 animationParams = new LayoutAnimationController.AnimationParameters();
4587 params.layoutAnimationParameters = animationParams;
4588 }
4589
4590 animationParams.count = count;
4591 animationParams.index = index;
4592 }
4593
4594 /**
4595 * {@inheritDoc}
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004596 *
Romain Guy393a52c2012-05-22 20:21:08 -07004597 * <p><strong>Note:</strong> do not invoke this method from
4598 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4599 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004601 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 public void removeView(View view) {
Alan Viverette177ec4602014-10-17 13:34:50 -07004603 if (removeViewInternal(view)) {
4604 requestLayout();
4605 invalidate(true);
4606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 }
4608
4609 /**
4610 * Removes a view during layout. This is useful if in your onLayout() method,
4611 * you need to remove more views.
4612 *
Romain Guy393a52c2012-05-22 20:21:08 -07004613 * <p><strong>Note:</strong> do not invoke this method from
4614 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4615 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 * @param view the view to remove from the group
4618 */
4619 public void removeViewInLayout(View view) {
4620 removeViewInternal(view);
4621 }
4622
4623 /**
4624 * Removes a range of views during layout. This is useful if in your onLayout() method,
4625 * you need to remove more views.
4626 *
Romain Guy393a52c2012-05-22 20:21:08 -07004627 * <p><strong>Note:</strong> do not invoke this method from
4628 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4629 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4630 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 * @param start the index of the first view to remove from the group
4632 * @param count the number of views to remove from the group
4633 */
4634 public void removeViewsInLayout(int start, int count) {
4635 removeViewsInternal(start, count);
4636 }
4637
4638 /**
4639 * Removes the view at the specified position in the group.
4640 *
Romain Guy393a52c2012-05-22 20:21:08 -07004641 * <p><strong>Note:</strong> do not invoke this method from
4642 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4643 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 * @param index the position in the group of the view to remove
4646 */
4647 public void removeViewAt(int index) {
4648 removeViewInternal(index, getChildAt(index));
4649 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004650 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 }
4652
4653 /**
4654 * Removes the specified range of views from the group.
4655 *
Romain Guy393a52c2012-05-22 20:21:08 -07004656 * <p><strong>Note:</strong> do not invoke this method from
4657 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4658 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 * @param start the first position in the group of the range of views to remove
4661 * @param count the number of views to remove
4662 */
4663 public void removeViews(int start, int count) {
4664 removeViewsInternal(start, count);
4665 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004666 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 }
4668
Alan Viverette177ec4602014-10-17 13:34:50 -07004669 private boolean removeViewInternal(View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 final int index = indexOfChild(view);
4671 if (index >= 0) {
4672 removeViewInternal(index, view);
Alan Viverette177ec4602014-10-17 13:34:50 -07004673 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 }
Alan Viverette177ec4602014-10-17 13:34:50 -07004675 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 }
4677
4678 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07004679 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004680 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004681 }
4682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 boolean clearChildFocus = false;
4684 if (view == mFocused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004685 view.unFocus(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 clearChildFocus = true;
4687 }
4688
Alan Viverette632af842014-10-28 13:45:11 -07004689 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004690
Jeff Brown59a422e2012-04-19 15:19:19 -07004691 cancelTouchTarget(view);
4692 cancelHoverTarget(view);
4693
Chet Haase21cd1382010-09-01 17:42:29 -07004694 if (view.getAnimation() != null ||
4695 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 addDisappearingView(view);
4697 } else if (view.mAttachInfo != null) {
4698 view.dispatchDetachedFromWindow();
4699 }
4700
Adam Powell539ee872012-02-03 19:00:49 -08004701 if (view.hasTransientState()) {
4702 childHasTransientStateChanged(view, false);
4703 }
4704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 removeFromArray(index);
4708
4709 if (clearChildFocus) {
4710 clearChildFocus(view);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004711 if (!rootViewRequestFocus()) {
4712 notifyGlobalFocusCleared(this);
4713 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004714 }
Romain Guy6fb05632012-11-29 10:50:33 -08004715
Adam Powell6690d012015-06-17 16:41:56 -07004716 dispatchViewRemoved(view);
Svetoslav6254f482013-06-04 17:22:14 -07004717
Svetoslav8e3feb12014-02-24 13:46:47 -08004718 if (view.getVisibility() != View.GONE) {
Svetoslav00dbe812013-06-10 12:51:09 -07004719 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07004720 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004721
4722 int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
4723 for (int i = 0; i < transientCount; ++i) {
4724 final int oldIndex = mTransientIndices.get(i);
4725 if (index < oldIndex) {
4726 mTransientIndices.set(i, oldIndex - 1);
4727 }
4728 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07004729
4730 if (mCurrentDragStartEvent != null) {
4731 mChildrenInterestedInDrag.remove(view);
4732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004733 }
4734
Chet Haase21cd1382010-09-01 17:42:29 -07004735 /**
4736 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4737 * not null, changes in layout which occur because of children being added to or removed from
4738 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4739 * object. By default, the transition object is null (so layout changes are not animated).
4740 *
Chet Haaseef3cbfd2013-08-21 14:01:02 -07004741 * <p>Replacing a non-null transition will cause that previous transition to be
4742 * canceled, if it is currently running, to restore this container to
4743 * its correct post-transition state.</p>
4744 *
Chet Haase21cd1382010-09-01 17:42:29 -07004745 * @param transition The LayoutTransition object that will animated changes in layout. A value
4746 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07004747 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07004748 */
4749 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07004750 if (mTransition != null) {
Chet Haasefee6f2b2013-08-27 12:22:29 -07004751 LayoutTransition previousTransition = mTransition;
4752 previousTransition.cancel();
4753 previousTransition.removeTransitionListener(mLayoutTransitionListener);
Chet Haaseb20db3e2010-09-10 13:07:30 -07004754 }
Chet Haase21cd1382010-09-01 17:42:29 -07004755 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07004756 if (mTransition != null) {
4757 mTransition.addTransitionListener(mLayoutTransitionListener);
4758 }
4759 }
4760
4761 /**
4762 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
4763 * not null, changes in layout which occur because of children being added to or removed from
4764 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
4765 * object. By default, the transition object is null (so layout changes are not animated).
4766 *
4767 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
4768 * A value of <code>null</code> means no transition will run on layout changes.
4769 */
4770 public LayoutTransition getLayoutTransition() {
4771 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07004772 }
4773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 private void removeViewsInternal(int start, int count) {
Chris Craik18a759d2015-10-14 14:16:33 -07004775 final int end = start + count;
4776
4777 if (start < 0 || count < 0 || end > mChildrenCount) {
4778 throw new IndexOutOfBoundsException();
4779 }
4780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 final View focused = mFocused;
4782 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004783 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784
4785 final View[] children = mChildren;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786
4787 for (int i = start; i < end; i++) {
4788 final View view = children[i];
4789
Chet Haase21cd1382010-09-01 17:42:29 -07004790 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004791 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004792 }
4793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004794 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004795 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004796 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 }
4798
Alan Viverette632af842014-10-28 13:45:11 -07004799 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004800
Jeff Brown59a422e2012-04-19 15:19:19 -07004801 cancelTouchTarget(view);
4802 cancelHoverTarget(view);
4803
Chet Haase21cd1382010-09-01 17:42:29 -07004804 if (view.getAnimation() != null ||
4805 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 addDisappearingView(view);
4807 } else if (detach) {
4808 view.dispatchDetachedFromWindow();
4809 }
4810
Adam Powell539ee872012-02-03 19:00:49 -08004811 if (view.hasTransientState()) {
4812 childHasTransientStateChanged(view, false);
4813 }
4814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004816
Adam Powell6690d012015-06-17 16:41:56 -07004817 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 }
4819
4820 removeFromArray(start, count);
4821
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004822 if (clearChildFocus) {
4823 clearChildFocus(focused);
4824 if (!rootViewRequestFocus()) {
4825 notifyGlobalFocusCleared(focused);
4826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 }
4828 }
4829
4830 /**
4831 * Call this method to remove all child views from the
4832 * ViewGroup.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004833 *
Romain Guy393a52c2012-05-22 20:21:08 -07004834 * <p><strong>Note:</strong> do not invoke this method from
4835 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4836 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 */
4838 public void removeAllViews() {
4839 removeAllViewsInLayout();
4840 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004841 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 }
4843
4844 /**
4845 * Called by a ViewGroup subclass to remove child views from itself,
4846 * when it must first know its size on screen before it can calculate how many
4847 * child views it will render. An example is a Gallery or a ListView, which
4848 * may "have" 50 children, but actually only render the number of children
4849 * that can currently fit inside the object on screen. Do not call
4850 * this method unless you are extending ViewGroup and understand the
4851 * view measuring and layout pipeline.
Romain Guy393a52c2012-05-22 20:21:08 -07004852 *
4853 * <p><strong>Note:</strong> do not invoke this method from
4854 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4855 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004856 */
4857 public void removeAllViewsInLayout() {
4858 final int count = mChildrenCount;
4859 if (count <= 0) {
4860 return;
4861 }
4862
4863 final View[] children = mChildren;
4864 mChildrenCount = 0;
4865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 final View focused = mFocused;
4867 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004868 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869
4870 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07004871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 for (int i = count - 1; i >= 0; i--) {
4873 final View view = children[i];
4874
Chet Haase21cd1382010-09-01 17:42:29 -07004875 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004876 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07004877 }
4878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08004880 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004881 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 }
4883
Alan Viverette632af842014-10-28 13:45:11 -07004884 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004885
Jeff Brown59a422e2012-04-19 15:19:19 -07004886 cancelTouchTarget(view);
4887 cancelHoverTarget(view);
4888
Chet Haase21cd1382010-09-01 17:42:29 -07004889 if (view.getAnimation() != null ||
4890 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 addDisappearingView(view);
4892 } else if (detach) {
4893 view.dispatchDetachedFromWindow();
4894 }
4895
Adam Powell539ee872012-02-03 19:00:49 -08004896 if (view.hasTransientState()) {
4897 childHasTransientStateChanged(view, false);
4898 }
4899
Adam Powell6690d012015-06-17 16:41:56 -07004900 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901
4902 view.mParent = null;
4903 children[i] = null;
4904 }
4905
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004906 if (clearChildFocus) {
4907 clearChildFocus(focused);
4908 if (!rootViewRequestFocus()) {
4909 notifyGlobalFocusCleared(focused);
4910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 }
4912 }
4913
4914 /**
4915 * Finishes the removal of a detached view. This method will dispatch the detached from
4916 * window event and notify the hierarchy change listener.
Chet Haaseca479d42012-08-30 17:20:08 -07004917 * <p>
4918 * This method is intended to be lightweight and makes no assumptions about whether the
4919 * parent or child should be redrawn. Proper use of this method will include also making
4920 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4921 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4922 * which performs a {@link #requestLayout()} on the next frame, after all detach/remove
4923 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 *
4925 * @param child the child to be definitely removed from the view hierarchy
4926 * @param animate if true and the view has an animation, the view is placed in the
4927 * disappearing views list, otherwise, it is detached from the window
4928 *
4929 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
4930 * @see #detachAllViewsFromParent()
4931 * @see #detachViewFromParent(View)
4932 * @see #detachViewFromParent(int)
4933 */
4934 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07004935 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07004936 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07004937 }
4938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 if (child == mFocused) {
4940 child.clearFocus();
4941 }
Romain Guy8506ab42009-06-11 17:35:47 -07004942
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07004943 child.clearAccessibilityFocus();
4944
Jeff Brown59a422e2012-04-19 15:19:19 -07004945 cancelTouchTarget(child);
4946 cancelHoverTarget(child);
4947
Chet Haase21cd1382010-09-01 17:42:29 -07004948 if ((animate && child.getAnimation() != null) ||
4949 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004950 addDisappearingView(child);
4951 } else if (child.mAttachInfo != null) {
4952 child.dispatchDetachedFromWindow();
4953 }
4954
Adam Powell539ee872012-02-03 19:00:49 -08004955 if (child.hasTransientState()) {
4956 childHasTransientStateChanged(child, false);
4957 }
4958
Adam Powell6690d012015-06-17 16:41:56 -07004959 dispatchViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 }
4961
4962 /**
4963 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
Chet Haaseca479d42012-08-30 17:20:08 -07004964 * sets the layout parameters and puts the view in the list of children so that
4965 * it can be retrieved by calling {@link #getChildAt(int)}.
4966 * <p>
4967 * This method is intended to be lightweight and makes no assumptions about whether the
4968 * parent or child should be redrawn. Proper use of this method will include also making
4969 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
4970 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
4971 * which performs a {@link #requestLayout()} on the next frame, after all detach/attach
4972 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
4973 * <p>
4974 * This method should be called only for views which were detached from their parent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 *
4976 * @param child the child to attach
4977 * @param index the index at which the child should be attached
4978 * @param params the layout parameters of the child
4979 *
4980 * @see #removeDetachedView(View, boolean)
4981 * @see #detachAllViewsFromParent()
4982 * @see #detachViewFromParent(View)
4983 * @see #detachViewFromParent(int)
4984 */
4985 protected void attachViewToParent(View child, int index, LayoutParams params) {
4986 child.mLayoutParams = params;
4987
4988 if (index < 0) {
4989 index = mChildrenCount;
4990 }
4991
4992 addInArray(child, index);
4993
4994 child.mParent = this;
Dianne Hackborn4702a852012-08-17 15:18:29 -07004995 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK
4996 & ~PFLAG_DRAWING_CACHE_VALID)
4997 | PFLAG_DRAWN | PFLAG_INVALIDATED;
4998 this.mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999
5000 if (child.hasFocus()) {
5001 requestChildFocus(child, child.findFocus());
5002 }
Adam Powellc5874092016-03-17 16:27:24 -07005003 dispatchVisibilityAggregated(isAttachedToWindow() && getWindowVisibility() == VISIBLE
5004 && isShown());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 }
5006
5007 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005008 * Detaches a view from its parent. Detaching a view should be followed
5009 * either by a call to
5010 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5011 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5012 * temporary; reattachment or removal should happen within the same drawing cycle as
5013 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5014 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 *
5016 * @param child the child to detach
5017 *
5018 * @see #detachViewFromParent(int)
5019 * @see #detachViewsFromParent(int, int)
5020 * @see #detachAllViewsFromParent()
5021 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5022 * @see #removeDetachedView(View, boolean)
5023 */
5024 protected void detachViewFromParent(View child) {
5025 removeFromArray(indexOfChild(child));
5026 }
5027
5028 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005029 * Detaches a view from its parent. Detaching a view should be followed
5030 * either by a call to
5031 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5032 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5033 * temporary; reattachment or removal should happen within the same drawing cycle as
5034 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5035 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 *
5037 * @param index the index of the child to detach
5038 *
5039 * @see #detachViewFromParent(View)
5040 * @see #detachAllViewsFromParent()
5041 * @see #detachViewsFromParent(int, int)
5042 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5043 * @see #removeDetachedView(View, boolean)
5044 */
5045 protected void detachViewFromParent(int index) {
5046 removeFromArray(index);
5047 }
5048
5049 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005050 * Detaches a range of views from their parents. Detaching a view should be followed
5051 * either by a call to
5052 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5053 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5054 * temporary; reattachment or removal should happen within the same drawing cycle as
5055 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5056 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 *
5058 * @param start the first index of the childrend range to detach
5059 * @param count the number of children to detach
5060 *
5061 * @see #detachViewFromParent(View)
5062 * @see #detachViewFromParent(int)
5063 * @see #detachAllViewsFromParent()
5064 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5065 * @see #removeDetachedView(View, boolean)
5066 */
5067 protected void detachViewsFromParent(int start, int count) {
5068 removeFromArray(start, count);
5069 }
5070
5071 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005072 * Detaches all views from the parent. Detaching a view should be followed
5073 * either by a call to
5074 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5075 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5076 * temporary; reattachment or removal should happen within the same drawing cycle as
5077 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5078 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 *
5080 * @see #detachViewFromParent(View)
5081 * @see #detachViewFromParent(int)
5082 * @see #detachViewsFromParent(int, int)
5083 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5084 * @see #removeDetachedView(View, boolean)
5085 */
5086 protected void detachAllViewsFromParent() {
5087 final int count = mChildrenCount;
5088 if (count <= 0) {
5089 return;
5090 }
5091
5092 final View[] children = mChildren;
5093 mChildrenCount = 0;
5094
5095 for (int i = count - 1; i >= 0; i--) {
5096 children[i].mParent = null;
5097 children[i] = null;
5098 }
5099 }
5100
5101 /**
5102 * Don't call or override this method. It is used for the implementation of
5103 * the view hierarchy.
5104 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005105 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 public final void invalidateChild(View child, final Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 ViewParent parent = this;
5108
5109 final AttachInfo attachInfo = mAttachInfo;
5110 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 // If the child is drawing an animation, we want to copy this flag onto
5112 // ourselves and the parent to make sure the invalidate request goes
5113 // through
Dianne Hackborn4702a852012-08-17 15:18:29 -07005114 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION)
5115 == PFLAG_DRAW_ANIMATION;
Romain Guy24443ea2009-05-11 11:56:30 -07005116
Romain Guyfe455af2012-02-15 16:40:20 -08005117 // Check whether the child that requests the invalidate is fully opaque
5118 // Views being animated or transformed are not considered opaque because we may
5119 // be invalidating their old position and need the parent to paint behind them.
5120 Matrix childMatrix = child.getMatrix();
5121 final boolean isOpaque = child.isOpaque() && !drawAnimation &&
5122 child.getAnimation() == null && childMatrix.isIdentity();
5123 // Mark the child as dirty, using the appropriate flag
5124 // Make sure we do not set both flags at the same time
Dianne Hackborn4702a852012-08-17 15:18:29 -07005125 int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005126
John Reck96bb8ad2014-06-19 10:53:03 -07005127 if (child.mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005128 mPrivateFlags |= PFLAG_INVALIDATED;
5129 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guyfe455af2012-02-15 16:40:20 -08005130 }
5131
5132 final int[] location = attachInfo.mInvalidateChildLocation;
5133 location[CHILD_LEFT_INDEX] = child.mLeft;
5134 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase599913d2012-07-23 16:22:05 -07005135 if (!childMatrix.isIdentity() ||
5136 (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Romain Guyfe455af2012-02-15 16:40:20 -08005137 RectF boundingRect = attachInfo.mTmpTransformRect;
5138 boundingRect.set(dirty);
Chet Haase599913d2012-07-23 16:22:05 -07005139 Matrix transformMatrix;
5140 if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
5141 Transformation t = attachInfo.mTmpTransformation;
5142 boolean transformed = getChildStaticTransformation(child, t);
5143 if (transformed) {
5144 transformMatrix = attachInfo.mTmpMatrix;
5145 transformMatrix.set(t.getMatrix());
5146 if (!childMatrix.isIdentity()) {
5147 transformMatrix.preConcat(childMatrix);
5148 }
5149 } else {
5150 transformMatrix = childMatrix;
5151 }
5152 } else {
5153 transformMatrix = childMatrix;
5154 }
5155 transformMatrix.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005156 dirty.set((int) Math.floor(boundingRect.left),
5157 (int) Math.floor(boundingRect.top),
5158 (int) Math.ceil(boundingRect.right),
5159 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005160 }
5161
5162 do {
5163 View view = null;
5164 if (parent instanceof View) {
5165 view = (View) parent;
Romain Guyfe455af2012-02-15 16:40:20 -08005166 }
5167
5168 if (drawAnimation) {
5169 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005170 view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Romain Guyfe455af2012-02-15 16:40:20 -08005171 } else if (parent instanceof ViewRootImpl) {
5172 ((ViewRootImpl) parent).mIsAnimating = true;
5173 }
5174 }
5175
5176 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
5177 // flag coming from the child that initiated the invalidate
5178 if (view != null) {
5179 if ((view.mViewFlags & FADING_EDGE_MASK) != 0 &&
5180 view.getSolidColor() == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005181 opaqueFlag = PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005182 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005183 if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) {
5184 view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag;
Romain Guyfe455af2012-02-15 16:40:20 -08005185 }
5186 }
5187
5188 parent = parent.invalidateChildInParent(location, dirty);
5189 if (view != null) {
5190 // Account for transform on current parent
5191 Matrix m = view.getMatrix();
5192 if (!m.isIdentity()) {
5193 RectF boundingRect = attachInfo.mTmpTransformRect;
5194 boundingRect.set(dirty);
5195 m.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005196 dirty.set((int) Math.floor(boundingRect.left),
5197 (int) Math.floor(boundingRect.top),
5198 (int) Math.ceil(boundingRect.right),
5199 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005200 }
5201 }
5202 } while (parent != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 }
5204 }
5205
5206 /**
5207 * Don't call or override this method. It is used for the implementation of
5208 * the view hierarchy.
5209 *
5210 * This implementation returns null if this ViewGroup does not have a parent,
5211 * if this ViewGroup is already fully invalidated or if the dirty rectangle
5212 * does not intersect with this ViewGroup's bounds.
5213 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005216 if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
5217 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
5219 FLAG_OPTIMIZE_INVALIDATE) {
5220 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
5221 location[CHILD_TOP_INDEX] - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005222 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5223 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225
5226 final int left = mLeft;
5227 final int top = mTop;
5228
Chet Haase05e91ed2012-07-03 14:17:57 -07005229 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5230 if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) {
5231 dirty.setEmpty();
Romain Guy3a3133d2011-02-01 22:59:58 -08005232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005234 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haase05e91ed2012-07-03 14:17:57 -07005235
5236 location[CHILD_LEFT_INDEX] = left;
5237 location[CHILD_TOP_INDEX] = top;
5238
John Reck96bb8ad2014-06-19 10:53:03 -07005239 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005240 mPrivateFlags |= PFLAG_INVALIDATED;
Chet Haase05e91ed2012-07-03 14:17:57 -07005241 }
5242
5243 return mParent;
5244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005246 mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247
5248 location[CHILD_LEFT_INDEX] = mLeft;
5249 location[CHILD_TOP_INDEX] = mTop;
Chet Haasea3db8662011-07-19 10:36:05 -07005250 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
5251 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
5252 } else {
5253 // in case the dirty rect extends outside the bounds of this container
5254 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5255 }
Romain Guy3a3133d2011-02-01 22:59:58 -08005256
John Reck96bb8ad2014-06-19 10:53:03 -07005257 if (mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005258 mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy3a3133d2011-02-01 22:59:58 -08005259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260
5261 return mParent;
5262 }
5263 }
5264
5265 return null;
5266 }
5267
5268 /**
John Recke4267ea2014-06-03 15:53:15 -07005269 * Native-calculated damage path
5270 * Returns false if this path was unable to complete successfully. This means
5271 * it hit a ViewParent it doesn't recognize and needs to fall back to calculating
5272 * damage area
5273 * @hide
5274 */
5275 public boolean damageChildDeferred(View child) {
5276 ViewParent parent = getParent();
5277 while (parent != null) {
5278 if (parent instanceof ViewGroup) {
5279 parent = parent.getParent();
5280 } else if (parent instanceof ViewRootImpl) {
5281 ((ViewRootImpl) parent).invalidate();
5282 return true;
5283 } else {
5284 parent = null;
5285 }
5286 }
5287 return false;
5288 }
5289
5290 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07005291 * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the
5292 * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods
5293 * do; all we want to do here is schedule a traversal with the appropriate dirty rect.
5294 *
5295 * @hide
5296 */
Chris Craik49e6c732014-03-31 12:34:11 -07005297 public void damageChild(View child, final Rect dirty) {
John Recke4267ea2014-06-03 15:53:15 -07005298 if (damageChildDeferred(child)) {
5299 return;
5300 }
5301
Chet Haase9d1992d2012-03-13 11:03:25 -07005302 ViewParent parent = this;
5303
5304 final AttachInfo attachInfo = mAttachInfo;
5305 if (attachInfo != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005306 int left = child.mLeft;
5307 int top = child.mTop;
5308 if (!child.getMatrix().isIdentity()) {
5309 child.transformRect(dirty);
5310 }
5311
5312 do {
5313 if (parent instanceof ViewGroup) {
5314 ViewGroup parentVG = (ViewGroup) parent;
Chet Haaseb85967b2012-03-26 14:37:51 -07005315 if (parentVG.mLayerType != LAYER_TYPE_NONE) {
5316 // Layered parents should be recreated, not just re-issued
5317 parentVG.invalidate();
5318 parent = null;
5319 } else {
Chris Craik49e6c732014-03-31 12:34:11 -07005320 parent = parentVG.damageChildInParent(left, top, dirty);
Chet Haaseb85967b2012-03-26 14:37:51 -07005321 left = parentVG.mLeft;
5322 top = parentVG.mTop;
5323 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005324 } else {
5325 // Reached the top; this calls into the usual invalidate method in
5326 // ViewRootImpl, which schedules a traversal
5327 final int[] location = attachInfo.mInvalidateChildLocation;
5328 location[0] = left;
5329 location[1] = top;
5330 parent = parent.invalidateChildInParent(location, dirty);
5331 }
5332 } while (parent != null);
5333 }
5334 }
5335
5336 /**
5337 * Quick invalidation method that simply transforms the dirty rect into the parent's
5338 * coordinate system, pruning the invalidation if the parent has already been invalidated.
Chet Haasee4a2d7c2013-06-21 17:49:36 -07005339 *
5340 * @hide
Chet Haase9d1992d2012-03-13 11:03:25 -07005341 */
Chris Craik49e6c732014-03-31 12:34:11 -07005342 protected ViewParent damageChildInParent(int left, int top, final Rect dirty) {
5343 if ((mPrivateFlags & PFLAG_DRAWN) != 0
5344 || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) {
Chet Haase9d1992d2012-03-13 11:03:25 -07005345 dirty.offset(left - mScrollX, top - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07005346 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
5347 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
5348 }
Chet Haase9d1992d2012-03-13 11:03:25 -07005349
5350 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 ||
5351 dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) {
5352
Chet Haase9d1992d2012-03-13 11:03:25 -07005353 if (!getMatrix().isIdentity()) {
5354 transformRect(dirty);
5355 }
5356
5357 return mParent;
5358 }
5359 }
5360
5361 return null;
5362 }
5363
5364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 * Offset a rectangle that is in a descendant's coordinate
5366 * space into our coordinate space.
5367 * @param descendant A descendant of this view
5368 * @param rect A rectangle defined in descendant's coordinate space.
5369 */
5370 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
5371 offsetRectBetweenParentAndChild(descendant, rect, true, false);
5372 }
5373
5374 /**
5375 * Offset a rectangle that is in our coordinate space into an ancestor's
5376 * coordinate space.
5377 * @param descendant A descendant of this view
5378 * @param rect A rectangle defined in descendant's coordinate space.
5379 */
5380 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
5381 offsetRectBetweenParentAndChild(descendant, rect, false, false);
5382 }
5383
5384 /**
5385 * Helper method that offsets a rect either from parent to descendant or
5386 * descendant to parent.
5387 */
5388 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
5389 boolean offsetFromChildToParent, boolean clipToBounds) {
5390
5391 // already in the same coord system :)
5392 if (descendant == this) {
5393 return;
5394 }
5395
5396 ViewParent theParent = descendant.mParent;
5397
5398 // search and offset up to the parent
5399 while ((theParent != null)
5400 && (theParent instanceof View)
5401 && (theParent != this)) {
5402
5403 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005404 rect.offset(descendant.mLeft - descendant.mScrollX,
5405 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 if (clipToBounds) {
5407 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005408 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5409 p.mBottom - p.mTop);
5410 if (!intersected) {
5411 rect.setEmpty();
5412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 }
5414 } else {
5415 if (clipToBounds) {
5416 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07005417 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
5418 p.mBottom - p.mTop);
5419 if (!intersected) {
5420 rect.setEmpty();
5421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 }
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005423 rect.offset(descendant.mScrollX - descendant.mLeft,
5424 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 }
5426
5427 descendant = (View) theParent;
5428 theParent = descendant.mParent;
5429 }
5430
5431 // now that we are up to this view, need to offset one more time
5432 // to get into our coordinate space
5433 if (theParent == this) {
5434 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005435 rect.offset(descendant.mLeft - descendant.mScrollX,
5436 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 } else {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00005438 rect.offset(descendant.mScrollX - descendant.mLeft,
5439 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 }
5441 } else {
5442 throw new IllegalArgumentException("parameter must be a descendant of this view");
5443 }
5444 }
5445
5446 /**
5447 * Offset the vertical location of all children of this view by the specified number of pixels.
5448 *
5449 * @param offset the number of pixels to offset
5450 *
5451 * @hide
5452 */
5453 public void offsetChildrenTopAndBottom(int offset) {
5454 final int count = mChildrenCount;
5455 final View[] children = mChildren;
Romain Guy5549cb52013-05-06 18:42:08 -07005456 boolean invalidate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457
5458 for (int i = 0; i < count; i++) {
5459 final View v = children[i];
5460 v.mTop += offset;
5461 v.mBottom += offset;
Chris Craik64a12e12014-03-28 18:12:12 -07005462 if (v.mRenderNode != null) {
Romain Guy5549cb52013-05-06 18:42:08 -07005463 invalidate = true;
Chris Craik64a12e12014-03-28 18:12:12 -07005464 v.mRenderNode.offsetTopAndBottom(offset);
Chet Haasea1cff502012-02-21 13:43:44 -08005465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
Romain Guy5549cb52013-05-06 18:42:08 -07005467
5468 if (invalidate) {
5469 invalidateViewProperty(false, false);
5470 }
Guang Zhu84e25092014-05-01 21:12:55 -07005471 notifySubtreeAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 }
5473
Alan Viverettebe463f22016-01-21 10:50:10 -05005474 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005476 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
5477 // but for some simple tests it can be useful. If we don't have attach info this
5478 // will allocate memory.
5479 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01005480 rect.set(r);
5481
5482 if (!child.hasIdentityMatrix()) {
George Mount002d43d2014-11-11 12:54:43 -08005483 child.getMatrix().mapRect(rect);
Gilles Debunnecea45132011-11-24 02:19:27 +01005484 }
5485
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005486 final int dx = child.mLeft - mScrollX;
5487 final int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01005488
5489 rect.offset(dx, dy);
5490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01005492 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08005493 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
5494 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01005495 position[0] = offset.x;
5496 position[1] = offset.y;
5497 child.getMatrix().mapPoints(position);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005498 offset.x = Math.round(position[0]);
5499 offset.y = Math.round(position[1]);
Gilles Debunnecea45132011-11-24 02:19:27 +01005500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 offset.x += dx;
5502 offset.y += dy;
5503 }
Gilles Debunnecea45132011-11-24 02:19:27 +01005504
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005505 final int width = mRight - mLeft;
5506 final int height = mBottom - mTop;
5507
George Mount002d43d2014-11-11 12:54:43 -08005508 boolean rectIsVisible = true;
Adam Powell35da41e2014-12-10 18:59:34 -08005509 if (mParent == null ||
5510 (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005511 // Clip to bounds.
5512 rectIsVisible = rect.intersect(0, 0, width, height);
Gilles Debunnecea45132011-11-24 02:19:27 +01005513 }
5514
George Mount002d43d2014-11-11 12:54:43 -08005515 if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005516 // Clip to padding.
George Mount002d43d2014-11-11 12:54:43 -08005517 rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop,
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005518 width - mPaddingRight, height - mPaddingBottom);
George Mount002d43d2014-11-11 12:54:43 -08005519 }
5520
5521 if (rectIsVisible && mClipBounds != null) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08005522 // Clip to clipBounds.
George Mount002d43d2014-11-11 12:54:43 -08005523 rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right,
5524 mClipBounds.bottom);
5525 }
Alan Viverettec45b1d42015-11-16 15:38:59 -05005526 r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top),
5527 (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom));
George Mount002d43d2014-11-11 12:54:43 -08005528 if (rectIsVisible && mParent != null) {
5529 rectIsVisible = mParent.getChildVisibleRect(this, r, offset);
5530 }
5531 return rectIsVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 }
5533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 @Override
Chet Haase9c087442011-01-12 16:20:16 -08005535 public final void layout(int l, int t, int r, int b) {
Chet Haase430742f2013-04-12 11:18:36 -07005536 if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
Chet Haase7dd4a532012-04-16 13:35:09 -07005537 if (mTransition != null) {
5538 mTransition.layoutChange(this);
5539 }
Chet Haase9c087442011-01-12 16:20:16 -08005540 super.layout(l, t, r, b);
5541 } else {
5542 // record the fact that we noop'd it; request layout when transition finishes
Chet Haaseb9895022013-04-02 15:10:58 -07005543 mLayoutCalledWhileSuppressed = true;
Chet Haase9c087442011-01-12 16:20:16 -08005544 }
5545 }
5546
Chet Haase9c087442011-01-12 16:20:16 -08005547 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 protected abstract void onLayout(boolean changed,
5549 int l, int t, int r, int b);
5550
5551 /**
5552 * Indicates whether the view group has the ability to animate its children
5553 * after the first layout.
5554 *
5555 * @return true if the children can be animated, false otherwise
5556 */
5557 protected boolean canAnimate() {
5558 return mLayoutAnimationController != null;
5559 }
5560
5561 /**
5562 * Runs the layout animation. Calling this method triggers a relayout of
5563 * this view group.
5564 */
5565 public void startLayoutAnimation() {
5566 if (mLayoutAnimationController != null) {
5567 mGroupFlags |= FLAG_RUN_ANIMATION;
5568 requestLayout();
5569 }
5570 }
5571
5572 /**
5573 * Schedules the layout animation to be played after the next layout pass
5574 * of this view group. This can be used to restart the layout animation
5575 * when the content of the view group changes or when the activity is
5576 * paused and resumed.
5577 */
5578 public void scheduleLayoutAnimation() {
5579 mGroupFlags |= FLAG_RUN_ANIMATION;
5580 }
5581
5582 /**
5583 * Sets the layout animation controller used to animate the group's
5584 * children after the first layout.
5585 *
5586 * @param controller the animation controller
5587 */
5588 public void setLayoutAnimation(LayoutAnimationController controller) {
5589 mLayoutAnimationController = controller;
5590 if (mLayoutAnimationController != null) {
5591 mGroupFlags |= FLAG_RUN_ANIMATION;
5592 }
5593 }
5594
5595 /**
5596 * Returns the layout animation controller used to animate the group's
5597 * children.
5598 *
5599 * @return the current animation controller
5600 */
5601 public LayoutAnimationController getLayoutAnimation() {
5602 return mLayoutAnimationController;
5603 }
5604
5605 /**
5606 * Indicates whether the children's drawing cache is used during a layout
5607 * animation. By default, the drawing cache is enabled but this will prevent
5608 * nested layout animations from working. To nest animations, you must disable
5609 * the cache.
5610 *
5611 * @return true if the animation cache is enabled, false otherwise
5612 *
5613 * @see #setAnimationCacheEnabled(boolean)
5614 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005615 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005616 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005617 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 public boolean isAnimationCacheEnabled() {
5620 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
5621 }
5622
5623 /**
5624 * Enables or disables the children's drawing cache during a layout animation.
5625 * By default, the drawing cache is enabled but this will prevent nested
5626 * layout animations from working. To nest animations, you must disable the
5627 * cache.
5628 *
5629 * @param enabled true to enable the animation cache, false otherwise
5630 *
5631 * @see #isAnimationCacheEnabled()
5632 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005633 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005634 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005635 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 */
5637 public void setAnimationCacheEnabled(boolean enabled) {
5638 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
5639 }
5640
5641 /**
5642 * Indicates whether this ViewGroup will always try to draw its children using their
5643 * drawing cache. By default this property is enabled.
5644 *
5645 * @return true if the animation cache is enabled, false otherwise
5646 *
5647 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5648 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5649 * @see View#setDrawingCacheEnabled(boolean)
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 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 public boolean isAlwaysDrawnWithCacheEnabled() {
5655 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
5656 }
5657
5658 /**
5659 * Indicates whether this ViewGroup will always try to draw its children using their
5660 * drawing cache. This property can be set to true when the cache rendering is
5661 * slightly different from the children's normal rendering. Renderings can be different,
5662 * for instance, when the cache's quality is set to low.
5663 *
5664 * When this property is disabled, the ViewGroup will use the drawing cache of its
5665 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
5666 * when to start using the drawing cache and when to stop using it.
5667 *
5668 * @param always true to always draw with the drawing cache, false otherwise
5669 *
5670 * @see #isAlwaysDrawnWithCacheEnabled()
5671 * @see #setChildrenDrawnWithCacheEnabled(boolean)
5672 * @see View#setDrawingCacheEnabled(boolean)
5673 * @see View#setDrawingCacheQuality(int)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005674 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005675 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005676 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 */
5678 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
5679 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
5680 }
5681
5682 /**
5683 * Indicates whether the ViewGroup is currently drawing its children using
5684 * their drawing cache.
5685 *
5686 * @return true if children should be drawn with their cache, false otherwise
5687 *
5688 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5689 * @see #setChildrenDrawnWithCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07005690 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005691 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005692 * Child views may no longer be forced to cache their rendering state by their parents.
5693 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 protected boolean isChildrenDrawnWithCacheEnabled() {
5696 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
5697 }
5698
5699 /**
5700 * Tells the ViewGroup to draw its children using their drawing cache. This property
5701 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
5702 * will be used only if it has been enabled.
5703 *
5704 * Subclasses should call this method to start and stop using the drawing cache when
5705 * they perform performance sensitive operations, like scrolling or animating.
5706 *
5707 * @param enabled true if children should be drawn with their cache, false otherwise
5708 *
5709 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
5710 * @see #isChildrenDrawnWithCacheEnabled()
Chris Craik5a6bbae2015-04-10 17:41:34 -07005711 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07005712 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07005713 * Child views may no longer be forced to cache their rendering state by their parents.
5714 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 */
5716 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
5717 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
5718 }
5719
Romain Guy293451e2009-11-04 13:59:48 -08005720 /**
5721 * Indicates whether the ViewGroup is drawing its children in the order defined by
5722 * {@link #getChildDrawingOrder(int, int)}.
5723 *
5724 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
5725 * false otherwise
5726 *
5727 * @see #setChildrenDrawingOrderEnabled(boolean)
5728 * @see #getChildDrawingOrder(int, int)
5729 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005730 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08005731 protected boolean isChildrenDrawingOrderEnabled() {
5732 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
5733 }
5734
5735 /**
5736 * Tells the ViewGroup whether to draw its children in the order defined by the method
5737 * {@link #getChildDrawingOrder(int, int)}.
Chris Craike83cbd42014-09-03 17:52:24 -07005738 * <p>
5739 * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)},
5740 * will override custom child ordering done via this method.
Romain Guy293451e2009-11-04 13:59:48 -08005741 *
5742 * @param enabled true if the order of the children when drawing is determined by
5743 * {@link #getChildDrawingOrder(int, int)}, false otherwise
5744 *
5745 * @see #isChildrenDrawingOrderEnabled()
5746 * @see #getChildDrawingOrder(int, int)
5747 */
5748 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
5749 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
5750 }
5751
Svetoslav6254f482013-06-04 17:22:14 -07005752 private boolean hasBooleanFlag(int flag) {
Philip Milnef091b662013-02-27 11:15:21 -08005753 return (mGroupFlags & flag) == flag;
5754 }
5755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 private void setBooleanFlag(int flag, boolean value) {
5757 if (value) {
5758 mGroupFlags |= flag;
5759 } else {
5760 mGroupFlags &= ~flag;
5761 }
5762 }
5763
5764 /**
5765 * Returns an integer indicating what types of drawing caches are kept in memory.
5766 *
5767 * @see #setPersistentDrawingCache(int)
5768 * @see #setAnimationCacheEnabled(boolean)
5769 *
5770 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
5771 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5772 * and {@link #PERSISTENT_ALL_CACHES}
5773 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005774 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07005776 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
5778 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
5779 })
5780 public int getPersistentDrawingCache() {
5781 return mPersistentDrawingCache;
5782 }
5783
5784 /**
5785 * Indicates what types of drawing caches should be kept in memory after
5786 * they have been created.
5787 *
5788 * @see #getPersistentDrawingCache()
5789 * @see #setAnimationCacheEnabled(boolean)
5790 *
5791 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
5792 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
5793 * and {@link #PERSISTENT_ALL_CACHES}
5794 */
5795 public void setPersistentDrawingCache(int drawingCacheToKeep) {
5796 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
5797 }
5798
Philip Milnef091b662013-02-27 11:15:21 -08005799 private void setLayoutMode(int layoutMode, boolean explicitly) {
5800 mLayoutMode = layoutMode;
5801 setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly);
5802 }
5803
5804 /**
5805 * Recursively traverse the view hierarchy, resetting the layoutMode of any
5806 * descendants that had inherited a different layoutMode from a previous parent.
5807 * Recursion terminates when a descendant's mode is:
5808 * <ul>
5809 * <li>Undefined</li>
5810 * <li>The same as the root node's</li>
5811 * <li>A mode that had been explicitly set</li>
5812 * <ul/>
5813 * The first two clauses are optimizations.
5814 * @param layoutModeOfRoot
5815 */
5816 @Override
5817 void invalidateInheritedLayoutMode(int layoutModeOfRoot) {
5818 if (mLayoutMode == LAYOUT_MODE_UNDEFINED ||
5819 mLayoutMode == layoutModeOfRoot ||
Svetoslav6254f482013-06-04 17:22:14 -07005820 hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnef091b662013-02-27 11:15:21 -08005821 return;
5822 }
5823 setLayoutMode(LAYOUT_MODE_UNDEFINED, false);
5824
5825 // apply recursively
5826 for (int i = 0, N = getChildCount(); i < N; i++) {
5827 getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot);
5828 }
5829 }
5830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005832 * Returns the basis of alignment during layout operations on this ViewGroup:
Philip Milne7b757812012-09-19 18:13:44 -07005833 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milnecfb631b2012-10-26 10:51:46 -07005834 * <p>
5835 * If no layoutMode was explicitly set, either programmatically or in an XML resource,
5836 * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
5837 * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005838 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005839 * @return the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005840 *
5841 * @see #setLayoutMode(int)
5842 */
5843 public int getLayoutMode() {
Philip Milnecfb631b2012-10-26 10:51:46 -07005844 if (mLayoutMode == LAYOUT_MODE_UNDEFINED) {
Philip Milnef091b662013-02-27 11:15:21 -08005845 int inheritedLayoutMode = (mParent instanceof ViewGroup) ?
5846 ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT;
5847 setLayoutMode(inheritedLayoutMode, false);
Philip Milnecfb631b2012-10-26 10:51:46 -07005848 }
Philip Milne1557fd72012-04-04 23:41:34 -07005849 return mLayoutMode;
5850 }
5851
5852 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07005853 * Sets the basis of alignment during the layout of this ViewGroup.
Philip Milne7b757812012-09-19 18:13:44 -07005854 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
5855 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07005856 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07005857 * @param layoutMode the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07005858 *
5859 * @see #getLayoutMode()
Scott Main27a85082013-06-10 10:39:48 -07005860 * @attr ref android.R.styleable#ViewGroup_layoutMode
Philip Milne1557fd72012-04-04 23:41:34 -07005861 */
5862 public void setLayoutMode(int layoutMode) {
5863 if (mLayoutMode != layoutMode) {
Philip Milnef091b662013-02-27 11:15:21 -08005864 invalidateInheritedLayoutMode(layoutMode);
5865 setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED);
Philip Milne1557fd72012-04-04 23:41:34 -07005866 requestLayout();
5867 }
5868 }
5869
5870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 * Returns a new set of layout parameters based on the supplied attributes set.
5872 *
5873 * @param attrs the attributes to build the layout parameters from
5874 *
5875 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5876 * of its descendants
5877 */
5878 public LayoutParams generateLayoutParams(AttributeSet attrs) {
5879 return new LayoutParams(getContext(), attrs);
5880 }
5881
5882 /**
5883 * Returns a safe set of layout parameters based on the supplied layout params.
5884 * When a ViewGroup is passed a View whose layout params do not pass the test of
5885 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
5886 * is invoked. This method should return a new set of layout params suitable for
5887 * this ViewGroup, possibly by copying the appropriate attributes from the
5888 * specified set of layout params.
5889 *
5890 * @param p The layout parameters to convert into a suitable set of layout parameters
5891 * for this ViewGroup.
5892 *
5893 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
5894 * of its descendants
5895 */
5896 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Chet Haase4610eef2015-12-03 07:38:11 -08005897 return p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 }
5899
5900 /**
5901 * Returns a set of default layout parameters. These parameters are requested
5902 * when the View passed to {@link #addView(View)} has no layout parameters
5903 * already set. If null is returned, an exception is thrown from addView.
5904 *
5905 * @return a set of default layout parameters or null
5906 */
5907 protected LayoutParams generateDefaultLayoutParams() {
5908 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
5909 }
5910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 @Override
5912 protected void debug(int depth) {
5913 super.debug(depth);
5914 String output;
5915
5916 if (mFocused != null) {
5917 output = debugIndent(depth);
5918 output += "mFocused";
5919 Log.d(VIEW_LOG_TAG, output);
5920 }
5921 if (mChildrenCount != 0) {
5922 output = debugIndent(depth);
5923 output += "{";
5924 Log.d(VIEW_LOG_TAG, output);
5925 }
5926 int count = mChildrenCount;
5927 for (int i = 0; i < count; i++) {
5928 View child = mChildren[i];
5929 child.debug(depth + 1);
5930 }
5931
5932 if (mChildrenCount != 0) {
5933 output = debugIndent(depth);
5934 output += "}";
5935 Log.d(VIEW_LOG_TAG, output);
5936 }
5937 }
5938
5939 /**
5940 * Returns the position in the group of the specified child view.
5941 *
5942 * @param child the view for which to get the position
5943 * @return a positive integer representing the position of the view in the
5944 * group, or -1 if the view does not exist in the group
5945 */
5946 public int indexOfChild(View child) {
5947 final int count = mChildrenCount;
5948 final View[] children = mChildren;
5949 for (int i = 0; i < count; i++) {
5950 if (children[i] == child) {
5951 return i;
5952 }
5953 }
5954 return -1;
5955 }
5956
5957 /**
5958 * Returns the number of children in the group.
5959 *
5960 * @return a positive integer representing the number of children in
5961 * the group
5962 */
5963 public int getChildCount() {
5964 return mChildrenCount;
5965 }
5966
5967 /**
5968 * Returns the view at the specified position in the group.
5969 *
5970 * @param index the position at which to get the view from
5971 * @return the view at the specified position or null if the position
5972 * does not exist within the group
5973 */
5974 public View getChildAt(int index) {
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005975 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 return null;
5977 }
Adam Powell3ba8f5d62011-03-07 15:36:33 -08005978 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 }
5980
5981 /**
5982 * Ask all of the children of this view to measure themselves, taking into
5983 * account both the MeasureSpec requirements for this view and its padding.
5984 * We skip children that are in the GONE state The heavy lifting is done in
5985 * getChildMeasureSpec.
5986 *
5987 * @param widthMeasureSpec The width requirements for this view
5988 * @param heightMeasureSpec The height requirements for this view
5989 */
5990 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
5991 final int size = mChildrenCount;
5992 final View[] children = mChildren;
5993 for (int i = 0; i < size; ++i) {
5994 final View child = children[i];
5995 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
5996 measureChild(child, widthMeasureSpec, heightMeasureSpec);
5997 }
5998 }
5999 }
6000
6001 /**
6002 * Ask one of the children of this view to measure itself, taking into
6003 * account both the MeasureSpec requirements for this view and its padding.
6004 * The heavy lifting is done in getChildMeasureSpec.
6005 *
6006 * @param child The child to measure
6007 * @param parentWidthMeasureSpec The width requirements for this view
6008 * @param parentHeightMeasureSpec The height requirements for this view
6009 */
6010 protected void measureChild(View child, int parentWidthMeasureSpec,
6011 int parentHeightMeasureSpec) {
6012 final LayoutParams lp = child.getLayoutParams();
6013
6014 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6015 mPaddingLeft + mPaddingRight, lp.width);
6016 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6017 mPaddingTop + mPaddingBottom, lp.height);
6018
6019 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6020 }
6021
6022 /**
6023 * Ask one of the children of this view to measure itself, taking into
6024 * account both the MeasureSpec requirements for this view and its padding
6025 * and margins. The child must have MarginLayoutParams The heavy lifting is
6026 * done in getChildMeasureSpec.
6027 *
6028 * @param child The child to measure
6029 * @param parentWidthMeasureSpec The width requirements for this view
6030 * @param widthUsed Extra space that has been used up by the parent
6031 * horizontally (possibly by other children of the parent)
6032 * @param parentHeightMeasureSpec The height requirements for this view
6033 * @param heightUsed Extra space that has been used up by the parent
6034 * vertically (possibly by other children of the parent)
6035 */
6036 protected void measureChildWithMargins(View child,
6037 int parentWidthMeasureSpec, int widthUsed,
6038 int parentHeightMeasureSpec, int heightUsed) {
6039 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
6040
6041 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6042 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
6043 + widthUsed, lp.width);
6044 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6045 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
6046 + heightUsed, lp.height);
6047
6048 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6049 }
6050
6051 /**
6052 * Does the hard part of measureChildren: figuring out the MeasureSpec to
6053 * pass to a particular child. This method figures out the right MeasureSpec
6054 * for one dimension (height or width) of one child view.
6055 *
6056 * The goal is to combine information from our MeasureSpec with the
6057 * LayoutParams of the child to get the best possible results. For example,
6058 * if the this view knows its size (because its MeasureSpec has a mode of
6059 * EXACTLY), and the child has indicated in its LayoutParams that it wants
6060 * to be the same size as the parent, the parent should ask the child to
6061 * layout given an exact size.
6062 *
6063 * @param spec The requirements for this view
6064 * @param padding The padding of this view for the current dimension and
6065 * margins, if applicable
6066 * @param childDimension How big the child wants to be in the current
6067 * dimension
6068 * @return a MeasureSpec integer for the child
6069 */
6070 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
6071 int specMode = MeasureSpec.getMode(spec);
6072 int specSize = MeasureSpec.getSize(spec);
6073
6074 int size = Math.max(0, specSize - padding);
6075
6076 int resultSize = 0;
6077 int resultMode = 0;
6078
6079 switch (specMode) {
6080 // Parent has imposed an exact size on us
6081 case MeasureSpec.EXACTLY:
6082 if (childDimension >= 0) {
6083 resultSize = childDimension;
6084 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006085 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 // Child wants to be our size. So be it.
6087 resultSize = size;
6088 resultMode = MeasureSpec.EXACTLY;
6089 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6090 // Child wants to determine its own size. It can't be
6091 // bigger than us.
6092 resultSize = size;
6093 resultMode = MeasureSpec.AT_MOST;
6094 }
6095 break;
6096
6097 // Parent has imposed a maximum size on us
6098 case MeasureSpec.AT_MOST:
6099 if (childDimension >= 0) {
6100 // Child wants a specific size... so be it
6101 resultSize = childDimension;
6102 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006103 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 // Child wants to be our size, but our size is not fixed.
6105 // Constrain child to not be bigger than us.
6106 resultSize = size;
6107 resultMode = MeasureSpec.AT_MOST;
6108 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6109 // Child wants to determine its own size. It can't be
6110 // bigger than us.
6111 resultSize = size;
6112 resultMode = MeasureSpec.AT_MOST;
6113 }
6114 break;
6115
6116 // Parent asked to see how big we want to be
6117 case MeasureSpec.UNSPECIFIED:
6118 if (childDimension >= 0) {
6119 // Child wants a specific size... let him have it
6120 resultSize = childDimension;
6121 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006122 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006123 // Child wants to be our size... find out how big it should
6124 // be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006125 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 resultMode = MeasureSpec.UNSPECIFIED;
6127 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6128 // Child wants to determine its own size.... find out how
6129 // big it should be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006130 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 resultMode = MeasureSpec.UNSPECIFIED;
6132 }
6133 break;
6134 }
Tor Norbye67568642015-03-31 07:47:23 -07006135 //noinspection ResourceType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006136 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
6137 }
6138
6139
6140 /**
6141 * Removes any pending animations for views that have been removed. Call
6142 * this if you don't want animations for exiting views to stack up.
6143 */
6144 public void clearDisappearingChildren() {
John Reckca7a9da2014-03-05 16:29:07 -08006145 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6146 if (disappearingChildren != null) {
6147 final int count = disappearingChildren.size();
6148 for (int i = 0; i < count; i++) {
6149 final View view = disappearingChildren.get(i);
6150 if (view.mAttachInfo != null) {
6151 view.dispatchDetachedFromWindow();
6152 }
6153 view.clearAnimation();
6154 }
6155 disappearingChildren.clear();
Chet Haaseb85967b2012-03-26 14:37:51 -07006156 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 }
6158 }
6159
6160 /**
6161 * Add a view which is removed from mChildren but still needs animation
6162 *
6163 * @param v View to add
6164 */
6165 private void addDisappearingView(View v) {
6166 ArrayList<View> disappearingChildren = mDisappearingChildren;
6167
6168 if (disappearingChildren == null) {
6169 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
6170 }
6171
6172 disappearingChildren.add(v);
6173 }
6174
6175 /**
6176 * Cleanup a view when its animation is done. This may mean removing it from
6177 * the list of disappearing views.
6178 *
6179 * @param view The view whose animation has finished
6180 * @param animation The animation, cannot be null
6181 */
Chet Haase64a48c12012-02-13 16:33:29 -08006182 void finishAnimatingView(final View view, Animation animation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6184 if (disappearingChildren != null) {
6185 if (disappearingChildren.contains(view)) {
6186 disappearingChildren.remove(view);
6187
6188 if (view.mAttachInfo != null) {
6189 view.dispatchDetachedFromWindow();
6190 }
6191
6192 view.clearAnimation();
6193 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6194 }
6195 }
6196
6197 if (animation != null && !animation.getFillAfter()) {
6198 view.clearAnimation();
6199 }
6200
Dianne Hackborn4702a852012-08-17 15:18:29 -07006201 if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006202 view.onAnimationEnd();
6203 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
6204 // so we'd rather be safe than sorry
Dianne Hackborn4702a852012-08-17 15:18:29 -07006205 view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 // Draw one more frame after the animation is done
6207 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6208 }
6209 }
6210
Chet Haaseb20db3e2010-09-10 13:07:30 -07006211 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07006212 * Utility function called by View during invalidation to determine whether a view that
6213 * is invisible or gone should still be invalidated because it is being transitioned (and
6214 * therefore still needs to be drawn).
6215 */
6216 boolean isViewTransitioning(View view) {
6217 return (mTransitioningViews != null && mTransitioningViews.contains(view));
6218 }
6219
6220 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07006221 * This method tells the ViewGroup that the given View object, which should have this
6222 * ViewGroup as its parent,
6223 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
6224 * is removed from its parent. This allows animations, such as those used by
6225 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
6226 * the removal of views. A call to this method should always be accompanied by a later call
6227 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
6228 * so that the View finally gets removed.
6229 *
6230 * @param view The View object to be kept visible even if it gets removed from its parent.
6231 */
6232 public void startViewTransition(View view) {
6233 if (view.mParent == this) {
6234 if (mTransitioningViews == null) {
6235 mTransitioningViews = new ArrayList<View>();
6236 }
6237 mTransitioningViews.add(view);
6238 }
6239 }
6240
6241 /**
6242 * This method should always be called following an earlier call to
6243 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
6244 * and will no longer be displayed. Note that this method does not perform the functionality
6245 * of removing a view from its parent; it just discontinues the display of a View that
6246 * has previously been removed.
6247 *
6248 * @return view The View object that has been removed but is being kept around in the visible
6249 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
6250 */
6251 public void endViewTransition(View view) {
6252 if (mTransitioningViews != null) {
6253 mTransitioningViews.remove(view);
6254 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6255 if (disappearingChildren != null && disappearingChildren.contains(view)) {
6256 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006257 if (mVisibilityChangingChildren != null &&
6258 mVisibilityChangingChildren.contains(view)) {
6259 mVisibilityChangingChildren.remove(view);
6260 } else {
6261 if (view.mAttachInfo != null) {
6262 view.dispatchDetachedFromWindow();
6263 }
6264 if (view.mParent != null) {
6265 view.mParent = null;
6266 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07006267 }
Chet Haaseb85967b2012-03-26 14:37:51 -07006268 invalidate();
Chet Haaseb20db3e2010-09-10 13:07:30 -07006269 }
6270 }
6271 }
6272
Chet Haase21cd1382010-09-01 17:42:29 -07006273 private LayoutTransition.TransitionListener mLayoutTransitionListener =
6274 new LayoutTransition.TransitionListener() {
6275 @Override
6276 public void startTransition(LayoutTransition transition, ViewGroup container,
6277 View view, int transitionType) {
6278 // We only care about disappearing items, since we need special logic to keep
6279 // those items visible after they've been 'removed'
6280 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006281 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006282 }
6283 }
6284
6285 @Override
6286 public void endTransition(LayoutTransition transition, ViewGroup container,
6287 View view, int transitionType) {
Chet Haaseb9895022013-04-02 15:10:58 -07006288 if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) {
Chet Haase9c087442011-01-12 16:20:16 -08006289 requestLayout();
Chet Haaseb9895022013-04-02 15:10:58 -07006290 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08006291 }
Chet Haase21cd1382010-09-01 17:42:29 -07006292 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07006293 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07006294 }
6295 }
6296 };
6297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 /**
Chet Haaseb9895022013-04-02 15:10:58 -07006299 * Tells this ViewGroup to suppress all layout() calls until layout
6300 * suppression is disabled with a later call to suppressLayout(false).
6301 * When layout suppression is disabled, a requestLayout() call is sent
6302 * if layout() was attempted while layout was being suppressed.
6303 *
6304 * @hide
6305 */
6306 public void suppressLayout(boolean suppress) {
6307 mSuppressLayout = suppress;
6308 if (!suppress) {
6309 if (mLayoutCalledWhileSuppressed) {
6310 requestLayout();
6311 mLayoutCalledWhileSuppressed = false;
6312 }
6313 }
6314 }
6315
6316 /**
Chet Haase199acdf2013-07-24 18:40:55 -07006317 * Returns whether layout calls on this container are currently being
6318 * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}.
6319 *
6320 * @return true if layout calls are currently suppressed, false otherwise.
6321 *
6322 * @hide
6323 */
6324 public boolean isLayoutSuppressed() {
6325 return mSuppressLayout;
6326 }
6327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 @Override
6329 public boolean gatherTransparentRegion(Region region) {
6330 // If no transparent regions requested, we are always opaque.
Dianne Hackborn4702a852012-08-17 15:18:29 -07006331 final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 if (meOpaque && region == null) {
6333 // The caller doesn't care about the region, so stop now.
6334 return true;
6335 }
6336 super.gatherTransparentRegion(region);
6337 final View[] children = mChildren;
6338 final int count = mChildrenCount;
6339 boolean noneOfTheChildrenAreTransparent = true;
6340 for (int i = 0; i < count; i++) {
6341 final View child = children[i];
Mathias Agopiane3381152010-12-02 15:19:36 -08006342 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343 if (!child.gatherTransparentRegion(region)) {
6344 noneOfTheChildrenAreTransparent = false;
6345 }
6346 }
6347 }
6348 return meOpaque || noneOfTheChildrenAreTransparent;
6349 }
6350
Alan Viverettebe463f22016-01-21 10:50:10 -05006351 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 public void requestTransparentRegion(View child) {
6353 if (child != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006354 child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006355 if (mParent != null) {
6356 mParent.requestTransparentRegion(this);
6357 }
6358 }
6359 }
Romain Guy8506ab42009-06-11 17:35:47 -07006360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 @Override
Adam Powell46e38fd2014-02-03 10:16:49 -08006362 public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
6363 insets = super.dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006364 if (!insets.isConsumed()) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006365 final int count = getChildCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 for (int i = 0; i < count; i++) {
Adam Powell46e38fd2014-02-03 10:16:49 -08006367 insets = getChildAt(i).dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07006368 if (insets.isConsumed()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 break;
6370 }
6371 }
6372 }
Adam Powell46e38fd2014-02-03 10:16:49 -08006373 return insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 }
6375
6376 /**
6377 * Returns the animation listener to which layout animation events are
6378 * sent.
6379 *
6380 * @return an {@link android.view.animation.Animation.AnimationListener}
6381 */
6382 public Animation.AnimationListener getLayoutAnimationListener() {
6383 return mAnimationListener;
6384 }
6385
6386 @Override
6387 protected void drawableStateChanged() {
6388 super.drawableStateChanged();
6389
6390 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
6391 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6392 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
6393 + " child has duplicateParentState set to true");
6394 }
6395
6396 final View[] children = mChildren;
6397 final int count = mChildrenCount;
6398
6399 for (int i = 0; i < count; i++) {
6400 final View child = children[i];
6401 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
6402 child.refreshDrawableState();
6403 }
6404 }
6405 }
6406 }
6407
6408 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07006409 public void jumpDrawablesToCurrentState() {
6410 super.jumpDrawablesToCurrentState();
6411 final View[] children = mChildren;
6412 final int count = mChildrenCount;
6413 for (int i = 0; i < count; i++) {
6414 children[i].jumpDrawablesToCurrentState();
6415 }
6416 }
6417
6418 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 protected int[] onCreateDrawableState(int extraSpace) {
6420 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
6421 return super.onCreateDrawableState(extraSpace);
6422 }
6423
6424 int need = 0;
6425 int n = getChildCount();
6426 for (int i = 0; i < n; i++) {
6427 int[] childState = getChildAt(i).getDrawableState();
6428
6429 if (childState != null) {
6430 need += childState.length;
6431 }
6432 }
6433
6434 int[] state = super.onCreateDrawableState(extraSpace + need);
6435
6436 for (int i = 0; i < n; i++) {
6437 int[] childState = getChildAt(i).getDrawableState();
6438
6439 if (childState != null) {
6440 state = mergeDrawableStates(state, childState);
6441 }
6442 }
6443
6444 return state;
6445 }
6446
6447 /**
6448 * Sets whether this ViewGroup's drawable states also include
6449 * its children's drawable states. This is used, for example, to
6450 * make a group appear to be focused when its child EditText or button
6451 * is focused.
6452 */
6453 public void setAddStatesFromChildren(boolean addsStates) {
6454 if (addsStates) {
6455 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
6456 } else {
6457 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
6458 }
6459
6460 refreshDrawableState();
6461 }
6462
6463 /**
6464 * Returns whether this ViewGroup's drawable states also include
6465 * its children's drawable states. This is used, for example, to
6466 * make a group appear to be focused when its child EditText or button
6467 * is focused.
6468 */
6469 public boolean addStatesFromChildren() {
6470 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
6471 }
6472
6473 /**
Jeff Smitha45746e2012-07-19 14:19:24 -05006474 * If {@link #addStatesFromChildren} is true, refreshes this group's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 * drawable state (to include the states from its children).
6476 */
Alan Viverettebe463f22016-01-21 10:50:10 -05006477 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 public void childDrawableStateChanged(View child) {
6479 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
6480 refreshDrawableState();
6481 }
6482 }
6483
6484 /**
6485 * Specifies the animation listener to which layout animation events must
6486 * be sent. Only
6487 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
6488 * and
6489 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
6490 * are invoked.
6491 *
6492 * @param animationListener the layout animation listener
6493 */
6494 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
6495 mAnimationListener = animationListener;
6496 }
6497
6498 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006499 * This method is called by LayoutTransition when there are 'changing' animations that need
6500 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
6501 * starts all pending transitions prior to the drawing phase in the current traversal.
6502 *
6503 * @param transition The LayoutTransition to be started on the next traversal.
6504 *
6505 * @hide
6506 */
6507 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006508 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07006509 if (viewAncestor != null) {
6510 viewAncestor.requestTransitionStart(transition);
6511 }
Chet Haasecca2c982011-05-20 14:34:18 -07006512 }
6513
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006514 /**
6515 * @hide
6516 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006517 @Override
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006518 public boolean resolveRtlPropertiesIfNeeded() {
6519 final boolean result = super.resolveRtlPropertiesIfNeeded();
6520 // We dont need to resolve the children RTL properties if nothing has changed for the parent
6521 if (result) {
6522 int count = getChildCount();
6523 for (int i = 0; i < count; i++) {
6524 final View child = getChildAt(i);
6525 if (child.isLayoutDirectionInherited()) {
6526 child.resolveRtlPropertiesIfNeeded();
6527 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006528 }
6529 }
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07006530 return result;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006531 }
6532
6533 /**
6534 * @hide
6535 */
6536 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006537 public boolean resolveLayoutDirection() {
6538 final boolean result = super.resolveLayoutDirection();
6539 if (result) {
6540 int count = getChildCount();
6541 for (int i = 0; i < count; i++) {
6542 final View child = getChildAt(i);
6543 if (child.isLayoutDirectionInherited()) {
6544 child.resolveLayoutDirection();
6545 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006546 }
6547 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006548 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006549 }
6550
6551 /**
6552 * @hide
6553 */
6554 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006555 public boolean resolveTextDirection() {
6556 final boolean result = super.resolveTextDirection();
6557 if (result) {
6558 int count = getChildCount();
6559 for (int i = 0; i < count; i++) {
6560 final View child = getChildAt(i);
6561 if (child.isTextDirectionInherited()) {
6562 child.resolveTextDirection();
6563 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006564 }
6565 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006566 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006567 }
6568
6569 /**
6570 * @hide
6571 */
6572 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006573 public boolean resolveTextAlignment() {
6574 final boolean result = super.resolveTextAlignment();
6575 if (result) {
6576 int count = getChildCount();
6577 for (int i = 0; i < count; i++) {
6578 final View child = getChildAt(i);
6579 if (child.isTextAlignmentInherited()) {
6580 child.resolveTextAlignment();
6581 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006582 }
6583 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006584 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006585 }
6586
6587 /**
6588 * @hide
6589 */
6590 @Override
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006591 public void resolvePadding() {
6592 super.resolvePadding();
6593 int count = getChildCount();
6594 for (int i = 0; i < count; i++) {
6595 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006596 if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006597 child.resolvePadding();
6598 }
6599 }
6600 }
6601
6602 /**
6603 * @hide
6604 */
6605 @Override
6606 protected void resolveDrawables() {
6607 super.resolveDrawables();
6608 int count = getChildCount();
6609 for (int i = 0; i < count; i++) {
6610 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08006611 if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006612 child.resolveDrawables();
6613 }
6614 }
6615 }
6616
Fabrice Di Meglio1e0ed6b2012-10-18 16:06:52 -07006617 /**
6618 * @hide
6619 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -07006620 @Override
6621 public void resolveLayoutParams() {
6622 super.resolveLayoutParams();
6623 int count = getChildCount();
6624 for (int i = 0; i < count; i++) {
6625 final View child = getChildAt(i);
6626 child.resolveLayoutParams();
6627 }
6628 }
6629
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006630 /**
6631 * @hide
6632 */
6633 @Override
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006634 public void resetResolvedLayoutDirection() {
6635 super.resetResolvedLayoutDirection();
6636
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006637 int count = getChildCount();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006638 for (int i = 0; i < count; i++) {
6639 final View child = getChildAt(i);
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006640 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07006641 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006642 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006643 }
6644 }
6645
6646 /**
6647 * @hide
6648 */
6649 @Override
6650 public void resetResolvedTextDirection() {
6651 super.resetResolvedTextDirection();
6652
6653 int count = getChildCount();
6654 for (int i = 0; i < count; i++) {
6655 final View child = getChildAt(i);
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -07006656 if (child.isTextDirectionInherited()) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006657 child.resetResolvedTextDirection();
6658 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006659 }
6660 }
6661
6662 /**
6663 * @hide
6664 */
6665 @Override
6666 public void resetResolvedTextAlignment() {
6667 super.resetResolvedTextAlignment();
6668
6669 int count = getChildCount();
6670 for (int i = 0; i < count; i++) {
6671 final View child = getChildAt(i);
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -07006672 if (child.isTextAlignmentInherited()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07006673 child.resetResolvedTextAlignment();
6674 }
6675 }
6676 }
6677
Fabrice Di Meglio22268862011-06-27 18:13:18 -07006678 /**
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07006679 * @hide
6680 */
6681 @Override
6682 public void resetResolvedPadding() {
6683 super.resetResolvedPadding();
6684
6685 int count = getChildCount();
6686 for (int i = 0; i < count; i++) {
6687 final View child = getChildAt(i);
6688 if (child.isLayoutDirectionInherited()) {
6689 child.resetResolvedPadding();
6690 }
6691 }
6692 }
6693
6694 /**
6695 * @hide
6696 */
6697 @Override
6698 protected void resetResolvedDrawables() {
6699 super.resetResolvedDrawables();
6700
6701 int count = getChildCount();
6702 for (int i = 0; i < count; i++) {
6703 final View child = getChildAt(i);
6704 if (child.isLayoutDirectionInherited()) {
6705 child.resetResolvedDrawables();
6706 }
6707 }
6708 }
6709
6710 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006711 * Return true if the pressed state should be delayed for children or descendants of this
6712 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
6713 * This prevents the pressed state from appearing when the user is actually trying to scroll
6714 * the content.
6715 *
6716 * The default implementation returns true for compatibility reasons. Subclasses that do
6717 * not scroll should generally override this method and return false.
6718 */
6719 public boolean shouldDelayChildPressedState() {
6720 return true;
6721 }
6722
Adam Powell10ba2772014-04-15 09:46:51 -07006723 /**
6724 * @inheritDoc
6725 */
6726 @Override
6727 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6728 return false;
6729 }
6730
6731 /**
6732 * @inheritDoc
6733 */
6734 @Override
6735 public void onNestedScrollAccepted(View child, View target, int axes) {
6736 mNestedScrollAxes = axes;
6737 }
6738
6739 /**
6740 * @inheritDoc
6741 *
6742 * <p>The default implementation of onStopNestedScroll calls
6743 * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p>
6744 */
6745 @Override
6746 public void onStopNestedScroll(View child) {
6747 // Stop any recursive nested scrolling.
6748 stopNestedScroll();
Adam Powelld4a22d42015-04-16 15:44:10 -07006749 mNestedScrollAxes = 0;
Adam Powell10ba2772014-04-15 09:46:51 -07006750 }
6751
6752 /**
6753 * @inheritDoc
6754 */
6755 @Override
6756 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6757 int dxUnconsumed, int dyUnconsumed) {
Chris Banes61d50702016-03-23 13:11:45 +00006758 // Re-dispatch up the tree by default
6759 dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
Adam Powell10ba2772014-04-15 09:46:51 -07006760 }
6761
6762 /**
6763 * @inheritDoc
6764 */
6765 @Override
6766 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
Chris Banes61d50702016-03-23 13:11:45 +00006767 // Re-dispatch up the tree by default
6768 dispatchNestedPreScroll(dx, dy, consumed, null);
Adam Powell10ba2772014-04-15 09:46:51 -07006769 }
6770
6771 /**
6772 * @inheritDoc
6773 */
6774 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07006775 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Chris Banes61d50702016-03-23 13:11:45 +00006776 // Re-dispatch up the tree by default
6777 return dispatchNestedFling(velocityX, velocityY, consumed);
Adam Powell10ba2772014-04-15 09:46:51 -07006778 }
6779
6780 /**
Adam Powellb72be592014-07-16 21:41:31 -07006781 * @inheritDoc
6782 */
6783 @Override
6784 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
Chris Banes61d50702016-03-23 13:11:45 +00006785 // Re-dispatch up the tree by default
6786 return dispatchNestedPreFling(velocityX, velocityY);
Adam Powellb72be592014-07-16 21:41:31 -07006787 }
6788
6789 /**
Adam Powell10ba2772014-04-15 09:46:51 -07006790 * Return the current axes of nested scrolling for this ViewGroup.
6791 *
6792 * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently
6793 * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p>
6794 *
6795 * @return Flags indicating the current axes of nested scrolling
6796 * @see #SCROLL_AXIS_HORIZONTAL
6797 * @see #SCROLL_AXIS_VERTICAL
6798 * @see #SCROLL_AXIS_NONE
6799 */
6800 public int getNestedScrollAxes() {
6801 return mNestedScrollAxes;
6802 }
6803
Philip Milned7dd8902012-01-26 16:55:30 -08006804 /** @hide */
6805 protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
6806 }
6807
George Mounte1803372014-02-26 19:00:52 +00006808 /** @hide */
6809 @Override
6810 public void captureTransitioningViews(List<View> transitioningViews) {
6811 if (getVisibility() != View.VISIBLE) {
6812 return;
6813 }
6814 if (isTransitionGroup()) {
6815 transitioningViews.add(this);
6816 } else {
6817 int count = getChildCount();
6818 for (int i = 0; i < count; i++) {
6819 View child = getChildAt(i);
6820 child.captureTransitioningViews(transitioningViews);
6821 }
6822 }
6823 }
6824
6825 /** @hide */
6826 @Override
George Mountabb352a2014-05-09 10:27:20 -07006827 public void findNamedViews(Map<String, View> namedElements) {
George Mountfe361d22014-07-08 17:25:25 -07006828 if (getVisibility() != VISIBLE && mGhostView == null) {
George Mounte1803372014-02-26 19:00:52 +00006829 return;
6830 }
George Mountabb352a2014-05-09 10:27:20 -07006831 super.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006832 int count = getChildCount();
6833 for (int i = 0; i < count; i++) {
6834 View child = getChildAt(i);
George Mountabb352a2014-05-09 10:27:20 -07006835 child.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00006836 }
6837 }
6838
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 * LayoutParams are used by views to tell their parents how they want to be
6841 * laid out. See
6842 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
6843 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07006844 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 * <p>
6846 * The base LayoutParams class just describes how big the view wants to be
6847 * for both width and height. For each dimension, it can specify one of:
6848 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006849 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
6850 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
6852 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006853 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006854 * </ul>
6855 * There are subclasses of LayoutParams for different subclasses of
6856 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07006857 * LayoutParams which adds an X and Y value.</p>
6858 *
6859 * <div class="special reference">
6860 * <h3>Developer Guides</h3>
6861 * <p>For more information about creating user interface layouts, read the
6862 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
6863 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 *
6865 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
6866 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
6867 */
6868 public static class LayoutParams {
6869 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006870 * Special value for the height or width requested by a View.
6871 * FILL_PARENT means that the view wants to be as big as its parent,
6872 * minus the parent's padding, if any. This value is deprecated
6873 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 */
Romain Guy980a9382010-01-08 15:06:28 -08006875 @SuppressWarnings({"UnusedDeclaration"})
6876 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 public static final int FILL_PARENT = -1;
6878
6879 /**
6880 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08006881 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006882 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08006883 */
6884 public static final int MATCH_PARENT = -1;
6885
6886 /**
6887 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 * WRAP_CONTENT means that the view wants to be just large enough to fit
6889 * its own internal content, taking its own padding into account.
6890 */
6891 public static final int WRAP_CONTENT = -2;
6892
6893 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006894 * Information about how wide the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006895 * constants FILL_PARENT (replaced by MATCH_PARENT
6896 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006897 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006898 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006899 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6901 })
6902 public int width;
6903
6904 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006905 * Information about how tall the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07006906 * constants FILL_PARENT (replaced by MATCH_PARENT
6907 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006909 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08006910 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
6912 })
6913 public int height;
6914
6915 /**
6916 * Used to animate layouts.
6917 */
6918 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
6919
6920 /**
6921 * Creates a new set of layout parameters. The values are extracted from
6922 * the supplied attributes set and context. The XML attributes mapped
6923 * to this set of layout parameters are:
6924 *
6925 * <ul>
6926 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006927 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6928 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006930 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
6931 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 * </ul>
6933 *
6934 * @param c the application environment
6935 * @param attrs the set of attributes from which to extract the layout
6936 * parameters' values
6937 */
6938 public LayoutParams(Context c, AttributeSet attrs) {
6939 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
6940 setBaseAttributes(a,
6941 R.styleable.ViewGroup_Layout_layout_width,
6942 R.styleable.ViewGroup_Layout_layout_height);
6943 a.recycle();
6944 }
6945
6946 /**
6947 * Creates a new set of layout parameters with the specified width
6948 * and height.
6949 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07006950 * @param width the width, either {@link #WRAP_CONTENT},
6951 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6952 * API Level 8), or a fixed size in pixels
6953 * @param height the height, either {@link #WRAP_CONTENT},
6954 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
6955 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 */
6957 public LayoutParams(int width, int height) {
6958 this.width = width;
6959 this.height = height;
6960 }
6961
6962 /**
6963 * Copy constructor. Clones the width and height values of the source.
6964 *
6965 * @param source The layout params to copy from.
6966 */
6967 public LayoutParams(LayoutParams source) {
6968 this.width = source.width;
6969 this.height = source.height;
6970 }
6971
6972 /**
6973 * Used internally by MarginLayoutParams.
6974 * @hide
6975 */
6976 LayoutParams() {
6977 }
6978
6979 /**
Dave Burke579e1402012-10-18 20:41:55 -07006980 * Extracts the layout parameters from the supplied attributes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 *
6982 * @param a the style attributes to extract the parameters from
6983 * @param widthAttr the identifier of the width attribute
6984 * @param heightAttr the identifier of the height attribute
6985 */
6986 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
Dave Burke579e1402012-10-18 20:41:55 -07006987 width = a.getLayoutDimension(widthAttr, "layout_width");
6988 height = a.getLayoutDimension(heightAttr, "layout_height");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 }
6990
6991 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07006992 * Resolve layout parameters depending on the layout direction. Subclasses that care about
6993 * layoutDirection changes should override this method. The default implementation does
6994 * nothing.
6995 *
6996 * @param layoutDirection the direction of the layout
6997 *
6998 * {@link View#LAYOUT_DIRECTION_LTR}
6999 * {@link View#LAYOUT_DIRECTION_RTL}
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007000 */
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007001 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007002 }
7003
7004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 * Returns a String representation of this set of layout parameters.
7006 *
7007 * @param output the String to prepend to the internal representation
7008 * @return a String with the following format: output +
7009 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07007010 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 * @hide
7012 */
7013 public String debug(String output) {
7014 return output + "ViewGroup.LayoutParams={ width="
7015 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
7016 }
7017
7018 /**
Philip Milne10ca24a2012-04-23 15:38:27 -07007019 * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
7020 *
7021 * @param view the view that contains these layout parameters
7022 * @param canvas the canvas on which to draw
7023 *
7024 * @hide
7025 */
Philip Milne7b757812012-09-19 18:13:44 -07007026 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07007027 }
7028
7029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030 * Converts the specified size to a readable String.
7031 *
7032 * @param size the size to convert
7033 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07007034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 * @hide
7036 */
7037 protected static String sizeToString(int size) {
7038 if (size == WRAP_CONTENT) {
7039 return "wrap-content";
7040 }
Romain Guy980a9382010-01-08 15:06:28 -08007041 if (size == MATCH_PARENT) {
7042 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 }
7044 return String.valueOf(size);
7045 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007046
7047 /** @hide */
7048 void encode(@NonNull ViewHierarchyEncoder encoder) {
7049 encoder.beginObject(this);
7050 encodeProperties(encoder);
7051 encoder.endObject();
7052 }
7053
7054 /** @hide */
7055 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7056 encoder.addProperty("width", width);
7057 encoder.addProperty("height", height);
7058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 }
7060
7061 /**
7062 * Per-child layout information for layouts that support margins.
7063 * See
7064 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
7065 * for a list of all child view attributes that this class supports.
7066 */
7067 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
7068 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007069 * The left margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007070 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7071 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007073 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 public int leftMargin;
7075
7076 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007077 * The top margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007078 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7079 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007081 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 public int topMargin;
7083
7084 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007085 * The right margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007086 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7087 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007089 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 public int rightMargin;
7091
7092 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007093 * The bottom margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007094 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7095 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007097 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098 public int bottomMargin;
7099
7100 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007101 * The start margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007102 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7103 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007104 */
7105 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007106 private int startMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007107
7108 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007109 * The end margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007110 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7111 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007112 */
7113 @ViewDebug.ExportedProperty(category = "layout")
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007114 private int endMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007115
7116 /**
7117 * The default start and end margin.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007118 * @hide
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007119 */
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007120 public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007121
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007122 /**
7123 * Bit 0: layout direction
7124 * Bit 1: layout direction
7125 * Bit 2: left margin undefined
7126 * Bit 3: right margin undefined
7127 * Bit 4: is RTL compatibility mode
7128 * Bit 5: need resolution
7129 *
7130 * Bit 6 to 7 not used
7131 *
7132 * @hide
7133 */
7134 @ViewDebug.ExportedProperty(category = "layout", flagMapping = {
7135 @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK,
7136 equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"),
7137 @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK,
7138 equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"),
7139 @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK,
7140 equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"),
7141 @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK,
7142 equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"),
7143 @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK,
7144 equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK")
Jon Miranda4597e982014-07-29 07:25:49 -07007145 }, formatToHexString = true)
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007146 byte mMarginFlags;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007147
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007148 private static final int LAYOUT_DIRECTION_MASK = 0x00000003;
7149 private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004;
7150 private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008;
7151 private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010;
7152 private static final int NEED_RESOLUTION_MASK = 0x00000020;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007153
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007154 private static final int DEFAULT_MARGIN_RESOLVED = 0;
7155 private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007156
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 * Creates a new set of layout parameters. The values are extracted from
7159 * the supplied attributes set and context.
7160 *
7161 * @param c the application environment
7162 * @param attrs the set of attributes from which to extract the layout
7163 * parameters' values
7164 */
7165 public MarginLayoutParams(Context c, AttributeSet attrs) {
7166 super();
7167
7168 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
7169 setBaseAttributes(a,
7170 R.styleable.ViewGroup_MarginLayout_layout_width,
7171 R.styleable.ViewGroup_MarginLayout_layout_height);
7172
7173 int margin = a.getDimensionPixelSize(
7174 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
7175 if (margin >= 0) {
7176 leftMargin = margin;
7177 topMargin = margin;
7178 rightMargin= margin;
7179 bottomMargin = margin;
7180 } else {
7181 leftMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007182 R.styleable.ViewGroup_MarginLayout_layout_marginLeft,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007183 UNDEFINED_MARGIN);
7184 if (leftMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007185 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007186 leftMargin = DEFAULT_MARGIN_RESOLVED;
7187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 rightMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007189 R.styleable.ViewGroup_MarginLayout_layout_marginRight,
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007190 UNDEFINED_MARGIN);
7191 if (rightMargin == UNDEFINED_MARGIN) {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007192 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007193 rightMargin = DEFAULT_MARGIN_RESOLVED;
7194 }
7195
7196 topMargin = a.getDimensionPixelSize(
7197 R.styleable.ViewGroup_MarginLayout_layout_marginTop,
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007198 DEFAULT_MARGIN_RESOLVED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 bottomMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007200 R.styleable.ViewGroup_MarginLayout_layout_marginBottom,
7201 DEFAULT_MARGIN_RESOLVED);
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007202
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007203 startMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007204 R.styleable.ViewGroup_MarginLayout_layout_marginStart,
7205 DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007206 endMargin = a.getDimensionPixelSize(
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007207 R.styleable.ViewGroup_MarginLayout_layout_marginEnd,
7208 DEFAULT_MARGIN_RELATIVE);
7209
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007210 if (isMarginRelative()) {
7211 mMarginFlags |= NEED_RESOLUTION_MASK;
7212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 }
7214
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007215 final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
7216 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007217 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
7218 mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
7219 }
7220
7221 // Layout direction is LTR by default
7222 mMarginFlags |= LAYOUT_DIRECTION_LTR;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 a.recycle();
7225 }
7226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 public MarginLayoutParams(int width, int height) {
7228 super(width, height);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007229
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007230 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7231 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007232
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007233 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7234 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 }
7236
7237 /**
7238 * Copy constructor. Clones the width, height and margin values of the source.
7239 *
7240 * @param source The layout params to copy from.
7241 */
7242 public MarginLayoutParams(MarginLayoutParams source) {
7243 this.width = source.width;
7244 this.height = source.height;
7245
7246 this.leftMargin = source.leftMargin;
7247 this.topMargin = source.topMargin;
7248 this.rightMargin = source.rightMargin;
7249 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007250 this.startMargin = source.startMargin;
7251 this.endMargin = source.endMargin;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007252
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007253 this.mMarginFlags = source.mMarginFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 }
7255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 public MarginLayoutParams(LayoutParams source) {
7257 super(source);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007258
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007259 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
7260 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007261
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007262 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7263 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 }
7265
7266 /**
Adam Powelld7600832014-07-01 15:22:50 -07007267 * @hide Used internally.
7268 */
7269 public final void copyMarginsFrom(MarginLayoutParams source) {
7270 this.leftMargin = source.leftMargin;
7271 this.topMargin = source.topMargin;
7272 this.rightMargin = source.rightMargin;
7273 this.bottomMargin = source.bottomMargin;
7274 this.startMargin = source.startMargin;
7275 this.endMargin = source.endMargin;
7276
7277 this.mMarginFlags = source.mMarginFlags;
7278 }
7279
7280 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007281 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
7282 * to be done so that the new margins are taken into account. Left and right margins may be
7283 * overriden by {@link android.view.View#requestLayout()} depending on layout direction.
Adam Powella7a735f2014-10-09 12:54:52 -07007284 * Margin values should be positive.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 *
7286 * @param left the left margin size
7287 * @param top the top margin size
7288 * @param right the right margin size
7289 * @param bottom the bottom margin size
7290 *
7291 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
7292 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7293 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
7294 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7295 */
7296 public void setMargins(int left, int top, int right, int bottom) {
7297 leftMargin = left;
7298 topMargin = top;
7299 rightMargin = right;
7300 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007301 mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
7302 mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
7303 if (isMarginRelative()) {
7304 mMarginFlags |= NEED_RESOLUTION_MASK;
7305 } else {
7306 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007309
7310 /**
7311 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
7312 * needs to be done so that the new relative margins are taken into account. Left and right
7313 * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout
Adam Powella7a735f2014-10-09 12:54:52 -07007314 * direction. Margin values should be positive.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007315 *
7316 * @param start the start margin size
7317 * @param top the top margin size
7318 * @param end the right margin size
7319 * @param bottom the bottom margin size
7320 *
7321 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7322 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7323 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7324 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7325 *
7326 * @hide
7327 */
7328 public void setMarginsRelative(int start, int top, int end, int bottom) {
7329 startMargin = start;
7330 topMargin = top;
7331 endMargin = end;
7332 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007333 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007334 }
7335
7336 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007337 * Sets the relative start margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007338 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007339 * @param start the start margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007340 *
7341 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7342 */
7343 public void setMarginStart(int start) {
7344 startMargin = start;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007345 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007346 }
7347
7348 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007349 * Returns the start margin in pixels.
7350 *
7351 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7352 *
7353 * @return the start margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007354 */
7355 public int getMarginStart() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007356 if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007357 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007358 doResolveMargins();
7359 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007360 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007361 case View.LAYOUT_DIRECTION_RTL:
7362 return rightMargin;
7363 case View.LAYOUT_DIRECTION_LTR:
7364 default:
7365 return leftMargin;
7366 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007367 }
7368
7369 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007370 * Sets the relative end margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007371 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07007372 * @param end the end margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007373 *
7374 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7375 */
7376 public void setMarginEnd(int end) {
7377 endMargin = end;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007378 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07007379 }
7380
7381 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007382 * Returns the end margin in pixels.
7383 *
7384 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7385 *
7386 * @return the end margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007387 */
7388 public int getMarginEnd() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007389 if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007390 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007391 doResolveMargins();
7392 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007393 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007394 case View.LAYOUT_DIRECTION_RTL:
7395 return leftMargin;
7396 case View.LAYOUT_DIRECTION_LTR:
7397 default:
7398 return rightMargin;
7399 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007400 }
7401
7402 /**
7403 * Check if margins are relative.
7404 *
7405 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7406 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
7407 *
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007408 * @return true if either marginStart or marginEnd has been set.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007409 */
7410 public boolean isMarginRelative() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007411 return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007412 }
7413
7414 /**
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007415 * Set the layout direction
7416 * @param layoutDirection the layout direction.
7417 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
7418 * or {@link View#LAYOUT_DIRECTION_RTL}.
7419 */
7420 public void setLayoutDirection(int layoutDirection) {
7421 if (layoutDirection != View.LAYOUT_DIRECTION_LTR &&
7422 layoutDirection != View.LAYOUT_DIRECTION_RTL) return;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007423 if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) {
7424 mMarginFlags &= ~LAYOUT_DIRECTION_MASK;
7425 mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK);
7426 if (isMarginRelative()) {
7427 mMarginFlags |= NEED_RESOLUTION_MASK;
7428 } else {
7429 mMarginFlags &= ~NEED_RESOLUTION_MASK;
7430 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007431 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007432 }
7433
7434 /**
7435 * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or
7436 * {@link View#LAYOUT_DIRECTION_RTL}.
7437 *
7438 * @return the layout direction.
7439 */
7440 public int getLayoutDirection() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007441 return (mMarginFlags & LAYOUT_DIRECTION_MASK);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007442 }
7443
7444 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007445 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08007446 * may be overridden depending on layout direction.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007447 */
7448 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007449 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007450 setLayoutDirection(layoutDirection);
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007451
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007452 // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything
7453 // Will use the left and right margins if no relative margin is defined.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007454 if (!isMarginRelative() ||
7455 (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07007456
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007457 // Proceed with resolution
7458 doResolveMargins();
7459 }
7460
7461 private void doResolveMargins() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007462 if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007463 // if left or right margins are not defined and if we have some start or end margin
7464 // defined then use those start and end margins.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007465 if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK
7466 && startMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007467 leftMargin = startMargin;
7468 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007469 if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK
7470 && endMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007471 rightMargin = endMargin;
7472 }
7473 } else {
7474 // We have some relative margins (either the start one or the end one or both). So use
7475 // them and override what has been defined for left and right margins. If either start
7476 // or end margin is not defined, just set it to default "0".
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007477 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07007478 case View.LAYOUT_DIRECTION_RTL:
7479 leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7480 endMargin : DEFAULT_MARGIN_RESOLVED;
7481 rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7482 startMargin : DEFAULT_MARGIN_RESOLVED;
7483 break;
7484 case View.LAYOUT_DIRECTION_LTR:
7485 default:
7486 leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
7487 startMargin : DEFAULT_MARGIN_RESOLVED;
7488 rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
7489 endMargin : DEFAULT_MARGIN_RESOLVED;
7490 break;
7491 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007492 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007493 mMarginFlags &= ~NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007494 }
Philip Milne10ca24a2012-04-23 15:38:27 -07007495
Fabrice Di Meglio03b8d3a2012-09-27 17:05:27 -07007496 /**
7497 * @hide
7498 */
7499 public boolean isLayoutRtl() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007500 return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007501 }
7502
Philip Milne10ca24a2012-04-23 15:38:27 -07007503 /**
7504 * @hide
7505 */
7506 @Override
Philip Milne7b757812012-09-19 18:13:44 -07007507 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
7508 Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE;
7509
7510 fillDifference(canvas,
7511 view.getLeft() + oi.left,
7512 view.getTop() + oi.top,
7513 view.getRight() - oi.right,
7514 view.getBottom() - oi.bottom,
7515 leftMargin,
7516 topMargin,
7517 rightMargin,
7518 bottomMargin,
7519 paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07007520 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007521
7522 /** @hide */
7523 @Override
7524 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7525 super.encodeProperties(encoder);
7526 encoder.addProperty("leftMargin", leftMargin);
7527 encoder.addProperty("topMargin", topMargin);
7528 encoder.addProperty("rightMargin", rightMargin);
7529 encoder.addProperty("bottomMargin", bottomMargin);
7530 encoder.addProperty("startMargin", startMargin);
7531 encoder.addProperty("endMargin", endMargin);
7532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 }
Adam Powell2b342f02010-08-18 18:14:13 -07007534
Jeff Brown20e987b2010-08-23 12:01:02 -07007535 /* Describes a touched view and the ids of the pointers that it has captured.
7536 *
7537 * This code assumes that pointer ids are always in the range 0..31 such that
7538 * it can use a bitfield to track which pointer ids are present.
7539 * As it happens, the lower layers of the input dispatch pipeline also use the
7540 * same trick so the assumption should be safe here...
7541 */
7542 private static final class TouchTarget {
7543 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007544 private static final Object sRecycleLock = new Object[0];
Jeff Brown20e987b2010-08-23 12:01:02 -07007545 private static TouchTarget sRecycleBin;
7546 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07007547
Jeff Brown20e987b2010-08-23 12:01:02 -07007548 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07007549
Jeff Brown20e987b2010-08-23 12:01:02 -07007550 // The touched child view.
7551 public View child;
7552
7553 // The combined bit mask of pointer ids for all pointers captured by the target.
7554 public int pointerIdBits;
7555
7556 // The next target in the target list.
7557 public TouchTarget next;
7558
7559 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07007560 }
7561
Alan Viverettea7b85e62016-01-22 10:14:02 -05007562 public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {
7563 if (child == null) {
7564 throw new IllegalArgumentException("child must be non-null");
7565 }
7566
Jeff Brown20e987b2010-08-23 12:01:02 -07007567 final TouchTarget target;
7568 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07007569 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007570 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07007571 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07007572 target = sRecycleBin;
7573 sRecycleBin = target.next;
7574 sRecycledCount--;
7575 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007576 }
Adam Powell816c3be2010-08-23 18:00:05 -07007577 }
Jeff Brown20e987b2010-08-23 12:01:02 -07007578 target.child = child;
7579 target.pointerIdBits = pointerIdBits;
7580 return target;
7581 }
Adam Powell816c3be2010-08-23 18:00:05 -07007582
Jeff Brown20e987b2010-08-23 12:01:02 -07007583 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05007584 if (child == null) {
7585 throw new IllegalStateException("already recycled once");
7586 }
7587
Jeff Brown20e987b2010-08-23 12:01:02 -07007588 synchronized (sRecycleLock) {
7589 if (sRecycledCount < MAX_RECYCLED) {
7590 next = sRecycleBin;
7591 sRecycleBin = this;
7592 sRecycledCount += 1;
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007593 } else {
7594 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007595 }
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07007596 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07007597 }
7598 }
Adam Powell2b342f02010-08-18 18:14:13 -07007599 }
Jeff Brown87b7f802011-06-21 18:35:45 -07007600
7601 /* Describes a hovered view. */
7602 private static final class HoverTarget {
7603 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07007604 private static final Object sRecycleLock = new Object[0];
Jeff Brown87b7f802011-06-21 18:35:45 -07007605 private static HoverTarget sRecycleBin;
7606 private static int sRecycledCount;
7607
7608 // The hovered child view.
7609 public View child;
7610
7611 // The next target in the target list.
7612 public HoverTarget next;
7613
7614 private HoverTarget() {
7615 }
7616
Alan Viverettea7b85e62016-01-22 10:14:02 -05007617 public static HoverTarget obtain(@NonNull View child) {
7618 if (child == null) {
7619 throw new IllegalArgumentException("child must be non-null");
7620 }
7621
Jeff Brown87b7f802011-06-21 18:35:45 -07007622 final HoverTarget target;
7623 synchronized (sRecycleLock) {
7624 if (sRecycleBin == null) {
7625 target = new HoverTarget();
7626 } else {
7627 target = sRecycleBin;
7628 sRecycleBin = target.next;
Alan Viverettea7b85e62016-01-22 10:14:02 -05007629 sRecycledCount--;
Jeff Brown87b7f802011-06-21 18:35:45 -07007630 target.next = null;
7631 }
7632 }
7633 target.child = child;
7634 return target;
7635 }
7636
7637 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05007638 if (child == null) {
7639 throw new IllegalStateException("already recycled once");
7640 }
7641
Jeff Brown87b7f802011-06-21 18:35:45 -07007642 synchronized (sRecycleLock) {
7643 if (sRecycledCount < MAX_RECYCLED) {
7644 next = sRecycleBin;
7645 sRecycleBin = this;
7646 sRecycledCount += 1;
7647 } else {
7648 next = null;
7649 }
7650 child = null;
7651 }
7652 }
7653 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007654
7655 /**
7656 * Pooled class that orderes the children of a ViewGroup from start
7657 * to end based on how they are laid out and the layout direction.
7658 */
7659 static class ChildListForAccessibility {
7660
7661 private static final int MAX_POOL_SIZE = 32;
7662
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007663 private static final SynchronizedPool<ChildListForAccessibility> sPool =
7664 new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007665
7666 private final ArrayList<View> mChildren = new ArrayList<View>();
7667
7668 private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
7669
7670 public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007671 ChildListForAccessibility list = sPool.acquire();
7672 if (list == null) {
7673 list = new ChildListForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007674 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007675 list.init(parent, sort);
7676 return list;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007677 }
7678
7679 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007680 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007681 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007682 }
7683
7684 public int getChildCount() {
7685 return mChildren.size();
7686 }
7687
7688 public View getChildAt(int index) {
7689 return mChildren.get(index);
7690 }
7691
7692 public int getChildIndex(View child) {
7693 return mChildren.indexOf(child);
7694 }
7695
7696 private void init(ViewGroup parent, boolean sort) {
7697 ArrayList<View> children = mChildren;
7698 final int childCount = parent.getChildCount();
7699 for (int i = 0; i < childCount; i++) {
7700 View child = parent.getChildAt(i);
7701 children.add(child);
7702 }
7703 if (sort) {
7704 ArrayList<ViewLocationHolder> holders = mHolders;
7705 for (int i = 0; i < childCount; i++) {
7706 View child = children.get(i);
7707 ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
7708 holders.add(holder);
7709 }
Svetoslav88e447b2014-10-09 15:49:02 -07007710 sort(holders);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007711 for (int i = 0; i < childCount; i++) {
7712 ViewLocationHolder holder = holders.get(i);
7713 children.set(i, holder.mView);
7714 holder.recycle();
7715 }
7716 holders.clear();
7717 }
7718 }
7719
Svetoslav88e447b2014-10-09 15:49:02 -07007720 private void sort(ArrayList<ViewLocationHolder> holders) {
7721 // This is gross but the least risky solution. The current comparison
7722 // strategy breaks transitivity but produces very good results. Coming
7723 // up with a new strategy requires time which we do not have, so ...
7724 try {
7725 ViewLocationHolder.setComparisonStrategy(
7726 ViewLocationHolder.COMPARISON_STRATEGY_STRIPE);
7727 Collections.sort(holders);
7728 } catch (IllegalArgumentException iae) {
7729 // Note that in practice this occurs extremely rarely in a couple
7730 // of pathological cases.
7731 ViewLocationHolder.setComparisonStrategy(
7732 ViewLocationHolder.COMPARISON_STRATEGY_LOCATION);
7733 Collections.sort(holders);
7734 }
7735 }
7736
Svetoslav Ganov42138042012-03-20 11:51:39 -07007737 private void clear() {
7738 mChildren.clear();
7739 }
7740 }
7741
7742 /**
7743 * Pooled class that holds a View and its location with respect to
7744 * a specified root. This enables sorting of views based on their
7745 * coordinates without recomputing the position relative to the root
7746 * on every comparison.
7747 */
7748 static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
7749
7750 private static final int MAX_POOL_SIZE = 32;
7751
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007752 private static final SynchronizedPool<ViewLocationHolder> sPool =
7753 new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007754
Svetoslav88e447b2014-10-09 15:49:02 -07007755 public static final int COMPARISON_STRATEGY_STRIPE = 1;
7756
7757 public static final int COMPARISON_STRATEGY_LOCATION = 2;
7758
7759 private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE;
7760
Svetoslav Ganov42138042012-03-20 11:51:39 -07007761 private final Rect mLocation = new Rect();
7762
7763 public View mView;
7764
7765 private int mLayoutDirection;
7766
7767 public static ViewLocationHolder obtain(ViewGroup root, View view) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007768 ViewLocationHolder holder = sPool.acquire();
7769 if (holder == null) {
7770 holder = new ViewLocationHolder();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007771 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007772 holder.init(root, view);
7773 return holder;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007774 }
7775
Svetoslav88e447b2014-10-09 15:49:02 -07007776 public static void setComparisonStrategy(int strategy) {
7777 sComparisonStrategy = strategy;
7778 }
7779
Svetoslav Ganov42138042012-03-20 11:51:39 -07007780 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007781 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08007782 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007783 }
7784
7785 @Override
7786 public int compareTo(ViewLocationHolder another) {
7787 // This instance is greater than an invalid argument.
7788 if (another == null) {
7789 return 1;
7790 }
Svetoslav88e447b2014-10-09 15:49:02 -07007791
7792 if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) {
7793 // First is above second.
7794 if (mLocation.bottom - another.mLocation.top <= 0) {
7795 return -1;
7796 }
7797 // First is below second.
7798 if (mLocation.top - another.mLocation.bottom >= 0) {
7799 return 1;
7800 }
7801 }
7802
Svetoslav04cab1b2014-08-25 18:35:57 -07007803 // We are ordering left-to-right, top-to-bottom.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007804 if (mLayoutDirection == LAYOUT_DIRECTION_LTR) {
7805 final int leftDifference = mLocation.left - another.mLocation.left;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007806 if (leftDifference != 0) {
7807 return leftDifference;
7808 }
7809 } else { // RTL
7810 final int rightDifference = mLocation.right - another.mLocation.right;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007811 if (rightDifference != 0) {
7812 return -rightDifference;
7813 }
7814 }
Svetoslav04cab1b2014-08-25 18:35:57 -07007815 // We are ordering left-to-right, top-to-bottom.
7816 final int topDifference = mLocation.top - another.mLocation.top;
7817 if (topDifference != 0) {
7818 return topDifference;
7819 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007820 // Break tie by height.
7821 final int heightDiference = mLocation.height() - another.mLocation.height();
7822 if (heightDiference != 0) {
7823 return -heightDiference;
7824 }
7825 // Break tie by width.
7826 final int widthDiference = mLocation.width() - another.mLocation.width();
7827 if (widthDiference != 0) {
7828 return -widthDiference;
7829 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007830 // Just break the tie somehow. The accessibliity ids are unique
7831 // and stable, hence this is deterministic tie breaking.
7832 return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007833 }
7834
7835 private void init(ViewGroup root, View view) {
7836 Rect viewLocation = mLocation;
7837 view.getDrawingRect(viewLocation);
7838 root.offsetDescendantRectToMyCoords(view, viewLocation);
7839 mView = view;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07007840 mLayoutDirection = root.getLayoutDirection();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007841 }
7842
7843 private void clear() {
7844 mView = null;
7845 mLocation.set(0, 0, 0, 0);
7846 }
7847 }
Romain Guycbc67742012-04-27 16:12:57 -07007848
7849 private static Paint getDebugPaint() {
7850 if (sDebugPaint == null) {
7851 sDebugPaint = new Paint();
7852 sDebugPaint.setAntiAlias(false);
7853 }
7854 return sDebugPaint;
7855 }
7856
Romain Guy6410c0a2013-06-17 11:21:58 -07007857 private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
Romain Guycbc67742012-04-27 16:12:57 -07007858 if (sDebugLines== null) {
Romain Guy6410c0a2013-06-17 11:21:58 -07007859 // TODO: This won't work with multiple UI threads in a single process
Romain Guycbc67742012-04-27 16:12:57 -07007860 sDebugLines = new float[16];
7861 }
7862
Romain Guycbc67742012-04-27 16:12:57 -07007863 sDebugLines[0] = x1;
7864 sDebugLines[1] = y1;
7865 sDebugLines[2] = x2;
7866 sDebugLines[3] = y1;
7867
7868 sDebugLines[4] = x2;
7869 sDebugLines[5] = y1;
7870 sDebugLines[6] = x2;
Philip Milne7b757812012-09-19 18:13:44 -07007871 sDebugLines[7] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007872
Philip Milne7b757812012-09-19 18:13:44 -07007873 sDebugLines[8] = x2;
Romain Guycbc67742012-04-27 16:12:57 -07007874 sDebugLines[9] = y2;
7875 sDebugLines[10] = x1;
7876 sDebugLines[11] = y2;
7877
Philip Milne7b757812012-09-19 18:13:44 -07007878 sDebugLines[12] = x1;
7879 sDebugLines[13] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07007880 sDebugLines[14] = x1;
7881 sDebugLines[15] = y1;
7882
Philip Milne7b757812012-09-19 18:13:44 -07007883 canvas.drawLines(sDebugLines, paint);
Romain Guycbc67742012-04-27 16:12:57 -07007884 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007885
7886 /** @hide */
7887 @Override
7888 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7889 super.encodeProperties(encoder);
7890
7891 encoder.addProperty("focus:descendantFocusability", getDescendantFocusability());
7892 encoder.addProperty("drawing:clipChildren", getClipChildren());
7893 encoder.addProperty("drawing:clipToPadding", getClipToPadding());
7894 encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled());
7895 encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache());
7896
7897 int n = getChildCount();
7898 encoder.addProperty("meta:__childCount__", (short)n);
7899 for (int i = 0; i < n; i++) {
7900 encoder.addPropertyKey("meta:__child__" + i);
7901 getChildAt(i).encode(encoder);
7902 }
7903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904}