The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 19 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 20 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 21 | import android.animation.LayoutTransition; |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 22 | import android.annotation.CallSuper; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 23 | import android.annotation.IdRes; |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Evan Rosky | 3ac6463 | 2017-02-13 18:04:43 -0800 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 26 | import android.annotation.UiThread; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 27 | import android.annotation.UnsupportedAppUsage; |
Vadim Tryshev | a61efa4 | 2016-09-28 15:15:52 -0700 | [diff] [blame] | 28 | import android.content.ClipData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 30 | import android.content.Intent; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 32 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.res.TypedArray; |
| 34 | import android.graphics.Bitmap; |
| 35 | import android.graphics.Canvas; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 36 | import android.graphics.Color; |
| 37 | import android.graphics.Insets; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 38 | import android.graphics.Matrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.graphics.Paint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 40 | import android.graphics.PointF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.graphics.RectF; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 43 | import android.graphics.Region; |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 44 | import android.os.Build; |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 45 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Parcelable; |
| 47 | import android.os.SystemClock; |
| 48 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.util.Log; |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 50 | import android.util.Pools; |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 51 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.util.SparseArray; |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 53 | import android.util.SparseBooleanArray; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 54 | import android.view.WindowInsetsAnimationListener.InsetsAnimation; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 55 | import android.view.accessibility.AccessibilityEvent; |
Phil Weaver | 4d3eec41 | 2016-09-01 16:28:34 -0700 | [diff] [blame] | 56 | import android.view.accessibility.AccessibilityManager; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 57 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.view.animation.Animation; |
| 59 | import android.view.animation.AnimationUtils; |
| 60 | import android.view.animation.LayoutAnimationController; |
| 61 | import android.view.animation.Transformation; |
Felipe Leme | fe05a52 | 2018-01-23 15:57:49 -0800 | [diff] [blame] | 62 | import android.view.autofill.Helper; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 63 | import android.view.inspector.InspectableProperty; |
| 64 | import android.view.inspector.InspectableProperty.EnumMap; |
Chet Haase | cb3d023 | 2017-05-24 18:27:14 -0700 | [diff] [blame] | 65 | |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 66 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
| 68 | import java.util.ArrayList; |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 69 | import java.util.Collection; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 70 | import java.util.Collections; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 71 | import java.util.HashSet; |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 72 | import java.util.List; |
| 73 | import java.util.Map; |
Paul Duffin | ca4964c | 2017-02-07 15:04:10 +0000 | [diff] [blame] | 74 | import java.util.function.Predicate; |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | /** |
| 77 | * <p> |
| 78 | * A <code>ViewGroup</code> is a special view that can contain other views |
| 79 | * (called children.) The view group is the base class for layouts and views |
| 80 | * containers. This class also defines the |
| 81 | * {@link android.view.ViewGroup.LayoutParams} class which serves as the base |
| 82 | * class for layouts parameters. |
| 83 | * </p> |
| 84 | * |
| 85 | * <p> |
| 86 | * Also see {@link LayoutParams} for layout attributes. |
| 87 | * </p> |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 88 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 89 | * <div class="special reference"> |
| 90 | * <h3>Developer Guides</h3> |
| 91 | * <p>For more information about creating user interface layouts, read the |
| 92 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 93 | * guide.</p></div> |
| 94 | * |
Dianne Hackborn | 7caab0f | 2013-03-06 13:47:04 -0800 | [diff] [blame] | 95 | * <p>Here is a complete implementation of a custom ViewGroup that implements |
| 96 | * a simple {@link android.widget.FrameLayout} along with the ability to stack |
| 97 | * children in left and right gutters.</p> |
| 98 | * |
| 99 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java |
| 100 | * Complete} |
| 101 | * |
| 102 | * <p>If you are implementing XML layout attributes as shown in the example, this is the |
| 103 | * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p> |
| 104 | * |
| 105 | * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout} |
| 106 | * |
| 107 | * <p>Finally the layout manager can be used in an XML layout like so:</p> |
| 108 | * |
| 109 | * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete} |
| 110 | * |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 111 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 112 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 113 | * @attr ref android.R.styleable#ViewGroup_layoutAnimation |
| 114 | * @attr ref android.R.styleable#ViewGroup_animationCache |
| 115 | * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache |
| 116 | * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache |
| 117 | * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren |
| 118 | * @attr ref android.R.styleable#ViewGroup_descendantFocusability |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 119 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 120 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
| 121 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 123 | @UiThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | public abstract class ViewGroup extends View implements ViewParent, ViewManager { |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 125 | private static final String TAG = "ViewGroup"; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 126 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | private static final boolean DBG = false; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Views which have been hidden or removed which need to be animated on |
| 132 | * their way out. |
| 133 | * This field should be made private, so it is hidden from the SDK. |
| 134 | * {@hide} |
| 135 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 136 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | protected ArrayList<View> mDisappearingChildren; |
| 138 | |
| 139 | /** |
| 140 | * Listener used to propagate events indicating when children are added |
| 141 | * and/or removed from a view group. |
| 142 | * This field should be made private, so it is hidden from the SDK. |
| 143 | * {@hide} |
| 144 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 145 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | protected OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 147 | |
| 148 | // The view contained within this ViewGroup that has or contains focus. |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 149 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | private View mFocused; |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 151 | // The view contained within this ViewGroup (excluding nested keyboard navigation clusters) |
| 152 | // that is or contains a default-focus view. |
| 153 | private View mDefaultFocus; |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 154 | // The last child of this ViewGroup which held focus within the current cluster |
Evan Rosky | 6c286be | 2017-04-19 17:23:32 -0700 | [diff] [blame] | 155 | View mFocusedInCluster; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 157 | /** |
| 158 | * A Transformation used when drawing children, to |
| 159 | * apply on the child being drawn. |
| 160 | */ |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 161 | private Transformation mChildTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Used to track the current invalidation region. |
| 165 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 166 | RectF mInvalidateRegion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 168 | /** |
| 169 | * A Transformation used to calculate a correct |
| 170 | * invalidation area when the application is autoscaled. |
| 171 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 172 | Transformation mInvalidationTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 173 | |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 174 | // Current frontmost child that can accept drag and lies under the drag location. |
| 175 | // Used only to generate ENTER/EXIT events for pre-Nougat aps. |
| 176 | private View mCurrentDragChild; |
| 177 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 178 | // Metadata about the ongoing drag |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 179 | private DragEvent mCurrentDragStartEvent; |
| 180 | private boolean mIsInterestedInDrag; |
| 181 | private HashSet<View> mChildrenInterestedInDrag; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 182 | |
| 183 | // Used during drag dispatch |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 184 | private PointF mLocalPoint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 185 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 186 | // Lazily-created holder for point computations. |
| 187 | private float[] mTempPoint; |
| 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | // Layout animation |
| 190 | private LayoutAnimationController mLayoutAnimationController; |
| 191 | private Animation.AnimationListener mAnimationListener; |
| 192 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 193 | // First touch target in the linked list of touch targets. |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 194 | @UnsupportedAppUsage |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 195 | private TouchTarget mFirstTouchTarget; |
| 196 | |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 197 | // For debugging only. You can see these in hierarchyviewer. |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 198 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 199 | @ViewDebug.ExportedProperty(category = "events") |
| 200 | private long mLastTouchDownTime; |
| 201 | @ViewDebug.ExportedProperty(category = "events") |
| 202 | private int mLastTouchDownIndex = -1; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 203 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 204 | @ViewDebug.ExportedProperty(category = "events") |
| 205 | private float mLastTouchDownX; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 206 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 207 | @ViewDebug.ExportedProperty(category = "events") |
| 208 | private float mLastTouchDownY; |
| 209 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 210 | // First hover target in the linked list of hover targets. |
| 211 | // The hover targets are children which have received ACTION_HOVER_ENTER. |
| 212 | // They might not have actually handled the hover event, but we will |
| 213 | // continue sending hover events to them as long as the pointer remains over |
| 214 | // their bounds and the view group does not intercept hover. |
| 215 | private HoverTarget mFirstHoverTarget; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 216 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 217 | // True if the view group itself received a hover event. |
| 218 | // It might not have actually handled the hover event. |
| 219 | private boolean mHoveredSelf; |
| 220 | |
Vladislav Kaznacheev | f847ee3 | 2016-11-21 14:11:00 -0800 | [diff] [blame] | 221 | // The child capable of showing a tooltip and currently under the pointer. |
| 222 | private View mTooltipHoverTarget; |
| 223 | |
| 224 | // True if the view group is capable of showing a tooltip and the pointer is directly |
| 225 | // over the view group but not one of its child views. |
| 226 | private boolean mTooltipHoveredSelf; |
| 227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | /** |
| 229 | * Internal flags. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 230 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | * This field should be made private, so it is hidden from the SDK. |
| 232 | * {@hide} |
| 233 | */ |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 234 | @ViewDebug.ExportedProperty(flagMapping = { |
| 235 | @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN, |
| 236 | name = "CLIP_CHILDREN"), |
| 237 | @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING, |
| 238 | name = "CLIP_TO_PADDING"), |
| 239 | @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL, |
| 240 | name = "PADDING_NOT_NULL") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 241 | }, formatToHexString = true) |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 242 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | protected int mGroupFlags; |
| 244 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 247 | */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 248 | private int mLayoutMode = LAYOUT_MODE_UNDEFINED; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 249 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 250 | /** |
| 251 | * NOTE: If you change the flags below make sure to reflect the changes |
| 252 | * the DisplayList class |
| 253 | */ |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // When set, ViewGroup invalidates only the child's rectangle |
| 256 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 257 | static final int FLAG_CLIP_CHILDREN = 0x1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | // When set, ViewGroup excludes the padding area from the invalidate rectangle |
| 260 | // Set by default |
| 261 | private static final int FLAG_CLIP_TO_PADDING = 0x2; |
| 262 | |
| 263 | // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when |
| 264 | // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 265 | static final int FLAG_INVALIDATE_REQUIRED = 0x4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
| 267 | // When set, dispatchDraw() will run the layout animation and unset the flag |
| 268 | private static final int FLAG_RUN_ANIMATION = 0x8; |
| 269 | |
| 270 | // When set, there is either no layout animation on the ViewGroup or the layout |
| 271 | // animation is over |
| 272 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 273 | static final int FLAG_ANIMATION_DONE = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | |
| 275 | // If set, this ViewGroup has padding; if unset there is no padding and we don't need |
| 276 | // to clip it, even if FLAG_CLIP_TO_PADDING is set |
| 277 | private static final int FLAG_PADDING_NOT_NULL = 0x20; |
| 278 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 279 | /** @deprecated - functionality removed */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 280 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | private static final int FLAG_ANIMATION_CACHE = 0x40; |
| 282 | |
| 283 | // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a |
| 284 | // layout animation; this avoid clobbering the hierarchy |
| 285 | // Automatically set when the layout animation starts, depending on the animation's |
| 286 | // characteristics |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 287 | static final int FLAG_OPTIMIZE_INVALIDATE = 0x80; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | |
| 289 | // When set, the next call to drawChild() will clear mChildTransformation's matrix |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 290 | static final int FLAG_CLEAR_TRANSFORMATION = 0x100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | |
| 292 | // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes |
| 293 | // the children's Bitmap caches if necessary |
| 294 | // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set) |
| 295 | private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200; |
| 296 | |
| 297 | /** |
| 298 | * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)} |
| 299 | * to get the index of the child to draw for that iteration. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 300 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 301 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 303 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 305 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | /** |
| 307 | * When set, this ViewGroup supports static transformations on children; this causes |
| 308 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 309 | * invoked when a child is drawn. |
| 310 | * |
| 311 | * Any subclass overriding |
| 312 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 313 | * set this flags in {@link #mGroupFlags}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 314 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | * {@hide} |
| 316 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 317 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800; |
| 319 | |
John Reck | fb5899d | 2014-08-15 18:51:27 -0700 | [diff] [blame] | 320 | // UNUSED FLAG VALUE: 0x1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * When set, this ViewGroup's drawable states also include those |
| 324 | * of its children. |
| 325 | */ |
| 326 | private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000; |
| 327 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 328 | /** @deprecated functionality removed */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 329 | @Deprecated |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 330 | private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 332 | /** @deprecated functionality removed */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 333 | @Deprecated |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 334 | private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * When set, this group will go through its list of children to notify them of |
| 338 | * any drawable state change. |
| 339 | */ |
| 340 | private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000; |
| 341 | |
| 342 | private static final int FLAG_MASK_FOCUSABILITY = 0x60000; |
| 343 | |
| 344 | /** |
| 345 | * This view will get focus before any of its descendants. |
| 346 | */ |
| 347 | public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000; |
| 348 | |
| 349 | /** |
| 350 | * This view will get focus only if none of its descendants want it. |
| 351 | */ |
| 352 | public static final int FOCUS_AFTER_DESCENDANTS = 0x40000; |
| 353 | |
| 354 | /** |
| 355 | * This view will block any of its descendants from getting focus, even |
| 356 | * if they are focusable. |
| 357 | */ |
| 358 | public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000; |
| 359 | |
| 360 | /** |
| 361 | * Used to map between enum in attrubutes and flag values. |
| 362 | */ |
| 363 | private static final int[] DESCENDANT_FOCUSABILITY_FLAGS = |
| 364 | {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, |
| 365 | FOCUS_BLOCK_DESCENDANTS}; |
| 366 | |
| 367 | /** |
| 368 | * When set, this ViewGroup should not intercept touch events. |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 369 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 371 | @UnsupportedAppUsage |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 372 | protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 373 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 375 | * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate. |
| 376 | */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 377 | private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 378 | |
| 379 | /** |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 380 | * When set, this ViewGroup will not dispatch onAttachedToWindow calls |
| 381 | * to children when adding new views. This is used to prevent multiple |
| 382 | * onAttached calls when a ViewGroup adds children in its own onAttached method. |
| 383 | */ |
| 384 | private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000; |
| 385 | |
| 386 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 387 | * When true, indicates that a layoutMode has been explicitly set, either with |
| 388 | * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource. |
| 389 | * This distinguishes the situation in which a layout mode was inherited from |
| 390 | * one of the ViewGroup's ancestors and cached locally. |
| 391 | */ |
| 392 | private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000; |
| 393 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 394 | static final int FLAG_IS_TRANSITION_GROUP = 0x1000000; |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 395 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 396 | static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 397 | |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 398 | /** |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 399 | * When set, focus will not be permitted to enter this group if a touchscreen is present. |
| 400 | */ |
| 401 | static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000; |
| 402 | |
| 403 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 404 | * When true, indicates that a call to startActionModeForChild was made with the type parameter |
| 405 | * and should not be ignored. This helps in backwards compatibility with the existing method |
| 406 | * without a type. |
| 407 | * |
| 408 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 409 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 410 | */ |
| 411 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000; |
| 412 | |
| 413 | /** |
| 414 | * When true, indicates that a call to startActionModeForChild was made without the type |
| 415 | * parameter. This helps in backwards compatibility with the existing method |
| 416 | * without a type. |
| 417 | * |
| 418 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 419 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 420 | */ |
| 421 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000; |
| 422 | |
| 423 | /** |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 424 | * When set, indicates that a call to showContextMenuForChild was made with explicit |
| 425 | * coordinates within the initiating child view. |
| 426 | */ |
| 427 | private static final int FLAG_SHOW_CONTEXT_MENU_WITH_COORDS = 0x20000000; |
| 428 | |
| 429 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | * Indicates which types of drawing caches are to be kept in memory. |
| 431 | * This field should be made private, so it is hidden from the SDK. |
| 432 | * {@hide} |
| 433 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 434 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | protected int mPersistentDrawingCache; |
| 436 | |
| 437 | /** |
| 438 | * Used to indicate that no drawing cache should be kept in memory. |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 439 | * |
| 440 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 441 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 442 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 443 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 444 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 445 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 446 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 447 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 448 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 449 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 450 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 451 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 453 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | public static final int PERSISTENT_NO_CACHE = 0x0; |
| 455 | |
| 456 | /** |
| 457 | * Used to indicate that the animation drawing cache should be kept in memory. |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 458 | * |
| 459 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 460 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 461 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 462 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 463 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 464 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 465 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 466 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 467 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 468 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 469 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 470 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 472 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | public static final int PERSISTENT_ANIMATION_CACHE = 0x1; |
| 474 | |
| 475 | /** |
| 476 | * Used to indicate that the scrolling drawing cache should be kept in memory. |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 477 | * |
| 478 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 479 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 480 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 481 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 482 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 483 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 484 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 485 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 486 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 487 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 488 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 489 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 491 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | public static final int PERSISTENT_SCROLLING_CACHE = 0x2; |
| 493 | |
| 494 | /** |
| 495 | * Used to indicate that all drawing caches should be kept in memory. |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 496 | * |
| 497 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 498 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 499 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 500 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 501 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 502 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 503 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 504 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 505 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 506 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 507 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 508 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 510 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | public static final int PERSISTENT_ALL_CACHES = 0x3; |
| 512 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 513 | // Layout Modes |
| 514 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 515 | private static final int LAYOUT_MODE_UNDEFINED = -1; |
| 516 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 517 | /** |
| 518 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 519 | * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top}, |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 520 | * {@link #getRight() right} and {@link #getBottom() bottom}. |
| 521 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 522 | public static final int LAYOUT_MODE_CLIP_BOUNDS = 0; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 523 | |
| 524 | /** |
| 525 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 526 | * Optical bounds describe where a widget appears to be. They sit inside the clip |
| 527 | * bounds which need to cover a larger area to allow other effects, |
| 528 | * such as shadows and glows, to be drawn. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 529 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 530 | public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1; |
| 531 | |
| 532 | /** @hide */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 533 | public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | /** |
| 536 | * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL |
| 537 | * are set at the same time. |
| 538 | */ |
| 539 | protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL; |
| 540 | |
| 541 | // Index of the child's left position in the mLocation array |
| 542 | private static final int CHILD_LEFT_INDEX = 0; |
| 543 | // Index of the child's top position in the mLocation array |
| 544 | private static final int CHILD_TOP_INDEX = 1; |
| 545 | |
| 546 | // Child views of this ViewGroup |
Mihai Popa | 831c1a9 | 2018-10-15 15:40:23 +0100 | [diff] [blame] | 547 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | private View[] mChildren; |
| 549 | // Number of valid children in the mChildren array, the rest should be null or not |
| 550 | // considered as children |
Mihai Popa | 831c1a9 | 2018-10-15 15:40:23 +0100 | [diff] [blame] | 551 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | private int mChildrenCount; |
| 553 | |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 554 | // Whether layout calls are currently being suppressed, controlled by calls to |
| 555 | // suppressLayout() |
| 556 | boolean mSuppressLayout = false; |
| 557 | |
| 558 | // Whether any layout calls have actually been suppressed while mSuppressLayout |
| 559 | // has been true. This tracks whether we need to issue a requestLayout() when |
| 560 | // layout is later re-enabled. |
| 561 | private boolean mLayoutCalledWhileSuppressed = false; |
| 562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | private static final int ARRAY_INITIAL_CAPACITY = 12; |
| 564 | private static final int ARRAY_CAPACITY_INCREMENT = 12; |
| 565 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 566 | private static float[] sDebugLines; |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | // Used to draw cached views |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 569 | Paint mCachePaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 571 | // Used to animate add/remove changes in layout |
| 572 | private LayoutTransition mTransition; |
| 573 | |
| 574 | // The set of views that are currently being transitioned. This list is used to track views |
| 575 | // being removed that should not actually be removed from the parent yet because they are |
| 576 | // being animated. |
| 577 | private ArrayList<View> mTransitioningViews; |
| 578 | |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 579 | // List of children changing visibility. This is used to potentially keep rendering |
| 580 | // views during a transition when they otherwise would have become gone/invisible |
| 581 | private ArrayList<View> mVisibilityChangingChildren; |
| 582 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 583 | // Temporary holder of presorted children, only used for |
| 584 | // input/software draw dispatch for correctly Z ordering. |
| 585 | private ArrayList<View> mPreSortedChildren; |
| 586 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 587 | // Indicates how many of this container's child subtrees contain transient state |
| 588 | @ViewDebug.ExportedProperty(category = "layout") |
| 589 | private int mChildCountWithTransientState = 0; |
| 590 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 591 | /** |
| 592 | * Currently registered axes for nested scrolling. Flag set consisting of |
| 593 | * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE} |
| 594 | * for null. |
| 595 | */ |
| 596 | private int mNestedScrollAxes; |
| 597 | |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 598 | // Used to manage the list of transient views, added by addTransientView() |
| 599 | private List<Integer> mTransientIndices = null; |
| 600 | private List<View> mTransientViews = null; |
| 601 | |
Evan Rosky | 4807ae2 | 2018-03-22 16:04:15 -0700 | [diff] [blame] | 602 | /** |
| 603 | * Keeps track of how many child views have UnhandledKeyEventListeners. This should only be |
| 604 | * updated on the UI thread so shouldn't require explicit synchronization. |
| 605 | */ |
| 606 | int mChildUnhandledKeyListeners = 0; |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 607 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 608 | /** |
| 609 | * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild. |
| 610 | * |
| 611 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 612 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 613 | */ |
| 614 | private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() { |
| 615 | @Override |
| 616 | public void setTitle(CharSequence title) {} |
| 617 | |
| 618 | @Override |
| 619 | public void setTitle(int resId) {} |
| 620 | |
| 621 | @Override |
| 622 | public void setSubtitle(CharSequence subtitle) {} |
| 623 | |
| 624 | @Override |
| 625 | public void setSubtitle(int resId) {} |
| 626 | |
| 627 | @Override |
| 628 | public void setCustomView(View view) {} |
| 629 | |
| 630 | @Override |
| 631 | public void invalidate() {} |
| 632 | |
| 633 | @Override |
| 634 | public void finish() {} |
| 635 | |
| 636 | @Override |
| 637 | public Menu getMenu() { |
| 638 | return null; |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public CharSequence getTitle() { |
| 643 | return null; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public CharSequence getSubtitle() { |
| 648 | return null; |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public View getCustomView() { |
| 653 | return null; |
| 654 | } |
| 655 | |
| 656 | @Override |
| 657 | public MenuInflater getMenuInflater() { |
| 658 | return null; |
| 659 | } |
| 660 | }; |
| 661 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | public ViewGroup(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 663 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | public ViewGroup(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 667 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 670 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 671 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 675 | super(context, attrs, defStyleAttr, defStyleRes); |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | initViewGroup(); |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 678 | initFromAttributes(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | private void initViewGroup() { |
| 682 | // ViewGroup doesn't draw by default |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 683 | if (!debugDraw()) { |
| 684 | setFlags(WILL_NOT_DRAW, DRAW_MASK); |
| 685 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | mGroupFlags |= FLAG_CLIP_CHILDREN; |
| 687 | mGroupFlags |= FLAG_CLIP_TO_PADDING; |
| 688 | mGroupFlags |= FLAG_ANIMATION_DONE; |
Chris Craik | f6ce8fd | 2015-05-11 15:33:11 -0700 | [diff] [blame] | 689 | mGroupFlags |= FLAG_ANIMATION_CACHE; |
| 690 | mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 692 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 693 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 694 | } |
| 695 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); |
| 697 | |
| 698 | mChildren = new View[ARRAY_INITIAL_CAPACITY]; |
| 699 | mChildrenCount = 0; |
| 700 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE; |
| 702 | } |
| 703 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 704 | private void initFromAttributes( |
| 705 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 706 | final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, |
| 707 | defStyleAttr, defStyleRes); |
| 708 | saveAttributeDataForStyleable(context, R.styleable.ViewGroup, attrs, a, defStyleAttr, |
Filip Gruszczyński | b50cea0 | 2014-03-05 17:54:58 -0800 | [diff] [blame] | 709 | defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | |
| 711 | final int N = a.getIndexCount(); |
| 712 | for (int i = 0; i < N; i++) { |
| 713 | int attr = a.getIndex(i); |
| 714 | switch (attr) { |
| 715 | case R.styleable.ViewGroup_clipChildren: |
| 716 | setClipChildren(a.getBoolean(attr, true)); |
| 717 | break; |
| 718 | case R.styleable.ViewGroup_clipToPadding: |
| 719 | setClipToPadding(a.getBoolean(attr, true)); |
| 720 | break; |
| 721 | case R.styleable.ViewGroup_animationCache: |
| 722 | setAnimationCacheEnabled(a.getBoolean(attr, true)); |
| 723 | break; |
| 724 | case R.styleable.ViewGroup_persistentDrawingCache: |
| 725 | setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE)); |
| 726 | break; |
| 727 | case R.styleable.ViewGroup_addStatesFromChildren: |
| 728 | setAddStatesFromChildren(a.getBoolean(attr, false)); |
| 729 | break; |
| 730 | case R.styleable.ViewGroup_alwaysDrawnWithCache: |
| 731 | setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true)); |
| 732 | break; |
| 733 | case R.styleable.ViewGroup_layoutAnimation: |
| 734 | int id = a.getResourceId(attr, -1); |
| 735 | if (id > 0) { |
| 736 | setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id)); |
| 737 | } |
| 738 | break; |
| 739 | case R.styleable.ViewGroup_descendantFocusability: |
| 740 | setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]); |
| 741 | break; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 742 | case R.styleable.ViewGroup_splitMotionEvents: |
| 743 | setMotionEventSplittingEnabled(a.getBoolean(attr, false)); |
| 744 | break; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 745 | case R.styleable.ViewGroup_animateLayoutChanges: |
| 746 | boolean animateLayoutChanges = a.getBoolean(attr, false); |
| 747 | if (animateLayoutChanges) { |
| 748 | setLayoutTransition(new LayoutTransition()); |
| 749 | } |
| 750 | break; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 751 | case R.styleable.ViewGroup_layoutMode: |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 752 | setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED)); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 753 | break; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 754 | case R.styleable.ViewGroup_transitionGroup: |
| 755 | setTransitionGroup(a.getBoolean(attr, false)); |
| 756 | break; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 757 | case R.styleable.ViewGroup_touchscreenBlocksFocus: |
| 758 | setTouchscreenBlocksFocus(a.getBoolean(attr, false)); |
| 759 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | a.recycle(); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Gets the descendant focusability of this view group. The descendant |
| 768 | * focusability defines the relationship between this view group and its |
| 769 | * descendants when looking for a view to take focus in |
| 770 | * {@link #requestFocus(int, android.graphics.Rect)}. |
| 771 | * |
| 772 | * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 773 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 774 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 775 | @ViewDebug.ExportedProperty(category = "focus", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"), |
| 777 | @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"), |
| 778 | @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS") |
| 779 | }) |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 780 | @InspectableProperty(enumMapping = { |
| 781 | @EnumMap(value = FOCUS_BEFORE_DESCENDANTS, name = "beforeDescendants"), |
| 782 | @EnumMap(value = FOCUS_AFTER_DESCENDANTS, name = "afterDescendants"), |
| 783 | @EnumMap(value = FOCUS_BLOCK_DESCENDANTS, name = "blocksDescendants") |
| 784 | }) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | public int getDescendantFocusability() { |
| 786 | return mGroupFlags & FLAG_MASK_FOCUSABILITY; |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Set the descendant focusability of this view group. This defines the relationship |
| 791 | * between this view group and its descendants when looking for a view to |
| 792 | * take focus in {@link #requestFocus(int, android.graphics.Rect)}. |
| 793 | * |
| 794 | * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 795 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 796 | */ |
| 797 | public void setDescendantFocusability(int focusability) { |
| 798 | switch (focusability) { |
| 799 | case FOCUS_BEFORE_DESCENDANTS: |
| 800 | case FOCUS_AFTER_DESCENDANTS: |
| 801 | case FOCUS_BLOCK_DESCENDANTS: |
| 802 | break; |
| 803 | default: |
| 804 | throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, " |
| 805 | + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS"); |
| 806 | } |
| 807 | mGroupFlags &= ~FLAG_MASK_FOCUSABILITY; |
| 808 | mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY); |
| 809 | } |
| 810 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | @Override |
| 812 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 813 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 814 | mFocused.unFocus(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | mFocused = null; |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 816 | mFocusedInCluster = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | } |
| 818 | super.handleFocusGainInternal(direction, previouslyFocusedRect); |
| 819 | } |
| 820 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 821 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | public void requestChildFocus(View child, View focused) { |
| 823 | if (DBG) { |
| 824 | System.out.println(this + " requestChildFocus()"); |
| 825 | } |
| 826 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | // Unfocus us, if necessary |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 831 | super.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | |
| 833 | // We had a previous notion of who had focus. Clear it. |
| 834 | if (mFocused != child) { |
| 835 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 836 | mFocused.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | mFocused = child; |
| 840 | } |
| 841 | if (mParent != null) { |
| 842 | mParent.requestChildFocus(this, focused); |
| 843 | } |
| 844 | } |
| 845 | |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 846 | void setDefaultFocus(View child) { |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 847 | // Stop at any higher view which is explicitly focused-by-default |
| 848 | if (mDefaultFocus != null && mDefaultFocus.isFocusedByDefault()) { |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 849 | return; |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 850 | } |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 851 | |
| 852 | mDefaultFocus = child; |
| 853 | |
| 854 | if (mParent instanceof ViewGroup) { |
| 855 | ((ViewGroup) mParent).setDefaultFocus(this); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /** |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 860 | * Clears the default-focus chain from {@param child} up to the first parent which has another |
| 861 | * default-focusable branch below it or until there is no default-focus chain. |
| 862 | * |
| 863 | * @param child |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 864 | */ |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 865 | void clearDefaultFocus(View child) { |
| 866 | // Stop at any higher view which is explicitly focused-by-default |
| 867 | if (mDefaultFocus != child && mDefaultFocus != null |
| 868 | && mDefaultFocus.isFocusedByDefault()) { |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | mDefaultFocus = null; |
| 873 | |
| 874 | // Search child siblings for default focusables. |
| 875 | for (int i = 0; i < mChildrenCount; ++i) { |
| 876 | View sibling = mChildren[i]; |
| 877 | if (sibling.isFocusedByDefault()) { |
| 878 | mDefaultFocus = sibling; |
| 879 | return; |
| 880 | } else if (mDefaultFocus == null && sibling.hasDefaultFocus()) { |
| 881 | mDefaultFocus = sibling; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if (mParent instanceof ViewGroup) { |
| 886 | ((ViewGroup) mParent).clearDefaultFocus(this); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | @Override |
| 891 | boolean hasDefaultFocus() { |
| 892 | return mDefaultFocus != null || super.hasDefaultFocus(); |
| 893 | } |
| 894 | |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 895 | /** |
| 896 | * Removes {@code child} (and associated focusedInCluster chain) from the cluster containing |
| 897 | * it. |
| 898 | * <br> |
| 899 | * This is intended to be run on {@code child}'s immediate parent. This is necessary because |
| 900 | * the chain is sometimes cleared after {@code child} has been detached. |
| 901 | */ |
| 902 | void clearFocusedInCluster(View child) { |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 903 | if (mFocusedInCluster != child) { |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 904 | return; |
| 905 | } |
Evan Rosky | 5b20e09 | 2017-07-13 18:04:39 -0700 | [diff] [blame] | 906 | clearFocusedInCluster(); |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * Removes the focusedInCluster chain from this up to the cluster containing it. |
| 911 | */ |
| 912 | void clearFocusedInCluster() { |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 913 | View top = findKeyboardNavigationCluster(); |
| 914 | ViewParent parent = this; |
| 915 | do { |
| 916 | ((ViewGroup) parent).mFocusedInCluster = null; |
Evan Rosky | 6c286be | 2017-04-19 17:23:32 -0700 | [diff] [blame] | 917 | if (parent == top) { |
| 918 | break; |
| 919 | } |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 920 | parent = parent.getParent(); |
Evan Rosky | 6c286be | 2017-04-19 17:23:32 -0700 | [diff] [blame] | 921 | } while (parent instanceof ViewGroup); |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | public void focusableViewAvailable(View v) { |
| 926 | if (mParent != null |
| 927 | // shortcut: don't report a new focusable view if we block our descendants from |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 928 | // getting focus or if we're not visible |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS) |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 930 | && ((mViewFlags & VISIBILITY_MASK) == VISIBLE) |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 931 | && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | // shortcut: don't report a new focusable view if we already are focused |
| 933 | // (and we don't prefer our descendants) |
| 934 | // |
| 935 | // note: knowing that mFocused is non-null is not a good enough reason |
| 936 | // to break the traversal since in that case we'd actually have to find |
| 937 | // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 938 | // an ancestor of v; this will get checked for at ViewAncestor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) { |
| 940 | mParent.focusableViewAvailable(v); |
| 941 | } |
| 942 | } |
| 943 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 944 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | public boolean showContextMenuForChild(View originalView) { |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 946 | if (isShowingContextMenuWithCoords()) { |
| 947 | // We're being called for compatibility. Return false and let the version |
| 948 | // with coordinates recurse up. |
| 949 | return false; |
| 950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | return mParent != null && mParent.showContextMenuForChild(originalView); |
| 952 | } |
| 953 | |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 954 | /** |
| 955 | * @hide used internally for compatibility with existing app code only |
| 956 | */ |
| 957 | public final boolean isShowingContextMenuWithCoords() { |
| 958 | return (mGroupFlags & FLAG_SHOW_CONTEXT_MENU_WITH_COORDS) != 0; |
| 959 | } |
| 960 | |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 961 | @Override |
| 962 | public boolean showContextMenuForChild(View originalView, float x, float y) { |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 963 | try { |
| 964 | mGroupFlags |= FLAG_SHOW_CONTEXT_MENU_WITH_COORDS; |
| 965 | if (showContextMenuForChild(originalView)) { |
| 966 | return true; |
| 967 | } |
| 968 | } finally { |
| 969 | mGroupFlags &= ~FLAG_SHOW_CONTEXT_MENU_WITH_COORDS; |
| 970 | } |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 971 | return mParent != null && mParent.showContextMenuForChild(originalView, x, y); |
| 972 | } |
| 973 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 974 | @Override |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 975 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 976 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) { |
| 977 | // This is the original call. |
| 978 | try { |
| 979 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 980 | return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY); |
| 981 | } finally { |
| 982 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 983 | } |
| 984 | } else { |
| 985 | // We are being called from the new method with type. |
| 986 | return SENTINEL_ACTION_MODE; |
| 987 | } |
| 988 | } |
| 989 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 990 | @Override |
| 991 | public ActionMode startActionModeForChild( |
| 992 | View originalView, ActionMode.Callback callback, int type) { |
Adam Powell | e9fd6d2 | 2015-06-01 11:26:32 -0700 | [diff] [blame] | 993 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0 |
| 994 | && type == ActionMode.TYPE_PRIMARY) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 995 | ActionMode mode; |
| 996 | try { |
| 997 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 998 | mode = startActionModeForChild(originalView, callback); |
| 999 | } finally { |
| 1000 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 1001 | } |
| 1002 | if (mode != SENTINEL_ACTION_MODE) { |
| 1003 | return mode; |
| 1004 | } |
| 1005 | } |
| 1006 | if (mParent != null) { |
| 1007 | try { |
| 1008 | return mParent.startActionModeForChild(originalView, callback, type); |
| 1009 | } catch (AbstractMethodError ame) { |
| 1010 | // Custom view parents might not implement this method. |
| 1011 | return mParent.startActionModeForChild(originalView, callback); |
| 1012 | } |
| 1013 | } |
| 1014 | return null; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 1018 | * @hide |
| 1019 | */ |
| 1020 | @Override |
| 1021 | public boolean dispatchActivityResult( |
| 1022 | String who, int requestCode, int resultCode, Intent data) { |
| 1023 | if (super.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 1024 | return true; |
| 1025 | } |
| 1026 | int childCount = getChildCount(); |
| 1027 | for (int i = 0; i < childCount; i++) { |
| 1028 | View child = getChildAt(i); |
| 1029 | if (child.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 1030 | return true; |
| 1031 | } |
| 1032 | } |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | * Find the nearest view in the specified direction that wants to take |
| 1038 | * focus. |
| 1039 | * |
| 1040 | * @param focused The view that currently has focus |
| 1041 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and |
| 1042 | * FOCUS_RIGHT, or 0 for not applicable. |
| 1043 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1044 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | public View focusSearch(View focused, int direction) { |
Vadim Tryshev | b5ced22 | 2017-01-17 19:31:35 -0800 | [diff] [blame] | 1046 | if (isRootNamespace()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | // root namespace means we should consider ourselves the top of the |
| 1048 | // tree for focus searching; otherwise we could be focus searching |
Vadim Tryshev | 418b1fc | 2016-11-28 18:26:24 -0800 | [diff] [blame] | 1049 | // into other tabs. see LocalActivityManager and TabHost for more info. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 1051 | } else if (mParent != null) { |
| 1052 | return mParent.focusSearch(focused, direction); |
| 1053 | } |
| 1054 | return null; |
| 1055 | } |
| 1056 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1057 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1062 | @Override |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1063 | public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1064 | ViewParent parent = mParent; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1065 | if (parent == null) { |
| 1066 | return false; |
| 1067 | } |
| 1068 | final boolean propagate = onRequestSendAccessibilityEvent(child, event); |
| 1069 | if (!propagate) { |
| 1070 | return false; |
| 1071 | } |
| 1072 | return parent.requestSendAccessibilityEvent(this, event); |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * Called when a child has requested sending an {@link AccessibilityEvent} and |
| 1077 | * gives an opportunity to its parent to augment the event. |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 1078 | * <p> |
Adam Powell | 2fcbbd0 | 2011-09-28 18:56:43 -0700 | [diff] [blame] | 1079 | * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling |
| 1080 | * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its |
| 1081 | * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)} |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 1082 | * is responsible for handling this call. |
| 1083 | * </p> |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1084 | * |
| 1085 | * @param child The child which requests sending the event. |
| 1086 | * @param event The event to be sent. |
| 1087 | * @return True if the event should be sent. |
| 1088 | * |
| 1089 | * @see #requestSendAccessibilityEvent(View, AccessibilityEvent) |
| 1090 | */ |
| 1091 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 1092 | if (mAccessibilityDelegate != null) { |
| 1093 | return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event); |
| 1094 | } else { |
| 1095 | return onRequestSendAccessibilityEventInternal(child, event); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent) |
| 1101 | * |
| 1102 | * Note: Called from the default {@link View.AccessibilityDelegate}. |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1103 | * |
| 1104 | * @hide |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 1105 | */ |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1106 | public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1107 | return true; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 1111 | * Called when a child view has changed whether or not it is tracking transient state. |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 1112 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1113 | @Override |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 1114 | public void childHasTransientStateChanged(View child, boolean childHasTransientState) { |
| 1115 | final boolean oldHasTransientState = hasTransientState(); |
| 1116 | if (childHasTransientState) { |
| 1117 | mChildCountWithTransientState++; |
| 1118 | } else { |
| 1119 | mChildCountWithTransientState--; |
| 1120 | } |
| 1121 | |
| 1122 | final boolean newHasTransientState = hasTransientState(); |
| 1123 | if (mParent != null && oldHasTransientState != newHasTransientState) { |
| 1124 | try { |
| 1125 | mParent.childHasTransientStateChanged(this, newHasTransientState); |
| 1126 | } catch (AbstractMethodError e) { |
| 1127 | Log.e(TAG, mParent.getClass().getSimpleName() + |
| 1128 | " does not fully implement ViewParent", e); |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 1133 | @Override |
| 1134 | public boolean hasTransientState() { |
| 1135 | return mChildCountWithTransientState > 0 || super.hasTransientState(); |
| 1136 | } |
| 1137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | @Override |
| 1139 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 1140 | return mFocused != null && |
| 1141 | mFocused.dispatchUnhandledMove(focused, direction); |
| 1142 | } |
| 1143 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1144 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | public void clearChildFocus(View child) { |
| 1146 | if (DBG) { |
| 1147 | System.out.println(this + " clearChildFocus()"); |
| 1148 | } |
| 1149 | |
| 1150 | mFocused = null; |
| 1151 | if (mParent != null) { |
| 1152 | mParent.clearChildFocus(this); |
| 1153 | } |
| 1154 | } |
| 1155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | @Override |
| 1157 | public void clearFocus() { |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 1158 | if (DBG) { |
| 1159 | System.out.println(this + " clearFocus()"); |
| 1160 | } |
| 1161 | if (mFocused == null) { |
| 1162 | super.clearFocus(); |
| 1163 | } else { |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 1164 | View focused = mFocused; |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 1165 | mFocused = null; |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 1166 | focused.clearFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | @Override |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 1171 | void unFocus(View focused) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | if (DBG) { |
| 1173 | System.out.println(this + " unFocus()"); |
| 1174 | } |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 1175 | if (mFocused == null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 1176 | super.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 1177 | } else { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 1178 | mFocused.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 1179 | mFocused = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * Returns the focused child of this view, if any. The child may have focus |
| 1185 | * or contain focus. |
| 1186 | * |
| 1187 | * @return the focused child or null. |
| 1188 | */ |
| 1189 | public View getFocusedChild() { |
| 1190 | return mFocused; |
| 1191 | } |
| 1192 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1193 | View getDeepestFocusedChild() { |
| 1194 | View v = this; |
| 1195 | while (v != null) { |
| 1196 | if (v.isFocused()) { |
| 1197 | return v; |
| 1198 | } |
| 1199 | v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null; |
| 1200 | } |
| 1201 | return null; |
| 1202 | } |
| 1203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | /** |
| 1205 | * Returns true if this view has or contains focus |
| 1206 | * |
| 1207 | * @return true if this view has or contains focus |
| 1208 | */ |
| 1209 | @Override |
| 1210 | public boolean hasFocus() { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1211 | return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * (non-Javadoc) |
| 1216 | * |
| 1217 | * @see android.view.View#findFocus() |
| 1218 | */ |
| 1219 | @Override |
| 1220 | public View findFocus() { |
| 1221 | if (DBG) { |
| 1222 | System.out.println("Find focus in " + this + ": flags=" |
| 1223 | + isFocused() + ", child=" + mFocused); |
| 1224 | } |
| 1225 | |
| 1226 | if (isFocused()) { |
| 1227 | return this; |
| 1228 | } |
| 1229 | |
| 1230 | if (mFocused != null) { |
| 1231 | return mFocused.findFocus(); |
| 1232 | } |
| 1233 | return null; |
| 1234 | } |
| 1235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | @Override |
Adam Powell | 0f552f4 | 2017-02-03 11:50:42 -0800 | [diff] [blame] | 1237 | boolean hasFocusable(boolean allowAutoFocus, boolean dispatchExplicit) { |
Alan Viverette | 7485a9b | 2017-02-27 11:55:53 -0500 | [diff] [blame] | 1238 | // This should probably be super.hasFocusable, but that would change |
| 1239 | // behavior. Historically, we have not checked the ancestor views for |
| 1240 | // shouldBlockFocusForTouchscreen() in ViewGroup.hasFocusable. |
| 1241 | |
| 1242 | // Invisible and gone views are never focusable. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
Alan Viverette | 7485a9b | 2017-02-27 11:55:53 -0500 | [diff] [blame] | 1247 | // Only use effective focusable value when allowed. |
| 1248 | if ((allowAutoFocus || getFocusable() != FOCUSABLE_AUTO) && isFocusable()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | return true; |
| 1250 | } |
| 1251 | |
Alan Viverette | 7485a9b | 2017-02-27 11:55:53 -0500 | [diff] [blame] | 1252 | // Determine whether we have a focused descendant. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | final int descendantFocusability = getDescendantFocusability(); |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1254 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 1255 | return hasFocusableChild(dispatchExplicit); |
| 1256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 1258 | return false; |
| 1259 | } |
Adam Powell | 0f552f4 | 2017-02-03 11:50:42 -0800 | [diff] [blame] | 1260 | |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 1261 | boolean hasFocusableChild(boolean dispatchExplicit) { |
| 1262 | // Determine whether we have a focusable descendant. |
| 1263 | final int count = mChildrenCount; |
| 1264 | final View[] children = mChildren; |
| 1265 | |
| 1266 | for (int i = 0; i < count; i++) { |
| 1267 | final View child = children[i]; |
| 1268 | |
| 1269 | // In case the subclass has overridden has[Explicit]Focusable, dispatch |
| 1270 | // to the expected one for each child even though we share logic here. |
| 1271 | if ((dispatchExplicit && child.hasExplicitFocusable()) |
| 1272 | || (!dispatchExplicit && child.hasFocusable())) { |
| 1273 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1281 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | final int focusableCount = views.size(); |
| 1283 | |
| 1284 | final int descendantFocusability = getDescendantFocusability(); |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 1285 | final boolean blockFocusForTouchscreen = shouldBlockFocusForTouchscreen(); |
| 1286 | final boolean focusSelf = (isFocusableInTouchMode() || !blockFocusForTouchscreen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | |
Evan Rosky | 3b94bf5 | 2017-01-10 17:05:28 -0800 | [diff] [blame] | 1288 | if (descendantFocusability == FOCUS_BLOCK_DESCENDANTS) { |
| 1289 | if (focusSelf) { |
| 1290 | super.addFocusables(views, direction, focusableMode); |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1291 | } |
Evan Rosky | 3b94bf5 | 2017-01-10 17:05:28 -0800 | [diff] [blame] | 1292 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 1295 | if (blockFocusForTouchscreen) { |
Evan Rosky | 3b94bf5 | 2017-01-10 17:05:28 -0800 | [diff] [blame] | 1296 | focusableMode |= FOCUSABLES_TOUCH_MODE; |
| 1297 | } |
| 1298 | |
| 1299 | if ((descendantFocusability == FOCUS_BEFORE_DESCENDANTS) && focusSelf) { |
| 1300 | super.addFocusables(views, direction, focusableMode); |
| 1301 | } |
| 1302 | |
| 1303 | int count = 0; |
| 1304 | final View[] children = new View[mChildrenCount]; |
| 1305 | for (int i = 0; i < mChildrenCount; ++i) { |
| 1306 | View child = mChildren[i]; |
| 1307 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1308 | children[count++] = child; |
| 1309 | } |
| 1310 | } |
Evan Rosky | d114e0f | 2017-03-23 11:20:04 -0700 | [diff] [blame] | 1311 | FocusFinder.sort(children, 0, count, this, isLayoutRtl()); |
Evan Rosky | 3b94bf5 | 2017-01-10 17:05:28 -0800 | [diff] [blame] | 1312 | for (int i = 0; i < count; ++i) { |
| 1313 | children[i].addFocusables(views, direction, focusableMode); |
| 1314 | } |
| 1315 | |
| 1316 | // When set to FOCUS_AFTER_DESCENDANTS, we only add ourselves if |
| 1317 | // there aren't any focusable descendants. this is |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1318 | // to avoid the focus search finding layouts when a more precise search |
| 1319 | // among the focusable children would be more interesting. |
Evan Rosky | 3b94bf5 | 2017-01-10 17:05:28 -0800 | [diff] [blame] | 1320 | if ((descendantFocusability == FOCUS_AFTER_DESCENDANTS) && focusSelf |
| 1321 | && focusableCount == views.size()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1322 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1326 | @Override |
Vadim Tryshev | b5ced22 | 2017-01-17 19:31:35 -0800 | [diff] [blame] | 1327 | public void addKeyboardNavigationClusters(Collection<View> views, int direction) { |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1328 | final int focusableCount = views.size(); |
| 1329 | |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 1330 | if (isKeyboardNavigationCluster()) { |
| 1331 | // Cluster-navigation can enter a touchscreenBlocksFocus cluster, so temporarily |
| 1332 | // disable touchscreenBlocksFocus to evaluate whether it contains focusables. |
| 1333 | final boolean blockedFocus = getTouchscreenBlocksFocus(); |
| 1334 | try { |
| 1335 | setTouchscreenBlocksFocusNoRefocus(false); |
| 1336 | super.addKeyboardNavigationClusters(views, direction); |
| 1337 | } finally { |
| 1338 | setTouchscreenBlocksFocusNoRefocus(blockedFocus); |
| 1339 | } |
| 1340 | } else { |
| 1341 | super.addKeyboardNavigationClusters(views, direction); |
| 1342 | } |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1343 | |
| 1344 | if (focusableCount != views.size()) { |
Vadim Tryshev | 311a5b5 | 2017-01-05 18:54:11 -0800 | [diff] [blame] | 1345 | // No need to look for groups inside a group. |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1346 | return; |
| 1347 | } |
| 1348 | |
| 1349 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 1350 | return; |
| 1351 | } |
| 1352 | |
Evan Rosky | bd10c52 | 2017-03-27 15:50:38 -0700 | [diff] [blame] | 1353 | int count = 0; |
| 1354 | final View[] visibleChildren = new View[mChildrenCount]; |
| 1355 | for (int i = 0; i < mChildrenCount; ++i) { |
| 1356 | final View child = mChildren[i]; |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1357 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
Evan Rosky | bd10c52 | 2017-03-27 15:50:38 -0700 | [diff] [blame] | 1358 | visibleChildren[count++] = child; |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1359 | } |
| 1360 | } |
Evan Rosky | d114e0f | 2017-03-23 11:20:04 -0700 | [diff] [blame] | 1361 | FocusFinder.sort(visibleChildren, 0, count, this, isLayoutRtl()); |
Evan Rosky | bd10c52 | 2017-03-27 15:50:38 -0700 | [diff] [blame] | 1362 | for (int i = 0; i < count; ++i) { |
| 1363 | visibleChildren[i].addKeyboardNavigationClusters(views, direction); |
| 1364 | } |
Vadim Tryshev | 01b0c9e | 2016-11-21 15:25:01 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1367 | /** |
| 1368 | * Set whether this ViewGroup should ignore focus requests for itself and its children. |
| 1369 | * If this option is enabled and the ViewGroup or a descendant currently has focus, focus |
| 1370 | * will proceed forward. |
| 1371 | * |
| 1372 | * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen |
| 1373 | */ |
| 1374 | public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) { |
| 1375 | if (touchscreenBlocksFocus) { |
| 1376 | mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 1377 | if (hasFocus() && !isKeyboardNavigationCluster()) { |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1378 | final View focusedChild = getDeepestFocusedChild(); |
| 1379 | if (!focusedChild.isFocusableInTouchMode()) { |
| 1380 | final View newFocus = focusSearch(FOCUS_FORWARD); |
| 1381 | if (newFocus != null) { |
| 1382 | newFocus.requestFocus(); |
| 1383 | } |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | } else { |
| 1387 | mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1388 | } |
| 1389 | } |
| 1390 | |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 1391 | private void setTouchscreenBlocksFocusNoRefocus(boolean touchscreenBlocksFocus) { |
| 1392 | if (touchscreenBlocksFocus) { |
| 1393 | mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1394 | } else { |
| 1395 | mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1396 | } |
| 1397 | } |
| 1398 | |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1399 | /** |
| 1400 | * Check whether this ViewGroup should ignore focus requests for itself and its children. |
| 1401 | */ |
Evan Rosky | ecb63fc | 2017-04-13 16:57:18 -0700 | [diff] [blame] | 1402 | @ViewDebug.ExportedProperty(category = "focus") |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 1403 | @InspectableProperty |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1404 | public boolean getTouchscreenBlocksFocus() { |
| 1405 | return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0; |
| 1406 | } |
| 1407 | |
| 1408 | boolean shouldBlockFocusForTouchscreen() { |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 1409 | // There is a special case for keyboard-navigation clusters. We allow cluster navigation |
| 1410 | // to jump into blockFocusForTouchscreen ViewGroups which are clusters. Once in the |
| 1411 | // cluster, focus is free to move around within it. |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1412 | return getTouchscreenBlocksFocus() && |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 1413 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN) |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 1414 | && !(isKeyboardNavigationCluster() |
| 1415 | && (hasFocus() || (findKeyboardNavigationCluster() != this))); |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1418 | @Override |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1419 | public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) { |
| 1420 | super.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1421 | final int childrenCount = mChildrenCount; |
| 1422 | final View[] children = mChildren; |
| 1423 | for (int i = 0; i < childrenCount; i++) { |
| 1424 | View child = children[i]; |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1425 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1426 | && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1427 | child.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1432 | /** @hide */ |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1433 | @Override |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1434 | public View findViewByAccessibilityIdTraversal(int accessibilityId) { |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1435 | View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1436 | if (foundView != null) { |
| 1437 | return foundView; |
| 1438 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1439 | |
| 1440 | if (getAccessibilityNodeProvider() != null) { |
| 1441 | return null; |
| 1442 | } |
| 1443 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1444 | final int childrenCount = mChildrenCount; |
| 1445 | final View[] children = mChildren; |
| 1446 | for (int i = 0; i < childrenCount; i++) { |
| 1447 | View child = children[i]; |
| 1448 | foundView = child.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1449 | if (foundView != null) { |
| 1450 | return foundView; |
| 1451 | } |
| 1452 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1453 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1454 | return null; |
| 1455 | } |
| 1456 | |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 1457 | /** @hide */ |
| 1458 | @Override |
| 1459 | public View findViewByAutofillIdTraversal(int autofillId) { |
| 1460 | View foundView = super.findViewByAutofillIdTraversal(autofillId); |
| 1461 | if (foundView != null) { |
| 1462 | return foundView; |
| 1463 | } |
| 1464 | |
| 1465 | final int childrenCount = mChildrenCount; |
| 1466 | final View[] children = mChildren; |
| 1467 | for (int i = 0; i < childrenCount; i++) { |
| 1468 | View child = children[i]; |
| 1469 | foundView = child.findViewByAutofillIdTraversal(autofillId); |
| 1470 | if (foundView != null) { |
| 1471 | return foundView; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | return null; |
| 1476 | } |
| 1477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | @Override |
| 1479 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 1480 | super.dispatchWindowFocusChanged(hasFocus); |
| 1481 | final int count = mChildrenCount; |
| 1482 | final View[] children = mChildren; |
| 1483 | for (int i = 0; i < count; i++) { |
| 1484 | children[i].dispatchWindowFocusChanged(hasFocus); |
| 1485 | } |
| 1486 | } |
| 1487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | @Override |
| 1489 | public void addTouchables(ArrayList<View> views) { |
| 1490 | super.addTouchables(views); |
| 1491 | |
| 1492 | final int count = mChildrenCount; |
| 1493 | final View[] children = mChildren; |
| 1494 | |
| 1495 | for (int i = 0; i < count; i++) { |
| 1496 | final View child = children[i]; |
| 1497 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1498 | child.addTouchables(views); |
| 1499 | } |
| 1500 | } |
| 1501 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1502 | |
| 1503 | /** |
| 1504 | * @hide |
| 1505 | */ |
| 1506 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 1507 | @UnsupportedAppUsage |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1508 | public void makeOptionalFitsSystemWindows() { |
| 1509 | super.makeOptionalFitsSystemWindows(); |
| 1510 | final int count = mChildrenCount; |
| 1511 | final View[] children = mChildren; |
| 1512 | for (int i = 0; i < count; i++) { |
| 1513 | children[i].makeOptionalFitsSystemWindows(); |
| 1514 | } |
| 1515 | } |
| 1516 | |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 1517 | @Override |
| 1518 | public void dispatchDisplayHint(int hint) { |
| 1519 | super.dispatchDisplayHint(hint); |
| 1520 | final int count = mChildrenCount; |
| 1521 | final View[] children = mChildren; |
| 1522 | for (int i = 0; i < count; i++) { |
| 1523 | children[i].dispatchDisplayHint(hint); |
| 1524 | } |
| 1525 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | |
| 1527 | /** |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1528 | * Called when a view's visibility has changed. Notify the parent to take any appropriate |
| 1529 | * action. |
| 1530 | * |
| 1531 | * @param child The view whose visibility has changed |
| 1532 | * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE). |
| 1533 | * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE). |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1534 | * @hide |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1535 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 1536 | @UnsupportedAppUsage |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1537 | protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1538 | if (mTransition != null) { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1539 | if (newVisibility == VISIBLE) { |
| 1540 | mTransition.showChild(this, child, oldVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1541 | } else { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1542 | mTransition.hideChild(this, child, newVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1543 | if (mTransitioningViews != null && mTransitioningViews.contains(child)) { |
Chet Haase | ddbb346 | 2012-06-19 13:54:29 -0700 | [diff] [blame] | 1544 | // Only track this on disappearing views - appearing views are already visible |
| 1545 | // and don't need special handling during drawChild() |
| 1546 | if (mVisibilityChangingChildren == null) { |
| 1547 | mVisibilityChangingChildren = new ArrayList<View>(); |
| 1548 | } |
| 1549 | mVisibilityChangingChildren.add(child); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1550 | addDisappearingView(child); |
| 1551 | } |
| 1552 | } |
| 1553 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1554 | |
| 1555 | // in all cases, for drags |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1556 | if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) { |
| 1557 | if (!mChildrenInterestedInDrag.contains(child)) { |
| 1558 | notifyChildOfDragStart(child); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1559 | } |
| 1560 | } |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | @Override |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1564 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 1565 | super.dispatchVisibilityChanged(changedView, visibility); |
| 1566 | final int count = mChildrenCount; |
| 1567 | final View[] children = mChildren; |
| 1568 | for (int i = 0; i < count; i++) { |
| 1569 | children[i].dispatchVisibilityChanged(changedView, visibility); |
| 1570 | } |
| 1571 | } |
| 1572 | |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1573 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 1575 | super.dispatchWindowVisibilityChanged(visibility); |
| 1576 | final int count = mChildrenCount; |
| 1577 | final View[] children = mChildren; |
| 1578 | for (int i = 0; i < count; i++) { |
| 1579 | children[i].dispatchWindowVisibilityChanged(visibility); |
| 1580 | } |
| 1581 | } |
| 1582 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1583 | @Override |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1584 | boolean dispatchVisibilityAggregated(boolean isVisible) { |
| 1585 | isVisible = super.dispatchVisibilityAggregated(isVisible); |
Adam Powell | 41d9690 | 2016-03-15 14:43:19 -0700 | [diff] [blame] | 1586 | final int count = mChildrenCount; |
| 1587 | final View[] children = mChildren; |
| 1588 | for (int i = 0; i < count; i++) { |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1589 | // Only dispatch to visible children. Not visible children and their subtrees already |
| 1590 | // know that they aren't visible and that's not going to change as a result of |
| 1591 | // whatever triggered this dispatch. |
| 1592 | if (children[i].getVisibility() == VISIBLE) { |
| 1593 | children[i].dispatchVisibilityAggregated(isVisible); |
Adam Powell | 41d9690 | 2016-03-15 14:43:19 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1596 | return isVisible; |
Adam Powell | 41d9690 | 2016-03-15 14:43:19 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | @Override |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1600 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 1601 | super.dispatchConfigurationChanged(newConfig); |
| 1602 | final int count = mChildrenCount; |
| 1603 | final View[] children = mChildren; |
| 1604 | for (int i = 0; i < count; i++) { |
| 1605 | children[i].dispatchConfigurationChanged(newConfig); |
| 1606 | } |
| 1607 | } |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 1608 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1609 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | public void recomputeViewAttributes(View child) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1611 | if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 1612 | ViewParent parent = mParent; |
| 1613 | if (parent != null) parent.recomputeViewAttributes(this); |
| 1614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1618 | void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) { |
| 1619 | if ((visibility & VISIBILITY_MASK) == VISIBLE) { |
| 1620 | super.dispatchCollectViewAttributes(attachInfo, visibility); |
| 1621 | final int count = mChildrenCount; |
| 1622 | final View[] children = mChildren; |
| 1623 | for (int i = 0; i < count; i++) { |
| 1624 | final View child = children[i]; |
| 1625 | child.dispatchCollectViewAttributes(attachInfo, |
| 1626 | visibility | (child.mViewFlags&VISIBILITY_MASK)); |
| 1627 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1631 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | public void bringChildToFront(View child) { |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 1633 | final int index = indexOfChild(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | if (index >= 0) { |
| 1635 | removeFromArray(index); |
| 1636 | addInArray(child, mChildrenCount); |
| 1637 | child.mParent = this; |
Chet Haase | cb96db8 | 2013-09-04 10:21:46 -0700 | [diff] [blame] | 1638 | requestLayout(); |
| 1639 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1643 | private PointF getLocalPoint() { |
| 1644 | if (mLocalPoint == null) mLocalPoint = new PointF(); |
| 1645 | return mLocalPoint; |
| 1646 | } |
| 1647 | |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 1648 | @Override |
| 1649 | boolean dispatchDragEnterExitInPreN(DragEvent event) { |
| 1650 | if (event.mAction == DragEvent.ACTION_DRAG_EXITED && mCurrentDragChild != null) { |
| 1651 | // The drag exited a sub-tree of views; notify of the exit all descendants that are in |
| 1652 | // entered state. |
| 1653 | // We don't need this recursive delivery for ENTERED events because they get generated |
| 1654 | // from the recursive delivery of LOCATION/DROP events, and hence, don't need their own |
| 1655 | // recursion. |
| 1656 | mCurrentDragChild.dispatchDragEnterExitInPreN(event); |
| 1657 | mCurrentDragChild = null; |
| 1658 | } |
| 1659 | return mIsInterestedInDrag && super.dispatchDragEnterExitInPreN(event); |
| 1660 | } |
| 1661 | |
Steve Block | 8a7259b | 2012-03-01 11:24:41 +0000 | [diff] [blame] | 1662 | // TODO: Write real docs |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1663 | @Override |
| 1664 | public boolean dispatchDragEvent(DragEvent event) { |
| 1665 | boolean retval = false; |
| 1666 | final float tx = event.mX; |
| 1667 | final float ty = event.mY; |
Vadim Tryshev | a61efa4 | 2016-09-28 15:15:52 -0700 | [diff] [blame] | 1668 | final ClipData td = event.mClipData; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1669 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1670 | // Dispatch down the view hierarchy |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1671 | final PointF localPoint = getLocalPoint(); |
| 1672 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1673 | switch (event.mAction) { |
| 1674 | case DragEvent.ACTION_DRAG_STARTED: { |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 1675 | // Clear the state to recalculate which views we drag over. |
| 1676 | mCurrentDragChild = null; |
| 1677 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1678 | // Set up our tracking of drag-started notifications |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1679 | mCurrentDragStartEvent = DragEvent.obtain(event); |
| 1680 | if (mChildrenInterestedInDrag == null) { |
| 1681 | mChildrenInterestedInDrag = new HashSet<View>(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1682 | } else { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1683 | mChildrenInterestedInDrag.clear(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1686 | // Now dispatch down to our children, caching the responses |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1687 | final int count = mChildrenCount; |
| 1688 | final View[] children = mChildren; |
| 1689 | for (int i = 0; i < count; i++) { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1690 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1691 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1692 | if (child.getVisibility() == VISIBLE) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1693 | if (notifyChildOfDragStart(children[i])) { |
| 1694 | retval = true; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1695 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1696 | } |
| 1697 | } |
| 1698 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1699 | // Notify itself of the drag start. |
| 1700 | mIsInterestedInDrag = super.dispatchDragEvent(event); |
| 1701 | if (mIsInterestedInDrag) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1702 | retval = true; |
| 1703 | } |
Yorke Lee | 0394b21 | 2016-03-10 18:20:04 -0800 | [diff] [blame] | 1704 | |
| 1705 | if (!retval) { |
| 1706 | // Neither us nor any of our children are interested in this drag, so stop tracking |
| 1707 | // the current drag event. |
| 1708 | mCurrentDragStartEvent.recycle(); |
| 1709 | mCurrentDragStartEvent = null; |
| 1710 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1711 | } break; |
| 1712 | |
| 1713 | case DragEvent.ACTION_DRAG_ENDED: { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1714 | // Release the bookkeeping now that the drag lifecycle has ended |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1715 | final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag; |
| 1716 | if (childrenInterestedInDrag != null) { |
| 1717 | for (View child : childrenInterestedInDrag) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1718 | // If a child was interested in the ongoing drag, it's told that it's over |
| 1719 | if (child.dispatchDragEvent(event)) { |
| 1720 | retval = true; |
| 1721 | } |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1722 | } |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1723 | childrenInterestedInDrag.clear(); |
| 1724 | } |
| 1725 | if (mCurrentDragStartEvent != null) { |
| 1726 | mCurrentDragStartEvent.recycle(); |
| 1727 | mCurrentDragStartEvent = null; |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1728 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1729 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1730 | if (mIsInterestedInDrag) { |
| 1731 | if (super.dispatchDragEvent(event)) { |
| 1732 | retval = true; |
| 1733 | } |
| 1734 | mIsInterestedInDrag = false; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1735 | } |
| 1736 | } break; |
| 1737 | |
Vadim Tryshev | 1edc6daf | 2016-09-15 16:19:15 -0700 | [diff] [blame] | 1738 | case DragEvent.ACTION_DRAG_LOCATION: |
| 1739 | case DragEvent.ACTION_DROP: { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1740 | // Find the [possibly new] drag target |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1741 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 1742 | |
| 1743 | if (target != mCurrentDragChild) { |
Vadim Tryshev | 45bee6b | 2016-09-19 11:00:37 -0700 | [diff] [blame] | 1744 | if (sCascadedDragDrop) { |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 1745 | // For pre-Nougat apps, make sure that the whole hierarchy of views that contain |
| 1746 | // the drag location is kept in the state between ENTERED and EXITED events. |
| 1747 | // (Starting with N, only the innermost view will be in that state). |
| 1748 | |
| 1749 | final int action = event.mAction; |
| 1750 | // Position should not be available for ACTION_DRAG_ENTERED and |
| 1751 | // ACTION_DRAG_EXITED. |
| 1752 | event.mX = 0; |
| 1753 | event.mY = 0; |
Vadim Tryshev | a61efa4 | 2016-09-28 15:15:52 -0700 | [diff] [blame] | 1754 | event.mClipData = null; |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 1755 | |
| 1756 | if (mCurrentDragChild != null) { |
| 1757 | event.mAction = DragEvent.ACTION_DRAG_EXITED; |
| 1758 | mCurrentDragChild.dispatchDragEnterExitInPreN(event); |
| 1759 | } |
| 1760 | |
| 1761 | if (target != null) { |
| 1762 | event.mAction = DragEvent.ACTION_DRAG_ENTERED; |
| 1763 | target.dispatchDragEnterExitInPreN(event); |
| 1764 | } |
| 1765 | |
| 1766 | event.mAction = action; |
| 1767 | event.mX = tx; |
| 1768 | event.mY = ty; |
Vadim Tryshev | a61efa4 | 2016-09-28 15:15:52 -0700 | [diff] [blame] | 1769 | event.mClipData = td; |
Vadim Tryshev | ef12811 | 2016-09-16 14:05:53 -0700 | [diff] [blame] | 1770 | } |
| 1771 | mCurrentDragChild = target; |
| 1772 | } |
| 1773 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1774 | if (target == null && mIsInterestedInDrag) { |
| 1775 | target = this; |
| 1776 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1777 | |
Vadim Tryshev | 1edc6daf | 2016-09-15 16:19:15 -0700 | [diff] [blame] | 1778 | // Dispatch the actual drag notice, localized into the target coordinates. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1779 | if (target != null) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1780 | if (target != this) { |
| 1781 | event.mX = localPoint.x; |
| 1782 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1783 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1784 | retval = target.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1785 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1786 | event.mX = tx; |
| 1787 | event.mY = ty; |
Vadim Tryshev | 1edc6daf | 2016-09-15 16:19:15 -0700 | [diff] [blame] | 1788 | |
Vadim Tryshev | 45bee6b | 2016-09-19 11:00:37 -0700 | [diff] [blame] | 1789 | if (mIsInterestedInDrag) { |
| 1790 | final boolean eventWasConsumed; |
| 1791 | if (sCascadedDragDrop) { |
| 1792 | eventWasConsumed = retval; |
| 1793 | } else { |
| 1794 | eventWasConsumed = event.mEventHandlerWasCalled; |
| 1795 | } |
| 1796 | |
| 1797 | if (!eventWasConsumed) { |
| 1798 | retval = super.dispatchDragEvent(event); |
| 1799 | } |
Vadim Tryshev | 1edc6daf | 2016-09-15 16:19:15 -0700 | [diff] [blame] | 1800 | } |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1801 | } else { |
| 1802 | retval = super.dispatchDragEvent(event); |
| 1803 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1804 | } |
| 1805 | } break; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1808 | return retval; |
| 1809 | } |
| 1810 | |
| 1811 | // Find the frontmost child view that lies under the given point, and calculate |
| 1812 | // the position within its own local coordinate system. |
| 1813 | View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1814 | final int count = mChildrenCount; |
| 1815 | final View[] children = mChildren; |
| 1816 | for (int i = count - 1; i >= 0; i--) { |
| 1817 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1818 | if (!child.canAcceptDrag()) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1819 | continue; |
| 1820 | } |
| 1821 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1822 | if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1823 | return child; |
| 1824 | } |
| 1825 | } |
| 1826 | return null; |
| 1827 | } |
| 1828 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1829 | boolean notifyChildOfDragStart(View child) { |
| 1830 | // The caller guarantees that the child is not in mChildrenInterestedInDrag yet. |
| 1831 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1832 | if (ViewDebug.DEBUG_DRAG) { |
| 1833 | Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child); |
| 1834 | } |
| 1835 | |
Vladislav Kaznacheev | c244970 | 2016-05-16 12:57:15 -0700 | [diff] [blame] | 1836 | final float tx = mCurrentDragStartEvent.mX; |
| 1837 | final float ty = mCurrentDragStartEvent.mY; |
| 1838 | |
| 1839 | final float[] point = getTempPoint(); |
| 1840 | point[0] = tx; |
| 1841 | point[1] = ty; |
| 1842 | transformPointToViewLocal(point, child); |
| 1843 | |
| 1844 | mCurrentDragStartEvent.mX = point[0]; |
| 1845 | mCurrentDragStartEvent.mY = point[1]; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1846 | final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent); |
Vladislav Kaznacheev | c244970 | 2016-05-16 12:57:15 -0700 | [diff] [blame] | 1847 | mCurrentDragStartEvent.mX = tx; |
| 1848 | mCurrentDragStartEvent.mY = ty; |
Vadim Tryshev | 1edc6daf | 2016-09-15 16:19:15 -0700 | [diff] [blame] | 1849 | mCurrentDragStartEvent.mEventHandlerWasCalled = false; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1850 | if (canAccept) { |
| 1851 | mChildrenInterestedInDrag.add(child); |
| 1852 | if (!child.canAcceptDrag()) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1853 | child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1854 | child.refreshDrawableState(); |
| 1855 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1856 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1857 | return canAccept; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1858 | } |
| 1859 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1860 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1861 | public void dispatchWindowSystemUiVisiblityChanged(int visible) { |
| 1862 | super.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1863 | |
| 1864 | final int count = mChildrenCount; |
| 1865 | final View[] children = mChildren; |
| 1866 | for (int i=0; i <count; i++) { |
| 1867 | final View child = children[i]; |
| 1868 | child.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | @Override |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1873 | public void dispatchSystemUiVisibilityChanged(int visible) { |
| 1874 | super.dispatchSystemUiVisibilityChanged(visible); |
| 1875 | |
| 1876 | final int count = mChildrenCount; |
| 1877 | final View[] children = mChildren; |
| 1878 | for (int i=0; i <count; i++) { |
| 1879 | final View child = children[i]; |
| 1880 | child.dispatchSystemUiVisibilityChanged(visible); |
| 1881 | } |
| 1882 | } |
| 1883 | |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1884 | @Override |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1885 | boolean updateLocalSystemUiVisibility(int localValue, int localChanges) { |
| 1886 | boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1887 | |
| 1888 | final int count = mChildrenCount; |
| 1889 | final View[] children = mChildren; |
| 1890 | for (int i=0; i <count; i++) { |
| 1891 | final View child = children[i]; |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1892 | changed |= child.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1893 | } |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1894 | return changed; |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1897 | @Override |
| 1898 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1899 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1900 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | return super.dispatchKeyEventPreIme(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1902 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1903 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1904 | return mFocused.dispatchKeyEventPreIme(event); |
| 1905 | } |
| 1906 | return false; |
| 1907 | } |
| 1908 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1909 | @Override |
| 1910 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1911 | if (mInputEventConsistencyVerifier != null) { |
| 1912 | mInputEventConsistencyVerifier.onKeyEvent(event, 1); |
| 1913 | } |
| 1914 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1915 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1916 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1917 | if (super.dispatchKeyEvent(event)) { |
| 1918 | return true; |
| 1919 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1920 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1921 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1922 | if (mFocused.dispatchKeyEvent(event)) { |
| 1923 | return true; |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | if (mInputEventConsistencyVerifier != null) { |
| 1928 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1929 | } |
| 1930 | return false; |
| 1931 | } |
| 1932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | @Override |
| 1934 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1935 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1936 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | return super.dispatchKeyShortcutEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1938 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1939 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1940 | return mFocused.dispatchKeyShortcutEvent(event); |
| 1941 | } |
| 1942 | return false; |
| 1943 | } |
| 1944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1945 | @Override |
| 1946 | public boolean dispatchTrackballEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1947 | if (mInputEventConsistencyVerifier != null) { |
| 1948 | mInputEventConsistencyVerifier.onTrackballEvent(event, 1); |
| 1949 | } |
| 1950 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1951 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1952 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1953 | if (super.dispatchTrackballEvent(event)) { |
| 1954 | return true; |
| 1955 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1956 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1957 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1958 | if (mFocused.dispatchTrackballEvent(event)) { |
| 1959 | return true; |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | if (mInputEventConsistencyVerifier != null) { |
| 1964 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | } |
| 1966 | return false; |
| 1967 | } |
| 1968 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1969 | @Override |
Vladislav Kaznacheev | 3787de1 | 2016-12-21 10:36:35 -0800 | [diff] [blame] | 1970 | public boolean dispatchCapturedPointerEvent(MotionEvent event) { |
| 1971 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1972 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
| 1973 | if (super.dispatchCapturedPointerEvent(event)) { |
| 1974 | return true; |
| 1975 | } |
| 1976 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1977 | == PFLAG_HAS_BOUNDS) { |
| 1978 | if (mFocused.dispatchCapturedPointerEvent(event)) { |
| 1979 | return true; |
| 1980 | } |
| 1981 | } |
| 1982 | return false; |
| 1983 | } |
| 1984 | |
| 1985 | @Override |
| 1986 | public void dispatchPointerCaptureChanged(boolean hasCapture) { |
| 1987 | exitHoverTargets(); |
| 1988 | |
| 1989 | super.dispatchPointerCaptureChanged(hasCapture); |
| 1990 | final int count = mChildrenCount; |
| 1991 | final View[] children = mChildren; |
| 1992 | for (int i = 0; i < count; i++) { |
| 1993 | children[i].dispatchPointerCaptureChanged(hasCapture); |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | @Override |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 1998 | public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) { |
| 1999 | final float x = event.getX(pointerIndex); |
| 2000 | final float y = event.getY(pointerIndex); |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2001 | if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 2002 | return PointerIcon.getSystemIcon(mContext, PointerIcon.TYPE_ARROW); |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2003 | } |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 2004 | // Check what the child under the pointer says about the pointer. |
| 2005 | final int childrenCount = mChildrenCount; |
| 2006 | if (childrenCount != 0) { |
| 2007 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2008 | final boolean customOrder = preorderedList == null |
| 2009 | && isChildrenDrawingOrderEnabled(); |
| 2010 | final View[] children = mChildren; |
| 2011 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2012 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 2013 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Vladislav Kaznacheev | 3d5cfc0 | 2017-01-05 17:50:38 -0800 | [diff] [blame] | 2014 | if (!canViewReceivePointerEvents(child) |
| 2015 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2016 | continue; |
| 2017 | } |
| 2018 | final PointerIcon pointerIcon = |
| 2019 | dispatchResolvePointerIcon(event, pointerIndex, child); |
| 2020 | if (pointerIcon != null) { |
| 2021 | if (preorderedList != null) preorderedList.clear(); |
| 2022 | return pointerIcon; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 2023 | } |
| 2024 | } |
Chris Craik | fc56377 | 2016-05-04 13:34:30 -0700 | [diff] [blame] | 2025 | if (preorderedList != null) preorderedList.clear(); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | // The pointer is not a child or the child has no preferences, returning the default |
| 2029 | // implementation. |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 2030 | return super.onResolvePointerIcon(event, pointerIndex); |
| 2031 | } |
| 2032 | |
| 2033 | private PointerIcon dispatchResolvePointerIcon(MotionEvent event, int pointerIndex, |
| 2034 | View child) { |
| 2035 | final PointerIcon pointerIcon; |
| 2036 | if (!child.hasIdentityMatrix()) { |
| 2037 | MotionEvent transformedEvent = getTransformedMotionEvent(event, child); |
| 2038 | pointerIcon = child.onResolvePointerIcon(transformedEvent, pointerIndex); |
| 2039 | transformedEvent.recycle(); |
| 2040 | } else { |
| 2041 | final float offsetX = mScrollX - child.mLeft; |
| 2042 | final float offsetY = mScrollY - child.mTop; |
| 2043 | event.offsetLocation(offsetX, offsetY); |
| 2044 | pointerIcon = child.onResolvePointerIcon(event, pointerIndex); |
| 2045 | event.offsetLocation(-offsetX, -offsetY); |
| 2046 | } |
| 2047 | return pointerIcon; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2050 | private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) { |
| 2051 | final int childIndex; |
| 2052 | if (customOrder) { |
| 2053 | final int childIndex1 = getChildDrawingOrder(childrenCount, i); |
| 2054 | if (childIndex1 >= childrenCount) { |
| 2055 | throw new IndexOutOfBoundsException("getChildDrawingOrder() " |
| 2056 | + "returned invalid index " + childIndex1 |
| 2057 | + " (child count is " + childrenCount + ")"); |
| 2058 | } |
| 2059 | childIndex = childIndex1; |
| 2060 | } else { |
| 2061 | childIndex = i; |
| 2062 | } |
| 2063 | return childIndex; |
| 2064 | } |
| 2065 | |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 2066 | @SuppressWarnings({"ConstantConditions"}) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | @Override |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2068 | protected boolean dispatchHoverEvent(MotionEvent event) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2069 | final int action = event.getAction(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2070 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2071 | // First check whether the view group wants to intercept the hover event. |
| 2072 | final boolean interceptHover = onInterceptHoverEvent(event); |
| 2073 | event.setAction(action); // restore action in case it was changed |
| 2074 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2075 | MotionEvent eventNoHistory = event; |
| 2076 | boolean handled = false; |
| 2077 | |
| 2078 | // Send events to the hovered children and build a new list of hover targets until |
| 2079 | // one is found that handles the event. |
| 2080 | HoverTarget firstOldHoverTarget = mFirstHoverTarget; |
| 2081 | mFirstHoverTarget = null; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2082 | if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2083 | final float x = event.getX(); |
| 2084 | final float y = event.getY(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2085 | final int childrenCount = mChildrenCount; |
| 2086 | if (childrenCount != 0) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2087 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2088 | final boolean customOrder = preorderedList == null |
| 2089 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2090 | final View[] children = mChildren; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2091 | HoverTarget lastHoverTarget = null; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2092 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2093 | final int childIndex = getAndVerifyPreorderedIndex( |
| 2094 | childrenCount, i, customOrder); |
| 2095 | final View child = getAndVerifyPreorderedView( |
| 2096 | preorderedList, children, childIndex); |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2097 | if (!canViewReceivePointerEvents(child) |
| 2098 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2099 | continue; |
| 2100 | } |
| 2101 | |
| 2102 | // Obtain a hover target for this child. Dequeue it from the |
| 2103 | // old hover target list if the child was previously hovered. |
| 2104 | HoverTarget hoverTarget = firstOldHoverTarget; |
| 2105 | final boolean wasHovered; |
| 2106 | for (HoverTarget predecessor = null; ;) { |
| 2107 | if (hoverTarget == null) { |
| 2108 | hoverTarget = HoverTarget.obtain(child); |
| 2109 | wasHovered = false; |
| 2110 | break; |
| 2111 | } |
| 2112 | |
| 2113 | if (hoverTarget.child == child) { |
| 2114 | if (predecessor != null) { |
| 2115 | predecessor.next = hoverTarget.next; |
| 2116 | } else { |
| 2117 | firstOldHoverTarget = hoverTarget.next; |
| 2118 | } |
| 2119 | hoverTarget.next = null; |
| 2120 | wasHovered = true; |
| 2121 | break; |
| 2122 | } |
| 2123 | |
| 2124 | predecessor = hoverTarget; |
| 2125 | hoverTarget = hoverTarget.next; |
| 2126 | } |
| 2127 | |
| 2128 | // Enqueue the hover target onto the new hover target list. |
| 2129 | if (lastHoverTarget != null) { |
| 2130 | lastHoverTarget.next = hoverTarget; |
| 2131 | } else { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2132 | mFirstHoverTarget = hoverTarget; |
| 2133 | } |
Sangkyu Lee | 8725f36 | 2013-03-13 09:38:45 +0900 | [diff] [blame] | 2134 | lastHoverTarget = hoverTarget; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2135 | |
| 2136 | // Dispatch the event to the child. |
| 2137 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 2138 | if (!wasHovered) { |
| 2139 | // Send the enter as is. |
| 2140 | handled |= dispatchTransformedGenericPointerEvent( |
| 2141 | event, child); // enter |
| 2142 | } |
| 2143 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 2144 | if (!wasHovered) { |
| 2145 | // Synthesize an enter from a move. |
| 2146 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 2147 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 2148 | handled |= dispatchTransformedGenericPointerEvent( |
| 2149 | eventNoHistory, child); // enter |
| 2150 | eventNoHistory.setAction(action); |
| 2151 | |
| 2152 | handled |= dispatchTransformedGenericPointerEvent( |
| 2153 | eventNoHistory, child); // move |
| 2154 | } else { |
| 2155 | // Send the move as is. |
| 2156 | handled |= dispatchTransformedGenericPointerEvent(event, child); |
| 2157 | } |
| 2158 | } |
| 2159 | if (handled) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2160 | break; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2161 | } |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2162 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2163 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2164 | } |
| 2165 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2166 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2167 | // Send exit events to all previously hovered children that are no longer hovered. |
| 2168 | while (firstOldHoverTarget != null) { |
| 2169 | final View child = firstOldHoverTarget.child; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2170 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2171 | // Exit the old hovered child. |
| 2172 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 2173 | // Send the exit as is. |
| 2174 | handled |= dispatchTransformedGenericPointerEvent( |
| 2175 | event, child); // exit |
| 2176 | } else { |
| 2177 | // Synthesize an exit from a move or enter. |
| 2178 | // Ignore the result because hover focus has moved to a different view. |
| 2179 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
Vladislav Kaznacheev | 5a77c37 | 2016-10-10 16:11:15 -0700 | [diff] [blame] | 2180 | final boolean hoverExitPending = event.isHoverExitPending(); |
| 2181 | event.setHoverExitPending(true); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2182 | dispatchTransformedGenericPointerEvent( |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2183 | event, child); // move |
Vladislav Kaznacheev | 5a77c37 | 2016-10-10 16:11:15 -0700 | [diff] [blame] | 2184 | event.setHoverExitPending(hoverExitPending); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2185 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2186 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 2187 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 2188 | dispatchTransformedGenericPointerEvent( |
| 2189 | eventNoHistory, child); // exit |
| 2190 | eventNoHistory.setAction(action); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2193 | final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next; |
| 2194 | firstOldHoverTarget.recycle(); |
| 2195 | firstOldHoverTarget = nextOldHoverTarget; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Vladislav Kaznacheev | 5a77c37 | 2016-10-10 16:11:15 -0700 | [diff] [blame] | 2198 | // Send events to the view group itself if no children have handled it and the view group |
| 2199 | // itself is not currently being hover-exited. |
| 2200 | boolean newHoveredSelf = !handled && |
| 2201 | (action != MotionEvent.ACTION_HOVER_EXIT) && !event.isHoverExitPending(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2202 | if (newHoveredSelf == mHoveredSelf) { |
| 2203 | if (newHoveredSelf) { |
| 2204 | // Send event to the view group as before. |
| 2205 | handled |= super.dispatchHoverEvent(event); |
| 2206 | } |
| 2207 | } else { |
| 2208 | if (mHoveredSelf) { |
| 2209 | // Exit the view group. |
| 2210 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 2211 | // Send the exit as is. |
| 2212 | handled |= super.dispatchHoverEvent(event); // exit |
| 2213 | } else { |
| 2214 | // Synthesize an exit from a move or enter. |
| 2215 | // Ignore the result because hover focus is moving to a different view. |
| 2216 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 2217 | super.dispatchHoverEvent(event); // move |
| 2218 | } |
| 2219 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 2220 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 2221 | super.dispatchHoverEvent(eventNoHistory); // exit |
| 2222 | eventNoHistory.setAction(action); |
| 2223 | } |
| 2224 | mHoveredSelf = false; |
| 2225 | } |
| 2226 | |
| 2227 | if (newHoveredSelf) { |
| 2228 | // Enter the view group. |
| 2229 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 2230 | // Send the enter as is. |
| 2231 | handled |= super.dispatchHoverEvent(event); // enter |
| 2232 | mHoveredSelf = true; |
| 2233 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 2234 | // Synthesize an enter from a move. |
| 2235 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 2236 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 2237 | handled |= super.dispatchHoverEvent(eventNoHistory); // enter |
| 2238 | eventNoHistory.setAction(action); |
| 2239 | |
| 2240 | handled |= super.dispatchHoverEvent(eventNoHistory); // move |
| 2241 | mHoveredSelf = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2242 | } |
| 2243 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2246 | // Recycle the copy of the event that we made. |
| 2247 | if (eventNoHistory != event) { |
| 2248 | eventNoHistory.recycle(); |
| 2249 | } |
| 2250 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2251 | // Done. |
| 2252 | return handled; |
| 2253 | } |
| 2254 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2255 | private void exitHoverTargets() { |
| 2256 | if (mHoveredSelf || mFirstHoverTarget != null) { |
| 2257 | final long now = SystemClock.uptimeMillis(); |
| 2258 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2259 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 2260 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2261 | dispatchHoverEvent(event); |
| 2262 | event.recycle(); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | private void cancelHoverTarget(View view) { |
| 2267 | HoverTarget predecessor = null; |
| 2268 | HoverTarget target = mFirstHoverTarget; |
| 2269 | while (target != null) { |
| 2270 | final HoverTarget next = target.next; |
| 2271 | if (target.child == view) { |
| 2272 | if (predecessor == null) { |
| 2273 | mFirstHoverTarget = next; |
| 2274 | } else { |
| 2275 | predecessor.next = next; |
| 2276 | } |
| 2277 | target.recycle(); |
| 2278 | |
| 2279 | final long now = SystemClock.uptimeMillis(); |
| 2280 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2281 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 2282 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2283 | view.dispatchHoverEvent(event); |
| 2284 | event.recycle(); |
| 2285 | return; |
| 2286 | } |
| 2287 | predecessor = target; |
| 2288 | target = next; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Vladislav Kaznacheev | f847ee3 | 2016-11-21 14:11:00 -0800 | [diff] [blame] | 2292 | @Override |
| 2293 | boolean dispatchTooltipHoverEvent(MotionEvent event) { |
| 2294 | final int action = event.getAction(); |
| 2295 | switch (action) { |
| 2296 | case MotionEvent.ACTION_HOVER_ENTER: |
| 2297 | break; |
| 2298 | |
| 2299 | case MotionEvent.ACTION_HOVER_MOVE: |
| 2300 | View newTarget = null; |
| 2301 | |
| 2302 | // Check what the child under the pointer says about the tooltip. |
| 2303 | final int childrenCount = mChildrenCount; |
| 2304 | if (childrenCount != 0) { |
| 2305 | final float x = event.getX(); |
| 2306 | final float y = event.getY(); |
| 2307 | |
| 2308 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2309 | final boolean customOrder = preorderedList == null |
| 2310 | && isChildrenDrawingOrderEnabled(); |
| 2311 | final View[] children = mChildren; |
| 2312 | for (int i = childrenCount - 1; i >= 0; i--) { |
| 2313 | final int childIndex = |
| 2314 | getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 2315 | final View child = |
| 2316 | getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Vladislav Kaznacheev | 943cf0e | 2017-01-06 09:52:51 -0800 | [diff] [blame] | 2317 | if (!canViewReceivePointerEvents(child) |
| 2318 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2319 | continue; |
| 2320 | } |
| 2321 | if (dispatchTooltipHoverEvent(event, child)) { |
| 2322 | newTarget = child; |
Vladislav Kaznacheev | f847ee3 | 2016-11-21 14:11:00 -0800 | [diff] [blame] | 2323 | break; |
| 2324 | } |
| 2325 | } |
| 2326 | if (preorderedList != null) preorderedList.clear(); |
| 2327 | } |
| 2328 | |
| 2329 | if (mTooltipHoverTarget != newTarget) { |
| 2330 | if (mTooltipHoverTarget != null) { |
| 2331 | event.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 2332 | mTooltipHoverTarget.dispatchTooltipHoverEvent(event); |
| 2333 | event.setAction(action); |
| 2334 | } |
| 2335 | mTooltipHoverTarget = newTarget; |
| 2336 | } |
| 2337 | |
| 2338 | if (mTooltipHoverTarget != null) { |
| 2339 | if (mTooltipHoveredSelf) { |
| 2340 | mTooltipHoveredSelf = false; |
| 2341 | event.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 2342 | super.dispatchTooltipHoverEvent(event); |
| 2343 | event.setAction(action); |
| 2344 | } |
| 2345 | return true; |
| 2346 | } |
| 2347 | |
| 2348 | mTooltipHoveredSelf = super.dispatchTooltipHoverEvent(event); |
| 2349 | return mTooltipHoveredSelf; |
| 2350 | |
| 2351 | case MotionEvent.ACTION_HOVER_EXIT: |
| 2352 | if (mTooltipHoverTarget != null) { |
| 2353 | mTooltipHoverTarget.dispatchTooltipHoverEvent(event); |
| 2354 | mTooltipHoverTarget = null; |
| 2355 | } else if (mTooltipHoveredSelf) { |
| 2356 | super.dispatchTooltipHoverEvent(event); |
| 2357 | mTooltipHoveredSelf = false; |
| 2358 | } |
| 2359 | break; |
| 2360 | } |
| 2361 | return false; |
| 2362 | } |
| 2363 | |
| 2364 | private boolean dispatchTooltipHoverEvent(MotionEvent event, View child) { |
| 2365 | final boolean result; |
| 2366 | if (!child.hasIdentityMatrix()) { |
| 2367 | MotionEvent transformedEvent = getTransformedMotionEvent(event, child); |
| 2368 | result = child.dispatchTooltipHoverEvent(transformedEvent); |
| 2369 | transformedEvent.recycle(); |
| 2370 | } else { |
| 2371 | final float offsetX = mScrollX - child.mLeft; |
| 2372 | final float offsetY = mScrollY - child.mTop; |
| 2373 | event.offsetLocation(offsetX, offsetY); |
| 2374 | result = child.dispatchTooltipHoverEvent(event); |
| 2375 | event.offsetLocation(-offsetX, -offsetY); |
| 2376 | } |
| 2377 | return result; |
| 2378 | } |
| 2379 | |
| 2380 | private void exitTooltipHoverTargets() { |
| 2381 | if (mTooltipHoveredSelf || mTooltipHoverTarget != null) { |
| 2382 | final long now = SystemClock.uptimeMillis(); |
| 2383 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2384 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 2385 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2386 | dispatchTooltipHoverEvent(event); |
| 2387 | event.recycle(); |
| 2388 | } |
| 2389 | } |
| 2390 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2391 | /** @hide */ |
| 2392 | @Override |
| 2393 | protected boolean hasHoveredChild() { |
| 2394 | return mFirstHoverTarget != null; |
| 2395 | } |
| 2396 | |
Dieter Hsu | fa1b8de | 2018-08-22 10:35:58 +0800 | [diff] [blame] | 2397 | /** @hide */ |
| 2398 | @Override |
| 2399 | protected boolean pointInHoveredChild(MotionEvent event) { |
| 2400 | if (mFirstHoverTarget != null) { |
| 2401 | return isTransformedTouchPointInView(event.getX(), event.getY(), |
| 2402 | mFirstHoverTarget.child, null); |
| 2403 | } |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2407 | @Override |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 2408 | public void addChildrenForAccessibility(ArrayList<View> outChildren) { |
Svetoslav | 762621c | 2015-06-01 17:21:59 -0700 | [diff] [blame] | 2409 | if (getAccessibilityNodeProvider() != null) { |
| 2410 | return; |
| 2411 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2412 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
| 2413 | try { |
| 2414 | final int childrenCount = children.getChildCount(); |
| 2415 | for (int i = 0; i < childrenCount; i++) { |
| 2416 | View child = children.getChildAt(i); |
Svetoslav Ganov | c406be9 | 2012-05-11 16:12:32 -0700 | [diff] [blame] | 2417 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2418 | if (child.includeForAccessibility()) { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 2419 | outChildren.add(child); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2420 | } else { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 2421 | child.addChildrenForAccessibility(outChildren); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2422 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2425 | } finally { |
| 2426 | children.recycle(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2431 | * Implement this method to intercept hover events before they are handled |
| 2432 | * by child views. |
| 2433 | * <p> |
| 2434 | * This method is called before dispatching a hover event to a child of |
| 2435 | * the view group or to the view group's own {@link #onHoverEvent} to allow |
| 2436 | * the view group a chance to intercept the hover event. |
| 2437 | * This method can also be used to watch all pointer motions that occur within |
| 2438 | * the bounds of the view group even when the pointer is hovering over |
| 2439 | * a child of the view group rather than over the view group itself. |
| 2440 | * </p><p> |
| 2441 | * The view group can prevent its children from receiving hover events by |
| 2442 | * implementing this method and returning <code>true</code> to indicate |
| 2443 | * that it would like to intercept hover events. The view group must |
| 2444 | * continuously return <code>true</code> from {@link #onInterceptHoverEvent} |
| 2445 | * for as long as it wishes to continue intercepting hover events from |
| 2446 | * its children. |
| 2447 | * </p><p> |
| 2448 | * Interception preserves the invariant that at most one view can be |
| 2449 | * hovered at a time by transferring hover focus from the currently hovered |
| 2450 | * child to the view group or vice-versa as needed. |
| 2451 | * </p><p> |
| 2452 | * If this method returns <code>true</code> and a child is already hovered, then the |
| 2453 | * child view will first receive a hover exit event and then the view group |
| 2454 | * itself will receive a hover enter event in {@link #onHoverEvent}. |
| 2455 | * Likewise, if this method had previously returned <code>true</code> to intercept hover |
| 2456 | * events and instead returns <code>false</code> while the pointer is hovering |
| 2457 | * within the bounds of one of a child, then the view group will first receive a |
| 2458 | * hover exit event in {@link #onHoverEvent} and then the hovered child will |
| 2459 | * receive a hover enter event. |
| 2460 | * </p><p> |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2461 | * The default implementation handles mouse hover on the scroll bars. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2462 | * </p> |
| 2463 | * |
| 2464 | * @param event The motion event that describes the hover. |
| 2465 | * @return True if the view group would like to intercept the hover event |
| 2466 | * and prevent its children from receiving it. |
| 2467 | */ |
| 2468 | public boolean onInterceptHoverEvent(MotionEvent event) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2469 | if (event.isFromSource(InputDevice.SOURCE_MOUSE)) { |
| 2470 | final int action = event.getAction(); |
| 2471 | final float x = event.getX(); |
| 2472 | final float y = event.getY(); |
| 2473 | if ((action == MotionEvent.ACTION_HOVER_MOVE |
| 2474 | || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) { |
| 2475 | return true; |
| 2476 | } |
| 2477 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2478 | return false; |
| 2479 | } |
| 2480 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2481 | private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) { |
| 2482 | if (event.getHistorySize() == 0) { |
| 2483 | return event; |
| 2484 | } |
| 2485 | return MotionEvent.obtainNoHistory(event); |
| 2486 | } |
| 2487 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2488 | @Override |
| 2489 | protected boolean dispatchGenericPointerEvent(MotionEvent event) { |
| 2490 | // Send the event to the child under the pointer. |
| 2491 | final int childrenCount = mChildrenCount; |
| 2492 | if (childrenCount != 0) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2493 | final float x = event.getX(); |
| 2494 | final float y = event.getY(); |
| 2495 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2496 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2497 | final boolean customOrder = preorderedList == null |
| 2498 | && isChildrenDrawingOrderEnabled(); |
| 2499 | final View[] children = mChildren; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2500 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2501 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 2502 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2503 | if (!canViewReceivePointerEvents(child) |
| 2504 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2505 | continue; |
| 2506 | } |
| 2507 | |
| 2508 | if (dispatchTransformedGenericPointerEvent(event, child)) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2509 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2510 | return true; |
| 2511 | } |
| 2512 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2513 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | // No child handled the event. Send it to this view group. |
| 2517 | return super.dispatchGenericPointerEvent(event); |
| 2518 | } |
| 2519 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2520 | @Override |
| 2521 | protected boolean dispatchGenericFocusedEvent(MotionEvent event) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2522 | // Send the event to the focused child or to this view group if it has focus. |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2523 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 2524 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2525 | return super.dispatchGenericFocusedEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2526 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 2527 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2528 | return mFocused.dispatchGenericMotionEvent(event); |
| 2529 | } |
| 2530 | return false; |
| 2531 | } |
| 2532 | |
| 2533 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2534 | * Dispatches a generic pointer event to a child, taking into account |
| 2535 | * transformations that apply to the child. |
| 2536 | * |
| 2537 | * @param event The event to send. |
| 2538 | * @param child The view to send the event to. |
| 2539 | * @return {@code true} if the child handled the event. |
| 2540 | */ |
| 2541 | private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2542 | boolean handled; |
| 2543 | if (!child.hasIdentityMatrix()) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 2544 | MotionEvent transformedEvent = getTransformedMotionEvent(event, child); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2545 | handled = child.dispatchGenericMotionEvent(transformedEvent); |
| 2546 | transformedEvent.recycle(); |
| 2547 | } else { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 2548 | final float offsetX = mScrollX - child.mLeft; |
| 2549 | final float offsetY = mScrollY - child.mTop; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2550 | event.offsetLocation(offsetX, offsetY); |
| 2551 | handled = child.dispatchGenericMotionEvent(event); |
| 2552 | event.offsetLocation(-offsetX, -offsetY); |
| 2553 | } |
| 2554 | return handled; |
| 2555 | } |
| 2556 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 2557 | /** |
| 2558 | * Returns a MotionEvent that's been transformed into the child's local coordinates. |
| 2559 | * |
| 2560 | * It's the responsibility of the caller to recycle it once they're finished with it. |
| 2561 | * @param event The event to transform. |
| 2562 | * @param child The view whose coordinate space is to be used. |
| 2563 | * @return A copy of the the given MotionEvent, transformed into the given View's coordinate |
| 2564 | * space. |
| 2565 | */ |
| 2566 | private MotionEvent getTransformedMotionEvent(MotionEvent event, View child) { |
| 2567 | final float offsetX = mScrollX - child.mLeft; |
| 2568 | final float offsetY = mScrollY - child.mTop; |
| 2569 | final MotionEvent transformedEvent = MotionEvent.obtain(event); |
| 2570 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2571 | if (!child.hasIdentityMatrix()) { |
| 2572 | transformedEvent.transform(child.getInverseMatrix()); |
| 2573 | } |
| 2574 | return transformedEvent; |
| 2575 | } |
| 2576 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2577 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2578 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 2579 | if (mInputEventConsistencyVerifier != null) { |
| 2580 | mInputEventConsistencyVerifier.onTouchEvent(ev, 1); |
| 2581 | } |
| 2582 | |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2583 | // If the event targets the accessibility focused view and this is it, start |
| 2584 | // normal event dispatch. Maybe a descendant is what will handle the click. |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2585 | if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) { |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2586 | ev.setTargetAccessibilityFocus(false); |
| 2587 | } |
| 2588 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2589 | boolean handled = false; |
| 2590 | if (onFilterTouchEventForSecurity(ev)) { |
| 2591 | final int action = ev.getAction(); |
| 2592 | final int actionMasked = action & MotionEvent.ACTION_MASK; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2593 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2594 | // Handle an initial down. |
| 2595 | if (actionMasked == MotionEvent.ACTION_DOWN) { |
| 2596 | // Throw away all previous state when starting a new touch gesture. |
| 2597 | // The framework may have dropped the up or cancel event for the previous gesture |
| 2598 | // due to an app switch, ANR, or some other state change. |
| 2599 | cancelAndClearTouchTargets(ev); |
| 2600 | resetTouchState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2601 | } |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 2602 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2603 | // Check for interception. |
| 2604 | final boolean intercepted; |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2605 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2606 | || mFirstTouchTarget != null) { |
| 2607 | final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 2608 | if (!disallowIntercept) { |
| 2609 | intercepted = onInterceptTouchEvent(ev); |
| 2610 | ev.setAction(action); // restore action in case it was changed |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2611 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2612 | intercepted = false; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2613 | } |
| 2614 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2615 | // There are no touch targets and this action is not an initial down |
| 2616 | // so this view group continues to intercept touches. |
| 2617 | intercepted = true; |
| 2618 | } |
| 2619 | |
| 2620 | // If intercepted, start normal event dispatch. Also if there is already |
| 2621 | // a view that is handling the gesture, do normal event dispatch. |
| 2622 | if (intercepted || mFirstTouchTarget != null) { |
| 2623 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2624 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2625 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2626 | // Check for cancelation. |
| 2627 | final boolean canceled = resetCancelNextUpFlag(this) |
| 2628 | || actionMasked == MotionEvent.ACTION_CANCEL; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2629 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2630 | // Update list of touch targets for pointer down, if needed. |
| 2631 | final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; |
| 2632 | TouchTarget newTouchTarget = null; |
| 2633 | boolean alreadyDispatchedToNewTouchTarget = false; |
| 2634 | if (!canceled && !intercepted) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2635 | |
Phil Weaver | 1e6ecc6 | 2017-11-07 15:28:21 -0800 | [diff] [blame] | 2636 | // If the event is targeting accessibility focus we give it to the |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2637 | // view that has accessibility focus and if it does not handle it |
| 2638 | // we clear the flag and dispatch the event to all children as usual. |
| 2639 | // We are looking up the accessibility focused host to avoid keeping |
| 2640 | // state since these events are very rare. |
| 2641 | View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() |
| 2642 | ? findChildWithAccessibilityFocus() : null; |
| 2643 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2644 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2645 | || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2646 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2647 | final int actionIndex = ev.getActionIndex(); // always 0 for down |
| 2648 | final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) |
| 2649 | : TouchTarget.ALL_POINTER_IDS; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2650 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2651 | // Clean up earlier touch targets for this pointer id in case they |
| 2652 | // have become out of sync. |
| 2653 | removePointersFromTouchTargets(idBitsToAssign); |
| 2654 | |
| 2655 | final int childrenCount = mChildrenCount; |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2656 | if (newTouchTarget == null && childrenCount != 0) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 2657 | final float x = ev.getX(actionIndex); |
| 2658 | final float y = ev.getY(actionIndex); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2659 | // Find a child that can receive the event. |
| 2660 | // Scan children from front to back. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2661 | final ArrayList<View> preorderedList = buildTouchDispatchChildList(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2662 | final boolean customOrder = preorderedList == null |
| 2663 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2664 | final View[] children = mChildren; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2665 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2666 | final int childIndex = getAndVerifyPreorderedIndex( |
| 2667 | childrenCount, i, customOrder); |
| 2668 | final View child = getAndVerifyPreorderedView( |
| 2669 | preorderedList, children, childIndex); |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2670 | |
| 2671 | // If there is a view that has accessibility focus we want it |
| 2672 | // to get the event first and if not handled we will perform a |
| 2673 | // normal dispatch. We may do a double iteration but this is |
| 2674 | // safer given the timeframe. |
| 2675 | if (childWithAccessibilityFocus != null) { |
| 2676 | if (childWithAccessibilityFocus != child) { |
| 2677 | continue; |
| 2678 | } |
| 2679 | childWithAccessibilityFocus = null; |
| 2680 | i = childrenCount - 1; |
| 2681 | } |
| 2682 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2683 | if (!canViewReceivePointerEvents(child) |
| 2684 | || !isTransformedTouchPointInView(x, y, child, null)) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2685 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2686 | continue; |
| 2687 | } |
| 2688 | |
| 2689 | newTouchTarget = getTouchTarget(child); |
| 2690 | if (newTouchTarget != null) { |
| 2691 | // Child is already receiving touch within its bounds. |
| 2692 | // Give it the new pointer in addition to the ones it is handling. |
| 2693 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | resetCancelNextUpFlag(child); |
| 2698 | if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { |
| 2699 | // Child wants to receive touch within its bounds. |
| 2700 | mLastTouchDownTime = ev.getDownTime(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2701 | if (preorderedList != null) { |
| 2702 | // childIndex points into presorted list, find original index |
| 2703 | for (int j = 0; j < childrenCount; j++) { |
| 2704 | if (children[childIndex] == mChildren[j]) { |
| 2705 | mLastTouchDownIndex = j; |
| 2706 | break; |
| 2707 | } |
| 2708 | } |
| 2709 | } else { |
| 2710 | mLastTouchDownIndex = childIndex; |
| 2711 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2712 | mLastTouchDownX = ev.getX(); |
| 2713 | mLastTouchDownY = ev.getY(); |
| 2714 | newTouchTarget = addTouchTarget(child, idBitsToAssign); |
| 2715 | alreadyDispatchedToNewTouchTarget = true; |
| 2716 | break; |
| 2717 | } |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2718 | |
| 2719 | // The accessibility focus didn't handle the event, so clear |
| 2720 | // the flag and do a normal dispatch to all children. |
| 2721 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2722 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2723 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | if (newTouchTarget == null && mFirstTouchTarget != null) { |
| 2727 | // Did not find a child to receive the event. |
| 2728 | // Assign the pointer to the least recently added target. |
| 2729 | newTouchTarget = mFirstTouchTarget; |
| 2730 | while (newTouchTarget.next != null) { |
| 2731 | newTouchTarget = newTouchTarget.next; |
| 2732 | } |
| 2733 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2734 | } |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | // Dispatch to touch targets. |
| 2739 | if (mFirstTouchTarget == null) { |
| 2740 | // No touch targets so treat this as an ordinary view. |
| 2741 | handled = dispatchTransformedTouchEvent(ev, canceled, null, |
| 2742 | TouchTarget.ALL_POINTER_IDS); |
| 2743 | } else { |
| 2744 | // Dispatch to touch targets, excluding the new touch target if we already |
| 2745 | // dispatched to it. Cancel touch targets if necessary. |
| 2746 | TouchTarget predecessor = null; |
| 2747 | TouchTarget target = mFirstTouchTarget; |
| 2748 | while (target != null) { |
| 2749 | final TouchTarget next = target.next; |
| 2750 | if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { |
| 2751 | handled = true; |
| 2752 | } else { |
| 2753 | final boolean cancelChild = resetCancelNextUpFlag(target.child) |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2754 | || intercepted; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2755 | if (dispatchTransformedTouchEvent(ev, cancelChild, |
| 2756 | target.child, target.pointerIdBits)) { |
| 2757 | handled = true; |
| 2758 | } |
| 2759 | if (cancelChild) { |
| 2760 | if (predecessor == null) { |
| 2761 | mFirstTouchTarget = next; |
| 2762 | } else { |
| 2763 | predecessor.next = next; |
| 2764 | } |
| 2765 | target.recycle(); |
| 2766 | target = next; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2767 | continue; |
| 2768 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2769 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2770 | predecessor = target; |
| 2771 | target = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2772 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2773 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2774 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2775 | // Update list of touch targets for pointer up or cancel, if needed. |
| 2776 | if (canceled |
| 2777 | || actionMasked == MotionEvent.ACTION_UP |
| 2778 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
| 2779 | resetTouchState(); |
| 2780 | } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { |
| 2781 | final int actionIndex = ev.getActionIndex(); |
| 2782 | final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); |
| 2783 | removePointersFromTouchTargets(idBitsToRemove); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2784 | } |
| 2785 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2786 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2787 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 2788 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2789 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2790 | return handled; |
| 2791 | } |
| 2792 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2793 | /** |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2794 | * Provide custom ordering of views in which the touch will be dispatched. |
| 2795 | * |
| 2796 | * This is called within a tight loop, so you are not allowed to allocate objects, including |
| 2797 | * the return array. Instead, you should return a pre-allocated list that will be cleared |
| 2798 | * after the dispatch is finished. |
| 2799 | * @hide |
| 2800 | */ |
| 2801 | public ArrayList<View> buildTouchDispatchChildList() { |
| 2802 | return buildOrderedChildList(); |
| 2803 | } |
| 2804 | |
| 2805 | /** |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2806 | * Finds the child which has accessibility focus. |
| 2807 | * |
| 2808 | * @return The child that has focus. |
| 2809 | */ |
| 2810 | private View findChildWithAccessibilityFocus() { |
| 2811 | ViewRootImpl viewRoot = getViewRootImpl(); |
| 2812 | if (viewRoot == null) { |
| 2813 | return null; |
| 2814 | } |
| 2815 | |
| 2816 | View current = viewRoot.getAccessibilityFocusedHost(); |
| 2817 | if (current == null) { |
| 2818 | return null; |
| 2819 | } |
| 2820 | |
| 2821 | ViewParent parent = current.getParent(); |
| 2822 | while (parent instanceof View) { |
| 2823 | if (parent == this) { |
| 2824 | return current; |
| 2825 | } |
| 2826 | current = (View) parent; |
| 2827 | parent = current.getParent(); |
| 2828 | } |
| 2829 | |
| 2830 | return null; |
| 2831 | } |
| 2832 | |
| 2833 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2834 | * Resets all touch state in preparation for a new cycle. |
| 2835 | */ |
| 2836 | private void resetTouchState() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2837 | clearTouchTargets(); |
| 2838 | resetCancelNextUpFlag(this); |
| 2839 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 2840 | mNestedScrollAxes = SCROLL_AXIS_NONE; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2843 | /** |
| 2844 | * Resets the cancel next up flag. |
| 2845 | * Returns true if the flag was previously set. |
| 2846 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2847 | private static boolean resetCancelNextUpFlag(@NonNull View view) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2848 | if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) { |
| 2849 | view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2850 | return true; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 2851 | } |
| 2852 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2853 | } |
| 2854 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2855 | /** |
| 2856 | * Clears all touch targets. |
| 2857 | */ |
| 2858 | private void clearTouchTargets() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2859 | TouchTarget target = mFirstTouchTarget; |
| 2860 | if (target != null) { |
| 2861 | do { |
| 2862 | TouchTarget next = target.next; |
| 2863 | target.recycle(); |
| 2864 | target = next; |
| 2865 | } while (target != null); |
| 2866 | mFirstTouchTarget = null; |
| 2867 | } |
| 2868 | } |
| 2869 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2870 | /** |
| 2871 | * Cancels and clears all touch targets. |
| 2872 | */ |
| 2873 | private void cancelAndClearTouchTargets(MotionEvent event) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2874 | if (mFirstTouchTarget != null) { |
| 2875 | boolean syntheticEvent = false; |
| 2876 | if (event == null) { |
| 2877 | final long now = SystemClock.uptimeMillis(); |
| 2878 | event = MotionEvent.obtain(now, now, |
| 2879 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
Jeff Brown | 2fdbc5a | 2011-06-30 12:25:54 -0700 | [diff] [blame] | 2880 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2881 | syntheticEvent = true; |
| 2882 | } |
| 2883 | |
| 2884 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2885 | resetCancelNextUpFlag(target.child); |
| 2886 | dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits); |
| 2887 | } |
| 2888 | clearTouchTargets(); |
| 2889 | |
| 2890 | if (syntheticEvent) { |
| 2891 | event.recycle(); |
| 2892 | } |
| 2893 | } |
| 2894 | } |
| 2895 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2896 | /** |
| 2897 | * Gets the touch target for specified child view. |
| 2898 | * Returns null if not found. |
| 2899 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2900 | private TouchTarget getTouchTarget(@NonNull View child) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2901 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2902 | if (target.child == child) { |
| 2903 | return target; |
| 2904 | } |
| 2905 | } |
| 2906 | return null; |
| 2907 | } |
| 2908 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2909 | /** |
| 2910 | * Adds a touch target for specified child to the beginning of the list. |
| 2911 | * Assumes the target child is not already present. |
| 2912 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2913 | private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) { |
| 2914 | final TouchTarget target = TouchTarget.obtain(child, pointerIdBits); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2915 | target.next = mFirstTouchTarget; |
| 2916 | mFirstTouchTarget = target; |
| 2917 | return target; |
| 2918 | } |
| 2919 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2920 | /** |
| 2921 | * Removes the pointer ids from consideration. |
| 2922 | */ |
| 2923 | private void removePointersFromTouchTargets(int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2924 | TouchTarget predecessor = null; |
| 2925 | TouchTarget target = mFirstTouchTarget; |
| 2926 | while (target != null) { |
| 2927 | final TouchTarget next = target.next; |
| 2928 | if ((target.pointerIdBits & pointerIdBits) != 0) { |
| 2929 | target.pointerIdBits &= ~pointerIdBits; |
| 2930 | if (target.pointerIdBits == 0) { |
| 2931 | if (predecessor == null) { |
| 2932 | mFirstTouchTarget = next; |
| 2933 | } else { |
| 2934 | predecessor.next = next; |
| 2935 | } |
| 2936 | target.recycle(); |
| 2937 | target = next; |
| 2938 | continue; |
| 2939 | } |
| 2940 | } |
| 2941 | predecessor = target; |
| 2942 | target = next; |
| 2943 | } |
| 2944 | } |
| 2945 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 2946 | @UnsupportedAppUsage |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2947 | private void cancelTouchTarget(View view) { |
| 2948 | TouchTarget predecessor = null; |
| 2949 | TouchTarget target = mFirstTouchTarget; |
| 2950 | while (target != null) { |
| 2951 | final TouchTarget next = target.next; |
| 2952 | if (target.child == view) { |
| 2953 | if (predecessor == null) { |
| 2954 | mFirstTouchTarget = next; |
| 2955 | } else { |
| 2956 | predecessor.next = next; |
| 2957 | } |
| 2958 | target.recycle(); |
| 2959 | |
| 2960 | final long now = SystemClock.uptimeMillis(); |
| 2961 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2962 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 2963 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2964 | view.dispatchTouchEvent(event); |
| 2965 | event.recycle(); |
| 2966 | return; |
| 2967 | } |
| 2968 | predecessor = target; |
| 2969 | target = next; |
| 2970 | } |
| 2971 | } |
| 2972 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2973 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2974 | * Returns true if a child view can receive pointer events. |
| 2975 | * @hide |
| 2976 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2977 | private static boolean canViewReceivePointerEvents(@NonNull View child) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2978 | return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 2979 | || child.getAnimation() != null; |
| 2980 | } |
| 2981 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2982 | private float[] getTempPoint() { |
| 2983 | if (mTempPoint == null) { |
| 2984 | mTempPoint = new float[2]; |
| 2985 | } |
| 2986 | return mTempPoint; |
| 2987 | } |
| 2988 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2989 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2990 | * Returns true if a child view contains the specified point when transformed |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2991 | * into its coordinate space. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2992 | * Child must not be null. |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2993 | * @hide |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2994 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 2995 | @UnsupportedAppUsage |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2996 | protected boolean isTransformedTouchPointInView(float x, float y, View child, |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2997 | PointF outLocalPoint) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2998 | final float[] point = getTempPoint(); |
| 2999 | point[0] = x; |
| 3000 | point[1] = y; |
| 3001 | transformPointToViewLocal(point, child); |
| 3002 | final boolean isInView = child.pointInView(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 3003 | if (isInView && outLocalPoint != null) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 3004 | outLocalPoint.set(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 3005 | } |
| 3006 | return isInView; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3007 | } |
| 3008 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 3009 | /** |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 3010 | * @hide |
| 3011 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 3012 | @UnsupportedAppUsage |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 3013 | public void transformPointToViewLocal(float[] point, View child) { |
| 3014 | point[0] += mScrollX - child.mLeft; |
| 3015 | point[1] += mScrollY - child.mTop; |
| 3016 | |
| 3017 | if (!child.hasIdentityMatrix()) { |
| 3018 | child.getInverseMatrix().mapPoints(point); |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 3023 | * Transforms a motion event into the coordinate space of a particular child view, |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3024 | * filters out irrelevant pointer ids, and overrides its action if necessary. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 3025 | * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead. |
| 3026 | */ |
| 3027 | private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3028 | View child, int desiredPointerIdBits) { |
| 3029 | final boolean handled; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3030 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3031 | // Canceling motions is a special case. We don't need to perform any transformations |
| 3032 | // or filtering. The important part is the action, not the contents. |
| 3033 | final int oldAction = event.getAction(); |
| 3034 | if (cancel || oldAction == MotionEvent.ACTION_CANCEL) { |
| 3035 | event.setAction(MotionEvent.ACTION_CANCEL); |
| 3036 | if (child == null) { |
| 3037 | handled = super.dispatchTouchEvent(event); |
| 3038 | } else { |
| 3039 | handled = child.dispatchTouchEvent(event); |
| 3040 | } |
| 3041 | event.setAction(oldAction); |
| 3042 | return handled; |
| 3043 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3044 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3045 | // Calculate the number of pointers to deliver. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3046 | final int oldPointerIdBits = event.getPointerIdBits(); |
| 3047 | final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3048 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3049 | // If for some reason we ended up in an inconsistent state where it looks like we |
| 3050 | // might produce a motion event with no pointers in it, then drop the event. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3051 | if (newPointerIdBits == 0) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3052 | return false; |
| 3053 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3054 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3055 | // If the number of pointers is the same and we don't need to perform any fancy |
| 3056 | // irreversible transformations, then we can reuse the motion event for this |
| 3057 | // dispatch as long as we are careful to revert any changes we make. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3058 | // Otherwise we need to make a copy. |
| 3059 | final MotionEvent transformedEvent; |
| 3060 | if (newPointerIdBits == oldPointerIdBits) { |
| 3061 | if (child == null || child.hasIdentityMatrix()) { |
| 3062 | if (child == null) { |
| 3063 | handled = super.dispatchTouchEvent(event); |
| 3064 | } else { |
| 3065 | final float offsetX = mScrollX - child.mLeft; |
| 3066 | final float offsetY = mScrollY - child.mTop; |
| 3067 | event.offsetLocation(offsetX, offsetY); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3068 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3069 | handled = child.dispatchTouchEvent(event); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3070 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3071 | event.offsetLocation(-offsetX, -offsetY); |
| 3072 | } |
| 3073 | return handled; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3074 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3075 | transformedEvent = MotionEvent.obtain(event); |
| 3076 | } else { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3077 | transformedEvent = event.split(newPointerIdBits); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3078 | } |
| 3079 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3080 | // Perform any necessary transformations and dispatch. |
| 3081 | if (child == null) { |
| 3082 | handled = super.dispatchTouchEvent(transformedEvent); |
| 3083 | } else { |
| 3084 | final float offsetX = mScrollX - child.mLeft; |
| 3085 | final float offsetY = mScrollY - child.mTop; |
| 3086 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 3087 | if (! child.hasIdentityMatrix()) { |
| 3088 | transformedEvent.transform(child.getInverseMatrix()); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3089 | } |
| 3090 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3091 | handled = child.dispatchTouchEvent(transformedEvent); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3094 | // Done. |
| 3095 | transformedEvent.recycle(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3096 | return handled; |
| 3097 | } |
| 3098 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 3099 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3100 | * Enable or disable the splitting of MotionEvents to multiple children during touch event |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 3101 | * dispatch. This behavior is enabled by default for applications that target an |
| 3102 | * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer. |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3103 | * |
| 3104 | * <p>When this option is enabled MotionEvents may be split and dispatched to different child |
| 3105 | * views depending on where each pointer initially went down. This allows for user interactions |
| 3106 | * such as scrolling two panes of content independently, chording of buttons, and performing |
| 3107 | * independent gestures on different pieces of content. |
| 3108 | * |
| 3109 | * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple |
| 3110 | * child views. <code>false</code> to only allow one child view to be the target of |
| 3111 | * any MotionEvent received by this ViewGroup. |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 3112 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3113 | */ |
| 3114 | public void setMotionEventSplittingEnabled(boolean split) { |
| 3115 | // TODO Applications really shouldn't change this setting mid-touch event, |
| 3116 | // but perhaps this should handle that case and send ACTION_CANCELs to any child views |
| 3117 | // with gestures in progress when this is changed. |
| 3118 | if (split) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3119 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 3120 | } else { |
| 3121 | mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 3126 | * Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3127 | * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
| 3128 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 3129 | @InspectableProperty(name = "splitMotionEvents") |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 3130 | public boolean isMotionEventSplittingEnabled() { |
| 3131 | return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS; |
| 3132 | } |
| 3133 | |
| 3134 | /** |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3135 | * Returns true if this ViewGroup should be considered as a single entity for removal |
| 3136 | * when executing an Activity transition. If this is false, child elements will move |
| 3137 | * individually during the transition. |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 3138 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3139 | * @return True if the ViewGroup should be acted on together during an Activity transition. |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 3140 | * The default value is true when there is a non-null background or if |
| 3141 | * {@link #getTransitionName()} is not null or if a |
| 3142 | * non-null {@link android.view.ViewOutlineProvider} other than |
| 3143 | * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to |
| 3144 | * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3145 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 3146 | @InspectableProperty |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3147 | public boolean isTransitionGroup() { |
| 3148 | if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) { |
| 3149 | return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0); |
| 3150 | } else { |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 3151 | final ViewOutlineProvider outlineProvider = getOutlineProvider(); |
| 3152 | return getBackground() != null || getTransitionName() != null || |
| 3153 | (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | /** |
| 3158 | * Changes whether or not this ViewGroup should be treated as a single entity during |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 3159 | * Activity Transitions. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3160 | * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit |
| 3161 | * in Activity transitions. If false, the ViewGroup won't transition, |
| 3162 | * only its children. If true, the entire ViewGroup will transition |
| 3163 | * together. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 3164 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, |
| 3165 | * android.util.Pair[]) |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3166 | */ |
| 3167 | public void setTransitionGroup(boolean isTransitionGroup) { |
| 3168 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET; |
| 3169 | if (isTransitionGroup) { |
| 3170 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP; |
| 3171 | } else { |
| 3172 | mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP; |
| 3173 | } |
| 3174 | } |
| 3175 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3176 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3177 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) { |
| 3180 | // We're already in this state, assume our ancestors are too |
| 3181 | return; |
| 3182 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3184 | if (disallowIntercept) { |
| 3185 | mGroupFlags |= FLAG_DISALLOW_INTERCEPT; |
| 3186 | } else { |
| 3187 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 3188 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3190 | // Pass it up to our parent |
| 3191 | if (mParent != null) { |
| 3192 | mParent.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | /** |
| 3197 | * Implement this method to intercept all touch screen motion events. This |
| 3198 | * allows you to watch events as they are dispatched to your children, and |
| 3199 | * take ownership of the current gesture at any point. |
| 3200 | * |
| 3201 | * <p>Using this function takes some care, as it has a fairly complicated |
| 3202 | * interaction with {@link View#onTouchEvent(MotionEvent) |
| 3203 | * View.onTouchEvent(MotionEvent)}, and using it requires implementing |
| 3204 | * that method as well as this one in the correct way. Events will be |
| 3205 | * received in the following order: |
| 3206 | * |
| 3207 | * <ol> |
| 3208 | * <li> You will receive the down event here. |
| 3209 | * <li> The down event will be handled either by a child of this view |
| 3210 | * group, or given to your own onTouchEvent() method to handle; this means |
| 3211 | * you should implement onTouchEvent() to return true, so you will |
| 3212 | * continue to see the rest of the gesture (instead of looking for |
| 3213 | * a parent view to handle it). Also, by returning true from |
| 3214 | * onTouchEvent(), you will not receive any following |
| 3215 | * events in onInterceptTouchEvent() and all touch processing must |
| 3216 | * happen in onTouchEvent() like normal. |
| 3217 | * <li> For as long as you return false from this function, each following |
| 3218 | * event (up to and including the final up) will be delivered first here |
| 3219 | * and then to the target's onTouchEvent(). |
| 3220 | * <li> If you return true from here, you will not receive any |
| 3221 | * following events: the target view will receive the same event but |
| 3222 | * with the action {@link MotionEvent#ACTION_CANCEL}, and all further |
| 3223 | * events will be delivered to your onTouchEvent() method and no longer |
| 3224 | * appear here. |
| 3225 | * </ol> |
| 3226 | * |
| 3227 | * @param ev The motion event being dispatched down the hierarchy. |
| 3228 | * @return Return true to steal motion events from the children and have |
| 3229 | * them dispatched to this ViewGroup through onTouchEvent(). |
| 3230 | * The current target will receive an ACTION_CANCEL event, and no further |
| 3231 | * messages will be delivered here. |
| 3232 | */ |
| 3233 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 3234 | if (ev.isFromSource(InputDevice.SOURCE_MOUSE) |
| 3235 | && ev.getAction() == MotionEvent.ACTION_DOWN |
| 3236 | && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY) |
| 3237 | && isOnScrollbarThumb(ev.getX(), ev.getY())) { |
| 3238 | return true; |
| 3239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3240 | return false; |
| 3241 | } |
| 3242 | |
| 3243 | /** |
| 3244 | * {@inheritDoc} |
| 3245 | * |
| 3246 | * Looks for a view to give focus to respecting the setting specified by |
| 3247 | * {@link #getDescendantFocusability()}. |
| 3248 | * |
| 3249 | * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to |
| 3250 | * find focus within the children of this group when appropriate. |
| 3251 | * |
| 3252 | * @see #FOCUS_BEFORE_DESCENDANTS |
| 3253 | * @see #FOCUS_AFTER_DESCENDANTS |
| 3254 | * @see #FOCUS_BLOCK_DESCENDANTS |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 3255 | * @see #onRequestFocusInDescendants(int, android.graphics.Rect) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3256 | */ |
| 3257 | @Override |
| 3258 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 3259 | if (DBG) { |
| 3260 | System.out.println(this + " ViewGroup.requestFocus direction=" |
| 3261 | + direction); |
| 3262 | } |
| 3263 | int descendantFocusability = getDescendantFocusability(); |
| 3264 | |
Evan Rosky | 5db64eb | 2017-11-20 23:04:46 +0000 | [diff] [blame] | 3265 | boolean result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3266 | switch (descendantFocusability) { |
| 3267 | case FOCUS_BLOCK_DESCENDANTS: |
Evan Rosky | 5db64eb | 2017-11-20 23:04:46 +0000 | [diff] [blame] | 3268 | result = super.requestFocus(direction, previouslyFocusedRect); |
| 3269 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3270 | case FOCUS_BEFORE_DESCENDANTS: { |
| 3271 | final boolean took = super.requestFocus(direction, previouslyFocusedRect); |
Evan Rosky | 5db64eb | 2017-11-20 23:04:46 +0000 | [diff] [blame] | 3272 | result = took ? took : onRequestFocusInDescendants(direction, |
| 3273 | previouslyFocusedRect); |
| 3274 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3275 | } |
| 3276 | case FOCUS_AFTER_DESCENDANTS: { |
| 3277 | final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect); |
Evan Rosky | 5db64eb | 2017-11-20 23:04:46 +0000 | [diff] [blame] | 3278 | result = took ? took : super.requestFocus(direction, previouslyFocusedRect); |
| 3279 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3280 | } |
| 3281 | default: |
| 3282 | throw new IllegalStateException("descendant focusability must be " |
| 3283 | + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS " |
| 3284 | + "but is " + descendantFocusability); |
| 3285 | } |
Evan Rosky | 5db64eb | 2017-11-20 23:04:46 +0000 | [diff] [blame] | 3286 | if (result && !isLayoutValid() && ((mPrivateFlags & PFLAG_WANTS_FOCUS) == 0)) { |
| 3287 | mPrivateFlags |= PFLAG_WANTS_FOCUS; |
| 3288 | } |
| 3289 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /** |
| 3293 | * Look for a descendant to call {@link View#requestFocus} on. |
| 3294 | * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)} |
| 3295 | * when it wants to request focus within its children. Override this to |
| 3296 | * customize how your {@link ViewGroup} requests focus within its children. |
| 3297 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 3298 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 3299 | * to give a finer grained hint about where focus is coming from. May be null |
| 3300 | * if there is no hint. |
| 3301 | * @return Whether focus was taken. |
| 3302 | */ |
| 3303 | @SuppressWarnings({"ConstantConditions"}) |
| 3304 | protected boolean onRequestFocusInDescendants(int direction, |
| 3305 | Rect previouslyFocusedRect) { |
| 3306 | int index; |
| 3307 | int increment; |
| 3308 | int end; |
| 3309 | int count = mChildrenCount; |
| 3310 | if ((direction & FOCUS_FORWARD) != 0) { |
| 3311 | index = 0; |
| 3312 | increment = 1; |
| 3313 | end = count; |
| 3314 | } else { |
| 3315 | index = count - 1; |
| 3316 | increment = -1; |
| 3317 | end = -1; |
| 3318 | } |
| 3319 | final View[] children = mChildren; |
| 3320 | for (int i = index; i != end; i += increment) { |
| 3321 | View child = children[i]; |
Vadim Tryshev | b5ced22 | 2017-01-17 19:31:35 -0800 | [diff] [blame] | 3322 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3323 | if (child.requestFocus(direction, previouslyFocusedRect)) { |
| 3324 | return true; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | return false; |
| 3329 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3330 | |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 3331 | @Override |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 3332 | public boolean restoreDefaultFocus() { |
| 3333 | if (mDefaultFocus != null |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 3334 | && getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 3335 | && (mDefaultFocus.mViewFlags & VISIBILITY_MASK) == VISIBLE |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 3336 | && mDefaultFocus.restoreDefaultFocus()) { |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 3337 | return true; |
| 3338 | } |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 3339 | return super.restoreDefaultFocus(); |
| 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * @hide |
| 3344 | */ |
Evan Rosky | 3ac6463 | 2017-02-13 18:04:43 -0800 | [diff] [blame] | 3345 | @TestApi |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 3346 | @Override |
| 3347 | public boolean restoreFocusInCluster(@FocusRealDirection int direction) { |
Evan Rosky | 18b886e | 2017-02-15 13:26:51 -0800 | [diff] [blame] | 3348 | // Allow cluster-navigation to enter touchscreenBlocksFocus ViewGroups. |
| 3349 | if (isKeyboardNavigationCluster()) { |
| 3350 | final boolean blockedFocus = getTouchscreenBlocksFocus(); |
| 3351 | try { |
| 3352 | setTouchscreenBlocksFocusNoRefocus(false); |
| 3353 | return restoreFocusInClusterInternal(direction); |
| 3354 | } finally { |
| 3355 | setTouchscreenBlocksFocusNoRefocus(blockedFocus); |
| 3356 | } |
| 3357 | } else { |
| 3358 | return restoreFocusInClusterInternal(direction); |
| 3359 | } |
| 3360 | } |
| 3361 | |
| 3362 | private boolean restoreFocusInClusterInternal(@FocusRealDirection int direction) { |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 3363 | if (mFocusedInCluster != null && getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 3364 | && (mFocusedInCluster.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 3365 | && mFocusedInCluster.restoreFocusInCluster(direction)) { |
| 3366 | return true; |
| 3367 | } |
| 3368 | return super.restoreFocusInCluster(direction); |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 3369 | } |
| 3370 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 3371 | /** |
Evan Rosky | 3ac6463 | 2017-02-13 18:04:43 -0800 | [diff] [blame] | 3372 | * @hide |
| 3373 | */ |
| 3374 | @Override |
| 3375 | public boolean restoreFocusNotInCluster() { |
| 3376 | if (mFocusedInCluster != null) { |
| 3377 | // since clusters don't nest; we can assume that a non-null mFocusedInCluster |
| 3378 | // will refer to a view not-in a cluster. |
| 3379 | return restoreFocusInCluster(View.FOCUS_DOWN); |
| 3380 | } |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 3381 | if (isKeyboardNavigationCluster() || (mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
Evan Rosky | 3ac6463 | 2017-02-13 18:04:43 -0800 | [diff] [blame] | 3382 | return false; |
| 3383 | } |
| 3384 | int descendentFocusability = getDescendantFocusability(); |
| 3385 | if (descendentFocusability == FOCUS_BLOCK_DESCENDANTS) { |
| 3386 | return super.requestFocus(FOCUS_DOWN, null); |
| 3387 | } |
| 3388 | if (descendentFocusability == FOCUS_BEFORE_DESCENDANTS |
| 3389 | && super.requestFocus(FOCUS_DOWN, null)) { |
| 3390 | return true; |
| 3391 | } |
| 3392 | for (int i = 0; i < mChildrenCount; ++i) { |
| 3393 | View child = mChildren[i]; |
| 3394 | if (!child.isKeyboardNavigationCluster() |
| 3395 | && child.restoreFocusNotInCluster()) { |
| 3396 | return true; |
| 3397 | } |
| 3398 | } |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 3399 | if (descendentFocusability == FOCUS_AFTER_DESCENDANTS && !hasFocusableChild(false)) { |
Evan Rosky | 3ac6463 | 2017-02-13 18:04:43 -0800 | [diff] [blame] | 3400 | return super.requestFocus(FOCUS_DOWN, null); |
| 3401 | } |
| 3402 | return false; |
| 3403 | } |
| 3404 | |
| 3405 | /** |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 3406 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3407 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 3408 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 3409 | */ |
| 3410 | @Override |
| 3411 | public void dispatchStartTemporaryDetach() { |
| 3412 | super.dispatchStartTemporaryDetach(); |
| 3413 | final int count = mChildrenCount; |
| 3414 | final View[] children = mChildren; |
| 3415 | for (int i = 0; i < count; i++) { |
| 3416 | children[i].dispatchStartTemporaryDetach(); |
| 3417 | } |
| 3418 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3419 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 3420 | /** |
| 3421 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3422 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 3423 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 3424 | */ |
| 3425 | @Override |
| 3426 | public void dispatchFinishTemporaryDetach() { |
| 3427 | super.dispatchFinishTemporaryDetach(); |
| 3428 | final int count = mChildrenCount; |
| 3429 | final View[] children = mChildren; |
| 3430 | for (int i = 0; i < count; i++) { |
| 3431 | children[i].dispatchFinishTemporaryDetach(); |
| 3432 | } |
| 3433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3435 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 3436 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3437 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 3438 | mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3439 | super.dispatchAttachedToWindow(info, visibility); |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 3440 | mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
| 3441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | final int count = mChildrenCount; |
| 3443 | final View[] children = mChildren; |
| 3444 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 3445 | final View child = children[i]; |
| 3446 | child.dispatchAttachedToWindow(info, |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 3447 | combineVisibility(visibility, child.getVisibility())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3448 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3449 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 3450 | for (int i = 0; i < transientCount; ++i) { |
| 3451 | View view = mTransientViews.get(i); |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 3452 | view.dispatchAttachedToWindow(info, |
| 3453 | combineVisibility(visibility, view.getVisibility())); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3454 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3455 | } |
| 3456 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3457 | @Override |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 3458 | void dispatchScreenStateChanged(int screenState) { |
| 3459 | super.dispatchScreenStateChanged(screenState); |
| 3460 | |
| 3461 | final int count = mChildrenCount; |
| 3462 | final View[] children = mChildren; |
| 3463 | for (int i = 0; i < count; i++) { |
| 3464 | children[i].dispatchScreenStateChanged(screenState); |
| 3465 | } |
| 3466 | } |
| 3467 | |
Andrii Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 3468 | @Override |
Andrii Kulian | e5c58ee | 2017-03-27 19:25:24 -0700 | [diff] [blame] | 3469 | void dispatchMovedToDisplay(Display display, Configuration config) { |
| 3470 | super.dispatchMovedToDisplay(display, config); |
Andrii Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 3471 | |
| 3472 | final int count = mChildrenCount; |
| 3473 | final View[] children = mChildren; |
| 3474 | for (int i = 0; i < count; i++) { |
Andrii Kulian | e5c58ee | 2017-03-27 19:25:24 -0700 | [diff] [blame] | 3475 | children[i].dispatchMovedToDisplay(display, config); |
Andrii Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 3476 | } |
| 3477 | } |
| 3478 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3479 | /** @hide */ |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 3480 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3481 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3482 | boolean handled = false; |
| 3483 | if (includeForAccessibility()) { |
| 3484 | handled = super.dispatchPopulateAccessibilityEventInternal(event); |
| 3485 | if (handled) { |
| 3486 | return handled; |
| 3487 | } |
Svetoslav Ganov | b84b94e | 2011-09-22 19:26:56 -0700 | [diff] [blame] | 3488 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3489 | // Let our children have a shot in populating the event. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3490 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 3491 | try { |
| 3492 | final int childCount = children.getChildCount(); |
| 3493 | for (int i = 0; i < childCount; i++) { |
| 3494 | View child = children.getChildAt(i); |
| 3495 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 3496 | handled = child.dispatchPopulateAccessibilityEvent(event); |
| 3497 | if (handled) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 3498 | return handled; |
| 3499 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 3500 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3501 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 3502 | } finally { |
| 3503 | children.recycle(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3504 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3505 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 3508 | /** |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 3509 | * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation |
| 3510 | * adds in all child views of the view group, in addition to calling the default View |
| 3511 | * implementation. |
| 3512 | */ |
| 3513 | @Override |
| 3514 | public void dispatchProvideStructure(ViewStructure structure) { |
| 3515 | super.dispatchProvideStructure(structure); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3516 | if (isAssistBlocked() || structure.getChildCount() != 0) { |
| 3517 | return; |
| 3518 | } |
| 3519 | final int childrenCount = mChildrenCount; |
| 3520 | if (childrenCount <= 0) { |
| 3521 | return; |
| 3522 | } |
Philip P. Moltmann | 083d8aa | 2017-08-15 15:22:37 -0700 | [diff] [blame] | 3523 | |
| 3524 | if (!isLaidOut()) { |
Felipe Leme | fe05a52 | 2018-01-23 15:57:49 -0800 | [diff] [blame] | 3525 | if (Helper.sVerbose) { |
| 3526 | Log.v(VIEW_LOG_TAG, "dispatchProvideStructure(): not laid out, ignoring " |
| 3527 | + childrenCount + " children of " + getAccessibilityViewId()); |
| 3528 | } |
Philip P. Moltmann | 083d8aa | 2017-08-15 15:22:37 -0700 | [diff] [blame] | 3529 | return; |
| 3530 | } |
| 3531 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3532 | structure.setChildCount(childrenCount); |
| 3533 | ArrayList<View> preorderedList = buildOrderedChildList(); |
| 3534 | boolean customOrder = preorderedList == null |
| 3535 | && isChildrenDrawingOrderEnabled(); |
| 3536 | for (int i = 0; i < childrenCount; i++) { |
| 3537 | int childIndex; |
| 3538 | try { |
| 3539 | childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3540 | } catch (IndexOutOfBoundsException e) { |
| 3541 | childIndex = i; |
| 3542 | if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.M) { |
| 3543 | Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ " |
| 3544 | + i + " of " + childrenCount, e); |
| 3545 | // At least one app is failing when we call getChildDrawingOrder |
| 3546 | // at this point, so deal semi-gracefully with it by falling back |
| 3547 | // on the basic order. |
| 3548 | customOrder = false; |
| 3549 | if (i > 0) { |
| 3550 | // If we failed at the first index, there really isn't |
| 3551 | // anything to do -- we will just proceed with the simple |
| 3552 | // sequence order. |
| 3553 | // Otherwise, we failed in the middle, so need to come up |
| 3554 | // with an order for the remaining indices and use that. |
| 3555 | // Failed at the first one, easy peasy. |
| 3556 | int[] permutation = new int[childrenCount]; |
| 3557 | SparseBooleanArray usedIndices = new SparseBooleanArray(); |
| 3558 | // Go back and collected the indices we have done so far. |
| 3559 | for (int j = 0; j < i; j++) { |
| 3560 | permutation[j] = getChildDrawingOrder(childrenCount, j); |
| 3561 | usedIndices.put(permutation[j], true); |
| 3562 | } |
| 3563 | // Fill in the remaining indices with indices that have not |
| 3564 | // yet been used. |
| 3565 | int nextIndex = 0; |
| 3566 | for (int j = i; j < childrenCount; j++) { |
| 3567 | while (usedIndices.get(nextIndex, false)) { |
| 3568 | nextIndex++; |
| 3569 | } |
| 3570 | permutation[j] = nextIndex; |
| 3571 | nextIndex++; |
| 3572 | } |
| 3573 | // Build the final view list. |
| 3574 | preorderedList = new ArrayList<>(childrenCount); |
| 3575 | for (int j = 0; j < childrenCount; j++) { |
| 3576 | final int index = permutation[j]; |
| 3577 | final View child = mChildren[index]; |
| 3578 | preorderedList.add(child); |
| 3579 | } |
| 3580 | } |
| 3581 | } else { |
| 3582 | throw e; |
| 3583 | } |
| 3584 | } |
| 3585 | final View child = getAndVerifyPreorderedView(preorderedList, mChildren, |
| 3586 | childIndex); |
| 3587 | final ViewStructure cstructure = structure.newChild(i); |
| 3588 | child.dispatchProvideStructure(cstructure); |
| 3589 | } |
| 3590 | if (preorderedList != null) { |
| 3591 | preorderedList.clear(); |
| 3592 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | /** |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 3596 | * {@inheritDoc} |
| 3597 | * |
| 3598 | * <p>This implementation adds in all child views of the view group, in addition to calling the |
| 3599 | * default {@link View} implementation. |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 3600 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3601 | @Override |
Svet Ganov | fd31f85 | 2017-04-26 15:54:27 -0700 | [diff] [blame] | 3602 | public void dispatchProvideAutofillStructure(ViewStructure structure, |
| 3603 | @AutofillFlags int flags) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3604 | super.dispatchProvideAutofillStructure(structure, flags); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 3605 | |
Svet Ganov | fd31f85 | 2017-04-26 15:54:27 -0700 | [diff] [blame] | 3606 | if (structure.getChildCount() != 0) { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3607 | return; |
| 3608 | } |
Philip P. Moltmann | 083d8aa | 2017-08-15 15:22:37 -0700 | [diff] [blame] | 3609 | |
| 3610 | if (!isLaidOut()) { |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 3611 | if (Helper.sVerbose) { |
| 3612 | Log.v(VIEW_LOG_TAG, "dispatchProvideAutofillStructure(): not laid out, ignoring " |
| 3613 | + mChildrenCount + " children of " + getAutofillId()); |
| 3614 | } |
Philip P. Moltmann | 083d8aa | 2017-08-15 15:22:37 -0700 | [diff] [blame] | 3615 | return; |
| 3616 | } |
| 3617 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 3618 | final ChildListForAutoFillOrContentCapture children = getChildrenForAutofill(flags); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3619 | final int childrenCount = children.size(); |
| 3620 | structure.setChildCount(childrenCount); |
| 3621 | for (int i = 0; i < childrenCount; i++) { |
| 3622 | final View child = children.get(i); |
| 3623 | final ViewStructure cstructure = structure.newChild(i); |
| 3624 | child.dispatchProvideAutofillStructure(cstructure, flags); |
| 3625 | } |
| 3626 | children.recycle(); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 3627 | } |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 3628 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 3629 | /** @hide */ |
| 3630 | @Override |
| 3631 | public void dispatchProvideContentCaptureStructure() { |
| 3632 | super.dispatchProvideContentCaptureStructure(); |
| 3633 | |
| 3634 | if (!isLaidOut()) return; |
| 3635 | |
| 3636 | final ChildListForAutoFillOrContentCapture children = getChildrenForContentCapture(); |
| 3637 | final int childrenCount = children.size(); |
| 3638 | for (int i = 0; i < childrenCount; i++) { |
| 3639 | final View child = children.get(i); |
| 3640 | child.dispatchProvideContentCaptureStructure(); |
| 3641 | } |
| 3642 | children.recycle(); |
| 3643 | } |
| 3644 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3645 | /** |
| 3646 | * Gets the children for autofill. Children for autofill are the first |
| 3647 | * level descendants that are important for autofill. The returned |
| 3648 | * child list object is pooled and the caller must recycle it once done. |
| 3649 | * @hide */ |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 3650 | private @NonNull ChildListForAutoFillOrContentCapture getChildrenForAutofill( |
| 3651 | @AutofillFlags int flags) { |
| 3652 | final ChildListForAutoFillOrContentCapture children = ChildListForAutoFillOrContentCapture |
| 3653 | .obtain(); |
Svet Ganov | fd31f85 | 2017-04-26 15:54:27 -0700 | [diff] [blame] | 3654 | populateChildrenForAutofill(children, flags); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3655 | return children; |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | /** @hide */ |
Svet Ganov | fd31f85 | 2017-04-26 15:54:27 -0700 | [diff] [blame] | 3659 | private void populateChildrenForAutofill(ArrayList<View> list, @AutofillFlags int flags) { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 3660 | final int childrenCount = mChildrenCount; |
| 3661 | if (childrenCount <= 0) { |
| 3662 | return; |
| 3663 | } |
| 3664 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 3665 | final boolean customOrder = preorderedList == null |
| 3666 | && isChildrenDrawingOrderEnabled(); |
| 3667 | for (int i = 0; i < childrenCount; i++) { |
| 3668 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3669 | final View child = (preorderedList == null) |
| 3670 | ? mChildren[childIndex] : preorderedList.get(childIndex); |
Svet Ganov | fd31f85 | 2017-04-26 15:54:27 -0700 | [diff] [blame] | 3671 | if ((flags & AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0 |
| 3672 | || child.isImportantForAutofill()) { |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 3673 | list.add(child); |
| 3674 | } else if (child instanceof ViewGroup) { |
Svet Ganov | fd31f85 | 2017-04-26 15:54:27 -0700 | [diff] [blame] | 3675 | ((ViewGroup) child).populateChildrenForAutofill(list, flags); |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | } |
| 3679 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 3680 | private @NonNull ChildListForAutoFillOrContentCapture getChildrenForContentCapture() { |
| 3681 | final ChildListForAutoFillOrContentCapture children = ChildListForAutoFillOrContentCapture |
| 3682 | .obtain(); |
| 3683 | populateChildrenForContentCapture(children); |
| 3684 | return children; |
| 3685 | } |
| 3686 | |
| 3687 | /** @hide */ |
| 3688 | private void populateChildrenForContentCapture(ArrayList<View> list) { |
| 3689 | final int childrenCount = mChildrenCount; |
| 3690 | if (childrenCount <= 0) { |
| 3691 | return; |
| 3692 | } |
| 3693 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 3694 | final boolean customOrder = preorderedList == null |
| 3695 | && isChildrenDrawingOrderEnabled(); |
| 3696 | for (int i = 0; i < childrenCount; i++) { |
| 3697 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3698 | final View child = (preorderedList == null) |
| 3699 | ? mChildren[childIndex] : preorderedList.get(childIndex); |
| 3700 | if (child.isImportantForContentCapture()) { |
| 3701 | list.add(child); |
| 3702 | } else if (child instanceof ViewGroup) { |
| 3703 | ((ViewGroup) child).populateChildrenForContentCapture(list); |
| 3704 | } |
| 3705 | } |
| 3706 | } |
| 3707 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3708 | private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children, |
| 3709 | int childIndex) { |
| 3710 | final View child; |
| 3711 | if (preorderedList != null) { |
| 3712 | child = preorderedList.get(childIndex); |
| 3713 | if (child == null) { |
| 3714 | throw new RuntimeException("Invalid preorderedList contained null child at index " |
| 3715 | + childIndex); |
| 3716 | } |
| 3717 | } else { |
| 3718 | child = children[childIndex]; |
| 3719 | } |
| 3720 | return child; |
| 3721 | } |
| 3722 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3723 | /** @hide */ |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3724 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 3725 | @UnsupportedAppUsage |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3726 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 3727 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svet Ganov | 55bdb10 | 2015-02-06 12:41:17 -0800 | [diff] [blame] | 3728 | if (getAccessibilityNodeProvider() != null) { |
| 3729 | return; |
| 3730 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3731 | if (mAttachInfo != null) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3732 | final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3733 | childrenForAccessibility.clear(); |
| 3734 | addChildrenForAccessibility(childrenForAccessibility); |
| 3735 | final int childrenForAccessibilityCount = childrenForAccessibility.size(); |
| 3736 | for (int i = 0; i < childrenForAccessibilityCount; i++) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3737 | final View child = childrenForAccessibility.get(i); |
| 3738 | info.addChildUnchecked(child); |
Svetoslav Ganov | ea1da3d | 2011-06-15 17:16:02 -0700 | [diff] [blame] | 3739 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3740 | childrenForAccessibility.clear(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3744 | @Override |
Maxim Bogatov | f399af3 | 2015-06-16 15:15:49 -0700 | [diff] [blame] | 3745 | public CharSequence getAccessibilityClassName() { |
| 3746 | return ViewGroup.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3747 | } |
| 3748 | |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 3749 | @Override |
| 3750 | public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) { |
| 3751 | // If this is a live region, we should send a subtree change event |
| 3752 | // from this view. Otherwise, we can let it propagate up. |
| 3753 | if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) { |
| 3754 | notifyViewAccessibilityStateChangedIfNeeded( |
| 3755 | AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE); |
| 3756 | } else if (mParent != null) { |
| 3757 | try { |
| 3758 | mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType); |
| 3759 | } catch (AbstractMethodError e) { |
| 3760 | Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() + |
| 3761 | " does not fully implement ViewParent", e); |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | |
Phil Weaver | 4d3eec41 | 2016-09-01 16:28:34 -0700 | [diff] [blame] | 3766 | /** @hide */ |
| 3767 | @Override |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 3768 | public void notifySubtreeAccessibilityStateChangedIfNeeded() { |
Phil Weaver | 4d3eec41 | 2016-09-01 16:28:34 -0700 | [diff] [blame] | 3769 | if (!AccessibilityManager.getInstance(mContext).isEnabled() || mAttachInfo == null) { |
| 3770 | return; |
| 3771 | } |
| 3772 | // If something important for a11y is happening in this subtree, make sure it's dispatched |
| 3773 | // from a view that is important for a11y so it doesn't get lost. |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 3774 | if ((getImportantForAccessibility() != IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) |
| 3775 | && !isImportantForAccessibility() && (getChildCount() > 0)) { |
Phil Weaver | 4d3eec41 | 2016-09-01 16:28:34 -0700 | [diff] [blame] | 3776 | ViewParent a11yParent = getParentForAccessibility(); |
| 3777 | if (a11yParent instanceof View) { |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 3778 | ((View) a11yParent).notifySubtreeAccessibilityStateChangedIfNeeded(); |
Phil Weaver | 4d3eec41 | 2016-09-01 16:28:34 -0700 | [diff] [blame] | 3779 | return; |
| 3780 | } |
| 3781 | } |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 3782 | super.notifySubtreeAccessibilityStateChangedIfNeeded(); |
Phil Weaver | 4d3eec41 | 2016-09-01 16:28:34 -0700 | [diff] [blame] | 3783 | } |
| 3784 | |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3785 | @Override |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 3786 | void resetSubtreeAccessibilityStateChanged() { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3787 | super.resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3788 | View[] children = mChildren; |
| 3789 | final int childCount = mChildrenCount; |
| 3790 | for (int i = 0; i < childCount; i++) { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3791 | children[i].resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3792 | } |
| 3793 | } |
| 3794 | |
| 3795 | /** |
Phil Weaver | 1f22254 | 2016-01-08 11:49:32 -0800 | [diff] [blame] | 3796 | * Counts the number of children of this View that will be sent to an accessibility service. |
| 3797 | * |
| 3798 | * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View |
| 3799 | * would have. |
| 3800 | */ |
| 3801 | int getNumChildrenForAccessibility() { |
| 3802 | int numChildrenForAccessibility = 0; |
| 3803 | for (int i = 0; i < getChildCount(); i++) { |
| 3804 | View child = getChildAt(i); |
| 3805 | if (child.includeForAccessibility()) { |
| 3806 | numChildrenForAccessibility++; |
| 3807 | } else if (child instanceof ViewGroup) { |
| 3808 | numChildrenForAccessibility += ((ViewGroup) child) |
| 3809 | .getNumChildrenForAccessibility(); |
| 3810 | } |
| 3811 | } |
| 3812 | return numChildrenForAccessibility; |
| 3813 | } |
| 3814 | |
| 3815 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3816 | * {@inheritDoc} |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 3817 | * |
| 3818 | * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p> |
| 3819 | * |
| 3820 | * @param target The target view dispatching this action |
| 3821 | * @param action Action being performed; see |
| 3822 | * {@link android.view.accessibility.AccessibilityNodeInfo} |
| 3823 | * @param args Optional action arguments |
| 3824 | * @return false by default. Subclasses should return true if they handle the event. |
| 3825 | */ |
| 3826 | @Override |
| 3827 | public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) { |
| 3828 | return false; |
| 3829 | } |
| 3830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3831 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 3832 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3833 | void dispatchDetachedFromWindow() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3834 | // If we still have a touch target, we are still in the process of |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3835 | // dispatching motion events to a child; we need to get rid of that |
| 3836 | // child to avoid dispatching events to it after the window is torn |
| 3837 | // down. To make sure we keep the child in a consistent state, we |
| 3838 | // first send it an ACTION_CANCEL motion event. |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3839 | cancelAndClearTouchTargets(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3840 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 3841 | // Similarly, set ACTION_EXIT to all hover targets and clear them. |
| 3842 | exitHoverTargets(); |
Vladislav Kaznacheev | f847ee3 | 2016-11-21 14:11:00 -0800 | [diff] [blame] | 3843 | exitTooltipHoverTargets(); |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 3844 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3845 | // In case view is detached while transition is running |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 3846 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3847 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3848 | // Tear down our drag tracking |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 3849 | mChildrenInterestedInDrag = null; |
| 3850 | mIsInterestedInDrag = false; |
| 3851 | if (mCurrentDragStartEvent != null) { |
| 3852 | mCurrentDragStartEvent.recycle(); |
| 3853 | mCurrentDragStartEvent = null; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3854 | } |
| 3855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3856 | final int count = mChildrenCount; |
| 3857 | final View[] children = mChildren; |
| 3858 | for (int i = 0; i < count; i++) { |
| 3859 | children[i].dispatchDetachedFromWindow(); |
| 3860 | } |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 3861 | clearDisappearingChildren(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3862 | final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size(); |
| 3863 | for (int i = 0; i < transientCount; ++i) { |
| 3864 | View view = mTransientViews.get(i); |
| 3865 | view.dispatchDetachedFromWindow(); |
| 3866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3867 | super.dispatchDetachedFromWindow(); |
| 3868 | } |
| 3869 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3870 | /** |
| 3871 | * @hide |
| 3872 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3873 | @Override |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3874 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 3875 | super.internalSetPadding(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3876 | |
Romain Guy | 13f35f3 | 2011-03-24 12:03:17 -0700 | [diff] [blame] | 3877 | if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3878 | mGroupFlags |= FLAG_PADDING_NOT_NULL; |
| 3879 | } else { |
| 3880 | mGroupFlags &= ~FLAG_PADDING_NOT_NULL; |
| 3881 | } |
| 3882 | } |
| 3883 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3884 | @Override |
| 3885 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 3886 | super.dispatchSaveInstanceState(container); |
| 3887 | final int count = mChildrenCount; |
| 3888 | final View[] children = mChildren; |
| 3889 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3890 | View c = children[i]; |
| 3891 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3892 | c.dispatchSaveInstanceState(container); |
| 3893 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 3898 | * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()} |
| 3899 | * to only this view, not to its children. For use when overriding |
| 3900 | * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow |
| 3901 | * subclasses to freeze their own state but not the state of their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3902 | * |
| 3903 | * @param container the container |
| 3904 | */ |
| 3905 | protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) { |
| 3906 | super.dispatchSaveInstanceState(container); |
| 3907 | } |
| 3908 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3909 | @Override |
| 3910 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 3911 | super.dispatchRestoreInstanceState(container); |
| 3912 | final int count = mChildrenCount; |
| 3913 | final View[] children = mChildren; |
| 3914 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3915 | View c = children[i]; |
| 3916 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3917 | c.dispatchRestoreInstanceState(container); |
| 3918 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | /** |
Romain Guy | 02739a8 | 2011-05-16 11:43:18 -0700 | [diff] [blame] | 3923 | * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)} |
| 3924 | * to only this view, not to its children. For use when overriding |
| 3925 | * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow |
| 3926 | * subclasses to thaw their own state but not the state of their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3927 | * |
| 3928 | * @param container the container |
| 3929 | */ |
| 3930 | protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) { |
| 3931 | super.dispatchRestoreInstanceState(container); |
| 3932 | } |
| 3933 | |
| 3934 | /** |
| 3935 | * Enables or disables the drawing cache for each child of this view group. |
| 3936 | * |
| 3937 | * @param enabled true to enable the cache, false to dispose of it |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 3938 | * |
| 3939 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 3940 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 3941 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 3942 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 3943 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 3944 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 3945 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 3946 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 3947 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 3948 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 3949 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 3950 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3951 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 3952 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 3954 | if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) { |
| 3955 | final View[] children = mChildren; |
| 3956 | final int count = mChildrenCount; |
| 3957 | for (int i = 0; i < count; i++) { |
| 3958 | children[i].setDrawingCacheEnabled(enabled); |
| 3959 | } |
| 3960 | } |
| 3961 | } |
| 3962 | |
sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 3963 | /** |
| 3964 | * @hide |
| 3965 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3966 | @Override |
Sunny Goyal | d1b287e | 2018-01-04 09:37:22 -0800 | [diff] [blame] | 3967 | public Bitmap createSnapshot(ViewDebug.CanvasProvider canvasProvider, boolean skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3968 | int count = mChildrenCount; |
| 3969 | int[] visibilities = null; |
| 3970 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3971 | if (skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3972 | visibilities = new int[count]; |
| 3973 | for (int i = 0; i < count; i++) { |
| 3974 | View child = getChildAt(i); |
| 3975 | visibilities[i] = child.getVisibility(); |
| 3976 | if (visibilities[i] == View.VISIBLE) { |
sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 3977 | child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK) |
| 3978 | | (View.INVISIBLE & View.VISIBILITY_MASK); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3979 | } |
| 3980 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3981 | } |
| 3982 | |
Sunny Goyal | d1b287e | 2018-01-04 09:37:22 -0800 | [diff] [blame] | 3983 | try { |
| 3984 | return super.createSnapshot(canvasProvider, skipChildren); |
| 3985 | } finally { |
| 3986 | if (skipChildren) { |
| 3987 | for (int i = 0; i < count; i++) { |
| 3988 | View child = getChildAt(i); |
| 3989 | child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK) |
| 3990 | | (visibilities[i] & View.VISIBILITY_MASK); |
| 3991 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3992 | } |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3993 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3994 | } |
| 3995 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3996 | /** Return true if this ViewGroup is laying out using optical bounds. */ |
| 3997 | boolean isLayoutModeOptical() { |
| 3998 | return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS; |
| 3999 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 4000 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4001 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4002 | Insets computeOpticalInsets() { |
| 4003 | if (isLayoutModeOptical()) { |
| 4004 | int left = 0; |
| 4005 | int top = 0; |
| 4006 | int right = 0; |
| 4007 | int bottom = 0; |
| 4008 | for (int i = 0; i < mChildrenCount; i++) { |
| 4009 | View child = getChildAt(i); |
| 4010 | if (child.getVisibility() == VISIBLE) { |
| 4011 | Insets insets = child.getOpticalInsets(); |
| 4012 | left = Math.max(left, insets.left); |
| 4013 | top = Math.max(top, insets.top); |
| 4014 | right = Math.max(right, insets.right); |
| 4015 | bottom = Math.max(bottom, insets.bottom); |
| 4016 | } |
| 4017 | } |
| 4018 | return Insets.of(left, top, right, bottom); |
| 4019 | } else { |
| 4020 | return Insets.NONE; |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) { |
| 4025 | if (x1 != x2 && y1 != y2) { |
| 4026 | if (x1 > x2) { |
| 4027 | int tmp = x1; x1 = x2; x2 = tmp; |
| 4028 | } |
| 4029 | if (y1 > y2) { |
| 4030 | int tmp = y1; y1 = y2; y2 = tmp; |
| 4031 | } |
| 4032 | canvas.drawRect(x1, y1, x2, y2, paint); |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | private static int sign(int x) { |
| 4037 | return (x >= 0) ? 1 : -1; |
| 4038 | } |
| 4039 | |
| 4040 | private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) { |
| 4041 | fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy)); |
| 4042 | fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy); |
| 4043 | } |
| 4044 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 4045 | private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint, |
| 4046 | int lineLength, int lineWidth) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4047 | drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth); |
| 4048 | drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth); |
| 4049 | drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth); |
| 4050 | drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth); |
| 4051 | } |
| 4052 | |
| 4053 | private static void fillDifference(Canvas canvas, |
| 4054 | int x2, int y2, int x3, int y3, |
| 4055 | int dx1, int dy1, int dx2, int dy2, Paint paint) { |
| 4056 | int x1 = x2 - dx1; |
| 4057 | int y1 = y2 - dy1; |
| 4058 | |
| 4059 | int x4 = x3 + dx2; |
| 4060 | int y4 = y3 + dy2; |
| 4061 | |
| 4062 | fillRect(canvas, paint, x1, y1, x4, y2); |
| 4063 | fillRect(canvas, paint, x1, y2, x2, y3); |
| 4064 | fillRect(canvas, paint, x3, y2, x4, y3); |
| 4065 | fillRect(canvas, paint, x1, y3, x4, y4); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | /** |
Alan Viverette | 2d7771c | 2018-01-31 17:04:31 -0500 | [diff] [blame] | 4069 | * @hide |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4070 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4071 | protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4072 | for (int i = 0; i < getChildCount(); i++) { |
| 4073 | View c = getChildAt(i); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4074 | c.getLayoutParams().onDebugDraw(c, canvas, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | /** |
Alan Viverette | 2d7771c | 2018-01-31 17:04:31 -0500 | [diff] [blame] | 4079 | * @hide |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4080 | */ |
| 4081 | protected void onDebugDraw(Canvas canvas) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4082 | Paint paint = getDebugPaint(); |
| 4083 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4084 | // Draw optical bounds |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4085 | { |
| 4086 | paint.setColor(Color.RED); |
| 4087 | paint.setStyle(Paint.Style.STROKE); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4088 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4089 | for (int i = 0; i < getChildCount(); i++) { |
| 4090 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 4091 | if (c.getVisibility() != View.GONE) { |
| 4092 | Insets insets = c.getOpticalInsets(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4093 | |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 4094 | drawRect(canvas, paint, |
| 4095 | c.getLeft() + insets.left, |
| 4096 | c.getTop() + insets.top, |
| 4097 | c.getRight() - insets.right - 1, |
| 4098 | c.getBottom() - insets.bottom - 1); |
| 4099 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4100 | } |
| 4101 | } |
| 4102 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4103 | // Draw margins |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4104 | { |
| 4105 | paint.setColor(Color.argb(63, 255, 0, 255)); |
| 4106 | paint.setStyle(Paint.Style.FILL); |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 4107 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4108 | onDebugDrawMargins(canvas, paint); |
| 4109 | } |
| 4110 | |
| 4111 | // Draw clip bounds |
| 4112 | { |
Vadim Tryshev | cdf38ba | 2016-10-11 18:33:10 -0700 | [diff] [blame] | 4113 | paint.setColor(DEBUG_CORNERS_COLOR); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4114 | paint.setStyle(Paint.Style.FILL); |
| 4115 | |
Vadim Tryshev | cdf38ba | 2016-10-11 18:33:10 -0700 | [diff] [blame] | 4116 | int lineLength = dipsToPixels(DEBUG_CORNERS_SIZE_DIP); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4117 | int lineWidth = dipsToPixels(1); |
| 4118 | for (int i = 0; i < getChildCount(); i++) { |
| 4119 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 4120 | if (c.getVisibility() != View.GONE) { |
| 4121 | drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), |
| 4122 | paint, lineLength, lineWidth); |
| 4123 | } |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 4124 | } |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 4125 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4128 | @Override |
| 4129 | protected void dispatchDraw(Canvas canvas) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 4130 | boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4131 | final int childrenCount = mChildrenCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4132 | final View[] children = mChildren; |
| 4133 | int flags = mGroupFlags; |
| 4134 | |
| 4135 | if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) { |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 4136 | final boolean buildCache = !isHardwareAccelerated(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4137 | for (int i = 0; i < childrenCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4138 | final View child = children[i]; |
| 4139 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 4140 | final LayoutParams params = child.getLayoutParams(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4141 | attachLayoutAnimationParameters(child, params, i, childrenCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4142 | bindLayoutAnimation(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | final LayoutAnimationController controller = mLayoutAnimationController; |
| 4147 | if (controller.willOverlap()) { |
| 4148 | mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE; |
| 4149 | } |
| 4150 | |
| 4151 | controller.start(); |
| 4152 | |
| 4153 | mGroupFlags &= ~FLAG_RUN_ANIMATION; |
| 4154 | mGroupFlags &= ~FLAG_ANIMATION_DONE; |
| 4155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4156 | if (mAnimationListener != null) { |
| 4157 | mAnimationListener.onAnimationStart(controller.getAnimation()); |
| 4158 | } |
| 4159 | } |
| 4160 | |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 4161 | int clipSaveCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4162 | final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 4163 | if (clipToPadding) { |
John Reck | 41f864e | 2016-05-12 15:07:49 -0700 | [diff] [blame] | 4164 | clipSaveCount = canvas.save(Canvas.CLIP_SAVE_FLAG); |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 4165 | canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, |
| 4166 | mScrollX + mRight - mLeft - mPaddingRight, |
| 4167 | mScrollY + mBottom - mTop - mPaddingBottom); |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 4168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4170 | // We will draw our child's animation, let's reset the flag |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4171 | mPrivateFlags &= ~PFLAG_DRAW_ANIMATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4172 | mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED; |
| 4173 | |
| 4174 | boolean more = false; |
| 4175 | final long drawingTime = getDrawingTime(); |
| 4176 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 4177 | if (usingRenderNodeProperties) canvas.insertReorderBarrier(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4178 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4179 | int transientIndex = transientCount != 0 ? 0 : -1; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4180 | // Only use the preordered list if not HW accelerated, since the HW pipeline will do the |
| 4181 | // draw reordering internally |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 4182 | final ArrayList<View> preorderedList = usingRenderNodeProperties |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4183 | ? null : buildOrderedChildList(); |
| 4184 | final boolean customOrder = preorderedList == null |
| 4185 | && isChildrenDrawingOrderEnabled(); |
| 4186 | for (int i = 0; i < childrenCount; i++) { |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4187 | while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) { |
| 4188 | final View transientChild = mTransientViews.get(transientIndex); |
| 4189 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 4190 | transientChild.getAnimation() != null) { |
| 4191 | more |= drawChild(canvas, transientChild, drawingTime); |
| 4192 | } |
| 4193 | transientIndex++; |
| 4194 | if (transientIndex >= transientCount) { |
| 4195 | transientIndex = -1; |
| 4196 | } |
| 4197 | } |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 4198 | |
| 4199 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 4200 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4201 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 4202 | more |= drawChild(canvas, child, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4203 | } |
| 4204 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4205 | while (transientIndex >= 0) { |
| 4206 | // there may be additional transient views after the normal views |
| 4207 | final View transientChild = mTransientViews.get(transientIndex); |
| 4208 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 4209 | transientChild.getAnimation() != null) { |
| 4210 | more |= drawChild(canvas, transientChild, drawingTime); |
| 4211 | } |
| 4212 | transientIndex++; |
| 4213 | if (transientIndex >= transientCount) { |
| 4214 | break; |
| 4215 | } |
| 4216 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4217 | if (preorderedList != null) preorderedList.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4218 | |
| 4219 | // Draw any disappearing views that have animations |
| 4220 | if (mDisappearingChildren != null) { |
| 4221 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 4222 | final int disappearingCount = disappearingChildren.size() - 1; |
| 4223 | // Go backwards -- we may delete as animations finish |
| 4224 | for (int i = disappearingCount; i >= 0; i--) { |
| 4225 | final View child = disappearingChildren.get(i); |
| 4226 | more |= drawChild(canvas, child, drawingTime); |
| 4227 | } |
| 4228 | } |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 4229 | if (usingRenderNodeProperties) canvas.insertInorderBarrier(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4230 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 4231 | if (debugDraw()) { |
| 4232 | onDebugDraw(canvas); |
| 4233 | } |
| 4234 | |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 4235 | if (clipToPadding) { |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 4236 | canvas.restoreToCount(clipSaveCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | // mGroupFlags might have been updated by drawChild() |
| 4240 | flags = mGroupFlags; |
| 4241 | |
| 4242 | if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) { |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4243 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 && |
| 4247 | mLayoutAnimationController.isDone() && !more) { |
| 4248 | // We want to erase the drawing cache and notify the listener after the |
| 4249 | // next frame is drawn because one extra invalidate() is caused by |
| 4250 | // drawChild() after the animation is over |
| 4251 | mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER; |
| 4252 | final Runnable end = new Runnable() { |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4253 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4254 | public void run() { |
| 4255 | notifyAnimationListener(); |
| 4256 | } |
| 4257 | }; |
| 4258 | post(end); |
| 4259 | } |
| 4260 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4262 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 4263 | * Returns the ViewGroupOverlay for this view group, creating it if it does |
| 4264 | * not yet exist. In addition to {@link ViewOverlay}'s support for drawables, |
| 4265 | * {@link ViewGroupOverlay} allows views to be added to the overlay. These |
| 4266 | * views, like overlay drawables, are visual-only; they do not receive input |
| 4267 | * events and should not be used as anything other than a temporary |
| 4268 | * representation of a view in a parent container, such as might be used |
| 4269 | * by an animation effect. |
| 4270 | * |
Chet Haase | 9539949 | 2013-04-08 14:30:31 -0700 | [diff] [blame] | 4271 | * <p>Note: Overlays do not currently work correctly with {@link |
| 4272 | * SurfaceView} or {@link TextureView}; contents in overlays for these |
| 4273 | * types of views may not display correctly.</p> |
| 4274 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 4275 | * @return The ViewGroupOverlay object for this view. |
| 4276 | * @see ViewGroupOverlay |
| 4277 | */ |
| 4278 | @Override |
| 4279 | public ViewGroupOverlay getOverlay() { |
| 4280 | if (mOverlay == null) { |
| 4281 | mOverlay = new ViewGroupOverlay(mContext, this); |
| 4282 | } |
| 4283 | return (ViewGroupOverlay) mOverlay; |
| 4284 | } |
| 4285 | |
| 4286 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4287 | * Returns the index of the child to draw for this iteration. Override this |
| 4288 | * if you want to change the drawing order of children. By default, it |
| 4289 | * returns i. |
| 4290 | * <p> |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 4291 | * NOTE: In order for this method to be called, you must enable child ordering |
| 4292 | * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4293 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4294 | * @param i The current iteration. |
| 4295 | * @return The index of the child to draw this iteration. |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 4296 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 4297 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 4298 | * @see #isChildrenDrawingOrderEnabled() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4299 | */ |
| 4300 | protected int getChildDrawingOrder(int childCount, int i) { |
| 4301 | return i; |
| 4302 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4303 | |
Andrey Kulikov | 7c4e693 | 2019-02-05 19:13:19 +0000 | [diff] [blame] | 4304 | /** |
| 4305 | * The public version of getChildDrawingOrder(). |
| 4306 | * |
| 4307 | * Returns the index of the child to draw for this iteration. |
| 4308 | * |
| 4309 | * @param i The current iteration. |
| 4310 | * @return The index of the child to draw this iteration. |
| 4311 | * |
| 4312 | * @see #getChildDrawingOrder(int, int)} |
| 4313 | */ |
| 4314 | public final int getChildDrawingOrder(int i) { |
| 4315 | return getChildDrawingOrder(getChildCount(), i); |
| 4316 | } |
| 4317 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4318 | private boolean hasChildWithZ() { |
| 4319 | for (int i = 0; i < mChildrenCount; i++) { |
| 4320 | if (mChildren[i].getZ() != 0) return true; |
| 4321 | } |
| 4322 | return false; |
| 4323 | } |
| 4324 | |
| 4325 | /** |
| 4326 | * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children, |
Chris Craik | 57c79c8 | 2014-09-30 12:54:31 -0700 | [diff] [blame] | 4327 | * sorted first by Z, then by child drawing order (if applicable). This list must be cleared |
| 4328 | * after use to avoid leaking child Views. |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4329 | * |
| 4330 | * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated |
| 4331 | * children. |
| 4332 | */ |
George Mount | 8120652 | 2014-09-26 21:53:39 -0700 | [diff] [blame] | 4333 | ArrayList<View> buildOrderedChildList() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 4334 | final int childrenCount = mChildrenCount; |
| 4335 | if (childrenCount <= 1 || !hasChildWithZ()) return null; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4336 | |
| 4337 | if (mPreSortedChildren == null) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 4338 | mPreSortedChildren = new ArrayList<>(childrenCount); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4339 | } else { |
Chris Craik | fc56377 | 2016-05-04 13:34:30 -0700 | [diff] [blame] | 4340 | // callers should clear, so clear shouldn't be necessary, but for safety... |
| 4341 | mPreSortedChildren.clear(); |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 4342 | mPreSortedChildren.ensureCapacity(childrenCount); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4343 | } |
| 4344 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 4345 | final boolean customOrder = isChildrenDrawingOrderEnabled(); |
| 4346 | for (int i = 0; i < childrenCount; i++) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4347 | // add next child (in child order) to end of list |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 4348 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 4349 | final View nextChild = mChildren[childIndex]; |
| 4350 | final float currentZ = nextChild.getZ(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 4351 | |
| 4352 | // insert ahead of any Views with greater Z |
| 4353 | int insertIndex = i; |
| 4354 | while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) { |
| 4355 | insertIndex--; |
| 4356 | } |
| 4357 | mPreSortedChildren.add(insertIndex, nextChild); |
| 4358 | } |
| 4359 | return mPreSortedChildren; |
| 4360 | } |
| 4361 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4362 | private void notifyAnimationListener() { |
| 4363 | mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER; |
| 4364 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 4365 | |
| 4366 | if (mAnimationListener != null) { |
| 4367 | final Runnable end = new Runnable() { |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4368 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4369 | public void run() { |
| 4370 | mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation()); |
| 4371 | } |
| 4372 | }; |
| 4373 | post(end); |
| 4374 | } |
| 4375 | |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4376 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 4380 | * This method is used to cause children of this ViewGroup to restore or recreate their |
| 4381 | * display lists. It is called by getDisplayList() when the parent ViewGroup does not need |
| 4382 | * to recreate its own display list, which would happen if it went through the normal |
| 4383 | * draw/dispatchDraw mechanisms. |
| 4384 | * |
| 4385 | * @hide |
| 4386 | */ |
| 4387 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 4388 | @UnsupportedAppUsage |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 4389 | protected void dispatchGetDisplayList() { |
| 4390 | final int count = mChildrenCount; |
| 4391 | final View[] children = mChildren; |
| 4392 | for (int i = 0; i < count; i++) { |
| 4393 | final View child = children[i]; |
John Reck | c2330f5 | 2015-04-28 13:18:52 -0700 | [diff] [blame] | 4394 | if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) { |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 4395 | recreateChildDisplayList(child); |
Romain Guy | 2f57ba5 | 2011-02-03 18:03:29 -0800 | [diff] [blame] | 4396 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 4397 | } |
Selim Cinek | d6a51b1 | 2018-03-15 17:10:20 -0700 | [diff] [blame] | 4398 | final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size(); |
| 4399 | for (int i = 0; i < transientCount; ++i) { |
| 4400 | View child = mTransientViews.get(i); |
| 4401 | if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) { |
| 4402 | recreateChildDisplayList(child); |
| 4403 | } |
| 4404 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 4405 | if (mOverlay != null) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 4406 | View overlayView = mOverlay.getOverlayView(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 4407 | recreateChildDisplayList(overlayView); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 4408 | } |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 4409 | if (mDisappearingChildren != null) { |
| 4410 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 4411 | final int disappearingCount = disappearingChildren.size(); |
| 4412 | for (int i = 0; i < disappearingCount; ++i) { |
| 4413 | final View child = disappearingChildren.get(i); |
| 4414 | recreateChildDisplayList(child); |
| 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | private void recreateChildDisplayList(View child) { |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 4420 | child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0; |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 4421 | child.mPrivateFlags &= ~PFLAG_INVALIDATED; |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 4422 | child.updateDisplayListIfDirty(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 4423 | child.mRecreateDisplayList = false; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4427 | * Draw one child of this View Group. This method is responsible for getting |
| 4428 | * the canvas in the right state. This includes clipping, translating so |
| 4429 | * that the child's scrolled origin is at 0, 0, and applying any animation |
| 4430 | * transformations. |
| 4431 | * |
| 4432 | * @param canvas The canvas on which to draw the child |
| 4433 | * @param child Who to draw |
Chet Haase | bcca79a | 2012-02-14 08:45:14 -0800 | [diff] [blame] | 4434 | * @param drawingTime The time at which draw is occurring |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4435 | * @return True if an invalidate() was issued |
| 4436 | */ |
| 4437 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 4438 | return child.draw(canvas, this, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4439 | } |
| 4440 | |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 4441 | @Override |
| 4442 | void getScrollIndicatorBounds(@NonNull Rect out) { |
| 4443 | super.getScrollIndicatorBounds(out); |
| 4444 | |
| 4445 | // If we have padding and we're supposed to clip children to that |
| 4446 | // padding, offset the scroll indicators to match our clip bounds. |
| 4447 | final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 4448 | if (clipToPadding) { |
| 4449 | out.left += mPaddingLeft; |
| 4450 | out.right -= mPaddingRight; |
| 4451 | out.top += mPaddingTop; |
| 4452 | out.bottom -= mPaddingBottom; |
| 4453 | } |
| 4454 | } |
| 4455 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4456 | /** |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 4457 | * Returns whether this group's children are clipped to their bounds before drawing. |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 4458 | * The default value is true. |
| 4459 | * @see #setClipChildren(boolean) |
| 4460 | * |
| 4461 | * @return True if the group's children will be clipped to their bounds, |
| 4462 | * false otherwise. |
| 4463 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 4464 | @ViewDebug.ExportedProperty(category = "drawing") |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 4465 | @InspectableProperty |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 4466 | public boolean getClipChildren() { |
| 4467 | return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0); |
| 4468 | } |
| 4469 | |
| 4470 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4471 | * By default, children are clipped to their bounds before drawing. This |
| 4472 | * allows view groups to override this behavior for animations, etc. |
| 4473 | * |
| 4474 | * @param clipChildren true to clip children to their bounds, |
| 4475 | * false otherwise |
| 4476 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 4477 | */ |
| 4478 | public void setClipChildren(boolean clipChildren) { |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 4479 | boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN; |
| 4480 | if (clipChildren != previousValue) { |
| 4481 | setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren); |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 4482 | for (int i = 0; i < mChildrenCount; ++i) { |
| 4483 | View child = getChildAt(i); |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 4484 | if (child.mRenderNode != null) { |
| 4485 | child.mRenderNode.setClipToBounds(clipChildren); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 4486 | } |
| 4487 | } |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 4488 | invalidate(true); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 4489 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 4493 | * Sets whether this ViewGroup will clip its children to its padding and resize (but not |
| 4494 | * clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 4495 | * <p> |
| 4496 | * By default, children are clipped to the padding of their parent |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 4497 | * ViewGroup. This clipping behavior is only enabled if padding is non-zero. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4498 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 4499 | * @param clipToPadding true to clip children to the padding of the group, and resize (but |
| 4500 | * not clip) any EdgeEffect to the padded region. False otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4501 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 4502 | */ |
| 4503 | public void setClipToPadding(boolean clipToPadding) { |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 4504 | if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) { |
| 4505 | setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding); |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 4506 | invalidate(true); |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 4507 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 4511 | * Returns whether this ViewGroup will clip its children to its padding, and resize (but |
| 4512 | * not clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 4513 | * <p> |
| 4514 | * By default, children are clipped to the padding of their parent |
| 4515 | * Viewgroup. This clipping behavior is only enabled if padding is non-zero. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 4516 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 4517 | * @return true if this ViewGroup clips children to its padding and resizes (but doesn't |
| 4518 | * clip) any EdgeEffect to the padded region, false otherwise. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 4519 | * |
| 4520 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 4521 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 4522 | @ViewDebug.ExportedProperty(category = "drawing") |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 4523 | @InspectableProperty |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 4524 | public boolean getClipToPadding() { |
| 4525 | return hasBooleanFlag(FLAG_CLIP_TO_PADDING); |
| 4526 | } |
| 4527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4528 | @Override |
| 4529 | public void dispatchSetSelected(boolean selected) { |
| 4530 | final View[] children = mChildren; |
| 4531 | final int count = mChildrenCount; |
| 4532 | for (int i = 0; i < count; i++) { |
| 4533 | children[i].setSelected(selected); |
| 4534 | } |
| 4535 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4536 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 4537 | @Override |
| 4538 | public void dispatchSetActivated(boolean activated) { |
| 4539 | final View[] children = mChildren; |
| 4540 | final int count = mChildrenCount; |
| 4541 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 4542 | children[i].setActivated(activated); |
| 4543 | } |
| 4544 | } |
| 4545 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4546 | @Override |
| 4547 | protected void dispatchSetPressed(boolean pressed) { |
| 4548 | final View[] children = mChildren; |
| 4549 | final int count = mChildrenCount; |
| 4550 | for (int i = 0; i < count; i++) { |
Adam Powell | 035a1fc | 2012-02-27 15:23:50 -0800 | [diff] [blame] | 4551 | final View child = children[i]; |
| 4552 | // Children that are clickable on their own should not |
| 4553 | // show a pressed state when their parent view does. |
| 4554 | // Clearing a pressed state always propagates. |
| 4555 | if (!pressed || (!child.isClickable() && !child.isLongClickable())) { |
| 4556 | child.setPressed(pressed); |
| 4557 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 4561 | /** |
| 4562 | * Dispatches drawable hotspot changes to child views that meet at least |
| 4563 | * one of the following criteria: |
| 4564 | * <ul> |
| 4565 | * <li>Returns {@code false} from both {@link View#isClickable()} and |
| 4566 | * {@link View#isLongClickable()}</li> |
| 4567 | * <li>Requests duplication of parent state via |
| 4568 | * {@link View#setDuplicateParentStateEnabled(boolean)}</li> |
| 4569 | * </ul> |
| 4570 | * |
| 4571 | * @param x hotspot x coordinate |
| 4572 | * @param y hotspot y coordinate |
| 4573 | * @see #drawableHotspotChanged(float, float) |
| 4574 | */ |
| 4575 | @Override |
| 4576 | public void dispatchDrawableHotspotChanged(float x, float y) { |
| 4577 | final int count = mChildrenCount; |
| 4578 | if (count == 0) { |
| 4579 | return; |
| 4580 | } |
| 4581 | |
| 4582 | final View[] children = mChildren; |
| 4583 | for (int i = 0; i < count; i++) { |
| 4584 | final View child = children[i]; |
| 4585 | // Children that are clickable on their own should not |
| 4586 | // receive hotspots when their parent view does. |
| 4587 | final boolean nonActionable = !child.isClickable() && !child.isLongClickable(); |
| 4588 | final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0; |
| 4589 | if (nonActionable || duplicatesState) { |
| 4590 | final float[] point = getTempPoint(); |
| 4591 | point[0] = x; |
| 4592 | point[1] = y; |
| 4593 | transformPointToViewLocal(point, child); |
| 4594 | child.drawableHotspotChanged(point[0], point[1]); |
| 4595 | } |
| 4596 | } |
| 4597 | } |
| 4598 | |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4599 | @Override |
| 4600 | void dispatchCancelPendingInputEvents() { |
| 4601 | super.dispatchCancelPendingInputEvents(); |
| 4602 | |
| 4603 | final View[] children = mChildren; |
| 4604 | final int count = mChildrenCount; |
| 4605 | for (int i = 0; i < count; i++) { |
| 4606 | children[i].dispatchCancelPendingInputEvents(); |
| 4607 | } |
| 4608 | } |
| 4609 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4610 | /** |
| 4611 | * When this property is set to true, this ViewGroup supports static transformations on |
| 4612 | * children; this causes |
| 4613 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 4614 | * invoked when a child is drawn. |
| 4615 | * |
| 4616 | * Any subclass overriding |
| 4617 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 4618 | * set this property to true. |
| 4619 | * |
| 4620 | * @param enabled True to enable static transformations on children, false otherwise. |
| 4621 | * |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 4622 | * @see #getChildStaticTransformation(View, android.view.animation.Transformation) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4623 | */ |
| 4624 | protected void setStaticTransformationsEnabled(boolean enabled) { |
| 4625 | setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled); |
| 4626 | } |
| 4627 | |
| 4628 | /** |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 4629 | * Sets <code>t</code> to be the static transformation of the child, if set, returning a |
| 4630 | * boolean to indicate whether a static transform was set. The default implementation |
| 4631 | * simply returns <code>false</code>; subclasses may override this method for different |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 4632 | * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true |
| 4633 | * for this method to be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4634 | * |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 4635 | * @param child The child view whose static transform is being requested |
| 4636 | * @param t The Transformation which will hold the result |
| 4637 | * @return true if the transformation was set, false otherwise |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4638 | * @see #setStaticTransformationsEnabled(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4639 | */ |
| 4640 | protected boolean getChildStaticTransformation(View child, Transformation t) { |
| 4641 | return false; |
| 4642 | } |
| 4643 | |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 4644 | Transformation getChildTransformation() { |
| 4645 | if (mChildTransformation == null) { |
| 4646 | mChildTransformation = new Transformation(); |
| 4647 | } |
| 4648 | return mChildTransformation; |
| 4649 | } |
| 4650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | /** |
| 4652 | * {@hide} |
| 4653 | */ |
| 4654 | @Override |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4655 | protected <T extends View> T findViewTraversal(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4656 | if (id == mID) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4657 | return (T) this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | final View[] where = mChildren; |
| 4661 | final int len = mChildrenCount; |
| 4662 | |
| 4663 | for (int i = 0; i < len; i++) { |
| 4664 | View v = where[i]; |
| 4665 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4666 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4667 | v = v.findViewById(id); |
| 4668 | |
| 4669 | if (v != null) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4670 | return (T) v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4671 | } |
| 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | return null; |
| 4676 | } |
| 4677 | |
| 4678 | /** |
| 4679 | * {@hide} |
| 4680 | */ |
| 4681 | @Override |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4682 | protected <T extends View> T findViewWithTagTraversal(Object tag) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4683 | if (tag != null && tag.equals(mTag)) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4684 | return (T) this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | final View[] where = mChildren; |
| 4688 | final int len = mChildrenCount; |
| 4689 | |
| 4690 | for (int i = 0; i < len; i++) { |
| 4691 | View v = where[i]; |
| 4692 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4693 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4694 | v = v.findViewWithTag(tag); |
| 4695 | |
| 4696 | if (v != null) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4697 | return (T) v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | } |
| 4701 | |
| 4702 | return null; |
| 4703 | } |
| 4704 | |
| 4705 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4706 | * {@hide} |
| 4707 | */ |
| 4708 | @Override |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4709 | protected <T extends View> T findViewByPredicateTraversal(Predicate<View> predicate, |
| 4710 | View childToSkip) { |
Paul Duffin | ca4964c | 2017-02-07 15:04:10 +0000 | [diff] [blame] | 4711 | if (predicate.test(this)) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4712 | return (T) this; |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | final View[] where = mChildren; |
| 4716 | final int len = mChildrenCount; |
| 4717 | |
| 4718 | for (int i = 0; i < len; i++) { |
| 4719 | View v = where[i]; |
| 4720 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4721 | if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4722 | v = v.findViewByPredicate(predicate); |
| 4723 | |
| 4724 | if (v != null) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 4725 | return (T) v; |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4726 | } |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | return null; |
| 4731 | } |
| 4732 | |
| 4733 | /** |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4734 | * This method adds a view to this container at the specified index purely for the |
| 4735 | * purposes of allowing that view to draw even though it is not a normal child of |
| 4736 | * the container. That is, the view does not participate in layout, focus, accessibility, |
| 4737 | * input, or other normal view operations; it is purely an item to be drawn during the normal |
| 4738 | * rendering operation of this container. The index that it is added at is the order |
| 4739 | * in which it will be drawn, with respect to the other views in the container. |
| 4740 | * For example, a transient view added at index 0 will be drawn before all other views |
| 4741 | * in the container because it will be drawn first (including before any real view |
| 4742 | * at index 0). There can be more than one transient view at any particular index; |
| 4743 | * these views will be drawn in the order in which they were added to the list of |
| 4744 | * transient views. The index of transient views can also be greater than the number |
| 4745 | * of normal views in the container; that just means that they will be drawn after all |
| 4746 | * other views are drawn. |
| 4747 | * |
| 4748 | * <p>Note that since transient views do not participate in layout, they must be sized |
| 4749 | * manually or, more typically, they should just use the size that they had before they |
| 4750 | * were removed from their container.</p> |
| 4751 | * |
| 4752 | * <p>Transient views are useful for handling animations of views that have been removed |
| 4753 | * from the container, but which should be animated out after the removal. Adding these |
| 4754 | * views as transient views allows them to participate in drawing without side-effecting |
| 4755 | * the layout of the container.</p> |
| 4756 | * |
| 4757 | * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed} |
| 4758 | * from the container when they are no longer needed. For example, a transient view |
| 4759 | * which is added in order to fade it out in its old location should be removed |
| 4760 | * once the animation is complete.</p> |
| 4761 | * |
Riddle Hsu | 95459d3 | 2018-06-08 15:54:08 +0800 | [diff] [blame] | 4762 | * @param view The view to be added. The view must not have a parent. |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4763 | * @param index The index at which this view should be drawn, must be >= 0. |
| 4764 | * This value is relative to the {@link #getChildAt(int) index} values in the normal |
| 4765 | * child list of this container, where any transient view at a particular index will |
| 4766 | * be drawn before any normal child at that same index. |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4767 | * |
| 4768 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4769 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 4770 | @UnsupportedAppUsage |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4771 | public void addTransientView(View view, int index) { |
Riddle Hsu | 95459d3 | 2018-06-08 15:54:08 +0800 | [diff] [blame] | 4772 | if (index < 0 || view == null) { |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4773 | return; |
| 4774 | } |
Riddle Hsu | 95459d3 | 2018-06-08 15:54:08 +0800 | [diff] [blame] | 4775 | if (view.mParent != null) { |
| 4776 | throw new IllegalStateException("The specified view already has a parent " |
| 4777 | + view.mParent); |
| 4778 | } |
| 4779 | |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4780 | if (mTransientIndices == null) { |
| 4781 | mTransientIndices = new ArrayList<Integer>(); |
| 4782 | mTransientViews = new ArrayList<View>(); |
| 4783 | } |
| 4784 | final int oldSize = mTransientIndices.size(); |
| 4785 | if (oldSize > 0) { |
| 4786 | int insertionIndex; |
| 4787 | for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) { |
| 4788 | if (index < mTransientIndices.get(insertionIndex)) { |
| 4789 | break; |
| 4790 | } |
| 4791 | } |
| 4792 | mTransientIndices.add(insertionIndex, index); |
| 4793 | mTransientViews.add(insertionIndex, view); |
| 4794 | } else { |
| 4795 | mTransientIndices.add(index); |
| 4796 | mTransientViews.add(view); |
| 4797 | } |
| 4798 | view.mParent = this; |
Riddle Hsu | 95459d3 | 2018-06-08 15:54:08 +0800 | [diff] [blame] | 4799 | if (mAttachInfo != null) { |
| 4800 | view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags & VISIBILITY_MASK)); |
| 4801 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4802 | invalidate(true); |
| 4803 | } |
| 4804 | |
| 4805 | /** |
| 4806 | * Removes a view from the list of transient views in this container. If there is no |
| 4807 | * such transient view, this method does nothing. |
| 4808 | * |
| 4809 | * @param view The transient view to be removed |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4810 | * |
| 4811 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4812 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 4813 | @UnsupportedAppUsage |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4814 | public void removeTransientView(View view) { |
| 4815 | if (mTransientViews == null) { |
| 4816 | return; |
| 4817 | } |
| 4818 | final int size = mTransientViews.size(); |
| 4819 | for (int i = 0; i < size; ++i) { |
| 4820 | if (view == mTransientViews.get(i)) { |
| 4821 | mTransientViews.remove(i); |
| 4822 | mTransientIndices.remove(i); |
| 4823 | view.mParent = null; |
Riddle Hsu | 95459d3 | 2018-06-08 15:54:08 +0800 | [diff] [blame] | 4824 | if (view.mAttachInfo != null) { |
| 4825 | view.dispatchDetachedFromWindow(); |
| 4826 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4827 | invalidate(true); |
| 4828 | return; |
| 4829 | } |
| 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | /** |
| 4834 | * Returns the number of transient views in this container. Specific transient |
| 4835 | * views and the index at which they were added can be retrieved via |
| 4836 | * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}. |
| 4837 | * |
| 4838 | * @see #addTransientView(View, int) |
| 4839 | * @return The number of transient views in this container |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4840 | * |
| 4841 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4842 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 4843 | @UnsupportedAppUsage |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4844 | public int getTransientViewCount() { |
| 4845 | return mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4846 | } |
| 4847 | |
| 4848 | /** |
| 4849 | * Given a valid position within the list of transient views, returns the index of |
| 4850 | * the transient view at that position. |
| 4851 | * |
| 4852 | * @param position The position of the index being queried. Must be at least 0 |
| 4853 | * and less than the value returned by {@link #getTransientViewCount()}. |
| 4854 | * @return The index of the transient view stored in the given position if the |
| 4855 | * position is valid, otherwise -1 |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4856 | * |
| 4857 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4858 | */ |
| 4859 | public int getTransientViewIndex(int position) { |
| 4860 | if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) { |
| 4861 | return -1; |
| 4862 | } |
| 4863 | return mTransientIndices.get(position); |
| 4864 | } |
| 4865 | |
| 4866 | /** |
| 4867 | * Given a valid position within the list of transient views, returns the |
| 4868 | * transient view at that position. |
| 4869 | * |
| 4870 | * @param position The position of the view being queried. Must be at least 0 |
| 4871 | * and less than the value returned by {@link #getTransientViewCount()}. |
| 4872 | * @return The transient view stored in the given position if the |
| 4873 | * position is valid, otherwise null |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4874 | * |
| 4875 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4876 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 4877 | @UnsupportedAppUsage |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4878 | public View getTransientView(int position) { |
| 4879 | if (mTransientViews == null || position >= mTransientViews.size()) { |
| 4880 | return null; |
| 4881 | } |
| 4882 | return mTransientViews.get(position); |
| 4883 | } |
| 4884 | |
| 4885 | /** |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4886 | * <p>Adds a child view. If no layout parameters are already set on the child, the |
| 4887 | * default parameters for this ViewGroup are set on the child.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4888 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4889 | * <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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4892 | * |
| 4893 | * @param child the child view to add |
| 4894 | * |
| 4895 | * @see #generateDefaultLayoutParams() |
| 4896 | */ |
| 4897 | public void addView(View child) { |
| 4898 | addView(child, -1); |
| 4899 | } |
| 4900 | |
| 4901 | /** |
| 4902 | * Adds a child view. If no layout parameters are already set on the child, the |
| 4903 | * default parameters for this ViewGroup are set on the child. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4904 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4905 | * <p><strong>Note:</strong> do not invoke this method from |
| 4906 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4907 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4908 | * |
| 4909 | * @param child the child view to add |
| 4910 | * @param index the position at which to add the child |
| 4911 | * |
| 4912 | * @see #generateDefaultLayoutParams() |
| 4913 | */ |
| 4914 | public void addView(View child, int index) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4915 | if (child == null) { |
| 4916 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4917 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4918 | LayoutParams params = child.getLayoutParams(); |
| 4919 | if (params == null) { |
| 4920 | params = generateDefaultLayoutParams(); |
| 4921 | if (params == null) { |
| 4922 | throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); |
| 4923 | } |
| 4924 | } |
| 4925 | addView(child, index, params); |
| 4926 | } |
| 4927 | |
| 4928 | /** |
| 4929 | * Adds a child view with this ViewGroup's default layout parameters and the |
| 4930 | * specified width and height. |
| 4931 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4932 | * <p><strong>Note:</strong> do not invoke this method from |
| 4933 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4934 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4935 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4936 | * @param child the child view to add |
| 4937 | */ |
| 4938 | public void addView(View child, int width, int height) { |
| 4939 | final LayoutParams params = generateDefaultLayoutParams(); |
| 4940 | params.width = width; |
| 4941 | params.height = height; |
| 4942 | addView(child, -1, params); |
| 4943 | } |
| 4944 | |
| 4945 | /** |
| 4946 | * Adds a child view with the specified layout parameters. |
| 4947 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4948 | * <p><strong>Note:</strong> do not invoke this method from |
| 4949 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4950 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4951 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4952 | * @param child the child view to add |
| 4953 | * @param params the layout parameters to set on the child |
| 4954 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4955 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4956 | public void addView(View child, LayoutParams params) { |
| 4957 | addView(child, -1, params); |
| 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * Adds a child view with the specified layout parameters. |
| 4962 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4963 | * <p><strong>Note:</strong> do not invoke this method from |
| 4964 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4965 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4966 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4967 | * @param child the child view to add |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4968 | * @param index the position at which to add the child or -1 to add last |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4969 | * @param params the layout parameters to set on the child |
| 4970 | */ |
| 4971 | public void addView(View child, int index, LayoutParams params) { |
| 4972 | if (DBG) { |
| 4973 | System.out.println(this + " addView"); |
| 4974 | } |
| 4975 | |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4976 | if (child == null) { |
| 4977 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4978 | } |
| 4979 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | // addViewInner() will call child.requestLayout() when setting the new LayoutParams |
| 4981 | // therefore, we call requestLayout() on ourselves before, so that the child's request |
| 4982 | // will be blocked at our level |
| 4983 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4984 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4985 | addViewInner(child, index, params, false); |
| 4986 | } |
| 4987 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4988 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4989 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 4990 | if (!checkLayoutParams(params)) { |
| 4991 | throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this); |
| 4992 | } |
| 4993 | if (view.mParent != this) { |
| 4994 | throw new IllegalArgumentException("Given view not a child of " + this); |
| 4995 | } |
| 4996 | view.setLayoutParams(params); |
| 4997 | } |
| 4998 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4999 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 5000 | return p != null; |
| 5001 | } |
| 5002 | |
| 5003 | /** |
| 5004 | * Interface definition for a callback to be invoked when the hierarchy |
| 5005 | * within this view changed. The hierarchy changes whenever a child is added |
| 5006 | * to or removed from this view. |
| 5007 | */ |
| 5008 | public interface OnHierarchyChangeListener { |
| 5009 | /** |
| 5010 | * Called when a new child is added to a parent view. |
| 5011 | * |
| 5012 | * @param parent the view in which a child was added |
| 5013 | * @param child the new child view added in the hierarchy |
| 5014 | */ |
| 5015 | void onChildViewAdded(View parent, View child); |
| 5016 | |
| 5017 | /** |
| 5018 | * Called when a child is removed from a parent view. |
| 5019 | * |
| 5020 | * @param parent the view from which the child was removed |
| 5021 | * @param child the child removed from the hierarchy |
| 5022 | */ |
| 5023 | void onChildViewRemoved(View parent, View child); |
| 5024 | } |
| 5025 | |
| 5026 | /** |
| 5027 | * Register a callback to be invoked when a child is added to or removed |
| 5028 | * from this view. |
| 5029 | * |
| 5030 | * @param listener the callback to invoke on hierarchy change |
| 5031 | */ |
| 5032 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 5033 | mOnHierarchyChangeListener = listener; |
| 5034 | } |
| 5035 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 5036 | @UnsupportedAppUsage |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5037 | void dispatchViewAdded(View child) { |
| 5038 | onViewAdded(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 5039 | if (mOnHierarchyChangeListener != null) { |
| 5040 | mOnHierarchyChangeListener.onChildViewAdded(this, child); |
| 5041 | } |
| 5042 | } |
| 5043 | |
| 5044 | /** |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5045 | * Called when a new child is added to this ViewGroup. Overrides should always |
| 5046 | * call super.onViewAdded. |
| 5047 | * |
| 5048 | * @param child the added child view |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 5049 | */ |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5050 | public void onViewAdded(View child) { |
| 5051 | } |
| 5052 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 5053 | @UnsupportedAppUsage |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5054 | void dispatchViewRemoved(View child) { |
| 5055 | onViewRemoved(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 5056 | if (mOnHierarchyChangeListener != null) { |
| 5057 | mOnHierarchyChangeListener.onChildViewRemoved(this, child); |
| 5058 | } |
| 5059 | } |
| 5060 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5061 | /** |
| 5062 | * Called when a child view is removed from this ViewGroup. Overrides should always |
| 5063 | * call super.onViewRemoved. |
| 5064 | * |
| 5065 | * @param child the removed child view |
| 5066 | */ |
| 5067 | public void onViewRemoved(View child) { |
| 5068 | } |
| 5069 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5070 | private void clearCachedLayoutMode() { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5071 | if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5072 | mLayoutMode = LAYOUT_MODE_UNDEFINED; |
| 5073 | } |
| 5074 | } |
| 5075 | |
| 5076 | @Override |
| 5077 | protected void onAttachedToWindow() { |
| 5078 | super.onAttachedToWindow(); |
| 5079 | clearCachedLayoutMode(); |
| 5080 | } |
| 5081 | |
| 5082 | @Override |
| 5083 | protected void onDetachedFromWindow() { |
| 5084 | super.onDetachedFromWindow(); |
| 5085 | clearCachedLayoutMode(); |
| 5086 | } |
| 5087 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 5088 | /** @hide */ |
| 5089 | @Override |
| 5090 | protected void destroyHardwareResources() { |
| 5091 | super.destroyHardwareResources(); |
| 5092 | int count = getChildCount(); |
| 5093 | for (int i = 0; i < count; i++) { |
| 5094 | getChildAt(i).destroyHardwareResources(); |
| 5095 | } |
| 5096 | } |
| 5097 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 5098 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5099 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 5100 | * you need to add more views (as does the list view for example). |
| 5101 | * |
| 5102 | * If index is negative, it means put it at the end of the list. |
| 5103 | * |
| 5104 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 5105 | * @param index the index at which the child must be added or -1 to add last |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5106 | * @param params the layout parameters to associate with the child |
| 5107 | * @return true if the child was added, false otherwise |
| 5108 | */ |
| 5109 | protected boolean addViewInLayout(View child, int index, LayoutParams params) { |
| 5110 | return addViewInLayout(child, index, params, false); |
| 5111 | } |
| 5112 | |
| 5113 | /** |
| 5114 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 5115 | * you need to add more views (as does the list view for example). |
| 5116 | * |
| 5117 | * If index is negative, it means put it at the end of the list. |
| 5118 | * |
| 5119 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 5120 | * @param index the index at which the child must be added or -1 to add last |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5121 | * @param params the layout parameters to associate with the child |
| 5122 | * @param preventRequestLayout if true, calling this method will not trigger a |
| 5123 | * layout request on child |
| 5124 | * @return true if the child was added, false otherwise |
| 5125 | */ |
| 5126 | protected boolean addViewInLayout(View child, int index, LayoutParams params, |
| 5127 | boolean preventRequestLayout) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 5128 | if (child == null) { |
| 5129 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 5130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5131 | child.mParent = null; |
| 5132 | addViewInner(child, index, params, preventRequestLayout); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5133 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5134 | return true; |
| 5135 | } |
| 5136 | |
| 5137 | /** |
| 5138 | * Prevents the specified child to be laid out during the next layout pass. |
| 5139 | * |
| 5140 | * @param child the child on which to perform the cleanup |
| 5141 | */ |
| 5142 | protected void cleanupLayoutState(View child) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5143 | child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | private void addViewInner(View child, int index, LayoutParams params, |
| 5147 | boolean preventRequestLayout) { |
| 5148 | |
Chet Haase | e8e45d3 | 2011-03-02 17:07:35 -0800 | [diff] [blame] | 5149 | if (mTransition != null) { |
| 5150 | // Don't prevent other add transitions from completing, but cancel remove |
| 5151 | // transitions to let them complete the process before we add to the container |
| 5152 | mTransition.cancel(LayoutTransition.DISAPPEARING); |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 5153 | } |
| 5154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5155 | if (child.getParent() != null) { |
| 5156 | throw new IllegalStateException("The specified child already has a parent. " + |
| 5157 | "You must call removeView() on the child's parent first."); |
| 5158 | } |
| 5159 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5160 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 5161 | mTransition.addChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5162 | } |
| 5163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5164 | if (!checkLayoutParams(params)) { |
| 5165 | params = generateLayoutParams(params); |
| 5166 | } |
| 5167 | |
| 5168 | if (preventRequestLayout) { |
| 5169 | child.mLayoutParams = params; |
| 5170 | } else { |
| 5171 | child.setLayoutParams(params); |
| 5172 | } |
| 5173 | |
| 5174 | if (index < 0) { |
| 5175 | index = mChildrenCount; |
| 5176 | } |
| 5177 | |
| 5178 | addInArray(child, index); |
| 5179 | |
| 5180 | // tell our children |
| 5181 | if (preventRequestLayout) { |
| 5182 | child.assignParent(this); |
| 5183 | } else { |
| 5184 | child.mParent = this; |
Evan Rosky | cd80e61 | 2018-05-17 17:46:09 -0700 | [diff] [blame] | 5185 | } |
| 5186 | if (child.hasUnhandledKeyListener()) { |
| 5187 | incrementChildUnhandledKeyListeners(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5188 | } |
| 5189 | |
Evan Rosky | 2ae1bf5 | 2017-05-11 11:18:45 -0700 | [diff] [blame] | 5190 | final boolean childHasFocus = child.hasFocus(); |
| 5191 | if (childHasFocus) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5192 | requestChildFocus(child, child.findFocus()); |
| 5193 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5195 | AttachInfo ai = mAttachInfo; |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 5196 | if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5197 | boolean lastKeepOn = ai.mKeepScreenOn; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5198 | ai.mKeepScreenOn = false; |
| 5199 | child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 5200 | if (ai.mKeepScreenOn) { |
| 5201 | needGlobalAttributesUpdate(true); |
| 5202 | } |
| 5203 | ai.mKeepScreenOn = lastKeepOn; |
| 5204 | } |
| 5205 | |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5206 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | a7e0bcd | 2012-10-16 19:55:01 -0700 | [diff] [blame] | 5207 | child.resetRtlProperties(); |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5208 | } |
| 5209 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5210 | dispatchViewAdded(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5211 | |
| 5212 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { |
| 5213 | mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; |
| 5214 | } |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 5215 | |
| 5216 | if (child.hasTransientState()) { |
| 5217 | childHasTransientStateChanged(child, true); |
| 5218 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5219 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 5220 | if (child.getVisibility() != View.GONE) { |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 5221 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5222 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 5223 | |
| 5224 | if (mTransientIndices != null) { |
| 5225 | final int transientCount = mTransientIndices.size(); |
| 5226 | for (int i = 0; i < transientCount; ++i) { |
| 5227 | final int oldIndex = mTransientIndices.get(i); |
| 5228 | if (index <= oldIndex) { |
| 5229 | mTransientIndices.set(i, oldIndex + 1); |
| 5230 | } |
| 5231 | } |
| 5232 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 5233 | |
| 5234 | if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) { |
| 5235 | notifyChildOfDragStart(child); |
| 5236 | } |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 5237 | |
| 5238 | if (child.hasDefaultFocus()) { |
| 5239 | // When adding a child that contains default focus, either during inflation or while |
| 5240 | // manually assembling the hierarchy, update the ancestor default-focus chain. |
| 5241 | setDefaultFocus(child); |
| 5242 | } |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 5243 | |
| 5244 | touchAccessibilityNodeProviderIfNeeded(child); |
| 5245 | } |
| 5246 | |
| 5247 | /** |
| 5248 | * We may need to touch the provider to bring up the a11y layer. In a11y mode |
| 5249 | * clients inspect the screen or the user touches it which triggers bringing up |
| 5250 | * of the a11y infrastructure while in autofill mode we want the infra up and |
| 5251 | * running from the beginning since we watch for a11y events to drive autofill. |
| 5252 | */ |
| 5253 | private void touchAccessibilityNodeProviderIfNeeded(View child) { |
| 5254 | if (mContext.isAutofillCompatibilityEnabled()) { |
| 5255 | child.getAccessibilityNodeProvider(); |
| 5256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | private void addInArray(View child, int index) { |
| 5260 | View[] children = mChildren; |
| 5261 | final int count = mChildrenCount; |
| 5262 | final int size = children.length; |
| 5263 | if (index == count) { |
| 5264 | if (size == count) { |
| 5265 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 5266 | System.arraycopy(children, 0, mChildren, 0, size); |
| 5267 | children = mChildren; |
| 5268 | } |
| 5269 | children[mChildrenCount++] = child; |
| 5270 | } else if (index < count) { |
| 5271 | if (size == count) { |
| 5272 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 5273 | System.arraycopy(children, 0, mChildren, 0, index); |
| 5274 | System.arraycopy(children, index, mChildren, index + 1, count - index); |
| 5275 | children = mChildren; |
| 5276 | } else { |
| 5277 | System.arraycopy(children, index, children, index + 1, count - index); |
| 5278 | } |
| 5279 | children[index] = child; |
| 5280 | mChildrenCount++; |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 5281 | if (mLastTouchDownIndex >= index) { |
| 5282 | mLastTouchDownIndex++; |
| 5283 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5284 | } else { |
| 5285 | throw new IndexOutOfBoundsException("index=" + index + " count=" + count); |
| 5286 | } |
| 5287 | } |
| 5288 | |
| 5289 | // This method also sets the child's mParent to null |
| 5290 | private void removeFromArray(int index) { |
| 5291 | final View[] children = mChildren; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5292 | if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { |
| 5293 | children[index].mParent = null; |
| 5294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5295 | final int count = mChildrenCount; |
| 5296 | if (index == count - 1) { |
| 5297 | children[--mChildrenCount] = null; |
| 5298 | } else if (index >= 0 && index < count) { |
| 5299 | System.arraycopy(children, index + 1, children, index, count - index - 1); |
| 5300 | children[--mChildrenCount] = null; |
| 5301 | } else { |
| 5302 | throw new IndexOutOfBoundsException(); |
| 5303 | } |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 5304 | if (mLastTouchDownIndex == index) { |
| 5305 | mLastTouchDownTime = 0; |
| 5306 | mLastTouchDownIndex = -1; |
| 5307 | } else if (mLastTouchDownIndex > index) { |
| 5308 | mLastTouchDownIndex--; |
| 5309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | // This method also sets the children's mParent to null |
| 5313 | private void removeFromArray(int start, int count) { |
| 5314 | final View[] children = mChildren; |
| 5315 | final int childrenCount = mChildrenCount; |
| 5316 | |
| 5317 | start = Math.max(0, start); |
| 5318 | final int end = Math.min(childrenCount, start + count); |
| 5319 | |
| 5320 | if (start == end) { |
| 5321 | return; |
| 5322 | } |
| 5323 | |
| 5324 | if (end == childrenCount) { |
| 5325 | for (int i = start; i < end; i++) { |
| 5326 | children[i].mParent = null; |
| 5327 | children[i] = null; |
| 5328 | } |
| 5329 | } else { |
| 5330 | for (int i = start; i < end; i++) { |
| 5331 | children[i].mParent = null; |
| 5332 | } |
| 5333 | |
| 5334 | // Since we're looping above, we might as well do the copy, but is arraycopy() |
| 5335 | // faster than the extra 2 bounds checks we would do in the loop? |
| 5336 | System.arraycopy(children, end, children, start, childrenCount - end); |
| 5337 | |
| 5338 | for (int i = childrenCount - (end - start); i < childrenCount; i++) { |
| 5339 | children[i] = null; |
| 5340 | } |
| 5341 | } |
| 5342 | |
| 5343 | mChildrenCount -= (end - start); |
| 5344 | } |
| 5345 | |
| 5346 | private void bindLayoutAnimation(View child) { |
| 5347 | Animation a = mLayoutAnimationController.getAnimationForView(child); |
| 5348 | child.setAnimation(a); |
| 5349 | } |
| 5350 | |
| 5351 | /** |
| 5352 | * Subclasses should override this method to set layout animation |
| 5353 | * parameters on the supplied child. |
| 5354 | * |
| 5355 | * @param child the child to associate with animation parameters |
| 5356 | * @param params the child's layout parameters which hold the animation |
| 5357 | * parameters |
| 5358 | * @param index the index of the child in the view group |
| 5359 | * @param count the number of children in the view group |
| 5360 | */ |
| 5361 | protected void attachLayoutAnimationParameters(View child, |
| 5362 | LayoutParams params, int index, int count) { |
| 5363 | LayoutAnimationController.AnimationParameters animationParams = |
| 5364 | params.layoutAnimationParameters; |
| 5365 | if (animationParams == null) { |
| 5366 | animationParams = new LayoutAnimationController.AnimationParameters(); |
| 5367 | params.layoutAnimationParameters = animationParams; |
| 5368 | } |
| 5369 | |
| 5370 | animationParams.count = count; |
| 5371 | animationParams.index = index; |
| 5372 | } |
| 5373 | |
| 5374 | /** |
| 5375 | * {@inheritDoc} |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 5376 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5377 | * <p><strong>Note:</strong> do not invoke this method from |
| 5378 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5379 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5380 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5381 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5382 | public void removeView(View view) { |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 5383 | if (removeViewInternal(view)) { |
| 5384 | requestLayout(); |
| 5385 | invalidate(true); |
| 5386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | /** |
| 5390 | * Removes a view during layout. This is useful if in your onLayout() method, |
| 5391 | * you need to remove more views. |
| 5392 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5393 | * <p><strong>Note:</strong> do not invoke this method from |
| 5394 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5395 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 5396 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5397 | * @param view the view to remove from the group |
| 5398 | */ |
| 5399 | public void removeViewInLayout(View view) { |
| 5400 | removeViewInternal(view); |
| 5401 | } |
| 5402 | |
| 5403 | /** |
| 5404 | * Removes a range of views during layout. This is useful if in your onLayout() method, |
| 5405 | * you need to remove more views. |
| 5406 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5407 | * <p><strong>Note:</strong> do not invoke this method from |
| 5408 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5409 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 5410 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5411 | * @param start the index of the first view to remove from the group |
| 5412 | * @param count the number of views to remove from the group |
| 5413 | */ |
| 5414 | public void removeViewsInLayout(int start, int count) { |
| 5415 | removeViewsInternal(start, count); |
| 5416 | } |
| 5417 | |
| 5418 | /** |
| 5419 | * Removes the view at the specified position in the group. |
| 5420 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5421 | * <p><strong>Note:</strong> do not invoke this method from |
| 5422 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5423 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 5424 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5425 | * @param index the position in the group of the view to remove |
| 5426 | */ |
| 5427 | public void removeViewAt(int index) { |
| 5428 | removeViewInternal(index, getChildAt(index)); |
| 5429 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 5430 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | /** |
| 5434 | * Removes the specified range of views from the group. |
| 5435 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5436 | * <p><strong>Note:</strong> do not invoke this method from |
| 5437 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5438 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 5439 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5440 | * @param start the first position in the group of the range of views to remove |
| 5441 | * @param count the number of views to remove |
| 5442 | */ |
| 5443 | public void removeViews(int start, int count) { |
| 5444 | removeViewsInternal(start, count); |
| 5445 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 5446 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5447 | } |
| 5448 | |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 5449 | private boolean removeViewInternal(View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5450 | final int index = indexOfChild(view); |
| 5451 | if (index >= 0) { |
| 5452 | removeViewInternal(index, view); |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 5453 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5454 | } |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 5455 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | private void removeViewInternal(int index, View view) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5459 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 5460 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5461 | } |
| 5462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5463 | boolean clearChildFocus = false; |
| 5464 | if (view == mFocused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 5465 | view.unFocus(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5466 | clearChildFocus = true; |
| 5467 | } |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5468 | if (view == mFocusedInCluster) { |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 5469 | clearFocusedInCluster(view); |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 5470 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5471 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 5472 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 5473 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 5474 | cancelTouchTarget(view); |
| 5475 | cancelHoverTarget(view); |
| 5476 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5477 | if (view.getAnimation() != null || |
| 5478 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5479 | addDisappearingView(view); |
| 5480 | } else if (view.mAttachInfo != null) { |
| 5481 | view.dispatchDetachedFromWindow(); |
| 5482 | } |
| 5483 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 5484 | if (view.hasTransientState()) { |
| 5485 | childHasTransientStateChanged(view, false); |
| 5486 | } |
| 5487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5488 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5489 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5490 | removeFromArray(index); |
| 5491 | |
Evan Rosky | 4807ae2 | 2018-03-22 16:04:15 -0700 | [diff] [blame] | 5492 | if (view.hasUnhandledKeyListener()) { |
| 5493 | decrementChildUnhandledKeyListeners(); |
| 5494 | } |
| 5495 | |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5496 | if (view == mDefaultFocus) { |
| 5497 | clearDefaultFocus(view); |
| 5498 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5499 | if (clearChildFocus) { |
| 5500 | clearChildFocus(view); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5501 | if (!rootViewRequestFocus()) { |
| 5502 | notifyGlobalFocusCleared(this); |
| 5503 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 5504 | } |
Romain Guy | 6fb0563 | 2012-11-29 10:50:33 -0800 | [diff] [blame] | 5505 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5506 | dispatchViewRemoved(view); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5507 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 5508 | if (view.getVisibility() != View.GONE) { |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 5509 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5510 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 5511 | |
| 5512 | int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 5513 | for (int i = 0; i < transientCount; ++i) { |
| 5514 | final int oldIndex = mTransientIndices.get(i); |
| 5515 | if (index < oldIndex) { |
| 5516 | mTransientIndices.set(i, oldIndex - 1); |
| 5517 | } |
| 5518 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 5519 | |
| 5520 | if (mCurrentDragStartEvent != null) { |
| 5521 | mChildrenInterestedInDrag.remove(view); |
| 5522 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5523 | } |
| 5524 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5525 | /** |
| 5526 | * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 5527 | * not null, changes in layout which occur because of children being added to or removed from |
| 5528 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 5529 | * object. By default, the transition object is null (so layout changes are not animated). |
| 5530 | * |
Chet Haase | ef3cbfd | 2013-08-21 14:01:02 -0700 | [diff] [blame] | 5531 | * <p>Replacing a non-null transition will cause that previous transition to be |
| 5532 | * canceled, if it is currently running, to restore this container to |
| 5533 | * its correct post-transition state.</p> |
| 5534 | * |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5535 | * @param transition The LayoutTransition object that will animated changes in layout. A value |
| 5536 | * of <code>null</code> means no transition will run on layout changes. |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 5537 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5538 | */ |
| 5539 | public void setLayoutTransition(LayoutTransition transition) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5540 | if (mTransition != null) { |
Chet Haase | fee6f2b | 2013-08-27 12:22:29 -0700 | [diff] [blame] | 5541 | LayoutTransition previousTransition = mTransition; |
| 5542 | previousTransition.cancel(); |
| 5543 | previousTransition.removeTransitionListener(mLayoutTransitionListener); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5544 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5545 | mTransition = transition; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 5546 | if (mTransition != null) { |
| 5547 | mTransition.addTransitionListener(mLayoutTransitionListener); |
| 5548 | } |
| 5549 | } |
| 5550 | |
| 5551 | /** |
| 5552 | * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 5553 | * not null, changes in layout which occur because of children being added to or removed from |
| 5554 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 5555 | * object. By default, the transition object is null (so layout changes are not animated). |
| 5556 | * |
| 5557 | * @return LayoutTranstion The LayoutTransition object that will animated changes in layout. |
| 5558 | * A value of <code>null</code> means no transition will run on layout changes. |
| 5559 | */ |
| 5560 | public LayoutTransition getLayoutTransition() { |
| 5561 | return mTransition; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5562 | } |
| 5563 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5564 | private void removeViewsInternal(int start, int count) { |
Chris Craik | 18a759d | 2015-10-14 14:16:33 -0700 | [diff] [blame] | 5565 | final int end = start + count; |
| 5566 | |
| 5567 | if (start < 0 || count < 0 || end > mChildrenCount) { |
| 5568 | throw new IndexOutOfBoundsException(); |
| 5569 | } |
| 5570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5571 | final View focused = mFocused; |
| 5572 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5573 | boolean clearChildFocus = false; |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5574 | View clearDefaultFocus = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5575 | |
| 5576 | final View[] children = mChildren; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5577 | |
| 5578 | for (int i = start; i < end; i++) { |
| 5579 | final View view = children[i]; |
| 5580 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5581 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 5582 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5583 | } |
| 5584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5585 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 5586 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5587 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5588 | } |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 5589 | if (view == mDefaultFocus) { |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5590 | clearDefaultFocus = view; |
| 5591 | } |
| 5592 | if (view == mFocusedInCluster) { |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 5593 | clearFocusedInCluster(view); |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 5594 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5595 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 5596 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 5597 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 5598 | cancelTouchTarget(view); |
| 5599 | cancelHoverTarget(view); |
| 5600 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5601 | if (view.getAnimation() != null || |
| 5602 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5603 | addDisappearingView(view); |
| 5604 | } else if (detach) { |
| 5605 | view.dispatchDetachedFromWindow(); |
| 5606 | } |
| 5607 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 5608 | if (view.hasTransientState()) { |
| 5609 | childHasTransientStateChanged(view, false); |
| 5610 | } |
| 5611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5612 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5613 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5614 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | removeFromArray(start, count); |
| 5618 | |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5619 | if (clearDefaultFocus != null) { |
| 5620 | clearDefaultFocus(clearDefaultFocus); |
| 5621 | } |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5622 | if (clearChildFocus) { |
| 5623 | clearChildFocus(focused); |
| 5624 | if (!rootViewRequestFocus()) { |
| 5625 | notifyGlobalFocusCleared(focused); |
| 5626 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | /** |
| 5631 | * Call this method to remove all child views from the |
| 5632 | * ViewGroup. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 5633 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5634 | * <p><strong>Note:</strong> do not invoke this method from |
| 5635 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5636 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5637 | */ |
| 5638 | public void removeAllViews() { |
| 5639 | removeAllViewsInLayout(); |
| 5640 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 5641 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | /** |
| 5645 | * Called by a ViewGroup subclass to remove child views from itself, |
| 5646 | * when it must first know its size on screen before it can calculate how many |
| 5647 | * child views it will render. An example is a Gallery or a ListView, which |
| 5648 | * may "have" 50 children, but actually only render the number of children |
| 5649 | * that can currently fit inside the object on screen. Do not call |
| 5650 | * this method unless you are extending ViewGroup and understand the |
| 5651 | * view measuring and layout pipeline. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 5652 | * |
| 5653 | * <p><strong>Note:</strong> do not invoke this method from |
| 5654 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 5655 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5656 | */ |
| 5657 | public void removeAllViewsInLayout() { |
| 5658 | final int count = mChildrenCount; |
| 5659 | if (count <= 0) { |
| 5660 | return; |
| 5661 | } |
| 5662 | |
| 5663 | final View[] children = mChildren; |
| 5664 | mChildrenCount = 0; |
| 5665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5666 | final View focused = mFocused; |
| 5667 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5668 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5669 | |
| 5670 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5672 | for (int i = count - 1; i >= 0; i--) { |
| 5673 | final View view = children[i]; |
| 5674 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5675 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 5676 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5677 | } |
| 5678 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5679 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 5680 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5681 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5682 | } |
| 5683 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 5684 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 5685 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 5686 | cancelTouchTarget(view); |
| 5687 | cancelHoverTarget(view); |
| 5688 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5689 | if (view.getAnimation() != null || |
| 5690 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5691 | addDisappearingView(view); |
| 5692 | } else if (detach) { |
| 5693 | view.dispatchDetachedFromWindow(); |
| 5694 | } |
| 5695 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 5696 | if (view.hasTransientState()) { |
| 5697 | childHasTransientStateChanged(view, false); |
| 5698 | } |
| 5699 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5700 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5701 | |
| 5702 | view.mParent = null; |
| 5703 | children[i] = null; |
| 5704 | } |
| 5705 | |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5706 | if (mDefaultFocus != null) { |
| 5707 | clearDefaultFocus(mDefaultFocus); |
| 5708 | } |
Evan Rosky | 776fa5f | 2017-04-26 16:47:54 -0700 | [diff] [blame] | 5709 | if (mFocusedInCluster != null) { |
| 5710 | clearFocusedInCluster(mFocusedInCluster); |
| 5711 | } |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 5712 | if (clearChildFocus) { |
| 5713 | clearChildFocus(focused); |
| 5714 | if (!rootViewRequestFocus()) { |
| 5715 | notifyGlobalFocusCleared(focused); |
| 5716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5717 | } |
| 5718 | } |
| 5719 | |
| 5720 | /** |
| 5721 | * Finishes the removal of a detached view. This method will dispatch the detached from |
| 5722 | * window event and notify the hierarchy change listener. |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5723 | * <p> |
| 5724 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 5725 | * parent or child should be redrawn. Proper use of this method will include also making |
| 5726 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 5727 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 5728 | * which performs a {@link #requestLayout()} on the next frame, after all detach/remove |
| 5729 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5730 | * |
| 5731 | * @param child the child to be definitely removed from the view hierarchy |
| 5732 | * @param animate if true and the view has an animation, the view is placed in the |
| 5733 | * disappearing views list, otherwise, it is detached from the window |
| 5734 | * |
| 5735 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5736 | * @see #detachAllViewsFromParent() |
| 5737 | * @see #detachViewFromParent(View) |
| 5738 | * @see #detachViewFromParent(int) |
| 5739 | */ |
| 5740 | protected void removeDetachedView(View child, boolean animate) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5741 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 5742 | mTransition.removeChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5743 | } |
| 5744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5745 | if (child == mFocused) { |
| 5746 | child.clearFocus(); |
| 5747 | } |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 5748 | if (child == mDefaultFocus) { |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 5749 | clearDefaultFocus(child); |
| 5750 | } |
| 5751 | if (child == mFocusedInCluster) { |
Evan Rosky | 0e8a683 | 2017-04-10 12:35:15 -0700 | [diff] [blame] | 5752 | clearFocusedInCluster(child); |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 5753 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5754 | |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 5755 | child.clearAccessibilityFocus(); |
| 5756 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 5757 | cancelTouchTarget(child); |
| 5758 | cancelHoverTarget(child); |
| 5759 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5760 | if ((animate && child.getAnimation() != null) || |
| 5761 | (mTransitioningViews != null && mTransitioningViews.contains(child))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5762 | addDisappearingView(child); |
| 5763 | } else if (child.mAttachInfo != null) { |
| 5764 | child.dispatchDetachedFromWindow(); |
| 5765 | } |
| 5766 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 5767 | if (child.hasTransientState()) { |
| 5768 | childHasTransientStateChanged(child, false); |
| 5769 | } |
| 5770 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 5771 | dispatchViewRemoved(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | /** |
| 5775 | * Attaches a view to this view group. Attaching a view assigns this group as the parent, |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5776 | * sets the layout parameters and puts the view in the list of children so that |
| 5777 | * it can be retrieved by calling {@link #getChildAt(int)}. |
| 5778 | * <p> |
| 5779 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 5780 | * parent or child should be redrawn. Proper use of this method will include also making |
| 5781 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 5782 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 5783 | * which performs a {@link #requestLayout()} on the next frame, after all detach/attach |
| 5784 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
| 5785 | * <p> |
| 5786 | * This method should be called only for views which were detached from their parent. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5787 | * |
| 5788 | * @param child the child to attach |
| 5789 | * @param index the index at which the child should be attached |
| 5790 | * @param params the layout parameters of the child |
| 5791 | * |
| 5792 | * @see #removeDetachedView(View, boolean) |
| 5793 | * @see #detachAllViewsFromParent() |
| 5794 | * @see #detachViewFromParent(View) |
| 5795 | * @see #detachViewFromParent(int) |
| 5796 | */ |
| 5797 | protected void attachViewToParent(View child, int index, LayoutParams params) { |
| 5798 | child.mLayoutParams = params; |
| 5799 | |
| 5800 | if (index < 0) { |
| 5801 | index = mChildrenCount; |
| 5802 | } |
| 5803 | |
| 5804 | addInArray(child, index); |
| 5805 | |
| 5806 | child.mParent = this; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5807 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK |
| 5808 | & ~PFLAG_DRAWING_CACHE_VALID) |
| 5809 | | PFLAG_DRAWN | PFLAG_INVALIDATED; |
| 5810 | this.mPrivateFlags |= PFLAG_INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5811 | |
| 5812 | if (child.hasFocus()) { |
| 5813 | requestChildFocus(child, child.findFocus()); |
| 5814 | } |
Adam Powell | c587409 | 2016-03-17 16:27:24 -0700 | [diff] [blame] | 5815 | dispatchVisibilityAggregated(isAttachedToWindow() && getWindowVisibility() == VISIBLE |
| 5816 | && isShown()); |
Phil Weaver | 1317162 | 2018-04-03 11:39:20 -0700 | [diff] [blame] | 5817 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5818 | } |
| 5819 | |
| 5820 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5821 | * Detaches a view from its parent. Detaching a view should be followed |
| 5822 | * either by a call to |
| 5823 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5824 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5825 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5826 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5827 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5828 | * |
| 5829 | * @param child the child to detach |
| 5830 | * |
| 5831 | * @see #detachViewFromParent(int) |
| 5832 | * @see #detachViewsFromParent(int, int) |
| 5833 | * @see #detachAllViewsFromParent() |
| 5834 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5835 | * @see #removeDetachedView(View, boolean) |
| 5836 | */ |
| 5837 | protected void detachViewFromParent(View child) { |
| 5838 | removeFromArray(indexOfChild(child)); |
| 5839 | } |
| 5840 | |
| 5841 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5842 | * Detaches a view from its parent. Detaching a view should be followed |
| 5843 | * either by a call to |
| 5844 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5845 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5846 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5847 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5848 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5849 | * |
| 5850 | * @param index the index of the child to detach |
| 5851 | * |
| 5852 | * @see #detachViewFromParent(View) |
| 5853 | * @see #detachAllViewsFromParent() |
| 5854 | * @see #detachViewsFromParent(int, int) |
| 5855 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5856 | * @see #removeDetachedView(View, boolean) |
| 5857 | */ |
| 5858 | protected void detachViewFromParent(int index) { |
| 5859 | removeFromArray(index); |
| 5860 | } |
| 5861 | |
| 5862 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5863 | * Detaches a range of views from their parents. Detaching a view should be followed |
| 5864 | * either by a call to |
| 5865 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5866 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5867 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5868 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5869 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5870 | * |
| 5871 | * @param start the first index of the childrend range to detach |
| 5872 | * @param count the number of children to detach |
| 5873 | * |
| 5874 | * @see #detachViewFromParent(View) |
| 5875 | * @see #detachViewFromParent(int) |
| 5876 | * @see #detachAllViewsFromParent() |
| 5877 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5878 | * @see #removeDetachedView(View, boolean) |
| 5879 | */ |
| 5880 | protected void detachViewsFromParent(int start, int count) { |
| 5881 | removeFromArray(start, count); |
| 5882 | } |
| 5883 | |
| 5884 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5885 | * Detaches all views from the parent. Detaching a view should be followed |
| 5886 | * either by a call to |
| 5887 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5888 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5889 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5890 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5891 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5892 | * |
| 5893 | * @see #detachViewFromParent(View) |
| 5894 | * @see #detachViewFromParent(int) |
| 5895 | * @see #detachViewsFromParent(int, int) |
| 5896 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5897 | * @see #removeDetachedView(View, boolean) |
| 5898 | */ |
| 5899 | protected void detachAllViewsFromParent() { |
| 5900 | final int count = mChildrenCount; |
| 5901 | if (count <= 0) { |
| 5902 | return; |
| 5903 | } |
| 5904 | |
| 5905 | final View[] children = mChildren; |
| 5906 | mChildrenCount = 0; |
| 5907 | |
| 5908 | for (int i = count - 1; i >= 0; i--) { |
| 5909 | children[i].mParent = null; |
| 5910 | children[i] = null; |
| 5911 | } |
| 5912 | } |
| 5913 | |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 5914 | @Override |
| 5915 | @CallSuper |
| 5916 | public void onDescendantInvalidated(@NonNull View child, @NonNull View target) { |
| 5917 | /* |
| 5918 | * HW-only, Rect-ignoring damage codepath |
| 5919 | * |
| 5920 | * We don't deal with rectangles here, since RenderThread native code computes damage for |
| 5921 | * everything drawn by HWUI (and SW layer / drawing cache doesn't keep track of damage area) |
| 5922 | */ |
| 5923 | |
| 5924 | // if set, combine the animation flag into the parent |
| 5925 | mPrivateFlags |= (target.mPrivateFlags & PFLAG_DRAW_ANIMATION); |
| 5926 | |
| 5927 | if ((target.mPrivateFlags & ~PFLAG_DIRTY_MASK) != 0) { |
| 5928 | // We lazily use PFLAG_DIRTY, since computing opaque isn't worth the potential |
| 5929 | // optimization in provides in a DisplayList world. |
| 5930 | mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DIRTY; |
| 5931 | |
| 5932 | // simplified invalidateChildInParent behavior: clear cache validity to be safe... |
| 5933 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 5936 | // ... and mark inval if in software layer that needs to repaint (hw handled in native) |
| 5937 | if (mLayerType == LAYER_TYPE_SOFTWARE) { |
| 5938 | // Layered parents should be invalidated. Escalate to a full invalidate (and note that |
| 5939 | // we do this after consuming any relevant flags from the originating descendant) |
| 5940 | mPrivateFlags |= PFLAG_INVALIDATED | PFLAG_DIRTY; |
| 5941 | target = this; |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 5942 | } |
| 5943 | |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 5944 | if (mParent != null) { |
| 5945 | mParent.onDescendantInvalidated(this, target); |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 5946 | } |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 5947 | } |
| 5948 | |
| 5949 | |
| 5950 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5951 | * Don't call or override this method. It is used for the implementation of |
| 5952 | * the view hierarchy. |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 5953 | * |
| 5954 | * @deprecated Use {@link #onDescendantInvalidated(View, View)} instead to observe updates to |
| 5955 | * draw state in descendants. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5956 | */ |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 5957 | @Deprecated |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5958 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5959 | public final void invalidateChild(View child, final Rect dirty) { |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 5960 | final AttachInfo attachInfo = mAttachInfo; |
| 5961 | if (attachInfo != null && attachInfo.mHardwareAccelerated) { |
| 5962 | // HW accelerated fast path |
| 5963 | onDescendantInvalidated(child, child); |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 5964 | return; |
| 5965 | } |
| 5966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5967 | ViewParent parent = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5968 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5969 | // If the child is drawing an animation, we want to copy this flag onto |
| 5970 | // ourselves and the parent to make sure the invalidate request goes |
| 5971 | // through |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 5972 | final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) != 0; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 5973 | |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5974 | // Check whether the child that requests the invalidate is fully opaque |
| 5975 | // Views being animated or transformed are not considered opaque because we may |
| 5976 | // be invalidating their old position and need the parent to paint behind them. |
| 5977 | Matrix childMatrix = child.getMatrix(); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5978 | // Mark the child as dirty, using the appropriate flag |
| 5979 | // Make sure we do not set both flags at the same time |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5980 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5981 | if (child.mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5982 | mPrivateFlags |= PFLAG_INVALIDATED; |
| 5983 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5987 | location[CHILD_LEFT_INDEX] = child.mLeft; |
| 5988 | location[CHILD_TOP_INDEX] = child.mTop; |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5989 | if (!childMatrix.isIdentity() || |
| 5990 | (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5991 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5992 | boundingRect.set(dirty); |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5993 | Matrix transformMatrix; |
| 5994 | if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
| 5995 | Transformation t = attachInfo.mTmpTransformation; |
| 5996 | boolean transformed = getChildStaticTransformation(child, t); |
| 5997 | if (transformed) { |
| 5998 | transformMatrix = attachInfo.mTmpMatrix; |
| 5999 | transformMatrix.set(t.getMatrix()); |
| 6000 | if (!childMatrix.isIdentity()) { |
| 6001 | transformMatrix.preConcat(childMatrix); |
| 6002 | } |
| 6003 | } else { |
| 6004 | transformMatrix = childMatrix; |
| 6005 | } |
| 6006 | } else { |
| 6007 | transformMatrix = childMatrix; |
| 6008 | } |
| 6009 | transformMatrix.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 6010 | dirty.set((int) Math.floor(boundingRect.left), |
| 6011 | (int) Math.floor(boundingRect.top), |
| 6012 | (int) Math.ceil(boundingRect.right), |
| 6013 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 6014 | } |
| 6015 | |
| 6016 | do { |
| 6017 | View view = null; |
| 6018 | if (parent instanceof View) { |
| 6019 | view = (View) parent; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | if (drawAnimation) { |
| 6023 | if (view != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6024 | view.mPrivateFlags |= PFLAG_DRAW_ANIMATION; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 6025 | } else if (parent instanceof ViewRootImpl) { |
| 6026 | ((ViewRootImpl) parent).mIsAnimating = true; |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | // If the parent is dirty opaque or not dirty, mark it dirty with the opaque |
| 6031 | // flag coming from the child that initiated the invalidate |
| 6032 | if (view != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6033 | if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) { |
Nader Jawad | 70e7299 | 2018-05-08 10:54:33 -0700 | [diff] [blame] | 6034 | view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 6035 | } |
| 6036 | } |
| 6037 | |
| 6038 | parent = parent.invalidateChildInParent(location, dirty); |
| 6039 | if (view != null) { |
| 6040 | // Account for transform on current parent |
| 6041 | Matrix m = view.getMatrix(); |
| 6042 | if (!m.isIdentity()) { |
| 6043 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 6044 | boundingRect.set(dirty); |
| 6045 | m.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 6046 | dirty.set((int) Math.floor(boundingRect.left), |
| 6047 | (int) Math.floor(boundingRect.top), |
| 6048 | (int) Math.ceil(boundingRect.right), |
| 6049 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 6050 | } |
| 6051 | } |
| 6052 | } while (parent != null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6053 | } |
| 6054 | } |
| 6055 | |
| 6056 | /** |
| 6057 | * Don't call or override this method. It is used for the implementation of |
| 6058 | * the view hierarchy. |
| 6059 | * |
| 6060 | * This implementation returns null if this ViewGroup does not have a parent, |
| 6061 | * if this ViewGroup is already fully invalidated or if the dirty rectangle |
| 6062 | * does not intersect with this ViewGroup's bounds. |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 6063 | * |
| 6064 | * @deprecated Use {@link #onDescendantInvalidated(View, View)} instead to observe updates to |
| 6065 | * draw state in descendants. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6066 | */ |
Chris Craik | 9de95db | 2017-01-18 17:59:23 -0800 | [diff] [blame] | 6067 | @Deprecated |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 6068 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6069 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 6070 | if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID)) != 0) { |
| 6071 | // either DRAWN, or DRAWING_CACHE_VALID |
| 6072 | if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) |
| 6073 | != FLAG_OPTIMIZE_INVALIDATE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6074 | dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, |
| 6075 | location[CHILD_TOP_INDEX] - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 6076 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 6077 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 6078 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6079 | |
| 6080 | final int left = mLeft; |
| 6081 | final int top = mTop; |
| 6082 | |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 6083 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 6084 | if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) { |
| 6085 | dirty.setEmpty(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 6086 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6087 | } |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 6088 | |
| 6089 | location[CHILD_LEFT_INDEX] = left; |
| 6090 | location[CHILD_TOP_INDEX] = top; |
Selim Cinek | 1cb8b08 | 2016-12-21 15:34:30 +0000 | [diff] [blame] | 6091 | } else { |
Selim Cinek | 1cb8b08 | 2016-12-21 15:34:30 +0000 | [diff] [blame] | 6092 | |
Chet Haase | a3db866 | 2011-07-19 10:36:05 -0700 | [diff] [blame] | 6093 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 6094 | dirty.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 6095 | } else { |
| 6096 | // in case the dirty rect extends outside the bounds of this container |
| 6097 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 6098 | } |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 6099 | location[CHILD_LEFT_INDEX] = mLeft; |
| 6100 | location[CHILD_TOP_INDEX] = mTop; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 6101 | |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 6102 | mPrivateFlags &= ~PFLAG_DRAWN; |
Selim Cinek | 1cb8b08 | 2016-12-21 15:34:30 +0000 | [diff] [blame] | 6103 | } |
Chris Craik | 3f06c6d | 2017-01-09 18:19:48 +0000 | [diff] [blame] | 6104 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
| 6105 | if (mLayerType != LAYER_TYPE_NONE) { |
| 6106 | mPrivateFlags |= PFLAG_INVALIDATED; |
| 6107 | } |
| 6108 | |
| 6109 | return mParent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | return null; |
| 6113 | } |
| 6114 | |
| 6115 | /** |
| 6116 | * Offset a rectangle that is in a descendant's coordinate |
| 6117 | * space into our coordinate space. |
| 6118 | * @param descendant A descendant of this view |
| 6119 | * @param rect A rectangle defined in descendant's coordinate space. |
| 6120 | */ |
| 6121 | public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) { |
| 6122 | offsetRectBetweenParentAndChild(descendant, rect, true, false); |
| 6123 | } |
| 6124 | |
| 6125 | /** |
| 6126 | * Offset a rectangle that is in our coordinate space into an ancestor's |
| 6127 | * coordinate space. |
| 6128 | * @param descendant A descendant of this view |
| 6129 | * @param rect A rectangle defined in descendant's coordinate space. |
| 6130 | */ |
| 6131 | public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) { |
| 6132 | offsetRectBetweenParentAndChild(descendant, rect, false, false); |
| 6133 | } |
| 6134 | |
| 6135 | /** |
| 6136 | * Helper method that offsets a rect either from parent to descendant or |
| 6137 | * descendant to parent. |
| 6138 | */ |
| 6139 | void offsetRectBetweenParentAndChild(View descendant, Rect rect, |
| 6140 | boolean offsetFromChildToParent, boolean clipToBounds) { |
| 6141 | |
| 6142 | // already in the same coord system :) |
| 6143 | if (descendant == this) { |
| 6144 | return; |
| 6145 | } |
| 6146 | |
| 6147 | ViewParent theParent = descendant.mParent; |
| 6148 | |
| 6149 | // search and offset up to the parent |
| 6150 | while ((theParent != null) |
| 6151 | && (theParent instanceof View) |
| 6152 | && (theParent != this)) { |
| 6153 | |
| 6154 | if (offsetFromChildToParent) { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 6155 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 6156 | descendant.mTop - descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6157 | if (clipToBounds) { |
| 6158 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 6159 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 6160 | p.mBottom - p.mTop); |
| 6161 | if (!intersected) { |
| 6162 | rect.setEmpty(); |
| 6163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6164 | } |
| 6165 | } else { |
| 6166 | if (clipToBounds) { |
| 6167 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 6168 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 6169 | p.mBottom - p.mTop); |
| 6170 | if (!intersected) { |
| 6171 | rect.setEmpty(); |
| 6172 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6173 | } |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 6174 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 6175 | descendant.mScrollY - descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | descendant = (View) theParent; |
| 6179 | theParent = descendant.mParent; |
| 6180 | } |
| 6181 | |
| 6182 | // now that we are up to this view, need to offset one more time |
| 6183 | // to get into our coordinate space |
| 6184 | if (theParent == this) { |
| 6185 | if (offsetFromChildToParent) { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 6186 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 6187 | descendant.mTop - descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6188 | } else { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 6189 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 6190 | descendant.mScrollY - descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6191 | } |
| 6192 | } else { |
| 6193 | throw new IllegalArgumentException("parameter must be a descendant of this view"); |
| 6194 | } |
| 6195 | } |
| 6196 | |
| 6197 | /** |
| 6198 | * Offset the vertical location of all children of this view by the specified number of pixels. |
| 6199 | * |
| 6200 | * @param offset the number of pixels to offset |
| 6201 | * |
| 6202 | * @hide |
| 6203 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 6204 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6205 | public void offsetChildrenTopAndBottom(int offset) { |
| 6206 | final int count = mChildrenCount; |
| 6207 | final View[] children = mChildren; |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 6208 | boolean invalidate = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6209 | |
| 6210 | for (int i = 0; i < count; i++) { |
| 6211 | final View v = children[i]; |
| 6212 | v.mTop += offset; |
| 6213 | v.mBottom += offset; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 6214 | if (v.mRenderNode != null) { |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 6215 | invalidate = true; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 6216 | v.mRenderNode.offsetTopAndBottom(offset); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 6217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6218 | } |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 6219 | |
| 6220 | if (invalidate) { |
| 6221 | invalidateViewProperty(false, false); |
| 6222 | } |
Eugene Susla | 72c510f | 2018-01-23 21:12:11 +0000 | [diff] [blame] | 6223 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6224 | } |
| 6225 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 6226 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6227 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
Abodunrinwa Toki | 4e7a120 | 2016-05-03 18:23:12 +0100 | [diff] [blame] | 6228 | return getChildVisibleRect(child, r, offset, false); |
| 6229 | } |
| 6230 | |
| 6231 | /** |
Abodunrinwa Toki | cb66406 | 2016-05-13 19:26:03 +0100 | [diff] [blame] | 6232 | * @param forceParentCheck true to guarantee that this call will propagate to all ancestors, |
| 6233 | * false otherwise |
| 6234 | * |
Abodunrinwa Toki | 4e7a120 | 2016-05-03 18:23:12 +0100 | [diff] [blame] | 6235 | * @hide |
| 6236 | */ |
| 6237 | public boolean getChildVisibleRect( |
| 6238 | View child, Rect r, android.graphics.Point offset, boolean forceParentCheck) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 6239 | // It doesn't make a whole lot of sense to call this on a view that isn't attached, |
| 6240 | // but for some simple tests it can be useful. If we don't have attach info this |
| 6241 | // will allocate memory. |
| 6242 | final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF(); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6243 | rect.set(r); |
| 6244 | |
| 6245 | if (!child.hasIdentityMatrix()) { |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 6246 | child.getMatrix().mapRect(rect); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6247 | } |
| 6248 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 6249 | final int dx = child.mLeft - mScrollX; |
| 6250 | final int dy = child.mTop - mScrollY; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6251 | |
| 6252 | rect.offset(dx, dy); |
| 6253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6254 | if (offset != null) { |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6255 | if (!child.hasIdentityMatrix()) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 6256 | float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation |
| 6257 | : new float[2]; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6258 | position[0] = offset.x; |
| 6259 | position[1] = offset.y; |
| 6260 | child.getMatrix().mapPoints(position); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 6261 | offset.x = Math.round(position[0]); |
| 6262 | offset.y = Math.round(position[1]); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6263 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6264 | offset.x += dx; |
| 6265 | offset.y += dy; |
| 6266 | } |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6267 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 6268 | final int width = mRight - mLeft; |
| 6269 | final int height = mBottom - mTop; |
| 6270 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 6271 | boolean rectIsVisible = true; |
Adam Powell | 35da41e | 2014-12-10 18:59:34 -0800 | [diff] [blame] | 6272 | if (mParent == null || |
| 6273 | (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 6274 | // Clip to bounds. |
| 6275 | rectIsVisible = rect.intersect(0, 0, width, height); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 6276 | } |
| 6277 | |
Abodunrinwa Toki | 4e7a120 | 2016-05-03 18:23:12 +0100 | [diff] [blame] | 6278 | if ((forceParentCheck || rectIsVisible) |
| 6279 | && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 6280 | // Clip to padding. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 6281 | rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop, |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 6282 | width - mPaddingRight, height - mPaddingBottom); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 6283 | } |
| 6284 | |
Abodunrinwa Toki | 4e7a120 | 2016-05-03 18:23:12 +0100 | [diff] [blame] | 6285 | if ((forceParentCheck || rectIsVisible) && mClipBounds != null) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 6286 | // Clip to clipBounds. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 6287 | rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right, |
| 6288 | mClipBounds.bottom); |
| 6289 | } |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 6290 | r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top), |
| 6291 | (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom)); |
Abodunrinwa Toki | 4e7a120 | 2016-05-03 18:23:12 +0100 | [diff] [blame] | 6292 | |
| 6293 | if ((forceParentCheck || rectIsVisible) && mParent != null) { |
Abodunrinwa Toki | cb66406 | 2016-05-13 19:26:03 +0100 | [diff] [blame] | 6294 | if (mParent instanceof ViewGroup) { |
| 6295 | rectIsVisible = ((ViewGroup) mParent) |
| 6296 | .getChildVisibleRect(this, r, offset, forceParentCheck); |
| 6297 | } else { |
| 6298 | rectIsVisible = mParent.getChildVisibleRect(this, r, offset); |
| 6299 | } |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 6300 | } |
| 6301 | return rectIsVisible; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6302 | } |
| 6303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6304 | @Override |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6305 | public final void layout(int l, int t, int r, int b) { |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 6306 | if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { |
Chet Haase | 7dd4a53 | 2012-04-16 13:35:09 -0700 | [diff] [blame] | 6307 | if (mTransition != null) { |
| 6308 | mTransition.layoutChange(this); |
| 6309 | } |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6310 | super.layout(l, t, r, b); |
| 6311 | } else { |
| 6312 | // record the fact that we noop'd it; request layout when transition finishes |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6313 | mLayoutCalledWhileSuppressed = true; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6314 | } |
| 6315 | } |
| 6316 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6317 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6318 | protected abstract void onLayout(boolean changed, |
| 6319 | int l, int t, int r, int b); |
| 6320 | |
| 6321 | /** |
| 6322 | * Indicates whether the view group has the ability to animate its children |
| 6323 | * after the first layout. |
| 6324 | * |
| 6325 | * @return true if the children can be animated, false otherwise |
| 6326 | */ |
| 6327 | protected boolean canAnimate() { |
| 6328 | return mLayoutAnimationController != null; |
| 6329 | } |
| 6330 | |
| 6331 | /** |
| 6332 | * Runs the layout animation. Calling this method triggers a relayout of |
| 6333 | * this view group. |
| 6334 | */ |
| 6335 | public void startLayoutAnimation() { |
| 6336 | if (mLayoutAnimationController != null) { |
| 6337 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 6338 | requestLayout(); |
| 6339 | } |
| 6340 | } |
| 6341 | |
| 6342 | /** |
| 6343 | * Schedules the layout animation to be played after the next layout pass |
| 6344 | * of this view group. This can be used to restart the layout animation |
| 6345 | * when the content of the view group changes or when the activity is |
| 6346 | * paused and resumed. |
| 6347 | */ |
| 6348 | public void scheduleLayoutAnimation() { |
| 6349 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 6350 | } |
| 6351 | |
| 6352 | /** |
| 6353 | * Sets the layout animation controller used to animate the group's |
| 6354 | * children after the first layout. |
| 6355 | * |
| 6356 | * @param controller the animation controller |
| 6357 | */ |
| 6358 | public void setLayoutAnimation(LayoutAnimationController controller) { |
| 6359 | mLayoutAnimationController = controller; |
| 6360 | if (mLayoutAnimationController != null) { |
| 6361 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 6362 | } |
| 6363 | } |
| 6364 | |
| 6365 | /** |
| 6366 | * Returns the layout animation controller used to animate the group's |
| 6367 | * children. |
| 6368 | * |
| 6369 | * @return the current animation controller |
| 6370 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 6371 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6372 | public LayoutAnimationController getLayoutAnimation() { |
| 6373 | return mLayoutAnimationController; |
| 6374 | } |
| 6375 | |
| 6376 | /** |
| 6377 | * Indicates whether the children's drawing cache is used during a layout |
| 6378 | * animation. By default, the drawing cache is enabled but this will prevent |
| 6379 | * nested layout animations from working. To nest animations, you must disable |
| 6380 | * the cache. |
| 6381 | * |
| 6382 | * @return true if the animation cache is enabled, false otherwise |
| 6383 | * |
| 6384 | * @see #setAnimationCacheEnabled(boolean) |
| 6385 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6386 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 6387 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6388 | * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6389 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 6390 | @Deprecated |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 6391 | @InspectableProperty(name = "animationCache") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6392 | public boolean isAnimationCacheEnabled() { |
| 6393 | return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 6394 | } |
| 6395 | |
| 6396 | /** |
| 6397 | * Enables or disables the children's drawing cache during a layout animation. |
| 6398 | * By default, the drawing cache is enabled but this will prevent nested |
| 6399 | * layout animations from working. To nest animations, you must disable the |
| 6400 | * cache. |
| 6401 | * |
| 6402 | * @param enabled true to enable the animation cache, false otherwise |
| 6403 | * |
| 6404 | * @see #isAnimationCacheEnabled() |
| 6405 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6406 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 6407 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6408 | * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6409 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 6410 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6411 | public void setAnimationCacheEnabled(boolean enabled) { |
| 6412 | setBooleanFlag(FLAG_ANIMATION_CACHE, enabled); |
| 6413 | } |
| 6414 | |
| 6415 | /** |
| 6416 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 6417 | * drawing cache. By default this property is enabled. |
| 6418 | * |
| 6419 | * @return true if the animation cache is enabled, false otherwise |
| 6420 | * |
| 6421 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 6422 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 6423 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6424 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 6425 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6426 | * Child views may no longer have their caching behavior disabled by parents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6427 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 6428 | @Deprecated |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 6429 | @InspectableProperty(name = "alwaysDrawnWithCache") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6430 | public boolean isAlwaysDrawnWithCacheEnabled() { |
| 6431 | return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 6432 | } |
| 6433 | |
| 6434 | /** |
| 6435 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 6436 | * drawing cache. This property can be set to true when the cache rendering is |
| 6437 | * slightly different from the children's normal rendering. Renderings can be different, |
| 6438 | * for instance, when the cache's quality is set to low. |
| 6439 | * |
| 6440 | * When this property is disabled, the ViewGroup will use the drawing cache of its |
| 6441 | * children only when asked to. It's usually the task of subclasses to tell ViewGroup |
| 6442 | * when to start using the drawing cache and when to stop using it. |
| 6443 | * |
| 6444 | * @param always true to always draw with the drawing cache, false otherwise |
| 6445 | * |
| 6446 | * @see #isAlwaysDrawnWithCacheEnabled() |
| 6447 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 6448 | * @see View#setDrawingCacheEnabled(boolean) |
| 6449 | * @see View#setDrawingCacheQuality(int) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6450 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 6451 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6452 | * Child views may no longer have their caching behavior disabled by parents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6453 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 6454 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6455 | public void setAlwaysDrawnWithCacheEnabled(boolean always) { |
| 6456 | setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always); |
| 6457 | } |
| 6458 | |
| 6459 | /** |
| 6460 | * Indicates whether the ViewGroup is currently drawing its children using |
| 6461 | * their drawing cache. |
| 6462 | * |
| 6463 | * @return true if children should be drawn with their cache, false otherwise |
| 6464 | * |
| 6465 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 6466 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6467 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 6468 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6469 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 6470 | * Use {@link View#setLayerType(int, Paint)} on individual Views instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6471 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 6472 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6473 | protected boolean isChildrenDrawnWithCacheEnabled() { |
| 6474 | return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 6475 | } |
| 6476 | |
| 6477 | /** |
| 6478 | * Tells the ViewGroup to draw its children using their drawing cache. This property |
| 6479 | * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache |
| 6480 | * will be used only if it has been enabled. |
| 6481 | * |
| 6482 | * Subclasses should call this method to start and stop using the drawing cache when |
| 6483 | * they perform performance sensitive operations, like scrolling or animating. |
| 6484 | * |
| 6485 | * @param enabled true if children should be drawn with their cache, false otherwise |
| 6486 | * |
| 6487 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 6488 | * @see #isChildrenDrawnWithCacheEnabled() |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6489 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 6490 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 6491 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 6492 | * Use {@link View#setLayerType(int, Paint)} on individual Views instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6493 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 6494 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6495 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 6496 | setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled); |
| 6497 | } |
| 6498 | |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 6499 | /** |
| 6500 | * Indicates whether the ViewGroup is drawing its children in the order defined by |
| 6501 | * {@link #getChildDrawingOrder(int, int)}. |
| 6502 | * |
| 6503 | * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)}, |
| 6504 | * false otherwise |
| 6505 | * |
| 6506 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 6507 | * @see #getChildDrawingOrder(int, int) |
| 6508 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6509 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 6510 | protected boolean isChildrenDrawingOrderEnabled() { |
| 6511 | return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER; |
| 6512 | } |
| 6513 | |
| 6514 | /** |
| 6515 | * Tells the ViewGroup whether to draw its children in the order defined by the method |
| 6516 | * {@link #getChildDrawingOrder(int, int)}. |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 6517 | * <p> |
| 6518 | * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)}, |
| 6519 | * will override custom child ordering done via this method. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 6520 | * |
| 6521 | * @param enabled true if the order of the children when drawing is determined by |
| 6522 | * {@link #getChildDrawingOrder(int, int)}, false otherwise |
| 6523 | * |
| 6524 | * @see #isChildrenDrawingOrderEnabled() |
| 6525 | * @see #getChildDrawingOrder(int, int) |
| 6526 | */ |
| 6527 | protected void setChildrenDrawingOrderEnabled(boolean enabled) { |
| 6528 | setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled); |
| 6529 | } |
| 6530 | |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 6531 | private boolean hasBooleanFlag(int flag) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 6532 | return (mGroupFlags & flag) == flag; |
| 6533 | } |
| 6534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6535 | private void setBooleanFlag(int flag, boolean value) { |
| 6536 | if (value) { |
| 6537 | mGroupFlags |= flag; |
| 6538 | } else { |
| 6539 | mGroupFlags &= ~flag; |
| 6540 | } |
| 6541 | } |
| 6542 | |
| 6543 | /** |
| 6544 | * Returns an integer indicating what types of drawing caches are kept in memory. |
| 6545 | * |
| 6546 | * @see #setPersistentDrawingCache(int) |
| 6547 | * @see #setAnimationCacheEnabled(boolean) |
| 6548 | * |
| 6549 | * @return one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 6550 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 6551 | * and {@link #PERSISTENT_ALL_CACHES} |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 6552 | * |
| 6553 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 6554 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 6555 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 6556 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 6557 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 6558 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 6559 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 6560 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 6561 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 6562 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 6563 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 6564 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6565 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 6566 | @Deprecated |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6567 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6568 | @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"), |
Romain Guy | 203688c | 2010-05-12 15:41:32 -0700 | [diff] [blame] | 6569 | @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6570 | @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"), |
| 6571 | @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL") |
| 6572 | }) |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 6573 | @InspectableProperty(enumMapping = { |
| 6574 | @EnumMap(value = PERSISTENT_NO_CACHE, name = "none"), |
| 6575 | @EnumMap(value = PERSISTENT_ANIMATION_CACHE, name = "animation"), |
| 6576 | @EnumMap(value = PERSISTENT_SCROLLING_CACHE, name = "scrolling"), |
| 6577 | @EnumMap(value = PERSISTENT_ALL_CACHES, name = "all"), |
| 6578 | }) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6579 | public int getPersistentDrawingCache() { |
| 6580 | return mPersistentDrawingCache; |
| 6581 | } |
| 6582 | |
| 6583 | /** |
| 6584 | * Indicates what types of drawing caches should be kept in memory after |
| 6585 | * they have been created. |
| 6586 | * |
| 6587 | * @see #getPersistentDrawingCache() |
| 6588 | * @see #setAnimationCacheEnabled(boolean) |
| 6589 | * |
| 6590 | * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 6591 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 6592 | * and {@link #PERSISTENT_ALL_CACHES} |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 6593 | * |
| 6594 | * @deprecated The view drawing cache was largely made obsolete with the introduction of |
| 6595 | * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache |
| 6596 | * layers are largely unnecessary and can easily result in a net loss in performance due to the |
| 6597 | * cost of creating and updating the layer. In the rare cases where caching layers are useful, |
| 6598 | * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware |
| 6599 | * rendering. For software-rendered snapshots of a small part of the View hierarchy or |
| 6600 | * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or |
| 6601 | * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these |
| 6602 | * software-rendered usages are discouraged and have compatibility issues with hardware-only |
| 6603 | * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE} |
| 6604 | * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback |
| 6605 | * reports or unit testing the {@link PixelCopy} API is recommended. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6606 | */ |
John Reck | 949cfe1 | 2017-10-09 13:27:03 -0700 | [diff] [blame] | 6607 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6608 | public void setPersistentDrawingCache(int drawingCacheToKeep) { |
| 6609 | mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES; |
| 6610 | } |
| 6611 | |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 6612 | private void setLayoutMode(int layoutMode, boolean explicitly) { |
| 6613 | mLayoutMode = layoutMode; |
| 6614 | setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly); |
| 6615 | } |
| 6616 | |
| 6617 | /** |
| 6618 | * Recursively traverse the view hierarchy, resetting the layoutMode of any |
| 6619 | * descendants that had inherited a different layoutMode from a previous parent. |
| 6620 | * Recursion terminates when a descendant's mode is: |
| 6621 | * <ul> |
| 6622 | * <li>Undefined</li> |
| 6623 | * <li>The same as the root node's</li> |
| 6624 | * <li>A mode that had been explicitly set</li> |
| 6625 | * <ul/> |
| 6626 | * The first two clauses are optimizations. |
| 6627 | * @param layoutModeOfRoot |
| 6628 | */ |
| 6629 | @Override |
| 6630 | void invalidateInheritedLayoutMode(int layoutModeOfRoot) { |
| 6631 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED || |
| 6632 | mLayoutMode == layoutModeOfRoot || |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 6633 | hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 6634 | return; |
| 6635 | } |
| 6636 | setLayoutMode(LAYOUT_MODE_UNDEFINED, false); |
| 6637 | |
| 6638 | // apply recursively |
| 6639 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 6640 | getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot); |
| 6641 | } |
| 6642 | } |
| 6643 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6644 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 6645 | * Returns the basis of alignment during layout operations on this ViewGroup: |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 6646 | * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 6647 | * <p> |
| 6648 | * If no layoutMode was explicitly set, either programmatically or in an XML resource, |
| 6649 | * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists, |
| 6650 | * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6651 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 6652 | * @return the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6653 | * |
| 6654 | * @see #setLayoutMode(int) |
| 6655 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 6656 | @InspectableProperty(enumMapping = { |
| 6657 | @EnumMap(value = LAYOUT_MODE_CLIP_BOUNDS, name = "clipBounds"), |
| 6658 | @EnumMap(value = LAYOUT_MODE_OPTICAL_BOUNDS, name = "opticalBounds") |
| 6659 | }) |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6660 | public int getLayoutMode() { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 6661 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 6662 | int inheritedLayoutMode = (mParent instanceof ViewGroup) ? |
| 6663 | ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT; |
| 6664 | setLayoutMode(inheritedLayoutMode, false); |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 6665 | } |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6666 | return mLayoutMode; |
| 6667 | } |
| 6668 | |
| 6669 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 6670 | * Sets the basis of alignment during the layout of this ViewGroup. |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 6671 | * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or |
| 6672 | * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6673 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 6674 | * @param layoutMode the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6675 | * |
| 6676 | * @see #getLayoutMode() |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 6677 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6678 | */ |
| 6679 | public void setLayoutMode(int layoutMode) { |
| 6680 | if (mLayoutMode != layoutMode) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 6681 | invalidateInheritedLayoutMode(layoutMode); |
| 6682 | setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 6683 | requestLayout(); |
| 6684 | } |
| 6685 | } |
| 6686 | |
| 6687 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6688 | * Returns a new set of layout parameters based on the supplied attributes set. |
| 6689 | * |
| 6690 | * @param attrs the attributes to build the layout parameters from |
| 6691 | * |
| 6692 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 6693 | * of its descendants |
| 6694 | */ |
| 6695 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 6696 | return new LayoutParams(getContext(), attrs); |
| 6697 | } |
| 6698 | |
| 6699 | /** |
| 6700 | * Returns a safe set of layout parameters based on the supplied layout params. |
| 6701 | * When a ViewGroup is passed a View whose layout params do not pass the test of |
| 6702 | * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method |
| 6703 | * is invoked. This method should return a new set of layout params suitable for |
| 6704 | * this ViewGroup, possibly by copying the appropriate attributes from the |
| 6705 | * specified set of layout params. |
| 6706 | * |
| 6707 | * @param p The layout parameters to convert into a suitable set of layout parameters |
| 6708 | * for this ViewGroup. |
| 6709 | * |
| 6710 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 6711 | * of its descendants |
| 6712 | */ |
| 6713 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
Chet Haase | 4610eef | 2015-12-03 07:38:11 -0800 | [diff] [blame] | 6714 | return p; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | /** |
| 6718 | * Returns a set of default layout parameters. These parameters are requested |
| 6719 | * when the View passed to {@link #addView(View)} has no layout parameters |
| 6720 | * already set. If null is returned, an exception is thrown from addView. |
| 6721 | * |
| 6722 | * @return a set of default layout parameters or null |
| 6723 | */ |
| 6724 | protected LayoutParams generateDefaultLayoutParams() { |
| 6725 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 6726 | } |
| 6727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6728 | @Override |
| 6729 | protected void debug(int depth) { |
| 6730 | super.debug(depth); |
| 6731 | String output; |
| 6732 | |
| 6733 | if (mFocused != null) { |
| 6734 | output = debugIndent(depth); |
| 6735 | output += "mFocused"; |
| 6736 | Log.d(VIEW_LOG_TAG, output); |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 6737 | mFocused.debug(depth + 1); |
| 6738 | } |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 6739 | if (mDefaultFocus != null) { |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 6740 | output = debugIndent(depth); |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 6741 | output += "mDefaultFocus"; |
Vadim Tryshev | 01d8c49 | 2016-12-15 11:33:15 -0800 | [diff] [blame] | 6742 | Log.d(VIEW_LOG_TAG, output); |
Vadim Tryshev | 5ca7398 | 2017-01-04 17:24:43 -0800 | [diff] [blame] | 6743 | mDefaultFocus.debug(depth + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6744 | } |
Evan Rosky | 53fcf11 | 2017-01-26 14:37:55 -0800 | [diff] [blame] | 6745 | if (mFocusedInCluster != null) { |
| 6746 | output = debugIndent(depth); |
| 6747 | output += "mFocusedInCluster"; |
| 6748 | Log.d(VIEW_LOG_TAG, output); |
| 6749 | mFocusedInCluster.debug(depth + 1); |
| 6750 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6751 | if (mChildrenCount != 0) { |
| 6752 | output = debugIndent(depth); |
| 6753 | output += "{"; |
| 6754 | Log.d(VIEW_LOG_TAG, output); |
| 6755 | } |
| 6756 | int count = mChildrenCount; |
| 6757 | for (int i = 0; i < count; i++) { |
| 6758 | View child = mChildren[i]; |
| 6759 | child.debug(depth + 1); |
| 6760 | } |
| 6761 | |
| 6762 | if (mChildrenCount != 0) { |
| 6763 | output = debugIndent(depth); |
| 6764 | output += "}"; |
| 6765 | Log.d(VIEW_LOG_TAG, output); |
| 6766 | } |
| 6767 | } |
| 6768 | |
| 6769 | /** |
| 6770 | * Returns the position in the group of the specified child view. |
| 6771 | * |
| 6772 | * @param child the view for which to get the position |
| 6773 | * @return a positive integer representing the position of the view in the |
| 6774 | * group, or -1 if the view does not exist in the group |
| 6775 | */ |
| 6776 | public int indexOfChild(View child) { |
| 6777 | final int count = mChildrenCount; |
| 6778 | final View[] children = mChildren; |
| 6779 | for (int i = 0; i < count; i++) { |
| 6780 | if (children[i] == child) { |
| 6781 | return i; |
| 6782 | } |
| 6783 | } |
| 6784 | return -1; |
| 6785 | } |
| 6786 | |
| 6787 | /** |
| 6788 | * Returns the number of children in the group. |
| 6789 | * |
| 6790 | * @return a positive integer representing the number of children in |
| 6791 | * the group |
| 6792 | */ |
| 6793 | public int getChildCount() { |
| 6794 | return mChildrenCount; |
| 6795 | } |
| 6796 | |
| 6797 | /** |
| 6798 | * Returns the view at the specified position in the group. |
| 6799 | * |
| 6800 | * @param index the position at which to get the view from |
| 6801 | * @return the view at the specified position or null if the position |
| 6802 | * does not exist within the group |
| 6803 | */ |
| 6804 | public View getChildAt(int index) { |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 6805 | if (index < 0 || index >= mChildrenCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6806 | return null; |
| 6807 | } |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 6808 | return mChildren[index]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | /** |
| 6812 | * Ask all of the children of this view to measure themselves, taking into |
| 6813 | * account both the MeasureSpec requirements for this view and its padding. |
| 6814 | * We skip children that are in the GONE state The heavy lifting is done in |
| 6815 | * getChildMeasureSpec. |
| 6816 | * |
| 6817 | * @param widthMeasureSpec The width requirements for this view |
| 6818 | * @param heightMeasureSpec The height requirements for this view |
| 6819 | */ |
| 6820 | protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { |
| 6821 | final int size = mChildrenCount; |
| 6822 | final View[] children = mChildren; |
| 6823 | for (int i = 0; i < size; ++i) { |
| 6824 | final View child = children[i]; |
| 6825 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { |
| 6826 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 6827 | } |
| 6828 | } |
| 6829 | } |
| 6830 | |
| 6831 | /** |
| 6832 | * Ask one of the children of this view to measure itself, taking into |
| 6833 | * account both the MeasureSpec requirements for this view and its padding. |
| 6834 | * The heavy lifting is done in getChildMeasureSpec. |
| 6835 | * |
| 6836 | * @param child The child to measure |
| 6837 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6838 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6839 | */ |
| 6840 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 6841 | int parentHeightMeasureSpec) { |
| 6842 | final LayoutParams lp = child.getLayoutParams(); |
| 6843 | |
| 6844 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6845 | mPaddingLeft + mPaddingRight, lp.width); |
| 6846 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6847 | mPaddingTop + mPaddingBottom, lp.height); |
| 6848 | |
| 6849 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6850 | } |
| 6851 | |
| 6852 | /** |
| 6853 | * Ask one of the children of this view to measure itself, taking into |
| 6854 | * account both the MeasureSpec requirements for this view and its padding |
| 6855 | * and margins. The child must have MarginLayoutParams The heavy lifting is |
| 6856 | * done in getChildMeasureSpec. |
| 6857 | * |
| 6858 | * @param child The child to measure |
| 6859 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6860 | * @param widthUsed Extra space that has been used up by the parent |
| 6861 | * horizontally (possibly by other children of the parent) |
| 6862 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6863 | * @param heightUsed Extra space that has been used up by the parent |
| 6864 | * vertically (possibly by other children of the parent) |
| 6865 | */ |
| 6866 | protected void measureChildWithMargins(View child, |
| 6867 | int parentWidthMeasureSpec, int widthUsed, |
| 6868 | int parentHeightMeasureSpec, int heightUsed) { |
| 6869 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 6870 | |
| 6871 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6872 | mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin |
| 6873 | + widthUsed, lp.width); |
| 6874 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6875 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin |
| 6876 | + heightUsed, lp.height); |
| 6877 | |
| 6878 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6879 | } |
| 6880 | |
| 6881 | /** |
| 6882 | * Does the hard part of measureChildren: figuring out the MeasureSpec to |
| 6883 | * pass to a particular child. This method figures out the right MeasureSpec |
| 6884 | * for one dimension (height or width) of one child view. |
| 6885 | * |
| 6886 | * The goal is to combine information from our MeasureSpec with the |
| 6887 | * LayoutParams of the child to get the best possible results. For example, |
| 6888 | * if the this view knows its size (because its MeasureSpec has a mode of |
| 6889 | * EXACTLY), and the child has indicated in its LayoutParams that it wants |
| 6890 | * to be the same size as the parent, the parent should ask the child to |
| 6891 | * layout given an exact size. |
| 6892 | * |
| 6893 | * @param spec The requirements for this view |
| 6894 | * @param padding The padding of this view for the current dimension and |
| 6895 | * margins, if applicable |
| 6896 | * @param childDimension How big the child wants to be in the current |
| 6897 | * dimension |
| 6898 | * @return a MeasureSpec integer for the child |
| 6899 | */ |
| 6900 | public static int getChildMeasureSpec(int spec, int padding, int childDimension) { |
| 6901 | int specMode = MeasureSpec.getMode(spec); |
| 6902 | int specSize = MeasureSpec.getSize(spec); |
| 6903 | |
| 6904 | int size = Math.max(0, specSize - padding); |
| 6905 | |
| 6906 | int resultSize = 0; |
| 6907 | int resultMode = 0; |
| 6908 | |
| 6909 | switch (specMode) { |
| 6910 | // Parent has imposed an exact size on us |
| 6911 | case MeasureSpec.EXACTLY: |
| 6912 | if (childDimension >= 0) { |
| 6913 | resultSize = childDimension; |
| 6914 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6915 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6916 | // Child wants to be our size. So be it. |
| 6917 | resultSize = size; |
| 6918 | resultMode = MeasureSpec.EXACTLY; |
| 6919 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6920 | // Child wants to determine its own size. It can't be |
| 6921 | // bigger than us. |
| 6922 | resultSize = size; |
| 6923 | resultMode = MeasureSpec.AT_MOST; |
| 6924 | } |
| 6925 | break; |
| 6926 | |
| 6927 | // Parent has imposed a maximum size on us |
| 6928 | case MeasureSpec.AT_MOST: |
| 6929 | if (childDimension >= 0) { |
| 6930 | // Child wants a specific size... so be it |
| 6931 | resultSize = childDimension; |
| 6932 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6933 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6934 | // Child wants to be our size, but our size is not fixed. |
| 6935 | // Constrain child to not be bigger than us. |
| 6936 | resultSize = size; |
| 6937 | resultMode = MeasureSpec.AT_MOST; |
| 6938 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6939 | // Child wants to determine its own size. It can't be |
| 6940 | // bigger than us. |
| 6941 | resultSize = size; |
| 6942 | resultMode = MeasureSpec.AT_MOST; |
| 6943 | } |
| 6944 | break; |
| 6945 | |
| 6946 | // Parent asked to see how big we want to be |
| 6947 | case MeasureSpec.UNSPECIFIED: |
| 6948 | if (childDimension >= 0) { |
| 6949 | // Child wants a specific size... let him have it |
| 6950 | resultSize = childDimension; |
| 6951 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6952 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6953 | // Child wants to be our size... find out how big it should |
| 6954 | // be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6955 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6956 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6957 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6958 | // Child wants to determine its own size.... find out how |
| 6959 | // big it should be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6960 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6961 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6962 | } |
| 6963 | break; |
| 6964 | } |
Tor Norbye | 6756864 | 2015-03-31 07:47:23 -0700 | [diff] [blame] | 6965 | //noinspection ResourceType |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6966 | return MeasureSpec.makeMeasureSpec(resultSize, resultMode); |
| 6967 | } |
| 6968 | |
| 6969 | |
| 6970 | /** |
| 6971 | * Removes any pending animations for views that have been removed. Call |
| 6972 | * this if you don't want animations for exiting views to stack up. |
| 6973 | */ |
| 6974 | public void clearDisappearingChildren() { |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 6975 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6976 | if (disappearingChildren != null) { |
| 6977 | final int count = disappearingChildren.size(); |
| 6978 | for (int i = 0; i < count; i++) { |
| 6979 | final View view = disappearingChildren.get(i); |
| 6980 | if (view.mAttachInfo != null) { |
| 6981 | view.dispatchDetachedFromWindow(); |
| 6982 | } |
| 6983 | view.clearAnimation(); |
| 6984 | } |
| 6985 | disappearingChildren.clear(); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6986 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6987 | } |
| 6988 | } |
| 6989 | |
| 6990 | /** |
| 6991 | * Add a view which is removed from mChildren but still needs animation |
| 6992 | * |
| 6993 | * @param v View to add |
| 6994 | */ |
| 6995 | private void addDisappearingView(View v) { |
| 6996 | ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6997 | |
| 6998 | if (disappearingChildren == null) { |
| 6999 | disappearingChildren = mDisappearingChildren = new ArrayList<View>(); |
| 7000 | } |
| 7001 | |
| 7002 | disappearingChildren.add(v); |
| 7003 | } |
| 7004 | |
| 7005 | /** |
| 7006 | * Cleanup a view when its animation is done. This may mean removing it from |
| 7007 | * the list of disappearing views. |
| 7008 | * |
| 7009 | * @param view The view whose animation has finished |
| 7010 | * @param animation The animation, cannot be null |
| 7011 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 7012 | void finishAnimatingView(final View view, Animation animation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7013 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 7014 | if (disappearingChildren != null) { |
| 7015 | if (disappearingChildren.contains(view)) { |
| 7016 | disappearingChildren.remove(view); |
| 7017 | |
| 7018 | if (view.mAttachInfo != null) { |
| 7019 | view.dispatchDetachedFromWindow(); |
| 7020 | } |
| 7021 | |
| 7022 | view.clearAnimation(); |
| 7023 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 7024 | } |
| 7025 | } |
| 7026 | |
| 7027 | if (animation != null && !animation.getFillAfter()) { |
| 7028 | view.clearAnimation(); |
| 7029 | } |
| 7030 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 7031 | if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7032 | view.onAnimationEnd(); |
| 7033 | // Should be performed by onAnimationEnd() but this avoid an infinite loop, |
| 7034 | // so we'd rather be safe than sorry |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 7035 | view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7036 | // Draw one more frame after the animation is done |
| 7037 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 7038 | } |
| 7039 | } |
| 7040 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 7041 | /** |
Chet Haase | aceafe6 | 2011-08-26 15:44:33 -0700 | [diff] [blame] | 7042 | * Utility function called by View during invalidation to determine whether a view that |
| 7043 | * is invisible or gone should still be invalidated because it is being transitioned (and |
| 7044 | * therefore still needs to be drawn). |
| 7045 | */ |
| 7046 | boolean isViewTransitioning(View view) { |
| 7047 | return (mTransitioningViews != null && mTransitioningViews.contains(view)); |
| 7048 | } |
| 7049 | |
| 7050 | /** |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 7051 | * This method tells the ViewGroup that the given View object, which should have this |
| 7052 | * ViewGroup as its parent, |
| 7053 | * should be kept around (re-displayed when the ViewGroup draws its children) even if it |
| 7054 | * is removed from its parent. This allows animations, such as those used by |
| 7055 | * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate |
| 7056 | * the removal of views. A call to this method should always be accompanied by a later call |
| 7057 | * to {@link #endViewTransition(View)}, such as after an animation on the View has finished, |
| 7058 | * so that the View finally gets removed. |
| 7059 | * |
| 7060 | * @param view The View object to be kept visible even if it gets removed from its parent. |
| 7061 | */ |
| 7062 | public void startViewTransition(View view) { |
| 7063 | if (view.mParent == this) { |
| 7064 | if (mTransitioningViews == null) { |
| 7065 | mTransitioningViews = new ArrayList<View>(); |
| 7066 | } |
| 7067 | mTransitioningViews.add(view); |
| 7068 | } |
| 7069 | } |
| 7070 | |
| 7071 | /** |
| 7072 | * This method should always be called following an earlier call to |
| 7073 | * {@link #startViewTransition(View)}. The given View is finally removed from its parent |
| 7074 | * and will no longer be displayed. Note that this method does not perform the functionality |
| 7075 | * of removing a view from its parent; it just discontinues the display of a View that |
| 7076 | * has previously been removed. |
| 7077 | * |
| 7078 | * @return view The View object that has been removed but is being kept around in the visible |
| 7079 | * hierarchy by an earlier call to {@link #startViewTransition(View)}. |
| 7080 | */ |
| 7081 | public void endViewTransition(View view) { |
| 7082 | if (mTransitioningViews != null) { |
| 7083 | mTransitioningViews.remove(view); |
| 7084 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 7085 | if (disappearingChildren != null && disappearingChildren.contains(view)) { |
| 7086 | disappearingChildren.remove(view); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 7087 | if (mVisibilityChangingChildren != null && |
| 7088 | mVisibilityChangingChildren.contains(view)) { |
| 7089 | mVisibilityChangingChildren.remove(view); |
| 7090 | } else { |
| 7091 | if (view.mAttachInfo != null) { |
| 7092 | view.dispatchDetachedFromWindow(); |
| 7093 | } |
| 7094 | if (view.mParent != null) { |
| 7095 | view.mParent = null; |
| 7096 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 7097 | } |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 7098 | invalidate(); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 7099 | } |
| 7100 | } |
| 7101 | } |
| 7102 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7103 | private LayoutTransition.TransitionListener mLayoutTransitionListener = |
| 7104 | new LayoutTransition.TransitionListener() { |
| 7105 | @Override |
| 7106 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 7107 | View view, int transitionType) { |
| 7108 | // We only care about disappearing items, since we need special logic to keep |
| 7109 | // those items visible after they've been 'removed' |
| 7110 | if (transitionType == LayoutTransition.DISAPPEARING) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 7111 | startViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7112 | } |
| 7113 | } |
| 7114 | |
| 7115 | @Override |
| 7116 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 7117 | View view, int transitionType) { |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 7118 | if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) { |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 7119 | requestLayout(); |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 7120 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 7121 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7122 | if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 7123 | endViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7124 | } |
| 7125 | } |
| 7126 | }; |
| 7127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7128 | /** |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 7129 | * Tells this ViewGroup to suppress all layout() calls until layout |
| 7130 | * suppression is disabled with a later call to suppressLayout(false). |
| 7131 | * When layout suppression is disabled, a requestLayout() call is sent |
| 7132 | * if layout() was attempted while layout was being suppressed. |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 7133 | */ |
| 7134 | public void suppressLayout(boolean suppress) { |
| 7135 | mSuppressLayout = suppress; |
| 7136 | if (!suppress) { |
| 7137 | if (mLayoutCalledWhileSuppressed) { |
| 7138 | requestLayout(); |
| 7139 | mLayoutCalledWhileSuppressed = false; |
| 7140 | } |
| 7141 | } |
| 7142 | } |
| 7143 | |
| 7144 | /** |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 7145 | * Returns whether layout calls on this container are currently being |
| 7146 | * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}. |
| 7147 | * |
| 7148 | * @return true if layout calls are currently suppressed, false otherwise. |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 7149 | */ |
| 7150 | public boolean isLayoutSuppressed() { |
| 7151 | return mSuppressLayout; |
| 7152 | } |
| 7153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7154 | @Override |
| 7155 | public boolean gatherTransparentRegion(Region region) { |
| 7156 | // If no transparent regions requested, we are always opaque. |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 7157 | final boolean meOpaque = (mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7158 | if (meOpaque && region == null) { |
| 7159 | // The caller doesn't care about the region, so stop now. |
| 7160 | return true; |
| 7161 | } |
| 7162 | super.gatherTransparentRegion(region); |
Teng-Hui Zhu | f8da30d | 2016-08-15 10:28:20 -0700 | [diff] [blame] | 7163 | // Instead of naively traversing the view tree, we have to traverse according to the Z |
| 7164 | // order here. We need to go with the same order as dispatchDraw(). |
| 7165 | // One example is that after surfaceView punch a hole, we will still allow other views drawn |
| 7166 | // on top of that hole. In this case, those other views should be able to cut the |
| 7167 | // transparent region into smaller area. |
| 7168 | final int childrenCount = mChildrenCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7169 | boolean noneOfTheChildrenAreTransparent = true; |
Teng-Hui Zhu | f8da30d | 2016-08-15 10:28:20 -0700 | [diff] [blame] | 7170 | if (childrenCount > 0) { |
| 7171 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 7172 | final boolean customOrder = preorderedList == null |
| 7173 | && isChildrenDrawingOrderEnabled(); |
| 7174 | final View[] children = mChildren; |
| 7175 | for (int i = 0; i < childrenCount; i++) { |
| 7176 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 7177 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
| 7178 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 7179 | if (!child.gatherTransparentRegion(region)) { |
| 7180 | noneOfTheChildrenAreTransparent = false; |
| 7181 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7182 | } |
| 7183 | } |
Teng-Hui Zhu | f8da30d | 2016-08-15 10:28:20 -0700 | [diff] [blame] | 7184 | if (preorderedList != null) preorderedList.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7185 | } |
| 7186 | return meOpaque || noneOfTheChildrenAreTransparent; |
| 7187 | } |
| 7188 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 7189 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7190 | public void requestTransparentRegion(View child) { |
| 7191 | if (child != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 7192 | child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7193 | if (mParent != null) { |
| 7194 | mParent.requestTransparentRegion(this); |
| 7195 | } |
| 7196 | } |
| 7197 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7199 | @Override |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 7200 | public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { |
| 7201 | insets = super.dispatchApplyWindowInsets(insets); |
Jorim Jaggi | 15637fd | 2018-12-04 13:38:03 +0100 | [diff] [blame] | 7202 | if (View.sBrokenInsetsDispatch) { |
| 7203 | return brokenDispatchApplyWindowInsets(insets); |
| 7204 | } else { |
| 7205 | return newDispatchApplyWindowInsets(insets); |
| 7206 | } |
| 7207 | } |
| 7208 | |
| 7209 | private WindowInsets brokenDispatchApplyWindowInsets(WindowInsets insets) { |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 7210 | if (!insets.isConsumed()) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 7211 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7212 | for (int i = 0; i < count; i++) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 7213 | insets = getChildAt(i).dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 7214 | if (insets.isConsumed()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7215 | break; |
| 7216 | } |
| 7217 | } |
| 7218 | } |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 7219 | return insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7220 | } |
| 7221 | |
Jorim Jaggi | 15637fd | 2018-12-04 13:38:03 +0100 | [diff] [blame] | 7222 | private WindowInsets newDispatchApplyWindowInsets(WindowInsets insets) { |
| 7223 | final int count = getChildCount(); |
| 7224 | for (int i = 0; i < count; i++) { |
| 7225 | getChildAt(i).dispatchApplyWindowInsets(insets); |
| 7226 | } |
| 7227 | return insets; |
| 7228 | } |
| 7229 | |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 7230 | @Override |
| 7231 | void dispatchWindowInsetsAnimationStarted(InsetsAnimation animation) { |
| 7232 | super.dispatchWindowInsetsAnimationStarted(animation); |
| 7233 | final int count = getChildCount(); |
| 7234 | for (int i = 0; i < count; i++) { |
| 7235 | getChildAt(i).dispatchWindowInsetsAnimationStarted(animation); |
| 7236 | } |
| 7237 | } |
| 7238 | |
| 7239 | @Override |
| 7240 | WindowInsets dispatchWindowInsetsAnimationProgress(WindowInsets insets) { |
| 7241 | insets = super.dispatchWindowInsetsAnimationProgress(insets); |
| 7242 | final int count = getChildCount(); |
| 7243 | for (int i = 0; i < count; i++) { |
| 7244 | getChildAt(i).dispatchWindowInsetsAnimationProgress(insets); |
| 7245 | } |
| 7246 | return insets; |
| 7247 | } |
| 7248 | |
| 7249 | @Override |
| 7250 | void dispatchWindowInsetsAnimationFinished(InsetsAnimation animation) { |
| 7251 | super.dispatchWindowInsetsAnimationFinished(animation); |
| 7252 | final int count = getChildCount(); |
| 7253 | for (int i = 0; i < count; i++) { |
| 7254 | getChildAt(i).dispatchWindowInsetsAnimationFinished(animation); |
| 7255 | } |
| 7256 | } |
| 7257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7258 | /** |
| 7259 | * Returns the animation listener to which layout animation events are |
| 7260 | * sent. |
| 7261 | * |
| 7262 | * @return an {@link android.view.animation.Animation.AnimationListener} |
| 7263 | */ |
| 7264 | public Animation.AnimationListener getLayoutAnimationListener() { |
| 7265 | return mAnimationListener; |
| 7266 | } |
| 7267 | |
| 7268 | @Override |
| 7269 | protected void drawableStateChanged() { |
| 7270 | super.drawableStateChanged(); |
| 7271 | |
| 7272 | if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) { |
| 7273 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 7274 | throw new IllegalStateException("addStateFromChildren cannot be enabled if a" |
| 7275 | + " child has duplicateParentState set to true"); |
| 7276 | } |
| 7277 | |
| 7278 | final View[] children = mChildren; |
| 7279 | final int count = mChildrenCount; |
| 7280 | |
| 7281 | for (int i = 0; i < count; i++) { |
| 7282 | final View child = children[i]; |
| 7283 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) { |
| 7284 | child.refreshDrawableState(); |
| 7285 | } |
| 7286 | } |
| 7287 | } |
| 7288 | } |
| 7289 | |
| 7290 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 7291 | public void jumpDrawablesToCurrentState() { |
| 7292 | super.jumpDrawablesToCurrentState(); |
| 7293 | final View[] children = mChildren; |
| 7294 | final int count = mChildrenCount; |
| 7295 | for (int i = 0; i < count; i++) { |
| 7296 | children[i].jumpDrawablesToCurrentState(); |
| 7297 | } |
| 7298 | } |
| 7299 | |
| 7300 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7301 | protected int[] onCreateDrawableState(int extraSpace) { |
| 7302 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) { |
| 7303 | return super.onCreateDrawableState(extraSpace); |
| 7304 | } |
| 7305 | |
| 7306 | int need = 0; |
| 7307 | int n = getChildCount(); |
| 7308 | for (int i = 0; i < n; i++) { |
| 7309 | int[] childState = getChildAt(i).getDrawableState(); |
| 7310 | |
| 7311 | if (childState != null) { |
| 7312 | need += childState.length; |
| 7313 | } |
| 7314 | } |
| 7315 | |
| 7316 | int[] state = super.onCreateDrawableState(extraSpace + need); |
| 7317 | |
| 7318 | for (int i = 0; i < n; i++) { |
| 7319 | int[] childState = getChildAt(i).getDrawableState(); |
| 7320 | |
| 7321 | if (childState != null) { |
| 7322 | state = mergeDrawableStates(state, childState); |
| 7323 | } |
| 7324 | } |
| 7325 | |
| 7326 | return state; |
| 7327 | } |
| 7328 | |
| 7329 | /** |
| 7330 | * Sets whether this ViewGroup's drawable states also include |
| 7331 | * its children's drawable states. This is used, for example, to |
| 7332 | * make a group appear to be focused when its child EditText or button |
| 7333 | * is focused. |
| 7334 | */ |
| 7335 | public void setAddStatesFromChildren(boolean addsStates) { |
| 7336 | if (addsStates) { |
| 7337 | mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN; |
| 7338 | } else { |
| 7339 | mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN; |
| 7340 | } |
| 7341 | |
| 7342 | refreshDrawableState(); |
| 7343 | } |
| 7344 | |
| 7345 | /** |
| 7346 | * Returns whether this ViewGroup's drawable states also include |
| 7347 | * its children's drawable states. This is used, for example, to |
| 7348 | * make a group appear to be focused when its child EditText or button |
| 7349 | * is focused. |
| 7350 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 7351 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7352 | public boolean addStatesFromChildren() { |
| 7353 | return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0; |
| 7354 | } |
| 7355 | |
| 7356 | /** |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 7357 | * If {@link #addStatesFromChildren} is true, refreshes this group's |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7358 | * drawable state (to include the states from its children). |
| 7359 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 7360 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7361 | public void childDrawableStateChanged(View child) { |
| 7362 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 7363 | refreshDrawableState(); |
| 7364 | } |
| 7365 | } |
| 7366 | |
| 7367 | /** |
| 7368 | * Specifies the animation listener to which layout animation events must |
| 7369 | * be sent. Only |
| 7370 | * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)} |
| 7371 | * and |
| 7372 | * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)} |
| 7373 | * are invoked. |
| 7374 | * |
| 7375 | * @param animationListener the layout animation listener |
| 7376 | */ |
| 7377 | public void setLayoutAnimationListener(Animation.AnimationListener animationListener) { |
| 7378 | mAnimationListener = animationListener; |
| 7379 | } |
| 7380 | |
| 7381 | /** |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 7382 | * This method is called by LayoutTransition when there are 'changing' animations that need |
| 7383 | * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who |
| 7384 | * starts all pending transitions prior to the drawing phase in the current traversal. |
| 7385 | * |
| 7386 | * @param transition The LayoutTransition to be started on the next traversal. |
| 7387 | * |
| 7388 | * @hide |
| 7389 | */ |
| 7390 | public void requestTransitionStart(LayoutTransition transition) { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 7391 | ViewRootImpl viewAncestor = getViewRootImpl(); |
Chet Haase | 1abf7fa | 2011-08-17 18:31:56 -0700 | [diff] [blame] | 7392 | if (viewAncestor != null) { |
| 7393 | viewAncestor.requestTransitionStart(transition); |
| 7394 | } |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 7395 | } |
| 7396 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 7397 | /** |
| 7398 | * @hide |
| 7399 | */ |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 7400 | @Override |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 7401 | public boolean resolveRtlPropertiesIfNeeded() { |
| 7402 | final boolean result = super.resolveRtlPropertiesIfNeeded(); |
| 7403 | // We dont need to resolve the children RTL properties if nothing has changed for the parent |
| 7404 | if (result) { |
| 7405 | int count = getChildCount(); |
| 7406 | for (int i = 0; i < count; i++) { |
| 7407 | final View child = getChildAt(i); |
| 7408 | if (child.isLayoutDirectionInherited()) { |
| 7409 | child.resolveRtlPropertiesIfNeeded(); |
| 7410 | } |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7411 | } |
| 7412 | } |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 7413 | return result; |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7414 | } |
| 7415 | |
| 7416 | /** |
| 7417 | * @hide |
| 7418 | */ |
| 7419 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 7420 | public boolean resolveLayoutDirection() { |
| 7421 | final boolean result = super.resolveLayoutDirection(); |
| 7422 | if (result) { |
| 7423 | int count = getChildCount(); |
| 7424 | for (int i = 0; i < count; i++) { |
| 7425 | final View child = getChildAt(i); |
| 7426 | if (child.isLayoutDirectionInherited()) { |
| 7427 | child.resolveLayoutDirection(); |
| 7428 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7429 | } |
| 7430 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 7431 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7432 | } |
| 7433 | |
| 7434 | /** |
| 7435 | * @hide |
| 7436 | */ |
| 7437 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 7438 | public boolean resolveTextDirection() { |
| 7439 | final boolean result = super.resolveTextDirection(); |
| 7440 | if (result) { |
| 7441 | int count = getChildCount(); |
| 7442 | for (int i = 0; i < count; i++) { |
| 7443 | final View child = getChildAt(i); |
| 7444 | if (child.isTextDirectionInherited()) { |
| 7445 | child.resolveTextDirection(); |
| 7446 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7447 | } |
| 7448 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 7449 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7450 | } |
| 7451 | |
| 7452 | /** |
| 7453 | * @hide |
| 7454 | */ |
| 7455 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 7456 | public boolean resolveTextAlignment() { |
| 7457 | final boolean result = super.resolveTextAlignment(); |
| 7458 | if (result) { |
| 7459 | int count = getChildCount(); |
| 7460 | for (int i = 0; i < count; i++) { |
| 7461 | final View child = getChildAt(i); |
| 7462 | if (child.isTextAlignmentInherited()) { |
| 7463 | child.resolveTextAlignment(); |
| 7464 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7465 | } |
| 7466 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 7467 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7468 | } |
| 7469 | |
| 7470 | /** |
| 7471 | * @hide |
| 7472 | */ |
| 7473 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 7474 | @UnsupportedAppUsage |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7475 | public void resolvePadding() { |
| 7476 | super.resolvePadding(); |
| 7477 | int count = getChildCount(); |
| 7478 | for (int i = 0; i < count; i++) { |
| 7479 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 7480 | if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7481 | child.resolvePadding(); |
| 7482 | } |
| 7483 | } |
| 7484 | } |
| 7485 | |
| 7486 | /** |
| 7487 | * @hide |
| 7488 | */ |
| 7489 | @Override |
| 7490 | protected void resolveDrawables() { |
| 7491 | super.resolveDrawables(); |
| 7492 | int count = getChildCount(); |
| 7493 | for (int i = 0; i < count; i++) { |
| 7494 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 7495 | if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7496 | child.resolveDrawables(); |
| 7497 | } |
| 7498 | } |
| 7499 | } |
| 7500 | |
Fabrice Di Meglio | 1e0ed6b | 2012-10-18 16:06:52 -0700 | [diff] [blame] | 7501 | /** |
| 7502 | * @hide |
| 7503 | */ |
Fabrice Di Meglio | fcc3348 | 2012-10-18 11:11:51 -0700 | [diff] [blame] | 7504 | @Override |
| 7505 | public void resolveLayoutParams() { |
| 7506 | super.resolveLayoutParams(); |
| 7507 | int count = getChildCount(); |
| 7508 | for (int i = 0; i < count; i++) { |
| 7509 | final View child = getChildAt(i); |
| 7510 | child.resolveLayoutParams(); |
| 7511 | } |
| 7512 | } |
| 7513 | |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7514 | /** |
| 7515 | * @hide |
| 7516 | */ |
Aurimas Liutikas | 80e4e18 | 2018-08-13 16:13:37 -0700 | [diff] [blame] | 7517 | @TestApi |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7518 | @Override |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 7519 | public void resetResolvedLayoutDirection() { |
| 7520 | super.resetResolvedLayoutDirection(); |
| 7521 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 7522 | int count = getChildCount(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 7523 | for (int i = 0; i < count; i++) { |
| 7524 | final View child = getChildAt(i); |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 7525 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 7526 | child.resetResolvedLayoutDirection(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 7527 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7528 | } |
| 7529 | } |
| 7530 | |
| 7531 | /** |
| 7532 | * @hide |
| 7533 | */ |
Aurimas Liutikas | 80e4e18 | 2018-08-13 16:13:37 -0700 | [diff] [blame] | 7534 | @TestApi |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7535 | @Override |
| 7536 | public void resetResolvedTextDirection() { |
| 7537 | super.resetResolvedTextDirection(); |
| 7538 | |
| 7539 | int count = getChildCount(); |
| 7540 | for (int i = 0; i < count; i++) { |
| 7541 | final View child = getChildAt(i); |
Fabrice Di Meglio | 97e146c | 2012-09-23 15:45:16 -0700 | [diff] [blame] | 7542 | if (child.isTextDirectionInherited()) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 7543 | child.resetResolvedTextDirection(); |
| 7544 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | /** |
| 7549 | * @hide |
| 7550 | */ |
Aurimas Liutikas | 80e4e18 | 2018-08-13 16:13:37 -0700 | [diff] [blame] | 7551 | @TestApi |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 7552 | @Override |
| 7553 | public void resetResolvedTextAlignment() { |
| 7554 | super.resetResolvedTextAlignment(); |
| 7555 | |
| 7556 | int count = getChildCount(); |
| 7557 | for (int i = 0; i < count; i++) { |
| 7558 | final View child = getChildAt(i); |
Fabrice Di Meglio | 1a7d487 | 2012-09-23 16:19:58 -0700 | [diff] [blame] | 7559 | if (child.isTextAlignmentInherited()) { |
Fabrice Di Meglio | 9da0f8a | 2012-03-13 19:37:57 -0700 | [diff] [blame] | 7560 | child.resetResolvedTextAlignment(); |
| 7561 | } |
| 7562 | } |
| 7563 | } |
| 7564 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 7565 | /** |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7566 | * @hide |
| 7567 | */ |
Aurimas Liutikas | 80e4e18 | 2018-08-13 16:13:37 -0700 | [diff] [blame] | 7568 | @TestApi |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7569 | @Override |
| 7570 | public void resetResolvedPadding() { |
| 7571 | super.resetResolvedPadding(); |
| 7572 | |
| 7573 | int count = getChildCount(); |
| 7574 | for (int i = 0; i < count; i++) { |
| 7575 | final View child = getChildAt(i); |
| 7576 | if (child.isLayoutDirectionInherited()) { |
| 7577 | child.resetResolvedPadding(); |
| 7578 | } |
| 7579 | } |
| 7580 | } |
| 7581 | |
| 7582 | /** |
| 7583 | * @hide |
| 7584 | */ |
Aurimas Liutikas | 80e4e18 | 2018-08-13 16:13:37 -0700 | [diff] [blame] | 7585 | @TestApi |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 7586 | @Override |
| 7587 | protected void resetResolvedDrawables() { |
| 7588 | super.resetResolvedDrawables(); |
| 7589 | |
| 7590 | int count = getChildCount(); |
| 7591 | for (int i = 0; i < count; i++) { |
| 7592 | final View child = getChildAt(i); |
| 7593 | if (child.isLayoutDirectionInherited()) { |
| 7594 | child.resetResolvedDrawables(); |
| 7595 | } |
| 7596 | } |
| 7597 | } |
| 7598 | |
| 7599 | /** |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 7600 | * Return true if the pressed state should be delayed for children or descendants of this |
| 7601 | * ViewGroup. Generally, this should be done for containers that can scroll, such as a List. |
| 7602 | * This prevents the pressed state from appearing when the user is actually trying to scroll |
| 7603 | * the content. |
| 7604 | * |
| 7605 | * The default implementation returns true for compatibility reasons. Subclasses that do |
| 7606 | * not scroll should generally override this method and return false. |
| 7607 | */ |
| 7608 | public boolean shouldDelayChildPressedState() { |
| 7609 | return true; |
| 7610 | } |
| 7611 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 7612 | /** |
| 7613 | * @inheritDoc |
| 7614 | */ |
| 7615 | @Override |
| 7616 | public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { |
| 7617 | return false; |
| 7618 | } |
| 7619 | |
| 7620 | /** |
| 7621 | * @inheritDoc |
| 7622 | */ |
| 7623 | @Override |
| 7624 | public void onNestedScrollAccepted(View child, View target, int axes) { |
| 7625 | mNestedScrollAxes = axes; |
| 7626 | } |
| 7627 | |
| 7628 | /** |
| 7629 | * @inheritDoc |
| 7630 | * |
| 7631 | * <p>The default implementation of onStopNestedScroll calls |
| 7632 | * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p> |
| 7633 | */ |
| 7634 | @Override |
| 7635 | public void onStopNestedScroll(View child) { |
| 7636 | // Stop any recursive nested scrolling. |
| 7637 | stopNestedScroll(); |
Adam Powell | d4a22d4 | 2015-04-16 15:44:10 -0700 | [diff] [blame] | 7638 | mNestedScrollAxes = 0; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | /** |
| 7642 | * @inheritDoc |
| 7643 | */ |
| 7644 | @Override |
| 7645 | public void onNestedScroll(View target, int dxConsumed, int dyConsumed, |
| 7646 | int dxUnconsumed, int dyUnconsumed) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 7647 | // Re-dispatch up the tree by default |
| 7648 | dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null); |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 7649 | } |
| 7650 | |
| 7651 | /** |
| 7652 | * @inheritDoc |
| 7653 | */ |
| 7654 | @Override |
| 7655 | public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 7656 | // Re-dispatch up the tree by default |
| 7657 | dispatchNestedPreScroll(dx, dy, consumed, null); |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 7658 | } |
| 7659 | |
| 7660 | /** |
| 7661 | * @inheritDoc |
| 7662 | */ |
| 7663 | @Override |
Adam Powell | b36e4f9 | 2014-05-01 10:23:33 -0700 | [diff] [blame] | 7664 | public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 7665 | // Re-dispatch up the tree by default |
| 7666 | return dispatchNestedFling(velocityX, velocityY, consumed); |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | /** |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 7670 | * @inheritDoc |
| 7671 | */ |
| 7672 | @Override |
| 7673 | public boolean onNestedPreFling(View target, float velocityX, float velocityY) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 7674 | // Re-dispatch up the tree by default |
| 7675 | return dispatchNestedPreFling(velocityX, velocityY); |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 7676 | } |
| 7677 | |
| 7678 | /** |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 7679 | * Return the current axes of nested scrolling for this ViewGroup. |
| 7680 | * |
| 7681 | * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently |
| 7682 | * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p> |
| 7683 | * |
| 7684 | * @return Flags indicating the current axes of nested scrolling |
| 7685 | * @see #SCROLL_AXIS_HORIZONTAL |
| 7686 | * @see #SCROLL_AXIS_VERTICAL |
| 7687 | * @see #SCROLL_AXIS_NONE |
| 7688 | */ |
| 7689 | public int getNestedScrollAxes() { |
| 7690 | return mNestedScrollAxes; |
| 7691 | } |
| 7692 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 7693 | /** @hide */ |
| 7694 | protected void onSetLayoutParams(View child, LayoutParams layoutParams) { |
Robert Carr | 5429daa | 2017-04-03 19:00:26 -0700 | [diff] [blame] | 7695 | requestLayout(); |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 7696 | } |
| 7697 | |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 7698 | /** @hide */ |
| 7699 | @Override |
| 7700 | public void captureTransitioningViews(List<View> transitioningViews) { |
| 7701 | if (getVisibility() != View.VISIBLE) { |
| 7702 | return; |
| 7703 | } |
| 7704 | if (isTransitionGroup()) { |
| 7705 | transitioningViews.add(this); |
| 7706 | } else { |
| 7707 | int count = getChildCount(); |
| 7708 | for (int i = 0; i < count; i++) { |
| 7709 | View child = getChildAt(i); |
| 7710 | child.captureTransitioningViews(transitioningViews); |
| 7711 | } |
| 7712 | } |
| 7713 | } |
| 7714 | |
| 7715 | /** @hide */ |
| 7716 | @Override |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 7717 | public void findNamedViews(Map<String, View> namedElements) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 7718 | if (getVisibility() != VISIBLE && mGhostView == null) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 7719 | return; |
| 7720 | } |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 7721 | super.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 7722 | int count = getChildCount(); |
| 7723 | for (int i = 0; i < count; i++) { |
| 7724 | View child = getChildAt(i); |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 7725 | child.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 7726 | } |
| 7727 | } |
| 7728 | |
Evan Rosky | 4807ae2 | 2018-03-22 16:04:15 -0700 | [diff] [blame] | 7729 | @Override |
| 7730 | boolean hasUnhandledKeyListener() { |
| 7731 | return (mChildUnhandledKeyListeners > 0) || super.hasUnhandledKeyListener(); |
| 7732 | } |
| 7733 | |
| 7734 | void incrementChildUnhandledKeyListeners() { |
| 7735 | mChildUnhandledKeyListeners += 1; |
| 7736 | if (mChildUnhandledKeyListeners == 1) { |
| 7737 | if (mParent instanceof ViewGroup) { |
| 7738 | ((ViewGroup) mParent).incrementChildUnhandledKeyListeners(); |
| 7739 | } |
| 7740 | } |
| 7741 | } |
| 7742 | |
| 7743 | void decrementChildUnhandledKeyListeners() { |
| 7744 | mChildUnhandledKeyListeners -= 1; |
| 7745 | if (mChildUnhandledKeyListeners == 0) { |
| 7746 | if (mParent instanceof ViewGroup) { |
| 7747 | ((ViewGroup) mParent).decrementChildUnhandledKeyListeners(); |
| 7748 | } |
| 7749 | } |
| 7750 | } |
| 7751 | |
| 7752 | @Override |
| 7753 | View dispatchUnhandledKeyEvent(KeyEvent evt) { |
| 7754 | if (!hasUnhandledKeyListener()) { |
| 7755 | return null; |
| 7756 | } |
| 7757 | ArrayList<View> orderedViews = buildOrderedChildList(); |
| 7758 | if (orderedViews != null) { |
| 7759 | try { |
| 7760 | for (int i = orderedViews.size() - 1; i >= 0; --i) { |
| 7761 | View v = orderedViews.get(i); |
| 7762 | View consumer = v.dispatchUnhandledKeyEvent(evt); |
| 7763 | if (consumer != null) { |
| 7764 | return consumer; |
| 7765 | } |
| 7766 | } |
| 7767 | } finally { |
| 7768 | orderedViews.clear(); |
| 7769 | } |
| 7770 | } else { |
| 7771 | for (int i = getChildCount() - 1; i >= 0; --i) { |
| 7772 | View v = getChildAt(i); |
| 7773 | View consumer = v.dispatchUnhandledKeyEvent(evt); |
| 7774 | if (consumer != null) { |
| 7775 | return consumer; |
| 7776 | } |
| 7777 | } |
| 7778 | } |
| 7779 | if (onUnhandledKeyEvent(evt)) { |
| 7780 | return this; |
| 7781 | } |
| 7782 | return null; |
| 7783 | } |
| 7784 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 7785 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7786 | * LayoutParams are used by views to tell their parents how they want to be |
| 7787 | * laid out. See |
| 7788 | * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes} |
| 7789 | * for a list of all child view attributes that this class supports. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7790 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7791 | * <p> |
| 7792 | * The base LayoutParams class just describes how big the view wants to be |
| 7793 | * for both width and height. For each dimension, it can specify one of: |
| 7794 | * <ul> |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7795 | * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which |
| 7796 | * means that the view wants to be as big as its parent (minus padding) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7797 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough |
| 7798 | * to enclose its content (plus padding) |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7799 | * <li> an exact number |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7800 | * </ul> |
| 7801 | * There are subclasses of LayoutParams for different subclasses of |
| 7802 | * ViewGroup. For example, AbsoluteLayout has its own subclass of |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 7803 | * LayoutParams which adds an X and Y value.</p> |
| 7804 | * |
| 7805 | * <div class="special reference"> |
| 7806 | * <h3>Developer Guides</h3> |
| 7807 | * <p>For more information about creating user interface layouts, read the |
| 7808 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 7809 | * guide.</p></div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7810 | * |
| 7811 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_height |
| 7812 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_width |
| 7813 | */ |
| 7814 | public static class LayoutParams { |
| 7815 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7816 | * Special value for the height or width requested by a View. |
| 7817 | * FILL_PARENT means that the view wants to be as big as its parent, |
| 7818 | * minus the parent's padding, if any. This value is deprecated |
| 7819 | * starting in API Level 8 and replaced by {@link #MATCH_PARENT}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7820 | */ |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7821 | @SuppressWarnings({"UnusedDeclaration"}) |
| 7822 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7823 | public static final int FILL_PARENT = -1; |
| 7824 | |
| 7825 | /** |
| 7826 | * Special value for the height or width requested by a View. |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 7827 | * MATCH_PARENT means that the view wants to be as big as its parent, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7828 | * minus the parent's padding, if any. Introduced in API Level 8. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7829 | */ |
| 7830 | public static final int MATCH_PARENT = -1; |
| 7831 | |
| 7832 | /** |
| 7833 | * Special value for the height or width requested by a View. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7834 | * WRAP_CONTENT means that the view wants to be just large enough to fit |
| 7835 | * its own internal content, taking its own padding into account. |
| 7836 | */ |
| 7837 | public static final int WRAP_CONTENT = -2; |
| 7838 | |
| 7839 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7840 | * Information about how wide the view wants to be. Can be one of the |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 7841 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 7842 | * in API Level 8) or WRAP_CONTENT, or an exact size. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7843 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7844 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7845 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7846 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 7847 | }) |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 7848 | @InspectableProperty(name = "layout_width", enumMapping = { |
| 7849 | @InspectableProperty.EnumMap(name = "match_parent", value = MATCH_PARENT), |
| 7850 | @InspectableProperty.EnumMap(name = "wrap_content", value = WRAP_CONTENT) |
| 7851 | }) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7852 | public int width; |
| 7853 | |
| 7854 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7855 | * Information about how tall the view wants to be. Can be one of the |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 7856 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 7857 | * in API Level 8) or WRAP_CONTENT, or an exact size. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7858 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7859 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7860 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7861 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 7862 | }) |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 7863 | @InspectableProperty(name = "layout_height", enumMapping = { |
| 7864 | @InspectableProperty.EnumMap(name = "match_parent", value = MATCH_PARENT), |
| 7865 | @InspectableProperty.EnumMap(name = "wrap_content", value = WRAP_CONTENT) |
| 7866 | }) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7867 | public int height; |
| 7868 | |
| 7869 | /** |
| 7870 | * Used to animate layouts. |
| 7871 | */ |
| 7872 | public LayoutAnimationController.AnimationParameters layoutAnimationParameters; |
| 7873 | |
| 7874 | /** |
| 7875 | * Creates a new set of layout parameters. The values are extracted from |
| 7876 | * the supplied attributes set and context. The XML attributes mapped |
| 7877 | * to this set of layout parameters are: |
| 7878 | * |
| 7879 | * <ul> |
| 7880 | * <li><code>layout_width</code>: the width, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7881 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 7882 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7883 | * <li><code>layout_height</code>: the height, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7884 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 7885 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7886 | * </ul> |
| 7887 | * |
| 7888 | * @param c the application environment |
| 7889 | * @param attrs the set of attributes from which to extract the layout |
| 7890 | * parameters' values |
| 7891 | */ |
| 7892 | public LayoutParams(Context c, AttributeSet attrs) { |
| 7893 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout); |
| 7894 | setBaseAttributes(a, |
| 7895 | R.styleable.ViewGroup_Layout_layout_width, |
| 7896 | R.styleable.ViewGroup_Layout_layout_height); |
| 7897 | a.recycle(); |
| 7898 | } |
| 7899 | |
| 7900 | /** |
| 7901 | * Creates a new set of layout parameters with the specified width |
| 7902 | * and height. |
| 7903 | * |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 7904 | * @param width the width, either {@link #WRAP_CONTENT}, |
| 7905 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 7906 | * API Level 8), or a fixed size in pixels |
| 7907 | * @param height the height, either {@link #WRAP_CONTENT}, |
| 7908 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 7909 | * API Level 8), or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7910 | */ |
| 7911 | public LayoutParams(int width, int height) { |
| 7912 | this.width = width; |
| 7913 | this.height = height; |
| 7914 | } |
| 7915 | |
| 7916 | /** |
| 7917 | * Copy constructor. Clones the width and height values of the source. |
| 7918 | * |
| 7919 | * @param source The layout params to copy from. |
| 7920 | */ |
| 7921 | public LayoutParams(LayoutParams source) { |
| 7922 | this.width = source.width; |
| 7923 | this.height = source.height; |
| 7924 | } |
| 7925 | |
| 7926 | /** |
| 7927 | * Used internally by MarginLayoutParams. |
| 7928 | * @hide |
| 7929 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 7930 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7931 | LayoutParams() { |
| 7932 | } |
| 7933 | |
| 7934 | /** |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 7935 | * Extracts the layout parameters from the supplied attributes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7936 | * |
| 7937 | * @param a the style attributes to extract the parameters from |
| 7938 | * @param widthAttr the identifier of the width attribute |
| 7939 | * @param heightAttr the identifier of the height attribute |
| 7940 | */ |
| 7941 | protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 7942 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 7943 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7944 | } |
| 7945 | |
| 7946 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7947 | * Resolve layout parameters depending on the layout direction. Subclasses that care about |
| 7948 | * layoutDirection changes should override this method. The default implementation does |
| 7949 | * nothing. |
| 7950 | * |
| 7951 | * @param layoutDirection the direction of the layout |
| 7952 | * |
| 7953 | * {@link View#LAYOUT_DIRECTION_LTR} |
| 7954 | * {@link View#LAYOUT_DIRECTION_RTL} |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7955 | */ |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 7956 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7957 | } |
| 7958 | |
| 7959 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7960 | * Returns a String representation of this set of layout parameters. |
| 7961 | * |
| 7962 | * @param output the String to prepend to the internal representation |
| 7963 | * @return a String with the following format: output + |
| 7964 | * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }" |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7965 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7966 | * @hide |
| 7967 | */ |
| 7968 | public String debug(String output) { |
| 7969 | return output + "ViewGroup.LayoutParams={ width=" |
| 7970 | + sizeToString(width) + ", height=" + sizeToString(height) + " }"; |
| 7971 | } |
| 7972 | |
| 7973 | /** |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7974 | * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters. |
| 7975 | * |
| 7976 | * @param view the view that contains these layout parameters |
| 7977 | * @param canvas the canvas on which to draw |
Alan Viverette | 2d7771c | 2018-01-31 17:04:31 -0500 | [diff] [blame] | 7978 | * |
| 7979 | * @hide |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7980 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7981 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7982 | } |
| 7983 | |
| 7984 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7985 | * Converts the specified size to a readable String. |
| 7986 | * |
| 7987 | * @param size the size to convert |
| 7988 | * @return a String instance representing the supplied size |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7989 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7990 | * @hide |
| 7991 | */ |
| 7992 | protected static String sizeToString(int size) { |
| 7993 | if (size == WRAP_CONTENT) { |
| 7994 | return "wrap-content"; |
| 7995 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7996 | if (size == MATCH_PARENT) { |
| 7997 | return "match-parent"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7998 | } |
| 7999 | return String.valueOf(size); |
| 8000 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 8001 | |
| 8002 | /** @hide */ |
| 8003 | void encode(@NonNull ViewHierarchyEncoder encoder) { |
| 8004 | encoder.beginObject(this); |
| 8005 | encodeProperties(encoder); |
| 8006 | encoder.endObject(); |
| 8007 | } |
| 8008 | |
| 8009 | /** @hide */ |
| 8010 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 8011 | encoder.addProperty("width", width); |
| 8012 | encoder.addProperty("height", height); |
| 8013 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8014 | } |
| 8015 | |
| 8016 | /** |
| 8017 | * Per-child layout information for layouts that support margins. |
| 8018 | * See |
| 8019 | * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes} |
| 8020 | * for a list of all child view attributes that this class supports. |
Chet Haase | 353d397 | 2017-06-29 07:54:19 -0700 | [diff] [blame] | 8021 | * |
| 8022 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_margin |
| 8023 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginHorizontal |
| 8024 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginVertical |
| 8025 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 8026 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 8027 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 8028 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 8029 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 8030 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | */ |
| 8032 | public static class MarginLayoutParams extends ViewGroup.LayoutParams { |
| 8033 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8034 | * The left margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 8035 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 8036 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8037 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 8038 | @ViewDebug.ExportedProperty(category = "layout") |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 8039 | @InspectableProperty(name = "layout_marginLeft") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8040 | public int leftMargin; |
| 8041 | |
| 8042 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8043 | * The top margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 8044 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 8045 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8046 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 8047 | @ViewDebug.ExportedProperty(category = "layout") |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 8048 | @InspectableProperty(name = "layout_marginTop") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8049 | public int topMargin; |
| 8050 | |
| 8051 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8052 | * The right margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 8053 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 8054 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8055 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 8056 | @ViewDebug.ExportedProperty(category = "layout") |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 8057 | @InspectableProperty(name = "layout_marginRight") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8058 | public int rightMargin; |
| 8059 | |
| 8060 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8061 | * The bottom margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 8062 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 8063 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8064 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 8065 | @ViewDebug.ExportedProperty(category = "layout") |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 8066 | @InspectableProperty(name = "layout_marginBottom") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8067 | public int bottomMargin; |
| 8068 | |
| 8069 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8070 | * The start margin in pixels of the child. Margin values should be positive. |
Fabrice Di Meglio | 54546f2 | 2012-02-14 16:26:16 -0800 | [diff] [blame] | 8071 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 8072 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8073 | */ |
| 8074 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 8075 | @UnsupportedAppUsage |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8076 | private int startMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8077 | |
| 8078 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8079 | * The end margin in pixels of the child. Margin values should be positive. |
Fabrice Di Meglio | 54546f2 | 2012-02-14 16:26:16 -0800 | [diff] [blame] | 8080 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 8081 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8082 | */ |
| 8083 | @ViewDebug.ExportedProperty(category = "layout") |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 8084 | @UnsupportedAppUsage |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8085 | private int endMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8086 | |
| 8087 | /** |
| 8088 | * The default start and end margin. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8089 | * @hide |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8090 | */ |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8091 | public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8092 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8093 | /** |
| 8094 | * Bit 0: layout direction |
| 8095 | * Bit 1: layout direction |
| 8096 | * Bit 2: left margin undefined |
| 8097 | * Bit 3: right margin undefined |
| 8098 | * Bit 4: is RTL compatibility mode |
| 8099 | * Bit 5: need resolution |
| 8100 | * |
| 8101 | * Bit 6 to 7 not used |
| 8102 | * |
| 8103 | * @hide |
| 8104 | */ |
| 8105 | @ViewDebug.ExportedProperty(category = "layout", flagMapping = { |
| 8106 | @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK, |
| 8107 | equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"), |
| 8108 | @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK, |
| 8109 | equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"), |
| 8110 | @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK, |
| 8111 | equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"), |
| 8112 | @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK, |
| 8113 | equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"), |
| 8114 | @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK, |
| 8115 | equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 8116 | }, formatToHexString = true) |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8117 | byte mMarginFlags; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8118 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8119 | private static final int LAYOUT_DIRECTION_MASK = 0x00000003; |
| 8120 | private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004; |
| 8121 | private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008; |
| 8122 | private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010; |
| 8123 | private static final int NEED_RESOLUTION_MASK = 0x00000020; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8124 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8125 | private static final int DEFAULT_MARGIN_RESOLVED = 0; |
| 8126 | private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8127 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8128 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8129 | * Creates a new set of layout parameters. The values are extracted from |
| 8130 | * the supplied attributes set and context. |
| 8131 | * |
| 8132 | * @param c the application environment |
| 8133 | * @param attrs the set of attributes from which to extract the layout |
| 8134 | * parameters' values |
| 8135 | */ |
| 8136 | public MarginLayoutParams(Context c, AttributeSet attrs) { |
| 8137 | super(); |
| 8138 | |
| 8139 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 8140 | setBaseAttributes(a, |
| 8141 | R.styleable.ViewGroup_MarginLayout_layout_width, |
| 8142 | R.styleable.ViewGroup_MarginLayout_layout_height); |
| 8143 | |
| 8144 | int margin = a.getDimensionPixelSize( |
| 8145 | com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1); |
| 8146 | if (margin >= 0) { |
| 8147 | leftMargin = margin; |
| 8148 | topMargin = margin; |
| 8149 | rightMargin= margin; |
| 8150 | bottomMargin = margin; |
| 8151 | } else { |
Chet Haase | 40b2de5 | 2016-11-28 16:11:42 -0800 | [diff] [blame] | 8152 | int horizontalMargin = a.getDimensionPixelSize( |
| 8153 | R.styleable.ViewGroup_MarginLayout_layout_marginHorizontal, -1); |
| 8154 | int verticalMargin = a.getDimensionPixelSize( |
| 8155 | R.styleable.ViewGroup_MarginLayout_layout_marginVertical, -1); |
| 8156 | |
| 8157 | if (horizontalMargin >= 0) { |
| 8158 | leftMargin = horizontalMargin; |
| 8159 | rightMargin = horizontalMargin; |
| 8160 | } else { |
| 8161 | leftMargin = a.getDimensionPixelSize( |
| 8162 | R.styleable.ViewGroup_MarginLayout_layout_marginLeft, |
| 8163 | UNDEFINED_MARGIN); |
| 8164 | if (leftMargin == UNDEFINED_MARGIN) { |
| 8165 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 8166 | leftMargin = DEFAULT_MARGIN_RESOLVED; |
| 8167 | } |
| 8168 | rightMargin = a.getDimensionPixelSize( |
| 8169 | R.styleable.ViewGroup_MarginLayout_layout_marginRight, |
| 8170 | UNDEFINED_MARGIN); |
| 8171 | if (rightMargin == UNDEFINED_MARGIN) { |
| 8172 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
| 8173 | rightMargin = DEFAULT_MARGIN_RESOLVED; |
| 8174 | } |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8175 | } |
| 8176 | |
Chet Haase | cb3d023 | 2017-05-24 18:27:14 -0700 | [diff] [blame] | 8177 | startMargin = a.getDimensionPixelSize( |
| 8178 | R.styleable.ViewGroup_MarginLayout_layout_marginStart, |
| 8179 | DEFAULT_MARGIN_RELATIVE); |
| 8180 | endMargin = a.getDimensionPixelSize( |
| 8181 | R.styleable.ViewGroup_MarginLayout_layout_marginEnd, |
| 8182 | DEFAULT_MARGIN_RELATIVE); |
| 8183 | |
Chet Haase | 40b2de5 | 2016-11-28 16:11:42 -0800 | [diff] [blame] | 8184 | if (verticalMargin >= 0) { |
| 8185 | topMargin = verticalMargin; |
| 8186 | bottomMargin = verticalMargin; |
| 8187 | } else { |
| 8188 | topMargin = a.getDimensionPixelSize( |
| 8189 | R.styleable.ViewGroup_MarginLayout_layout_marginTop, |
| 8190 | DEFAULT_MARGIN_RESOLVED); |
| 8191 | bottomMargin = a.getDimensionPixelSize( |
| 8192 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom, |
| 8193 | DEFAULT_MARGIN_RESOLVED); |
| 8194 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8195 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8196 | if (isMarginRelative()) { |
| 8197 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 8198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8199 | } |
| 8200 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8201 | final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport(); |
| 8202 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8203 | if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) { |
| 8204 | mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK; |
| 8205 | } |
| 8206 | |
| 8207 | // Layout direction is LTR by default |
| 8208 | mMarginFlags |= LAYOUT_DIRECTION_LTR; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8210 | a.recycle(); |
| 8211 | } |
| 8212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8213 | public MarginLayoutParams(int width, int height) { |
| 8214 | super(width, height); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8215 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8216 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 8217 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8218 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8219 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 8220 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | /** |
| 8224 | * Copy constructor. Clones the width, height and margin values of the source. |
| 8225 | * |
| 8226 | * @param source The layout params to copy from. |
| 8227 | */ |
| 8228 | public MarginLayoutParams(MarginLayoutParams source) { |
| 8229 | this.width = source.width; |
| 8230 | this.height = source.height; |
| 8231 | |
| 8232 | this.leftMargin = source.leftMargin; |
| 8233 | this.topMargin = source.topMargin; |
| 8234 | this.rightMargin = source.rightMargin; |
| 8235 | this.bottomMargin = source.bottomMargin; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8236 | this.startMargin = source.startMargin; |
| 8237 | this.endMargin = source.endMargin; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8238 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8239 | this.mMarginFlags = source.mMarginFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8240 | } |
| 8241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8242 | public MarginLayoutParams(LayoutParams source) { |
| 8243 | super(source); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8244 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8245 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 8246 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8247 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8248 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 8249 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | /** |
Adam Powell | d760083 | 2014-07-01 15:22:50 -0700 | [diff] [blame] | 8253 | * @hide Used internally. |
| 8254 | */ |
| 8255 | public final void copyMarginsFrom(MarginLayoutParams source) { |
| 8256 | this.leftMargin = source.leftMargin; |
| 8257 | this.topMargin = source.topMargin; |
| 8258 | this.rightMargin = source.rightMargin; |
| 8259 | this.bottomMargin = source.bottomMargin; |
| 8260 | this.startMargin = source.startMargin; |
| 8261 | this.endMargin = source.endMargin; |
| 8262 | |
| 8263 | this.mMarginFlags = source.mMarginFlags; |
| 8264 | } |
| 8265 | |
| 8266 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8267 | * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs |
| 8268 | * to be done so that the new margins are taken into account. Left and right margins may be |
kopriva | debd4ee | 2018-09-13 10:59:46 -0700 | [diff] [blame] | 8269 | * overridden by {@link android.view.View#requestLayout()} depending on layout direction. |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8270 | * Margin values should be positive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8271 | * |
| 8272 | * @param left the left margin size |
| 8273 | * @param top the top margin size |
| 8274 | * @param right the right margin size |
| 8275 | * @param bottom the bottom margin size |
| 8276 | * |
| 8277 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 8278 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 8279 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 8280 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 8281 | */ |
| 8282 | public void setMargins(int left, int top, int right, int bottom) { |
| 8283 | leftMargin = left; |
| 8284 | topMargin = top; |
| 8285 | rightMargin = right; |
| 8286 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8287 | mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK; |
| 8288 | mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK; |
| 8289 | if (isMarginRelative()) { |
| 8290 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 8291 | } else { |
| 8292 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 8293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8294 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8295 | |
| 8296 | /** |
| 8297 | * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()} |
| 8298 | * needs to be done so that the new relative margins are taken into account. Left and right |
kopriva | debd4ee | 2018-09-13 10:59:46 -0700 | [diff] [blame] | 8299 | * margins may be overridden by {@link android.view.View#requestLayout()} depending on |
| 8300 | * layout direction. Margin values should be positive. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8301 | * |
| 8302 | * @param start the start margin size |
| 8303 | * @param top the top margin size |
| 8304 | * @param end the right margin size |
| 8305 | * @param bottom the bottom margin size |
| 8306 | * |
| 8307 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 8308 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 8309 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 8310 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 8311 | * |
| 8312 | * @hide |
| 8313 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 8314 | @UnsupportedAppUsage |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8315 | public void setMarginsRelative(int start, int top, int end, int bottom) { |
| 8316 | startMargin = start; |
| 8317 | topMargin = top; |
| 8318 | endMargin = end; |
| 8319 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8320 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8321 | } |
| 8322 | |
| 8323 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8324 | * Sets the relative start margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 8325 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 8326 | * @param start the start margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 8327 | * |
| 8328 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 8329 | */ |
| 8330 | public void setMarginStart(int start) { |
| 8331 | startMargin = start; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8332 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 8333 | } |
| 8334 | |
| 8335 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8336 | * Returns the start margin in pixels. |
| 8337 | * |
| 8338 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 8339 | * |
| 8340 | * @return the start margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8341 | */ |
| 8342 | public int getMarginStart() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8343 | if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8344 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8345 | doResolveMargins(); |
| 8346 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8347 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8348 | case View.LAYOUT_DIRECTION_RTL: |
| 8349 | return rightMargin; |
| 8350 | case View.LAYOUT_DIRECTION_LTR: |
| 8351 | default: |
| 8352 | return leftMargin; |
| 8353 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8354 | } |
| 8355 | |
| 8356 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 8357 | * Sets the relative end margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 8358 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 8359 | * @param end the end margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 8360 | * |
| 8361 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 8362 | */ |
| 8363 | public void setMarginEnd(int end) { |
| 8364 | endMargin = end; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8365 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 8366 | } |
| 8367 | |
| 8368 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8369 | * Returns the end margin in pixels. |
| 8370 | * |
| 8371 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 8372 | * |
| 8373 | * @return the end margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8374 | */ |
| 8375 | public int getMarginEnd() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8376 | if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8377 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8378 | doResolveMargins(); |
| 8379 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8380 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8381 | case View.LAYOUT_DIRECTION_RTL: |
| 8382 | return leftMargin; |
| 8383 | case View.LAYOUT_DIRECTION_LTR: |
| 8384 | default: |
| 8385 | return rightMargin; |
| 8386 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8387 | } |
| 8388 | |
| 8389 | /** |
| 8390 | * Check if margins are relative. |
| 8391 | * |
| 8392 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 8393 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 8394 | * |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8395 | * @return true if either marginStart or marginEnd has been set. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8396 | */ |
| 8397 | public boolean isMarginRelative() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8398 | return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8399 | } |
| 8400 | |
| 8401 | /** |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8402 | * Set the layout direction |
| 8403 | * @param layoutDirection the layout direction. |
| 8404 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 8405 | * or {@link View#LAYOUT_DIRECTION_RTL}. |
| 8406 | */ |
| 8407 | public void setLayoutDirection(int layoutDirection) { |
| 8408 | if (layoutDirection != View.LAYOUT_DIRECTION_LTR && |
| 8409 | layoutDirection != View.LAYOUT_DIRECTION_RTL) return; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8410 | if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) { |
| 8411 | mMarginFlags &= ~LAYOUT_DIRECTION_MASK; |
| 8412 | mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK); |
| 8413 | if (isMarginRelative()) { |
| 8414 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 8415 | } else { |
| 8416 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 8417 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8418 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8419 | } |
| 8420 | |
| 8421 | /** |
| 8422 | * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or |
| 8423 | * {@link View#LAYOUT_DIRECTION_RTL}. |
| 8424 | * |
| 8425 | * @return the layout direction. |
| 8426 | */ |
| 8427 | public int getLayoutDirection() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8428 | return (mMarginFlags & LAYOUT_DIRECTION_MASK); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8429 | } |
| 8430 | |
| 8431 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8432 | * This will be called by {@link android.view.View#requestLayout()}. Left and Right margins |
Fabrice Di Meglio | 98aec1c | 2012-02-13 16:54:05 -0800 | [diff] [blame] | 8433 | * may be overridden depending on layout direction. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8434 | */ |
| 8435 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 8436 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8437 | setLayoutDirection(layoutDirection); |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8438 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8439 | // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything |
| 8440 | // Will use the left and right margins if no relative margin is defined. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8441 | if (!isMarginRelative() || |
| 8442 | (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 8443 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 8444 | // Proceed with resolution |
| 8445 | doResolveMargins(); |
| 8446 | } |
| 8447 | |
| 8448 | private void doResolveMargins() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8449 | if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8450 | // if left or right margins are not defined and if we have some start or end margin |
| 8451 | // defined then use those start and end margins. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8452 | if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK |
| 8453 | && startMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8454 | leftMargin = startMargin; |
| 8455 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8456 | if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK |
| 8457 | && endMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8458 | rightMargin = endMargin; |
| 8459 | } |
| 8460 | } else { |
| 8461 | // We have some relative margins (either the start one or the end one or both). So use |
| 8462 | // them and override what has been defined for left and right margins. If either start |
| 8463 | // or end margin is not defined, just set it to default "0". |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8464 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 8465 | case View.LAYOUT_DIRECTION_RTL: |
| 8466 | leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 8467 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 8468 | rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 8469 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 8470 | break; |
| 8471 | case View.LAYOUT_DIRECTION_LTR: |
| 8472 | default: |
| 8473 | leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 8474 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 8475 | rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 8476 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 8477 | break; |
| 8478 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8479 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8480 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 8481 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 8482 | |
Fabrice Di Meglio | 03b8d3a | 2012-09-27 17:05:27 -0700 | [diff] [blame] | 8483 | /** |
| 8484 | * @hide |
| 8485 | */ |
| 8486 | public boolean isLayoutRtl() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 8487 | return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 8488 | } |
| 8489 | |
Alan Viverette | 2d7771c | 2018-01-31 17:04:31 -0500 | [diff] [blame] | 8490 | /** |
| 8491 | * @hide |
| 8492 | */ |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 8493 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 8494 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
| 8495 | Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE; |
| 8496 | |
| 8497 | fillDifference(canvas, |
| 8498 | view.getLeft() + oi.left, |
| 8499 | view.getTop() + oi.top, |
| 8500 | view.getRight() - oi.right, |
| 8501 | view.getBottom() - oi.bottom, |
| 8502 | leftMargin, |
| 8503 | topMargin, |
| 8504 | rightMargin, |
| 8505 | bottomMargin, |
| 8506 | paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 8507 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 8508 | |
| 8509 | /** @hide */ |
| 8510 | @Override |
| 8511 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 8512 | super.encodeProperties(encoder); |
| 8513 | encoder.addProperty("leftMargin", leftMargin); |
| 8514 | encoder.addProperty("topMargin", topMargin); |
| 8515 | encoder.addProperty("rightMargin", rightMargin); |
| 8516 | encoder.addProperty("bottomMargin", bottomMargin); |
| 8517 | encoder.addProperty("startMargin", startMargin); |
| 8518 | encoder.addProperty("endMargin", endMargin); |
| 8519 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8520 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 8521 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8522 | /* Describes a touched view and the ids of the pointers that it has captured. |
| 8523 | * |
| 8524 | * This code assumes that pointer ids are always in the range 0..31 such that |
| 8525 | * it can use a bitfield to track which pointer ids are present. |
| 8526 | * As it happens, the lower layers of the input dispatch pipeline also use the |
| 8527 | * same trick so the assumption should be safe here... |
| 8528 | */ |
| 8529 | private static final class TouchTarget { |
| 8530 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 8531 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8532 | private static TouchTarget sRecycleBin; |
| 8533 | private static int sRecycledCount; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 8534 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8535 | public static final int ALL_POINTER_IDS = -1; // all ones |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 8536 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8537 | // The touched child view. |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 8538 | @UnsupportedAppUsage |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8539 | public View child; |
| 8540 | |
| 8541 | // The combined bit mask of pointer ids for all pointers captured by the target. |
| 8542 | public int pointerIdBits; |
| 8543 | |
| 8544 | // The next target in the target list. |
| 8545 | public TouchTarget next; |
| 8546 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 8547 | @UnsupportedAppUsage |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8548 | private TouchTarget() { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 8549 | } |
| 8550 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 8551 | public static TouchTarget obtain(@NonNull View child, int pointerIdBits) { |
| 8552 | if (child == null) { |
| 8553 | throw new IllegalArgumentException("child must be non-null"); |
| 8554 | } |
| 8555 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8556 | final TouchTarget target; |
| 8557 | synchronized (sRecycleLock) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8558 | if (sRecycleBin == null) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8559 | target = new TouchTarget(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8560 | } else { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8561 | target = sRecycleBin; |
| 8562 | sRecycleBin = target.next; |
| 8563 | sRecycledCount--; |
| 8564 | target.next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8565 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8566 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8567 | target.child = child; |
| 8568 | target.pointerIdBits = pointerIdBits; |
| 8569 | return target; |
| 8570 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8571 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8572 | public void recycle() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 8573 | if (child == null) { |
| 8574 | throw new IllegalStateException("already recycled once"); |
| 8575 | } |
| 8576 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 8577 | synchronized (sRecycleLock) { |
| 8578 | if (sRecycledCount < MAX_RECYCLED) { |
| 8579 | next = sRecycleBin; |
| 8580 | sRecycleBin = this; |
| 8581 | sRecycledCount += 1; |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 8582 | } else { |
| 8583 | next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8584 | } |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 8585 | child = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 8586 | } |
| 8587 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 8588 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 8589 | |
| 8590 | /* Describes a hovered view. */ |
| 8591 | private static final class HoverTarget { |
| 8592 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 8593 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 8594 | private static HoverTarget sRecycleBin; |
| 8595 | private static int sRecycledCount; |
| 8596 | |
| 8597 | // The hovered child view. |
| 8598 | public View child; |
| 8599 | |
| 8600 | // The next target in the target list. |
| 8601 | public HoverTarget next; |
| 8602 | |
| 8603 | private HoverTarget() { |
| 8604 | } |
| 8605 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 8606 | public static HoverTarget obtain(@NonNull View child) { |
| 8607 | if (child == null) { |
| 8608 | throw new IllegalArgumentException("child must be non-null"); |
| 8609 | } |
| 8610 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 8611 | final HoverTarget target; |
| 8612 | synchronized (sRecycleLock) { |
| 8613 | if (sRecycleBin == null) { |
| 8614 | target = new HoverTarget(); |
| 8615 | } else { |
| 8616 | target = sRecycleBin; |
| 8617 | sRecycleBin = target.next; |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 8618 | sRecycledCount--; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 8619 | target.next = null; |
| 8620 | } |
| 8621 | } |
| 8622 | target.child = child; |
| 8623 | return target; |
| 8624 | } |
| 8625 | |
| 8626 | public void recycle() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 8627 | if (child == null) { |
| 8628 | throw new IllegalStateException("already recycled once"); |
| 8629 | } |
| 8630 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 8631 | synchronized (sRecycleLock) { |
| 8632 | if (sRecycledCount < MAX_RECYCLED) { |
| 8633 | next = sRecycleBin; |
| 8634 | sRecycleBin = this; |
| 8635 | sRecycledCount += 1; |
| 8636 | } else { |
| 8637 | next = null; |
| 8638 | } |
| 8639 | child = null; |
| 8640 | } |
| 8641 | } |
| 8642 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8643 | |
| 8644 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 8645 | * Pooled class that to hold the children for autifill. |
| 8646 | */ |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 8647 | private static class ChildListForAutoFillOrContentCapture extends ArrayList<View> { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 8648 | private static final int MAX_POOL_SIZE = 32; |
| 8649 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 8650 | private static final Pools.SimplePool<ChildListForAutoFillOrContentCapture> sPool = |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 8651 | new Pools.SimplePool<>(MAX_POOL_SIZE); |
| 8652 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 8653 | public static ChildListForAutoFillOrContentCapture obtain() { |
| 8654 | ChildListForAutoFillOrContentCapture list = sPool.acquire(); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 8655 | if (list == null) { |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 8656 | list = new ChildListForAutoFillOrContentCapture(); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 8657 | } |
| 8658 | return list; |
| 8659 | } |
| 8660 | |
| 8661 | public void recycle() { |
| 8662 | clear(); |
| 8663 | sPool.release(this); |
| 8664 | } |
| 8665 | } |
| 8666 | |
| 8667 | /** |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8668 | * Pooled class that orderes the children of a ViewGroup from start |
| 8669 | * to end based on how they are laid out and the layout direction. |
| 8670 | */ |
| 8671 | static class ChildListForAccessibility { |
| 8672 | |
| 8673 | private static final int MAX_POOL_SIZE = 32; |
| 8674 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8675 | private static final SynchronizedPool<ChildListForAccessibility> sPool = |
| 8676 | new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8677 | |
| 8678 | private final ArrayList<View> mChildren = new ArrayList<View>(); |
| 8679 | |
| 8680 | private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>(); |
| 8681 | |
| 8682 | public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8683 | ChildListForAccessibility list = sPool.acquire(); |
| 8684 | if (list == null) { |
| 8685 | list = new ChildListForAccessibility(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8686 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8687 | list.init(parent, sort); |
| 8688 | return list; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8689 | } |
| 8690 | |
| 8691 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8692 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8693 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8694 | } |
| 8695 | |
| 8696 | public int getChildCount() { |
| 8697 | return mChildren.size(); |
| 8698 | } |
| 8699 | |
| 8700 | public View getChildAt(int index) { |
| 8701 | return mChildren.get(index); |
| 8702 | } |
| 8703 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8704 | private void init(ViewGroup parent, boolean sort) { |
| 8705 | ArrayList<View> children = mChildren; |
| 8706 | final int childCount = parent.getChildCount(); |
| 8707 | for (int i = 0; i < childCount; i++) { |
| 8708 | View child = parent.getChildAt(i); |
| 8709 | children.add(child); |
| 8710 | } |
| 8711 | if (sort) { |
| 8712 | ArrayList<ViewLocationHolder> holders = mHolders; |
| 8713 | for (int i = 0; i < childCount; i++) { |
| 8714 | View child = children.get(i); |
| 8715 | ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child); |
| 8716 | holders.add(holder); |
| 8717 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8718 | sort(holders); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8719 | for (int i = 0; i < childCount; i++) { |
| 8720 | ViewLocationHolder holder = holders.get(i); |
| 8721 | children.set(i, holder.mView); |
| 8722 | holder.recycle(); |
| 8723 | } |
| 8724 | holders.clear(); |
| 8725 | } |
| 8726 | } |
| 8727 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8728 | private void sort(ArrayList<ViewLocationHolder> holders) { |
| 8729 | // This is gross but the least risky solution. The current comparison |
| 8730 | // strategy breaks transitivity but produces very good results. Coming |
| 8731 | // up with a new strategy requires time which we do not have, so ... |
| 8732 | try { |
| 8733 | ViewLocationHolder.setComparisonStrategy( |
| 8734 | ViewLocationHolder.COMPARISON_STRATEGY_STRIPE); |
| 8735 | Collections.sort(holders); |
| 8736 | } catch (IllegalArgumentException iae) { |
| 8737 | // Note that in practice this occurs extremely rarely in a couple |
| 8738 | // of pathological cases. |
| 8739 | ViewLocationHolder.setComparisonStrategy( |
| 8740 | ViewLocationHolder.COMPARISON_STRATEGY_LOCATION); |
| 8741 | Collections.sort(holders); |
| 8742 | } |
| 8743 | } |
| 8744 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8745 | private void clear() { |
| 8746 | mChildren.clear(); |
| 8747 | } |
| 8748 | } |
| 8749 | |
| 8750 | /** |
| 8751 | * Pooled class that holds a View and its location with respect to |
| 8752 | * a specified root. This enables sorting of views based on their |
| 8753 | * coordinates without recomputing the position relative to the root |
| 8754 | * on every comparison. |
| 8755 | */ |
| 8756 | static class ViewLocationHolder implements Comparable<ViewLocationHolder> { |
| 8757 | |
| 8758 | private static final int MAX_POOL_SIZE = 32; |
| 8759 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8760 | private static final SynchronizedPool<ViewLocationHolder> sPool = |
| 8761 | new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8762 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8763 | public static final int COMPARISON_STRATEGY_STRIPE = 1; |
| 8764 | |
| 8765 | public static final int COMPARISON_STRATEGY_LOCATION = 2; |
| 8766 | |
| 8767 | private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE; |
| 8768 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8769 | private final Rect mLocation = new Rect(); |
| 8770 | |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8771 | private ViewGroup mRoot; |
| 8772 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8773 | public View mView; |
| 8774 | |
| 8775 | private int mLayoutDirection; |
| 8776 | |
| 8777 | public static ViewLocationHolder obtain(ViewGroup root, View view) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8778 | ViewLocationHolder holder = sPool.acquire(); |
| 8779 | if (holder == null) { |
| 8780 | holder = new ViewLocationHolder(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8781 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8782 | holder.init(root, view); |
| 8783 | return holder; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8784 | } |
| 8785 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8786 | public static void setComparisonStrategy(int strategy) { |
| 8787 | sComparisonStrategy = strategy; |
| 8788 | } |
| 8789 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8790 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8791 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 8792 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8793 | } |
| 8794 | |
| 8795 | @Override |
| 8796 | public int compareTo(ViewLocationHolder another) { |
| 8797 | // This instance is greater than an invalid argument. |
| 8798 | if (another == null) { |
| 8799 | return 1; |
| 8800 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8801 | |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8802 | int boundsResult = compareBoundsOfTree(this, another); |
| 8803 | if (boundsResult != 0) { |
| 8804 | return boundsResult; |
| 8805 | } |
| 8806 | |
| 8807 | // Just break the tie somehow. The accessibility ids are unique |
| 8808 | // and stable, hence this is deterministic tie breaking. |
| 8809 | return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId(); |
| 8810 | } |
| 8811 | |
| 8812 | /** |
| 8813 | * Compare two views based on their bounds. Use the bounds of their children to break ties. |
| 8814 | * |
| 8815 | * @param holder1 Holder of first view to compare |
Phil Weaver | 6adf3a1 | 2018-08-21 16:08:42 -0700 | [diff] [blame] | 8816 | * @param holder2 Holder of second view to compare. Must have the same root as holder1. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8817 | * @return The compare result, with equality if no good comparison was found. |
| 8818 | */ |
| 8819 | private static int compareBoundsOfTree( |
| 8820 | ViewLocationHolder holder1, ViewLocationHolder holder2) { |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8821 | if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) { |
| 8822 | // First is above second. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8823 | if (holder1.mLocation.bottom - holder2.mLocation.top <= 0) { |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8824 | return -1; |
| 8825 | } |
| 8826 | // First is below second. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8827 | if (holder1.mLocation.top - holder2.mLocation.bottom >= 0) { |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 8828 | return 1; |
| 8829 | } |
| 8830 | } |
| 8831 | |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 8832 | // We are ordering left-to-right, top-to-bottom. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8833 | if (holder1.mLayoutDirection == LAYOUT_DIRECTION_LTR) { |
| 8834 | final int leftDifference = holder1.mLocation.left - holder2.mLocation.left; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8835 | if (leftDifference != 0) { |
| 8836 | return leftDifference; |
| 8837 | } |
| 8838 | } else { // RTL |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8839 | final int rightDifference = holder1.mLocation.right - holder2.mLocation.right; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8840 | if (rightDifference != 0) { |
| 8841 | return -rightDifference; |
| 8842 | } |
| 8843 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 8844 | // We are ordering left-to-right, top-to-bottom. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8845 | final int topDifference = holder1.mLocation.top - holder2.mLocation.top; |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 8846 | if (topDifference != 0) { |
| 8847 | return topDifference; |
| 8848 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8849 | // Break tie by height. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8850 | final int heightDiference = holder1.mLocation.height() - holder2.mLocation.height(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8851 | if (heightDiference != 0) { |
| 8852 | return -heightDiference; |
| 8853 | } |
| 8854 | // Break tie by width. |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8855 | final int widthDifference = holder1.mLocation.width() - holder2.mLocation.width(); |
| 8856 | if (widthDifference != 0) { |
| 8857 | return -widthDifference; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8858 | } |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8859 | |
| 8860 | // Find a child of each view with different screen bounds. |
| 8861 | final Rect view1Bounds = new Rect(); |
| 8862 | final Rect view2Bounds = new Rect(); |
| 8863 | final Rect tempRect = new Rect(); |
| 8864 | holder1.mView.getBoundsOnScreen(view1Bounds, true); |
| 8865 | holder2.mView.getBoundsOnScreen(view2Bounds, true); |
| 8866 | final View child1 = holder1.mView.findViewByPredicateTraversal((view) -> { |
| 8867 | view.getBoundsOnScreen(tempRect, true); |
| 8868 | return !tempRect.equals(view1Bounds); |
| 8869 | }, null); |
| 8870 | final View child2 = holder2.mView.findViewByPredicateTraversal((view) -> { |
| 8871 | view.getBoundsOnScreen(tempRect, true); |
| 8872 | return !tempRect.equals(view2Bounds); |
| 8873 | }, null); |
| 8874 | |
| 8875 | |
| 8876 | // Compare the children recursively |
| 8877 | if ((child1 != null) && (child2 != null)) { |
| 8878 | final ViewLocationHolder childHolder1 = |
| 8879 | ViewLocationHolder.obtain(holder1.mRoot, child1); |
| 8880 | final ViewLocationHolder childHolder2 = |
| 8881 | ViewLocationHolder.obtain(holder1.mRoot, child2); |
| 8882 | return compareBoundsOfTree(childHolder1, childHolder2); |
| 8883 | } |
| 8884 | |
| 8885 | // If only one has a child, use that one |
| 8886 | if (child1 != null) { |
| 8887 | return 1; |
| 8888 | } |
| 8889 | |
| 8890 | if (child2 != null) { |
| 8891 | return -1; |
| 8892 | } |
| 8893 | |
| 8894 | // Give up |
| 8895 | return 0; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8896 | } |
| 8897 | |
| 8898 | private void init(ViewGroup root, View view) { |
| 8899 | Rect viewLocation = mLocation; |
| 8900 | view.getDrawingRect(viewLocation); |
| 8901 | root.offsetDescendantRectToMyCoords(view, viewLocation); |
| 8902 | mView = view; |
Phil Weaver | 86b3260 | 2018-05-10 18:00:18 -0700 | [diff] [blame] | 8903 | mRoot = root; |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 8904 | mLayoutDirection = root.getLayoutDirection(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8905 | } |
| 8906 | |
| 8907 | private void clear() { |
| 8908 | mView = null; |
Phil Weaver | 6adf3a1 | 2018-08-21 16:08:42 -0700 | [diff] [blame] | 8909 | mRoot = null; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 8910 | mLocation.set(0, 0, 0, 0); |
| 8911 | } |
| 8912 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8913 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 8914 | private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) { |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8915 | if (sDebugLines== null) { |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 8916 | // TODO: This won't work with multiple UI threads in a single process |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8917 | sDebugLines = new float[16]; |
| 8918 | } |
| 8919 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8920 | sDebugLines[0] = x1; |
| 8921 | sDebugLines[1] = y1; |
| 8922 | sDebugLines[2] = x2; |
| 8923 | sDebugLines[3] = y1; |
| 8924 | |
| 8925 | sDebugLines[4] = x2; |
| 8926 | sDebugLines[5] = y1; |
| 8927 | sDebugLines[6] = x2; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 8928 | sDebugLines[7] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8929 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 8930 | sDebugLines[8] = x2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8931 | sDebugLines[9] = y2; |
| 8932 | sDebugLines[10] = x1; |
| 8933 | sDebugLines[11] = y2; |
| 8934 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 8935 | sDebugLines[12] = x1; |
| 8936 | sDebugLines[13] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8937 | sDebugLines[14] = x1; |
| 8938 | sDebugLines[15] = y1; |
| 8939 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 8940 | canvas.drawLines(sDebugLines, paint); |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 8941 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 8942 | |
| 8943 | /** @hide */ |
| 8944 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 8945 | @UnsupportedAppUsage |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 8946 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 8947 | super.encodeProperties(encoder); |
| 8948 | |
| 8949 | encoder.addProperty("focus:descendantFocusability", getDescendantFocusability()); |
| 8950 | encoder.addProperty("drawing:clipChildren", getClipChildren()); |
| 8951 | encoder.addProperty("drawing:clipToPadding", getClipToPadding()); |
| 8952 | encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled()); |
| 8953 | encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache()); |
| 8954 | |
| 8955 | int n = getChildCount(); |
| 8956 | encoder.addProperty("meta:__childCount__", (short)n); |
| 8957 | for (int i = 0; i < n; i++) { |
| 8958 | encoder.addPropertyKey("meta:__child__" + i); |
| 8959 | getChildAt(i).encode(encoder); |
| 8960 | } |
| 8961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8962 | } |