blob: 0986cfa454b6604a426814815125fb71ba476dad [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
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070019import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
20
Chet Haase21cd1382010-09-01 17:42:29 -070021import android.animation.LayoutTransition;
Chris Craik9de95db2017-01-18 17:59:23 -080022import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070023import android.annotation.IdRes;
Alan Viverette922e1c62015-05-05 17:18:27 -070024import android.annotation.NonNull;
Evan Rosky3ac64632017-02-13 18:04:43 -080025import android.annotation.TestApi;
Tor Norbye83c68962015-03-10 20:55:31 -070026import android.annotation.UiThread;
Mathew Inwooda570dee2018-08-17 14:56:00 +010027import android.annotation.UnsupportedAppUsage;
Vadim Trysheva61efa42016-09-28 15:15:52 -070028import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +000030import android.content.Intent;
Adam Powellff0d2982014-07-10 20:34:14 -070031import android.content.pm.PackageManager;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080032import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.res.TypedArray;
34import android.graphics.Bitmap;
35import android.graphics.Canvas;
Philip Milne10ca24a2012-04-23 15:38:27 -070036import android.graphics.Color;
37import android.graphics.Insets;
Adam Powell6e346362010-07-23 10:18:23 -070038import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.Paint;
Christopher Tatea53146c2010-09-07 11:57:52 -070040import android.graphics.PointF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.graphics.RectF;
svetoslavganov75986cf2009-05-14 22:28:01 -070043import android.graphics.Region;
Jeff Brown995e7742010-12-22 16:59:36 -080044import android.os.Build;
Adam Powellb6ab0982015-01-07 17:00:12 -080045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcelable;
47import android.os.SystemClock;
48import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.Log;
Svet Ganov2f8fb1f2017-03-13 00:21:04 -070050import android.util.Pools;
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -080051import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.SparseArray;
Dianne Hackborn6ff55fc2015-08-05 18:07:31 -070053import android.util.SparseBooleanArray;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080054import android.view.WindowInsetsAnimationListener.InsetsAnimation;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.view.accessibility.AccessibilityEvent;
Phil Weaver4d3eec412016-09-01 16:28:34 -070056import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070057import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.view.animation.Animation;
59import android.view.animation.AnimationUtils;
60import android.view.animation.LayoutAnimationController;
61import android.view.animation.Transformation;
Felipe Lemefe05a522018-01-23 15:57:49 -080062import android.view.autofill.Helper;
Chet Haasecb3d0232017-05-24 18:27:14 -070063
Romain Guy0211a0a2011-02-14 16:34:59 -080064import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66import java.util.ArrayList;
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -080067import java.util.Collection;
Svetoslav Ganov42138042012-03-20 11:51:39 -070068import java.util.Collections;
Christopher Tate86cab1b2011-01-13 20:28:55 -080069import java.util.HashSet;
George Mounte1803372014-02-26 19:00:52 +000070import java.util.List;
71import java.util.Map;
Paul Duffinca4964c2017-02-07 15:04:10 +000072import java.util.function.Predicate;
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +090073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * <p>
76 * A <code>ViewGroup</code> is a special view that can contain other views
77 * (called children.) The view group is the base class for layouts and views
78 * containers. This class also defines the
79 * {@link android.view.ViewGroup.LayoutParams} class which serves as the base
80 * class for layouts parameters.
81 * </p>
82 *
83 * <p>
84 * Also see {@link LayoutParams} for layout attributes.
85 * </p>
Romain Guyd6a463a2009-05-21 23:10:10 -070086 *
Joe Fernandez558459f2011-10-13 16:47:36 -070087 * <div class="special reference">
88 * <h3>Developer Guides</h3>
89 * <p>For more information about creating user interface layouts, read the
90 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
91 * guide.</p></div>
92 *
Dianne Hackborn7caab0f2013-03-06 13:47:04 -080093 * <p>Here is a complete implementation of a custom ViewGroup that implements
94 * a simple {@link android.widget.FrameLayout} along with the ability to stack
95 * children in left and right gutters.</p>
96 *
97 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java
98 * Complete}
99 *
100 * <p>If you are implementing XML layout attributes as shown in the example, this is the
101 * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p>
102 *
103 * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout}
104 *
105 * <p>Finally the layout manager can be used in an XML layout like so:</p>
106 *
107 * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete}
108 *
Romain Guyd6a463a2009-05-21 23:10:10 -0700109 * @attr ref android.R.styleable#ViewGroup_clipChildren
110 * @attr ref android.R.styleable#ViewGroup_clipToPadding
111 * @attr ref android.R.styleable#ViewGroup_layoutAnimation
112 * @attr ref android.R.styleable#ViewGroup_animationCache
113 * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache
114 * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache
115 * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren
116 * @attr ref android.R.styleable#ViewGroup_descendantFocusability
Chet Haase13cc1202010-09-03 15:39:20 -0700117 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Scott Main27a85082013-06-10 10:39:48 -0700118 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
119 * @attr ref android.R.styleable#ViewGroup_layoutMode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
Tor Norbye83c68962015-03-10 20:55:31 -0700121@UiThread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122public abstract class ViewGroup extends View implements ViewParent, ViewManager {
Adam Powell539ee872012-02-03 19:00:49 -0800123 private static final String TAG = "ViewGroup";
Chet Haase21cd1382010-09-01 17:42:29 -0700124
Mathew Inwooda570dee2018-08-17 14:56:00 +0100125 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 private static final boolean DBG = false;
Gilles Debunnecea45132011-11-24 02:19:27 +0100127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
129 * Views which have been hidden or removed which need to be animated on
130 * their way out.
131 * This field should be made private, so it is hidden from the SDK.
132 * {@hide}
133 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100134 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 protected ArrayList<View> mDisappearingChildren;
136
137 /**
138 * Listener used to propagate events indicating when children are added
139 * and/or removed from a view group.
140 * This field should be made private, so it is hidden from the SDK.
141 * {@hide}
142 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100143 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 protected OnHierarchyChangeListener mOnHierarchyChangeListener;
145
146 // The view contained within this ViewGroup that has or contains focus.
Mathew Inwood31755f92018-12-20 13:53:36 +0000147 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 private View mFocused;
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800149 // The view contained within this ViewGroup (excluding nested keyboard navigation clusters)
150 // that is or contains a default-focus view.
151 private View mDefaultFocus;
Evan Rosky53fcf112017-01-26 14:37:55 -0800152 // The last child of this ViewGroup which held focus within the current cluster
Evan Rosky6c286be2017-04-19 17:23:32 -0700153 View mFocusedInCluster;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
Chet Haase48460322010-06-11 14:22:25 -0700155 /**
156 * A Transformation used when drawing children, to
157 * apply on the child being drawn.
158 */
Romain Guyf6991302013-06-05 17:19:01 -0700159 private Transformation mChildTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700160
161 /**
162 * Used to track the current invalidation region.
163 */
Chet Haase64a48c12012-02-13 16:33:29 -0800164 RectF mInvalidateRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Chet Haase48460322010-06-11 14:22:25 -0700166 /**
167 * A Transformation used to calculate a correct
168 * invalidation area when the application is autoscaled.
169 */
Chet Haase64a48c12012-02-13 16:33:29 -0800170 Transformation mInvalidationTransformation;
Chet Haase48460322010-06-11 14:22:25 -0700171
Vadim Tryshevef128112016-09-16 14:05:53 -0700172 // Current frontmost child that can accept drag and lies under the drag location.
173 // Used only to generate ENTER/EXIT events for pre-Nougat aps.
174 private View mCurrentDragChild;
175
Christopher Tate86cab1b2011-01-13 20:28:55 -0800176 // Metadata about the ongoing drag
Vadim Tryshev1a68dc92015-07-20 17:01:50 -0700177 private DragEvent mCurrentDragStartEvent;
178 private boolean mIsInterestedInDrag;
179 private HashSet<View> mChildrenInterestedInDrag;
Christopher Tatea53146c2010-09-07 11:57:52 -0700180
181 // Used during drag dispatch
Romain Guy6410c0a2013-06-17 11:21:58 -0700182 private PointF mLocalPoint;
Christopher Tatea53146c2010-09-07 11:57:52 -0700183
Alan Viveretteb942b6f2014-12-08 10:37:39 -0800184 // Lazily-created holder for point computations.
185 private float[] mTempPoint;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 // Layout animation
188 private LayoutAnimationController mLayoutAnimationController;
189 private Animation.AnimationListener mAnimationListener;
190
Jeff Brown20e987b2010-08-23 12:01:02 -0700191 // First touch target in the linked list of touch targets.
Mathew Inwooda570dee2018-08-17 14:56:00 +0100192 @UnsupportedAppUsage
Jeff Brown20e987b2010-08-23 12:01:02 -0700193 private TouchTarget mFirstTouchTarget;
194
Joe Onorato03ab0c72011-01-06 15:46:27 -0800195 // For debugging only. You can see these in hierarchyviewer.
Romain Guye95003e2011-01-09 13:53:06 -0800196 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800197 @ViewDebug.ExportedProperty(category = "events")
198 private long mLastTouchDownTime;
199 @ViewDebug.ExportedProperty(category = "events")
200 private int mLastTouchDownIndex = -1;
Romain Guye95003e2011-01-09 13:53:06 -0800201 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800202 @ViewDebug.ExportedProperty(category = "events")
203 private float mLastTouchDownX;
Romain Guye95003e2011-01-09 13:53:06 -0800204 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Joe Onorato03ab0c72011-01-06 15:46:27 -0800205 @ViewDebug.ExportedProperty(category = "events")
206 private float mLastTouchDownY;
207
Jeff Brown87b7f802011-06-21 18:35:45 -0700208 // First hover target in the linked list of hover targets.
209 // The hover targets are children which have received ACTION_HOVER_ENTER.
210 // They might not have actually handled the hover event, but we will
211 // continue sending hover events to them as long as the pointer remains over
212 // their bounds and the view group does not intercept hover.
213 private HoverTarget mFirstHoverTarget;
Jeff Browna032cc02011-03-07 16:56:21 -0800214
Jeff Brown10b62902011-06-20 16:40:37 -0700215 // True if the view group itself received a hover event.
216 // It might not have actually handled the hover event.
217 private boolean mHoveredSelf;
218
Vladislav Kaznacheevf847ee32016-11-21 14:11:00 -0800219 // The child capable of showing a tooltip and currently under the pointer.
220 private View mTooltipHoverTarget;
221
222 // True if the view group is capable of showing a tooltip and the pointer is directly
223 // over the view group but not one of its child views.
224 private boolean mTooltipHoveredSelf;
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * Internal flags.
Romain Guy8506ab42009-06-11 17:35:47 -0700228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * This field should be made private, so it is hidden from the SDK.
230 * {@hide}
231 */
Romain Guy2440e672012-08-07 14:43:43 -0700232 @ViewDebug.ExportedProperty(flagMapping = {
233 @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN,
234 name = "CLIP_CHILDREN"),
235 @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING,
236 name = "CLIP_TO_PADDING"),
237 @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL,
238 name = "PADDING_NOT_NULL")
Jon Miranda4597e982014-07-29 07:25:49 -0700239 }, formatToHexString = true)
Mathew Inwooda570dee2018-08-17 14:56:00 +0100240 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 protected int mGroupFlags;
242
Philip Milne7b757812012-09-19 18:13:44 -0700243 /**
244 * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -0700245 */
Philip Milnecfb631b2012-10-26 10:51:46 -0700246 private int mLayoutMode = LAYOUT_MODE_UNDEFINED;
Philip Milne1557fd72012-04-04 23:41:34 -0700247
Romain Guy33f6beb2012-02-16 19:24:51 -0800248 /**
249 * NOTE: If you change the flags below make sure to reflect the changes
250 * the DisplayList class
251 */
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 // When set, ViewGroup invalidates only the child's rectangle
254 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800255 static final int FLAG_CLIP_CHILDREN = 0x1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 // When set, ViewGroup excludes the padding area from the invalidate rectangle
258 // Set by default
259 private static final int FLAG_CLIP_TO_PADDING = 0x2;
260
261 // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when
262 // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set
Chet Haase64a48c12012-02-13 16:33:29 -0800263 static final int FLAG_INVALIDATE_REQUIRED = 0x4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264
265 // When set, dispatchDraw() will run the layout animation and unset the flag
266 private static final int FLAG_RUN_ANIMATION = 0x8;
267
268 // When set, there is either no layout animation on the ViewGroup or the layout
269 // animation is over
270 // Set by default
Chet Haase64a48c12012-02-13 16:33:29 -0800271 static final int FLAG_ANIMATION_DONE = 0x10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
273 // If set, this ViewGroup has padding; if unset there is no padding and we don't need
274 // to clip it, even if FLAG_CLIP_TO_PADDING is set
275 private static final int FLAG_PADDING_NOT_NULL = 0x20;
276
Chris Craik5a6bbae2015-04-10 17:41:34 -0700277 /** @deprecated - functionality removed */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700278 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 private static final int FLAG_ANIMATION_CACHE = 0x40;
280
281 // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a
282 // layout animation; this avoid clobbering the hierarchy
283 // Automatically set when the layout animation starts, depending on the animation's
284 // characteristics
Chet Haase64a48c12012-02-13 16:33:29 -0800285 static final int FLAG_OPTIMIZE_INVALIDATE = 0x80;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
287 // When set, the next call to drawChild() will clear mChildTransformation's matrix
Chet Haase64a48c12012-02-13 16:33:29 -0800288 static final int FLAG_CLEAR_TRANSFORMATION = 0x100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes
291 // the children's Bitmap caches if necessary
292 // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set)
293 private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200;
294
295 /**
296 * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)}
297 * to get the index of the child to draw for that iteration.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -0800298 *
Romain Guy293451e2009-11-04 13:59:48 -0800299 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100301 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400;
Romain Guy8506ab42009-06-11 17:35:47 -0700303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * When set, this ViewGroup supports static transformations on children; this causes
306 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
307 * invoked when a child is drawn.
308 *
309 * Any subclass overriding
310 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
311 * set this flags in {@link #mGroupFlags}.
Romain Guy8506ab42009-06-11 17:35:47 -0700312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * {@hide}
314 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100315 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800;
317
John Reckfb5899d2014-08-15 18:51:27 -0700318 // UNUSED FLAG VALUE: 0x1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
320 /**
321 * When set, this ViewGroup's drawable states also include those
322 * of its children.
323 */
324 private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000;
325
Chris Craik5a6bbae2015-04-10 17:41:34 -0700326 /** @deprecated functionality removed */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700327 @Deprecated
Chris Craik5a6bbae2015-04-10 17:41:34 -0700328 private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
Chris Craik5a6bbae2015-04-10 17:41:34 -0700330 /** @deprecated functionality removed */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700331 @Deprecated
Chris Craik5a6bbae2015-04-10 17:41:34 -0700332 private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
334 /**
335 * When set, this group will go through its list of children to notify them of
336 * any drawable state change.
337 */
338 private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000;
339
340 private static final int FLAG_MASK_FOCUSABILITY = 0x60000;
341
342 /**
343 * This view will get focus before any of its descendants.
344 */
345 public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000;
346
347 /**
348 * This view will get focus only if none of its descendants want it.
349 */
350 public static final int FOCUS_AFTER_DESCENDANTS = 0x40000;
351
352 /**
353 * This view will block any of its descendants from getting focus, even
354 * if they are focusable.
355 */
356 public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000;
357
358 /**
359 * Used to map between enum in attrubutes and flag values.
360 */
361 private static final int[] DESCENDANT_FOCUSABILITY_FLAGS =
362 {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS,
363 FOCUS_BLOCK_DESCENDANTS};
364
365 /**
366 * When set, this ViewGroup should not intercept touch events.
Adam Powell110486f2010-06-22 17:14:44 -0700367 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100369 @UnsupportedAppUsage
Adam Powell110486f2010-06-22 17:14:44 -0700370 protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000;
Romain Guy8506ab42009-06-11 17:35:47 -0700371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
Adam Powell2b342f02010-08-18 18:14:13 -0700373 * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate.
374 */
Adam Powellf37df072010-09-17 16:22:49 -0700375 private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000;
Adam Powell2b342f02010-08-18 18:14:13 -0700376
377 /**
Adam Powell4b867882011-09-16 12:59:46 -0700378 * When set, this ViewGroup will not dispatch onAttachedToWindow calls
379 * to children when adding new views. This is used to prevent multiple
380 * onAttached calls when a ViewGroup adds children in its own onAttached method.
381 */
382 private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000;
383
384 /**
Philip Milnecfb631b2012-10-26 10:51:46 -0700385 * When true, indicates that a layoutMode has been explicitly set, either with
386 * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource.
387 * This distinguishes the situation in which a layout mode was inherited from
388 * one of the ViewGroup's ancestors and cached locally.
389 */
390 private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000;
391
Chris Craikb49f4462014-03-20 12:44:20 -0700392 static final int FLAG_IS_TRANSITION_GROUP = 0x1000000;
Chris Craikd863a102013-12-19 13:31:15 -0800393
Chris Craikb49f4462014-03-20 12:44:20 -0700394 static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000;
George Mount0a778ed2013-12-13 13:35:36 -0800395
Chris Craikd863a102013-12-19 13:31:15 -0800396 /**
Adam Powellff0d2982014-07-10 20:34:14 -0700397 * When set, focus will not be permitted to enter this group if a touchscreen is present.
398 */
399 static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000;
400
401 /**
Clara Bayarri4423d912015-03-02 19:42:48 +0000402 * When true, indicates that a call to startActionModeForChild was made with the type parameter
403 * and should not be ignored. This helps in backwards compatibility with the existing method
404 * without a type.
405 *
406 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
407 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
408 */
409 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000;
410
411 /**
412 * When true, indicates that a call to startActionModeForChild was made without the type
413 * parameter. This helps in backwards compatibility with the existing method
414 * without a type.
415 *
416 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
417 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
418 */
419 private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000;
420
421 /**
Adam Powell2af189a2016-02-05 15:52:02 -0800422 * When set, indicates that a call to showContextMenuForChild was made with explicit
423 * coordinates within the initiating child view.
424 */
425 private static final int FLAG_SHOW_CONTEXT_MENU_WITH_COORDS = 0x20000000;
426
427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * Indicates which types of drawing caches are to be kept in memory.
429 * This field should be made private, so it is hidden from the SDK.
430 * {@hide}
431 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100432 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 protected int mPersistentDrawingCache;
434
435 /**
436 * Used to indicate that no drawing cache should be kept in memory.
John Reck949cfe12017-10-09 13:27:03 -0700437 *
438 * @deprecated The view drawing cache was largely made obsolete with the introduction of
439 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
440 * layers are largely unnecessary and can easily result in a net loss in performance due to the
441 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
442 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
443 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
444 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
445 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
446 * software-rendered usages are discouraged and have compatibility issues with hardware-only
447 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
448 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
449 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 */
John Reck949cfe12017-10-09 13:27:03 -0700451 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 public static final int PERSISTENT_NO_CACHE = 0x0;
453
454 /**
455 * Used to indicate that the animation drawing cache should be kept in memory.
John Reck949cfe12017-10-09 13:27:03 -0700456 *
457 * @deprecated The view drawing cache was largely made obsolete with the introduction of
458 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
459 * layers are largely unnecessary and can easily result in a net loss in performance due to the
460 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
461 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
462 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
463 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
464 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
465 * software-rendered usages are discouraged and have compatibility issues with hardware-only
466 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
467 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
468 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
John Reck949cfe12017-10-09 13:27:03 -0700470 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 public static final int PERSISTENT_ANIMATION_CACHE = 0x1;
472
473 /**
474 * Used to indicate that the scrolling drawing cache should be kept in memory.
John Reck949cfe12017-10-09 13:27:03 -0700475 *
476 * @deprecated The view drawing cache was largely made obsolete with the introduction of
477 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
478 * layers are largely unnecessary and can easily result in a net loss in performance due to the
479 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
480 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
481 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
482 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
483 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
484 * software-rendered usages are discouraged and have compatibility issues with hardware-only
485 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
486 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
487 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 */
John Reck949cfe12017-10-09 13:27:03 -0700489 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public static final int PERSISTENT_SCROLLING_CACHE = 0x2;
491
492 /**
493 * Used to indicate that all drawing caches should be kept in memory.
John Reck949cfe12017-10-09 13:27:03 -0700494 *
495 * @deprecated The view drawing cache was largely made obsolete with the introduction of
496 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
497 * layers are largely unnecessary and can easily result in a net loss in performance due to the
498 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
499 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
500 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
501 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
502 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
503 * software-rendered usages are discouraged and have compatibility issues with hardware-only
504 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
505 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
506 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 */
John Reck949cfe12017-10-09 13:27:03 -0700508 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public static final int PERSISTENT_ALL_CACHES = 0x3;
510
Philip Milne1557fd72012-04-04 23:41:34 -0700511 // Layout Modes
512
Philip Milnecfb631b2012-10-26 10:51:46 -0700513 private static final int LAYOUT_MODE_UNDEFINED = -1;
514
Philip Milne1557fd72012-04-04 23:41:34 -0700515 /**
516 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700517 * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top},
Philip Milne1557fd72012-04-04 23:41:34 -0700518 * {@link #getRight() right} and {@link #getBottom() bottom}.
519 */
Philip Milne7b757812012-09-19 18:13:44 -0700520 public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
Philip Milne1557fd72012-04-04 23:41:34 -0700521
522 /**
523 * This constant is a {@link #setLayoutMode(int) layoutMode}.
Philip Milne7a23b492012-04-24 22:12:36 -0700524 * Optical bounds describe where a widget appears to be. They sit inside the clip
525 * bounds which need to cover a larger area to allow other effects,
526 * such as shadows and glows, to be drawn.
Philip Milne1557fd72012-04-04 23:41:34 -0700527 */
Philip Milne7b757812012-09-19 18:13:44 -0700528 public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
529
530 /** @hide */
Philip Milnecfb631b2012-10-26 10:51:46 -0700531 public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS;
Philip Milne1557fd72012-04-04 23:41:34 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 /**
534 * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL
535 * are set at the same time.
536 */
537 protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL;
538
539 // Index of the child's left position in the mLocation array
540 private static final int CHILD_LEFT_INDEX = 0;
541 // Index of the child's top position in the mLocation array
542 private static final int CHILD_TOP_INDEX = 1;
543
544 // Child views of this ViewGroup
Mihai Popa831c1a92018-10-15 15:40:23 +0100545 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 private View[] mChildren;
547 // Number of valid children in the mChildren array, the rest should be null or not
548 // considered as children
Mihai Popa831c1a92018-10-15 15:40:23 +0100549 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 private int mChildrenCount;
551
Chet Haaseb9895022013-04-02 15:10:58 -0700552 // Whether layout calls are currently being suppressed, controlled by calls to
553 // suppressLayout()
554 boolean mSuppressLayout = false;
555
556 // Whether any layout calls have actually been suppressed while mSuppressLayout
557 // has been true. This tracks whether we need to issue a requestLayout() when
558 // layout is later re-enabled.
559 private boolean mLayoutCalledWhileSuppressed = false;
560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 private static final int ARRAY_INITIAL_CAPACITY = 12;
562 private static final int ARRAY_CAPACITY_INCREMENT = 12;
563
Romain Guycbc67742012-04-27 16:12:57 -0700564 private static float[] sDebugLines;
Philip Milne604f4402012-04-24 19:27:11 -0700565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 // Used to draw cached views
Chet Haase64a48c12012-02-13 16:33:29 -0800567 Paint mCachePaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
Chet Haase21cd1382010-09-01 17:42:29 -0700569 // Used to animate add/remove changes in layout
570 private LayoutTransition mTransition;
571
572 // The set of views that are currently being transitioned. This list is used to track views
573 // being removed that should not actually be removed from the parent yet because they are
574 // being animated.
575 private ArrayList<View> mTransitioningViews;
576
Chet Haase5e25c2c2010-09-16 11:15:56 -0700577 // List of children changing visibility. This is used to potentially keep rendering
578 // views during a transition when they otherwise would have become gone/invisible
579 private ArrayList<View> mVisibilityChangingChildren;
580
Chris Craikab008f02014-05-23 17:55:03 -0700581 // Temporary holder of presorted children, only used for
582 // input/software draw dispatch for correctly Z ordering.
583 private ArrayList<View> mPreSortedChildren;
584
Adam Powell539ee872012-02-03 19:00:49 -0800585 // Indicates how many of this container's child subtrees contain transient state
586 @ViewDebug.ExportedProperty(category = "layout")
587 private int mChildCountWithTransientState = 0;
588
Adam Powell10ba2772014-04-15 09:46:51 -0700589 /**
590 * Currently registered axes for nested scrolling. Flag set consisting of
591 * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE}
592 * for null.
593 */
594 private int mNestedScrollAxes;
595
Chet Haasec633d2f2015-04-07 10:29:39 -0700596 // Used to manage the list of transient views, added by addTransientView()
597 private List<Integer> mTransientIndices = null;
598 private List<View> mTransientViews = null;
599
Evan Rosky4807ae22018-03-22 16:04:15 -0700600 /**
601 * Keeps track of how many child views have UnhandledKeyEventListeners. This should only be
602 * updated on the UI thread so shouldn't require explicit synchronization.
603 */
604 int mChildUnhandledKeyListeners = 0;
Chet Haasec633d2f2015-04-07 10:29:39 -0700605
Clara Bayarri4423d912015-03-02 19:42:48 +0000606 /**
607 * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild.
608 *
609 * @see #startActionModeForChild(View, android.view.ActionMode.Callback)
610 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int)
611 */
612 private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() {
613 @Override
614 public void setTitle(CharSequence title) {}
615
616 @Override
617 public void setTitle(int resId) {}
618
619 @Override
620 public void setSubtitle(CharSequence subtitle) {}
621
622 @Override
623 public void setSubtitle(int resId) {}
624
625 @Override
626 public void setCustomView(View view) {}
627
628 @Override
629 public void invalidate() {}
630
631 @Override
632 public void finish() {}
633
634 @Override
635 public Menu getMenu() {
636 return null;
637 }
638
639 @Override
640 public CharSequence getTitle() {
641 return null;
642 }
643
644 @Override
645 public CharSequence getSubtitle() {
646 return null;
647 }
648
649 @Override
650 public View getCustomView() {
651 return null;
652 }
653
654 @Override
655 public MenuInflater getMenuInflater() {
656 return null;
657 }
658 };
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public ViewGroup(Context context) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700661 this(context, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
663
664 public ViewGroup(Context context, AttributeSet attrs) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700665 this(context, attrs, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
Alan Viverette617feb92013-09-09 18:09:13 -0700668 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
Alan Viveretted6479ec2013-09-10 17:03:02 -0700669 this(context, attrs, defStyleAttr, 0);
Alan Viverette617feb92013-09-09 18:09:13 -0700670 }
671
672 public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
673 super(context, attrs, defStyleAttr, defStyleRes);
Felipe Lemed04a6972017-03-02 12:56:18 -0800674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 initViewGroup();
Alan Viveretted6479ec2013-09-10 17:03:02 -0700676 initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678
679 private void initViewGroup() {
680 // ViewGroup doesn't draw by default
Philip Milne10ca24a2012-04-23 15:38:27 -0700681 if (!debugDraw()) {
682 setFlags(WILL_NOT_DRAW, DRAW_MASK);
683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 mGroupFlags |= FLAG_CLIP_CHILDREN;
685 mGroupFlags |= FLAG_CLIP_TO_PADDING;
686 mGroupFlags |= FLAG_ANIMATION_DONE;
Chris Craikf6ce8fd2015-05-11 15:33:11 -0700687 mGroupFlags |= FLAG_ANIMATION_CACHE;
688 mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689
Jeff Brown995e7742010-12-22 16:59:36 -0800690 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
691 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
695
696 mChildren = new View[ARRAY_INITIAL_CAPACITY];
697 mChildrenCount = 0;
698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE;
700 }
701
Alan Viveretted6479ec2013-09-10 17:03:02 -0700702 private void initFromAttributes(
703 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Filip Gruszczyńskib50cea02014-03-05 17:54:58 -0800704 final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr,
705 defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706
707 final int N = a.getIndexCount();
708 for (int i = 0; i < N; i++) {
709 int attr = a.getIndex(i);
710 switch (attr) {
711 case R.styleable.ViewGroup_clipChildren:
712 setClipChildren(a.getBoolean(attr, true));
713 break;
714 case R.styleable.ViewGroup_clipToPadding:
715 setClipToPadding(a.getBoolean(attr, true));
716 break;
717 case R.styleable.ViewGroup_animationCache:
718 setAnimationCacheEnabled(a.getBoolean(attr, true));
719 break;
720 case R.styleable.ViewGroup_persistentDrawingCache:
721 setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE));
722 break;
723 case R.styleable.ViewGroup_addStatesFromChildren:
724 setAddStatesFromChildren(a.getBoolean(attr, false));
725 break;
726 case R.styleable.ViewGroup_alwaysDrawnWithCache:
727 setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true));
728 break;
729 case R.styleable.ViewGroup_layoutAnimation:
730 int id = a.getResourceId(attr, -1);
731 if (id > 0) {
732 setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id));
733 }
734 break;
735 case R.styleable.ViewGroup_descendantFocusability:
736 setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]);
737 break;
Adam Powell2b342f02010-08-18 18:14:13 -0700738 case R.styleable.ViewGroup_splitMotionEvents:
739 setMotionEventSplittingEnabled(a.getBoolean(attr, false));
740 break;
Chet Haase13cc1202010-09-03 15:39:20 -0700741 case R.styleable.ViewGroup_animateLayoutChanges:
742 boolean animateLayoutChanges = a.getBoolean(attr, false);
743 if (animateLayoutChanges) {
744 setLayoutTransition(new LayoutTransition());
745 }
746 break;
Philip Milne7b757812012-09-19 18:13:44 -0700747 case R.styleable.ViewGroup_layoutMode:
Philip Milnecfb631b2012-10-26 10:51:46 -0700748 setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED));
Philip Milne7b757812012-09-19 18:13:44 -0700749 break;
George Mount0a778ed2013-12-13 13:35:36 -0800750 case R.styleable.ViewGroup_transitionGroup:
751 setTransitionGroup(a.getBoolean(attr, false));
752 break;
Adam Powellff0d2982014-07-10 20:34:14 -0700753 case R.styleable.ViewGroup_touchscreenBlocksFocus:
754 setTouchscreenBlocksFocus(a.getBoolean(attr, false));
755 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757 }
758
759 a.recycle();
760 }
761
762 /**
763 * Gets the descendant focusability of this view group. The descendant
764 * focusability defines the relationship between this view group and its
765 * descendants when looking for a view to take focus in
766 * {@link #requestFocus(int, android.graphics.Rect)}.
767 *
768 * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
769 * {@link #FOCUS_BLOCK_DESCENDANTS}.
770 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -0700771 @ViewDebug.ExportedProperty(category = "focus", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"),
773 @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"),
774 @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS")
775 })
776 public int getDescendantFocusability() {
777 return mGroupFlags & FLAG_MASK_FOCUSABILITY;
778 }
779
780 /**
781 * Set the descendant focusability of this view group. This defines the relationship
782 * between this view group and its descendants when looking for a view to
783 * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
784 *
785 * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
786 * {@link #FOCUS_BLOCK_DESCENDANTS}.
787 */
788 public void setDescendantFocusability(int focusability) {
789 switch (focusability) {
790 case FOCUS_BEFORE_DESCENDANTS:
791 case FOCUS_AFTER_DESCENDANTS:
792 case FOCUS_BLOCK_DESCENDANTS:
793 break;
794 default:
795 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
796 + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
797 }
798 mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
799 mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
800 }
801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 @Override
803 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
804 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800805 mFocused.unFocus(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 mFocused = null;
Evan Rosky53fcf112017-01-26 14:37:55 -0800807 mFocusedInCluster = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809 super.handleFocusGainInternal(direction, previouslyFocusedRect);
810 }
811
Alan Viverettebe463f22016-01-21 10:50:10 -0500812 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 public void requestChildFocus(View child, View focused) {
814 if (DBG) {
815 System.out.println(this + " requestChildFocus()");
816 }
817 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
818 return;
819 }
820
821 // Unfocus us, if necessary
Alan Viverette223622a2013-12-17 13:29:02 -0800822 super.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
824 // We had a previous notion of who had focus. Clear it.
825 if (mFocused != child) {
826 if (mFocused != null) {
Alan Viverette223622a2013-12-17 13:29:02 -0800827 mFocused.unFocus(focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829
830 mFocused = child;
831 }
832 if (mParent != null) {
833 mParent.requestChildFocus(this, focused);
834 }
835 }
836
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800837 void setDefaultFocus(View child) {
Evan Rosky53fcf112017-01-26 14:37:55 -0800838 // Stop at any higher view which is explicitly focused-by-default
839 if (mDefaultFocus != null && mDefaultFocus.isFocusedByDefault()) {
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800840 return;
Vadim Tryshev01d8c492016-12-15 11:33:15 -0800841 }
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800842
843 mDefaultFocus = child;
844
845 if (mParent instanceof ViewGroup) {
846 ((ViewGroup) mParent).setDefaultFocus(this);
847 }
848 }
849
850 /**
Evan Rosky53fcf112017-01-26 14:37:55 -0800851 * Clears the default-focus chain from {@param child} up to the first parent which has another
852 * default-focusable branch below it or until there is no default-focus chain.
853 *
854 * @param child
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800855 */
Evan Rosky53fcf112017-01-26 14:37:55 -0800856 void clearDefaultFocus(View child) {
857 // Stop at any higher view which is explicitly focused-by-default
858 if (mDefaultFocus != child && mDefaultFocus != null
859 && mDefaultFocus.isFocusedByDefault()) {
860 return;
861 }
862
863 mDefaultFocus = null;
864
865 // Search child siblings for default focusables.
866 for (int i = 0; i < mChildrenCount; ++i) {
867 View sibling = mChildren[i];
868 if (sibling.isFocusedByDefault()) {
869 mDefaultFocus = sibling;
870 return;
871 } else if (mDefaultFocus == null && sibling.hasDefaultFocus()) {
872 mDefaultFocus = sibling;
873 }
874 }
875
876 if (mParent instanceof ViewGroup) {
877 ((ViewGroup) mParent).clearDefaultFocus(this);
878 }
879 }
880
881 @Override
882 boolean hasDefaultFocus() {
883 return mDefaultFocus != null || super.hasDefaultFocus();
884 }
885
Evan Rosky0e8a6832017-04-10 12:35:15 -0700886 /**
887 * Removes {@code child} (and associated focusedInCluster chain) from the cluster containing
888 * it.
889 * <br>
890 * This is intended to be run on {@code child}'s immediate parent. This is necessary because
891 * the chain is sometimes cleared after {@code child} has been detached.
892 */
893 void clearFocusedInCluster(View child) {
Evan Rosky53fcf112017-01-26 14:37:55 -0800894 if (mFocusedInCluster != child) {
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800895 return;
896 }
Evan Rosky5b20e092017-07-13 18:04:39 -0700897 clearFocusedInCluster();
898 }
899
900 /**
901 * Removes the focusedInCluster chain from this up to the cluster containing it.
902 */
903 void clearFocusedInCluster() {
Evan Rosky0e8a6832017-04-10 12:35:15 -0700904 View top = findKeyboardNavigationCluster();
905 ViewParent parent = this;
906 do {
907 ((ViewGroup) parent).mFocusedInCluster = null;
Evan Rosky6c286be2017-04-19 17:23:32 -0700908 if (parent == top) {
909 break;
910 }
Evan Rosky0e8a6832017-04-10 12:35:15 -0700911 parent = parent.getParent();
Evan Rosky6c286be2017-04-19 17:23:32 -0700912 } while (parent instanceof ViewGroup);
Vadim Tryshev5ca73982017-01-04 17:24:43 -0800913 }
914
915 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public void focusableViewAvailable(View v) {
917 if (mParent != null
918 // shortcut: don't report a new focusable view if we block our descendants from
Evan Rosky2ae1bf52017-05-11 11:18:45 -0700919 // getting focus or if we're not visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS)
Evan Rosky2ae1bf52017-05-11 11:18:45 -0700921 && ((mViewFlags & VISIBILITY_MASK) == VISIBLE)
Adam Powell88c11752014-07-21 17:19:16 -0700922 && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // shortcut: don't report a new focusable view if we already are focused
924 // (and we don't prefer our descendants)
925 //
926 // note: knowing that mFocused is non-null is not a good enough reason
927 // to break the traversal since in that case we'd actually have to find
928 // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700929 // an ancestor of v; this will get checked for at ViewAncestor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) {
931 mParent.focusableViewAvailable(v);
932 }
933 }
934
Alan Viverettebe463f22016-01-21 10:50:10 -0500935 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 public boolean showContextMenuForChild(View originalView) {
Adam Powell2af189a2016-02-05 15:52:02 -0800937 if (isShowingContextMenuWithCoords()) {
938 // We're being called for compatibility. Return false and let the version
939 // with coordinates recurse up.
940 return false;
941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 return mParent != null && mParent.showContextMenuForChild(originalView);
943 }
944
Adam Powell2af189a2016-02-05 15:52:02 -0800945 /**
946 * @hide used internally for compatibility with existing app code only
947 */
948 public final boolean isShowingContextMenuWithCoords() {
949 return (mGroupFlags & FLAG_SHOW_CONTEXT_MENU_WITH_COORDS) != 0;
950 }
951
Oren Blasberged391262015-09-01 12:12:51 -0700952 @Override
953 public boolean showContextMenuForChild(View originalView, float x, float y) {
Adam Powell2af189a2016-02-05 15:52:02 -0800954 try {
955 mGroupFlags |= FLAG_SHOW_CONTEXT_MENU_WITH_COORDS;
956 if (showContextMenuForChild(originalView)) {
957 return true;
958 }
959 } finally {
960 mGroupFlags &= ~FLAG_SHOW_CONTEXT_MENU_WITH_COORDS;
961 }
Oren Blasberged391262015-09-01 12:12:51 -0700962 return mParent != null && mParent.showContextMenuForChild(originalView, x, y);
963 }
964
Clara Bayarri4423d912015-03-02 19:42:48 +0000965 @Override
Adam Powell6e346362010-07-23 10:18:23 -0700966 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000967 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) {
968 // This is the original call.
969 try {
970 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
971 return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY);
972 } finally {
973 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED;
974 }
975 } else {
976 // We are being called from the new method with type.
977 return SENTINEL_ACTION_MODE;
978 }
979 }
980
Clara Bayarri4423d912015-03-02 19:42:48 +0000981 @Override
982 public ActionMode startActionModeForChild(
983 View originalView, ActionMode.Callback callback, int type) {
Adam Powelle9fd6d22015-06-01 11:26:32 -0700984 if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0
985 && type == ActionMode.TYPE_PRIMARY) {
Clara Bayarri4423d912015-03-02 19:42:48 +0000986 ActionMode mode;
987 try {
988 mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
989 mode = startActionModeForChild(originalView, callback);
990 } finally {
991 mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED;
992 }
993 if (mode != SENTINEL_ACTION_MODE) {
994 return mode;
995 }
996 }
997 if (mParent != null) {
998 try {
999 return mParent.startActionModeForChild(originalView, callback, type);
1000 } catch (AbstractMethodError ame) {
1001 // Custom view parents might not implement this method.
1002 return mParent.startActionModeForChild(originalView, callback);
1003 }
1004 }
1005 return null;
Adam Powell6e346362010-07-23 10:18:23 -07001006 }
1007
1008 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00001009 * @hide
1010 */
1011 @Override
1012 public boolean dispatchActivityResult(
1013 String who, int requestCode, int resultCode, Intent data) {
1014 if (super.dispatchActivityResult(who, requestCode, resultCode, data)) {
1015 return true;
1016 }
1017 int childCount = getChildCount();
1018 for (int i = 0; i < childCount; i++) {
1019 View child = getChildAt(i);
1020 if (child.dispatchActivityResult(who, requestCode, resultCode, data)) {
1021 return true;
1022 }
1023 }
1024 return false;
1025 }
1026
1027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 * Find the nearest view in the specified direction that wants to take
1029 * focus.
1030 *
1031 * @param focused The view that currently has focus
1032 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
1033 * FOCUS_RIGHT, or 0 for not applicable.
1034 */
Alan Viverettebe463f22016-01-21 10:50:10 -05001035 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 public View focusSearch(View focused, int direction) {
Vadim Tryshevb5ced222017-01-17 19:31:35 -08001037 if (isRootNamespace()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 // root namespace means we should consider ourselves the top of the
1039 // tree for focus searching; otherwise we could be focus searching
Vadim Tryshev418b1fc2016-11-28 18:26:24 -08001040 // into other tabs. see LocalActivityManager and TabHost for more info.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
1042 } else if (mParent != null) {
1043 return mParent.focusSearch(focused, direction);
1044 }
1045 return null;
1046 }
1047
Alan Viverettebe463f22016-01-21 10:50:10 -05001048 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
1050 return false;
1051 }
1052
Svetoslav Ganov42138042012-03-20 11:51:39 -07001053 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001054 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07001055 ViewParent parent = mParent;
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001056 if (parent == null) {
1057 return false;
1058 }
1059 final boolean propagate = onRequestSendAccessibilityEvent(child, event);
1060 if (!propagate) {
1061 return false;
1062 }
1063 return parent.requestSendAccessibilityEvent(this, event);
1064 }
1065
1066 /**
1067 * Called when a child has requested sending an {@link AccessibilityEvent} and
1068 * gives an opportunity to its parent to augment the event.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07001069 * <p>
Adam Powell2fcbbd02011-09-28 18:56:43 -07001070 * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling
1071 * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its
1072 * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)}
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07001073 * is responsible for handling this call.
1074 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001075 *
1076 * @param child The child which requests sending the event.
1077 * @param event The event to be sent.
1078 * @return True if the event should be sent.
1079 *
1080 * @see #requestSendAccessibilityEvent(View, AccessibilityEvent)
1081 */
1082 public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07001083 if (mAccessibilityDelegate != null) {
1084 return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event);
1085 } else {
1086 return onRequestSendAccessibilityEventInternal(child, event);
1087 }
1088 }
1089
1090 /**
1091 * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent)
1092 *
1093 * Note: Called from the default {@link View.AccessibilityDelegate}.
Alan Viverettea54956a2015-01-07 16:05:02 -08001094 *
1095 * @hide
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07001096 */
Alan Viverettea54956a2015-01-07 16:05:02 -08001097 public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001098 return true;
1099 }
1100
1101 /**
Adam Powell539ee872012-02-03 19:00:49 -08001102 * Called when a child view has changed whether or not it is tracking transient state.
Adam Powell539ee872012-02-03 19:00:49 -08001103 */
Alan Viverettebe463f22016-01-21 10:50:10 -05001104 @Override
Adam Powell539ee872012-02-03 19:00:49 -08001105 public void childHasTransientStateChanged(View child, boolean childHasTransientState) {
1106 final boolean oldHasTransientState = hasTransientState();
1107 if (childHasTransientState) {
1108 mChildCountWithTransientState++;
1109 } else {
1110 mChildCountWithTransientState--;
1111 }
1112
1113 final boolean newHasTransientState = hasTransientState();
1114 if (mParent != null && oldHasTransientState != newHasTransientState) {
1115 try {
1116 mParent.childHasTransientStateChanged(this, newHasTransientState);
1117 } catch (AbstractMethodError e) {
1118 Log.e(TAG, mParent.getClass().getSimpleName() +
1119 " does not fully implement ViewParent", e);
1120 }
1121 }
1122 }
1123
Adam Powell539ee872012-02-03 19:00:49 -08001124 @Override
1125 public boolean hasTransientState() {
1126 return mChildCountWithTransientState > 0 || super.hasTransientState();
1127 }
1128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 @Override
1130 public boolean dispatchUnhandledMove(View focused, int direction) {
1131 return mFocused != null &&
1132 mFocused.dispatchUnhandledMove(focused, direction);
1133 }
1134
Alan Viverettebe463f22016-01-21 10:50:10 -05001135 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 public void clearChildFocus(View child) {
1137 if (DBG) {
1138 System.out.println(this + " clearChildFocus()");
1139 }
1140
1141 mFocused = null;
1142 if (mParent != null) {
1143 mParent.clearChildFocus(this);
1144 }
1145 }
1146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 @Override
1148 public void clearFocus() {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08001149 if (DBG) {
1150 System.out.println(this + " clearFocus()");
1151 }
1152 if (mFocused == null) {
1153 super.clearFocus();
1154 } else {
Svetoslav Ganovb552d892012-06-02 14:35:02 -07001155 View focused = mFocused;
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08001156 mFocused = null;
Svetoslav Ganovb552d892012-06-02 14:35:02 -07001157 focused.clearFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 }
1160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 @Override
Alan Viverette223622a2013-12-17 13:29:02 -08001162 void unFocus(View focused) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 if (DBG) {
1164 System.out.println(this + " unFocus()");
1165 }
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08001166 if (mFocused == null) {
Alan Viverette223622a2013-12-17 13:29:02 -08001167 super.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08001168 } else {
Alan Viverette223622a2013-12-17 13:29:02 -08001169 mFocused.unFocus(focused);
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08001170 mFocused = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173
1174 /**
1175 * Returns the focused child of this view, if any. The child may have focus
1176 * or contain focus.
1177 *
1178 * @return the focused child or null.
1179 */
1180 public View getFocusedChild() {
1181 return mFocused;
1182 }
1183
Adam Powell88c11752014-07-21 17:19:16 -07001184 View getDeepestFocusedChild() {
1185 View v = this;
1186 while (v != null) {
1187 if (v.isFocused()) {
1188 return v;
1189 }
1190 v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null;
1191 }
1192 return null;
1193 }
1194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Returns true if this view has or contains focus
1197 *
1198 * @return true if this view has or contains focus
1199 */
1200 @Override
1201 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001202 return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204
1205 /*
1206 * (non-Javadoc)
1207 *
1208 * @see android.view.View#findFocus()
1209 */
1210 @Override
1211 public View findFocus() {
1212 if (DBG) {
1213 System.out.println("Find focus in " + this + ": flags="
1214 + isFocused() + ", child=" + mFocused);
1215 }
1216
1217 if (isFocused()) {
1218 return this;
1219 }
1220
1221 if (mFocused != null) {
1222 return mFocused.findFocus();
1223 }
1224 return null;
1225 }
1226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 @Override
Adam Powell0f552f42017-02-03 11:50:42 -08001228 boolean hasFocusable(boolean allowAutoFocus, boolean dispatchExplicit) {
Alan Viverette7485a9b2017-02-27 11:55:53 -05001229 // This should probably be super.hasFocusable, but that would change
1230 // behavior. Historically, we have not checked the ancestor views for
1231 // shouldBlockFocusForTouchscreen() in ViewGroup.hasFocusable.
1232
1233 // Invisible and gone views are never focusable.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
1235 return false;
1236 }
1237
Alan Viverette7485a9b2017-02-27 11:55:53 -05001238 // Only use effective focusable value when allowed.
1239 if ((allowAutoFocus || getFocusable() != FOCUSABLE_AUTO) && isFocusable()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 return true;
1241 }
1242
Alan Viverette7485a9b2017-02-27 11:55:53 -05001243 // Determine whether we have a focused descendant.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 final int descendantFocusability = getDescendantFocusability();
Adam Powell88c11752014-07-21 17:19:16 -07001245 if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
Evan Rosky2ae1bf52017-05-11 11:18:45 -07001246 return hasFocusableChild(dispatchExplicit);
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248
Evan Rosky2ae1bf52017-05-11 11:18:45 -07001249 return false;
1250 }
Adam Powell0f552f42017-02-03 11:50:42 -08001251
Evan Rosky2ae1bf52017-05-11 11:18:45 -07001252 boolean hasFocusableChild(boolean dispatchExplicit) {
1253 // Determine whether we have a focusable descendant.
1254 final int count = mChildrenCount;
1255 final View[] children = mChildren;
1256
1257 for (int i = 0; i < count; i++) {
1258 final View child = children[i];
1259
1260 // In case the subclass has overridden has[Explicit]Focusable, dispatch
1261 // to the expected one for each child even though we share logic here.
1262 if ((dispatchExplicit && child.hasExplicitFocusable())
1263 || (!dispatchExplicit && child.hasFocusable())) {
1264 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 }
1266 }
1267
1268 return false;
1269 }
1270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001272 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 final int focusableCount = views.size();
1274
1275 final int descendantFocusability = getDescendantFocusability();
Evan Rosky18b886e2017-02-15 13:26:51 -08001276 final boolean blockFocusForTouchscreen = shouldBlockFocusForTouchscreen();
1277 final boolean focusSelf = (isFocusableInTouchMode() || !blockFocusForTouchscreen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278
Evan Rosky3b94bf52017-01-10 17:05:28 -08001279 if (descendantFocusability == FOCUS_BLOCK_DESCENDANTS) {
1280 if (focusSelf) {
1281 super.addFocusables(views, direction, focusableMode);
Adam Powell88c11752014-07-21 17:19:16 -07001282 }
Evan Rosky3b94bf52017-01-10 17:05:28 -08001283 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285
Evan Rosky18b886e2017-02-15 13:26:51 -08001286 if (blockFocusForTouchscreen) {
Evan Rosky3b94bf52017-01-10 17:05:28 -08001287 focusableMode |= FOCUSABLES_TOUCH_MODE;
1288 }
1289
1290 if ((descendantFocusability == FOCUS_BEFORE_DESCENDANTS) && focusSelf) {
1291 super.addFocusables(views, direction, focusableMode);
1292 }
1293
1294 int count = 0;
1295 final View[] children = new View[mChildrenCount];
1296 for (int i = 0; i < mChildrenCount; ++i) {
1297 View child = mChildren[i];
1298 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1299 children[count++] = child;
1300 }
1301 }
Evan Roskyd114e0f2017-03-23 11:20:04 -07001302 FocusFinder.sort(children, 0, count, this, isLayoutRtl());
Evan Rosky3b94bf52017-01-10 17:05:28 -08001303 for (int i = 0; i < count; ++i) {
1304 children[i].addFocusables(views, direction, focusableMode);
1305 }
1306
1307 // When set to FOCUS_AFTER_DESCENDANTS, we only add ourselves if
1308 // there aren't any focusable descendants. this is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 // to avoid the focus search finding layouts when a more precise search
1310 // among the focusable children would be more interesting.
Evan Rosky3b94bf52017-01-10 17:05:28 -08001311 if ((descendantFocusability == FOCUS_AFTER_DESCENDANTS) && focusSelf
1312 && focusableCount == views.size()) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001313 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
1315 }
1316
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001317 @Override
Vadim Tryshevb5ced222017-01-17 19:31:35 -08001318 public void addKeyboardNavigationClusters(Collection<View> views, int direction) {
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001319 final int focusableCount = views.size();
1320
Evan Rosky18b886e2017-02-15 13:26:51 -08001321 if (isKeyboardNavigationCluster()) {
1322 // Cluster-navigation can enter a touchscreenBlocksFocus cluster, so temporarily
1323 // disable touchscreenBlocksFocus to evaluate whether it contains focusables.
1324 final boolean blockedFocus = getTouchscreenBlocksFocus();
1325 try {
1326 setTouchscreenBlocksFocusNoRefocus(false);
1327 super.addKeyboardNavigationClusters(views, direction);
1328 } finally {
1329 setTouchscreenBlocksFocusNoRefocus(blockedFocus);
1330 }
1331 } else {
1332 super.addKeyboardNavigationClusters(views, direction);
1333 }
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001334
1335 if (focusableCount != views.size()) {
Vadim Tryshev311a5b52017-01-05 18:54:11 -08001336 // No need to look for groups inside a group.
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001337 return;
1338 }
1339
1340 if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
1341 return;
1342 }
1343
Evan Roskybd10c522017-03-27 15:50:38 -07001344 int count = 0;
1345 final View[] visibleChildren = new View[mChildrenCount];
1346 for (int i = 0; i < mChildrenCount; ++i) {
1347 final View child = mChildren[i];
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001348 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Evan Roskybd10c522017-03-27 15:50:38 -07001349 visibleChildren[count++] = child;
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001350 }
1351 }
Evan Roskyd114e0f2017-03-23 11:20:04 -07001352 FocusFinder.sort(visibleChildren, 0, count, this, isLayoutRtl());
Evan Roskybd10c522017-03-27 15:50:38 -07001353 for (int i = 0; i < count; ++i) {
1354 visibleChildren[i].addKeyboardNavigationClusters(views, direction);
1355 }
Vadim Tryshev01b0c9e2016-11-21 15:25:01 -08001356 }
1357
Adam Powellff0d2982014-07-10 20:34:14 -07001358 /**
1359 * Set whether this ViewGroup should ignore focus requests for itself and its children.
1360 * If this option is enabled and the ViewGroup or a descendant currently has focus, focus
1361 * will proceed forward.
1362 *
1363 * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen
1364 */
1365 public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {
1366 if (touchscreenBlocksFocus) {
1367 mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
Evan Rosky0e8a6832017-04-10 12:35:15 -07001368 if (hasFocus() && !isKeyboardNavigationCluster()) {
Adam Powell88c11752014-07-21 17:19:16 -07001369 final View focusedChild = getDeepestFocusedChild();
1370 if (!focusedChild.isFocusableInTouchMode()) {
1371 final View newFocus = focusSearch(FOCUS_FORWARD);
1372 if (newFocus != null) {
1373 newFocus.requestFocus();
1374 }
Adam Powellff0d2982014-07-10 20:34:14 -07001375 }
1376 }
1377 } else {
1378 mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1379 }
1380 }
1381
Evan Rosky18b886e2017-02-15 13:26:51 -08001382 private void setTouchscreenBlocksFocusNoRefocus(boolean touchscreenBlocksFocus) {
1383 if (touchscreenBlocksFocus) {
1384 mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1385 } else {
1386 mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1387 }
1388 }
1389
Adam Powellff0d2982014-07-10 20:34:14 -07001390 /**
1391 * Check whether this ViewGroup should ignore focus requests for itself and its children.
1392 */
Evan Roskyecb63fc2017-04-13 16:57:18 -07001393 @ViewDebug.ExportedProperty(category = "focus")
Adam Powellff0d2982014-07-10 20:34:14 -07001394 public boolean getTouchscreenBlocksFocus() {
1395 return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0;
1396 }
1397
1398 boolean shouldBlockFocusForTouchscreen() {
Evan Rosky18b886e2017-02-15 13:26:51 -08001399 // There is a special case for keyboard-navigation clusters. We allow cluster navigation
1400 // to jump into blockFocusForTouchscreen ViewGroups which are clusters. Once in the
1401 // cluster, focus is free to move around within it.
Adam Powellff0d2982014-07-10 20:34:14 -07001402 return getTouchscreenBlocksFocus() &&
Evan Rosky18b886e2017-02-15 13:26:51 -08001403 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
Evan Rosky0e8a6832017-04-10 12:35:15 -07001404 && !(isKeyboardNavigationCluster()
1405 && (hasFocus() || (findKeyboardNavigationCluster() != this)));
Adam Powellff0d2982014-07-10 20:34:14 -07001406 }
1407
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001408 @Override
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001409 public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) {
1410 super.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001411 final int childrenCount = mChildrenCount;
1412 final View[] children = mChildren;
1413 for (int i = 0; i < childrenCount; i++) {
1414 View child = children[i];
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001415 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
Dianne Hackborn4702a852012-08-17 15:18:29 -07001416 && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001417 child.findViewsWithText(outViews, text, flags);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001418 }
1419 }
1420 }
1421
Svetoslav5b578da2013-05-08 14:23:32 -07001422 /** @hide */
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001423 @Override
Svetoslav5b578da2013-05-08 14:23:32 -07001424 public View findViewByAccessibilityIdTraversal(int accessibilityId) {
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001425 View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId);
1426 if (foundView != null) {
1427 return foundView;
1428 }
Svetoslav6cc46272015-06-03 11:38:30 -07001429
1430 if (getAccessibilityNodeProvider() != null) {
1431 return null;
1432 }
1433
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001434 final int childrenCount = mChildrenCount;
1435 final View[] children = mChildren;
1436 for (int i = 0; i < childrenCount; i++) {
1437 View child = children[i];
1438 foundView = child.findViewByAccessibilityIdTraversal(accessibilityId);
1439 if (foundView != null) {
1440 return foundView;
1441 }
1442 }
Svetoslav6cc46272015-06-03 11:38:30 -07001443
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -07001444 return null;
1445 }
1446
Phil Weaver846cda932017-06-15 10:10:06 -07001447 /** @hide */
1448 @Override
1449 public View findViewByAutofillIdTraversal(int autofillId) {
1450 View foundView = super.findViewByAutofillIdTraversal(autofillId);
1451 if (foundView != null) {
1452 return foundView;
1453 }
1454
1455 final int childrenCount = mChildrenCount;
1456 final View[] children = mChildren;
1457 for (int i = 0; i < childrenCount; i++) {
1458 View child = children[i];
1459 foundView = child.findViewByAutofillIdTraversal(autofillId);
1460 if (foundView != null) {
1461 return foundView;
1462 }
1463 }
1464
1465 return null;
1466 }
1467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 @Override
1469 public void dispatchWindowFocusChanged(boolean hasFocus) {
1470 super.dispatchWindowFocusChanged(hasFocus);
1471 final int count = mChildrenCount;
1472 final View[] children = mChildren;
1473 for (int i = 0; i < count; i++) {
1474 children[i].dispatchWindowFocusChanged(hasFocus);
1475 }
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 @Override
1479 public void addTouchables(ArrayList<View> views) {
1480 super.addTouchables(views);
1481
1482 final int count = mChildrenCount;
1483 final View[] children = mChildren;
1484
1485 for (int i = 0; i < count; i++) {
1486 final View child = children[i];
1487 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
1488 child.addTouchables(views);
1489 }
1490 }
1491 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001492
1493 /**
1494 * @hide
1495 */
1496 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01001497 @UnsupportedAppUsage
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001498 public void makeOptionalFitsSystemWindows() {
1499 super.makeOptionalFitsSystemWindows();
1500 final int count = mChildrenCount;
1501 final View[] children = mChildren;
1502 for (int i = 0; i < count; i++) {
1503 children[i].makeOptionalFitsSystemWindows();
1504 }
1505 }
1506
Romain Guy43c9cdf2010-01-27 13:53:55 -08001507 @Override
1508 public void dispatchDisplayHint(int hint) {
1509 super.dispatchDisplayHint(hint);
1510 final int count = mChildrenCount;
1511 final View[] children = mChildren;
1512 for (int i = 0; i < count; i++) {
1513 children[i].dispatchDisplayHint(hint);
1514 }
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
1517 /**
Chet Haase0d299362012-01-26 10:51:48 -08001518 * Called when a view's visibility has changed. Notify the parent to take any appropriate
1519 * action.
1520 *
1521 * @param child The view whose visibility has changed
1522 * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE).
1523 * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE).
Chet Haase5e25c2c2010-09-16 11:15:56 -07001524 * @hide
Chet Haase5e25c2c2010-09-16 11:15:56 -07001525 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01001526 @UnsupportedAppUsage
Chet Haase0d299362012-01-26 10:51:48 -08001527 protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07001528 if (mTransition != null) {
Chet Haase0d299362012-01-26 10:51:48 -08001529 if (newVisibility == VISIBLE) {
1530 mTransition.showChild(this, child, oldVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001531 } else {
Chet Haase0d299362012-01-26 10:51:48 -08001532 mTransition.hideChild(this, child, newVisibility);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001533 if (mTransitioningViews != null && mTransitioningViews.contains(child)) {
Chet Haaseddbb3462012-06-19 13:54:29 -07001534 // Only track this on disappearing views - appearing views are already visible
1535 // and don't need special handling during drawChild()
1536 if (mVisibilityChangingChildren == null) {
1537 mVisibilityChangingChildren = new ArrayList<View>();
1538 }
1539 mVisibilityChangingChildren.add(child);
Chet Haase5e25c2c2010-09-16 11:15:56 -07001540 addDisappearingView(child);
1541 }
1542 }
1543 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001544
1545 // in all cases, for drags
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001546 if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) {
1547 if (!mChildrenInterestedInDrag.contains(child)) {
1548 notifyChildOfDragStart(child);
Christopher Tate86cab1b2011-01-13 20:28:55 -08001549 }
1550 }
Chet Haase5e25c2c2010-09-16 11:15:56 -07001551 }
1552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 @Override
Adam Powell326d8082009-12-09 15:10:07 -08001554 protected void dispatchVisibilityChanged(View changedView, int visibility) {
1555 super.dispatchVisibilityChanged(changedView, visibility);
1556 final int count = mChildrenCount;
1557 final View[] children = mChildren;
1558 for (int i = 0; i < count; i++) {
1559 children[i].dispatchVisibilityChanged(changedView, visibility);
1560 }
1561 }
1562
Adam Powell326d8082009-12-09 15:10:07 -08001563 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 public void dispatchWindowVisibilityChanged(int visibility) {
1565 super.dispatchWindowVisibilityChanged(visibility);
1566 final int count = mChildrenCount;
1567 final View[] children = mChildren;
1568 for (int i = 0; i < count; i++) {
1569 children[i].dispatchWindowVisibilityChanged(visibility);
1570 }
1571 }
1572
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001573 @Override
Adam Powell9c146bf2016-03-15 17:35:00 -07001574 boolean dispatchVisibilityAggregated(boolean isVisible) {
1575 isVisible = super.dispatchVisibilityAggregated(isVisible);
Adam Powell41d96902016-03-15 14:43:19 -07001576 final int count = mChildrenCount;
1577 final View[] children = mChildren;
1578 for (int i = 0; i < count; i++) {
Adam Powell9c146bf2016-03-15 17:35:00 -07001579 // Only dispatch to visible children. Not visible children and their subtrees already
1580 // know that they aren't visible and that's not going to change as a result of
1581 // whatever triggered this dispatch.
1582 if (children[i].getVisibility() == VISIBLE) {
1583 children[i].dispatchVisibilityAggregated(isVisible);
Adam Powell41d96902016-03-15 14:43:19 -07001584 }
1585 }
Adam Powell9c146bf2016-03-15 17:35:00 -07001586 return isVisible;
Adam Powell41d96902016-03-15 14:43:19 -07001587 }
1588
1589 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001590 public void dispatchConfigurationChanged(Configuration newConfig) {
1591 super.dispatchConfigurationChanged(newConfig);
1592 final int count = mChildrenCount;
1593 final View[] children = mChildren;
1594 for (int i = 0; i < count; i++) {
1595 children[i].dispatchConfigurationChanged(newConfig);
1596 }
1597 }
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08001598
Alan Viverettebe463f22016-01-21 10:50:10 -05001599 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 public void recomputeViewAttributes(View child) {
Joe Onorato664644d2011-01-23 17:53:23 -08001601 if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
1602 ViewParent parent = mParent;
1603 if (parent != null) parent.recomputeViewAttributes(this);
1604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
Romain Guy8506ab42009-06-11 17:35:47 -07001606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001608 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
1609 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
1610 super.dispatchCollectViewAttributes(attachInfo, visibility);
1611 final int count = mChildrenCount;
1612 final View[] children = mChildren;
1613 for (int i = 0; i < count; i++) {
1614 final View child = children[i];
1615 child.dispatchCollectViewAttributes(attachInfo,
1616 visibility | (child.mViewFlags&VISIBILITY_MASK));
1617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
1619 }
1620
Alan Viverettebe463f22016-01-21 10:50:10 -05001621 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 public void bringChildToFront(View child) {
Alan Viverette77bb6f12015-02-11 17:24:33 -08001623 final int index = indexOfChild(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 if (index >= 0) {
1625 removeFromArray(index);
1626 addInArray(child, mChildrenCount);
1627 child.mParent = this;
Chet Haasecb96db82013-09-04 10:21:46 -07001628 requestLayout();
1629 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
1631 }
1632
Romain Guy6410c0a2013-06-17 11:21:58 -07001633 private PointF getLocalPoint() {
1634 if (mLocalPoint == null) mLocalPoint = new PointF();
1635 return mLocalPoint;
1636 }
1637
Vadim Tryshevef128112016-09-16 14:05:53 -07001638 @Override
1639 boolean dispatchDragEnterExitInPreN(DragEvent event) {
1640 if (event.mAction == DragEvent.ACTION_DRAG_EXITED && mCurrentDragChild != null) {
1641 // The drag exited a sub-tree of views; notify of the exit all descendants that are in
1642 // entered state.
1643 // We don't need this recursive delivery for ENTERED events because they get generated
1644 // from the recursive delivery of LOCATION/DROP events, and hence, don't need their own
1645 // recursion.
1646 mCurrentDragChild.dispatchDragEnterExitInPreN(event);
1647 mCurrentDragChild = null;
1648 }
1649 return mIsInterestedInDrag && super.dispatchDragEnterExitInPreN(event);
1650 }
1651
Steve Block8a7259b2012-03-01 11:24:41 +00001652 // TODO: Write real docs
Christopher Tatea53146c2010-09-07 11:57:52 -07001653 @Override
1654 public boolean dispatchDragEvent(DragEvent event) {
1655 boolean retval = false;
1656 final float tx = event.mX;
1657 final float ty = event.mY;
Vadim Trysheva61efa42016-09-28 15:15:52 -07001658 final ClipData td = event.mClipData;
Christopher Tatea53146c2010-09-07 11:57:52 -07001659
Christopher Tatea53146c2010-09-07 11:57:52 -07001660 // Dispatch down the view hierarchy
Romain Guy6410c0a2013-06-17 11:21:58 -07001661 final PointF localPoint = getLocalPoint();
1662
Christopher Tatea53146c2010-09-07 11:57:52 -07001663 switch (event.mAction) {
1664 case DragEvent.ACTION_DRAG_STARTED: {
Vadim Tryshevef128112016-09-16 14:05:53 -07001665 // Clear the state to recalculate which views we drag over.
1666 mCurrentDragChild = null;
1667
Christopher Tate86cab1b2011-01-13 20:28:55 -08001668 // Set up our tracking of drag-started notifications
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001669 mCurrentDragStartEvent = DragEvent.obtain(event);
1670 if (mChildrenInterestedInDrag == null) {
1671 mChildrenInterestedInDrag = new HashSet<View>();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001672 } else {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001673 mChildrenInterestedInDrag.clear();
Christopher Tate86cab1b2011-01-13 20:28:55 -08001674 }
1675
Christopher Tatea53146c2010-09-07 11:57:52 -07001676 // Now dispatch down to our children, caching the responses
Christopher Tatea53146c2010-09-07 11:57:52 -07001677 final int count = mChildrenCount;
1678 final View[] children = mChildren;
1679 for (int i = 0; i < count; i++) {
Christopher Tate2c095f32010-10-04 14:13:40 -07001680 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001681 child.mPrivateFlags2 &= ~View.DRAG_MASK;
Christopher Tate2c095f32010-10-04 14:13:40 -07001682 if (child.getVisibility() == VISIBLE) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001683 if (notifyChildOfDragStart(children[i])) {
1684 retval = true;
Christopher Tate2c095f32010-10-04 14:13:40 -07001685 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001686 }
1687 }
1688
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001689 // Notify itself of the drag start.
1690 mIsInterestedInDrag = super.dispatchDragEvent(event);
1691 if (mIsInterestedInDrag) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001692 retval = true;
1693 }
Yorke Lee0394b212016-03-10 18:20:04 -08001694
1695 if (!retval) {
1696 // Neither us nor any of our children are interested in this drag, so stop tracking
1697 // the current drag event.
1698 mCurrentDragStartEvent.recycle();
1699 mCurrentDragStartEvent = null;
1700 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001701 } break;
1702
1703 case DragEvent.ACTION_DRAG_ENDED: {
Christopher Tate86cab1b2011-01-13 20:28:55 -08001704 // Release the bookkeeping now that the drag lifecycle has ended
Vadim Tryshev15177892015-12-18 08:01:00 -08001705 final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag;
1706 if (childrenInterestedInDrag != null) {
1707 for (View child : childrenInterestedInDrag) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001708 // If a child was interested in the ongoing drag, it's told that it's over
1709 if (child.dispatchDragEvent(event)) {
1710 retval = true;
1711 }
Christopher Tate1fc014f2011-01-19 12:56:26 -08001712 }
Vadim Tryshev15177892015-12-18 08:01:00 -08001713 childrenInterestedInDrag.clear();
1714 }
1715 if (mCurrentDragStartEvent != null) {
1716 mCurrentDragStartEvent.recycle();
1717 mCurrentDragStartEvent = null;
Christopher Tate1fc014f2011-01-19 12:56:26 -08001718 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001719
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001720 if (mIsInterestedInDrag) {
1721 if (super.dispatchDragEvent(event)) {
1722 retval = true;
1723 }
1724 mIsInterestedInDrag = false;
Christopher Tatea53146c2010-09-07 11:57:52 -07001725 }
1726 } break;
1727
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -07001728 case DragEvent.ACTION_DRAG_LOCATION:
1729 case DragEvent.ACTION_DROP: {
Christopher Tatea53146c2010-09-07 11:57:52 -07001730 // Find the [possibly new] drag target
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001731 View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
Vadim Tryshevef128112016-09-16 14:05:53 -07001732
1733 if (target != mCurrentDragChild) {
Vadim Tryshev45bee6b2016-09-19 11:00:37 -07001734 if (sCascadedDragDrop) {
Vadim Tryshevef128112016-09-16 14:05:53 -07001735 // For pre-Nougat apps, make sure that the whole hierarchy of views that contain
1736 // the drag location is kept in the state between ENTERED and EXITED events.
1737 // (Starting with N, only the innermost view will be in that state).
1738
1739 final int action = event.mAction;
1740 // Position should not be available for ACTION_DRAG_ENTERED and
1741 // ACTION_DRAG_EXITED.
1742 event.mX = 0;
1743 event.mY = 0;
Vadim Trysheva61efa42016-09-28 15:15:52 -07001744 event.mClipData = null;
Vadim Tryshevef128112016-09-16 14:05:53 -07001745
1746 if (mCurrentDragChild != null) {
1747 event.mAction = DragEvent.ACTION_DRAG_EXITED;
1748 mCurrentDragChild.dispatchDragEnterExitInPreN(event);
1749 }
1750
1751 if (target != null) {
1752 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
1753 target.dispatchDragEnterExitInPreN(event);
1754 }
1755
1756 event.mAction = action;
1757 event.mX = tx;
1758 event.mY = ty;
Vadim Trysheva61efa42016-09-28 15:15:52 -07001759 event.mClipData = td;
Vadim Tryshevef128112016-09-16 14:05:53 -07001760 }
1761 mCurrentDragChild = target;
1762 }
1763
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001764 if (target == null && mIsInterestedInDrag) {
1765 target = this;
1766 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001767
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -07001768 // Dispatch the actual drag notice, localized into the target coordinates.
Christopher Tatea53146c2010-09-07 11:57:52 -07001769 if (target != null) {
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001770 if (target != this) {
1771 event.mX = localPoint.x;
1772 event.mY = localPoint.y;
Christopher Tatea53146c2010-09-07 11:57:52 -07001773
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001774 retval = target.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07001775
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001776 event.mX = tx;
1777 event.mY = ty;
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -07001778
Vadim Tryshev45bee6b2016-09-19 11:00:37 -07001779 if (mIsInterestedInDrag) {
1780 final boolean eventWasConsumed;
1781 if (sCascadedDragDrop) {
1782 eventWasConsumed = retval;
1783 } else {
1784 eventWasConsumed = event.mEventHandlerWasCalled;
1785 }
1786
1787 if (!eventWasConsumed) {
1788 retval = super.dispatchDragEvent(event);
1789 }
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -07001790 }
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001791 } else {
1792 retval = super.dispatchDragEvent(event);
1793 }
Christopher Tatea53146c2010-09-07 11:57:52 -07001794 }
1795 } break;
Christopher Tatea53146c2010-09-07 11:57:52 -07001796 }
1797
Christopher Tatea53146c2010-09-07 11:57:52 -07001798 return retval;
1799 }
1800
1801 // Find the frontmost child view that lies under the given point, and calculate
1802 // the position within its own local coordinate system.
1803 View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001804 final int count = mChildrenCount;
1805 final View[] children = mChildren;
1806 for (int i = count - 1; i >= 0; i--) {
1807 final View child = children[i];
Christopher Tate3d4bf172011-03-28 16:16:46 -07001808 if (!child.canAcceptDrag()) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001809 continue;
1810 }
1811
Christopher Tate2c095f32010-10-04 14:13:40 -07001812 if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001813 return child;
1814 }
1815 }
1816 return null;
1817 }
1818
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001819 boolean notifyChildOfDragStart(View child) {
1820 // The caller guarantees that the child is not in mChildrenInterestedInDrag yet.
1821
Christopher Tate86cab1b2011-01-13 20:28:55 -08001822 if (ViewDebug.DEBUG_DRAG) {
1823 Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child);
1824 }
1825
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -07001826 final float tx = mCurrentDragStartEvent.mX;
1827 final float ty = mCurrentDragStartEvent.mY;
1828
1829 final float[] point = getTempPoint();
1830 point[0] = tx;
1831 point[1] = ty;
1832 transformPointToViewLocal(point, child);
1833
1834 mCurrentDragStartEvent.mX = point[0];
1835 mCurrentDragStartEvent.mY = point[1];
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001836 final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent);
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -07001837 mCurrentDragStartEvent.mX = tx;
1838 mCurrentDragStartEvent.mY = ty;
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -07001839 mCurrentDragStartEvent.mEventHandlerWasCalled = false;
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07001840 if (canAccept) {
1841 mChildrenInterestedInDrag.add(child);
1842 if (!child.canAcceptDrag()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001843 child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001844 child.refreshDrawableState();
1845 }
Christopher Tate86cab1b2011-01-13 20:28:55 -08001846 }
Christopher Tate3d4bf172011-03-28 16:16:46 -07001847 return canAccept;
Christopher Tate86cab1b2011-01-13 20:28:55 -08001848 }
1849
Joe Onorato664644d2011-01-23 17:53:23 -08001850 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001851 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
1852 super.dispatchWindowSystemUiVisiblityChanged(visible);
1853
1854 final int count = mChildrenCount;
1855 final View[] children = mChildren;
1856 for (int i=0; i <count; i++) {
1857 final View child = children[i];
1858 child.dispatchWindowSystemUiVisiblityChanged(visible);
1859 }
1860 }
1861
1862 @Override
Joe Onorato664644d2011-01-23 17:53:23 -08001863 public void dispatchSystemUiVisibilityChanged(int visible) {
1864 super.dispatchSystemUiVisibilityChanged(visible);
1865
1866 final int count = mChildrenCount;
1867 final View[] children = mChildren;
1868 for (int i=0; i <count; i++) {
1869 final View child = children[i];
1870 child.dispatchSystemUiVisibilityChanged(visible);
1871 }
1872 }
1873
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001874 @Override
Dianne Hackborncf675782012-05-10 15:07:24 -07001875 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
1876 boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001877
1878 final int count = mChildrenCount;
1879 final View[] children = mChildren;
1880 for (int i=0; i <count; i++) {
1881 final View child = children[i];
Dianne Hackborncf675782012-05-10 15:07:24 -07001882 changed |= child.updateLocalSystemUiVisibility(localValue, localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001883 }
Dianne Hackborncf675782012-05-10 15:07:24 -07001884 return changed;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001885 }
1886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 @Override
1888 public boolean dispatchKeyEventPreIme(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001889 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1890 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 return super.dispatchKeyEventPreIme(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001892 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1893 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 return mFocused.dispatchKeyEventPreIme(event);
1895 }
1896 return false;
1897 }
1898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 @Override
1900 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001901 if (mInputEventConsistencyVerifier != null) {
1902 mInputEventConsistencyVerifier.onKeyEvent(event, 1);
1903 }
1904
Dianne Hackborn4702a852012-08-17 15:18:29 -07001905 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1906 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001907 if (super.dispatchKeyEvent(event)) {
1908 return true;
1909 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001910 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1911 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001912 if (mFocused.dispatchKeyEvent(event)) {
1913 return true;
1914 }
1915 }
1916
1917 if (mInputEventConsistencyVerifier != null) {
1918 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
1920 return false;
1921 }
1922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 @Override
1924 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001925 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1926 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 return super.dispatchKeyShortcutEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07001928 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1929 == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 return mFocused.dispatchKeyShortcutEvent(event);
1931 }
1932 return false;
1933 }
1934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 @Override
1936 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08001937 if (mInputEventConsistencyVerifier != null) {
1938 mInputEventConsistencyVerifier.onTrackballEvent(event, 1);
1939 }
1940
Dianne Hackborn4702a852012-08-17 15:18:29 -07001941 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1942 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001943 if (super.dispatchTrackballEvent(event)) {
1944 return true;
1945 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001946 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1947 == PFLAG_HAS_BOUNDS) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07001948 if (mFocused.dispatchTrackballEvent(event)) {
1949 return true;
1950 }
1951 }
1952
1953 if (mInputEventConsistencyVerifier != null) {
1954 mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
1956 return false;
1957 }
1958
Jun Mukai1db53972015-09-11 18:08:31 -07001959 @Override
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001960 public boolean dispatchCapturedPointerEvent(MotionEvent event) {
1961 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
1962 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
1963 if (super.dispatchCapturedPointerEvent(event)) {
1964 return true;
1965 }
1966 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
1967 == PFLAG_HAS_BOUNDS) {
1968 if (mFocused.dispatchCapturedPointerEvent(event)) {
1969 return true;
1970 }
1971 }
1972 return false;
1973 }
1974
1975 @Override
1976 public void dispatchPointerCaptureChanged(boolean hasCapture) {
1977 exitHoverTargets();
1978
1979 super.dispatchPointerCaptureChanged(hasCapture);
1980 final int count = mChildrenCount;
1981 final View[] children = mChildren;
1982 for (int i = 0; i < count; i++) {
1983 children[i].dispatchPointerCaptureChanged(hasCapture);
1984 }
1985 }
1986
1987 @Override
Michael Wrighte051f6f2016-05-13 17:44:16 +01001988 public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
1989 final float x = event.getX(pointerIndex);
1990 final float y = event.getY(pointerIndex);
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09001991 if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) {
Michael Wrighte051f6f2016-05-13 17:44:16 +01001992 return PointerIcon.getSystemIcon(mContext, PointerIcon.TYPE_ARROW);
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09001993 }
Jun Mukai1db53972015-09-11 18:08:31 -07001994 // Check what the child under the pointer says about the pointer.
1995 final int childrenCount = mChildrenCount;
1996 if (childrenCount != 0) {
1997 final ArrayList<View> preorderedList = buildOrderedChildList();
1998 final boolean customOrder = preorderedList == null
1999 && isChildrenDrawingOrderEnabled();
2000 final View[] children = mChildren;
2001 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002002 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
2003 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Vladislav Kaznacheev3d5cfc02017-01-05 17:50:38 -08002004 if (!canViewReceivePointerEvents(child)
2005 || !isTransformedTouchPointInView(x, y, child, null)) {
2006 continue;
2007 }
2008 final PointerIcon pointerIcon =
2009 dispatchResolvePointerIcon(event, pointerIndex, child);
2010 if (pointerIcon != null) {
2011 if (preorderedList != null) preorderedList.clear();
2012 return pointerIcon;
Jun Mukai1db53972015-09-11 18:08:31 -07002013 }
2014 }
Chris Craikfc563772016-05-04 13:34:30 -07002015 if (preorderedList != null) preorderedList.clear();
Jun Mukai1db53972015-09-11 18:08:31 -07002016 }
2017
2018 // The pointer is not a child or the child has no preferences, returning the default
2019 // implementation.
Michael Wrighte051f6f2016-05-13 17:44:16 +01002020 return super.onResolvePointerIcon(event, pointerIndex);
2021 }
2022
2023 private PointerIcon dispatchResolvePointerIcon(MotionEvent event, int pointerIndex,
2024 View child) {
2025 final PointerIcon pointerIcon;
2026 if (!child.hasIdentityMatrix()) {
2027 MotionEvent transformedEvent = getTransformedMotionEvent(event, child);
2028 pointerIcon = child.onResolvePointerIcon(transformedEvent, pointerIndex);
2029 transformedEvent.recycle();
2030 } else {
2031 final float offsetX = mScrollX - child.mLeft;
2032 final float offsetY = mScrollY - child.mTop;
2033 event.offsetLocation(offsetX, offsetY);
2034 pointerIcon = child.onResolvePointerIcon(event, pointerIndex);
2035 event.offsetLocation(-offsetX, -offsetY);
2036 }
2037 return pointerIcon;
Jun Mukai1db53972015-09-11 18:08:31 -07002038 }
2039
Alan Viverettea7b85e62016-01-22 10:14:02 -05002040 private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {
2041 final int childIndex;
2042 if (customOrder) {
2043 final int childIndex1 = getChildDrawingOrder(childrenCount, i);
2044 if (childIndex1 >= childrenCount) {
2045 throw new IndexOutOfBoundsException("getChildDrawingOrder() "
2046 + "returned invalid index " + childIndex1
2047 + " (child count is " + childrenCount + ")");
2048 }
2049 childIndex = childIndex1;
2050 } else {
2051 childIndex = i;
2052 }
2053 return childIndex;
2054 }
2055
Romain Guya9489272011-06-22 20:58:11 -07002056 @SuppressWarnings({"ConstantConditions"})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 @Override
Jeff Browna032cc02011-03-07 16:56:21 -08002058 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08002059 final int action = event.getAction();
Jeff Browna032cc02011-03-07 16:56:21 -08002060
Jeff Brown10b62902011-06-20 16:40:37 -07002061 // First check whether the view group wants to intercept the hover event.
2062 final boolean interceptHover = onInterceptHoverEvent(event);
2063 event.setAction(action); // restore action in case it was changed
2064
Jeff Brown87b7f802011-06-21 18:35:45 -07002065 MotionEvent eventNoHistory = event;
2066 boolean handled = false;
2067
2068 // Send events to the hovered children and build a new list of hover targets until
2069 // one is found that handles the event.
2070 HoverTarget firstOldHoverTarget = mFirstHoverTarget;
2071 mFirstHoverTarget = null;
Jeff Brown10b62902011-06-20 16:40:37 -07002072 if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) {
Jeff Browna032cc02011-03-07 16:56:21 -08002073 final float x = event.getX();
2074 final float y = event.getY();
Jeff Brown33bbfd22011-02-24 20:55:35 -08002075 final int childrenCount = mChildrenCount;
2076 if (childrenCount != 0) {
Chris Craikab008f02014-05-23 17:55:03 -07002077 final ArrayList<View> preorderedList = buildOrderedChildList();
2078 final boolean customOrder = preorderedList == null
2079 && isChildrenDrawingOrderEnabled();
Jeff Brown33bbfd22011-02-24 20:55:35 -08002080 final View[] children = mChildren;
Jeff Brown87b7f802011-06-21 18:35:45 -07002081 HoverTarget lastHoverTarget = null;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002082 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002083 final int childIndex = getAndVerifyPreorderedIndex(
2084 childrenCount, i, customOrder);
2085 final View child = getAndVerifyPreorderedView(
2086 preorderedList, children, childIndex);
Jeff Brown87b7f802011-06-21 18:35:45 -07002087 if (!canViewReceivePointerEvents(child)
2088 || !isTransformedTouchPointInView(x, y, child, null)) {
2089 continue;
2090 }
2091
2092 // Obtain a hover target for this child. Dequeue it from the
2093 // old hover target list if the child was previously hovered.
2094 HoverTarget hoverTarget = firstOldHoverTarget;
2095 final boolean wasHovered;
2096 for (HoverTarget predecessor = null; ;) {
2097 if (hoverTarget == null) {
2098 hoverTarget = HoverTarget.obtain(child);
2099 wasHovered = false;
2100 break;
2101 }
2102
2103 if (hoverTarget.child == child) {
2104 if (predecessor != null) {
2105 predecessor.next = hoverTarget.next;
2106 } else {
2107 firstOldHoverTarget = hoverTarget.next;
2108 }
2109 hoverTarget.next = null;
2110 wasHovered = true;
2111 break;
2112 }
2113
2114 predecessor = hoverTarget;
2115 hoverTarget = hoverTarget.next;
2116 }
2117
2118 // Enqueue the hover target onto the new hover target list.
2119 if (lastHoverTarget != null) {
2120 lastHoverTarget.next = hoverTarget;
2121 } else {
Jeff Brown87b7f802011-06-21 18:35:45 -07002122 mFirstHoverTarget = hoverTarget;
2123 }
Sangkyu Lee8725f362013-03-13 09:38:45 +09002124 lastHoverTarget = hoverTarget;
Jeff Brown87b7f802011-06-21 18:35:45 -07002125
2126 // Dispatch the event to the child.
2127 if (action == MotionEvent.ACTION_HOVER_ENTER) {
2128 if (!wasHovered) {
2129 // Send the enter as is.
2130 handled |= dispatchTransformedGenericPointerEvent(
2131 event, child); // enter
2132 }
2133 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
2134 if (!wasHovered) {
2135 // Synthesize an enter from a move.
2136 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
2137 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
2138 handled |= dispatchTransformedGenericPointerEvent(
2139 eventNoHistory, child); // enter
2140 eventNoHistory.setAction(action);
2141
2142 handled |= dispatchTransformedGenericPointerEvent(
2143 eventNoHistory, child); // move
2144 } else {
2145 // Send the move as is.
2146 handled |= dispatchTransformedGenericPointerEvent(event, child);
2147 }
2148 }
2149 if (handled) {
Jeff Brown10b62902011-06-20 16:40:37 -07002150 break;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002151 }
Jeff Brown10b62902011-06-20 16:40:37 -07002152 }
Chris Craikab008f02014-05-23 17:55:03 -07002153 if (preorderedList != null) preorderedList.clear();
Jeff Brown10b62902011-06-20 16:40:37 -07002154 }
2155 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002156
Jeff Brown87b7f802011-06-21 18:35:45 -07002157 // Send exit events to all previously hovered children that are no longer hovered.
2158 while (firstOldHoverTarget != null) {
2159 final View child = firstOldHoverTarget.child;
Jeff Brown10b62902011-06-20 16:40:37 -07002160
Jeff Brown87b7f802011-06-21 18:35:45 -07002161 // Exit the old hovered child.
2162 if (action == MotionEvent.ACTION_HOVER_EXIT) {
2163 // Send the exit as is.
2164 handled |= dispatchTransformedGenericPointerEvent(
2165 event, child); // exit
2166 } else {
2167 // Synthesize an exit from a move or enter.
2168 // Ignore the result because hover focus has moved to a different view.
2169 if (action == MotionEvent.ACTION_HOVER_MOVE) {
Vladislav Kaznacheev5a77c372016-10-10 16:11:15 -07002170 final boolean hoverExitPending = event.isHoverExitPending();
2171 event.setHoverExitPending(true);
Jeff Brown10b62902011-06-20 16:40:37 -07002172 dispatchTransformedGenericPointerEvent(
Jeff Brown87b7f802011-06-21 18:35:45 -07002173 event, child); // move
Vladislav Kaznacheev5a77c372016-10-10 16:11:15 -07002174 event.setHoverExitPending(hoverExitPending);
Jeff Brown10b62902011-06-20 16:40:37 -07002175 }
Jeff Brown87b7f802011-06-21 18:35:45 -07002176 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
2177 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
2178 dispatchTransformedGenericPointerEvent(
2179 eventNoHistory, child); // exit
2180 eventNoHistory.setAction(action);
Jeff Brown10b62902011-06-20 16:40:37 -07002181 }
2182
Jeff Brown87b7f802011-06-21 18:35:45 -07002183 final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next;
2184 firstOldHoverTarget.recycle();
2185 firstOldHoverTarget = nextOldHoverTarget;
Jeff Brown10b62902011-06-20 16:40:37 -07002186 }
2187
Vladislav Kaznacheev5a77c372016-10-10 16:11:15 -07002188 // Send events to the view group itself if no children have handled it and the view group
2189 // itself is not currently being hover-exited.
2190 boolean newHoveredSelf = !handled &&
2191 (action != MotionEvent.ACTION_HOVER_EXIT) && !event.isHoverExitPending();
Jeff Brown10b62902011-06-20 16:40:37 -07002192 if (newHoveredSelf == mHoveredSelf) {
2193 if (newHoveredSelf) {
2194 // Send event to the view group as before.
2195 handled |= super.dispatchHoverEvent(event);
2196 }
2197 } else {
2198 if (mHoveredSelf) {
2199 // Exit the view group.
2200 if (action == MotionEvent.ACTION_HOVER_EXIT) {
2201 // Send the exit as is.
2202 handled |= super.dispatchHoverEvent(event); // exit
2203 } else {
2204 // Synthesize an exit from a move or enter.
2205 // Ignore the result because hover focus is moving to a different view.
2206 if (action == MotionEvent.ACTION_HOVER_MOVE) {
2207 super.dispatchHoverEvent(event); // move
2208 }
2209 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
2210 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT);
2211 super.dispatchHoverEvent(eventNoHistory); // exit
2212 eventNoHistory.setAction(action);
2213 }
2214 mHoveredSelf = false;
2215 }
2216
2217 if (newHoveredSelf) {
2218 // Enter the view group.
2219 if (action == MotionEvent.ACTION_HOVER_ENTER) {
2220 // Send the enter as is.
2221 handled |= super.dispatchHoverEvent(event); // enter
2222 mHoveredSelf = true;
2223 } else if (action == MotionEvent.ACTION_HOVER_MOVE) {
2224 // Synthesize an enter from a move.
2225 eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory);
2226 eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER);
2227 handled |= super.dispatchHoverEvent(eventNoHistory); // enter
2228 eventNoHistory.setAction(action);
2229
2230 handled |= super.dispatchHoverEvent(eventNoHistory); // move
2231 mHoveredSelf = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002232 }
2233 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002234 }
2235
Jeff Browna032cc02011-03-07 16:56:21 -08002236 // Recycle the copy of the event that we made.
2237 if (eventNoHistory != event) {
2238 eventNoHistory.recycle();
2239 }
2240
Jeff Browna032cc02011-03-07 16:56:21 -08002241 // Done.
2242 return handled;
2243 }
2244
Jeff Brown59a422e2012-04-19 15:19:19 -07002245 private void exitHoverTargets() {
2246 if (mHoveredSelf || mFirstHoverTarget != null) {
2247 final long now = SystemClock.uptimeMillis();
2248 MotionEvent event = MotionEvent.obtain(now, now,
2249 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
2250 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2251 dispatchHoverEvent(event);
2252 event.recycle();
2253 }
2254 }
2255
2256 private void cancelHoverTarget(View view) {
2257 HoverTarget predecessor = null;
2258 HoverTarget target = mFirstHoverTarget;
2259 while (target != null) {
2260 final HoverTarget next = target.next;
2261 if (target.child == view) {
2262 if (predecessor == null) {
2263 mFirstHoverTarget = next;
2264 } else {
2265 predecessor.next = next;
2266 }
2267 target.recycle();
2268
2269 final long now = SystemClock.uptimeMillis();
2270 MotionEvent event = MotionEvent.obtain(now, now,
2271 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
2272 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2273 view.dispatchHoverEvent(event);
2274 event.recycle();
2275 return;
2276 }
2277 predecessor = target;
2278 target = next;
2279 }
2280 }
2281
Vladislav Kaznacheevf847ee32016-11-21 14:11:00 -08002282 @Override
2283 boolean dispatchTooltipHoverEvent(MotionEvent event) {
2284 final int action = event.getAction();
2285 switch (action) {
2286 case MotionEvent.ACTION_HOVER_ENTER:
2287 break;
2288
2289 case MotionEvent.ACTION_HOVER_MOVE:
2290 View newTarget = null;
2291
2292 // Check what the child under the pointer says about the tooltip.
2293 final int childrenCount = mChildrenCount;
2294 if (childrenCount != 0) {
2295 final float x = event.getX();
2296 final float y = event.getY();
2297
2298 final ArrayList<View> preorderedList = buildOrderedChildList();
2299 final boolean customOrder = preorderedList == null
2300 && isChildrenDrawingOrderEnabled();
2301 final View[] children = mChildren;
2302 for (int i = childrenCount - 1; i >= 0; i--) {
2303 final int childIndex =
2304 getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
2305 final View child =
2306 getAndVerifyPreorderedView(preorderedList, children, childIndex);
Vladislav Kaznacheev943cf0e2017-01-06 09:52:51 -08002307 if (!canViewReceivePointerEvents(child)
2308 || !isTransformedTouchPointInView(x, y, child, null)) {
2309 continue;
2310 }
2311 if (dispatchTooltipHoverEvent(event, child)) {
2312 newTarget = child;
Vladislav Kaznacheevf847ee32016-11-21 14:11:00 -08002313 break;
2314 }
2315 }
2316 if (preorderedList != null) preorderedList.clear();
2317 }
2318
2319 if (mTooltipHoverTarget != newTarget) {
2320 if (mTooltipHoverTarget != null) {
2321 event.setAction(MotionEvent.ACTION_HOVER_EXIT);
2322 mTooltipHoverTarget.dispatchTooltipHoverEvent(event);
2323 event.setAction(action);
2324 }
2325 mTooltipHoverTarget = newTarget;
2326 }
2327
2328 if (mTooltipHoverTarget != null) {
2329 if (mTooltipHoveredSelf) {
2330 mTooltipHoveredSelf = false;
2331 event.setAction(MotionEvent.ACTION_HOVER_EXIT);
2332 super.dispatchTooltipHoverEvent(event);
2333 event.setAction(action);
2334 }
2335 return true;
2336 }
2337
2338 mTooltipHoveredSelf = super.dispatchTooltipHoverEvent(event);
2339 return mTooltipHoveredSelf;
2340
2341 case MotionEvent.ACTION_HOVER_EXIT:
2342 if (mTooltipHoverTarget != null) {
2343 mTooltipHoverTarget.dispatchTooltipHoverEvent(event);
2344 mTooltipHoverTarget = null;
2345 } else if (mTooltipHoveredSelf) {
2346 super.dispatchTooltipHoverEvent(event);
2347 mTooltipHoveredSelf = false;
2348 }
2349 break;
2350 }
2351 return false;
2352 }
2353
2354 private boolean dispatchTooltipHoverEvent(MotionEvent event, View child) {
2355 final boolean result;
2356 if (!child.hasIdentityMatrix()) {
2357 MotionEvent transformedEvent = getTransformedMotionEvent(event, child);
2358 result = child.dispatchTooltipHoverEvent(transformedEvent);
2359 transformedEvent.recycle();
2360 } else {
2361 final float offsetX = mScrollX - child.mLeft;
2362 final float offsetY = mScrollY - child.mTop;
2363 event.offsetLocation(offsetX, offsetY);
2364 result = child.dispatchTooltipHoverEvent(event);
2365 event.offsetLocation(-offsetX, -offsetY);
2366 }
2367 return result;
2368 }
2369
2370 private void exitTooltipHoverTargets() {
2371 if (mTooltipHoveredSelf || mTooltipHoverTarget != null) {
2372 final long now = SystemClock.uptimeMillis();
2373 MotionEvent event = MotionEvent.obtain(now, now,
2374 MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0);
2375 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2376 dispatchTooltipHoverEvent(event);
2377 event.recycle();
2378 }
2379 }
2380
Jeff Brown87b7f802011-06-21 18:35:45 -07002381 /** @hide */
2382 @Override
2383 protected boolean hasHoveredChild() {
2384 return mFirstHoverTarget != null;
2385 }
2386
Dieter Hsufa1b8de2018-08-22 10:35:58 +08002387 /** @hide */
2388 @Override
2389 protected boolean pointInHoveredChild(MotionEvent event) {
2390 if (mFirstHoverTarget != null) {
2391 return isTransformedTouchPointInView(event.getX(), event.getY(),
2392 mFirstHoverTarget.child, null);
2393 }
2394 return false;
2395 }
2396
Svetoslav Ganov42138042012-03-20 11:51:39 -07002397 @Override
Adam Powellcf392d12015-06-25 14:48:45 -07002398 public void addChildrenForAccessibility(ArrayList<View> outChildren) {
Svetoslav762621c2015-06-01 17:21:59 -07002399 if (getAccessibilityNodeProvider() != null) {
2400 return;
2401 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002402 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
2403 try {
2404 final int childrenCount = children.getChildCount();
2405 for (int i = 0; i < childrenCount; i++) {
2406 View child = children.getChildAt(i);
Svetoslav Ganovc406be92012-05-11 16:12:32 -07002407 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002408 if (child.includeForAccessibility()) {
Adam Powellcf392d12015-06-25 14:48:45 -07002409 outChildren.add(child);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002410 } else {
Adam Powellcf392d12015-06-25 14:48:45 -07002411 child.addChildrenForAccessibility(outChildren);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002412 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002413 }
2414 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002415 } finally {
2416 children.recycle();
Svetoslav Ganov42138042012-03-20 11:51:39 -07002417 }
2418 }
2419
2420 /**
Jeff Brown10b62902011-06-20 16:40:37 -07002421 * Implement this method to intercept hover events before they are handled
2422 * by child views.
2423 * <p>
2424 * This method is called before dispatching a hover event to a child of
2425 * the view group or to the view group's own {@link #onHoverEvent} to allow
2426 * the view group a chance to intercept the hover event.
2427 * This method can also be used to watch all pointer motions that occur within
2428 * the bounds of the view group even when the pointer is hovering over
2429 * a child of the view group rather than over the view group itself.
2430 * </p><p>
2431 * The view group can prevent its children from receiving hover events by
2432 * implementing this method and returning <code>true</code> to indicate
2433 * that it would like to intercept hover events. The view group must
2434 * continuously return <code>true</code> from {@link #onInterceptHoverEvent}
2435 * for as long as it wishes to continue intercepting hover events from
2436 * its children.
2437 * </p><p>
2438 * Interception preserves the invariant that at most one view can be
2439 * hovered at a time by transferring hover focus from the currently hovered
2440 * child to the view group or vice-versa as needed.
2441 * </p><p>
2442 * If this method returns <code>true</code> and a child is already hovered, then the
2443 * child view will first receive a hover exit event and then the view group
2444 * itself will receive a hover enter event in {@link #onHoverEvent}.
2445 * Likewise, if this method had previously returned <code>true</code> to intercept hover
2446 * events and instead returns <code>false</code> while the pointer is hovering
2447 * within the bounds of one of a child, then the view group will first receive a
2448 * hover exit event in {@link #onHoverEvent} and then the hovered child will
2449 * receive a hover enter event.
2450 * </p><p>
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002451 * The default implementation handles mouse hover on the scroll bars.
Jeff Brown10b62902011-06-20 16:40:37 -07002452 * </p>
2453 *
2454 * @param event The motion event that describes the hover.
2455 * @return True if the view group would like to intercept the hover event
2456 * and prevent its children from receiving it.
2457 */
2458 public boolean onInterceptHoverEvent(MotionEvent event) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09002459 if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
2460 final int action = event.getAction();
2461 final float x = event.getX();
2462 final float y = event.getY();
2463 if ((action == MotionEvent.ACTION_HOVER_MOVE
2464 || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) {
2465 return true;
2466 }
2467 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07002468 return false;
2469 }
2470
Jeff Browna032cc02011-03-07 16:56:21 -08002471 private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) {
2472 if (event.getHistorySize() == 0) {
2473 return event;
2474 }
2475 return MotionEvent.obtainNoHistory(event);
2476 }
2477
Jeff Browna032cc02011-03-07 16:56:21 -08002478 @Override
2479 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
2480 // Send the event to the child under the pointer.
2481 final int childrenCount = mChildrenCount;
2482 if (childrenCount != 0) {
Jeff Browna032cc02011-03-07 16:56:21 -08002483 final float x = event.getX();
2484 final float y = event.getY();
2485
Chris Craikab008f02014-05-23 17:55:03 -07002486 final ArrayList<View> preorderedList = buildOrderedChildList();
2487 final boolean customOrder = preorderedList == null
2488 && isChildrenDrawingOrderEnabled();
2489 final View[] children = mChildren;
Jeff Browna032cc02011-03-07 16:56:21 -08002490 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002491 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
2492 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Jeff Browna032cc02011-03-07 16:56:21 -08002493 if (!canViewReceivePointerEvents(child)
2494 || !isTransformedTouchPointInView(x, y, child, null)) {
2495 continue;
2496 }
2497
2498 if (dispatchTransformedGenericPointerEvent(event, child)) {
Chris Craikab008f02014-05-23 17:55:03 -07002499 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002500 return true;
2501 }
2502 }
Chris Craikab008f02014-05-23 17:55:03 -07002503 if (preorderedList != null) preorderedList.clear();
Jeff Browna032cc02011-03-07 16:56:21 -08002504 }
2505
2506 // No child handled the event. Send it to this view group.
2507 return super.dispatchGenericPointerEvent(event);
2508 }
2509
Jeff Browna032cc02011-03-07 16:56:21 -08002510 @Override
2511 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08002512 // Send the event to the focused child or to this view group if it has focus.
Dianne Hackborn4702a852012-08-17 15:18:29 -07002513 if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
2514 == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
Jeff Browna032cc02011-03-07 16:56:21 -08002515 return super.dispatchGenericFocusedEvent(event);
Dianne Hackborn4702a852012-08-17 15:18:29 -07002516 } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
2517 == PFLAG_HAS_BOUNDS) {
Jeff Browncb1404e2011-01-15 18:14:15 -08002518 return mFocused.dispatchGenericMotionEvent(event);
2519 }
2520 return false;
2521 }
2522
2523 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002524 * Dispatches a generic pointer event to a child, taking into account
2525 * transformations that apply to the child.
2526 *
2527 * @param event The event to send.
2528 * @param child The view to send the event to.
2529 * @return {@code true} if the child handled the event.
2530 */
2531 private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) {
Jeff Browna032cc02011-03-07 16:56:21 -08002532 boolean handled;
2533 if (!child.hasIdentityMatrix()) {
Michael Wrighte051f6f2016-05-13 17:44:16 +01002534 MotionEvent transformedEvent = getTransformedMotionEvent(event, child);
Jeff Browna032cc02011-03-07 16:56:21 -08002535 handled = child.dispatchGenericMotionEvent(transformedEvent);
2536 transformedEvent.recycle();
2537 } else {
Michael Wrighte051f6f2016-05-13 17:44:16 +01002538 final float offsetX = mScrollX - child.mLeft;
2539 final float offsetY = mScrollY - child.mTop;
Jeff Browna032cc02011-03-07 16:56:21 -08002540 event.offsetLocation(offsetX, offsetY);
2541 handled = child.dispatchGenericMotionEvent(event);
2542 event.offsetLocation(-offsetX, -offsetY);
2543 }
2544 return handled;
2545 }
2546
Michael Wrighte051f6f2016-05-13 17:44:16 +01002547 /**
2548 * Returns a MotionEvent that's been transformed into the child's local coordinates.
2549 *
2550 * It's the responsibility of the caller to recycle it once they're finished with it.
2551 * @param event The event to transform.
2552 * @param child The view whose coordinate space is to be used.
2553 * @return A copy of the the given MotionEvent, transformed into the given View's coordinate
2554 * space.
2555 */
2556 private MotionEvent getTransformedMotionEvent(MotionEvent event, View child) {
2557 final float offsetX = mScrollX - child.mLeft;
2558 final float offsetY = mScrollY - child.mTop;
2559 final MotionEvent transformedEvent = MotionEvent.obtain(event);
2560 transformedEvent.offsetLocation(offsetX, offsetY);
2561 if (!child.hasIdentityMatrix()) {
2562 transformedEvent.transform(child.getInverseMatrix());
2563 }
2564 return transformedEvent;
2565 }
2566
Jeff Browncb1404e2011-01-15 18:14:15 -08002567 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 public boolean dispatchTouchEvent(MotionEvent ev) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08002569 if (mInputEventConsistencyVerifier != null) {
2570 mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
2571 }
2572
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002573 // If the event targets the accessibility focused view and this is it, start
2574 // normal event dispatch. Maybe a descendant is what will handle the click.
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002575 if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
Svet Ganov0a2ccee2015-02-06 10:12:32 -08002576 ev.setTargetAccessibilityFocus(false);
2577 }
2578
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002579 boolean handled = false;
2580 if (onFilterTouchEventForSecurity(ev)) {
2581 final int action = ev.getAction();
2582 final int actionMasked = action & MotionEvent.ACTION_MASK;
Jeff Brown85a31762010-09-01 17:01:00 -07002583
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002584 // Handle an initial down.
2585 if (actionMasked == MotionEvent.ACTION_DOWN) {
2586 // Throw away all previous state when starting a new touch gesture.
2587 // The framework may have dropped the up or cancel event for the previous gesture
2588 // due to an app switch, ANR, or some other state change.
2589 cancelAndClearTouchTargets(ev);
2590 resetTouchState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 }
Adam Powellb08013c2010-09-16 16:28:11 -07002592
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002593 // Check for interception.
2594 final boolean intercepted;
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002595 if (actionMasked == MotionEvent.ACTION_DOWN
2596 || mFirstTouchTarget != null) {
2597 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
2598 if (!disallowIntercept) {
2599 intercepted = onInterceptTouchEvent(ev);
2600 ev.setAction(action); // restore action in case it was changed
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002601 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002602 intercepted = false;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002603 }
2604 } else {
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002605 // There are no touch targets and this action is not an initial down
2606 // so this view group continues to intercept touches.
2607 intercepted = true;
2608 }
2609
2610 // If intercepted, start normal event dispatch. Also if there is already
2611 // a view that is handling the gesture, do normal event dispatch.
2612 if (intercepted || mFirstTouchTarget != null) {
2613 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002614 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002615
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002616 // Check for cancelation.
2617 final boolean canceled = resetCancelNextUpFlag(this)
2618 || actionMasked == MotionEvent.ACTION_CANCEL;
Jeff Brown20e987b2010-08-23 12:01:02 -07002619
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002620 // Update list of touch targets for pointer down, if needed.
2621 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
2622 TouchTarget newTouchTarget = null;
2623 boolean alreadyDispatchedToNewTouchTarget = false;
2624 if (!canceled && !intercepted) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002625
Phil Weaver1e6ecc62017-11-07 15:28:21 -08002626 // If the event is targeting accessibility focus we give it to the
Svetoslavc73cfa02015-02-09 17:14:28 -08002627 // view that has accessibility focus and if it does not handle it
2628 // we clear the flag and dispatch the event to all children as usual.
2629 // We are looking up the accessibility focused host to avoid keeping
2630 // state since these events are very rare.
2631 View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
2632 ? findChildWithAccessibilityFocus() : null;
2633
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002634 if (actionMasked == MotionEvent.ACTION_DOWN
2635 || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
Svet Ganovf33fe1f2015-02-06 19:23:31 -08002636 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002637 final int actionIndex = ev.getActionIndex(); // always 0 for down
2638 final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
2639 : TouchTarget.ALL_POINTER_IDS;
Jeff Brown20e987b2010-08-23 12:01:02 -07002640
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002641 // Clean up earlier touch targets for this pointer id in case they
2642 // have become out of sync.
2643 removePointersFromTouchTargets(idBitsToAssign);
2644
2645 final int childrenCount = mChildrenCount;
Chet Haase9c17fe62013-03-22 17:05:55 -07002646 if (newTouchTarget == null && childrenCount != 0) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07002647 final float x = ev.getX(actionIndex);
2648 final float y = ev.getY(actionIndex);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002649 // Find a child that can receive the event.
2650 // Scan children from front to back.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002651 final ArrayList<View> preorderedList = buildTouchDispatchChildList();
Chris Craikab008f02014-05-23 17:55:03 -07002652 final boolean customOrder = preorderedList == null
2653 && isChildrenDrawingOrderEnabled();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002654 final View[] children = mChildren;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002655 for (int i = childrenCount - 1; i >= 0; i--) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05002656 final int childIndex = getAndVerifyPreorderedIndex(
2657 childrenCount, i, customOrder);
2658 final View child = getAndVerifyPreorderedView(
2659 preorderedList, children, childIndex);
Svetoslavc73cfa02015-02-09 17:14:28 -08002660
2661 // If there is a view that has accessibility focus we want it
2662 // to get the event first and if not handled we will perform a
2663 // normal dispatch. We may do a double iteration but this is
2664 // safer given the timeframe.
2665 if (childWithAccessibilityFocus != null) {
2666 if (childWithAccessibilityFocus != child) {
2667 continue;
2668 }
2669 childWithAccessibilityFocus = null;
2670 i = childrenCount - 1;
2671 }
2672
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002673 if (!canViewReceivePointerEvents(child)
2674 || !isTransformedTouchPointInView(x, y, child, null)) {
Svetoslavc73cfa02015-02-09 17:14:28 -08002675 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002676 continue;
2677 }
2678
2679 newTouchTarget = getTouchTarget(child);
2680 if (newTouchTarget != null) {
2681 // Child is already receiving touch within its bounds.
2682 // Give it the new pointer in addition to the ones it is handling.
2683 newTouchTarget.pointerIdBits |= idBitsToAssign;
2684 break;
2685 }
2686
2687 resetCancelNextUpFlag(child);
2688 if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
2689 // Child wants to receive touch within its bounds.
2690 mLastTouchDownTime = ev.getDownTime();
Chris Craikab008f02014-05-23 17:55:03 -07002691 if (preorderedList != null) {
2692 // childIndex points into presorted list, find original index
2693 for (int j = 0; j < childrenCount; j++) {
2694 if (children[childIndex] == mChildren[j]) {
2695 mLastTouchDownIndex = j;
2696 break;
2697 }
2698 }
2699 } else {
2700 mLastTouchDownIndex = childIndex;
2701 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002702 mLastTouchDownX = ev.getX();
2703 mLastTouchDownY = ev.getY();
2704 newTouchTarget = addTouchTarget(child, idBitsToAssign);
2705 alreadyDispatchedToNewTouchTarget = true;
2706 break;
2707 }
Svetoslavc73cfa02015-02-09 17:14:28 -08002708
2709 // The accessibility focus didn't handle the event, so clear
2710 // the flag and do a normal dispatch to all children.
2711 ev.setTargetAccessibilityFocus(false);
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002712 }
Chris Craikab008f02014-05-23 17:55:03 -07002713 if (preorderedList != null) preorderedList.clear();
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002714 }
2715
2716 if (newTouchTarget == null && mFirstTouchTarget != null) {
2717 // Did not find a child to receive the event.
2718 // Assign the pointer to the least recently added target.
2719 newTouchTarget = mFirstTouchTarget;
2720 while (newTouchTarget.next != null) {
2721 newTouchTarget = newTouchTarget.next;
2722 }
2723 newTouchTarget.pointerIdBits |= idBitsToAssign;
2724 }
2725 }
2726 }
2727
2728 // Dispatch to touch targets.
2729 if (mFirstTouchTarget == null) {
2730 // No touch targets so treat this as an ordinary view.
2731 handled = dispatchTransformedTouchEvent(ev, canceled, null,
2732 TouchTarget.ALL_POINTER_IDS);
2733 } else {
2734 // Dispatch to touch targets, excluding the new touch target if we already
2735 // dispatched to it. Cancel touch targets if necessary.
2736 TouchTarget predecessor = null;
2737 TouchTarget target = mFirstTouchTarget;
2738 while (target != null) {
2739 final TouchTarget next = target.next;
2740 if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
2741 handled = true;
2742 } else {
2743 final boolean cancelChild = resetCancelNextUpFlag(target.child)
Chet Haase9c17fe62013-03-22 17:05:55 -07002744 || intercepted;
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002745 if (dispatchTransformedTouchEvent(ev, cancelChild,
2746 target.child, target.pointerIdBits)) {
2747 handled = true;
2748 }
2749 if (cancelChild) {
2750 if (predecessor == null) {
2751 mFirstTouchTarget = next;
2752 } else {
2753 predecessor.next = next;
2754 }
2755 target.recycle();
2756 target = next;
Jeff Brown20e987b2010-08-23 12:01:02 -07002757 continue;
2758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002760 predecessor = target;
2761 target = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002763 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002764
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002765 // Update list of touch targets for pointer up or cancel, if needed.
2766 if (canceled
2767 || actionMasked == MotionEvent.ACTION_UP
2768 || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
2769 resetTouchState();
2770 } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
2771 final int actionIndex = ev.getActionIndex();
2772 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
2773 removePointersFromTouchTargets(idBitsToRemove);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 }
2775 }
Romain Guy8506ab42009-06-11 17:35:47 -07002776
Jeff Brownbbdc50b2011-04-19 23:46:52 -07002777 if (!handled && mInputEventConsistencyVerifier != null) {
2778 mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
Jeff Brown20e987b2010-08-23 12:01:02 -07002779 }
Jeff Brown20e987b2010-08-23 12:01:02 -07002780 return handled;
2781 }
2782
Romain Guy469b1db2010-10-05 11:49:57 -07002783 /**
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08002784 * Provide custom ordering of views in which the touch will be dispatched.
2785 *
2786 * This is called within a tight loop, so you are not allowed to allocate objects, including
2787 * the return array. Instead, you should return a pre-allocated list that will be cleared
2788 * after the dispatch is finished.
2789 * @hide
2790 */
2791 public ArrayList<View> buildTouchDispatchChildList() {
2792 return buildOrderedChildList();
2793 }
2794
2795 /**
Svetoslavc73cfa02015-02-09 17:14:28 -08002796 * Finds the child which has accessibility focus.
2797 *
2798 * @return The child that has focus.
2799 */
2800 private View findChildWithAccessibilityFocus() {
2801 ViewRootImpl viewRoot = getViewRootImpl();
2802 if (viewRoot == null) {
2803 return null;
2804 }
2805
2806 View current = viewRoot.getAccessibilityFocusedHost();
2807 if (current == null) {
2808 return null;
2809 }
2810
2811 ViewParent parent = current.getParent();
2812 while (parent instanceof View) {
2813 if (parent == this) {
2814 return current;
2815 }
2816 current = (View) parent;
2817 parent = current.getParent();
2818 }
2819
2820 return null;
2821 }
2822
2823 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002824 * Resets all touch state in preparation for a new cycle.
2825 */
2826 private void resetTouchState() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002827 clearTouchTargets();
2828 resetCancelNextUpFlag(this);
2829 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
Adam Powell10ba2772014-04-15 09:46:51 -07002830 mNestedScrollAxes = SCROLL_AXIS_NONE;
Jeff Brown20e987b2010-08-23 12:01:02 -07002831 }
2832
Romain Guy469b1db2010-10-05 11:49:57 -07002833 /**
2834 * Resets the cancel next up flag.
2835 * Returns true if the flag was previously set.
2836 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002837 private static boolean resetCancelNextUpFlag(@NonNull View view) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002838 if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) {
2839 view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Jeff Brown20e987b2010-08-23 12:01:02 -07002840 return true;
Adam Cohen9b073942010-08-19 16:49:52 -07002841 }
2842 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 }
2844
Romain Guy469b1db2010-10-05 11:49:57 -07002845 /**
2846 * Clears all touch targets.
2847 */
2848 private void clearTouchTargets() {
Jeff Brown20e987b2010-08-23 12:01:02 -07002849 TouchTarget target = mFirstTouchTarget;
2850 if (target != null) {
2851 do {
2852 TouchTarget next = target.next;
2853 target.recycle();
2854 target = next;
2855 } while (target != null);
2856 mFirstTouchTarget = null;
2857 }
2858 }
2859
Romain Guy469b1db2010-10-05 11:49:57 -07002860 /**
2861 * Cancels and clears all touch targets.
2862 */
2863 private void cancelAndClearTouchTargets(MotionEvent event) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002864 if (mFirstTouchTarget != null) {
2865 boolean syntheticEvent = false;
2866 if (event == null) {
2867 final long now = SystemClock.uptimeMillis();
2868 event = MotionEvent.obtain(now, now,
2869 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
Jeff Brown2fdbc5a2011-06-30 12:25:54 -07002870 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Jeff Brown20e987b2010-08-23 12:01:02 -07002871 syntheticEvent = true;
2872 }
2873
2874 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2875 resetCancelNextUpFlag(target.child);
2876 dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
2877 }
2878 clearTouchTargets();
2879
2880 if (syntheticEvent) {
2881 event.recycle();
2882 }
2883 }
2884 }
2885
Romain Guy469b1db2010-10-05 11:49:57 -07002886 /**
2887 * Gets the touch target for specified child view.
2888 * Returns null if not found.
2889 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002890 private TouchTarget getTouchTarget(@NonNull View child) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002891 for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
2892 if (target.child == child) {
2893 return target;
2894 }
2895 }
2896 return null;
2897 }
2898
Romain Guy469b1db2010-10-05 11:49:57 -07002899 /**
2900 * Adds a touch target for specified child to the beginning of the list.
2901 * Assumes the target child is not already present.
2902 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002903 private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
2904 final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
Jeff Brown20e987b2010-08-23 12:01:02 -07002905 target.next = mFirstTouchTarget;
2906 mFirstTouchTarget = target;
2907 return target;
2908 }
2909
Romain Guy469b1db2010-10-05 11:49:57 -07002910 /**
2911 * Removes the pointer ids from consideration.
2912 */
2913 private void removePointersFromTouchTargets(int pointerIdBits) {
Jeff Brown20e987b2010-08-23 12:01:02 -07002914 TouchTarget predecessor = null;
2915 TouchTarget target = mFirstTouchTarget;
2916 while (target != null) {
2917 final TouchTarget next = target.next;
2918 if ((target.pointerIdBits & pointerIdBits) != 0) {
2919 target.pointerIdBits &= ~pointerIdBits;
2920 if (target.pointerIdBits == 0) {
2921 if (predecessor == null) {
2922 mFirstTouchTarget = next;
2923 } else {
2924 predecessor.next = next;
2925 }
2926 target.recycle();
2927 target = next;
2928 continue;
2929 }
2930 }
2931 predecessor = target;
2932 target = next;
2933 }
2934 }
2935
Mathew Inwooda570dee2018-08-17 14:56:00 +01002936 @UnsupportedAppUsage
Jeff Brown59a422e2012-04-19 15:19:19 -07002937 private void cancelTouchTarget(View view) {
2938 TouchTarget predecessor = null;
2939 TouchTarget target = mFirstTouchTarget;
2940 while (target != null) {
2941 final TouchTarget next = target.next;
2942 if (target.child == view) {
2943 if (predecessor == null) {
2944 mFirstTouchTarget = next;
2945 } else {
2946 predecessor.next = next;
2947 }
2948 target.recycle();
2949
2950 final long now = SystemClock.uptimeMillis();
2951 MotionEvent event = MotionEvent.obtain(now, now,
2952 MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
2953 event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
2954 view.dispatchTouchEvent(event);
2955 event.recycle();
2956 return;
2957 }
2958 predecessor = target;
2959 target = next;
2960 }
2961 }
2962
Romain Guy469b1db2010-10-05 11:49:57 -07002963 /**
Jeff Browna032cc02011-03-07 16:56:21 -08002964 * Returns true if a child view can receive pointer events.
2965 * @hide
2966 */
Alan Viverettea7b85e62016-01-22 10:14:02 -05002967 private static boolean canViewReceivePointerEvents(@NonNull View child) {
Jeff Browna032cc02011-03-07 16:56:21 -08002968 return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
2969 || child.getAnimation() != null;
2970 }
2971
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002972 private float[] getTempPoint() {
2973 if (mTempPoint == null) {
2974 mTempPoint = new float[2];
2975 }
2976 return mTempPoint;
2977 }
2978
Jeff Browna032cc02011-03-07 16:56:21 -08002979 /**
Romain Guy469b1db2010-10-05 11:49:57 -07002980 * Returns true if a child view contains the specified point when transformed
Jeff Brown20e987b2010-08-23 12:01:02 -07002981 * into its coordinate space.
Romain Guy469b1db2010-10-05 11:49:57 -07002982 * Child must not be null.
Adam Cohena32edd42010-10-26 10:35:01 -07002983 * @hide
Romain Guy469b1db2010-10-05 11:49:57 -07002984 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01002985 @UnsupportedAppUsage
Adam Cohena32edd42010-10-26 10:35:01 -07002986 protected boolean isTransformedTouchPointInView(float x, float y, View child,
Christopher Tate2c095f32010-10-04 14:13:40 -07002987 PointF outLocalPoint) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002988 final float[] point = getTempPoint();
2989 point[0] = x;
2990 point[1] = y;
2991 transformPointToViewLocal(point, child);
2992 final boolean isInView = child.pointInView(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002993 if (isInView && outLocalPoint != null) {
Alan Viveretteb942b6f2014-12-08 10:37:39 -08002994 outLocalPoint.set(point[0], point[1]);
Christopher Tate2c095f32010-10-04 14:13:40 -07002995 }
2996 return isInView;
Adam Powell2b342f02010-08-18 18:14:13 -07002997 }
2998
Romain Guy469b1db2010-10-05 11:49:57 -07002999 /**
Alan Viveretteb942b6f2014-12-08 10:37:39 -08003000 * @hide
3001 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01003002 @UnsupportedAppUsage
Alan Viveretteb942b6f2014-12-08 10:37:39 -08003003 public void transformPointToViewLocal(float[] point, View child) {
3004 point[0] += mScrollX - child.mLeft;
3005 point[1] += mScrollY - child.mTop;
3006
3007 if (!child.hasIdentityMatrix()) {
3008 child.getInverseMatrix().mapPoints(point);
3009 }
3010 }
3011
3012 /**
Romain Guy469b1db2010-10-05 11:49:57 -07003013 * Transforms a motion event into the coordinate space of a particular child view,
Jeff Brown20e987b2010-08-23 12:01:02 -07003014 * filters out irrelevant pointer ids, and overrides its action if necessary.
Romain Guy469b1db2010-10-05 11:49:57 -07003015 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
3016 */
3017 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
Jeff Brown20e987b2010-08-23 12:01:02 -07003018 View child, int desiredPointerIdBits) {
3019 final boolean handled;
Adam Powell2b342f02010-08-18 18:14:13 -07003020
Jeff Brown20e987b2010-08-23 12:01:02 -07003021 // Canceling motions is a special case. We don't need to perform any transformations
3022 // or filtering. The important part is the action, not the contents.
3023 final int oldAction = event.getAction();
3024 if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
3025 event.setAction(MotionEvent.ACTION_CANCEL);
3026 if (child == null) {
3027 handled = super.dispatchTouchEvent(event);
3028 } else {
3029 handled = child.dispatchTouchEvent(event);
3030 }
3031 event.setAction(oldAction);
3032 return handled;
3033 }
Adam Powell2b342f02010-08-18 18:14:13 -07003034
Jeff Brown20e987b2010-08-23 12:01:02 -07003035 // Calculate the number of pointers to deliver.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003036 final int oldPointerIdBits = event.getPointerIdBits();
3037 final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
Adam Powell2b342f02010-08-18 18:14:13 -07003038
Jeff Brown20e987b2010-08-23 12:01:02 -07003039 // If for some reason we ended up in an inconsistent state where it looks like we
3040 // might produce a motion event with no pointers in it, then drop the event.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003041 if (newPointerIdBits == 0) {
Jeff Brown20e987b2010-08-23 12:01:02 -07003042 return false;
3043 }
Adam Powell2b342f02010-08-18 18:14:13 -07003044
Jeff Brown20e987b2010-08-23 12:01:02 -07003045 // If the number of pointers is the same and we don't need to perform any fancy
3046 // irreversible transformations, then we can reuse the motion event for this
3047 // dispatch as long as we are careful to revert any changes we make.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003048 // Otherwise we need to make a copy.
3049 final MotionEvent transformedEvent;
3050 if (newPointerIdBits == oldPointerIdBits) {
3051 if (child == null || child.hasIdentityMatrix()) {
3052 if (child == null) {
3053 handled = super.dispatchTouchEvent(event);
3054 } else {
3055 final float offsetX = mScrollX - child.mLeft;
3056 final float offsetY = mScrollY - child.mTop;
3057 event.offsetLocation(offsetX, offsetY);
Adam Powell2b342f02010-08-18 18:14:13 -07003058
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003059 handled = child.dispatchTouchEvent(event);
Jeff Brown20e987b2010-08-23 12:01:02 -07003060
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003061 event.offsetLocation(-offsetX, -offsetY);
3062 }
3063 return handled;
Jeff Brown20e987b2010-08-23 12:01:02 -07003064 }
Jeff Brown20e987b2010-08-23 12:01:02 -07003065 transformedEvent = MotionEvent.obtain(event);
3066 } else {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003067 transformedEvent = event.split(newPointerIdBits);
Adam Powell2b342f02010-08-18 18:14:13 -07003068 }
3069
Jeff Brown20e987b2010-08-23 12:01:02 -07003070 // Perform any necessary transformations and dispatch.
3071 if (child == null) {
3072 handled = super.dispatchTouchEvent(transformedEvent);
3073 } else {
3074 final float offsetX = mScrollX - child.mLeft;
3075 final float offsetY = mScrollY - child.mTop;
3076 transformedEvent.offsetLocation(offsetX, offsetY);
3077 if (! child.hasIdentityMatrix()) {
3078 transformedEvent.transform(child.getInverseMatrix());
Adam Powell2b342f02010-08-18 18:14:13 -07003079 }
3080
Jeff Brown20e987b2010-08-23 12:01:02 -07003081 handled = child.dispatchTouchEvent(transformedEvent);
Adam Powell2b342f02010-08-18 18:14:13 -07003082 }
3083
Jeff Brown20e987b2010-08-23 12:01:02 -07003084 // Done.
3085 transformedEvent.recycle();
Adam Powell2b342f02010-08-18 18:14:13 -07003086 return handled;
3087 }
3088
Romain Guy469b1db2010-10-05 11:49:57 -07003089 /**
Adam Powell2b342f02010-08-18 18:14:13 -07003090 * Enable or disable the splitting of MotionEvents to multiple children during touch event
Jeff Brown995e7742010-12-22 16:59:36 -08003091 * dispatch. This behavior is enabled by default for applications that target an
3092 * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer.
Adam Powell2b342f02010-08-18 18:14:13 -07003093 *
3094 * <p>When this option is enabled MotionEvents may be split and dispatched to different child
3095 * views depending on where each pointer initially went down. This allows for user interactions
3096 * such as scrolling two panes of content independently, chording of buttons, and performing
3097 * independent gestures on different pieces of content.
3098 *
3099 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
3100 * child views. <code>false</code> to only allow one child view to be the target of
3101 * any MotionEvent received by this ViewGroup.
Scott Main27a85082013-06-10 10:39:48 -07003102 * @attr ref android.R.styleable#ViewGroup_splitMotionEvents
Adam Powell2b342f02010-08-18 18:14:13 -07003103 */
3104 public void setMotionEventSplittingEnabled(boolean split) {
3105 // TODO Applications really shouldn't change this setting mid-touch event,
3106 // but perhaps this should handle that case and send ACTION_CANCELs to any child views
3107 // with gestures in progress when this is changed.
3108 if (split) {
Adam Powell2b342f02010-08-18 18:14:13 -07003109 mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
3110 } else {
3111 mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
Adam Powell2b342f02010-08-18 18:14:13 -07003112 }
3113 }
3114
3115 /**
Jeff Brown995e7742010-12-22 16:59:36 -08003116 * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Adam Powell2b342f02010-08-18 18:14:13 -07003117 * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
3118 */
3119 public boolean isMotionEventSplittingEnabled() {
3120 return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS;
3121 }
3122
3123 /**
George Mount0a778ed2013-12-13 13:35:36 -08003124 * Returns true if this ViewGroup should be considered as a single entity for removal
3125 * when executing an Activity transition. If this is false, child elements will move
3126 * individually during the transition.
George Mount427c6412014-11-05 16:45:36 -08003127 *
George Mount0a778ed2013-12-13 13:35:36 -08003128 * @return True if the ViewGroup should be acted on together during an Activity transition.
George Mount427c6412014-11-05 16:45:36 -08003129 * The default value is true when there is a non-null background or if
3130 * {@link #getTransitionName()} is not null or if a
3131 * non-null {@link android.view.ViewOutlineProvider} other than
3132 * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to
3133 * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise.
George Mount0a778ed2013-12-13 13:35:36 -08003134 */
3135 public boolean isTransitionGroup() {
3136 if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) {
3137 return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0);
3138 } else {
George Mount427c6412014-11-05 16:45:36 -08003139 final ViewOutlineProvider outlineProvider = getOutlineProvider();
3140 return getBackground() != null || getTransitionName() != null ||
3141 (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND);
George Mount0a778ed2013-12-13 13:35:36 -08003142 }
3143 }
3144
3145 /**
3146 * Changes whether or not this ViewGroup should be treated as a single entity during
George Mount31a21722014-03-24 17:44:36 -07003147 * Activity Transitions.
George Mount0a778ed2013-12-13 13:35:36 -08003148 * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
3149 * in Activity transitions. If false, the ViewGroup won't transition,
3150 * only its children. If true, the entire ViewGroup will transition
3151 * together.
George Mount62ab9b72014-05-02 13:51:17 -07003152 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity,
3153 * android.util.Pair[])
George Mount0a778ed2013-12-13 13:35:36 -08003154 */
3155 public void setTransitionGroup(boolean isTransitionGroup) {
3156 mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET;
3157 if (isTransitionGroup) {
3158 mGroupFlags |= FLAG_IS_TRANSITION_GROUP;
3159 } else {
3160 mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP;
3161 }
3162 }
3163
Alan Viverettebe463f22016-01-21 10:50:10 -05003164 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Romain Guy8506ab42009-06-11 17:35:47 -07003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
3168 // We're already in this state, assume our ancestors are too
3169 return;
3170 }
Romain Guy8506ab42009-06-11 17:35:47 -07003171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 if (disallowIntercept) {
3173 mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
3174 } else {
3175 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
3176 }
Romain Guy8506ab42009-06-11 17:35:47 -07003177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 // Pass it up to our parent
3179 if (mParent != null) {
3180 mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
3181 }
3182 }
3183
3184 /**
3185 * Implement this method to intercept all touch screen motion events. This
3186 * allows you to watch events as they are dispatched to your children, and
3187 * take ownership of the current gesture at any point.
3188 *
3189 * <p>Using this function takes some care, as it has a fairly complicated
3190 * interaction with {@link View#onTouchEvent(MotionEvent)
3191 * View.onTouchEvent(MotionEvent)}, and using it requires implementing
3192 * that method as well as this one in the correct way. Events will be
3193 * received in the following order:
3194 *
3195 * <ol>
3196 * <li> You will receive the down event here.
3197 * <li> The down event will be handled either by a child of this view
3198 * group, or given to your own onTouchEvent() method to handle; this means
3199 * you should implement onTouchEvent() to return true, so you will
3200 * continue to see the rest of the gesture (instead of looking for
3201 * a parent view to handle it). Also, by returning true from
3202 * onTouchEvent(), you will not receive any following
3203 * events in onInterceptTouchEvent() and all touch processing must
3204 * happen in onTouchEvent() like normal.
3205 * <li> For as long as you return false from this function, each following
3206 * event (up to and including the final up) will be delivered first here
3207 * and then to the target's onTouchEvent().
3208 * <li> If you return true from here, you will not receive any
3209 * following events: the target view will receive the same event but
3210 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further
3211 * events will be delivered to your onTouchEvent() method and no longer
3212 * appear here.
3213 * </ol>
3214 *
3215 * @param ev The motion event being dispatched down the hierarchy.
3216 * @return Return true to steal motion events from the children and have
3217 * them dispatched to this ViewGroup through onTouchEvent().
3218 * The current target will receive an ACTION_CANCEL event, and no further
3219 * messages will be delivered here.
3220 */
3221 public boolean onInterceptTouchEvent(MotionEvent ev) {
Keisuke Kuroyanagid85bc502016-01-21 14:50:38 +09003222 if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
3223 && ev.getAction() == MotionEvent.ACTION_DOWN
3224 && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
3225 && isOnScrollbarThumb(ev.getX(), ev.getY())) {
3226 return true;
3227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 return false;
3229 }
3230
3231 /**
3232 * {@inheritDoc}
3233 *
3234 * Looks for a view to give focus to respecting the setting specified by
3235 * {@link #getDescendantFocusability()}.
3236 *
3237 * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
3238 * find focus within the children of this group when appropriate.
3239 *
3240 * @see #FOCUS_BEFORE_DESCENDANTS
3241 * @see #FOCUS_AFTER_DESCENDANTS
3242 * @see #FOCUS_BLOCK_DESCENDANTS
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08003243 * @see #onRequestFocusInDescendants(int, android.graphics.Rect)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 */
3245 @Override
3246 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
3247 if (DBG) {
3248 System.out.println(this + " ViewGroup.requestFocus direction="
3249 + direction);
3250 }
3251 int descendantFocusability = getDescendantFocusability();
3252
Evan Rosky5db64eb2017-11-20 23:04:46 +00003253 boolean result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 switch (descendantFocusability) {
3255 case FOCUS_BLOCK_DESCENDANTS:
Evan Rosky5db64eb2017-11-20 23:04:46 +00003256 result = super.requestFocus(direction, previouslyFocusedRect);
3257 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 case FOCUS_BEFORE_DESCENDANTS: {
3259 final boolean took = super.requestFocus(direction, previouslyFocusedRect);
Evan Rosky5db64eb2017-11-20 23:04:46 +00003260 result = took ? took : onRequestFocusInDescendants(direction,
3261 previouslyFocusedRect);
3262 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 }
3264 case FOCUS_AFTER_DESCENDANTS: {
3265 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
Evan Rosky5db64eb2017-11-20 23:04:46 +00003266 result = took ? took : super.requestFocus(direction, previouslyFocusedRect);
3267 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 }
3269 default:
3270 throw new IllegalStateException("descendant focusability must be "
3271 + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
3272 + "but is " + descendantFocusability);
3273 }
Evan Rosky5db64eb2017-11-20 23:04:46 +00003274 if (result && !isLayoutValid() && ((mPrivateFlags & PFLAG_WANTS_FOCUS) == 0)) {
3275 mPrivateFlags |= PFLAG_WANTS_FOCUS;
3276 }
3277 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 }
3279
3280 /**
3281 * Look for a descendant to call {@link View#requestFocus} on.
3282 * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)}
3283 * when it wants to request focus within its children. Override this to
3284 * customize how your {@link ViewGroup} requests focus within its children.
3285 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3286 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
3287 * to give a finer grained hint about where focus is coming from. May be null
3288 * if there is no hint.
3289 * @return Whether focus was taken.
3290 */
3291 @SuppressWarnings({"ConstantConditions"})
3292 protected boolean onRequestFocusInDescendants(int direction,
3293 Rect previouslyFocusedRect) {
3294 int index;
3295 int increment;
3296 int end;
3297 int count = mChildrenCount;
3298 if ((direction & FOCUS_FORWARD) != 0) {
3299 index = 0;
3300 increment = 1;
3301 end = count;
3302 } else {
3303 index = count - 1;
3304 increment = -1;
3305 end = -1;
3306 }
3307 final View[] children = mChildren;
3308 for (int i = index; i != end; i += increment) {
3309 View child = children[i];
Vadim Tryshevb5ced222017-01-17 19:31:35 -08003310 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 if (child.requestFocus(direction, previouslyFocusedRect)) {
3312 return true;
3313 }
3314 }
3315 }
3316 return false;
3317 }
Chet Haase5c13d892010-10-08 08:37:55 -07003318
Vadim Tryshev01d8c492016-12-15 11:33:15 -08003319 @Override
Evan Rosky53fcf112017-01-26 14:37:55 -08003320 public boolean restoreDefaultFocus() {
3321 if (mDefaultFocus != null
Vadim Tryshev01d8c492016-12-15 11:33:15 -08003322 && getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS
Vadim Tryshev5ca73982017-01-04 17:24:43 -08003323 && (mDefaultFocus.mViewFlags & VISIBILITY_MASK) == VISIBLE
Evan Rosky53fcf112017-01-26 14:37:55 -08003324 && mDefaultFocus.restoreDefaultFocus()) {
Vadim Tryshev01d8c492016-12-15 11:33:15 -08003325 return true;
3326 }
Evan Rosky53fcf112017-01-26 14:37:55 -08003327 return super.restoreDefaultFocus();
3328 }
3329
3330 /**
3331 * @hide
3332 */
Evan Rosky3ac64632017-02-13 18:04:43 -08003333 @TestApi
Evan Rosky53fcf112017-01-26 14:37:55 -08003334 @Override
3335 public boolean restoreFocusInCluster(@FocusRealDirection int direction) {
Evan Rosky18b886e2017-02-15 13:26:51 -08003336 // Allow cluster-navigation to enter touchscreenBlocksFocus ViewGroups.
3337 if (isKeyboardNavigationCluster()) {
3338 final boolean blockedFocus = getTouchscreenBlocksFocus();
3339 try {
3340 setTouchscreenBlocksFocusNoRefocus(false);
3341 return restoreFocusInClusterInternal(direction);
3342 } finally {
3343 setTouchscreenBlocksFocusNoRefocus(blockedFocus);
3344 }
3345 } else {
3346 return restoreFocusInClusterInternal(direction);
3347 }
3348 }
3349
3350 private boolean restoreFocusInClusterInternal(@FocusRealDirection int direction) {
Evan Rosky0e8a6832017-04-10 12:35:15 -07003351 if (mFocusedInCluster != null && getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS
Evan Rosky53fcf112017-01-26 14:37:55 -08003352 && (mFocusedInCluster.mViewFlags & VISIBILITY_MASK) == VISIBLE
3353 && mFocusedInCluster.restoreFocusInCluster(direction)) {
3354 return true;
3355 }
3356 return super.restoreFocusInCluster(direction);
Vadim Tryshev01d8c492016-12-15 11:33:15 -08003357 }
3358
Romain Guya440b002010-02-24 15:57:54 -08003359 /**
Evan Rosky3ac64632017-02-13 18:04:43 -08003360 * @hide
3361 */
3362 @Override
3363 public boolean restoreFocusNotInCluster() {
3364 if (mFocusedInCluster != null) {
3365 // since clusters don't nest; we can assume that a non-null mFocusedInCluster
3366 // will refer to a view not-in a cluster.
3367 return restoreFocusInCluster(View.FOCUS_DOWN);
3368 }
Evan Rosky2ae1bf52017-05-11 11:18:45 -07003369 if (isKeyboardNavigationCluster() || (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
Evan Rosky3ac64632017-02-13 18:04:43 -08003370 return false;
3371 }
3372 int descendentFocusability = getDescendantFocusability();
3373 if (descendentFocusability == FOCUS_BLOCK_DESCENDANTS) {
3374 return super.requestFocus(FOCUS_DOWN, null);
3375 }
3376 if (descendentFocusability == FOCUS_BEFORE_DESCENDANTS
3377 && super.requestFocus(FOCUS_DOWN, null)) {
3378 return true;
3379 }
3380 for (int i = 0; i < mChildrenCount; ++i) {
3381 View child = mChildren[i];
3382 if (!child.isKeyboardNavigationCluster()
3383 && child.restoreFocusNotInCluster()) {
3384 return true;
3385 }
3386 }
Evan Rosky2ae1bf52017-05-11 11:18:45 -07003387 if (descendentFocusability == FOCUS_AFTER_DESCENDANTS && !hasFocusableChild(false)) {
Evan Rosky3ac64632017-02-13 18:04:43 -08003388 return super.requestFocus(FOCUS_DOWN, null);
3389 }
3390 return false;
3391 }
3392
3393 /**
Romain Guya440b002010-02-24 15:57:54 -08003394 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07003395 *
Romain Guydcc490f2010-02-24 17:59:35 -08003396 * @hide
Romain Guya440b002010-02-24 15:57:54 -08003397 */
3398 @Override
3399 public void dispatchStartTemporaryDetach() {
3400 super.dispatchStartTemporaryDetach();
3401 final int count = mChildrenCount;
3402 final View[] children = mChildren;
3403 for (int i = 0; i < count; i++) {
3404 children[i].dispatchStartTemporaryDetach();
3405 }
3406 }
Chet Haase5c13d892010-10-08 08:37:55 -07003407
Romain Guya440b002010-02-24 15:57:54 -08003408 /**
3409 * {@inheritDoc}
Chet Haase5c13d892010-10-08 08:37:55 -07003410 *
Romain Guydcc490f2010-02-24 17:59:35 -08003411 * @hide
Romain Guya440b002010-02-24 15:57:54 -08003412 */
3413 @Override
3414 public void dispatchFinishTemporaryDetach() {
3415 super.dispatchFinishTemporaryDetach();
3416 final int count = mChildrenCount;
3417 final View[] children = mChildren;
3418 for (int i = 0; i < count; i++) {
3419 children[i].dispatchFinishTemporaryDetach();
3420 }
3421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01003424 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
Adam Powell4b867882011-09-16 12:59:46 -07003426 mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 super.dispatchAttachedToWindow(info, visibility);
Adam Powell4b867882011-09-16 12:59:46 -07003428 mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 final int count = mChildrenCount;
3431 final View[] children = mChildren;
3432 for (int i = 0; i < count; i++) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07003433 final View child = children[i];
3434 child.dispatchAttachedToWindow(info,
Adam Powelleb2b0af2015-05-20 18:26:35 -07003435 combineVisibility(visibility, child.getVisibility()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 }
Chet Haasec633d2f2015-04-07 10:29:39 -07003437 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
3438 for (int i = 0; i < transientCount; ++i) {
3439 View view = mTransientViews.get(i);
Adam Powelleb2b0af2015-05-20 18:26:35 -07003440 view.dispatchAttachedToWindow(info,
3441 combineVisibility(visibility, view.getVisibility()));
Chet Haasec633d2f2015-04-07 10:29:39 -07003442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 }
3444
svetoslavganov75986cf2009-05-14 22:28:01 -07003445 @Override
Romain Guybb9908b2012-03-08 11:14:07 -08003446 void dispatchScreenStateChanged(int screenState) {
3447 super.dispatchScreenStateChanged(screenState);
3448
3449 final int count = mChildrenCount;
3450 final View[] children = mChildren;
3451 for (int i = 0; i < count; i++) {
3452 children[i].dispatchScreenStateChanged(screenState);
3453 }
3454 }
3455
Andrii Kulianb047b8b2017-02-08 18:38:26 -08003456 @Override
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07003457 void dispatchMovedToDisplay(Display display, Configuration config) {
3458 super.dispatchMovedToDisplay(display, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08003459
3460 final int count = mChildrenCount;
3461 final View[] children = mChildren;
3462 for (int i = 0; i < count; i++) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07003463 children[i].dispatchMovedToDisplay(display, config);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08003464 }
3465 }
3466
Alan Viverettea54956a2015-01-07 16:05:02 -08003467 /** @hide */
Romain Guybb9908b2012-03-08 11:14:07 -08003468 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08003469 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07003470 boolean handled = false;
3471 if (includeForAccessibility()) {
3472 handled = super.dispatchPopulateAccessibilityEventInternal(event);
3473 if (handled) {
3474 return handled;
3475 }
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07003476 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003477 // Let our children have a shot in populating the event.
Svetoslav Ganov42138042012-03-20 11:51:39 -07003478 ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true);
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07003479 try {
3480 final int childCount = children.getChildCount();
3481 for (int i = 0; i < childCount; i++) {
3482 View child = children.getChildAt(i);
3483 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
3484 handled = child.dispatchPopulateAccessibilityEvent(event);
3485 if (handled) {
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07003486 return handled;
3487 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07003488 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003489 }
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07003490 } finally {
3491 children.recycle();
svetoslavganov75986cf2009-05-14 22:28:01 -07003492 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003493 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07003494 }
3495
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07003496 /**
Felipe Leme6d553872016-12-08 17:13:25 -08003497 * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation
3498 * adds in all child views of the view group, in addition to calling the default View
3499 * implementation.
3500 */
3501 @Override
3502 public void dispatchProvideStructure(ViewStructure structure) {
3503 super.dispatchProvideStructure(structure);
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003504 if (isAssistBlocked() || structure.getChildCount() != 0) {
3505 return;
3506 }
3507 final int childrenCount = mChildrenCount;
3508 if (childrenCount <= 0) {
3509 return;
3510 }
Philip P. Moltmann083d8aa2017-08-15 15:22:37 -07003511
3512 if (!isLaidOut()) {
Felipe Lemefe05a522018-01-23 15:57:49 -08003513 if (Helper.sVerbose) {
3514 Log.v(VIEW_LOG_TAG, "dispatchProvideStructure(): not laid out, ignoring "
3515 + childrenCount + " children of " + getAccessibilityViewId());
3516 }
Philip P. Moltmann083d8aa2017-08-15 15:22:37 -07003517 return;
3518 }
3519
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003520 structure.setChildCount(childrenCount);
3521 ArrayList<View> preorderedList = buildOrderedChildList();
3522 boolean customOrder = preorderedList == null
3523 && isChildrenDrawingOrderEnabled();
3524 for (int i = 0; i < childrenCount; i++) {
3525 int childIndex;
3526 try {
3527 childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3528 } catch (IndexOutOfBoundsException e) {
3529 childIndex = i;
3530 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.M) {
3531 Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ "
3532 + i + " of " + childrenCount, e);
3533 // At least one app is failing when we call getChildDrawingOrder
3534 // at this point, so deal semi-gracefully with it by falling back
3535 // on the basic order.
3536 customOrder = false;
3537 if (i > 0) {
3538 // If we failed at the first index, there really isn't
3539 // anything to do -- we will just proceed with the simple
3540 // sequence order.
3541 // Otherwise, we failed in the middle, so need to come up
3542 // with an order for the remaining indices and use that.
3543 // Failed at the first one, easy peasy.
3544 int[] permutation = new int[childrenCount];
3545 SparseBooleanArray usedIndices = new SparseBooleanArray();
3546 // Go back and collected the indices we have done so far.
3547 for (int j = 0; j < i; j++) {
3548 permutation[j] = getChildDrawingOrder(childrenCount, j);
3549 usedIndices.put(permutation[j], true);
3550 }
3551 // Fill in the remaining indices with indices that have not
3552 // yet been used.
3553 int nextIndex = 0;
3554 for (int j = i; j < childrenCount; j++) {
3555 while (usedIndices.get(nextIndex, false)) {
3556 nextIndex++;
3557 }
3558 permutation[j] = nextIndex;
3559 nextIndex++;
3560 }
3561 // Build the final view list.
3562 preorderedList = new ArrayList<>(childrenCount);
3563 for (int j = 0; j < childrenCount; j++) {
3564 final int index = permutation[j];
3565 final View child = mChildren[index];
3566 preorderedList.add(child);
3567 }
3568 }
3569 } else {
3570 throw e;
3571 }
3572 }
3573 final View child = getAndVerifyPreorderedView(preorderedList, mChildren,
3574 childIndex);
3575 final ViewStructure cstructure = structure.newChild(i);
3576 child.dispatchProvideStructure(cstructure);
3577 }
3578 if (preorderedList != null) {
3579 preorderedList.clear();
3580 }
Felipe Leme6d553872016-12-08 17:13:25 -08003581 }
3582
3583 /**
Felipe Leme1ca634a2016-11-28 17:21:21 -08003584 * {@inheritDoc}
3585 *
3586 * <p>This implementation adds in all child views of the view group, in addition to calling the
3587 * default {@link View} implementation.
Dianne Hackborn6251f0d2015-04-01 16:45:03 -07003588 */
Alan Viverettebe463f22016-01-21 10:50:10 -05003589 @Override
Svet Ganovfd31f852017-04-26 15:54:27 -07003590 public void dispatchProvideAutofillStructure(ViewStructure structure,
3591 @AutofillFlags int flags) {
Felipe Leme640f30a2017-03-06 15:44:06 -08003592 super.dispatchProvideAutofillStructure(structure, flags);
Svetoslav Ganov24c90452017-12-27 15:17:14 -08003593
Svet Ganovfd31f852017-04-26 15:54:27 -07003594 if (structure.getChildCount() != 0) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003595 return;
3596 }
Philip P. Moltmann083d8aa2017-08-15 15:22:37 -07003597
3598 if (!isLaidOut()) {
Svetoslav Ganov24c90452017-12-27 15:17:14 -08003599 if (Helper.sVerbose) {
3600 Log.v(VIEW_LOG_TAG, "dispatchProvideAutofillStructure(): not laid out, ignoring "
3601 + mChildrenCount + " children of " + getAutofillId());
3602 }
Philip P. Moltmann083d8aa2017-08-15 15:22:37 -07003603 return;
3604 }
3605
Svet Ganovfd31f852017-04-26 15:54:27 -07003606 final ChildListForAutoFill children = getChildrenForAutofill(flags);
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003607 final int childrenCount = children.size();
3608 structure.setChildCount(childrenCount);
3609 for (int i = 0; i < childrenCount; i++) {
3610 final View child = children.get(i);
3611 final ViewStructure cstructure = structure.newChild(i);
3612 child.dispatchProvideAutofillStructure(cstructure, flags);
3613 }
3614 children.recycle();
Felipe Leme6d553872016-12-08 17:13:25 -08003615 }
Felipe Leme1ca634a2016-11-28 17:21:21 -08003616
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003617 /**
3618 * Gets the children for autofill. Children for autofill are the first
3619 * level descendants that are important for autofill. The returned
3620 * child list object is pooled and the caller must recycle it once done.
3621 * @hide */
Svet Ganovfd31f852017-04-26 15:54:27 -07003622 private @NonNull ChildListForAutoFill getChildrenForAutofill(@AutofillFlags int flags) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003623 final ChildListForAutoFill children = ChildListForAutoFill.obtain();
Svet Ganovfd31f852017-04-26 15:54:27 -07003624 populateChildrenForAutofill(children, flags);
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003625 return children;
Felipe Lemed04a6972017-03-02 12:56:18 -08003626 }
3627
3628 /** @hide */
Svet Ganovfd31f852017-04-26 15:54:27 -07003629 private void populateChildrenForAutofill(ArrayList<View> list, @AutofillFlags int flags) {
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07003630 final int childrenCount = mChildrenCount;
3631 if (childrenCount <= 0) {
3632 return;
3633 }
3634 final ArrayList<View> preorderedList = buildOrderedChildList();
3635 final boolean customOrder = preorderedList == null
3636 && isChildrenDrawingOrderEnabled();
3637 for (int i = 0; i < childrenCount; i++) {
3638 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
3639 final View child = (preorderedList == null)
3640 ? mChildren[childIndex] : preorderedList.get(childIndex);
Svet Ganovfd31f852017-04-26 15:54:27 -07003641 if ((flags & AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0
3642 || child.isImportantForAutofill()) {
Felipe Lemed04a6972017-03-02 12:56:18 -08003643 list.add(child);
3644 } else if (child instanceof ViewGroup) {
Svet Ganovfd31f852017-04-26 15:54:27 -07003645 ((ViewGroup) child).populateChildrenForAutofill(list, flags);
Felipe Lemed04a6972017-03-02 12:56:18 -08003646 }
3647 }
3648 }
3649
Alan Viverettea7b85e62016-01-22 10:14:02 -05003650 private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,
3651 int childIndex) {
3652 final View child;
3653 if (preorderedList != null) {
3654 child = preorderedList.get(childIndex);
3655 if (child == null) {
3656 throw new RuntimeException("Invalid preorderedList contained null child at index "
3657 + childIndex);
3658 }
3659 } else {
3660 child = children[childIndex];
3661 }
3662 return child;
3663 }
3664
Alan Viverettea54956a2015-01-07 16:05:02 -08003665 /** @hide */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003666 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01003667 @UnsupportedAppUsage
Alan Viverettea54956a2015-01-07 16:05:02 -08003668 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003669 super.onInitializeAccessibilityNodeInfoInternal(info);
Svet Ganov55bdb102015-02-06 12:41:17 -08003670 if (getAccessibilityNodeProvider() != null) {
3671 return;
3672 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003673 if (mAttachInfo != null) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003674 final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003675 childrenForAccessibility.clear();
3676 addChildrenForAccessibility(childrenForAccessibility);
3677 final int childrenForAccessibilityCount = childrenForAccessibility.size();
3678 for (int i = 0; i < childrenForAccessibilityCount; i++) {
Alan Viverettef0aed092013-11-06 15:33:03 -08003679 final View child = childrenForAccessibility.get(i);
3680 info.addChildUnchecked(child);
Svetoslav Ganovea1da3d2011-06-15 17:16:02 -07003681 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003682 childrenForAccessibility.clear();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003683 }
3684 }
3685
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003686 @Override
Maxim Bogatovf399af32015-06-16 15:15:49 -07003687 public CharSequence getAccessibilityClassName() {
3688 return ViewGroup.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08003689 }
3690
Eugene Susla72c510f2018-01-23 21:12:11 +00003691 @Override
3692 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
3693 // If this is a live region, we should send a subtree change event
3694 // from this view. Otherwise, we can let it propagate up.
3695 if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) {
3696 notifyViewAccessibilityStateChangedIfNeeded(
3697 AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
3698 } else if (mParent != null) {
3699 try {
3700 mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType);
3701 } catch (AbstractMethodError e) {
3702 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
3703 " does not fully implement ViewParent", e);
3704 }
3705 }
3706 }
3707
Phil Weaver4d3eec412016-09-01 16:28:34 -07003708 /** @hide */
3709 @Override
Eugene Susla72c510f2018-01-23 21:12:11 +00003710 public void notifySubtreeAccessibilityStateChangedIfNeeded() {
Phil Weaver4d3eec412016-09-01 16:28:34 -07003711 if (!AccessibilityManager.getInstance(mContext).isEnabled() || mAttachInfo == null) {
3712 return;
3713 }
3714 // If something important for a11y is happening in this subtree, make sure it's dispatched
3715 // from a view that is important for a11y so it doesn't get lost.
Eugene Susla72c510f2018-01-23 21:12:11 +00003716 if ((getImportantForAccessibility() != IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS)
3717 && !isImportantForAccessibility() && (getChildCount() > 0)) {
Phil Weaver4d3eec412016-09-01 16:28:34 -07003718 ViewParent a11yParent = getParentForAccessibility();
3719 if (a11yParent instanceof View) {
Eugene Susla72c510f2018-01-23 21:12:11 +00003720 ((View) a11yParent).notifySubtreeAccessibilityStateChangedIfNeeded();
Phil Weaver4d3eec412016-09-01 16:28:34 -07003721 return;
3722 }
3723 }
Eugene Susla72c510f2018-01-23 21:12:11 +00003724 super.notifySubtreeAccessibilityStateChangedIfNeeded();
Phil Weaver4d3eec412016-09-01 16:28:34 -07003725 }
3726
Svetoslav6254f482013-06-04 17:22:14 -07003727 @Override
Eugene Susla72c510f2018-01-23 21:12:11 +00003728 void resetSubtreeAccessibilityStateChanged() {
Svetoslav6254f482013-06-04 17:22:14 -07003729 super.resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003730 View[] children = mChildren;
3731 final int childCount = mChildrenCount;
3732 for (int i = 0; i < childCount; i++) {
Svetoslav6254f482013-06-04 17:22:14 -07003733 children[i].resetSubtreeAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07003734 }
3735 }
3736
3737 /**
Phil Weaver1f222542016-01-08 11:49:32 -08003738 * Counts the number of children of this View that will be sent to an accessibility service.
3739 *
3740 * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View
3741 * would have.
3742 */
3743 int getNumChildrenForAccessibility() {
3744 int numChildrenForAccessibility = 0;
3745 for (int i = 0; i < getChildCount(); i++) {
3746 View child = getChildAt(i);
3747 if (child.includeForAccessibility()) {
3748 numChildrenForAccessibility++;
3749 } else if (child instanceof ViewGroup) {
3750 numChildrenForAccessibility += ((ViewGroup) child)
3751 .getNumChildrenForAccessibility();
3752 }
3753 }
3754 return numChildrenForAccessibility;
3755 }
3756
3757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 * {@inheritDoc}
Adam Powellb6ab0982015-01-07 17:00:12 -08003759 *
3760 * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p>
3761 *
3762 * @param target The target view dispatching this action
3763 * @param action Action being performed; see
3764 * {@link android.view.accessibility.AccessibilityNodeInfo}
3765 * @param args Optional action arguments
3766 * @return false by default. Subclasses should return true if they handle the event.
3767 */
3768 @Override
3769 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
3770 return false;
3771 }
3772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01003774 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 void dispatchDetachedFromWindow() {
Jeff Brown20e987b2010-08-23 12:01:02 -07003776 // If we still have a touch target, we are still in the process of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 // dispatching motion events to a child; we need to get rid of that
3778 // child to avoid dispatching events to it after the window is torn
3779 // down. To make sure we keep the child in a consistent state, we
3780 // first send it an ACTION_CANCEL motion event.
Jeff Brown20e987b2010-08-23 12:01:02 -07003781 cancelAndClearTouchTargets(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782
Jeff Brown59a422e2012-04-19 15:19:19 -07003783 // Similarly, set ACTION_EXIT to all hover targets and clear them.
3784 exitHoverTargets();
Vladislav Kaznacheevf847ee32016-11-21 14:11:00 -08003785 exitTooltipHoverTargets();
Jeff Brown59a422e2012-04-19 15:19:19 -07003786
Chet Haase9c087442011-01-12 16:20:16 -08003787 // In case view is detached while transition is running
Chet Haaseb9895022013-04-02 15:10:58 -07003788 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08003789
Christopher Tate86cab1b2011-01-13 20:28:55 -08003790 // Tear down our drag tracking
Vadim Tryshev1a68dc92015-07-20 17:01:50 -07003791 mChildrenInterestedInDrag = null;
3792 mIsInterestedInDrag = false;
3793 if (mCurrentDragStartEvent != null) {
3794 mCurrentDragStartEvent.recycle();
3795 mCurrentDragStartEvent = null;
Christopher Tate86cab1b2011-01-13 20:28:55 -08003796 }
3797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 final int count = mChildrenCount;
3799 final View[] children = mChildren;
3800 for (int i = 0; i < count; i++) {
3801 children[i].dispatchDetachedFromWindow();
3802 }
John Reckca7a9da2014-03-05 16:29:07 -08003803 clearDisappearingChildren();
Chet Haasec633d2f2015-04-07 10:29:39 -07003804 final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size();
3805 for (int i = 0; i < transientCount; ++i) {
3806 View view = mTransientViews.get(i);
3807 view.dispatchDetachedFromWindow();
3808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 super.dispatchDetachedFromWindow();
3810 }
3811
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003812 /**
3813 * @hide
3814 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 @Override
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003816 protected void internalSetPadding(int left, int top, int right, int bottom) {
Romain Guy2440e672012-08-07 14:43:43 -07003817 super.internalSetPadding(left, top, right, bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818
Romain Guy13f35f32011-03-24 12:03:17 -07003819 if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 mGroupFlags |= FLAG_PADDING_NOT_NULL;
3821 } else {
3822 mGroupFlags &= ~FLAG_PADDING_NOT_NULL;
3823 }
3824 }
3825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 @Override
3827 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
3828 super.dispatchSaveInstanceState(container);
3829 final int count = mChildrenCount;
3830 final View[] children = mChildren;
3831 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003832 View c = children[i];
3833 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3834 c.dispatchSaveInstanceState(container);
3835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 }
3837 }
3838
3839 /**
Romain Guy9fc27812011-04-27 14:21:41 -07003840 * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()}
3841 * to only this view, not to its children. For use when overriding
3842 * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow
3843 * subclasses to freeze their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 *
3845 * @param container the container
3846 */
3847 protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
3848 super.dispatchSaveInstanceState(container);
3849 }
3850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 @Override
3852 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
3853 super.dispatchRestoreInstanceState(container);
3854 final int count = mChildrenCount;
3855 final View[] children = mChildren;
3856 for (int i = 0; i < count; i++) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003857 View c = children[i];
3858 if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) {
3859 c.dispatchRestoreInstanceState(container);
3860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 }
3862 }
3863
3864 /**
Romain Guy02739a82011-05-16 11:43:18 -07003865 * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)}
3866 * to only this view, not to its children. For use when overriding
3867 * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow
3868 * subclasses to thaw their own state but not the state of their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 *
3870 * @param container the container
3871 */
3872 protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
3873 super.dispatchRestoreInstanceState(container);
3874 }
3875
3876 /**
3877 * Enables or disables the drawing cache for each child of this view group.
3878 *
3879 * @param enabled true to enable the cache, false to dispose of it
John Reck949cfe12017-10-09 13:27:03 -07003880 *
3881 * @deprecated The view drawing cache was largely made obsolete with the introduction of
3882 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
3883 * layers are largely unnecessary and can easily result in a net loss in performance due to the
3884 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
3885 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
3886 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
3887 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
3888 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
3889 * software-rendered usages are discouraged and have compatibility issues with hardware-only
3890 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
3891 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
3892 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 */
John Reck949cfe12017-10-09 13:27:03 -07003894 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
3896 if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) {
3897 final View[] children = mChildren;
3898 final int count = mChildrenCount;
3899 for (int i = 0; i < count; i++) {
3900 children[i].setDrawingCacheEnabled(enabled);
3901 }
3902 }
3903 }
3904
sergeyvb37d44e2016-03-29 20:27:44 -07003905 /**
3906 * @hide
3907 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 @Override
Sunny Goyald1b287e2018-01-04 09:37:22 -08003909 public Bitmap createSnapshot(ViewDebug.CanvasProvider canvasProvider, boolean skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003910 int count = mChildrenCount;
3911 int[] visibilities = null;
3912
Romain Guy223ff5c2010-03-02 17:07:47 -08003913 if (skipChildren) {
Romain Guy65554f22010-03-22 18:58:21 -07003914 visibilities = new int[count];
3915 for (int i = 0; i < count; i++) {
3916 View child = getChildAt(i);
3917 visibilities[i] = child.getVisibility();
3918 if (visibilities[i] == View.VISIBLE) {
sergeyvb37d44e2016-03-29 20:27:44 -07003919 child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK)
3920 | (View.INVISIBLE & View.VISIBILITY_MASK);
Romain Guy65554f22010-03-22 18:58:21 -07003921 }
3922 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003923 }
3924
Sunny Goyald1b287e2018-01-04 09:37:22 -08003925 try {
3926 return super.createSnapshot(canvasProvider, skipChildren);
3927 } finally {
3928 if (skipChildren) {
3929 for (int i = 0; i < count; i++) {
3930 View child = getChildAt(i);
3931 child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK)
3932 | (visibilities[i] & View.VISIBILITY_MASK);
3933 }
Chet Haase5c13d892010-10-08 08:37:55 -07003934 }
Romain Guy65554f22010-03-22 18:58:21 -07003935 }
Romain Guy223ff5c2010-03-02 17:07:47 -08003936 }
3937
Philip Milne7b757812012-09-19 18:13:44 -07003938 /** Return true if this ViewGroup is laying out using optical bounds. */
3939 boolean isLayoutModeOptical() {
3940 return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS;
3941 }
Romain Guycbc67742012-04-27 16:12:57 -07003942
Alan Viverettebe463f22016-01-21 10:50:10 -05003943 @Override
Philip Milne7b757812012-09-19 18:13:44 -07003944 Insets computeOpticalInsets() {
3945 if (isLayoutModeOptical()) {
3946 int left = 0;
3947 int top = 0;
3948 int right = 0;
3949 int bottom = 0;
3950 for (int i = 0; i < mChildrenCount; i++) {
3951 View child = getChildAt(i);
3952 if (child.getVisibility() == VISIBLE) {
3953 Insets insets = child.getOpticalInsets();
3954 left = Math.max(left, insets.left);
3955 top = Math.max(top, insets.top);
3956 right = Math.max(right, insets.right);
3957 bottom = Math.max(bottom, insets.bottom);
3958 }
3959 }
3960 return Insets.of(left, top, right, bottom);
3961 } else {
3962 return Insets.NONE;
3963 }
3964 }
3965
3966 private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
3967 if (x1 != x2 && y1 != y2) {
3968 if (x1 > x2) {
3969 int tmp = x1; x1 = x2; x2 = tmp;
3970 }
3971 if (y1 > y2) {
3972 int tmp = y1; y1 = y2; y2 = tmp;
3973 }
3974 canvas.drawRect(x1, y1, x2, y2, paint);
3975 }
3976 }
3977
3978 private static int sign(int x) {
3979 return (x >= 0) ? 1 : -1;
3980 }
3981
3982 private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) {
3983 fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy));
3984 fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
3985 }
3986
Romain Guy6410c0a2013-06-17 11:21:58 -07003987 private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
3988 int lineLength, int lineWidth) {
Philip Milne7b757812012-09-19 18:13:44 -07003989 drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
3990 drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth);
3991 drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth);
3992 drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth);
3993 }
3994
3995 private static void fillDifference(Canvas canvas,
3996 int x2, int y2, int x3, int y3,
3997 int dx1, int dy1, int dx2, int dy2, Paint paint) {
3998 int x1 = x2 - dx1;
3999 int y1 = y2 - dy1;
4000
4001 int x4 = x3 + dx2;
4002 int y4 = y3 + dy2;
4003
4004 fillRect(canvas, paint, x1, y1, x4, y2);
4005 fillRect(canvas, paint, x1, y2, x2, y3);
4006 fillRect(canvas, paint, x3, y2, x4, y3);
4007 fillRect(canvas, paint, x1, y3, x4, y4);
Philip Milne10ca24a2012-04-23 15:38:27 -07004008 }
4009
4010 /**
Alan Viverette2d7771c2018-01-31 17:04:31 -05004011 * @hide
Philip Milne10ca24a2012-04-23 15:38:27 -07004012 */
Philip Milne7b757812012-09-19 18:13:44 -07004013 protected void onDebugDrawMargins(Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07004014 for (int i = 0; i < getChildCount(); i++) {
4015 View c = getChildAt(i);
Philip Milne7b757812012-09-19 18:13:44 -07004016 c.getLayoutParams().onDebugDraw(c, canvas, paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07004017 }
4018 }
4019
4020 /**
Alan Viverette2d7771c2018-01-31 17:04:31 -05004021 * @hide
Philip Milne10ca24a2012-04-23 15:38:27 -07004022 */
4023 protected void onDebugDraw(Canvas canvas) {
Philip Milne7b757812012-09-19 18:13:44 -07004024 Paint paint = getDebugPaint();
4025
Philip Milne10ca24a2012-04-23 15:38:27 -07004026 // Draw optical bounds
Philip Milne7b757812012-09-19 18:13:44 -07004027 {
4028 paint.setColor(Color.RED);
4029 paint.setStyle(Paint.Style.STROKE);
Philip Milne7b757812012-09-19 18:13:44 -07004030
Philip Milne10ca24a2012-04-23 15:38:27 -07004031 for (int i = 0; i < getChildCount(); i++) {
4032 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07004033 if (c.getVisibility() != View.GONE) {
4034 Insets insets = c.getOpticalInsets();
Philip Milne7b757812012-09-19 18:13:44 -07004035
Chris Craika1dab8b2015-06-30 13:51:25 -07004036 drawRect(canvas, paint,
4037 c.getLeft() + insets.left,
4038 c.getTop() + insets.top,
4039 c.getRight() - insets.right - 1,
4040 c.getBottom() - insets.bottom - 1);
4041 }
Philip Milne10ca24a2012-04-23 15:38:27 -07004042 }
4043 }
4044
Philip Milne10ca24a2012-04-23 15:38:27 -07004045 // Draw margins
Philip Milne7b757812012-09-19 18:13:44 -07004046 {
4047 paint.setColor(Color.argb(63, 255, 0, 255));
4048 paint.setStyle(Paint.Style.FILL);
Philip Milne604f4402012-04-24 19:27:11 -07004049
Philip Milne7b757812012-09-19 18:13:44 -07004050 onDebugDrawMargins(canvas, paint);
4051 }
4052
4053 // Draw clip bounds
4054 {
Vadim Tryshevcdf38ba2016-10-11 18:33:10 -07004055 paint.setColor(DEBUG_CORNERS_COLOR);
Philip Milne7b757812012-09-19 18:13:44 -07004056 paint.setStyle(Paint.Style.FILL);
4057
Vadim Tryshevcdf38ba2016-10-11 18:33:10 -07004058 int lineLength = dipsToPixels(DEBUG_CORNERS_SIZE_DIP);
Philip Milne7b757812012-09-19 18:13:44 -07004059 int lineWidth = dipsToPixels(1);
4060 for (int i = 0; i < getChildCount(); i++) {
4061 View c = getChildAt(i);
Chris Craika1dab8b2015-06-30 13:51:25 -07004062 if (c.getVisibility() != View.GONE) {
4063 drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(),
4064 paint, lineLength, lineWidth);
4065 }
Philip Milne7b757812012-09-19 18:13:44 -07004066 }
Philip Milne604f4402012-04-24 19:27:11 -07004067 }
Philip Milne10ca24a2012-04-23 15:38:27 -07004068 }
4069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 @Override
4071 protected void dispatchDraw(Canvas canvas) {
Chris Craika753f4c2014-07-24 12:39:17 -07004072 boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
Chris Craikab008f02014-05-23 17:55:03 -07004073 final int childrenCount = mChildrenCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 final View[] children = mChildren;
4075 int flags = mGroupFlags;
4076
4077 if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
Romain Guy0d9275e2010-10-26 14:22:30 -07004078 final boolean buildCache = !isHardwareAccelerated();
Chris Craikab008f02014-05-23 17:55:03 -07004079 for (int i = 0; i < childrenCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 final View child = children[i];
4081 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
4082 final LayoutParams params = child.getLayoutParams();
Chris Craikab008f02014-05-23 17:55:03 -07004083 attachLayoutAnimationParameters(child, params, i, childrenCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 bindLayoutAnimation(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 }
4086 }
4087
4088 final LayoutAnimationController controller = mLayoutAnimationController;
4089 if (controller.willOverlap()) {
4090 mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE;
4091 }
4092
4093 controller.start();
4094
4095 mGroupFlags &= ~FLAG_RUN_ANIMATION;
4096 mGroupFlags &= ~FLAG_ANIMATION_DONE;
4097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 if (mAnimationListener != null) {
4099 mAnimationListener.onAnimationStart(controller.getAnimation());
4100 }
4101 }
4102
Selim Cinek19cadc22014-04-16 17:27:19 +02004103 int clipSaveCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
4105 if (clipToPadding) {
John Reck41f864e2016-05-12 15:07:49 -07004106 clipSaveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
Romain Guy8f2d94f2009-03-25 18:04:42 -07004107 canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
4108 mScrollX + mRight - mLeft - mPaddingRight,
4109 mScrollY + mBottom - mTop - mPaddingBottom);
Selim Cinek19cadc22014-04-16 17:27:19 +02004110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 // We will draw our child's animation, let's reset the flag
Dianne Hackborn4702a852012-08-17 15:18:29 -07004113 mPrivateFlags &= ~PFLAG_DRAW_ANIMATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
4115
4116 boolean more = false;
4117 final long drawingTime = getDrawingTime();
4118
Chris Craik8afd0f22014-08-21 17:41:57 -07004119 if (usingRenderNodeProperties) canvas.insertReorderBarrier();
Chet Haasec633d2f2015-04-07 10:29:39 -07004120 final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
4121 int transientIndex = transientCount != 0 ? 0 : -1;
Chris Craikab008f02014-05-23 17:55:03 -07004122 // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
4123 // draw reordering internally
Chris Craika753f4c2014-07-24 12:39:17 -07004124 final ArrayList<View> preorderedList = usingRenderNodeProperties
Chris Craikab008f02014-05-23 17:55:03 -07004125 ? null : buildOrderedChildList();
4126 final boolean customOrder = preorderedList == null
4127 && isChildrenDrawingOrderEnabled();
4128 for (int i = 0; i < childrenCount; i++) {
Chet Haasec633d2f2015-04-07 10:29:39 -07004129 while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
4130 final View transientChild = mTransientViews.get(transientIndex);
4131 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
4132 transientChild.getAnimation() != null) {
4133 more |= drawChild(canvas, transientChild, drawingTime);
4134 }
4135 transientIndex++;
4136 if (transientIndex >= transientCount) {
4137 transientIndex = -1;
4138 }
4139 }
Alan Viverettea7b85e62016-01-22 10:14:02 -05004140
4141 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
4142 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
Chris Craikab008f02014-05-23 17:55:03 -07004143 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
4144 more |= drawChild(canvas, child, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 }
4146 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004147 while (transientIndex >= 0) {
4148 // there may be additional transient views after the normal views
4149 final View transientChild = mTransientViews.get(transientIndex);
4150 if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
4151 transientChild.getAnimation() != null) {
4152 more |= drawChild(canvas, transientChild, drawingTime);
4153 }
4154 transientIndex++;
4155 if (transientIndex >= transientCount) {
4156 break;
4157 }
4158 }
Chris Craikab008f02014-05-23 17:55:03 -07004159 if (preorderedList != null) preorderedList.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160
4161 // Draw any disappearing views that have animations
4162 if (mDisappearingChildren != null) {
4163 final ArrayList<View> disappearingChildren = mDisappearingChildren;
4164 final int disappearingCount = disappearingChildren.size() - 1;
4165 // Go backwards -- we may delete as animations finish
4166 for (int i = disappearingCount; i >= 0; i--) {
4167 final View child = disappearingChildren.get(i);
4168 more |= drawChild(canvas, child, drawingTime);
4169 }
4170 }
Chris Craik8afd0f22014-08-21 17:41:57 -07004171 if (usingRenderNodeProperties) canvas.insertInorderBarrier();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172
Philip Milne10ca24a2012-04-23 15:38:27 -07004173 if (debugDraw()) {
4174 onDebugDraw(canvas);
4175 }
4176
Chris Craike4cf1522014-08-04 17:55:22 -07004177 if (clipToPadding) {
Selim Cinek19cadc22014-04-16 17:27:19 +02004178 canvas.restoreToCount(clipSaveCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 }
4180
4181 // mGroupFlags might have been updated by drawChild()
4182 flags = mGroupFlags;
4183
4184 if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
Romain Guy849d0a32011-02-01 17:20:48 -08004185 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
4187
4188 if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
4189 mLayoutAnimationController.isDone() && !more) {
4190 // We want to erase the drawing cache and notify the listener after the
4191 // next frame is drawn because one extra invalidate() is caused by
4192 // drawChild() after the animation is over
4193 mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
4194 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05004195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 public void run() {
4197 notifyAnimationListener();
4198 }
4199 };
4200 post(end);
4201 }
4202 }
Romain Guy8506ab42009-06-11 17:35:47 -07004203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -07004205 * Returns the ViewGroupOverlay for this view group, creating it if it does
4206 * not yet exist. In addition to {@link ViewOverlay}'s support for drawables,
4207 * {@link ViewGroupOverlay} allows views to be added to the overlay. These
4208 * views, like overlay drawables, are visual-only; they do not receive input
4209 * events and should not be used as anything other than a temporary
4210 * representation of a view in a parent container, such as might be used
4211 * by an animation effect.
4212 *
Chet Haase95399492013-04-08 14:30:31 -07004213 * <p>Note: Overlays do not currently work correctly with {@link
4214 * SurfaceView} or {@link TextureView}; contents in overlays for these
4215 * types of views may not display correctly.</p>
4216 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -07004217 * @return The ViewGroupOverlay object for this view.
4218 * @see ViewGroupOverlay
4219 */
4220 @Override
4221 public ViewGroupOverlay getOverlay() {
4222 if (mOverlay == null) {
4223 mOverlay = new ViewGroupOverlay(mContext, this);
4224 }
4225 return (ViewGroupOverlay) mOverlay;
4226 }
4227
4228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 * Returns the index of the child to draw for this iteration. Override this
4230 * if you want to change the drawing order of children. By default, it
4231 * returns i.
4232 * <p>
Romain Guy293451e2009-11-04 13:59:48 -08004233 * NOTE: In order for this method to be called, you must enable child ordering
4234 * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}.
Romain Guy8506ab42009-06-11 17:35:47 -07004235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 * @param i The current iteration.
4237 * @return The index of the child to draw this iteration.
Chet Haase5c13d892010-10-08 08:37:55 -07004238 *
Romain Guy293451e2009-11-04 13:59:48 -08004239 * @see #setChildrenDrawingOrderEnabled(boolean)
4240 * @see #isChildrenDrawingOrderEnabled()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 */
4242 protected int getChildDrawingOrder(int childCount, int i) {
4243 return i;
4244 }
Romain Guy8506ab42009-06-11 17:35:47 -07004245
Chris Craikab008f02014-05-23 17:55:03 -07004246 private boolean hasChildWithZ() {
4247 for (int i = 0; i < mChildrenCount; i++) {
4248 if (mChildren[i].getZ() != 0) return true;
4249 }
4250 return false;
4251 }
4252
4253 /**
4254 * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,
Chris Craik57c79c82014-09-30 12:54:31 -07004255 * sorted first by Z, then by child drawing order (if applicable). This list must be cleared
4256 * after use to avoid leaking child Views.
Chris Craikab008f02014-05-23 17:55:03 -07004257 *
4258 * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated
4259 * children.
4260 */
George Mount81206522014-09-26 21:53:39 -07004261 ArrayList<View> buildOrderedChildList() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05004262 final int childrenCount = mChildrenCount;
4263 if (childrenCount <= 1 || !hasChildWithZ()) return null;
Chris Craikab008f02014-05-23 17:55:03 -07004264
4265 if (mPreSortedChildren == null) {
Alan Viverettea7b85e62016-01-22 10:14:02 -05004266 mPreSortedChildren = new ArrayList<>(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07004267 } else {
Chris Craikfc563772016-05-04 13:34:30 -07004268 // callers should clear, so clear shouldn't be necessary, but for safety...
4269 mPreSortedChildren.clear();
Alan Viverettea7b85e62016-01-22 10:14:02 -05004270 mPreSortedChildren.ensureCapacity(childrenCount);
Chris Craikab008f02014-05-23 17:55:03 -07004271 }
4272
Alan Viverettea7b85e62016-01-22 10:14:02 -05004273 final boolean customOrder = isChildrenDrawingOrderEnabled();
4274 for (int i = 0; i < childrenCount; i++) {
Chris Craikab008f02014-05-23 17:55:03 -07004275 // add next child (in child order) to end of list
Alan Viverettea7b85e62016-01-22 10:14:02 -05004276 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
4277 final View nextChild = mChildren[childIndex];
4278 final float currentZ = nextChild.getZ();
Chris Craikab008f02014-05-23 17:55:03 -07004279
4280 // insert ahead of any Views with greater Z
4281 int insertIndex = i;
4282 while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {
4283 insertIndex--;
4284 }
4285 mPreSortedChildren.add(insertIndex, nextChild);
4286 }
4287 return mPreSortedChildren;
4288 }
4289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 private void notifyAnimationListener() {
4291 mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER;
4292 mGroupFlags |= FLAG_ANIMATION_DONE;
4293
4294 if (mAnimationListener != null) {
4295 final Runnable end = new Runnable() {
Alan Viverettebe463f22016-01-21 10:50:10 -05004296 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 public void run() {
4298 mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation());
4299 }
4300 };
4301 post(end);
4302 }
4303
Romain Guy849d0a32011-02-01 17:20:48 -08004304 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 }
4306
4307 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08004308 * This method is used to cause children of this ViewGroup to restore or recreate their
4309 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
4310 * to recreate its own display list, which would happen if it went through the normal
4311 * draw/dispatchDraw mechanisms.
4312 *
4313 * @hide
4314 */
4315 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01004316 @UnsupportedAppUsage
Chet Haasedaf98e92011-01-10 14:10:36 -08004317 protected void dispatchGetDisplayList() {
4318 final int count = mChildrenCount;
4319 final View[] children = mChildren;
4320 for (int i = 0; i < count; i++) {
4321 final View child = children[i];
John Reckc2330f52015-04-28 13:18:52 -07004322 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) {
Chet Haase6c0665f2014-08-01 13:32:27 -07004323 recreateChildDisplayList(child);
Romain Guy2f57ba52011-02-03 18:03:29 -08004324 }
Chet Haasedaf98e92011-01-10 14:10:36 -08004325 }
Selim Cinekd6a51b12018-03-15 17:10:20 -07004326 final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size();
4327 for (int i = 0; i < transientCount; ++i) {
4328 View child = mTransientViews.get(i);
4329 if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) {
4330 recreateChildDisplayList(child);
4331 }
4332 }
Chet Haase91cedf12013-03-11 07:56:30 -07004333 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -07004334 View overlayView = mOverlay.getOverlayView();
Chet Haase6c0665f2014-08-01 13:32:27 -07004335 recreateChildDisplayList(overlayView);
Chet Haase91cedf12013-03-11 07:56:30 -07004336 }
Chet Haase6c0665f2014-08-01 13:32:27 -07004337 if (mDisappearingChildren != null) {
4338 final ArrayList<View> disappearingChildren = mDisappearingChildren;
4339 final int disappearingCount = disappearingChildren.size();
4340 for (int i = 0; i < disappearingCount; ++i) {
4341 final View child = disappearingChildren.get(i);
4342 recreateChildDisplayList(child);
4343 }
4344 }
4345 }
4346
4347 private void recreateChildDisplayList(View child) {
Chris Craik31a2d062015-05-01 14:22:47 -07004348 child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0;
Chet Haase6c0665f2014-08-01 13:32:27 -07004349 child.mPrivateFlags &= ~PFLAG_INVALIDATED;
Chris Craik31a2d062015-05-01 14:22:47 -07004350 child.updateDisplayListIfDirty();
Chet Haase6c0665f2014-08-01 13:32:27 -07004351 child.mRecreateDisplayList = false;
Chet Haasedaf98e92011-01-10 14:10:36 -08004352 }
4353
4354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 * Draw one child of this View Group. This method is responsible for getting
4356 * the canvas in the right state. This includes clipping, translating so
4357 * that the child's scrolled origin is at 0, 0, and applying any animation
4358 * transformations.
4359 *
4360 * @param canvas The canvas on which to draw the child
4361 * @param child Who to draw
Chet Haasebcca79a2012-02-14 08:45:14 -08004362 * @param drawingTime The time at which draw is occurring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 * @return True if an invalidate() was issued
4364 */
4365 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Chet Haase64a48c12012-02-13 16:33:29 -08004366 return child.draw(canvas, this, drawingTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
4368
Alan Viverette922e1c62015-05-05 17:18:27 -07004369 @Override
4370 void getScrollIndicatorBounds(@NonNull Rect out) {
4371 super.getScrollIndicatorBounds(out);
4372
4373 // If we have padding and we're supposed to clip children to that
4374 // padding, offset the scroll indicators to match our clip bounds.
4375 final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
4376 if (clipToPadding) {
4377 out.left += mPaddingLeft;
4378 out.right -= mPaddingRight;
4379 out.top += mPaddingTop;
4380 out.bottom -= mPaddingBottom;
4381 }
4382 }
4383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 /**
Chris Craikd863a102013-12-19 13:31:15 -08004385 * Returns whether this group's children are clipped to their bounds before drawing.
Chet Haase430742f2013-04-12 11:18:36 -07004386 * The default value is true.
4387 * @see #setClipChildren(boolean)
4388 *
4389 * @return True if the group's children will be clipped to their bounds,
4390 * false otherwise.
4391 */
Chris Craik5c75c522014-09-05 14:08:08 -07004392 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haase430742f2013-04-12 11:18:36 -07004393 public boolean getClipChildren() {
4394 return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0);
4395 }
4396
4397 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 * By default, children are clipped to their bounds before drawing. This
4399 * allows view groups to override this behavior for animations, etc.
4400 *
4401 * @param clipChildren true to clip children to their bounds,
4402 * false otherwise
4403 * @attr ref android.R.styleable#ViewGroup_clipChildren
4404 */
4405 public void setClipChildren(boolean clipChildren) {
Chet Haasea1cff502012-02-21 13:43:44 -08004406 boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN;
4407 if (clipChildren != previousValue) {
4408 setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren);
Chet Haase1271e2c2012-04-20 09:54:27 -07004409 for (int i = 0; i < mChildrenCount; ++i) {
4410 View child = getChildAt(i);
Chris Craik64a12e12014-03-28 18:12:12 -07004411 if (child.mRenderNode != null) {
4412 child.mRenderNode.setClipToBounds(clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -08004413 }
4414 }
John Reckaae9f3b2014-07-28 09:30:36 -07004415 invalidate(true);
Chet Haasea1cff502012-02-21 13:43:44 -08004416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 }
4418
4419 /**
Doris Liub134b5e2015-05-28 17:26:46 -07004420 * Sets whether this ViewGroup will clip its children to its padding and resize (but not
4421 * clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08004422 * <p>
4423 * By default, children are clipped to the padding of their parent
Doris Liub134b5e2015-05-28 17:26:46 -07004424 * ViewGroup. This clipping behavior is only enabled if padding is non-zero.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 *
Doris Liub134b5e2015-05-28 17:26:46 -07004426 * @param clipToPadding true to clip children to the padding of the group, and resize (but
4427 * not clip) any EdgeEffect to the padded region. False otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 * @attr ref android.R.styleable#ViewGroup_clipToPadding
4429 */
4430 public void setClipToPadding(boolean clipToPadding) {
John Reck9fa3a242014-06-27 15:57:19 -07004431 if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) {
4432 setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding);
John Reckaae9f3b2014-07-28 09:30:36 -07004433 invalidate(true);
John Reck9fa3a242014-06-27 15:57:19 -07004434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 }
4436
4437 /**
Doris Liub134b5e2015-05-28 17:26:46 -07004438 * Returns whether this ViewGroup will clip its children to its padding, and resize (but
4439 * not clip) any EdgeEffect to the padded region, if padding is present.
Chris Craikb1652962014-11-14 17:05:06 -08004440 * <p>
4441 * By default, children are clipped to the padding of their parent
4442 * Viewgroup. This clipping behavior is only enabled if padding is non-zero.
Adam Powell1c35b082014-07-11 15:37:15 -07004443 *
Doris Liub134b5e2015-05-28 17:26:46 -07004444 * @return true if this ViewGroup clips children to its padding and resizes (but doesn't
4445 * clip) any EdgeEffect to the padded region, false otherwise.
Adam Powell1c35b082014-07-11 15:37:15 -07004446 *
4447 * @attr ref android.R.styleable#ViewGroup_clipToPadding
4448 */
Chris Craik5c75c522014-09-05 14:08:08 -07004449 @ViewDebug.ExportedProperty(category = "drawing")
Adam Powell1c35b082014-07-11 15:37:15 -07004450 public boolean getClipToPadding() {
4451 return hasBooleanFlag(FLAG_CLIP_TO_PADDING);
4452 }
4453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 @Override
4455 public void dispatchSetSelected(boolean selected) {
4456 final View[] children = mChildren;
4457 final int count = mChildrenCount;
4458 for (int i = 0; i < count; i++) {
4459 children[i].setSelected(selected);
4460 }
4461 }
Romain Guy8506ab42009-06-11 17:35:47 -07004462
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07004463 @Override
4464 public void dispatchSetActivated(boolean activated) {
4465 final View[] children = mChildren;
4466 final int count = mChildrenCount;
4467 for (int i = 0; i < count; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07004468 children[i].setActivated(activated);
4469 }
4470 }
4471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 @Override
4473 protected void dispatchSetPressed(boolean pressed) {
4474 final View[] children = mChildren;
4475 final int count = mChildrenCount;
4476 for (int i = 0; i < count; i++) {
Adam Powell035a1fc2012-02-27 15:23:50 -08004477 final View child = children[i];
4478 // Children that are clickable on their own should not
4479 // show a pressed state when their parent view does.
4480 // Clearing a pressed state always propagates.
4481 if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
4482 child.setPressed(pressed);
4483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 }
4485 }
4486
Alan Viveretteb942b6f2014-12-08 10:37:39 -08004487 /**
4488 * Dispatches drawable hotspot changes to child views that meet at least
4489 * one of the following criteria:
4490 * <ul>
4491 * <li>Returns {@code false} from both {@link View#isClickable()} and
4492 * {@link View#isLongClickable()}</li>
4493 * <li>Requests duplication of parent state via
4494 * {@link View#setDuplicateParentStateEnabled(boolean)}</li>
4495 * </ul>
4496 *
4497 * @param x hotspot x coordinate
4498 * @param y hotspot y coordinate
4499 * @see #drawableHotspotChanged(float, float)
4500 */
4501 @Override
4502 public void dispatchDrawableHotspotChanged(float x, float y) {
4503 final int count = mChildrenCount;
4504 if (count == 0) {
4505 return;
4506 }
4507
4508 final View[] children = mChildren;
4509 for (int i = 0; i < count; i++) {
4510 final View child = children[i];
4511 // Children that are clickable on their own should not
4512 // receive hotspots when their parent view does.
4513 final boolean nonActionable = !child.isClickable() && !child.isLongClickable();
4514 final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0;
4515 if (nonActionable || duplicatesState) {
4516 final float[] point = getTempPoint();
4517 point[0] = x;
4518 point[1] = y;
4519 transformPointToViewLocal(point, child);
4520 child.drawableHotspotChanged(point[0], point[1]);
4521 }
4522 }
4523 }
4524
Adam Powell14874662013-07-18 19:42:41 -07004525 @Override
4526 void dispatchCancelPendingInputEvents() {
4527 super.dispatchCancelPendingInputEvents();
4528
4529 final View[] children = mChildren;
4530 final int count = mChildrenCount;
4531 for (int i = 0; i < count; i++) {
4532 children[i].dispatchCancelPendingInputEvents();
4533 }
4534 }
4535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 /**
4537 * When this property is set to true, this ViewGroup supports static transformations on
4538 * children; this causes
4539 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be
4540 * invoked when a child is drawn.
4541 *
4542 * Any subclass overriding
4543 * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should
4544 * set this property to true.
4545 *
4546 * @param enabled True to enable static transformations on children, false otherwise.
4547 *
Chet Haase599913d2012-07-23 16:22:05 -07004548 * @see #getChildStaticTransformation(View, android.view.animation.Transformation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 */
4550 protected void setStaticTransformationsEnabled(boolean enabled) {
4551 setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled);
4552 }
4553
4554 /**
Chet Haase2d46fcc2011-12-19 18:01:05 -08004555 * Sets <code>t</code> to be the static transformation of the child, if set, returning a
4556 * boolean to indicate whether a static transform was set. The default implementation
4557 * simply returns <code>false</code>; subclasses may override this method for different
Chet Haase599913d2012-07-23 16:22:05 -07004558 * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true
4559 * for this method to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 *
Chet Haase2d46fcc2011-12-19 18:01:05 -08004561 * @param child The child view whose static transform is being requested
4562 * @param t The Transformation which will hold the result
4563 * @return true if the transformation was set, false otherwise
Romain Guy8506ab42009-06-11 17:35:47 -07004564 * @see #setStaticTransformationsEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 */
4566 protected boolean getChildStaticTransformation(View child, Transformation t) {
4567 return false;
4568 }
4569
Romain Guyf6991302013-06-05 17:19:01 -07004570 Transformation getChildTransformation() {
4571 if (mChildTransformation == null) {
4572 mChildTransformation = new Transformation();
4573 }
4574 return mChildTransformation;
4575 }
4576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 /**
4578 * {@hide}
4579 */
4580 @Override
Alan Viverette8e1a7292017-02-27 10:57:58 -05004581 protected <T extends View> T findViewTraversal(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 if (id == mID) {
Alan Viverette8e1a7292017-02-27 10:57:58 -05004583 return (T) this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 }
4585
4586 final View[] where = mChildren;
4587 final int len = mChildrenCount;
4588
4589 for (int i = 0; i < len; i++) {
4590 View v = where[i];
4591
Dianne Hackborn4702a852012-08-17 15:18:29 -07004592 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 v = v.findViewById(id);
4594
4595 if (v != null) {
Alan Viverette8e1a7292017-02-27 10:57:58 -05004596 return (T) v;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 }
4598 }
4599 }
4600
4601 return null;
4602 }
4603
4604 /**
4605 * {@hide}
4606 */
4607 @Override
Alan Viverette8e1a7292017-02-27 10:57:58 -05004608 protected <T extends View> T findViewWithTagTraversal(Object tag) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 if (tag != null && tag.equals(mTag)) {
Alan Viverette8e1a7292017-02-27 10:57:58 -05004610 return (T) this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 }
4612
4613 final View[] where = mChildren;
4614 final int len = mChildrenCount;
4615
4616 for (int i = 0; i < len; i++) {
4617 View v = where[i];
4618
Dianne Hackborn4702a852012-08-17 15:18:29 -07004619 if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 v = v.findViewWithTag(tag);
4621
4622 if (v != null) {
Alan Viverette8e1a7292017-02-27 10:57:58 -05004623 return (T) v;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 }
4625 }
4626 }
4627
4628 return null;
4629 }
4630
4631 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004632 * {@hide}
4633 */
4634 @Override
Alan Viverette8e1a7292017-02-27 10:57:58 -05004635 protected <T extends View> T findViewByPredicateTraversal(Predicate<View> predicate,
4636 View childToSkip) {
Paul Duffinca4964c2017-02-07 15:04:10 +00004637 if (predicate.test(this)) {
Alan Viverette8e1a7292017-02-27 10:57:58 -05004638 return (T) this;
Jeff Brown4e6319b2010-12-13 10:36:51 -08004639 }
4640
4641 final View[] where = mChildren;
4642 final int len = mChildrenCount;
4643
4644 for (int i = 0; i < len; i++) {
4645 View v = where[i];
4646
Dianne Hackborn4702a852012-08-17 15:18:29 -07004647 if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08004648 v = v.findViewByPredicate(predicate);
4649
4650 if (v != null) {
Alan Viverette8e1a7292017-02-27 10:57:58 -05004651 return (T) v;
Jeff Brown4e6319b2010-12-13 10:36:51 -08004652 }
4653 }
4654 }
4655
4656 return null;
4657 }
4658
4659 /**
Chet Haasec633d2f2015-04-07 10:29:39 -07004660 * This method adds a view to this container at the specified index purely for the
4661 * purposes of allowing that view to draw even though it is not a normal child of
4662 * the container. That is, the view does not participate in layout, focus, accessibility,
4663 * input, or other normal view operations; it is purely an item to be drawn during the normal
4664 * rendering operation of this container. The index that it is added at is the order
4665 * in which it will be drawn, with respect to the other views in the container.
4666 * For example, a transient view added at index 0 will be drawn before all other views
4667 * in the container because it will be drawn first (including before any real view
4668 * at index 0). There can be more than one transient view at any particular index;
4669 * these views will be drawn in the order in which they were added to the list of
4670 * transient views. The index of transient views can also be greater than the number
4671 * of normal views in the container; that just means that they will be drawn after all
4672 * other views are drawn.
4673 *
4674 * <p>Note that since transient views do not participate in layout, they must be sized
4675 * manually or, more typically, they should just use the size that they had before they
4676 * were removed from their container.</p>
4677 *
4678 * <p>Transient views are useful for handling animations of views that have been removed
4679 * from the container, but which should be animated out after the removal. Adding these
4680 * views as transient views allows them to participate in drawing without side-effecting
4681 * the layout of the container.</p>
4682 *
4683 * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed}
4684 * from the container when they are no longer needed. For example, a transient view
4685 * which is added in order to fade it out in its old location should be removed
4686 * once the animation is complete.</p>
4687 *
Riddle Hsu95459d32018-06-08 15:54:08 +08004688 * @param view The view to be added. The view must not have a parent.
Chet Haasec633d2f2015-04-07 10:29:39 -07004689 * @param index The index at which this view should be drawn, must be >= 0.
4690 * This value is relative to the {@link #getChildAt(int) index} values in the normal
4691 * child list of this container, where any transient view at a particular index will
4692 * be drawn before any normal child at that same index.
Chris Craik66b41392015-04-17 10:08:10 -07004693 *
4694 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004695 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01004696 @UnsupportedAppUsage
Chet Haasec633d2f2015-04-07 10:29:39 -07004697 public void addTransientView(View view, int index) {
Riddle Hsu95459d32018-06-08 15:54:08 +08004698 if (index < 0 || view == null) {
Chet Haasec633d2f2015-04-07 10:29:39 -07004699 return;
4700 }
Riddle Hsu95459d32018-06-08 15:54:08 +08004701 if (view.mParent != null) {
4702 throw new IllegalStateException("The specified view already has a parent "
4703 + view.mParent);
4704 }
4705
Chet Haasec633d2f2015-04-07 10:29:39 -07004706 if (mTransientIndices == null) {
4707 mTransientIndices = new ArrayList<Integer>();
4708 mTransientViews = new ArrayList<View>();
4709 }
4710 final int oldSize = mTransientIndices.size();
4711 if (oldSize > 0) {
4712 int insertionIndex;
4713 for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) {
4714 if (index < mTransientIndices.get(insertionIndex)) {
4715 break;
4716 }
4717 }
4718 mTransientIndices.add(insertionIndex, index);
4719 mTransientViews.add(insertionIndex, view);
4720 } else {
4721 mTransientIndices.add(index);
4722 mTransientViews.add(view);
4723 }
4724 view.mParent = this;
Riddle Hsu95459d32018-06-08 15:54:08 +08004725 if (mAttachInfo != null) {
4726 view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags & VISIBILITY_MASK));
4727 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004728 invalidate(true);
4729 }
4730
4731 /**
4732 * Removes a view from the list of transient views in this container. If there is no
4733 * such transient view, this method does nothing.
4734 *
4735 * @param view The transient view to be removed
Chris Craik66b41392015-04-17 10:08:10 -07004736 *
4737 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004738 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01004739 @UnsupportedAppUsage
Chet Haasec633d2f2015-04-07 10:29:39 -07004740 public void removeTransientView(View view) {
4741 if (mTransientViews == null) {
4742 return;
4743 }
4744 final int size = mTransientViews.size();
4745 for (int i = 0; i < size; ++i) {
4746 if (view == mTransientViews.get(i)) {
4747 mTransientViews.remove(i);
4748 mTransientIndices.remove(i);
4749 view.mParent = null;
Riddle Hsu95459d32018-06-08 15:54:08 +08004750 if (view.mAttachInfo != null) {
4751 view.dispatchDetachedFromWindow();
4752 }
Chet Haasec633d2f2015-04-07 10:29:39 -07004753 invalidate(true);
4754 return;
4755 }
4756 }
4757 }
4758
4759 /**
4760 * Returns the number of transient views in this container. Specific transient
4761 * views and the index at which they were added can be retrieved via
4762 * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}.
4763 *
4764 * @see #addTransientView(View, int)
4765 * @return The number of transient views in this container
Chris Craik66b41392015-04-17 10:08:10 -07004766 *
4767 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004768 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01004769 @UnsupportedAppUsage
Chet Haasec633d2f2015-04-07 10:29:39 -07004770 public int getTransientViewCount() {
4771 return mTransientIndices == null ? 0 : mTransientIndices.size();
4772 }
4773
4774 /**
4775 * Given a valid position within the list of transient views, returns the index of
4776 * the transient view at that position.
4777 *
4778 * @param position The position of the index being queried. Must be at least 0
4779 * and less than the value returned by {@link #getTransientViewCount()}.
4780 * @return The index of the transient view stored in the given position if the
4781 * position is valid, otherwise -1
Chris Craik66b41392015-04-17 10:08:10 -07004782 *
4783 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004784 */
4785 public int getTransientViewIndex(int position) {
4786 if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) {
4787 return -1;
4788 }
4789 return mTransientIndices.get(position);
4790 }
4791
4792 /**
4793 * Given a valid position within the list of transient views, returns the
4794 * transient view at that position.
4795 *
4796 * @param position The position of the view being queried. Must be at least 0
4797 * and less than the value returned by {@link #getTransientViewCount()}.
4798 * @return The transient view stored in the given position if the
4799 * position is valid, otherwise null
Chris Craik66b41392015-04-17 10:08:10 -07004800 *
4801 * @hide
Chet Haasec633d2f2015-04-07 10:29:39 -07004802 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01004803 @UnsupportedAppUsage
Chet Haasec633d2f2015-04-07 10:29:39 -07004804 public View getTransientView(int position) {
4805 if (mTransientViews == null || position >= mTransientViews.size()) {
4806 return null;
4807 }
4808 return mTransientViews.get(position);
4809 }
4810
4811 /**
Romain Guy393a52c2012-05-22 20:21:08 -07004812 * <p>Adds a child view. If no layout parameters are already set on the child, the
4813 * default parameters for this ViewGroup are set on the child.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004814 *
Romain Guy393a52c2012-05-22 20:21:08 -07004815 * <p><strong>Note:</strong> do not invoke this method from
4816 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4817 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 *
4819 * @param child the child view to add
4820 *
4821 * @see #generateDefaultLayoutParams()
4822 */
4823 public void addView(View child) {
4824 addView(child, -1);
4825 }
4826
4827 /**
4828 * Adds a child view. If no layout parameters are already set on the child, the
4829 * default parameters for this ViewGroup are set on the child.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08004830 *
Romain Guy393a52c2012-05-22 20:21:08 -07004831 * <p><strong>Note:</strong> do not invoke this method from
4832 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4833 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 *
4835 * @param child the child view to add
4836 * @param index the position at which to add the child
4837 *
4838 * @see #generateDefaultLayoutParams()
4839 */
4840 public void addView(View child, int index) {
Adam Powell45a9da52014-10-09 09:44:18 -07004841 if (child == null) {
4842 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 LayoutParams params = child.getLayoutParams();
4845 if (params == null) {
4846 params = generateDefaultLayoutParams();
4847 if (params == null) {
4848 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
4849 }
4850 }
4851 addView(child, index, params);
4852 }
4853
4854 /**
4855 * Adds a child view with this ViewGroup's default layout parameters and the
4856 * specified width and height.
4857 *
Romain Guy393a52c2012-05-22 20:21:08 -07004858 * <p><strong>Note:</strong> do not invoke this method from
4859 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4860 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4861 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 * @param child the child view to add
4863 */
4864 public void addView(View child, int width, int height) {
4865 final LayoutParams params = generateDefaultLayoutParams();
4866 params.width = width;
4867 params.height = height;
4868 addView(child, -1, params);
4869 }
4870
4871 /**
4872 * Adds a child view with the specified layout parameters.
4873 *
Romain Guy393a52c2012-05-22 20:21:08 -07004874 * <p><strong>Note:</strong> do not invoke this method from
4875 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4876 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 * @param child the child view to add
4879 * @param params the layout parameters to set on the child
4880 */
Alan Viverettebe463f22016-01-21 10:50:10 -05004881 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 public void addView(View child, LayoutParams params) {
4883 addView(child, -1, params);
4884 }
4885
4886 /**
4887 * Adds a child view with the specified layout parameters.
4888 *
Romain Guy393a52c2012-05-22 20:21:08 -07004889 * <p><strong>Note:</strong> do not invoke this method from
4890 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
4891 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
4892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004893 * @param child the child view to add
Alan Viverette77bb6f12015-02-11 17:24:33 -08004894 * @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 -08004895 * @param params the layout parameters to set on the child
4896 */
4897 public void addView(View child, int index, LayoutParams params) {
4898 if (DBG) {
4899 System.out.println(this + " addView");
4900 }
4901
Adam Powell45a9da52014-10-09 09:44:18 -07004902 if (child == null) {
4903 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
4904 }
4905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 // addViewInner() will call child.requestLayout() when setting the new LayoutParams
4907 // therefore, we call requestLayout() on ourselves before, so that the child's request
4908 // will be blocked at our level
4909 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08004910 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 addViewInner(child, index, params, false);
4912 }
4913
Alan Viverettebe463f22016-01-21 10:50:10 -05004914 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
4916 if (!checkLayoutParams(params)) {
4917 throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this);
4918 }
4919 if (view.mParent != this) {
4920 throw new IllegalArgumentException("Given view not a child of " + this);
4921 }
4922 view.setLayoutParams(params);
4923 }
4924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
4926 return p != null;
4927 }
4928
4929 /**
4930 * Interface definition for a callback to be invoked when the hierarchy
4931 * within this view changed. The hierarchy changes whenever a child is added
4932 * to or removed from this view.
4933 */
4934 public interface OnHierarchyChangeListener {
4935 /**
4936 * Called when a new child is added to a parent view.
4937 *
4938 * @param parent the view in which a child was added
4939 * @param child the new child view added in the hierarchy
4940 */
4941 void onChildViewAdded(View parent, View child);
4942
4943 /**
4944 * Called when a child is removed from a parent view.
4945 *
4946 * @param parent the view from which the child was removed
4947 * @param child the child removed from the hierarchy
4948 */
4949 void onChildViewRemoved(View parent, View child);
4950 }
4951
4952 /**
4953 * Register a callback to be invoked when a child is added to or removed
4954 * from this view.
4955 *
4956 * @param listener the callback to invoke on hierarchy change
4957 */
4958 public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
4959 mOnHierarchyChangeListener = listener;
4960 }
4961
Mathew Inwooda570dee2018-08-17 14:56:00 +01004962 @UnsupportedAppUsage
Adam Powell6690d012015-06-17 16:41:56 -07004963 void dispatchViewAdded(View child) {
4964 onViewAdded(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004965 if (mOnHierarchyChangeListener != null) {
4966 mOnHierarchyChangeListener.onChildViewAdded(this, child);
4967 }
4968 }
4969
4970 /**
Adam Powell6690d012015-06-17 16:41:56 -07004971 * Called when a new child is added to this ViewGroup. Overrides should always
4972 * call super.onViewAdded.
4973 *
4974 * @param child the added child view
Philip Milnef51d91c2011-07-18 16:12:19 -07004975 */
Adam Powell6690d012015-06-17 16:41:56 -07004976 public void onViewAdded(View child) {
4977 }
4978
Mathew Inwooda570dee2018-08-17 14:56:00 +01004979 @UnsupportedAppUsage
Adam Powell6690d012015-06-17 16:41:56 -07004980 void dispatchViewRemoved(View child) {
4981 onViewRemoved(child);
Philip Milnef51d91c2011-07-18 16:12:19 -07004982 if (mOnHierarchyChangeListener != null) {
4983 mOnHierarchyChangeListener.onChildViewRemoved(this, child);
4984 }
4985 }
4986
Adam Powell6690d012015-06-17 16:41:56 -07004987 /**
4988 * Called when a child view is removed from this ViewGroup. Overrides should always
4989 * call super.onViewRemoved.
4990 *
4991 * @param child the removed child view
4992 */
4993 public void onViewRemoved(View child) {
4994 }
4995
Philip Milnecfb631b2012-10-26 10:51:46 -07004996 private void clearCachedLayoutMode() {
Svetoslav6254f482013-06-04 17:22:14 -07004997 if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnecfb631b2012-10-26 10:51:46 -07004998 mLayoutMode = LAYOUT_MODE_UNDEFINED;
4999 }
5000 }
5001
5002 @Override
5003 protected void onAttachedToWindow() {
5004 super.onAttachedToWindow();
5005 clearCachedLayoutMode();
5006 }
5007
5008 @Override
5009 protected void onDetachedFromWindow() {
5010 super.onDetachedFromWindow();
5011 clearCachedLayoutMode();
5012 }
5013
John Reck2de950d2017-01-25 10:58:30 -08005014 /** @hide */
5015 @Override
5016 protected void destroyHardwareResources() {
5017 super.destroyHardwareResources();
5018 int count = getChildCount();
5019 for (int i = 0; i < count; i++) {
5020 getChildAt(i).destroyHardwareResources();
5021 }
5022 }
5023
Philip Milnef51d91c2011-07-18 16:12:19 -07005024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 * Adds a view during layout. This is useful if in your onLayout() method,
5026 * you need to add more views (as does the list view for example).
5027 *
5028 * If index is negative, it means put it at the end of the list.
5029 *
5030 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08005031 * @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 -08005032 * @param params the layout parameters to associate with the child
5033 * @return true if the child was added, false otherwise
5034 */
5035 protected boolean addViewInLayout(View child, int index, LayoutParams params) {
5036 return addViewInLayout(child, index, params, false);
5037 }
5038
5039 /**
5040 * Adds a view during layout. This is useful if in your onLayout() method,
5041 * you need to add more views (as does the list view for example).
5042 *
5043 * If index is negative, it means put it at the end of the list.
5044 *
5045 * @param child the view to add to the group
Alan Viverette77bb6f12015-02-11 17:24:33 -08005046 * @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 -08005047 * @param params the layout parameters to associate with the child
5048 * @param preventRequestLayout if true, calling this method will not trigger a
5049 * layout request on child
5050 * @return true if the child was added, false otherwise
5051 */
5052 protected boolean addViewInLayout(View child, int index, LayoutParams params,
5053 boolean preventRequestLayout) {
Adam Powell45a9da52014-10-09 09:44:18 -07005054 if (child == null) {
5055 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
5056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 child.mParent = null;
5058 addViewInner(child, index, params, preventRequestLayout);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005059 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 return true;
5061 }
5062
5063 /**
5064 * Prevents the specified child to be laid out during the next layout pass.
5065 *
5066 * @param child the child on which to perform the cleanup
5067 */
5068 protected void cleanupLayoutState(View child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005069 child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
5071
5072 private void addViewInner(View child, int index, LayoutParams params,
5073 boolean preventRequestLayout) {
5074
Chet Haasee8e45d32011-03-02 17:07:35 -08005075 if (mTransition != null) {
5076 // Don't prevent other add transitions from completing, but cancel remove
5077 // transitions to let them complete the process before we add to the container
5078 mTransition.cancel(LayoutTransition.DISAPPEARING);
Chet Haaseadd65772011-02-09 16:47:29 -08005079 }
5080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 if (child.getParent() != null) {
5082 throw new IllegalStateException("The specified child already has a parent. " +
5083 "You must call removeView() on the child's parent first.");
5084 }
5085
Chet Haase21cd1382010-09-01 17:42:29 -07005086 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005087 mTransition.addChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07005088 }
5089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 if (!checkLayoutParams(params)) {
5091 params = generateLayoutParams(params);
5092 }
5093
5094 if (preventRequestLayout) {
5095 child.mLayoutParams = params;
5096 } else {
5097 child.setLayoutParams(params);
5098 }
5099
5100 if (index < 0) {
5101 index = mChildrenCount;
5102 }
5103
5104 addInArray(child, index);
5105
5106 // tell our children
5107 if (preventRequestLayout) {
5108 child.assignParent(this);
5109 } else {
5110 child.mParent = this;
Evan Roskycd80e612018-05-17 17:46:09 -07005111 }
5112 if (child.hasUnhandledKeyListener()) {
5113 incrementChildUnhandledKeyListeners();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 }
5115
Evan Rosky2ae1bf52017-05-11 11:18:45 -07005116 final boolean childHasFocus = child.hasFocus();
5117 if (childHasFocus) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 requestChildFocus(child, child.findFocus());
5119 }
Romain Guy8506ab42009-06-11 17:35:47 -07005120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 AttachInfo ai = mAttachInfo;
Adam Powell4b867882011-09-16 12:59:46 -07005122 if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) {
Romain Guy8506ab42009-06-11 17:35:47 -07005123 boolean lastKeepOn = ai.mKeepScreenOn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 ai.mKeepScreenOn = false;
5125 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
5126 if (ai.mKeepScreenOn) {
5127 needGlobalAttributesUpdate(true);
5128 }
5129 ai.mKeepScreenOn = lastKeepOn;
5130 }
5131
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005132 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglioa7e0bcd2012-10-16 19:55:01 -07005133 child.resetRtlProperties();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005134 }
5135
Adam Powell6690d012015-06-17 16:41:56 -07005136 dispatchViewAdded(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137
5138 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
5139 mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;
5140 }
Adam Powell539ee872012-02-03 19:00:49 -08005141
5142 if (child.hasTransientState()) {
5143 childHasTransientStateChanged(child, true);
5144 }
Svetoslav6254f482013-06-04 17:22:14 -07005145
Svetoslav8e3feb12014-02-24 13:46:47 -08005146 if (child.getVisibility() != View.GONE) {
Eugene Susla72c510f2018-01-23 21:12:11 +00005147 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07005148 }
Chet Haasec633d2f2015-04-07 10:29:39 -07005149
5150 if (mTransientIndices != null) {
5151 final int transientCount = mTransientIndices.size();
5152 for (int i = 0; i < transientCount; ++i) {
5153 final int oldIndex = mTransientIndices.get(i);
5154 if (index <= oldIndex) {
5155 mTransientIndices.set(i, oldIndex + 1);
5156 }
5157 }
5158 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07005159
5160 if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) {
5161 notifyChildOfDragStart(child);
5162 }
Vadim Tryshev5ca73982017-01-04 17:24:43 -08005163
5164 if (child.hasDefaultFocus()) {
5165 // When adding a child that contains default focus, either during inflation or while
5166 // manually assembling the hierarchy, update the ancestor default-focus chain.
5167 setDefaultFocus(child);
5168 }
Svetoslav Ganov24c90452017-12-27 15:17:14 -08005169
5170 touchAccessibilityNodeProviderIfNeeded(child);
5171 }
5172
5173 /**
5174 * We may need to touch the provider to bring up the a11y layer. In a11y mode
5175 * clients inspect the screen or the user touches it which triggers bringing up
5176 * of the a11y infrastructure while in autofill mode we want the infra up and
5177 * running from the beginning since we watch for a11y events to drive autofill.
5178 */
5179 private void touchAccessibilityNodeProviderIfNeeded(View child) {
5180 if (mContext.isAutofillCompatibilityEnabled()) {
5181 child.getAccessibilityNodeProvider();
5182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 }
5184
5185 private void addInArray(View child, int index) {
5186 View[] children = mChildren;
5187 final int count = mChildrenCount;
5188 final int size = children.length;
5189 if (index == count) {
5190 if (size == count) {
5191 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
5192 System.arraycopy(children, 0, mChildren, 0, size);
5193 children = mChildren;
5194 }
5195 children[mChildrenCount++] = child;
5196 } else if (index < count) {
5197 if (size == count) {
5198 mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
5199 System.arraycopy(children, 0, mChildren, 0, index);
5200 System.arraycopy(children, index, mChildren, index + 1, count - index);
5201 children = mChildren;
5202 } else {
5203 System.arraycopy(children, index, children, index + 1, count - index);
5204 }
5205 children[index] = child;
5206 mChildrenCount++;
Joe Onorato03ab0c72011-01-06 15:46:27 -08005207 if (mLastTouchDownIndex >= index) {
5208 mLastTouchDownIndex++;
5209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 } else {
5211 throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
5212 }
5213 }
5214
5215 // This method also sets the child's mParent to null
5216 private void removeFromArray(int index) {
5217 final View[] children = mChildren;
Chet Haase21cd1382010-09-01 17:42:29 -07005218 if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
5219 children[index].mParent = null;
5220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 final int count = mChildrenCount;
5222 if (index == count - 1) {
5223 children[--mChildrenCount] = null;
5224 } else if (index >= 0 && index < count) {
5225 System.arraycopy(children, index + 1, children, index, count - index - 1);
5226 children[--mChildrenCount] = null;
5227 } else {
5228 throw new IndexOutOfBoundsException();
5229 }
Joe Onorato03ab0c72011-01-06 15:46:27 -08005230 if (mLastTouchDownIndex == index) {
5231 mLastTouchDownTime = 0;
5232 mLastTouchDownIndex = -1;
5233 } else if (mLastTouchDownIndex > index) {
5234 mLastTouchDownIndex--;
5235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237
5238 // This method also sets the children's mParent to null
5239 private void removeFromArray(int start, int count) {
5240 final View[] children = mChildren;
5241 final int childrenCount = mChildrenCount;
5242
5243 start = Math.max(0, start);
5244 final int end = Math.min(childrenCount, start + count);
5245
5246 if (start == end) {
5247 return;
5248 }
5249
5250 if (end == childrenCount) {
5251 for (int i = start; i < end; i++) {
5252 children[i].mParent = null;
5253 children[i] = null;
5254 }
5255 } else {
5256 for (int i = start; i < end; i++) {
5257 children[i].mParent = null;
5258 }
5259
5260 // Since we're looping above, we might as well do the copy, but is arraycopy()
5261 // faster than the extra 2 bounds checks we would do in the loop?
5262 System.arraycopy(children, end, children, start, childrenCount - end);
5263
5264 for (int i = childrenCount - (end - start); i < childrenCount; i++) {
5265 children[i] = null;
5266 }
5267 }
5268
5269 mChildrenCount -= (end - start);
5270 }
5271
5272 private void bindLayoutAnimation(View child) {
5273 Animation a = mLayoutAnimationController.getAnimationForView(child);
5274 child.setAnimation(a);
5275 }
5276
5277 /**
5278 * Subclasses should override this method to set layout animation
5279 * parameters on the supplied child.
5280 *
5281 * @param child the child to associate with animation parameters
5282 * @param params the child's layout parameters which hold the animation
5283 * parameters
5284 * @param index the index of the child in the view group
5285 * @param count the number of children in the view group
5286 */
5287 protected void attachLayoutAnimationParameters(View child,
5288 LayoutParams params, int index, int count) {
5289 LayoutAnimationController.AnimationParameters animationParams =
5290 params.layoutAnimationParameters;
5291 if (animationParams == null) {
5292 animationParams = new LayoutAnimationController.AnimationParameters();
5293 params.layoutAnimationParameters = animationParams;
5294 }
5295
5296 animationParams.count = count;
5297 animationParams.index = index;
5298 }
5299
5300 /**
5301 * {@inheritDoc}
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08005302 *
Romain Guy393a52c2012-05-22 20:21:08 -07005303 * <p><strong>Note:</strong> do not invoke this method from
5304 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5305 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 */
Alan Viverettebe463f22016-01-21 10:50:10 -05005307 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 public void removeView(View view) {
Alan Viverette177ec4602014-10-17 13:34:50 -07005309 if (removeViewInternal(view)) {
5310 requestLayout();
5311 invalidate(true);
5312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 }
5314
5315 /**
5316 * Removes a view during layout. This is useful if in your onLayout() method,
5317 * you need to remove more views.
5318 *
Romain Guy393a52c2012-05-22 20:21:08 -07005319 * <p><strong>Note:</strong> do not invoke this method from
5320 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5321 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08005322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 * @param view the view to remove from the group
5324 */
5325 public void removeViewInLayout(View view) {
5326 removeViewInternal(view);
5327 }
5328
5329 /**
5330 * Removes a range of views during layout. This is useful if in your onLayout() method,
5331 * you need to remove more views.
5332 *
Romain Guy393a52c2012-05-22 20:21:08 -07005333 * <p><strong>Note:</strong> do not invoke this method from
5334 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5335 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
5336 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 * @param start the index of the first view to remove from the group
5338 * @param count the number of views to remove from the group
5339 */
5340 public void removeViewsInLayout(int start, int count) {
5341 removeViewsInternal(start, count);
5342 }
5343
5344 /**
5345 * Removes the view at the specified position in the group.
5346 *
Romain Guy393a52c2012-05-22 20:21:08 -07005347 * <p><strong>Note:</strong> do not invoke this method from
5348 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5349 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08005350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 * @param index the position in the group of the view to remove
5352 */
5353 public void removeViewAt(int index) {
5354 removeViewInternal(index, getChildAt(index));
5355 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08005356 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 }
5358
5359 /**
5360 * Removes the specified range of views from the group.
5361 *
Romain Guy393a52c2012-05-22 20:21:08 -07005362 * <p><strong>Note:</strong> do not invoke this method from
5363 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5364 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
5365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 * @param start the first position in the group of the range of views to remove
5367 * @param count the number of views to remove
5368 */
5369 public void removeViews(int start, int count) {
5370 removeViewsInternal(start, count);
5371 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08005372 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 }
5374
Alan Viverette177ec4602014-10-17 13:34:50 -07005375 private boolean removeViewInternal(View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 final int index = indexOfChild(view);
5377 if (index >= 0) {
5378 removeViewInternal(index, view);
Alan Viverette177ec4602014-10-17 13:34:50 -07005379 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 }
Alan Viverette177ec4602014-10-17 13:34:50 -07005381 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 }
5383
5384 private void removeViewInternal(int index, View view) {
Chet Haase21cd1382010-09-01 17:42:29 -07005385 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005386 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07005387 }
5388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 boolean clearChildFocus = false;
5390 if (view == mFocused) {
Alan Viverette223622a2013-12-17 13:29:02 -08005391 view.unFocus(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 clearChildFocus = true;
5393 }
Evan Rosky53fcf112017-01-26 14:37:55 -08005394 if (view == mFocusedInCluster) {
Evan Rosky0e8a6832017-04-10 12:35:15 -07005395 clearFocusedInCluster(view);
Vadim Tryshev01d8c492016-12-15 11:33:15 -08005396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397
Alan Viverette632af842014-10-28 13:45:11 -07005398 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07005399
Jeff Brown59a422e2012-04-19 15:19:19 -07005400 cancelTouchTarget(view);
5401 cancelHoverTarget(view);
5402
Chet Haase21cd1382010-09-01 17:42:29 -07005403 if (view.getAnimation() != null ||
5404 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 addDisappearingView(view);
5406 } else if (view.mAttachInfo != null) {
5407 view.dispatchDetachedFromWindow();
5408 }
5409
Adam Powell539ee872012-02-03 19:00:49 -08005410 if (view.hasTransientState()) {
5411 childHasTransientStateChanged(view, false);
5412 }
5413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07005415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 removeFromArray(index);
5417
Evan Rosky4807ae22018-03-22 16:04:15 -07005418 if (view.hasUnhandledKeyListener()) {
5419 decrementChildUnhandledKeyListeners();
5420 }
5421
Evan Rosky53fcf112017-01-26 14:37:55 -08005422 if (view == mDefaultFocus) {
5423 clearDefaultFocus(view);
5424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 if (clearChildFocus) {
5426 clearChildFocus(view);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005427 if (!rootViewRequestFocus()) {
5428 notifyGlobalFocusCleared(this);
5429 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005430 }
Romain Guy6fb05632012-11-29 10:50:33 -08005431
Adam Powell6690d012015-06-17 16:41:56 -07005432 dispatchViewRemoved(view);
Svetoslav6254f482013-06-04 17:22:14 -07005433
Svetoslav8e3feb12014-02-24 13:46:47 -08005434 if (view.getVisibility() != View.GONE) {
Eugene Susla72c510f2018-01-23 21:12:11 +00005435 notifySubtreeAccessibilityStateChangedIfNeeded();
Svetoslav6254f482013-06-04 17:22:14 -07005436 }
Chet Haasec633d2f2015-04-07 10:29:39 -07005437
5438 int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();
5439 for (int i = 0; i < transientCount; ++i) {
5440 final int oldIndex = mTransientIndices.get(i);
5441 if (index < oldIndex) {
5442 mTransientIndices.set(i, oldIndex - 1);
5443 }
5444 }
Vadim Tryshev02ed4a02015-10-23 17:39:33 -07005445
5446 if (mCurrentDragStartEvent != null) {
5447 mChildrenInterestedInDrag.remove(view);
5448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 }
5450
Chet Haase21cd1382010-09-01 17:42:29 -07005451 /**
5452 * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
5453 * not null, changes in layout which occur because of children being added to or removed from
5454 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
5455 * object. By default, the transition object is null (so layout changes are not animated).
5456 *
Chet Haaseef3cbfd2013-08-21 14:01:02 -07005457 * <p>Replacing a non-null transition will cause that previous transition to be
5458 * canceled, if it is currently running, to restore this container to
5459 * its correct post-transition state.</p>
5460 *
Chet Haase21cd1382010-09-01 17:42:29 -07005461 * @param transition The LayoutTransition object that will animated changes in layout. A value
5462 * of <code>null</code> means no transition will run on layout changes.
Chet Haase13cc1202010-09-03 15:39:20 -07005463 * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges
Chet Haase21cd1382010-09-01 17:42:29 -07005464 */
5465 public void setLayoutTransition(LayoutTransition transition) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07005466 if (mTransition != null) {
Chet Haasefee6f2b2013-08-27 12:22:29 -07005467 LayoutTransition previousTransition = mTransition;
5468 previousTransition.cancel();
5469 previousTransition.removeTransitionListener(mLayoutTransitionListener);
Chet Haaseb20db3e2010-09-10 13:07:30 -07005470 }
Chet Haase21cd1382010-09-01 17:42:29 -07005471 mTransition = transition;
Chet Haase13cc1202010-09-03 15:39:20 -07005472 if (mTransition != null) {
5473 mTransition.addTransitionListener(mLayoutTransitionListener);
5474 }
5475 }
5476
5477 /**
5478 * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is
5479 * not null, changes in layout which occur because of children being added to or removed from
5480 * the ViewGroup will be animated according to the animations defined in that LayoutTransition
5481 * object. By default, the transition object is null (so layout changes are not animated).
5482 *
5483 * @return LayoutTranstion The LayoutTransition object that will animated changes in layout.
5484 * A value of <code>null</code> means no transition will run on layout changes.
5485 */
5486 public LayoutTransition getLayoutTransition() {
5487 return mTransition;
Chet Haase21cd1382010-09-01 17:42:29 -07005488 }
5489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 private void removeViewsInternal(int start, int count) {
Chris Craik18a759d2015-10-14 14:16:33 -07005491 final int end = start + count;
5492
5493 if (start < 0 || count < 0 || end > mChildrenCount) {
5494 throw new IndexOutOfBoundsException();
5495 }
5496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 final View focused = mFocused;
5498 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005499 boolean clearChildFocus = false;
Evan Rosky53fcf112017-01-26 14:37:55 -08005500 View clearDefaultFocus = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501
5502 final View[] children = mChildren;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503
5504 for (int i = start; i < end; i++) {
5505 final View view = children[i];
5506
Chet Haase21cd1382010-09-01 17:42:29 -07005507 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005508 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07005509 }
5510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005511 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08005512 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005513 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 }
Vadim Tryshev5ca73982017-01-04 17:24:43 -08005515 if (view == mDefaultFocus) {
Evan Rosky53fcf112017-01-26 14:37:55 -08005516 clearDefaultFocus = view;
5517 }
5518 if (view == mFocusedInCluster) {
Evan Rosky0e8a6832017-04-10 12:35:15 -07005519 clearFocusedInCluster(view);
Vadim Tryshev01d8c492016-12-15 11:33:15 -08005520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521
Alan Viverette632af842014-10-28 13:45:11 -07005522 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07005523
Jeff Brown59a422e2012-04-19 15:19:19 -07005524 cancelTouchTarget(view);
5525 cancelHoverTarget(view);
5526
Chet Haase21cd1382010-09-01 17:42:29 -07005527 if (view.getAnimation() != null ||
5528 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 addDisappearingView(view);
5530 } else if (detach) {
5531 view.dispatchDetachedFromWindow();
5532 }
5533
Adam Powell539ee872012-02-03 19:00:49 -08005534 if (view.hasTransientState()) {
5535 childHasTransientStateChanged(view, false);
5536 }
5537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07005539
Adam Powell6690d012015-06-17 16:41:56 -07005540 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 }
5542
5543 removeFromArray(start, count);
5544
Evan Rosky53fcf112017-01-26 14:37:55 -08005545 if (clearDefaultFocus != null) {
5546 clearDefaultFocus(clearDefaultFocus);
5547 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005548 if (clearChildFocus) {
5549 clearChildFocus(focused);
5550 if (!rootViewRequestFocus()) {
5551 notifyGlobalFocusCleared(focused);
5552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 }
5554 }
5555
5556 /**
5557 * Call this method to remove all child views from the
5558 * ViewGroup.
Filip Gruszczynskia33bdf32015-11-19 18:22:16 -08005559 *
Romain Guy393a52c2012-05-22 20:21:08 -07005560 * <p><strong>Note:</strong> do not invoke this method from
5561 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5562 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 */
5564 public void removeAllViews() {
5565 removeAllViewsInLayout();
5566 requestLayout();
Romain Guy849d0a32011-02-01 17:20:48 -08005567 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 }
5569
5570 /**
5571 * Called by a ViewGroup subclass to remove child views from itself,
5572 * when it must first know its size on screen before it can calculate how many
5573 * child views it will render. An example is a Gallery or a ListView, which
5574 * may "have" 50 children, but actually only render the number of children
5575 * that can currently fit inside the object on screen. Do not call
5576 * this method unless you are extending ViewGroup and understand the
5577 * view measuring and layout pipeline.
Romain Guy393a52c2012-05-22 20:21:08 -07005578 *
5579 * <p><strong>Note:</strong> do not invoke this method from
5580 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},
5581 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 */
5583 public void removeAllViewsInLayout() {
5584 final int count = mChildrenCount;
5585 if (count <= 0) {
5586 return;
5587 }
5588
5589 final View[] children = mChildren;
5590 mChildrenCount = 0;
5591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 final View focused = mFocused;
5593 final boolean detach = mAttachInfo != null;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005594 boolean clearChildFocus = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595
5596 needGlobalAttributesUpdate(false);
Romain Guy8506ab42009-06-11 17:35:47 -07005597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 for (int i = count - 1; i >= 0; i--) {
5599 final View view = children[i];
5600
Chet Haase21cd1382010-09-01 17:42:29 -07005601 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005602 mTransition.removeChild(this, view);
Chet Haase21cd1382010-09-01 17:42:29 -07005603 }
5604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 if (view == focused) {
Alan Viverette223622a2013-12-17 13:29:02 -08005606 view.unFocus(null);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005607 clearChildFocus = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 }
5609
Alan Viverette632af842014-10-28 13:45:11 -07005610 view.clearAccessibilityFocus();
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07005611
Jeff Brown59a422e2012-04-19 15:19:19 -07005612 cancelTouchTarget(view);
5613 cancelHoverTarget(view);
5614
Chet Haase21cd1382010-09-01 17:42:29 -07005615 if (view.getAnimation() != null ||
5616 (mTransitioningViews != null && mTransitioningViews.contains(view))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 addDisappearingView(view);
5618 } else if (detach) {
5619 view.dispatchDetachedFromWindow();
5620 }
5621
Adam Powell539ee872012-02-03 19:00:49 -08005622 if (view.hasTransientState()) {
5623 childHasTransientStateChanged(view, false);
5624 }
5625
Adam Powell6690d012015-06-17 16:41:56 -07005626 dispatchViewRemoved(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627
5628 view.mParent = null;
5629 children[i] = null;
5630 }
5631
Evan Rosky53fcf112017-01-26 14:37:55 -08005632 if (mDefaultFocus != null) {
5633 clearDefaultFocus(mDefaultFocus);
5634 }
Evan Rosky776fa5f2017-04-26 16:47:54 -07005635 if (mFocusedInCluster != null) {
5636 clearFocusedInCluster(mFocusedInCluster);
5637 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08005638 if (clearChildFocus) {
5639 clearChildFocus(focused);
5640 if (!rootViewRequestFocus()) {
5641 notifyGlobalFocusCleared(focused);
5642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 }
5644 }
5645
5646 /**
5647 * Finishes the removal of a detached view. This method will dispatch the detached from
5648 * window event and notify the hierarchy change listener.
Chet Haaseca479d42012-08-30 17:20:08 -07005649 * <p>
5650 * This method is intended to be lightweight and makes no assumptions about whether the
5651 * parent or child should be redrawn. Proper use of this method will include also making
5652 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
5653 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
5654 * which performs a {@link #requestLayout()} on the next frame, after all detach/remove
5655 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 *
5657 * @param child the child to be definitely removed from the view hierarchy
5658 * @param animate if true and the view has an animation, the view is placed in the
5659 * disappearing views list, otherwise, it is detached from the window
5660 *
5661 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5662 * @see #detachAllViewsFromParent()
5663 * @see #detachViewFromParent(View)
5664 * @see #detachViewFromParent(int)
5665 */
5666 protected void removeDetachedView(View child, boolean animate) {
Chet Haase21cd1382010-09-01 17:42:29 -07005667 if (mTransition != null) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005668 mTransition.removeChild(this, child);
Chet Haase21cd1382010-09-01 17:42:29 -07005669 }
5670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 if (child == mFocused) {
5672 child.clearFocus();
5673 }
Vadim Tryshev5ca73982017-01-04 17:24:43 -08005674 if (child == mDefaultFocus) {
Evan Rosky53fcf112017-01-26 14:37:55 -08005675 clearDefaultFocus(child);
5676 }
5677 if (child == mFocusedInCluster) {
Evan Rosky0e8a6832017-04-10 12:35:15 -07005678 clearFocusedInCluster(child);
Vadim Tryshev01d8c492016-12-15 11:33:15 -08005679 }
Romain Guy8506ab42009-06-11 17:35:47 -07005680
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07005681 child.clearAccessibilityFocus();
5682
Jeff Brown59a422e2012-04-19 15:19:19 -07005683 cancelTouchTarget(child);
5684 cancelHoverTarget(child);
5685
Chet Haase21cd1382010-09-01 17:42:29 -07005686 if ((animate && child.getAnimation() != null) ||
5687 (mTransitioningViews != null && mTransitioningViews.contains(child))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 addDisappearingView(child);
5689 } else if (child.mAttachInfo != null) {
5690 child.dispatchDetachedFromWindow();
5691 }
5692
Adam Powell539ee872012-02-03 19:00:49 -08005693 if (child.hasTransientState()) {
5694 childHasTransientStateChanged(child, false);
5695 }
5696
Adam Powell6690d012015-06-17 16:41:56 -07005697 dispatchViewRemoved(child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 }
5699
5700 /**
5701 * Attaches a view to this view group. Attaching a view assigns this group as the parent,
Chet Haaseca479d42012-08-30 17:20:08 -07005702 * sets the layout parameters and puts the view in the list of children so that
5703 * it can be retrieved by calling {@link #getChildAt(int)}.
5704 * <p>
5705 * This method is intended to be lightweight and makes no assumptions about whether the
5706 * parent or child should be redrawn. Proper use of this method will include also making
5707 * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls.
5708 * For example, callers can {@link #post(Runnable) post} a {@link Runnable}
5709 * which performs a {@link #requestLayout()} on the next frame, after all detach/attach
5710 * calls are finished, causing layout to be run prior to redrawing the view hierarchy.
5711 * <p>
5712 * This method should be called only for views which were detached from their parent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 *
5714 * @param child the child to attach
5715 * @param index the index at which the child should be attached
5716 * @param params the layout parameters of the child
5717 *
5718 * @see #removeDetachedView(View, boolean)
5719 * @see #detachAllViewsFromParent()
5720 * @see #detachViewFromParent(View)
5721 * @see #detachViewFromParent(int)
5722 */
5723 protected void attachViewToParent(View child, int index, LayoutParams params) {
5724 child.mLayoutParams = params;
5725
5726 if (index < 0) {
5727 index = mChildrenCount;
5728 }
5729
5730 addInArray(child, index);
5731
5732 child.mParent = this;
Dianne Hackborn4702a852012-08-17 15:18:29 -07005733 child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK
5734 & ~PFLAG_DRAWING_CACHE_VALID)
5735 | PFLAG_DRAWN | PFLAG_INVALIDATED;
5736 this.mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737
5738 if (child.hasFocus()) {
5739 requestChildFocus(child, child.findFocus());
5740 }
Adam Powellc5874092016-03-17 16:27:24 -07005741 dispatchVisibilityAggregated(isAttachedToWindow() && getWindowVisibility() == VISIBLE
5742 && isShown());
Phil Weaver13171622018-04-03 11:39:20 -07005743 notifySubtreeAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 }
5745
5746 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005747 * Detaches a view from its parent. Detaching a view should be followed
5748 * either by a call to
5749 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5750 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5751 * temporary; reattachment or removal should happen within the same drawing cycle as
5752 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5753 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 *
5755 * @param child the child to detach
5756 *
5757 * @see #detachViewFromParent(int)
5758 * @see #detachViewsFromParent(int, int)
5759 * @see #detachAllViewsFromParent()
5760 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5761 * @see #removeDetachedView(View, boolean)
5762 */
5763 protected void detachViewFromParent(View child) {
5764 removeFromArray(indexOfChild(child));
5765 }
5766
5767 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005768 * Detaches a view from its parent. Detaching a view should be followed
5769 * either by a call to
5770 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5771 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5772 * temporary; reattachment or removal should happen within the same drawing cycle as
5773 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5774 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 *
5776 * @param index the index of the child to detach
5777 *
5778 * @see #detachViewFromParent(View)
5779 * @see #detachAllViewsFromParent()
5780 * @see #detachViewsFromParent(int, int)
5781 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5782 * @see #removeDetachedView(View, boolean)
5783 */
5784 protected void detachViewFromParent(int index) {
5785 removeFromArray(index);
5786 }
5787
5788 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005789 * Detaches a range of views from their parents. Detaching a view should be followed
5790 * either by a call to
5791 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5792 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5793 * temporary; reattachment or removal should happen within the same drawing cycle as
5794 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5795 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 *
5797 * @param start the first index of the childrend range to detach
5798 * @param count the number of children to detach
5799 *
5800 * @see #detachViewFromParent(View)
5801 * @see #detachViewFromParent(int)
5802 * @see #detachAllViewsFromParent()
5803 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5804 * @see #removeDetachedView(View, boolean)
5805 */
5806 protected void detachViewsFromParent(int start, int count) {
5807 removeFromArray(start, count);
5808 }
5809
5810 /**
Chet Haaseca479d42012-08-30 17:20:08 -07005811 * Detaches all views from the parent. Detaching a view should be followed
5812 * either by a call to
5813 * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)}
5814 * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be
5815 * temporary; reattachment or removal should happen within the same drawing cycle as
5816 * detachment. When a view is detached, its parent is null and cannot be retrieved by a
5817 * call to {@link #getChildAt(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 *
5819 * @see #detachViewFromParent(View)
5820 * @see #detachViewFromParent(int)
5821 * @see #detachViewsFromParent(int, int)
5822 * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
5823 * @see #removeDetachedView(View, boolean)
5824 */
5825 protected void detachAllViewsFromParent() {
5826 final int count = mChildrenCount;
5827 if (count <= 0) {
5828 return;
5829 }
5830
5831 final View[] children = mChildren;
5832 mChildrenCount = 0;
5833
5834 for (int i = count - 1; i >= 0; i--) {
5835 children[i].mParent = null;
5836 children[i] = null;
5837 }
5838 }
5839
Chris Craik9de95db2017-01-18 17:59:23 -08005840 @Override
5841 @CallSuper
5842 public void onDescendantInvalidated(@NonNull View child, @NonNull View target) {
5843 /*
5844 * HW-only, Rect-ignoring damage codepath
5845 *
5846 * We don't deal with rectangles here, since RenderThread native code computes damage for
5847 * everything drawn by HWUI (and SW layer / drawing cache doesn't keep track of damage area)
5848 */
5849
5850 // if set, combine the animation flag into the parent
5851 mPrivateFlags |= (target.mPrivateFlags & PFLAG_DRAW_ANIMATION);
5852
5853 if ((target.mPrivateFlags & ~PFLAG_DIRTY_MASK) != 0) {
5854 // We lazily use PFLAG_DIRTY, since computing opaque isn't worth the potential
5855 // optimization in provides in a DisplayList world.
5856 mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DIRTY;
5857
5858 // simplified invalidateChildInParent behavior: clear cache validity to be safe...
5859 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chris Craik3f06c6d2017-01-09 18:19:48 +00005860 }
5861
Chris Craik9de95db2017-01-18 17:59:23 -08005862 // ... and mark inval if in software layer that needs to repaint (hw handled in native)
5863 if (mLayerType == LAYER_TYPE_SOFTWARE) {
5864 // Layered parents should be invalidated. Escalate to a full invalidate (and note that
5865 // we do this after consuming any relevant flags from the originating descendant)
5866 mPrivateFlags |= PFLAG_INVALIDATED | PFLAG_DIRTY;
5867 target = this;
Chris Craik3f06c6d2017-01-09 18:19:48 +00005868 }
5869
Chris Craik9de95db2017-01-18 17:59:23 -08005870 if (mParent != null) {
5871 mParent.onDescendantInvalidated(this, target);
Chris Craik3f06c6d2017-01-09 18:19:48 +00005872 }
Chris Craik3f06c6d2017-01-09 18:19:48 +00005873 }
5874
5875
5876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 * Don't call or override this method. It is used for the implementation of
5878 * the view hierarchy.
Chris Craik9de95db2017-01-18 17:59:23 -08005879 *
5880 * @deprecated Use {@link #onDescendantInvalidated(View, View)} instead to observe updates to
5881 * draw state in descendants.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 */
Chris Craik9de95db2017-01-18 17:59:23 -08005883 @Deprecated
Alan Viverettebe463f22016-01-21 10:50:10 -05005884 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 public final void invalidateChild(View child, final Rect dirty) {
Chris Craik9de95db2017-01-18 17:59:23 -08005886 final AttachInfo attachInfo = mAttachInfo;
5887 if (attachInfo != null && attachInfo.mHardwareAccelerated) {
5888 // HW accelerated fast path
5889 onDescendantInvalidated(child, child);
Chris Craik3f06c6d2017-01-09 18:19:48 +00005890 return;
5891 }
5892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 ViewParent parent = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 // If the child is drawing an animation, we want to copy this flag onto
5896 // ourselves and the parent to make sure the invalidate request goes
5897 // through
Chris Craik3f06c6d2017-01-09 18:19:48 +00005898 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) != 0;
Romain Guy24443ea2009-05-11 11:56:30 -07005899
Romain Guyfe455af2012-02-15 16:40:20 -08005900 // Check whether the child that requests the invalidate is fully opaque
5901 // Views being animated or transformed are not considered opaque because we may
5902 // be invalidating their old position and need the parent to paint behind them.
5903 Matrix childMatrix = child.getMatrix();
Romain Guyfe455af2012-02-15 16:40:20 -08005904 // Mark the child as dirty, using the appropriate flag
5905 // Make sure we do not set both flags at the same time
Romain Guyfe455af2012-02-15 16:40:20 -08005906
John Reck96bb8ad2014-06-19 10:53:03 -07005907 if (child.mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005908 mPrivateFlags |= PFLAG_INVALIDATED;
5909 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guyfe455af2012-02-15 16:40:20 -08005910 }
5911
5912 final int[] location = attachInfo.mInvalidateChildLocation;
5913 location[CHILD_LEFT_INDEX] = child.mLeft;
5914 location[CHILD_TOP_INDEX] = child.mTop;
Chet Haase599913d2012-07-23 16:22:05 -07005915 if (!childMatrix.isIdentity() ||
5916 (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Romain Guyfe455af2012-02-15 16:40:20 -08005917 RectF boundingRect = attachInfo.mTmpTransformRect;
5918 boundingRect.set(dirty);
Chet Haase599913d2012-07-23 16:22:05 -07005919 Matrix transformMatrix;
5920 if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
5921 Transformation t = attachInfo.mTmpTransformation;
5922 boolean transformed = getChildStaticTransformation(child, t);
5923 if (transformed) {
5924 transformMatrix = attachInfo.mTmpMatrix;
5925 transformMatrix.set(t.getMatrix());
5926 if (!childMatrix.isIdentity()) {
5927 transformMatrix.preConcat(childMatrix);
5928 }
5929 } else {
5930 transformMatrix = childMatrix;
5931 }
5932 } else {
5933 transformMatrix = childMatrix;
5934 }
5935 transformMatrix.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005936 dirty.set((int) Math.floor(boundingRect.left),
5937 (int) Math.floor(boundingRect.top),
5938 (int) Math.ceil(boundingRect.right),
5939 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005940 }
5941
5942 do {
5943 View view = null;
5944 if (parent instanceof View) {
5945 view = (View) parent;
Romain Guyfe455af2012-02-15 16:40:20 -08005946 }
5947
5948 if (drawAnimation) {
5949 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005950 view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Romain Guyfe455af2012-02-15 16:40:20 -08005951 } else if (parent instanceof ViewRootImpl) {
5952 ((ViewRootImpl) parent).mIsAnimating = true;
5953 }
5954 }
5955
5956 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
5957 // flag coming from the child that initiated the invalidate
5958 if (view != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005959 if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) {
Nader Jawad70e72992018-05-08 10:54:33 -07005960 view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DIRTY;
Romain Guyfe455af2012-02-15 16:40:20 -08005961 }
5962 }
5963
5964 parent = parent.invalidateChildInParent(location, dirty);
5965 if (view != null) {
5966 // Account for transform on current parent
5967 Matrix m = view.getMatrix();
5968 if (!m.isIdentity()) {
5969 RectF boundingRect = attachInfo.mTmpTransformRect;
5970 boundingRect.set(dirty);
5971 m.mapRect(boundingRect);
Alan Viverettec45b1d42015-11-16 15:38:59 -05005972 dirty.set((int) Math.floor(boundingRect.left),
5973 (int) Math.floor(boundingRect.top),
5974 (int) Math.ceil(boundingRect.right),
5975 (int) Math.ceil(boundingRect.bottom));
Romain Guyfe455af2012-02-15 16:40:20 -08005976 }
5977 }
5978 } while (parent != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 }
5980 }
5981
5982 /**
5983 * Don't call or override this method. It is used for the implementation of
5984 * the view hierarchy.
5985 *
5986 * This implementation returns null if this ViewGroup does not have a parent,
5987 * if this ViewGroup is already fully invalidated or if the dirty rectangle
5988 * does not intersect with this ViewGroup's bounds.
Chris Craik9de95db2017-01-18 17:59:23 -08005989 *
5990 * @deprecated Use {@link #onDescendantInvalidated(View, View)} instead to observe updates to
5991 * draw state in descendants.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 */
Chris Craik9de95db2017-01-18 17:59:23 -08005993 @Deprecated
Alan Viverettebe463f22016-01-21 10:50:10 -05005994 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
Chris Craik3f06c6d2017-01-09 18:19:48 +00005996 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID)) != 0) {
5997 // either DRAWN, or DRAWING_CACHE_VALID
5998 if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE))
5999 != FLAG_OPTIMIZE_INVALIDATE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
6001 location[CHILD_TOP_INDEX] - mScrollY);
Chet Haasea4f14eb2013-04-22 11:11:39 -07006002 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) {
6003 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
6004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005
6006 final int left = mLeft;
6007 final int top = mTop;
6008
Chet Haase05e91ed2012-07-03 14:17:57 -07006009 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
6010 if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) {
6011 dirty.setEmpty();
Romain Guy3a3133d2011-02-01 22:59:58 -08006012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 }
Chet Haase05e91ed2012-07-03 14:17:57 -07006014
6015 location[CHILD_LEFT_INDEX] = left;
6016 location[CHILD_TOP_INDEX] = top;
Selim Cinek1cb8b082016-12-21 15:34:30 +00006017 } else {
Selim Cinek1cb8b082016-12-21 15:34:30 +00006018
Chet Haasea3db8662011-07-19 10:36:05 -07006019 if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) {
6020 dirty.set(0, 0, mRight - mLeft, mBottom - mTop);
6021 } else {
6022 // in case the dirty rect extends outside the bounds of this container
6023 dirty.union(0, 0, mRight - mLeft, mBottom - mTop);
6024 }
Chris Craik3f06c6d2017-01-09 18:19:48 +00006025 location[CHILD_LEFT_INDEX] = mLeft;
6026 location[CHILD_TOP_INDEX] = mTop;
Romain Guy3a3133d2011-02-01 22:59:58 -08006027
Chris Craik3f06c6d2017-01-09 18:19:48 +00006028 mPrivateFlags &= ~PFLAG_DRAWN;
Selim Cinek1cb8b082016-12-21 15:34:30 +00006029 }
Chris Craik3f06c6d2017-01-09 18:19:48 +00006030 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
6031 if (mLayerType != LAYER_TYPE_NONE) {
6032 mPrivateFlags |= PFLAG_INVALIDATED;
6033 }
6034
6035 return mParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 }
6037
6038 return null;
6039 }
6040
6041 /**
6042 * Offset a rectangle that is in a descendant's coordinate
6043 * space into our coordinate space.
6044 * @param descendant A descendant of this view
6045 * @param rect A rectangle defined in descendant's coordinate space.
6046 */
6047 public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) {
6048 offsetRectBetweenParentAndChild(descendant, rect, true, false);
6049 }
6050
6051 /**
6052 * Offset a rectangle that is in our coordinate space into an ancestor's
6053 * coordinate space.
6054 * @param descendant A descendant of this view
6055 * @param rect A rectangle defined in descendant's coordinate space.
6056 */
6057 public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) {
6058 offsetRectBetweenParentAndChild(descendant, rect, false, false);
6059 }
6060
6061 /**
6062 * Helper method that offsets a rect either from parent to descendant or
6063 * descendant to parent.
6064 */
6065 void offsetRectBetweenParentAndChild(View descendant, Rect rect,
6066 boolean offsetFromChildToParent, boolean clipToBounds) {
6067
6068 // already in the same coord system :)
6069 if (descendant == this) {
6070 return;
6071 }
6072
6073 ViewParent theParent = descendant.mParent;
6074
6075 // search and offset up to the parent
6076 while ((theParent != null)
6077 && (theParent instanceof View)
6078 && (theParent != this)) {
6079
6080 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00006081 rect.offset(descendant.mLeft - descendant.mScrollX,
6082 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 if (clipToBounds) {
6084 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07006085 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
6086 p.mBottom - p.mTop);
6087 if (!intersected) {
6088 rect.setEmpty();
6089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 }
6091 } else {
6092 if (clipToBounds) {
6093 View p = (View) theParent;
Doris Liu9607fbe2015-05-28 17:17:28 -07006094 boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft,
6095 p.mBottom - p.mTop);
6096 if (!intersected) {
6097 rect.setEmpty();
6098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 }
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00006100 rect.offset(descendant.mScrollX - descendant.mLeft,
6101 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 }
6103
6104 descendant = (View) theParent;
6105 theParent = descendant.mParent;
6106 }
6107
6108 // now that we are up to this view, need to offset one more time
6109 // to get into our coordinate space
6110 if (theParent == this) {
6111 if (offsetFromChildToParent) {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00006112 rect.offset(descendant.mLeft - descendant.mScrollX,
6113 descendant.mTop - descendant.mScrollY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 } else {
Hyunyoung Song4eb1a4e2016-03-09 22:51:02 +00006115 rect.offset(descendant.mScrollX - descendant.mLeft,
6116 descendant.mScrollY - descendant.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 }
6118 } else {
6119 throw new IllegalArgumentException("parameter must be a descendant of this view");
6120 }
6121 }
6122
6123 /**
6124 * Offset the vertical location of all children of this view by the specified number of pixels.
6125 *
6126 * @param offset the number of pixels to offset
6127 *
6128 * @hide
6129 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01006130 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 public void offsetChildrenTopAndBottom(int offset) {
6132 final int count = mChildrenCount;
6133 final View[] children = mChildren;
Romain Guy5549cb52013-05-06 18:42:08 -07006134 boolean invalidate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135
6136 for (int i = 0; i < count; i++) {
6137 final View v = children[i];
6138 v.mTop += offset;
6139 v.mBottom += offset;
Chris Craik64a12e12014-03-28 18:12:12 -07006140 if (v.mRenderNode != null) {
Romain Guy5549cb52013-05-06 18:42:08 -07006141 invalidate = true;
Chris Craik64a12e12014-03-28 18:12:12 -07006142 v.mRenderNode.offsetTopAndBottom(offset);
Chet Haasea1cff502012-02-21 13:43:44 -08006143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 }
Romain Guy5549cb52013-05-06 18:42:08 -07006145
6146 if (invalidate) {
6147 invalidateViewProperty(false, false);
6148 }
Eugene Susla72c510f2018-01-23 21:12:11 +00006149 notifySubtreeAccessibilityStateChangedIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 }
6151
Alan Viverettebe463f22016-01-21 10:50:10 -05006152 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
Abodunrinwa Toki4e7a1202016-05-03 18:23:12 +01006154 return getChildVisibleRect(child, r, offset, false);
6155 }
6156
6157 /**
Abodunrinwa Tokicb664062016-05-13 19:26:03 +01006158 * @param forceParentCheck true to guarantee that this call will propagate to all ancestors,
6159 * false otherwise
6160 *
Abodunrinwa Toki4e7a1202016-05-03 18:23:12 +01006161 * @hide
6162 */
6163 public boolean getChildVisibleRect(
6164 View child, Rect r, android.graphics.Point offset, boolean forceParentCheck) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08006165 // It doesn't make a whole lot of sense to call this on a view that isn't attached,
6166 // but for some simple tests it can be useful. If we don't have attach info this
6167 // will allocate memory.
6168 final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF();
Gilles Debunnecea45132011-11-24 02:19:27 +01006169 rect.set(r);
6170
6171 if (!child.hasIdentityMatrix()) {
George Mount002d43d2014-11-11 12:54:43 -08006172 child.getMatrix().mapRect(rect);
Gilles Debunnecea45132011-11-24 02:19:27 +01006173 }
6174
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08006175 final int dx = child.mLeft - mScrollX;
6176 final int dy = child.mTop - mScrollY;
Gilles Debunnecea45132011-11-24 02:19:27 +01006177
6178 rect.offset(dx, dy);
6179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 if (offset != null) {
Gilles Debunnecea45132011-11-24 02:19:27 +01006181 if (!child.hasIdentityMatrix()) {
Adam Powellf93bb6d2011-12-12 15:21:57 -08006182 float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation
6183 : new float[2];
Gilles Debunnecea45132011-11-24 02:19:27 +01006184 position[0] = offset.x;
6185 position[1] = offset.y;
6186 child.getMatrix().mapPoints(position);
Alan Viverettec45b1d42015-11-16 15:38:59 -05006187 offset.x = Math.round(position[0]);
6188 offset.y = Math.round(position[1]);
Gilles Debunnecea45132011-11-24 02:19:27 +01006189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 offset.x += dx;
6191 offset.y += dy;
6192 }
Gilles Debunnecea45132011-11-24 02:19:27 +01006193
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08006194 final int width = mRight - mLeft;
6195 final int height = mBottom - mTop;
6196
George Mount002d43d2014-11-11 12:54:43 -08006197 boolean rectIsVisible = true;
Adam Powell35da41e2014-12-10 18:59:34 -08006198 if (mParent == null ||
6199 (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08006200 // Clip to bounds.
6201 rectIsVisible = rect.intersect(0, 0, width, height);
Gilles Debunnecea45132011-11-24 02:19:27 +01006202 }
6203
Abodunrinwa Toki4e7a1202016-05-03 18:23:12 +01006204 if ((forceParentCheck || rectIsVisible)
6205 && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08006206 // Clip to padding.
George Mount002d43d2014-11-11 12:54:43 -08006207 rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop,
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08006208 width - mPaddingRight, height - mPaddingBottom);
George Mount002d43d2014-11-11 12:54:43 -08006209 }
6210
Abodunrinwa Toki4e7a1202016-05-03 18:23:12 +01006211 if ((forceParentCheck || rectIsVisible) && mClipBounds != null) {
Alan Viveretteab2cf6d2014-11-26 14:34:36 -08006212 // Clip to clipBounds.
George Mount002d43d2014-11-11 12:54:43 -08006213 rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right,
6214 mClipBounds.bottom);
6215 }
Alan Viverettec45b1d42015-11-16 15:38:59 -05006216 r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top),
6217 (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom));
Abodunrinwa Toki4e7a1202016-05-03 18:23:12 +01006218
6219 if ((forceParentCheck || rectIsVisible) && mParent != null) {
Abodunrinwa Tokicb664062016-05-13 19:26:03 +01006220 if (mParent instanceof ViewGroup) {
6221 rectIsVisible = ((ViewGroup) mParent)
6222 .getChildVisibleRect(this, r, offset, forceParentCheck);
6223 } else {
6224 rectIsVisible = mParent.getChildVisibleRect(this, r, offset);
6225 }
George Mount002d43d2014-11-11 12:54:43 -08006226 }
6227 return rectIsVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 }
6229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 @Override
Chet Haase9c087442011-01-12 16:20:16 -08006231 public final void layout(int l, int t, int r, int b) {
Chet Haase430742f2013-04-12 11:18:36 -07006232 if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
Chet Haase7dd4a532012-04-16 13:35:09 -07006233 if (mTransition != null) {
6234 mTransition.layoutChange(this);
6235 }
Chet Haase9c087442011-01-12 16:20:16 -08006236 super.layout(l, t, r, b);
6237 } else {
6238 // record the fact that we noop'd it; request layout when transition finishes
Chet Haaseb9895022013-04-02 15:10:58 -07006239 mLayoutCalledWhileSuppressed = true;
Chet Haase9c087442011-01-12 16:20:16 -08006240 }
6241 }
6242
Chet Haase9c087442011-01-12 16:20:16 -08006243 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 protected abstract void onLayout(boolean changed,
6245 int l, int t, int r, int b);
6246
6247 /**
6248 * Indicates whether the view group has the ability to animate its children
6249 * after the first layout.
6250 *
6251 * @return true if the children can be animated, false otherwise
6252 */
6253 protected boolean canAnimate() {
6254 return mLayoutAnimationController != null;
6255 }
6256
6257 /**
6258 * Runs the layout animation. Calling this method triggers a relayout of
6259 * this view group.
6260 */
6261 public void startLayoutAnimation() {
6262 if (mLayoutAnimationController != null) {
6263 mGroupFlags |= FLAG_RUN_ANIMATION;
6264 requestLayout();
6265 }
6266 }
6267
6268 /**
6269 * Schedules the layout animation to be played after the next layout pass
6270 * of this view group. This can be used to restart the layout animation
6271 * when the content of the view group changes or when the activity is
6272 * paused and resumed.
6273 */
6274 public void scheduleLayoutAnimation() {
6275 mGroupFlags |= FLAG_RUN_ANIMATION;
6276 }
6277
6278 /**
6279 * Sets the layout animation controller used to animate the group's
6280 * children after the first layout.
6281 *
6282 * @param controller the animation controller
6283 */
6284 public void setLayoutAnimation(LayoutAnimationController controller) {
6285 mLayoutAnimationController = controller;
6286 if (mLayoutAnimationController != null) {
6287 mGroupFlags |= FLAG_RUN_ANIMATION;
6288 }
6289 }
6290
6291 /**
6292 * Returns the layout animation controller used to animate the group's
6293 * children.
6294 *
6295 * @return the current animation controller
6296 */
6297 public LayoutAnimationController getLayoutAnimation() {
6298 return mLayoutAnimationController;
6299 }
6300
6301 /**
6302 * Indicates whether the children's drawing cache is used during a layout
6303 * animation. By default, the drawing cache is enabled but this will prevent
6304 * nested layout animations from working. To nest animations, you must disable
6305 * the cache.
6306 *
6307 * @return true if the animation cache is enabled, false otherwise
6308 *
6309 * @see #setAnimationCacheEnabled(boolean)
6310 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07006311 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006312 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07006313 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006314 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07006315 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 public boolean isAnimationCacheEnabled() {
6317 return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
6318 }
6319
6320 /**
6321 * Enables or disables the children's drawing cache during a layout animation.
6322 * By default, the drawing cache is enabled but this will prevent nested
6323 * layout animations from working. To nest animations, you must disable the
6324 * cache.
6325 *
6326 * @param enabled true to enable the animation cache, false otherwise
6327 *
6328 * @see #isAnimationCacheEnabled()
6329 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07006330 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006331 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07006332 * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07006334 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 public void setAnimationCacheEnabled(boolean enabled) {
6336 setBooleanFlag(FLAG_ANIMATION_CACHE, enabled);
6337 }
6338
6339 /**
6340 * Indicates whether this ViewGroup will always try to draw its children using their
6341 * drawing cache. By default this property is enabled.
6342 *
6343 * @return true if the animation cache is enabled, false otherwise
6344 *
6345 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
6346 * @see #setChildrenDrawnWithCacheEnabled(boolean)
6347 * @see View#setDrawingCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07006348 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006349 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07006350 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07006352 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 public boolean isAlwaysDrawnWithCacheEnabled() {
6354 return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE;
6355 }
6356
6357 /**
6358 * Indicates whether this ViewGroup will always try to draw its children using their
6359 * drawing cache. This property can be set to true when the cache rendering is
6360 * slightly different from the children's normal rendering. Renderings can be different,
6361 * for instance, when the cache's quality is set to low.
6362 *
6363 * When this property is disabled, the ViewGroup will use the drawing cache of its
6364 * children only when asked to. It's usually the task of subclasses to tell ViewGroup
6365 * when to start using the drawing cache and when to stop using it.
6366 *
6367 * @param always true to always draw with the drawing cache, false otherwise
6368 *
6369 * @see #isAlwaysDrawnWithCacheEnabled()
6370 * @see #setChildrenDrawnWithCacheEnabled(boolean)
6371 * @see View#setDrawingCacheEnabled(boolean)
6372 * @see View#setDrawingCacheQuality(int)
Chris Craik5a6bbae2015-04-10 17:41:34 -07006373 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006374 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07006375 * Child views may no longer have their caching behavior disabled by parents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07006377 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 public void setAlwaysDrawnWithCacheEnabled(boolean always) {
6379 setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always);
6380 }
6381
6382 /**
6383 * Indicates whether the ViewGroup is currently drawing its children using
6384 * their drawing cache.
6385 *
6386 * @return true if children should be drawn with their cache, false otherwise
6387 *
6388 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
6389 * @see #setChildrenDrawnWithCacheEnabled(boolean)
Chris Craik5a6bbae2015-04-10 17:41:34 -07006390 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006391 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07006392 * Child views may no longer be forced to cache their rendering state by their parents.
6393 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07006395 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 protected boolean isChildrenDrawnWithCacheEnabled() {
6397 return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE;
6398 }
6399
6400 /**
6401 * Tells the ViewGroup to draw its children using their drawing cache. This property
6402 * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache
6403 * will be used only if it has been enabled.
6404 *
6405 * Subclasses should call this method to start and stop using the drawing cache when
6406 * they perform performance sensitive operations, like scrolling or animating.
6407 *
6408 * @param enabled true if children should be drawn with their cache, false otherwise
6409 *
6410 * @see #setAlwaysDrawnWithCacheEnabled(boolean)
6411 * @see #isChildrenDrawnWithCacheEnabled()
Chris Craik5a6bbae2015-04-10 17:41:34 -07006412 *
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006413 * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored.
Chris Craik5a6bbae2015-04-10 17:41:34 -07006414 * Child views may no longer be forced to cache their rendering state by their parents.
6415 * Use {@link View#setLayerType(int, Paint)} on individual Views instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07006417 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
6419 setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled);
6420 }
6421
Romain Guy293451e2009-11-04 13:59:48 -08006422 /**
6423 * Indicates whether the ViewGroup is drawing its children in the order defined by
6424 * {@link #getChildDrawingOrder(int, int)}.
6425 *
6426 * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)},
6427 * false otherwise
6428 *
6429 * @see #setChildrenDrawingOrderEnabled(boolean)
6430 * @see #getChildDrawingOrder(int, int)
6431 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006432 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy293451e2009-11-04 13:59:48 -08006433 protected boolean isChildrenDrawingOrderEnabled() {
6434 return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER;
6435 }
6436
6437 /**
6438 * Tells the ViewGroup whether to draw its children in the order defined by the method
6439 * {@link #getChildDrawingOrder(int, int)}.
Chris Craike83cbd42014-09-03 17:52:24 -07006440 * <p>
6441 * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)},
6442 * will override custom child ordering done via this method.
Romain Guy293451e2009-11-04 13:59:48 -08006443 *
6444 * @param enabled true if the order of the children when drawing is determined by
6445 * {@link #getChildDrawingOrder(int, int)}, false otherwise
6446 *
6447 * @see #isChildrenDrawingOrderEnabled()
6448 * @see #getChildDrawingOrder(int, int)
6449 */
6450 protected void setChildrenDrawingOrderEnabled(boolean enabled) {
6451 setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled);
6452 }
6453
Svetoslav6254f482013-06-04 17:22:14 -07006454 private boolean hasBooleanFlag(int flag) {
Philip Milnef091b662013-02-27 11:15:21 -08006455 return (mGroupFlags & flag) == flag;
6456 }
6457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 private void setBooleanFlag(int flag, boolean value) {
6459 if (value) {
6460 mGroupFlags |= flag;
6461 } else {
6462 mGroupFlags &= ~flag;
6463 }
6464 }
6465
6466 /**
6467 * Returns an integer indicating what types of drawing caches are kept in memory.
6468 *
6469 * @see #setPersistentDrawingCache(int)
6470 * @see #setAnimationCacheEnabled(boolean)
6471 *
6472 * @return one or a combination of {@link #PERSISTENT_NO_CACHE},
6473 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
6474 * and {@link #PERSISTENT_ALL_CACHES}
John Reck949cfe12017-10-09 13:27:03 -07006475 *
6476 * @deprecated The view drawing cache was largely made obsolete with the introduction of
6477 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
6478 * layers are largely unnecessary and can easily result in a net loss in performance due to the
6479 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
6480 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
6481 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
6482 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
6483 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
6484 * software-rendered usages are discouraged and have compatibility issues with hardware-only
6485 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
6486 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
6487 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 */
John Reck949cfe12017-10-09 13:27:03 -07006489 @Deprecated
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006490 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"),
Romain Guy203688c2010-05-12 15:41:32 -07006492 @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"),
6494 @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL")
6495 })
6496 public int getPersistentDrawingCache() {
6497 return mPersistentDrawingCache;
6498 }
6499
6500 /**
6501 * Indicates what types of drawing caches should be kept in memory after
6502 * they have been created.
6503 *
6504 * @see #getPersistentDrawingCache()
6505 * @see #setAnimationCacheEnabled(boolean)
6506 *
6507 * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE},
6508 * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE}
6509 * and {@link #PERSISTENT_ALL_CACHES}
John Reck949cfe12017-10-09 13:27:03 -07006510 *
6511 * @deprecated The view drawing cache was largely made obsolete with the introduction of
6512 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
6513 * layers are largely unnecessary and can easily result in a net loss in performance due to the
6514 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
6515 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
6516 * rendering. For software-rendered snapshots of a small part of the View hierarchy or
6517 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
6518 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
6519 * software-rendered usages are discouraged and have compatibility issues with hardware-only
6520 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
6521 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
6522 * reports or unit testing the {@link PixelCopy} API is recommended.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 */
John Reck949cfe12017-10-09 13:27:03 -07006524 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 public void setPersistentDrawingCache(int drawingCacheToKeep) {
6526 mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES;
6527 }
6528
Philip Milnef091b662013-02-27 11:15:21 -08006529 private void setLayoutMode(int layoutMode, boolean explicitly) {
6530 mLayoutMode = layoutMode;
6531 setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly);
6532 }
6533
6534 /**
6535 * Recursively traverse the view hierarchy, resetting the layoutMode of any
6536 * descendants that had inherited a different layoutMode from a previous parent.
6537 * Recursion terminates when a descendant's mode is:
6538 * <ul>
6539 * <li>Undefined</li>
6540 * <li>The same as the root node's</li>
6541 * <li>A mode that had been explicitly set</li>
6542 * <ul/>
6543 * The first two clauses are optimizations.
6544 * @param layoutModeOfRoot
6545 */
6546 @Override
6547 void invalidateInheritedLayoutMode(int layoutModeOfRoot) {
6548 if (mLayoutMode == LAYOUT_MODE_UNDEFINED ||
6549 mLayoutMode == layoutModeOfRoot ||
Svetoslav6254f482013-06-04 17:22:14 -07006550 hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) {
Philip Milnef091b662013-02-27 11:15:21 -08006551 return;
6552 }
6553 setLayoutMode(LAYOUT_MODE_UNDEFINED, false);
6554
6555 // apply recursively
6556 for (int i = 0, N = getChildCount(); i < N; i++) {
6557 getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot);
6558 }
6559 }
6560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07006562 * Returns the basis of alignment during layout operations on this ViewGroup:
Philip Milne7b757812012-09-19 18:13:44 -07006563 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milnecfb631b2012-10-26 10:51:46 -07006564 * <p>
6565 * If no layoutMode was explicitly set, either programmatically or in an XML resource,
6566 * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
6567 * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07006568 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07006569 * @return the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07006570 *
6571 * @see #setLayoutMode(int)
6572 */
6573 public int getLayoutMode() {
Philip Milnecfb631b2012-10-26 10:51:46 -07006574 if (mLayoutMode == LAYOUT_MODE_UNDEFINED) {
Philip Milnef091b662013-02-27 11:15:21 -08006575 int inheritedLayoutMode = (mParent instanceof ViewGroup) ?
6576 ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT;
6577 setLayoutMode(inheritedLayoutMode, false);
Philip Milnecfb631b2012-10-26 10:51:46 -07006578 }
Philip Milne1557fd72012-04-04 23:41:34 -07006579 return mLayoutMode;
6580 }
6581
6582 /**
Philip Milnecfb631b2012-10-26 10:51:46 -07006583 * Sets the basis of alignment during the layout of this ViewGroup.
Philip Milne7b757812012-09-19 18:13:44 -07006584 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
6585 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
Philip Milne1557fd72012-04-04 23:41:34 -07006586 *
Philip Milnefcc6a0f2012-04-16 16:12:19 -07006587 * @param layoutMode the layout mode to use during layout operations
Philip Milne1557fd72012-04-04 23:41:34 -07006588 *
6589 * @see #getLayoutMode()
Scott Main27a85082013-06-10 10:39:48 -07006590 * @attr ref android.R.styleable#ViewGroup_layoutMode
Philip Milne1557fd72012-04-04 23:41:34 -07006591 */
6592 public void setLayoutMode(int layoutMode) {
6593 if (mLayoutMode != layoutMode) {
Philip Milnef091b662013-02-27 11:15:21 -08006594 invalidateInheritedLayoutMode(layoutMode);
6595 setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED);
Philip Milne1557fd72012-04-04 23:41:34 -07006596 requestLayout();
6597 }
6598 }
6599
6600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 * Returns a new set of layout parameters based on the supplied attributes set.
6602 *
6603 * @param attrs the attributes to build the layout parameters from
6604 *
6605 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
6606 * of its descendants
6607 */
6608 public LayoutParams generateLayoutParams(AttributeSet attrs) {
6609 return new LayoutParams(getContext(), attrs);
6610 }
6611
6612 /**
6613 * Returns a safe set of layout parameters based on the supplied layout params.
6614 * When a ViewGroup is passed a View whose layout params do not pass the test of
6615 * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method
6616 * is invoked. This method should return a new set of layout params suitable for
6617 * this ViewGroup, possibly by copying the appropriate attributes from the
6618 * specified set of layout params.
6619 *
6620 * @param p The layout parameters to convert into a suitable set of layout parameters
6621 * for this ViewGroup.
6622 *
6623 * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one
6624 * of its descendants
6625 */
6626 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
Chet Haase4610eef2015-12-03 07:38:11 -08006627 return p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 }
6629
6630 /**
6631 * Returns a set of default layout parameters. These parameters are requested
6632 * when the View passed to {@link #addView(View)} has no layout parameters
6633 * already set. If null is returned, an exception is thrown from addView.
6634 *
6635 * @return a set of default layout parameters or null
6636 */
6637 protected LayoutParams generateDefaultLayoutParams() {
6638 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
6639 }
6640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 @Override
6642 protected void debug(int depth) {
6643 super.debug(depth);
6644 String output;
6645
6646 if (mFocused != null) {
6647 output = debugIndent(depth);
6648 output += "mFocused";
6649 Log.d(VIEW_LOG_TAG, output);
Vadim Tryshev01d8c492016-12-15 11:33:15 -08006650 mFocused.debug(depth + 1);
6651 }
Vadim Tryshev5ca73982017-01-04 17:24:43 -08006652 if (mDefaultFocus != null) {
Vadim Tryshev01d8c492016-12-15 11:33:15 -08006653 output = debugIndent(depth);
Vadim Tryshev5ca73982017-01-04 17:24:43 -08006654 output += "mDefaultFocus";
Vadim Tryshev01d8c492016-12-15 11:33:15 -08006655 Log.d(VIEW_LOG_TAG, output);
Vadim Tryshev5ca73982017-01-04 17:24:43 -08006656 mDefaultFocus.debug(depth + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 }
Evan Rosky53fcf112017-01-26 14:37:55 -08006658 if (mFocusedInCluster != null) {
6659 output = debugIndent(depth);
6660 output += "mFocusedInCluster";
6661 Log.d(VIEW_LOG_TAG, output);
6662 mFocusedInCluster.debug(depth + 1);
6663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 if (mChildrenCount != 0) {
6665 output = debugIndent(depth);
6666 output += "{";
6667 Log.d(VIEW_LOG_TAG, output);
6668 }
6669 int count = mChildrenCount;
6670 for (int i = 0; i < count; i++) {
6671 View child = mChildren[i];
6672 child.debug(depth + 1);
6673 }
6674
6675 if (mChildrenCount != 0) {
6676 output = debugIndent(depth);
6677 output += "}";
6678 Log.d(VIEW_LOG_TAG, output);
6679 }
6680 }
6681
6682 /**
6683 * Returns the position in the group of the specified child view.
6684 *
6685 * @param child the view for which to get the position
6686 * @return a positive integer representing the position of the view in the
6687 * group, or -1 if the view does not exist in the group
6688 */
6689 public int indexOfChild(View child) {
6690 final int count = mChildrenCount;
6691 final View[] children = mChildren;
6692 for (int i = 0; i < count; i++) {
6693 if (children[i] == child) {
6694 return i;
6695 }
6696 }
6697 return -1;
6698 }
6699
6700 /**
6701 * Returns the number of children in the group.
6702 *
6703 * @return a positive integer representing the number of children in
6704 * the group
6705 */
6706 public int getChildCount() {
6707 return mChildrenCount;
6708 }
6709
6710 /**
6711 * Returns the view at the specified position in the group.
6712 *
6713 * @param index the position at which to get the view from
6714 * @return the view at the specified position or null if the position
6715 * does not exist within the group
6716 */
6717 public View getChildAt(int index) {
Adam Powell3ba8f5d62011-03-07 15:36:33 -08006718 if (index < 0 || index >= mChildrenCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 return null;
6720 }
Adam Powell3ba8f5d62011-03-07 15:36:33 -08006721 return mChildren[index];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 }
6723
6724 /**
6725 * Ask all of the children of this view to measure themselves, taking into
6726 * account both the MeasureSpec requirements for this view and its padding.
6727 * We skip children that are in the GONE state The heavy lifting is done in
6728 * getChildMeasureSpec.
6729 *
6730 * @param widthMeasureSpec The width requirements for this view
6731 * @param heightMeasureSpec The height requirements for this view
6732 */
6733 protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
6734 final int size = mChildrenCount;
6735 final View[] children = mChildren;
6736 for (int i = 0; i < size; ++i) {
6737 final View child = children[i];
6738 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
6739 measureChild(child, widthMeasureSpec, heightMeasureSpec);
6740 }
6741 }
6742 }
6743
6744 /**
6745 * Ask one of the children of this view to measure itself, taking into
6746 * account both the MeasureSpec requirements for this view and its padding.
6747 * The heavy lifting is done in getChildMeasureSpec.
6748 *
6749 * @param child The child to measure
6750 * @param parentWidthMeasureSpec The width requirements for this view
6751 * @param parentHeightMeasureSpec The height requirements for this view
6752 */
6753 protected void measureChild(View child, int parentWidthMeasureSpec,
6754 int parentHeightMeasureSpec) {
6755 final LayoutParams lp = child.getLayoutParams();
6756
6757 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6758 mPaddingLeft + mPaddingRight, lp.width);
6759 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6760 mPaddingTop + mPaddingBottom, lp.height);
6761
6762 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6763 }
6764
6765 /**
6766 * Ask one of the children of this view to measure itself, taking into
6767 * account both the MeasureSpec requirements for this view and its padding
6768 * and margins. The child must have MarginLayoutParams The heavy lifting is
6769 * done in getChildMeasureSpec.
6770 *
6771 * @param child The child to measure
6772 * @param parentWidthMeasureSpec The width requirements for this view
6773 * @param widthUsed Extra space that has been used up by the parent
6774 * horizontally (possibly by other children of the parent)
6775 * @param parentHeightMeasureSpec The height requirements for this view
6776 * @param heightUsed Extra space that has been used up by the parent
6777 * vertically (possibly by other children of the parent)
6778 */
6779 protected void measureChildWithMargins(View child,
6780 int parentWidthMeasureSpec, int widthUsed,
6781 int parentHeightMeasureSpec, int heightUsed) {
6782 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
6783
6784 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
6785 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
6786 + widthUsed, lp.width);
6787 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
6788 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
6789 + heightUsed, lp.height);
6790
6791 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
6792 }
6793
6794 /**
6795 * Does the hard part of measureChildren: figuring out the MeasureSpec to
6796 * pass to a particular child. This method figures out the right MeasureSpec
6797 * for one dimension (height or width) of one child view.
6798 *
6799 * The goal is to combine information from our MeasureSpec with the
6800 * LayoutParams of the child to get the best possible results. For example,
6801 * if the this view knows its size (because its MeasureSpec has a mode of
6802 * EXACTLY), and the child has indicated in its LayoutParams that it wants
6803 * to be the same size as the parent, the parent should ask the child to
6804 * layout given an exact size.
6805 *
6806 * @param spec The requirements for this view
6807 * @param padding The padding of this view for the current dimension and
6808 * margins, if applicable
6809 * @param childDimension How big the child wants to be in the current
6810 * dimension
6811 * @return a MeasureSpec integer for the child
6812 */
6813 public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
6814 int specMode = MeasureSpec.getMode(spec);
6815 int specSize = MeasureSpec.getSize(spec);
6816
6817 int size = Math.max(0, specSize - padding);
6818
6819 int resultSize = 0;
6820 int resultMode = 0;
6821
6822 switch (specMode) {
6823 // Parent has imposed an exact size on us
6824 case MeasureSpec.EXACTLY:
6825 if (childDimension >= 0) {
6826 resultSize = childDimension;
6827 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006828 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 // Child wants to be our size. So be it.
6830 resultSize = size;
6831 resultMode = MeasureSpec.EXACTLY;
6832 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6833 // Child wants to determine its own size. It can't be
6834 // bigger than us.
6835 resultSize = size;
6836 resultMode = MeasureSpec.AT_MOST;
6837 }
6838 break;
6839
6840 // Parent has imposed a maximum size on us
6841 case MeasureSpec.AT_MOST:
6842 if (childDimension >= 0) {
6843 // Child wants a specific size... so be it
6844 resultSize = childDimension;
6845 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006846 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 // Child wants to be our size, but our size is not fixed.
6848 // Constrain child to not be bigger than us.
6849 resultSize = size;
6850 resultMode = MeasureSpec.AT_MOST;
6851 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6852 // Child wants to determine its own size. It can't be
6853 // bigger than us.
6854 resultSize = size;
6855 resultMode = MeasureSpec.AT_MOST;
6856 }
6857 break;
6858
6859 // Parent asked to see how big we want to be
6860 case MeasureSpec.UNSPECIFIED:
6861 if (childDimension >= 0) {
6862 // Child wants a specific size... let him have it
6863 resultSize = childDimension;
6864 resultMode = MeasureSpec.EXACTLY;
Romain Guy980a9382010-01-08 15:06:28 -08006865 } else if (childDimension == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 // Child wants to be our size... find out how big it should
6867 // be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006868 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 resultMode = MeasureSpec.UNSPECIFIED;
6870 } else if (childDimension == LayoutParams.WRAP_CONTENT) {
6871 // Child wants to determine its own size.... find out how
6872 // big it should be
Adam Powelld5dbf4b2015-06-11 13:19:24 -07006873 resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 resultMode = MeasureSpec.UNSPECIFIED;
6875 }
6876 break;
6877 }
Tor Norbye67568642015-03-31 07:47:23 -07006878 //noinspection ResourceType
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
6880 }
6881
6882
6883 /**
6884 * Removes any pending animations for views that have been removed. Call
6885 * this if you don't want animations for exiting views to stack up.
6886 */
6887 public void clearDisappearingChildren() {
John Reckca7a9da2014-03-05 16:29:07 -08006888 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6889 if (disappearingChildren != null) {
6890 final int count = disappearingChildren.size();
6891 for (int i = 0; i < count; i++) {
6892 final View view = disappearingChildren.get(i);
6893 if (view.mAttachInfo != null) {
6894 view.dispatchDetachedFromWindow();
6895 }
6896 view.clearAnimation();
6897 }
6898 disappearingChildren.clear();
Chet Haaseb85967b2012-03-26 14:37:51 -07006899 invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 }
6901 }
6902
6903 /**
6904 * Add a view which is removed from mChildren but still needs animation
6905 *
6906 * @param v View to add
6907 */
6908 private void addDisappearingView(View v) {
6909 ArrayList<View> disappearingChildren = mDisappearingChildren;
6910
6911 if (disappearingChildren == null) {
6912 disappearingChildren = mDisappearingChildren = new ArrayList<View>();
6913 }
6914
6915 disappearingChildren.add(v);
6916 }
6917
6918 /**
6919 * Cleanup a view when its animation is done. This may mean removing it from
6920 * the list of disappearing views.
6921 *
6922 * @param view The view whose animation has finished
6923 * @param animation The animation, cannot be null
6924 */
Chet Haase64a48c12012-02-13 16:33:29 -08006925 void finishAnimatingView(final View view, Animation animation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6927 if (disappearingChildren != null) {
6928 if (disappearingChildren.contains(view)) {
6929 disappearingChildren.remove(view);
6930
6931 if (view.mAttachInfo != null) {
6932 view.dispatchDetachedFromWindow();
6933 }
6934
6935 view.clearAnimation();
6936 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6937 }
6938 }
6939
6940 if (animation != null && !animation.getFillAfter()) {
6941 view.clearAnimation();
6942 }
6943
Dianne Hackborn4702a852012-08-17 15:18:29 -07006944 if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 view.onAnimationEnd();
6946 // Should be performed by onAnimationEnd() but this avoid an infinite loop,
6947 // so we'd rather be safe than sorry
Dianne Hackborn4702a852012-08-17 15:18:29 -07006948 view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 // Draw one more frame after the animation is done
6950 mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
6951 }
6952 }
6953
Chet Haaseb20db3e2010-09-10 13:07:30 -07006954 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07006955 * Utility function called by View during invalidation to determine whether a view that
6956 * is invisible or gone should still be invalidated because it is being transitioned (and
6957 * therefore still needs to be drawn).
6958 */
6959 boolean isViewTransitioning(View view) {
6960 return (mTransitioningViews != null && mTransitioningViews.contains(view));
6961 }
6962
6963 /**
Chet Haaseb20db3e2010-09-10 13:07:30 -07006964 * This method tells the ViewGroup that the given View object, which should have this
6965 * ViewGroup as its parent,
6966 * should be kept around (re-displayed when the ViewGroup draws its children) even if it
6967 * is removed from its parent. This allows animations, such as those used by
6968 * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate
6969 * the removal of views. A call to this method should always be accompanied by a later call
6970 * to {@link #endViewTransition(View)}, such as after an animation on the View has finished,
6971 * so that the View finally gets removed.
6972 *
6973 * @param view The View object to be kept visible even if it gets removed from its parent.
6974 */
6975 public void startViewTransition(View view) {
6976 if (view.mParent == this) {
6977 if (mTransitioningViews == null) {
6978 mTransitioningViews = new ArrayList<View>();
6979 }
6980 mTransitioningViews.add(view);
6981 }
6982 }
6983
6984 /**
6985 * This method should always be called following an earlier call to
6986 * {@link #startViewTransition(View)}. The given View is finally removed from its parent
6987 * and will no longer be displayed. Note that this method does not perform the functionality
6988 * of removing a view from its parent; it just discontinues the display of a View that
6989 * has previously been removed.
6990 *
6991 * @return view The View object that has been removed but is being kept around in the visible
6992 * hierarchy by an earlier call to {@link #startViewTransition(View)}.
6993 */
6994 public void endViewTransition(View view) {
6995 if (mTransitioningViews != null) {
6996 mTransitioningViews.remove(view);
6997 final ArrayList<View> disappearingChildren = mDisappearingChildren;
6998 if (disappearingChildren != null && disappearingChildren.contains(view)) {
6999 disappearingChildren.remove(view);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007000 if (mVisibilityChangingChildren != null &&
7001 mVisibilityChangingChildren.contains(view)) {
7002 mVisibilityChangingChildren.remove(view);
7003 } else {
7004 if (view.mAttachInfo != null) {
7005 view.dispatchDetachedFromWindow();
7006 }
7007 if (view.mParent != null) {
7008 view.mParent = null;
7009 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07007010 }
Chet Haaseb85967b2012-03-26 14:37:51 -07007011 invalidate();
Chet Haaseb20db3e2010-09-10 13:07:30 -07007012 }
7013 }
7014 }
7015
Chet Haase21cd1382010-09-01 17:42:29 -07007016 private LayoutTransition.TransitionListener mLayoutTransitionListener =
7017 new LayoutTransition.TransitionListener() {
7018 @Override
7019 public void startTransition(LayoutTransition transition, ViewGroup container,
7020 View view, int transitionType) {
7021 // We only care about disappearing items, since we need special logic to keep
7022 // those items visible after they've been 'removed'
7023 if (transitionType == LayoutTransition.DISAPPEARING) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07007024 startViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07007025 }
7026 }
7027
7028 @Override
7029 public void endTransition(LayoutTransition transition, ViewGroup container,
7030 View view, int transitionType) {
Chet Haaseb9895022013-04-02 15:10:58 -07007031 if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) {
Chet Haase9c087442011-01-12 16:20:16 -08007032 requestLayout();
Chet Haaseb9895022013-04-02 15:10:58 -07007033 mLayoutCalledWhileSuppressed = false;
Chet Haase9c087442011-01-12 16:20:16 -08007034 }
Chet Haase21cd1382010-09-01 17:42:29 -07007035 if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07007036 endViewTransition(view);
Chet Haase21cd1382010-09-01 17:42:29 -07007037 }
7038 }
7039 };
7040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 /**
Chet Haaseb9895022013-04-02 15:10:58 -07007042 * Tells this ViewGroup to suppress all layout() calls until layout
7043 * suppression is disabled with a later call to suppressLayout(false).
7044 * When layout suppression is disabled, a requestLayout() call is sent
7045 * if layout() was attempted while layout was being suppressed.
Chet Haaseb9895022013-04-02 15:10:58 -07007046 */
7047 public void suppressLayout(boolean suppress) {
7048 mSuppressLayout = suppress;
7049 if (!suppress) {
7050 if (mLayoutCalledWhileSuppressed) {
7051 requestLayout();
7052 mLayoutCalledWhileSuppressed = false;
7053 }
7054 }
7055 }
7056
7057 /**
Chet Haase199acdf2013-07-24 18:40:55 -07007058 * Returns whether layout calls on this container are currently being
7059 * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}.
7060 *
7061 * @return true if layout calls are currently suppressed, false otherwise.
Chet Haase199acdf2013-07-24 18:40:55 -07007062 */
7063 public boolean isLayoutSuppressed() {
7064 return mSuppressLayout;
7065 }
7066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 @Override
7068 public boolean gatherTransparentRegion(Region region) {
7069 // If no transparent regions requested, we are always opaque.
Dianne Hackborn4702a852012-08-17 15:18:29 -07007070 final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007071 if (meOpaque && region == null) {
7072 // The caller doesn't care about the region, so stop now.
7073 return true;
7074 }
7075 super.gatherTransparentRegion(region);
Teng-Hui Zhuf8da30d2016-08-15 10:28:20 -07007076 // Instead of naively traversing the view tree, we have to traverse according to the Z
7077 // order here. We need to go with the same order as dispatchDraw().
7078 // One example is that after surfaceView punch a hole, we will still allow other views drawn
7079 // on top of that hole. In this case, those other views should be able to cut the
7080 // transparent region into smaller area.
7081 final int childrenCount = mChildrenCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 boolean noneOfTheChildrenAreTransparent = true;
Teng-Hui Zhuf8da30d2016-08-15 10:28:20 -07007083 if (childrenCount > 0) {
7084 final ArrayList<View> preorderedList = buildOrderedChildList();
7085 final boolean customOrder = preorderedList == null
7086 && isChildrenDrawingOrderEnabled();
7087 final View[] children = mChildren;
7088 for (int i = 0; i < childrenCount; i++) {
7089 final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
7090 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
7091 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
7092 if (!child.gatherTransparentRegion(region)) {
7093 noneOfTheChildrenAreTransparent = false;
7094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007095 }
7096 }
Teng-Hui Zhuf8da30d2016-08-15 10:28:20 -07007097 if (preorderedList != null) preorderedList.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098 }
7099 return meOpaque || noneOfTheChildrenAreTransparent;
7100 }
7101
Alan Viverettebe463f22016-01-21 10:50:10 -05007102 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 public void requestTransparentRegion(View child) {
7104 if (child != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007105 child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 if (mParent != null) {
7107 mParent.requestTransparentRegion(this);
7108 }
7109 }
7110 }
Romain Guy8506ab42009-06-11 17:35:47 -07007111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 @Override
Adam Powell46e38fd2014-02-03 10:16:49 -08007113 public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
7114 insets = super.dispatchApplyWindowInsets(insets);
Jorim Jaggi15637fd2018-12-04 13:38:03 +01007115 if (View.sBrokenInsetsDispatch) {
7116 return brokenDispatchApplyWindowInsets(insets);
7117 } else {
7118 return newDispatchApplyWindowInsets(insets);
7119 }
7120 }
7121
7122 private WindowInsets brokenDispatchApplyWindowInsets(WindowInsets insets) {
Adam Powell0d9fdba2014-06-11 15:33:08 -07007123 if (!insets.isConsumed()) {
Adam Powell46e38fd2014-02-03 10:16:49 -08007124 final int count = getChildCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 for (int i = 0; i < count; i++) {
Adam Powell46e38fd2014-02-03 10:16:49 -08007126 insets = getChildAt(i).dispatchApplyWindowInsets(insets);
Adam Powell0d9fdba2014-06-11 15:33:08 -07007127 if (insets.isConsumed()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 break;
7129 }
7130 }
7131 }
Adam Powell46e38fd2014-02-03 10:16:49 -08007132 return insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 }
7134
Jorim Jaggi15637fd2018-12-04 13:38:03 +01007135 private WindowInsets newDispatchApplyWindowInsets(WindowInsets insets) {
7136 final int count = getChildCount();
7137 for (int i = 0; i < count; i++) {
7138 getChildAt(i).dispatchApplyWindowInsets(insets);
7139 }
7140 return insets;
7141 }
7142
Jorim Jaggi02a741f2018-12-12 17:40:19 -08007143 @Override
7144 void dispatchWindowInsetsAnimationStarted(InsetsAnimation animation) {
7145 super.dispatchWindowInsetsAnimationStarted(animation);
7146 final int count = getChildCount();
7147 for (int i = 0; i < count; i++) {
7148 getChildAt(i).dispatchWindowInsetsAnimationStarted(animation);
7149 }
7150 }
7151
7152 @Override
7153 WindowInsets dispatchWindowInsetsAnimationProgress(WindowInsets insets) {
7154 insets = super.dispatchWindowInsetsAnimationProgress(insets);
7155 final int count = getChildCount();
7156 for (int i = 0; i < count; i++) {
7157 getChildAt(i).dispatchWindowInsetsAnimationProgress(insets);
7158 }
7159 return insets;
7160 }
7161
7162 @Override
7163 void dispatchWindowInsetsAnimationFinished(InsetsAnimation animation) {
7164 super.dispatchWindowInsetsAnimationFinished(animation);
7165 final int count = getChildCount();
7166 for (int i = 0; i < count; i++) {
7167 getChildAt(i).dispatchWindowInsetsAnimationFinished(animation);
7168 }
7169 }
7170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 /**
7172 * Returns the animation listener to which layout animation events are
7173 * sent.
7174 *
7175 * @return an {@link android.view.animation.Animation.AnimationListener}
7176 */
7177 public Animation.AnimationListener getLayoutAnimationListener() {
7178 return mAnimationListener;
7179 }
7180
7181 @Override
7182 protected void drawableStateChanged() {
7183 super.drawableStateChanged();
7184
7185 if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {
7186 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
7187 throw new IllegalStateException("addStateFromChildren cannot be enabled if a"
7188 + " child has duplicateParentState set to true");
7189 }
7190
7191 final View[] children = mChildren;
7192 final int count = mChildrenCount;
7193
7194 for (int i = 0; i < count; i++) {
7195 final View child = children[i];
7196 if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {
7197 child.refreshDrawableState();
7198 }
7199 }
7200 }
7201 }
7202
7203 @Override
Dianne Hackborne2136772010-11-04 15:08:59 -07007204 public void jumpDrawablesToCurrentState() {
7205 super.jumpDrawablesToCurrentState();
7206 final View[] children = mChildren;
7207 final int count = mChildrenCount;
7208 for (int i = 0; i < count; i++) {
7209 children[i].jumpDrawablesToCurrentState();
7210 }
7211 }
7212
7213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 protected int[] onCreateDrawableState(int extraSpace) {
7215 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) {
7216 return super.onCreateDrawableState(extraSpace);
7217 }
7218
7219 int need = 0;
7220 int n = getChildCount();
7221 for (int i = 0; i < n; i++) {
7222 int[] childState = getChildAt(i).getDrawableState();
7223
7224 if (childState != null) {
7225 need += childState.length;
7226 }
7227 }
7228
7229 int[] state = super.onCreateDrawableState(extraSpace + need);
7230
7231 for (int i = 0; i < n; i++) {
7232 int[] childState = getChildAt(i).getDrawableState();
7233
7234 if (childState != null) {
7235 state = mergeDrawableStates(state, childState);
7236 }
7237 }
7238
7239 return state;
7240 }
7241
7242 /**
7243 * Sets whether this ViewGroup's drawable states also include
7244 * its children's drawable states. This is used, for example, to
7245 * make a group appear to be focused when its child EditText or button
7246 * is focused.
7247 */
7248 public void setAddStatesFromChildren(boolean addsStates) {
7249 if (addsStates) {
7250 mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN;
7251 } else {
7252 mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN;
7253 }
7254
7255 refreshDrawableState();
7256 }
7257
7258 /**
7259 * Returns whether this ViewGroup's drawable states also include
7260 * its children's drawable states. This is used, for example, to
7261 * make a group appear to be focused when its child EditText or button
7262 * is focused.
7263 */
7264 public boolean addStatesFromChildren() {
7265 return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0;
7266 }
7267
7268 /**
Jeff Smitha45746e2012-07-19 14:19:24 -05007269 * If {@link #addStatesFromChildren} is true, refreshes this group's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 * drawable state (to include the states from its children).
7271 */
Alan Viverettebe463f22016-01-21 10:50:10 -05007272 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 public void childDrawableStateChanged(View child) {
7274 if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) {
7275 refreshDrawableState();
7276 }
7277 }
7278
7279 /**
7280 * Specifies the animation listener to which layout animation events must
7281 * be sent. Only
7282 * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)}
7283 * and
7284 * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)}
7285 * are invoked.
7286 *
7287 * @param animationListener the layout animation listener
7288 */
7289 public void setLayoutAnimationListener(Animation.AnimationListener animationListener) {
7290 mAnimationListener = animationListener;
7291 }
7292
7293 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007294 * This method is called by LayoutTransition when there are 'changing' animations that need
7295 * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who
7296 * starts all pending transitions prior to the drawing phase in the current traversal.
7297 *
7298 * @param transition The LayoutTransition to be started on the next traversal.
7299 *
7300 * @hide
7301 */
7302 public void requestTransitionStart(LayoutTransition transition) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007303 ViewRootImpl viewAncestor = getViewRootImpl();
Chet Haase1abf7fa2011-08-17 18:31:56 -07007304 if (viewAncestor != null) {
7305 viewAncestor.requestTransitionStart(transition);
7306 }
Chet Haasecca2c982011-05-20 14:34:18 -07007307 }
7308
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07007309 /**
7310 * @hide
7311 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07007312 @Override
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07007313 public boolean resolveRtlPropertiesIfNeeded() {
7314 final boolean result = super.resolveRtlPropertiesIfNeeded();
7315 // We dont need to resolve the children RTL properties if nothing has changed for the parent
7316 if (result) {
7317 int count = getChildCount();
7318 for (int i = 0; i < count; i++) {
7319 final View child = getChildAt(i);
7320 if (child.isLayoutDirectionInherited()) {
7321 child.resolveRtlPropertiesIfNeeded();
7322 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007323 }
7324 }
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -07007325 return result;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007326 }
7327
7328 /**
7329 * @hide
7330 */
7331 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07007332 public boolean resolveLayoutDirection() {
7333 final boolean result = super.resolveLayoutDirection();
7334 if (result) {
7335 int count = getChildCount();
7336 for (int i = 0; i < count; i++) {
7337 final View child = getChildAt(i);
7338 if (child.isLayoutDirectionInherited()) {
7339 child.resolveLayoutDirection();
7340 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007341 }
7342 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07007343 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007344 }
7345
7346 /**
7347 * @hide
7348 */
7349 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07007350 public boolean resolveTextDirection() {
7351 final boolean result = super.resolveTextDirection();
7352 if (result) {
7353 int count = getChildCount();
7354 for (int i = 0; i < count; i++) {
7355 final View child = getChildAt(i);
7356 if (child.isTextDirectionInherited()) {
7357 child.resolveTextDirection();
7358 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007359 }
7360 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07007361 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007362 }
7363
7364 /**
7365 * @hide
7366 */
7367 @Override
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07007368 public boolean resolveTextAlignment() {
7369 final boolean result = super.resolveTextAlignment();
7370 if (result) {
7371 int count = getChildCount();
7372 for (int i = 0; i < count; i++) {
7373 final View child = getChildAt(i);
7374 if (child.isTextAlignmentInherited()) {
7375 child.resolveTextAlignment();
7376 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007377 }
7378 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07007379 return result;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007380 }
7381
7382 /**
7383 * @hide
7384 */
7385 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01007386 @UnsupportedAppUsage
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007387 public void resolvePadding() {
7388 super.resolvePadding();
7389 int count = getChildCount();
7390 for (int i = 0; i < count; i++) {
7391 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08007392 if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007393 child.resolvePadding();
7394 }
7395 }
7396 }
7397
7398 /**
7399 * @hide
7400 */
7401 @Override
7402 protected void resolveDrawables() {
7403 super.resolveDrawables();
7404 int count = getChildCount();
7405 for (int i = 0; i < count; i++) {
7406 final View child = getChildAt(i);
Adam Powell05f35122014-11-10 17:47:37 -08007407 if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007408 child.resolveDrawables();
7409 }
7410 }
7411 }
7412
Fabrice Di Meglio1e0ed6b2012-10-18 16:06:52 -07007413 /**
7414 * @hide
7415 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -07007416 @Override
7417 public void resolveLayoutParams() {
7418 super.resolveLayoutParams();
7419 int count = getChildCount();
7420 for (int i = 0; i < count; i++) {
7421 final View child = getChildAt(i);
7422 child.resolveLayoutParams();
7423 }
7424 }
7425
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007426 /**
7427 * @hide
7428 */
Aurimas Liutikas80e4e182018-08-13 16:13:37 -07007429 @TestApi
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007430 @Override
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07007431 public void resetResolvedLayoutDirection() {
7432 super.resetResolvedLayoutDirection();
7433
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07007434 int count = getChildCount();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07007435 for (int i = 0; i < count; i++) {
7436 final View child = getChildAt(i);
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07007437 if (child.isLayoutDirectionInherited()) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07007438 child.resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07007439 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007440 }
7441 }
7442
7443 /**
7444 * @hide
7445 */
Aurimas Liutikas80e4e182018-08-13 16:13:37 -07007446 @TestApi
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007447 @Override
7448 public void resetResolvedTextDirection() {
7449 super.resetResolvedTextDirection();
7450
7451 int count = getChildCount();
7452 for (int i = 0; i < count; i++) {
7453 final View child = getChildAt(i);
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -07007454 if (child.isTextDirectionInherited()) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07007455 child.resetResolvedTextDirection();
7456 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007457 }
7458 }
7459
7460 /**
7461 * @hide
7462 */
Aurimas Liutikas80e4e182018-08-13 16:13:37 -07007463 @TestApi
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07007464 @Override
7465 public void resetResolvedTextAlignment() {
7466 super.resetResolvedTextAlignment();
7467
7468 int count = getChildCount();
7469 for (int i = 0; i < count; i++) {
7470 final View child = getChildAt(i);
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -07007471 if (child.isTextAlignmentInherited()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07007472 child.resetResolvedTextAlignment();
7473 }
7474 }
7475 }
7476
Fabrice Di Meglio22268862011-06-27 18:13:18 -07007477 /**
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007478 * @hide
7479 */
Aurimas Liutikas80e4e182018-08-13 16:13:37 -07007480 @TestApi
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007481 @Override
7482 public void resetResolvedPadding() {
7483 super.resetResolvedPadding();
7484
7485 int count = getChildCount();
7486 for (int i = 0; i < count; i++) {
7487 final View child = getChildAt(i);
7488 if (child.isLayoutDirectionInherited()) {
7489 child.resetResolvedPadding();
7490 }
7491 }
7492 }
7493
7494 /**
7495 * @hide
7496 */
Aurimas Liutikas80e4e182018-08-13 16:13:37 -07007497 @TestApi
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07007498 @Override
7499 protected void resetResolvedDrawables() {
7500 super.resetResolvedDrawables();
7501
7502 int count = getChildCount();
7503 for (int i = 0; i < count; i++) {
7504 final View child = getChildAt(i);
7505 if (child.isLayoutDirectionInherited()) {
7506 child.resetResolvedDrawables();
7507 }
7508 }
7509 }
7510
7511 /**
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007512 * Return true if the pressed state should be delayed for children or descendants of this
7513 * ViewGroup. Generally, this should be done for containers that can scroll, such as a List.
7514 * This prevents the pressed state from appearing when the user is actually trying to scroll
7515 * the content.
7516 *
7517 * The default implementation returns true for compatibility reasons. Subclasses that do
7518 * not scroll should generally override this method and return false.
7519 */
7520 public boolean shouldDelayChildPressedState() {
7521 return true;
7522 }
7523
Adam Powell10ba2772014-04-15 09:46:51 -07007524 /**
7525 * @inheritDoc
7526 */
7527 @Override
7528 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
7529 return false;
7530 }
7531
7532 /**
7533 * @inheritDoc
7534 */
7535 @Override
7536 public void onNestedScrollAccepted(View child, View target, int axes) {
7537 mNestedScrollAxes = axes;
7538 }
7539
7540 /**
7541 * @inheritDoc
7542 *
7543 * <p>The default implementation of onStopNestedScroll calls
7544 * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p>
7545 */
7546 @Override
7547 public void onStopNestedScroll(View child) {
7548 // Stop any recursive nested scrolling.
7549 stopNestedScroll();
Adam Powelld4a22d42015-04-16 15:44:10 -07007550 mNestedScrollAxes = 0;
Adam Powell10ba2772014-04-15 09:46:51 -07007551 }
7552
7553 /**
7554 * @inheritDoc
7555 */
7556 @Override
7557 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
7558 int dxUnconsumed, int dyUnconsumed) {
Chris Banes61d50702016-03-23 13:11:45 +00007559 // Re-dispatch up the tree by default
7560 dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
Adam Powell10ba2772014-04-15 09:46:51 -07007561 }
7562
7563 /**
7564 * @inheritDoc
7565 */
7566 @Override
7567 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
Chris Banes61d50702016-03-23 13:11:45 +00007568 // Re-dispatch up the tree by default
7569 dispatchNestedPreScroll(dx, dy, consumed, null);
Adam Powell10ba2772014-04-15 09:46:51 -07007570 }
7571
7572 /**
7573 * @inheritDoc
7574 */
7575 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07007576 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Chris Banes61d50702016-03-23 13:11:45 +00007577 // Re-dispatch up the tree by default
7578 return dispatchNestedFling(velocityX, velocityY, consumed);
Adam Powell10ba2772014-04-15 09:46:51 -07007579 }
7580
7581 /**
Adam Powellb72be592014-07-16 21:41:31 -07007582 * @inheritDoc
7583 */
7584 @Override
7585 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
Chris Banes61d50702016-03-23 13:11:45 +00007586 // Re-dispatch up the tree by default
7587 return dispatchNestedPreFling(velocityX, velocityY);
Adam Powellb72be592014-07-16 21:41:31 -07007588 }
7589
7590 /**
Adam Powell10ba2772014-04-15 09:46:51 -07007591 * Return the current axes of nested scrolling for this ViewGroup.
7592 *
7593 * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently
7594 * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p>
7595 *
7596 * @return Flags indicating the current axes of nested scrolling
7597 * @see #SCROLL_AXIS_HORIZONTAL
7598 * @see #SCROLL_AXIS_VERTICAL
7599 * @see #SCROLL_AXIS_NONE
7600 */
7601 public int getNestedScrollAxes() {
7602 return mNestedScrollAxes;
7603 }
7604
Philip Milned7dd8902012-01-26 16:55:30 -08007605 /** @hide */
7606 protected void onSetLayoutParams(View child, LayoutParams layoutParams) {
Robert Carr5429daa2017-04-03 19:00:26 -07007607 requestLayout();
Philip Milned7dd8902012-01-26 16:55:30 -08007608 }
7609
George Mounte1803372014-02-26 19:00:52 +00007610 /** @hide */
7611 @Override
7612 public void captureTransitioningViews(List<View> transitioningViews) {
7613 if (getVisibility() != View.VISIBLE) {
7614 return;
7615 }
7616 if (isTransitionGroup()) {
7617 transitioningViews.add(this);
7618 } else {
7619 int count = getChildCount();
7620 for (int i = 0; i < count; i++) {
7621 View child = getChildAt(i);
7622 child.captureTransitioningViews(transitioningViews);
7623 }
7624 }
7625 }
7626
7627 /** @hide */
7628 @Override
George Mountabb352a2014-05-09 10:27:20 -07007629 public void findNamedViews(Map<String, View> namedElements) {
George Mountfe361d22014-07-08 17:25:25 -07007630 if (getVisibility() != VISIBLE && mGhostView == null) {
George Mounte1803372014-02-26 19:00:52 +00007631 return;
7632 }
George Mountabb352a2014-05-09 10:27:20 -07007633 super.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00007634 int count = getChildCount();
7635 for (int i = 0; i < count; i++) {
7636 View child = getChildAt(i);
George Mountabb352a2014-05-09 10:27:20 -07007637 child.findNamedViews(namedElements);
George Mounte1803372014-02-26 19:00:52 +00007638 }
7639 }
7640
Evan Rosky4807ae22018-03-22 16:04:15 -07007641 @Override
7642 boolean hasUnhandledKeyListener() {
7643 return (mChildUnhandledKeyListeners > 0) || super.hasUnhandledKeyListener();
7644 }
7645
7646 void incrementChildUnhandledKeyListeners() {
7647 mChildUnhandledKeyListeners += 1;
7648 if (mChildUnhandledKeyListeners == 1) {
7649 if (mParent instanceof ViewGroup) {
7650 ((ViewGroup) mParent).incrementChildUnhandledKeyListeners();
7651 }
7652 }
7653 }
7654
7655 void decrementChildUnhandledKeyListeners() {
7656 mChildUnhandledKeyListeners -= 1;
7657 if (mChildUnhandledKeyListeners == 0) {
7658 if (mParent instanceof ViewGroup) {
7659 ((ViewGroup) mParent).decrementChildUnhandledKeyListeners();
7660 }
7661 }
7662 }
7663
7664 @Override
7665 View dispatchUnhandledKeyEvent(KeyEvent evt) {
7666 if (!hasUnhandledKeyListener()) {
7667 return null;
7668 }
7669 ArrayList<View> orderedViews = buildOrderedChildList();
7670 if (orderedViews != null) {
7671 try {
7672 for (int i = orderedViews.size() - 1; i >= 0; --i) {
7673 View v = orderedViews.get(i);
7674 View consumer = v.dispatchUnhandledKeyEvent(evt);
7675 if (consumer != null) {
7676 return consumer;
7677 }
7678 }
7679 } finally {
7680 orderedViews.clear();
7681 }
7682 } else {
7683 for (int i = getChildCount() - 1; i >= 0; --i) {
7684 View v = getChildAt(i);
7685 View consumer = v.dispatchUnhandledKeyEvent(evt);
7686 if (consumer != null) {
7687 return consumer;
7688 }
7689 }
7690 }
7691 if (onUnhandledKeyEvent(evt)) {
7692 return this;
7693 }
7694 return null;
7695 }
7696
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 * LayoutParams are used by views to tell their parents how they want to be
7699 * laid out. See
7700 * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes}
7701 * for a list of all child view attributes that this class supports.
Romain Guy8506ab42009-06-11 17:35:47 -07007702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 * <p>
7704 * The base LayoutParams class just describes how big the view wants to be
7705 * for both width and height. For each dimension, it can specify one of:
7706 * <ul>
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007707 * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
7708 * means that the view wants to be as big as its parent (minus padding)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 * <li> WRAP_CONTENT, which means that the view wants to be just big enough
7710 * to enclose its content (plus padding)
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007711 * <li> an exact number
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 * </ul>
7713 * There are subclasses of LayoutParams for different subclasses of
7714 * ViewGroup. For example, AbsoluteLayout has its own subclass of
Joe Fernandez558459f2011-10-13 16:47:36 -07007715 * LayoutParams which adds an X and Y value.</p>
7716 *
7717 * <div class="special reference">
7718 * <h3>Developer Guides</h3>
7719 * <p>For more information about creating user interface layouts, read the
7720 * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer
7721 * guide.</p></div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 *
7723 * @attr ref android.R.styleable#ViewGroup_Layout_layout_height
7724 * @attr ref android.R.styleable#ViewGroup_Layout_layout_width
7725 */
7726 public static class LayoutParams {
7727 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007728 * Special value for the height or width requested by a View.
7729 * FILL_PARENT means that the view wants to be as big as its parent,
7730 * minus the parent's padding, if any. This value is deprecated
7731 * starting in API Level 8 and replaced by {@link #MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 */
Romain Guy980a9382010-01-08 15:06:28 -08007733 @SuppressWarnings({"UnusedDeclaration"})
7734 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 public static final int FILL_PARENT = -1;
7736
7737 /**
7738 * Special value for the height or width requested by a View.
Gilles Debunnef5c6eff2010-02-09 19:08:36 -08007739 * MATCH_PARENT means that the view wants to be as big as its parent,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007740 * minus the parent's padding, if any. Introduced in API Level 8.
Romain Guy980a9382010-01-08 15:06:28 -08007741 */
7742 public static final int MATCH_PARENT = -1;
7743
7744 /**
7745 * Special value for the height or width requested by a View.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 * WRAP_CONTENT means that the view wants to be just large enough to fit
7747 * its own internal content, taking its own padding into account.
7748 */
7749 public static final int WRAP_CONTENT = -2;
7750
7751 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007752 * Information about how wide the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07007753 * constants FILL_PARENT (replaced by MATCH_PARENT
7754 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007756 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08007757 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
7759 })
7760 public int width;
7761
7762 /**
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007763 * Information about how tall the view wants to be. Can be one of the
Mark Dolinerd0646dc2014-08-27 16:04:02 -07007764 * constants FILL_PARENT (replaced by MATCH_PARENT
7765 * in API Level 8) or WRAP_CONTENT, or an exact size.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007767 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Romain Guy980a9382010-01-08 15:06:28 -08007768 @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT")
7770 })
7771 public int height;
7772
7773 /**
7774 * Used to animate layouts.
7775 */
7776 public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
7777
7778 /**
7779 * Creates a new set of layout parameters. The values are extracted from
7780 * the supplied attributes set and context. The XML attributes mapped
7781 * to this set of layout parameters are:
7782 *
7783 * <ul>
7784 * <li><code>layout_width</code>: the width, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007785 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
7786 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 * <li><code>layout_height</code>: the height, either an exact value,
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007788 * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by
7789 * {@link #MATCH_PARENT} in API Level 8)</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 * </ul>
7791 *
7792 * @param c the application environment
7793 * @param attrs the set of attributes from which to extract the layout
7794 * parameters' values
7795 */
7796 public LayoutParams(Context c, AttributeSet attrs) {
7797 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
7798 setBaseAttributes(a,
7799 R.styleable.ViewGroup_Layout_layout_width,
7800 R.styleable.ViewGroup_Layout_layout_height);
7801 a.recycle();
7802 }
7803
7804 /**
7805 * Creates a new set of layout parameters with the specified width
7806 * and height.
7807 *
Dirk Dougherty75c66da2010-03-25 16:33:33 -07007808 * @param width the width, either {@link #WRAP_CONTENT},
7809 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
7810 * API Level 8), or a fixed size in pixels
7811 * @param height the height, either {@link #WRAP_CONTENT},
7812 * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in
7813 * API Level 8), or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007814 */
7815 public LayoutParams(int width, int height) {
7816 this.width = width;
7817 this.height = height;
7818 }
7819
7820 /**
7821 * Copy constructor. Clones the width and height values of the source.
7822 *
7823 * @param source The layout params to copy from.
7824 */
7825 public LayoutParams(LayoutParams source) {
7826 this.width = source.width;
7827 this.height = source.height;
7828 }
7829
7830 /**
7831 * Used internally by MarginLayoutParams.
7832 * @hide
7833 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01007834 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 LayoutParams() {
7836 }
7837
7838 /**
Dave Burke579e1402012-10-18 20:41:55 -07007839 * Extracts the layout parameters from the supplied attributes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 *
7841 * @param a the style attributes to extract the parameters from
7842 * @param widthAttr the identifier of the width attribute
7843 * @param heightAttr the identifier of the height attribute
7844 */
7845 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
Dave Burke579e1402012-10-18 20:41:55 -07007846 width = a.getLayoutDimension(widthAttr, "layout_width");
7847 height = a.getLayoutDimension(heightAttr, "layout_height");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849
7850 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007851 * Resolve layout parameters depending on the layout direction. Subclasses that care about
7852 * layoutDirection changes should override this method. The default implementation does
7853 * nothing.
7854 *
7855 * @param layoutDirection the direction of the layout
7856 *
7857 * {@link View#LAYOUT_DIRECTION_LTR}
7858 * {@link View#LAYOUT_DIRECTION_RTL}
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007859 */
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07007860 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007861 }
7862
7863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 * Returns a String representation of this set of layout parameters.
7865 *
7866 * @param output the String to prepend to the internal representation
7867 * @return a String with the following format: output +
7868 * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"
Romain Guy8506ab42009-06-11 17:35:47 -07007869 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 * @hide
7871 */
7872 public String debug(String output) {
7873 return output + "ViewGroup.LayoutParams={ width="
7874 + sizeToString(width) + ", height=" + sizeToString(height) + " }";
7875 }
7876
7877 /**
Philip Milne10ca24a2012-04-23 15:38:27 -07007878 * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
7879 *
7880 * @param view the view that contains these layout parameters
7881 * @param canvas the canvas on which to draw
Alan Viverette2d7771c2018-01-31 17:04:31 -05007882 *
7883 * @hide
Philip Milne10ca24a2012-04-23 15:38:27 -07007884 */
Philip Milne7b757812012-09-19 18:13:44 -07007885 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
Philip Milne10ca24a2012-04-23 15:38:27 -07007886 }
7887
7888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 * Converts the specified size to a readable String.
7890 *
7891 * @param size the size to convert
7892 * @return a String instance representing the supplied size
Romain Guy8506ab42009-06-11 17:35:47 -07007893 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 * @hide
7895 */
7896 protected static String sizeToString(int size) {
7897 if (size == WRAP_CONTENT) {
7898 return "wrap-content";
7899 }
Romain Guy980a9382010-01-08 15:06:28 -08007900 if (size == MATCH_PARENT) {
7901 return "match-parent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 }
7903 return String.valueOf(size);
7904 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07007905
7906 /** @hide */
7907 void encode(@NonNull ViewHierarchyEncoder encoder) {
7908 encoder.beginObject(this);
7909 encodeProperties(encoder);
7910 encoder.endObject();
7911 }
7912
7913 /** @hide */
7914 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
7915 encoder.addProperty("width", width);
7916 encoder.addProperty("height", height);
7917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 }
7919
7920 /**
7921 * Per-child layout information for layouts that support margins.
7922 * See
7923 * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes}
7924 * for a list of all child view attributes that this class supports.
Chet Haase353d3972017-06-29 07:54:19 -07007925 *
7926 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_margin
7927 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginHorizontal
7928 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginVertical
7929 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
7930 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
7931 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
7932 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
7933 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
7934 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 */
7936 public static class MarginLayoutParams extends ViewGroup.LayoutParams {
7937 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007938 * The left margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007939 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7940 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007942 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 public int leftMargin;
7944
7945 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007946 * The top margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007947 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7948 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007950 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951 public int topMargin;
7952
7953 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007954 * The right margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007955 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7956 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007958 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 public int rightMargin;
7960
7961 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007962 * The bottom margin in pixels of the child. Margin values should be positive.
Philip Milned7dd8902012-01-26 16:55:30 -08007963 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7964 * to this field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007966 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007967 public int bottomMargin;
7968
7969 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007970 * The start margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007971 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7972 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007973 */
7974 @ViewDebug.ExportedProperty(category = "layout")
Mathew Inwooda570dee2018-08-17 14:56:00 +01007975 @UnsupportedAppUsage
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007976 private int startMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007977
7978 /**
Adam Powella7a735f2014-10-09 12:54:52 -07007979 * The end margin in pixels of the child. Margin values should be positive.
Fabrice Di Meglio54546f22012-02-14 16:26:16 -08007980 * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value
7981 * to this field.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007982 */
7983 @ViewDebug.ExportedProperty(category = "layout")
Mathew Inwooda570dee2018-08-17 14:56:00 +01007984 @UnsupportedAppUsage
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007985 private int endMargin = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007986
7987 /**
7988 * The default start and end margin.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07007989 * @hide
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007990 */
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07007991 public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07007992
Fabrice Di Megliob365f912013-03-27 16:36:21 -07007993 /**
7994 * Bit 0: layout direction
7995 * Bit 1: layout direction
7996 * Bit 2: left margin undefined
7997 * Bit 3: right margin undefined
7998 * Bit 4: is RTL compatibility mode
7999 * Bit 5: need resolution
8000 *
8001 * Bit 6 to 7 not used
8002 *
8003 * @hide
8004 */
8005 @ViewDebug.ExportedProperty(category = "layout", flagMapping = {
8006 @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK,
8007 equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"),
8008 @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK,
8009 equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"),
8010 @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK,
8011 equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"),
8012 @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK,
8013 equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"),
8014 @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK,
8015 equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK")
Jon Miranda4597e982014-07-29 07:25:49 -07008016 }, formatToHexString = true)
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008017 byte mMarginFlags;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008018
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008019 private static final int LAYOUT_DIRECTION_MASK = 0x00000003;
8020 private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004;
8021 private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008;
8022 private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010;
8023 private static final int NEED_RESOLUTION_MASK = 0x00000020;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008024
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008025 private static final int DEFAULT_MARGIN_RESOLVED = 0;
8026 private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008027
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 * Creates a new set of layout parameters. The values are extracted from
8030 * the supplied attributes set and context.
8031 *
8032 * @param c the application environment
8033 * @param attrs the set of attributes from which to extract the layout
8034 * parameters' values
8035 */
8036 public MarginLayoutParams(Context c, AttributeSet attrs) {
8037 super();
8038
8039 TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
8040 setBaseAttributes(a,
8041 R.styleable.ViewGroup_MarginLayout_layout_width,
8042 R.styleable.ViewGroup_MarginLayout_layout_height);
8043
8044 int margin = a.getDimensionPixelSize(
8045 com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
8046 if (margin >= 0) {
8047 leftMargin = margin;
8048 topMargin = margin;
8049 rightMargin= margin;
8050 bottomMargin = margin;
8051 } else {
Chet Haase40b2de52016-11-28 16:11:42 -08008052 int horizontalMargin = a.getDimensionPixelSize(
8053 R.styleable.ViewGroup_MarginLayout_layout_marginHorizontal, -1);
8054 int verticalMargin = a.getDimensionPixelSize(
8055 R.styleable.ViewGroup_MarginLayout_layout_marginVertical, -1);
8056
8057 if (horizontalMargin >= 0) {
8058 leftMargin = horizontalMargin;
8059 rightMargin = horizontalMargin;
8060 } else {
8061 leftMargin = a.getDimensionPixelSize(
8062 R.styleable.ViewGroup_MarginLayout_layout_marginLeft,
8063 UNDEFINED_MARGIN);
8064 if (leftMargin == UNDEFINED_MARGIN) {
8065 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
8066 leftMargin = DEFAULT_MARGIN_RESOLVED;
8067 }
8068 rightMargin = a.getDimensionPixelSize(
8069 R.styleable.ViewGroup_MarginLayout_layout_marginRight,
8070 UNDEFINED_MARGIN);
8071 if (rightMargin == UNDEFINED_MARGIN) {
8072 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
8073 rightMargin = DEFAULT_MARGIN_RESOLVED;
8074 }
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008075 }
8076
Chet Haasecb3d0232017-05-24 18:27:14 -07008077 startMargin = a.getDimensionPixelSize(
8078 R.styleable.ViewGroup_MarginLayout_layout_marginStart,
8079 DEFAULT_MARGIN_RELATIVE);
8080 endMargin = a.getDimensionPixelSize(
8081 R.styleable.ViewGroup_MarginLayout_layout_marginEnd,
8082 DEFAULT_MARGIN_RELATIVE);
8083
Chet Haase40b2de52016-11-28 16:11:42 -08008084 if (verticalMargin >= 0) {
8085 topMargin = verticalMargin;
8086 bottomMargin = verticalMargin;
8087 } else {
8088 topMargin = a.getDimensionPixelSize(
8089 R.styleable.ViewGroup_MarginLayout_layout_marginTop,
8090 DEFAULT_MARGIN_RESOLVED);
8091 bottomMargin = a.getDimensionPixelSize(
8092 R.styleable.ViewGroup_MarginLayout_layout_marginBottom,
8093 DEFAULT_MARGIN_RESOLVED);
8094 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008095
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008096 if (isMarginRelative()) {
8097 mMarginFlags |= NEED_RESOLUTION_MASK;
8098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 }
8100
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008101 final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
8102 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008103 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
8104 mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
8105 }
8106
8107 // Layout direction is LTR by default
8108 mMarginFlags |= LAYOUT_DIRECTION_LTR;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 a.recycle();
8111 }
8112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 public MarginLayoutParams(int width, int height) {
8114 super(width, height);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008115
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008116 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
8117 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008118
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008119 mMarginFlags &= ~NEED_RESOLUTION_MASK;
8120 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122
8123 /**
8124 * Copy constructor. Clones the width, height and margin values of the source.
8125 *
8126 * @param source The layout params to copy from.
8127 */
8128 public MarginLayoutParams(MarginLayoutParams source) {
8129 this.width = source.width;
8130 this.height = source.height;
8131
8132 this.leftMargin = source.leftMargin;
8133 this.topMargin = source.topMargin;
8134 this.rightMargin = source.rightMargin;
8135 this.bottomMargin = source.bottomMargin;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008136 this.startMargin = source.startMargin;
8137 this.endMargin = source.endMargin;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008138
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008139 this.mMarginFlags = source.mMarginFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 }
8141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 public MarginLayoutParams(LayoutParams source) {
8143 super(source);
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008144
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008145 mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK;
8146 mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK;
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008147
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008148 mMarginFlags &= ~NEED_RESOLUTION_MASK;
8149 mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 }
8151
8152 /**
Adam Powelld7600832014-07-01 15:22:50 -07008153 * @hide Used internally.
8154 */
8155 public final void copyMarginsFrom(MarginLayoutParams source) {
8156 this.leftMargin = source.leftMargin;
8157 this.topMargin = source.topMargin;
8158 this.rightMargin = source.rightMargin;
8159 this.bottomMargin = source.bottomMargin;
8160 this.startMargin = source.startMargin;
8161 this.endMargin = source.endMargin;
8162
8163 this.mMarginFlags = source.mMarginFlags;
8164 }
8165
8166 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008167 * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs
8168 * to be done so that the new margins are taken into account. Left and right margins may be
koprivadebd4ee2018-09-13 10:59:46 -07008169 * overridden by {@link android.view.View#requestLayout()} depending on layout direction.
Adam Powella7a735f2014-10-09 12:54:52 -07008170 * Margin values should be positive.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 *
8172 * @param left the left margin size
8173 * @param top the top margin size
8174 * @param right the right margin size
8175 * @param bottom the bottom margin size
8176 *
8177 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft
8178 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
8179 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight
8180 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
8181 */
8182 public void setMargins(int left, int top, int right, int bottom) {
8183 leftMargin = left;
8184 topMargin = top;
8185 rightMargin = right;
8186 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008187 mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
8188 mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
8189 if (isMarginRelative()) {
8190 mMarginFlags |= NEED_RESOLUTION_MASK;
8191 } else {
8192 mMarginFlags &= ~NEED_RESOLUTION_MASK;
8193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008194 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008195
8196 /**
8197 * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()}
8198 * needs to be done so that the new relative margins are taken into account. Left and right
koprivadebd4ee2018-09-13 10:59:46 -07008199 * margins may be overridden by {@link android.view.View#requestLayout()} depending on
8200 * layout direction. Margin values should be positive.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008201 *
8202 * @param start the start margin size
8203 * @param top the top margin size
8204 * @param end the right margin size
8205 * @param bottom the bottom margin size
8206 *
8207 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
8208 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop
8209 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
8210 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom
8211 *
8212 * @hide
8213 */
Mathew Inwooda570dee2018-08-17 14:56:00 +01008214 @UnsupportedAppUsage
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008215 public void setMarginsRelative(int start, int top, int end, int bottom) {
8216 startMargin = start;
8217 topMargin = top;
8218 endMargin = end;
8219 bottomMargin = bottom;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008220 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008221 }
8222
8223 /**
Adam Powella7a735f2014-10-09 12:54:52 -07008224 * Sets the relative start margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07008225 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07008226 * @param start the start margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07008227 *
8228 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
8229 */
8230 public void setMarginStart(int start) {
8231 startMargin = start;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008232 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07008233 }
8234
8235 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008236 * Returns the start margin in pixels.
8237 *
8238 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
8239 *
8240 * @return the start margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008241 */
8242 public int getMarginStart() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008243 if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008244 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008245 doResolveMargins();
8246 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008247 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008248 case View.LAYOUT_DIRECTION_RTL:
8249 return rightMargin;
8250 case View.LAYOUT_DIRECTION_LTR:
8251 default:
8252 return leftMargin;
8253 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008254 }
8255
8256 /**
Adam Powella7a735f2014-10-09 12:54:52 -07008257 * Sets the relative end margin. Margin values should be positive.
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07008258 *
Fabrice Di Meglio61a21772012-09-12 16:33:13 -07008259 * @param end the end margin size
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07008260 *
8261 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
8262 */
8263 public void setMarginEnd(int end) {
8264 endMargin = end;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008265 mMarginFlags |= NEED_RESOLUTION_MASK;
Fabrice Di Meglioa40627d2012-09-11 16:47:21 -07008266 }
8267
8268 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008269 * Returns the end margin in pixels.
8270 *
8271 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
8272 *
8273 * @return the end margin in pixels.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008274 */
8275 public int getMarginEnd() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008276 if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008277 if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008278 doResolveMargins();
8279 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008280 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008281 case View.LAYOUT_DIRECTION_RTL:
8282 return leftMargin;
8283 case View.LAYOUT_DIRECTION_LTR:
8284 default:
8285 return rightMargin;
8286 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008287 }
8288
8289 /**
8290 * Check if margins are relative.
8291 *
8292 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart
8293 * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd
8294 *
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008295 * @return true if either marginStart or marginEnd has been set.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008296 */
8297 public boolean isMarginRelative() {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008298 return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE);
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008299 }
8300
8301 /**
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008302 * Set the layout direction
8303 * @param layoutDirection the layout direction.
8304 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
8305 * or {@link View#LAYOUT_DIRECTION_RTL}.
8306 */
8307 public void setLayoutDirection(int layoutDirection) {
8308 if (layoutDirection != View.LAYOUT_DIRECTION_LTR &&
8309 layoutDirection != View.LAYOUT_DIRECTION_RTL) return;
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008310 if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) {
8311 mMarginFlags &= ~LAYOUT_DIRECTION_MASK;
8312 mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK);
8313 if (isMarginRelative()) {
8314 mMarginFlags |= NEED_RESOLUTION_MASK;
8315 } else {
8316 mMarginFlags &= ~NEED_RESOLUTION_MASK;
8317 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008318 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008319 }
8320
8321 /**
8322 * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or
8323 * {@link View#LAYOUT_DIRECTION_RTL}.
8324 *
8325 * @return the layout direction.
8326 */
8327 public int getLayoutDirection() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008328 return (mMarginFlags & LAYOUT_DIRECTION_MASK);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008329 }
8330
8331 /**
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008332 * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08008333 * may be overridden depending on layout direction.
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008334 */
8335 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07008336 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008337 setLayoutDirection(layoutDirection);
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008338
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008339 // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything
8340 // Will use the left and right margins if no relative margin is defined.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008341 if (!isMarginRelative() ||
8342 (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return;
Fabrice Di Meglio69bd5582012-07-02 13:17:24 -07008343
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07008344 // Proceed with resolution
8345 doResolveMargins();
8346 }
8347
8348 private void doResolveMargins() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008349 if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008350 // if left or right margins are not defined and if we have some start or end margin
8351 // defined then use those start and end margins.
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008352 if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK
8353 && startMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008354 leftMargin = startMargin;
8355 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008356 if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK
8357 && endMargin > DEFAULT_MARGIN_RELATIVE) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008358 rightMargin = endMargin;
8359 }
8360 } else {
8361 // We have some relative margins (either the start one or the end one or both). So use
8362 // them and override what has been defined for left and right margins. If either start
8363 // or end margin is not defined, just set it to default "0".
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008364 switch(mMarginFlags & LAYOUT_DIRECTION_MASK) {
Fabrice Di Meglio02a7d562013-03-27 12:41:22 -07008365 case View.LAYOUT_DIRECTION_RTL:
8366 leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
8367 endMargin : DEFAULT_MARGIN_RESOLVED;
8368 rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
8369 startMargin : DEFAULT_MARGIN_RESOLVED;
8370 break;
8371 case View.LAYOUT_DIRECTION_LTR:
8372 default:
8373 leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ?
8374 startMargin : DEFAULT_MARGIN_RESOLVED;
8375 rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ?
8376 endMargin : DEFAULT_MARGIN_RESOLVED;
8377 break;
8378 }
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008379 }
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008380 mMarginFlags &= ~NEED_RESOLUTION_MASK;
Fabrice Di Megliob76023a2011-06-20 17:41:21 -07008381 }
Philip Milne10ca24a2012-04-23 15:38:27 -07008382
Fabrice Di Meglio03b8d3a2012-09-27 17:05:27 -07008383 /**
8384 * @hide
8385 */
8386 public boolean isLayoutRtl() {
Fabrice Di Megliob365f912013-03-27 16:36:21 -07008387 return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07008388 }
8389
Alan Viverette2d7771c2018-01-31 17:04:31 -05008390 /**
8391 * @hide
8392 */
Philip Milne10ca24a2012-04-23 15:38:27 -07008393 @Override
Philip Milne7b757812012-09-19 18:13:44 -07008394 public void onDebugDraw(View view, Canvas canvas, Paint paint) {
8395 Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE;
8396
8397 fillDifference(canvas,
8398 view.getLeft() + oi.left,
8399 view.getTop() + oi.top,
8400 view.getRight() - oi.right,
8401 view.getBottom() - oi.bottom,
8402 leftMargin,
8403 topMargin,
8404 rightMargin,
8405 bottomMargin,
8406 paint);
Philip Milne10ca24a2012-04-23 15:38:27 -07008407 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07008408
8409 /** @hide */
8410 @Override
8411 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
8412 super.encodeProperties(encoder);
8413 encoder.addProperty("leftMargin", leftMargin);
8414 encoder.addProperty("topMargin", topMargin);
8415 encoder.addProperty("rightMargin", rightMargin);
8416 encoder.addProperty("bottomMargin", bottomMargin);
8417 encoder.addProperty("startMargin", startMargin);
8418 encoder.addProperty("endMargin", endMargin);
8419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 }
Adam Powell2b342f02010-08-18 18:14:13 -07008421
Jeff Brown20e987b2010-08-23 12:01:02 -07008422 /* Describes a touched view and the ids of the pointers that it has captured.
8423 *
8424 * This code assumes that pointer ids are always in the range 0..31 such that
8425 * it can use a bitfield to track which pointer ids are present.
8426 * As it happens, the lower layers of the input dispatch pipeline also use the
8427 * same trick so the assumption should be safe here...
8428 */
8429 private static final class TouchTarget {
8430 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07008431 private static final Object sRecycleLock = new Object[0];
Jeff Brown20e987b2010-08-23 12:01:02 -07008432 private static TouchTarget sRecycleBin;
8433 private static int sRecycledCount;
Adam Powell2b342f02010-08-18 18:14:13 -07008434
Jeff Brown20e987b2010-08-23 12:01:02 -07008435 public static final int ALL_POINTER_IDS = -1; // all ones
Adam Powell2b342f02010-08-18 18:14:13 -07008436
Jeff Brown20e987b2010-08-23 12:01:02 -07008437 // The touched child view.
Mathew Inwooda570dee2018-08-17 14:56:00 +01008438 @UnsupportedAppUsage
Jeff Brown20e987b2010-08-23 12:01:02 -07008439 public View child;
8440
8441 // The combined bit mask of pointer ids for all pointers captured by the target.
8442 public int pointerIdBits;
8443
8444 // The next target in the target list.
8445 public TouchTarget next;
8446
Mathew Inwooda570dee2018-08-17 14:56:00 +01008447 @UnsupportedAppUsage
Jeff Brown20e987b2010-08-23 12:01:02 -07008448 private TouchTarget() {
Adam Powell2b342f02010-08-18 18:14:13 -07008449 }
8450
Alan Viverettea7b85e62016-01-22 10:14:02 -05008451 public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {
8452 if (child == null) {
8453 throw new IllegalArgumentException("child must be non-null");
8454 }
8455
Jeff Brown20e987b2010-08-23 12:01:02 -07008456 final TouchTarget target;
8457 synchronized (sRecycleLock) {
Adam Powell816c3be2010-08-23 18:00:05 -07008458 if (sRecycleBin == null) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008459 target = new TouchTarget();
Adam Powell816c3be2010-08-23 18:00:05 -07008460 } else {
Jeff Brown20e987b2010-08-23 12:01:02 -07008461 target = sRecycleBin;
8462 sRecycleBin = target.next;
8463 sRecycledCount--;
8464 target.next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07008465 }
Adam Powell816c3be2010-08-23 18:00:05 -07008466 }
Jeff Brown20e987b2010-08-23 12:01:02 -07008467 target.child = child;
8468 target.pointerIdBits = pointerIdBits;
8469 return target;
8470 }
Adam Powell816c3be2010-08-23 18:00:05 -07008471
Jeff Brown20e987b2010-08-23 12:01:02 -07008472 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05008473 if (child == null) {
8474 throw new IllegalStateException("already recycled once");
8475 }
8476
Jeff Brown20e987b2010-08-23 12:01:02 -07008477 synchronized (sRecycleLock) {
8478 if (sRecycledCount < MAX_RECYCLED) {
8479 next = sRecycleBin;
8480 sRecycleBin = this;
8481 sRecycledCount += 1;
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07008482 } else {
8483 next = null;
Adam Powell816c3be2010-08-23 18:00:05 -07008484 }
Patrick Dubroyfb0547d22010-10-19 17:36:18 -07008485 child = null;
Adam Powell816c3be2010-08-23 18:00:05 -07008486 }
8487 }
Adam Powell2b342f02010-08-18 18:14:13 -07008488 }
Jeff Brown87b7f802011-06-21 18:35:45 -07008489
8490 /* Describes a hovered view. */
8491 private static final class HoverTarget {
8492 private static final int MAX_RECYCLED = 32;
Romain Guy6410c0a2013-06-17 11:21:58 -07008493 private static final Object sRecycleLock = new Object[0];
Jeff Brown87b7f802011-06-21 18:35:45 -07008494 private static HoverTarget sRecycleBin;
8495 private static int sRecycledCount;
8496
8497 // The hovered child view.
8498 public View child;
8499
8500 // The next target in the target list.
8501 public HoverTarget next;
8502
8503 private HoverTarget() {
8504 }
8505
Alan Viverettea7b85e62016-01-22 10:14:02 -05008506 public static HoverTarget obtain(@NonNull View child) {
8507 if (child == null) {
8508 throw new IllegalArgumentException("child must be non-null");
8509 }
8510
Jeff Brown87b7f802011-06-21 18:35:45 -07008511 final HoverTarget target;
8512 synchronized (sRecycleLock) {
8513 if (sRecycleBin == null) {
8514 target = new HoverTarget();
8515 } else {
8516 target = sRecycleBin;
8517 sRecycleBin = target.next;
Alan Viverettea7b85e62016-01-22 10:14:02 -05008518 sRecycledCount--;
Jeff Brown87b7f802011-06-21 18:35:45 -07008519 target.next = null;
8520 }
8521 }
8522 target.child = child;
8523 return target;
8524 }
8525
8526 public void recycle() {
Alan Viverettea7b85e62016-01-22 10:14:02 -05008527 if (child == null) {
8528 throw new IllegalStateException("already recycled once");
8529 }
8530
Jeff Brown87b7f802011-06-21 18:35:45 -07008531 synchronized (sRecycleLock) {
8532 if (sRecycledCount < MAX_RECYCLED) {
8533 next = sRecycleBin;
8534 sRecycleBin = this;
8535 sRecycledCount += 1;
8536 } else {
8537 next = null;
8538 }
8539 child = null;
8540 }
8541 }
8542 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008543
8544 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07008545 * Pooled class that to hold the children for autifill.
8546 */
8547 static class ChildListForAutoFill extends ArrayList<View> {
8548 private static final int MAX_POOL_SIZE = 32;
8549
8550 private static final Pools.SimplePool<ChildListForAutoFill> sPool =
8551 new Pools.SimplePool<>(MAX_POOL_SIZE);
8552
8553 public static ChildListForAutoFill obtain() {
8554 ChildListForAutoFill list = sPool.acquire();
8555 if (list == null) {
8556 list = new ChildListForAutoFill();
8557 }
8558 return list;
8559 }
8560
8561 public void recycle() {
8562 clear();
8563 sPool.release(this);
8564 }
8565 }
8566
8567 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07008568 * Pooled class that orderes the children of a ViewGroup from start
8569 * to end based on how they are laid out and the layout direction.
8570 */
8571 static class ChildListForAccessibility {
8572
8573 private static final int MAX_POOL_SIZE = 32;
8574
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008575 private static final SynchronizedPool<ChildListForAccessibility> sPool =
8576 new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008577
8578 private final ArrayList<View> mChildren = new ArrayList<View>();
8579
8580 private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();
8581
8582 public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008583 ChildListForAccessibility list = sPool.acquire();
8584 if (list == null) {
8585 list = new ChildListForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008586 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008587 list.init(parent, sort);
8588 return list;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008589 }
8590
8591 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008592 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008593 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008594 }
8595
8596 public int getChildCount() {
8597 return mChildren.size();
8598 }
8599
8600 public View getChildAt(int index) {
8601 return mChildren.get(index);
8602 }
8603
Svetoslav Ganov42138042012-03-20 11:51:39 -07008604 private void init(ViewGroup parent, boolean sort) {
8605 ArrayList<View> children = mChildren;
8606 final int childCount = parent.getChildCount();
8607 for (int i = 0; i < childCount; i++) {
8608 View child = parent.getChildAt(i);
8609 children.add(child);
8610 }
8611 if (sort) {
8612 ArrayList<ViewLocationHolder> holders = mHolders;
8613 for (int i = 0; i < childCount; i++) {
8614 View child = children.get(i);
8615 ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child);
8616 holders.add(holder);
8617 }
Svetoslav88e447b2014-10-09 15:49:02 -07008618 sort(holders);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008619 for (int i = 0; i < childCount; i++) {
8620 ViewLocationHolder holder = holders.get(i);
8621 children.set(i, holder.mView);
8622 holder.recycle();
8623 }
8624 holders.clear();
8625 }
8626 }
8627
Svetoslav88e447b2014-10-09 15:49:02 -07008628 private void sort(ArrayList<ViewLocationHolder> holders) {
8629 // This is gross but the least risky solution. The current comparison
8630 // strategy breaks transitivity but produces very good results. Coming
8631 // up with a new strategy requires time which we do not have, so ...
8632 try {
8633 ViewLocationHolder.setComparisonStrategy(
8634 ViewLocationHolder.COMPARISON_STRATEGY_STRIPE);
8635 Collections.sort(holders);
8636 } catch (IllegalArgumentException iae) {
8637 // Note that in practice this occurs extremely rarely in a couple
8638 // of pathological cases.
8639 ViewLocationHolder.setComparisonStrategy(
8640 ViewLocationHolder.COMPARISON_STRATEGY_LOCATION);
8641 Collections.sort(holders);
8642 }
8643 }
8644
Svetoslav Ganov42138042012-03-20 11:51:39 -07008645 private void clear() {
8646 mChildren.clear();
8647 }
8648 }
8649
8650 /**
8651 * Pooled class that holds a View and its location with respect to
8652 * a specified root. This enables sorting of views based on their
8653 * coordinates without recomputing the position relative to the root
8654 * on every comparison.
8655 */
8656 static class ViewLocationHolder implements Comparable<ViewLocationHolder> {
8657
8658 private static final int MAX_POOL_SIZE = 32;
8659
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008660 private static final SynchronizedPool<ViewLocationHolder> sPool =
8661 new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008662
Svetoslav88e447b2014-10-09 15:49:02 -07008663 public static final int COMPARISON_STRATEGY_STRIPE = 1;
8664
8665 public static final int COMPARISON_STRATEGY_LOCATION = 2;
8666
8667 private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE;
8668
Svetoslav Ganov42138042012-03-20 11:51:39 -07008669 private final Rect mLocation = new Rect();
8670
Phil Weaver86b32602018-05-10 18:00:18 -07008671 private ViewGroup mRoot;
8672
Svetoslav Ganov42138042012-03-20 11:51:39 -07008673 public View mView;
8674
8675 private int mLayoutDirection;
8676
8677 public static ViewLocationHolder obtain(ViewGroup root, View view) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008678 ViewLocationHolder holder = sPool.acquire();
8679 if (holder == null) {
8680 holder = new ViewLocationHolder();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008681 }
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008682 holder.init(root, view);
8683 return holder;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008684 }
8685
Svetoslav88e447b2014-10-09 15:49:02 -07008686 public static void setComparisonStrategy(int strategy) {
8687 sComparisonStrategy = strategy;
8688 }
8689
Svetoslav Ganov42138042012-03-20 11:51:39 -07008690 public void recycle() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008691 clear();
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -08008692 sPool.release(this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008693 }
8694
8695 @Override
8696 public int compareTo(ViewLocationHolder another) {
8697 // This instance is greater than an invalid argument.
8698 if (another == null) {
8699 return 1;
8700 }
Svetoslav88e447b2014-10-09 15:49:02 -07008701
Phil Weaver86b32602018-05-10 18:00:18 -07008702 int boundsResult = compareBoundsOfTree(this, another);
8703 if (boundsResult != 0) {
8704 return boundsResult;
8705 }
8706
8707 // Just break the tie somehow. The accessibility ids are unique
8708 // and stable, hence this is deterministic tie breaking.
8709 return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId();
8710 }
8711
8712 /**
8713 * Compare two views based on their bounds. Use the bounds of their children to break ties.
8714 *
8715 * @param holder1 Holder of first view to compare
Phil Weaver6adf3a12018-08-21 16:08:42 -07008716 * @param holder2 Holder of second view to compare. Must have the same root as holder1.
Phil Weaver86b32602018-05-10 18:00:18 -07008717 * @return The compare result, with equality if no good comparison was found.
8718 */
8719 private static int compareBoundsOfTree(
8720 ViewLocationHolder holder1, ViewLocationHolder holder2) {
Svetoslav88e447b2014-10-09 15:49:02 -07008721 if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) {
8722 // First is above second.
Phil Weaver86b32602018-05-10 18:00:18 -07008723 if (holder1.mLocation.bottom - holder2.mLocation.top <= 0) {
Svetoslav88e447b2014-10-09 15:49:02 -07008724 return -1;
8725 }
8726 // First is below second.
Phil Weaver86b32602018-05-10 18:00:18 -07008727 if (holder1.mLocation.top - holder2.mLocation.bottom >= 0) {
Svetoslav88e447b2014-10-09 15:49:02 -07008728 return 1;
8729 }
8730 }
8731
Svetoslav04cab1b2014-08-25 18:35:57 -07008732 // We are ordering left-to-right, top-to-bottom.
Phil Weaver86b32602018-05-10 18:00:18 -07008733 if (holder1.mLayoutDirection == LAYOUT_DIRECTION_LTR) {
8734 final int leftDifference = holder1.mLocation.left - holder2.mLocation.left;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008735 if (leftDifference != 0) {
8736 return leftDifference;
8737 }
8738 } else { // RTL
Phil Weaver86b32602018-05-10 18:00:18 -07008739 final int rightDifference = holder1.mLocation.right - holder2.mLocation.right;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008740 if (rightDifference != 0) {
8741 return -rightDifference;
8742 }
8743 }
Svetoslav04cab1b2014-08-25 18:35:57 -07008744 // We are ordering left-to-right, top-to-bottom.
Phil Weaver86b32602018-05-10 18:00:18 -07008745 final int topDifference = holder1.mLocation.top - holder2.mLocation.top;
Svetoslav04cab1b2014-08-25 18:35:57 -07008746 if (topDifference != 0) {
8747 return topDifference;
8748 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008749 // Break tie by height.
Phil Weaver86b32602018-05-10 18:00:18 -07008750 final int heightDiference = holder1.mLocation.height() - holder2.mLocation.height();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008751 if (heightDiference != 0) {
8752 return -heightDiference;
8753 }
8754 // Break tie by width.
Phil Weaver86b32602018-05-10 18:00:18 -07008755 final int widthDifference = holder1.mLocation.width() - holder2.mLocation.width();
8756 if (widthDifference != 0) {
8757 return -widthDifference;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008758 }
Phil Weaver86b32602018-05-10 18:00:18 -07008759
8760 // Find a child of each view with different screen bounds.
8761 final Rect view1Bounds = new Rect();
8762 final Rect view2Bounds = new Rect();
8763 final Rect tempRect = new Rect();
8764 holder1.mView.getBoundsOnScreen(view1Bounds, true);
8765 holder2.mView.getBoundsOnScreen(view2Bounds, true);
8766 final View child1 = holder1.mView.findViewByPredicateTraversal((view) -> {
8767 view.getBoundsOnScreen(tempRect, true);
8768 return !tempRect.equals(view1Bounds);
8769 }, null);
8770 final View child2 = holder2.mView.findViewByPredicateTraversal((view) -> {
8771 view.getBoundsOnScreen(tempRect, true);
8772 return !tempRect.equals(view2Bounds);
8773 }, null);
8774
8775
8776 // Compare the children recursively
8777 if ((child1 != null) && (child2 != null)) {
8778 final ViewLocationHolder childHolder1 =
8779 ViewLocationHolder.obtain(holder1.mRoot, child1);
8780 final ViewLocationHolder childHolder2 =
8781 ViewLocationHolder.obtain(holder1.mRoot, child2);
8782 return compareBoundsOfTree(childHolder1, childHolder2);
8783 }
8784
8785 // If only one has a child, use that one
8786 if (child1 != null) {
8787 return 1;
8788 }
8789
8790 if (child2 != null) {
8791 return -1;
8792 }
8793
8794 // Give up
8795 return 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008796 }
8797
8798 private void init(ViewGroup root, View view) {
8799 Rect viewLocation = mLocation;
8800 view.getDrawingRect(viewLocation);
8801 root.offsetDescendantRectToMyCoords(view, viewLocation);
8802 mView = view;
Phil Weaver86b32602018-05-10 18:00:18 -07008803 mRoot = root;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07008804 mLayoutDirection = root.getLayoutDirection();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008805 }
8806
8807 private void clear() {
8808 mView = null;
Phil Weaver6adf3a12018-08-21 16:08:42 -07008809 mRoot = null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07008810 mLocation.set(0, 0, 0, 0);
8811 }
8812 }
Romain Guycbc67742012-04-27 16:12:57 -07008813
Romain Guy6410c0a2013-06-17 11:21:58 -07008814 private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
Romain Guycbc67742012-04-27 16:12:57 -07008815 if (sDebugLines== null) {
Romain Guy6410c0a2013-06-17 11:21:58 -07008816 // TODO: This won't work with multiple UI threads in a single process
Romain Guycbc67742012-04-27 16:12:57 -07008817 sDebugLines = new float[16];
8818 }
8819
Romain Guycbc67742012-04-27 16:12:57 -07008820 sDebugLines[0] = x1;
8821 sDebugLines[1] = y1;
8822 sDebugLines[2] = x2;
8823 sDebugLines[3] = y1;
8824
8825 sDebugLines[4] = x2;
8826 sDebugLines[5] = y1;
8827 sDebugLines[6] = x2;
Philip Milne7b757812012-09-19 18:13:44 -07008828 sDebugLines[7] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07008829
Philip Milne7b757812012-09-19 18:13:44 -07008830 sDebugLines[8] = x2;
Romain Guycbc67742012-04-27 16:12:57 -07008831 sDebugLines[9] = y2;
8832 sDebugLines[10] = x1;
8833 sDebugLines[11] = y2;
8834
Philip Milne7b757812012-09-19 18:13:44 -07008835 sDebugLines[12] = x1;
8836 sDebugLines[13] = y2;
Romain Guycbc67742012-04-27 16:12:57 -07008837 sDebugLines[14] = x1;
8838 sDebugLines[15] = y1;
8839
Philip Milne7b757812012-09-19 18:13:44 -07008840 canvas.drawLines(sDebugLines, paint);
Romain Guycbc67742012-04-27 16:12:57 -07008841 }
Siva Velusamy0d857b92015-04-22 10:23:56 -07008842
8843 /** @hide */
8844 @Override
Mathew Inwooda570dee2018-08-17 14:56:00 +01008845 @UnsupportedAppUsage
Siva Velusamy0d857b92015-04-22 10:23:56 -07008846 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
8847 super.encodeProperties(encoder);
8848
8849 encoder.addProperty("focus:descendantFocusability", getDescendantFocusability());
8850 encoder.addProperty("drawing:clipChildren", getClipChildren());
8851 encoder.addProperty("drawing:clipToPadding", getClipToPadding());
8852 encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled());
8853 encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache());
8854
8855 int n = getChildCount();
8856 encoder.addProperty("meta:__childCount__", (short)n);
8857 for (int i = 0; i < n; i++) {
8858 encoder.addPropertyKey("meta:__child__" + i);
8859 getChildAt(i).encode(encoder);
8860 }
8861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008862}